1 /*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
21 * Windows has enough specialized port stuff that we push most of it off
23 * Note: Some CYGWIN includes might #define WIN32.
25 #if defined(WIN32) && !defined(__CYGWIN__)
26 #include "port/win32_port.h"
29 /* socket has a different definition on WIN32 */
33 #define PGINVALID_SOCKET (-1)
35 typedef SOCKET pgsocket
;
37 #define PGINVALID_SOCKET INVALID_SOCKET
41 extern bool pg_set_noblock(pgsocket sock
);
42 extern bool pg_set_block(pgsocket sock
);
44 /* Portable path handling for Unix/Win32 (in path.c) */
46 extern bool has_drive_prefix(const char *filename
);
47 extern char *first_dir_separator(const char *filename
);
48 extern char *last_dir_separator(const char *filename
);
49 extern char *first_path_var_separator(const char *pathlist
);
50 extern void join_path_components(char *ret_path
,
51 const char *head
, const char *tail
);
52 extern void canonicalize_path(char *path
);
53 extern void make_native_path(char *path
);
54 extern void cleanup_path(char *path
);
55 extern bool path_contains_parent_reference(const char *path
);
56 extern bool path_is_relative_and_below_cwd(const char *path
);
57 extern bool path_is_prefix_of_path(const char *path1
, const char *path2
);
58 extern char *make_absolute_path(const char *path
);
59 extern const char *get_progname(const char *argv0
);
60 extern void get_share_path(const char *my_exec_path
, char *ret_path
);
61 extern void get_etc_path(const char *my_exec_path
, char *ret_path
);
62 extern void get_include_path(const char *my_exec_path
, char *ret_path
);
63 extern void get_pkginclude_path(const char *my_exec_path
, char *ret_path
);
64 extern void get_includeserver_path(const char *my_exec_path
, char *ret_path
);
65 extern void get_lib_path(const char *my_exec_path
, char *ret_path
);
66 extern void get_pkglib_path(const char *my_exec_path
, char *ret_path
);
67 extern void get_locale_path(const char *my_exec_path
, char *ret_path
);
68 extern void get_doc_path(const char *my_exec_path
, char *ret_path
);
69 extern void get_html_path(const char *my_exec_path
, char *ret_path
);
70 extern void get_man_path(const char *my_exec_path
, char *ret_path
);
71 extern bool get_home_path(char *ret_path
);
72 extern void get_parent_directory(char *path
);
74 /* common/pgfnames.c */
75 extern char **pgfnames(const char *path
);
76 extern void pgfnames_cleanup(char **filenames
);
81 * By making this a macro we avoid needing to include path.c in libpq.
84 #define IS_DIR_SEP(ch) ((ch) == '/')
86 #define is_absolute_path(filename) \
88 IS_DIR_SEP((filename)[0]) \
91 #define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
93 /* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
94 #define is_absolute_path(filename) \
96 IS_DIR_SEP((filename)[0]) || \
97 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
98 IS_DIR_SEP((filename)[2])) \
103 * This macro provides a centralized list of all errnos that identify
104 * hard failure of a previously-established network connection.
105 * The macro is intended to be used in a switch statement, in the form
106 * "case ALL_CONNECTION_FAILURE_ERRNOS:".
108 * Note: this groups EPIPE and ECONNRESET, which we take to indicate a
109 * probable server crash, with other errors that indicate loss of network
110 * connectivity without proving much about the server's state. Places that
111 * are actually reporting errors typically single out EPIPE and ECONNRESET,
112 * while allowing the network failures to be reported generically.
114 #define ALL_CONNECTION_FAILURE_ERRNOS \
124 /* Portable locale initialization (in exec.c) */
125 extern void set_pglocale_pgservice(const char *argv0
, const char *app
);
127 /* Portable way to find and execute binaries (in exec.c) */
128 extern int validate_exec(const char *path
);
129 extern int find_my_exec(const char *argv0
, char *retpath
);
130 extern int find_other_exec(const char *argv0
, const char *target
,
131 const char *versionstr
, char *retpath
);
132 extern char *pipe_read_line(char *cmd
, char *line
, int maxsize
);
134 /* Doesn't belong here, but this is used with find_other_exec(), so... */
135 #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
138 #if defined(WIN32) || defined(__CYGWIN__)
144 #if defined(WIN32) && !defined(__CYGWIN__)
145 #define DEVNULL "nul"
147 #define DEVNULL "/dev/null"
150 /* Portable delay handling */
151 extern void pg_usleep(long microsec
);
153 /* Portable SQL-like case-independent comparisons and conversions */
154 extern int pg_strcasecmp(const char *s1
, const char *s2
);
155 extern int pg_strncasecmp(const char *s1
, const char *s2
, size_t n
);
156 extern unsigned char pg_toupper(unsigned char ch
);
157 extern unsigned char pg_tolower(unsigned char ch
);
158 extern unsigned char pg_ascii_toupper(unsigned char ch
);
159 extern unsigned char pg_ascii_tolower(unsigned char ch
);
162 * Beginning in v12, we always replace snprintf() and friends with our own
163 * implementation. This symbol is no longer consulted by the core code,
164 * but keep it defined anyway in case any extensions are looking at it.
166 #define USE_REPL_SNPRINTF 1
169 * Versions of libintl >= 0.13 try to replace printf() and friends with
170 * macros to their own versions that understand the %$ format. We do the
171 * same, so disable their macros, if they exist.
198 extern int pg_vsnprintf(char *str
, size_t count
, const char *fmt
, va_list args
);
199 extern int pg_snprintf(char *str
, size_t count
, const char *fmt
,...) pg_attribute_printf(3, 4);
200 extern int pg_vsprintf(char *str
, const char *fmt
, va_list args
);
201 extern int pg_sprintf(char *str
, const char *fmt
,...) pg_attribute_printf(2, 3);
202 extern int pg_vfprintf(FILE *stream
, const char *fmt
, va_list args
);
203 extern int pg_fprintf(FILE *stream
, const char *fmt
,...) pg_attribute_printf(2, 3);
204 extern int pg_vprintf(const char *fmt
, va_list args
);
205 extern int pg_printf(const char *fmt
,...) pg_attribute_printf(1, 2);
208 * We use __VA_ARGS__ for printf to prevent replacing references to
209 * the "printf" format archetype in format() attribute declarations.
210 * That unfortunately means that taking a function pointer to printf
211 * will not do what we'd wish. (If you need to do that, you must name
212 * pg_printf explicitly.) For printf's sibling functions, use
213 * parameterless macros so that function pointers will work unsurprisingly.
215 #define vsnprintf pg_vsnprintf
216 #define snprintf pg_snprintf
217 #define vsprintf pg_vsprintf
218 #define sprintf pg_sprintf
219 #define vfprintf pg_vfprintf
220 #define fprintf pg_fprintf
221 #define vprintf pg_vprintf
222 #define printf(...) pg_printf(__VA_ARGS__)
224 /* This is also provided by snprintf.c */
225 extern int pg_strfromd(char *str
, size_t count
, int precision
, double value
);
227 /* Replace strerror() with our own, somewhat more robust wrapper */
228 extern char *pg_strerror(int errnum
);
229 #define strerror pg_strerror
231 /* Likewise for strerror_r(); note we prefer the GNU API for that */
232 extern char *pg_strerror_r(int errnum
, char *buf
, size_t buflen
);
233 #define strerror_r pg_strerror_r
234 #define PG_STRERROR_R_BUFLEN 256 /* Recommended buffer size for strerror_r */
236 /* Wrap strsignal(), or provide our own version if necessary */
237 extern const char *pg_strsignal(int signum
);
239 extern int pclose_check(FILE *stream
);
241 /* Global variable holding time zone information. */
242 #if defined(WIN32) || defined(__CYGWIN__)
243 #define TIMEZONE_GLOBAL _timezone
244 #define TZNAME_GLOBAL _tzname
246 #define TIMEZONE_GLOBAL timezone
247 #define TZNAME_GLOBAL tzname
250 #if defined(WIN32) || defined(__CYGWIN__)
252 * Win32 doesn't have reliable rename/unlink during concurrent access.
254 extern int pgrename(const char *from
, const char *to
);
255 extern int pgunlink(const char *path
);
257 /* Include this first so later includes don't see these defines */
262 #define rename(from, to) pgrename(from, to)
263 #define unlink(path) pgunlink(path)
264 #endif /* defined(WIN32) || defined(__CYGWIN__) */
267 * Win32 also doesn't have symlinks, but we can emulate them with
268 * junction points on newer Win32 versions.
270 * Cygwin has its own symlinks which work on Win95/98/ME where
271 * junction points don't, so use those instead. We have no way of
272 * knowing what type of system Cygwin binaries will be run on.
273 * Note: Some CYGWIN includes might #define WIN32.
275 #if defined(WIN32) && !defined(__CYGWIN__)
276 extern int pgsymlink(const char *oldpath
, const char *newpath
);
277 extern int pgreadlink(const char *path
, char *buf
, size_t size
);
278 extern bool pgwin32_is_junction(const char *path
);
280 #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
281 #define readlink(path, buf, size) pgreadlink(path, buf, size)
284 extern bool rmtree(const char *path
, bool rmtopdir
);
286 #if defined(WIN32) && !defined(__CYGWIN__)
289 * open() and fopen() replacements to allow deletion of open files and
290 * passing of other special options.
292 #define O_DIRECT 0x80000000
293 extern int pgwin32_open(const char *, int,...);
294 extern FILE *pgwin32_fopen(const char *, const char *);
295 #define open(a,b,c) pgwin32_open(a,b,c)
296 #define fopen(a,b) pgwin32_fopen(a,b)
299 * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
300 * to use our popen wrapper, rather than plain _popen, so override that. For
301 * consistency, use our version of pclose, too.
311 * system() and popen() replacements to enclose the command in an extra
314 extern int pgwin32_system(const char *command
);
315 extern FILE *pgwin32_popen(const char *command
, const char *type
);
317 #define system(a) pgwin32_system(a)
318 #define popen(a,b) pgwin32_popen(a,b)
319 #define pclose(a) _pclose(a)
321 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
322 #ifndef HAVE_GETTIMEOFDAY
323 /* Last parameter not used */
324 extern int gettimeofday(struct timeval
*tp
, struct timezone
*tzp
);
329 * Win32 requires a special close for sockets and pipes, while on Unix
330 * close() does them all.
332 #define closesocket close
336 * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
337 * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
338 * crashes outright if "parameter validation" is enabled. Therefore, in
339 * places where we'd like to select line-buffered mode, we fall back to
340 * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
341 * directly in order to implement this behavior.
344 #define PG_IOLBF _IOLBF
346 #define PG_IOLBF _IONBF
350 * Default "extern" declarations or macro substitutes for library routines.
351 * When necessary, these routines are provided by files in src/port/.
354 /* Type to use with fseeko/ftello */
355 #ifndef WIN32 /* WIN32 is handled in port/win32_port.h */
356 #define pgoff_t off_t
359 extern double pg_erand48(unsigned short xseed
[3]);
360 extern long pg_lrand48(void);
361 extern long pg_jrand48(unsigned short xseed
[3]);
362 extern void pg_srand48(long seed
);
365 extern int fls(int mask
);
368 #ifndef HAVE_GETPEEREID
369 /* On Windows, Perl might have incompatible definitions of uid_t and gid_t. */
370 #ifndef PLPERL_HAVE_UID_GID
371 extern int getpeereid(int sock
, uid_t
*uid
, gid_t
*gid
);
376 * Glibc doesn't use the builtin for clang due to a *gcc* bug in a version
377 * newer than the gcc compatibility clang claims to have. This would cause a
378 * *lot* of superfluous function calls, therefore revert when using clang. In
379 * C++ there's issues with libc++ (not libstdc++), so disable as well.
381 #if defined(__clang__) && !defined(__cplusplus)
382 /* needs to be separate to not confuse other compilers */
383 #if __has_builtin(__builtin_isinf)
384 /* need to include before, to avoid getting overwritten */
387 #define isinf __builtin_isinf
388 #endif /* __has_builtin(isinf) */
389 #endif /* __clang__ && !__cplusplus */
391 #ifndef HAVE_EXPLICIT_BZERO
392 extern void explicit_bzero(void *buf
, size_t len
);
396 extern float strtof(const char *nptr
, char **endptr
);
399 #ifdef HAVE_BUGGY_STRTOF
400 extern float pg_strtof(const char *nptr
, char **endptr
);
401 #define strtof(a,b) (pg_strtof((a),(b)))
405 extern int link(const char *src
, const char *dst
);
409 extern char *mkdtemp(char *path
);
412 #ifndef HAVE_INET_ATON
413 #include <netinet/in.h>
414 #include <arpa/inet.h>
415 extern int inet_aton(const char *cp
, struct in_addr
*addr
);
419 * Windows and older Unix don't have pread(2) and pwrite(2). We have
420 * replacement functions, but they have slightly different semantics so we'll
421 * use a name with a pg_ prefix to avoid confusion.
424 #define pg_pread pread
426 extern ssize_t
pg_pread(int fd
, void *buf
, size_t nbyte
, off_t offset
);
430 #define pg_pwrite pwrite
432 extern ssize_t
pg_pwrite(int fd
, const void *buf
, size_t nbyte
, off_t offset
);
435 /* For pg_pwritev() and pg_preadv(), see port/pg_iovec.h. */
437 #if !HAVE_DECL_STRLCAT
438 extern size_t strlcat(char *dst
, const char *src
, size_t siz
);
441 #if !HAVE_DECL_STRLCPY
442 extern size_t strlcpy(char *dst
, const char *src
, size_t siz
);
445 #if !HAVE_DECL_STRNLEN
446 extern size_t strnlen(const char *str
, size_t maxlen
);
449 #if !defined(HAVE_RANDOM)
450 extern long random(void);
454 extern int setenv(const char *name
, const char *value
, int overwrite
);
457 #ifndef HAVE_UNSETENV
458 extern int unsetenv(const char *name
);
462 extern void srandom(unsigned int seed
);
466 extern void *dlopen(const char *file
, int mode
);
467 extern void *dlsym(void *handle
, const char *symbol
);
468 extern int dlclose(void *handle
);
469 extern char *dlerror(void);
473 * In some older systems, the RTLD_NOW flag isn't defined and the mode
474 * argument to dlopen must always be 1.
476 #if !HAVE_DECL_RTLD_NOW
481 * The RTLD_GLOBAL flag is wanted if available, but it doesn't exist
482 * everywhere. If it doesn't exist, set it to 0 so it has no effect.
484 #if !HAVE_DECL_RTLD_GLOBAL
485 #define RTLD_GLOBAL 0
490 extern int pqGetpwuid(uid_t uid
, struct passwd
*resultbuf
, char *buffer
,
491 size_t buflen
, struct passwd
**result
);
494 extern int pqGethostbyname(const char *name
,
495 struct hostent
*resultbuf
,
496 char *buffer
, size_t buflen
,
497 struct hostent
**result
,
500 extern void pg_qsort(void *base
, size_t nel
, size_t elsize
,
501 int (*cmp
) (const void *, const void *));
502 extern int pg_qsort_strcmp(const void *a
, const void *b
);
504 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
506 typedef int (*qsort_arg_comparator
) (const void *a
, const void *b
, void *arg
);
508 extern void qsort_arg(void *base
, size_t nel
, size_t elsize
,
509 qsort_arg_comparator cmp
, void *arg
);
511 extern void *bsearch_arg(const void *key
, const void *base
,
512 size_t nmemb
, size_t size
,
513 int (*compar
) (const void *, const void *, void *),
516 /* port/chklocale.c */
517 extern int pg_get_encoding_from_locale(const char *ctype
, bool write_message
);
519 #if defined(WIN32) && !defined(FRONTEND)
520 extern int pg_codepage_to_encoding(UINT cp
);
523 /* port/inet_net_ntop.c */
524 extern char *pg_inet_net_ntop(int af
, const void *src
, int bits
,
525 char *dst
, size_t size
);
527 /* port/pg_strong_random.c */
528 extern void pg_strong_random_init(void);
529 extern bool pg_strong_random(void *buf
, size_t len
);
532 * pg_backend_random used to be a wrapper for pg_strong_random before
533 * Postgres 12 for the backend code.
535 #define pg_backend_random pg_strong_random
537 /* port/pgcheckdir.c */
538 extern int pg_check_dir(const char *dir
);
540 /* port/pgmkdirp.c */
541 extern int pg_mkdir_p(char *path
, int omode
);
543 /* port/pqsignal.c */
544 typedef void (*pqsigfunc
) (int signo
);
545 extern pqsigfunc
pqsignal(int signo
, pqsigfunc func
);
548 extern char *escape_single_quotes_ascii(const char *src
);
550 /* common/wait_error.c */
551 extern char *wait_result_to_str(int exit_status
);
552 extern bool wait_result_is_signal(int exit_status
, int signum
);
553 extern bool wait_result_is_any_signal(int exit_status
, bool include_command_not_found
);
555 #endif /* PG_PORT_H */