postgresql/src/include/port.h

508 lines
15 KiB
C
Raw Normal View History

/*-------------------------------------------------------------------------
*
* port.h
* Header for src/port/ compatibility functions.
*
* Portions Copyright (c) 1996-2015, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California
*
2010-09-20 22:08:53 +02:00
* src/include/port.h
*
*-------------------------------------------------------------------------
*/
#ifndef PG_PORT_H
#define PG_PORT_H
#include <ctype.h>
#include <netdb.h>
#include <pwd.h>
/* socket has a different definition on WIN32 */
#ifndef WIN32
typedef int pgsocket;
2010-02-26 03:01:40 +01:00
2010-01-31 18:35:46 +01:00
#define PGINVALID_SOCKET (-1)
#else
typedef SOCKET pgsocket;
2010-02-26 03:01:40 +01:00
#define PGINVALID_SOCKET INVALID_SOCKET
#endif
/* non-blocking */
extern bool pg_set_noblock(pgsocket sock);
extern bool pg_set_block(pgsocket sock);
/* Portable path handling for Unix/Win32 (in path.c) */
extern bool has_drive_prefix(const char *filename);
extern char *first_dir_separator(const char *filename);
extern char *last_dir_separator(const char *filename);
extern char *first_path_var_separator(const char *pathlist);
extern void join_path_components(char *ret_path,
2005-10-15 04:49:52 +02:00
const char *head, const char *tail);
extern void canonicalize_path(char *path);
extern void make_native_path(char *path);
extern bool path_contains_parent_reference(const char *path);
extern bool path_is_relative_and_below_cwd(const char *path);
extern bool path_is_prefix_of_path(const char *path1, const char *path2);
extern char *make_absolute_path(const char *path);
extern const char *get_progname(const char *argv0);
extern void get_share_path(const char *my_exec_path, char *ret_path);
extern void get_etc_path(const char *my_exec_path, char *ret_path);
extern void get_include_path(const char *my_exec_path, char *ret_path);
extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
extern void get_lib_path(const char *my_exec_path, char *ret_path);
extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
extern void get_locale_path(const char *my_exec_path, char *ret_path);
extern void get_doc_path(const char *my_exec_path, char *ret_path);
extern void get_html_path(const char *my_exec_path, char *ret_path);
extern void get_man_path(const char *my_exec_path, char *ret_path);
extern bool get_home_path(char *ret_path);
extern void get_parent_directory(char *path);
/* common/pgfnames.c */
extern char **pgfnames(const char *path);
extern void pgfnames_cleanup(char **filenames);
/*
* is_absolute_path
*
* By making this a macro we avoid needing to include path.c in libpq.
*/
#ifndef WIN32
#define IS_DIR_SEP(ch) ((ch) == '/')
#define is_absolute_path(filename) \
( \
IS_DIR_SEP((filename)[0]) \
)
#else
#define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
/* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
#define is_absolute_path(filename) \
( \
IS_DIR_SEP((filename)[0]) || \
(isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
IS_DIR_SEP((filename)[2])) \
)
#endif
/* Portable locale initialization (in exec.c) */
extern void set_pglocale_pgservice(const char *argv0, const char *app);
/* Portable way to find binaries (in exec.c) */
2004-08-29 07:07:03 +02:00
extern int find_my_exec(const char *argv0, char *retpath);
extern int find_other_exec(const char *argv0, const char *target,
2004-08-29 07:07:03 +02:00
const char *versionstr, char *retpath);
/* Windows security token manipulation (in exec.c) */
#ifdef WIN32
extern BOOL AddUserToTokenDacl(HANDLE hToken);
#endif
#if defined(WIN32) || defined(__CYGWIN__)
#define EXE ".exe"
#else
#define EXE ""
#endif
#if defined(WIN32) && !defined(__CYGWIN__)
#define DEVNULL "nul"
#else
#define DEVNULL "/dev/null"
#endif
/* Portable delay handling */
extern void pg_usleep(long microsec);
/* Portable SQL-like case-independent comparisons and conversions */
extern int pg_strcasecmp(const char *s1, const char *s2);
extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
extern unsigned char pg_toupper(unsigned char ch);
extern unsigned char pg_tolower(unsigned char ch);
extern unsigned char pg_ascii_toupper(unsigned char ch);
extern unsigned char pg_ascii_tolower(unsigned char ch);
/*
* Capture macro-compatible calls to printf() and friends, and redirect them
* to wrappers that throw errors in lieu of reporting failure in a return
* value. Versions of libintl >= 0.13 similarly redirect to versions that
* understand the %$ format, so disable libintl macros first.
*/
#ifdef vsnprintf
#undef vsnprintf
#endif
#ifdef snprintf
#undef snprintf
#endif
#ifdef vsprintf
#undef vsprintf
#endif
#ifdef sprintf
#undef sprintf
#endif
#ifdef vfprintf
#undef vfprintf
#endif
#ifdef fprintf
#undef fprintf
#endif
#ifdef printf
#undef printf
#endif
extern int
vsnprintf_throw_on_fail(char *str, size_t count, const char *fmt, va_list args)
pg_attribute_printf(3, 0);
extern int
snprintf_throw_on_fail(char *str, size_t count, const char *fmt,...)
pg_attribute_printf(3, 4);
extern int
vsprintf_throw_on_fail(char *str, const char *fmt, va_list args)
pg_attribute_printf(2, 0);
extern int
sprintf_throw_on_fail(char *str, const char *fmt,...)
pg_attribute_printf(2, 3);
extern int
vfprintf_throw_on_fail(FILE *stream, const char *fmt, va_list args)
pg_attribute_printf(2, 0);
extern int
fprintf_throw_on_fail(FILE *stream, const char *fmt,...)
pg_attribute_printf(2, 3);
extern int
printf_throw_on_fail(const char *fmt,...)
pg_attribute_printf(1, 2);
/*
* The GCC-specific code below prevents the pg_attribute_printf above from
* being replaced, and this is required because gcc doesn't know anything
* about printf_throw_on_fail.
*/
#ifdef __GNUC__
#define vsnprintf(...) vsnprintf_throw_on_fail(__VA_ARGS__)
#define snprintf(...) snprintf_throw_on_fail(__VA_ARGS__)
#define vsprintf(...) vsprintf_throw_on_fail(__VA_ARGS__)
#define sprintf(...) sprintf_throw_on_fail(__VA_ARGS__)
#define vfprintf(...) vfprintf_throw_on_fail(__VA_ARGS__)
#define fprintf(...) fprintf_throw_on_fail(__VA_ARGS__)
#define printf(...) printf_throw_on_fail(__VA_ARGS__)
#else
#define vsnprintf vsnprintf_throw_on_fail
#define snprintf snprintf_throw_on_fail
#define vsprintf vsprintf_throw_on_fail
#define sprintf sprintf_throw_on_fail
#define vfprintf vfprintf_throw_on_fail
#define fprintf fprintf_throw_on_fail
#define printf printf_throw_on_fail
#endif
#ifdef USE_REPL_SNPRINTF
/* Code outside syswrap.c should not call these. */
extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
extern int pg_vsprintf(char *str, const char *fmt, va_list args);
extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3);
extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
2006-10-04 02:30:14 +02:00
#endif /* USE_REPL_SNPRINTF */
#if defined(WIN32)
/*
* Versions of libintl >= 0.18? try to replace setlocale() with a macro
* to their own versions. Remove the macro, if it exists, because it
* ends up calling the wrong version when the backend and libintl use
* different versions of msvcrt.
*/
#if defined(setlocale)
#undef setlocale
#endif
/*
* Define our own wrapper macro around setlocale() to work around bugs in
* Windows' native setlocale() function.
*/
extern char *pgwin32_setlocale(int category, const char *locale);
#define setlocale(a,b) pgwin32_setlocale(a,b)
#endif /* WIN32 */
/* Portable prompt handling */
extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
2012-03-29 05:24:07 +02:00
#ifdef WIN32
2004-05-27 16:39:33 +02:00
#define PG_SIGNAL_COUNT 32
2004-08-29 07:07:03 +02:00
#define kill(pid,sig) pgkill(pid,sig)
extern int pgkill(int pid, int sig);
#endif
2004-05-27 16:39:33 +02:00
2004-08-29 07:07:03 +02:00
extern int pclose_check(FILE *stream);
/* Global variable holding time zone information. */
#ifndef __CYGWIN__
#define TIMEZONE_GLOBAL timezone
#define TZNAME_GLOBAL tzname
#else
#define TIMEZONE_GLOBAL _timezone
#define TZNAME_GLOBAL _tzname
#endif
#if defined(WIN32) || defined(__CYGWIN__)
/*
* Win32 doesn't have reliable rename/unlink during concurrent access.
*/
extern int pgrename(const char *from, const char *to);
extern int pgunlink(const char *path);
2005-10-15 04:49:52 +02:00
/* Include this first so later includes don't see these defines */
#ifdef WIN32_ONLY_COMPILER
#include <io.h>
#endif
#define rename(from, to) pgrename(from, to)
#define unlink(path) pgunlink(path)
#endif /* defined(WIN32) || defined(__CYGWIN__) */
/*
* Win32 also doesn't have symlinks, but we can emulate them with
* junction points on newer Win32 versions.
*
* Cygwin has its own symlinks which work on Win95/98/ME where
* junction points don't, so use those instead. We have no way of
* knowing what type of system Cygwin binaries will be run on.
2005-10-15 04:49:52 +02:00
* Note: Some CYGWIN includes might #define WIN32.
*/
#if defined(WIN32) && !defined(__CYGWIN__)
extern int pgsymlink(const char *oldpath, const char *newpath);
extern int pgreadlink(const char *path, char *buf, size_t size);
extern bool pgwin32_is_junction(char *path);
2005-10-15 04:49:52 +02:00
#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
#define readlink(path, buf, size) pgreadlink(path, buf, size)
#endif
extern bool rmtree(const char *path, bool rmtopdir);
2004-08-01 08:19:26 +02:00
/*
* stat() is not guaranteed to set the st_size field on win32, so we
* redefine it to our own implementation that is.
*
* We must pull in sys/stat.h here so the system header definition
* goes in first, and we redefine that, and not the other way around.
*
* Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
* is defined we don't bother with this.
*/
#if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
#include <sys/stat.h>
extern int pgwin32_safestat(const char *path, struct stat * buf);
#define stat(a,b) pgwin32_safestat(a,b)
#endif
#if defined(WIN32) && !defined(__CYGWIN__)
/*
* open() and fopen() replacements to allow deletion of open files and
* passing of other special options.
*/
#define O_DIRECT 0x80000000
extern int pgwin32_open(const char *, int,...);
extern FILE *pgwin32_fopen(const char *, const char *);
2004-08-29 07:07:03 +02:00
#ifndef FRONTEND
2006-10-04 02:30:14 +02:00
#define open(a,b,c) pgwin32_open(a,b,c)
#define fopen(a,b) pgwin32_fopen(a,b)
#endif
/*
* Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
* to use our popen wrapper, rather than plain _popen, so override that. For
* consistency, use our version of pclose, too.
*/
#ifdef popen
#undef popen
#endif
#ifdef pclose
#undef pclose
#endif
/*
* system() and popen() replacements to enclose the command in an extra
* pair of quotes.
*/
extern int pgwin32_system(const char *command);
extern FILE *pgwin32_popen(const char *command, const char *type);
#define system(a) pgwin32_system(a)
#define popen(a,b) pgwin32_popen(a,b)
#define pclose(a) _pclose(a)
/* New versions of MingW have gettimeofday, old mingw and msvc don't */
#ifndef HAVE_GETTIMEOFDAY
/* Last parameter not used */
2003-08-04 02:43:34 +02:00
extern int gettimeofday(struct timeval * tp, struct timezone * tzp);
#endif
2005-10-15 04:49:52 +02:00
#else /* !WIN32 */
/*
* Win32 requires a special close for sockets and pipes, while on Unix
* close() does them all.
*/
2003-08-04 02:43:34 +02:00
#define closesocket close
2005-10-15 04:49:52 +02:00
#endif /* WIN32 */
2003-08-04 02:43:34 +02:00
/*
* On Windows, setvbuf() does not support _IOLBF mode, and interprets that
* as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
* crashes outright if "parameter validation" is enabled. Therefore, in
* places where we'd like to select line-buffered mode, we fall back to
* unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
* directly in order to implement this behavior.
*/
#ifndef WIN32
#define PG_IOLBF _IOLBF
#else
#define PG_IOLBF _IONBF
#endif
/*
* Default "extern" declarations or macro substitutes for library routines.
* When necessary, these routines are provided by files in src/port/.
*/
#ifndef HAVE_CRYPT
extern char *crypt(const char *key, const char *setting);
#endif
/* WIN32 handled in port/win32.h */
#ifndef WIN32
#define pgoff_t off_t
#ifdef __NetBSD__
extern int fseeko(FILE *stream, off_t offset, int whence);
extern off_t ftello(FILE *stream);
#endif
#endif
#define RAND48_SEED_0 (0x330e)
#define RAND48_SEED_1 (0xabcd)
#define RAND48_SEED_2 (0x1234)
extern double pg_erand48(unsigned short xseed[3]);
extern long pg_lrand48(void);
extern void pg_srand48(long seed);
#ifndef HAVE_FLS
extern int fls(int mask);
#endif
#ifndef HAVE_FSEEKO
#define fseeko(a, b, c) fseek(a, b, c)
#define ftello(a) ftell(a)
#endif
#if !defined(HAVE_GETPEEREID) && !defined(WIN32)
extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
#endif
#ifndef HAVE_ISINF
2003-08-04 02:43:34 +02:00
extern int isinf(double x);
#endif
#ifndef HAVE_MKDTEMP
extern char *mkdtemp(char *path);
#endif
#ifndef HAVE_RINT
extern double rint(double x);
#endif
#ifndef HAVE_INET_ATON
2003-08-04 02:43:34 +02:00
#include <netinet/in.h>
#include <arpa/inet.h>
extern int inet_aton(const char *cp, struct in_addr * addr);
#endif
#if !HAVE_DECL_STRLCAT
extern size_t strlcat(char *dst, const char *src, size_t siz);
#endif
#if !HAVE_DECL_STRLCPY
extern size_t strlcpy(char *dst, const char *src, size_t siz);
#endif
#if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
extern long random(void);
#endif
#ifndef HAVE_UNSETENV
extern void unsetenv(const char *name);
#endif
#ifndef HAVE_SRANDOM
extern void srandom(unsigned int seed);
#endif
#ifndef HAVE_SSL_GET_CURRENT_COMPRESSION
#define SSL_get_current_compression(x) 0
#endif
/* thread.h */
extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
Fix libpq's behavior when /etc/passwd isn't readable. Some users run their applications in chroot environments that lack an /etc/passwd file. This means that the current UID's user name and home directory are not obtainable. libpq used to be all right with that, so long as the database role name to use was specified explicitly. But commit a4c8f14364c27508233f8a31ac4b10a4c90235a9 broke such cases by causing any failure of pg_fe_getauthname() to be treated as a hard error. In any case it did little to advance its nominal goal of causing errors in pg_fe_getauthname() to be reported better. So revert that and instead put some real error-reporting code in place. This requires changes to the APIs of pg_fe_getauthname() and pqGetpwuid(), since the latter had departed from the POSIX-specified API of getpwuid_r() in a way that made it impossible to distinguish actual lookup errors from "no such user". To allow such failures to be reported, while not failing if the caller supplies a role name, add a second call of pg_fe_getauthname() in connectOptions2(). This is a tad ugly, and could perhaps be avoided with some refactoring of PQsetdbLogin(), but I'll leave that idea for later. (Note that the complained-of misbehavior only occurs in PQsetdbLogin, not when using the PQconnect functions, because in the latter we will never bother to call pg_fe_getauthname() if the user gives a role name.) In passing also clean up the Windows-side usage of GetUserName(): the recommended buffer size is 257 bytes, the passed buffer length should be the buffer size not buffer size less 1, and any error is reported by GetLastError() not errno. Per report from Christoph Berg. Back-patch to 9.4 where the chroot failure case was introduced. The generally poor reporting of errors here is of very long standing, of course, but given the lack of field complaints about it we won't risk changing these APIs further back (even though they're theoretically internal to libpq).
2015-01-11 18:35:44 +01:00
#ifndef WIN32
2003-08-04 02:43:34 +02:00
extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
2004-08-29 07:07:03 +02:00
size_t buflen, struct passwd ** result);
#endif
extern int pqGethostbyname(const char *name,
2004-08-29 07:07:03 +02:00
struct hostent * resultbuf,
char *buffer, size_t buflen,
2004-08-29 07:07:03 +02:00
struct hostent ** result,
2003-08-04 02:43:34 +02:00
int *herrno);
extern void pg_qsort(void *base, size_t nel, size_t elsize,
2007-11-15 22:14:46 +01:00
int (*cmp) (const void *, const void *));
extern int pg_qsort_strcmp(const void *a, const void *b);
#define qsort(a,b,c,d) pg_qsort(a,b,c,d)
typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
extern void qsort_arg(void *base, size_t nel, size_t elsize,
2006-10-04 02:30:14 +02:00
qsort_arg_comparator cmp, void *arg);
/* port/chklocale.c */
extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
Renovate display of non-ASCII messages on Windows. GNU gettext selects a default encoding for the messages it emits in a platform-specific manner; it uses the Windows ANSI code page on Windows and follows LC_CTYPE on other platforms. This is inconvenient for PostgreSQL server processes, so realize consistent cross-platform behavior by calling bind_textdomain_codeset() on Windows each time we permanently change LC_CTYPE. This primarily affects SQL_ASCII databases and processes like the postmaster that do not attach to a database, making their behavior consistent with PostgreSQL on non-Windows platforms. Messages from SQL_ASCII databases use the encoding implied by the database LC_CTYPE, and messages from non-database processes use LC_CTYPE from the postmaster system environment. PlatformEncoding becomes unused, so remove it. Make write_console() prefer WriteConsoleW() to write() regardless of the encodings in use. In this situation, write() will invariably mishandle non-ASCII characters. elog.c has assumed that messages conform to the database encoding. While usually true, this does not hold for SQL_ASCII and MULE_INTERNAL. Introduce MessageEncoding to track the actual encoding of message text. The present consumers are Windows-specific code for converting messages to UTF16 for use in system interfaces. This fixes the appearance in Windows event logs and consoles of translated messages from SQL_ASCII processes like the postmaster. Note that SQL_ASCII inherently disclaims a strong notion of encoding, so non-ASCII byte sequences interpolated into messages by %s may yet yield a nonsensical message. MULE_INTERNAL has similar problems at present, albeit for a different reason: its lack of libiconv support or a conversion to UTF8. Consequently, one need no longer restart Windows with a different Windows ANSI code page to broadly test backend logging under a given language. Changing the user's locale ("Format") is enough. Several accounts can simultaneously run postmasters under different locales, all correctly logging localized messages to Windows event logs and consoles. Alexander Law and Noah Misch
2013-06-26 17:17:33 +02:00
#if defined(WIN32) && !defined(FRONTEND)
extern int pg_codepage_to_encoding(UINT cp);
#endif
/* port/inet_net_ntop.c */
extern char *inet_net_ntop(int af, const void *src, int bits,
char *dst, size_t size);
/* port/pgcheckdir.c */
extern int pg_check_dir(const char *dir);
/* port/pgmkdirp.c */
extern int pg_mkdir_p(char *path, int omode);
/* port/pqsignal.c */
typedef void (*pqsigfunc) (int signo);
extern pqsigfunc pqsignal(int signo, pqsigfunc func);
/* port/quotes.c */
extern char *escape_single_quotes_ascii(const char *src);
/* port/wait_error.c */
extern char *wait_result_to_str(int exit_status);
2006-10-04 02:30:14 +02:00
#endif /* PG_PORT_H */