1/*-------------------------------------------------------------------------
4 * Header for src/port/ compatibility functions.
6 * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 *-------------------------------------------------------------------------
19 * Windows has enough specialized port stuff that we push most of it off
21 * Note: Some CYGWIN includes might #define WIN32.
23#if defined(WIN32) && !defined(__CYGWIN__)
27/* socket has a different definition on WIN32 */
31 #define PGINVALID_SOCKET (-1)
35#define PGINVALID_SOCKET INVALID_SOCKET
38/* if platform lacks socklen_t, we assume this will work */
47/* Portable path handling for Unix/Win32 (in path.c) */
54 const char *head,
const char *tail);
78/* common/pgfnames.c */
79extern char **
pgfnames(
const char *path);
82 #define IS_NONWINDOWS_DIR_SEP(ch) ((ch) == '/')
83 #define is_nonwindows_absolute_path(filename) \
85 IS_NONWINDOWS_DIR_SEP((filename)[0]) \
88 #define IS_WINDOWS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
89/* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
90 #define is_windows_absolute_path(filename) \
92 IS_WINDOWS_DIR_SEP((filename)[0]) || \
93 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
94 IS_WINDOWS_DIR_SEP((filename)[2])) \
98 * is_absolute_path and IS_DIR_SEP
100 * By using macros here we avoid needing to include path.c in libpq.
103 #define IS_DIR_SEP(ch) IS_NONWINDOWS_DIR_SEP(ch)
104 #define is_absolute_path(filename) is_nonwindows_absolute_path(filename)
106#define IS_DIR_SEP(ch) IS_WINDOWS_DIR_SEP(ch)
107#define is_absolute_path(filename) is_windows_absolute_path(filename)
111 * This macro provides a centralized list of all errnos that identify
112 * hard failure of a previously-established network connection.
113 * The macro is intended to be used in a switch statement, in the form
114 * "case ALL_CONNECTION_FAILURE_ERRNOS:".
116 * Note: this groups EPIPE and ECONNRESET, which we take to indicate a
117 * probable server crash, with other errors that indicate loss of network
118 * connectivity without proving much about the server's state. Places that
119 * are actually reporting errors typically single out EPIPE and ECONNRESET,
120 * while allowing the network failures to be reported generically.
122 #define ALL_CONNECTION_FAILURE_ERRNOS \
133/* Portable locale initialization (in exec.c) */
136/* Portable way to find and execute binaries (in exec.c) */
140 const char *versionstr,
char *retpath);
143/* Doesn't belong here, but this is used with find_other_exec(), so... */
144 #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
147/* Disable ASLR before exec, for developer builds only (in exec.c) */
148extern int pg_disable_aslr(
void);
152#if defined(WIN32) || defined(__CYGWIN__)
158#if defined(WIN32) && !defined(__CYGWIN__)
161 #define DEVNULL "/dev/null"
164/* Portable delay handling */
167/* Portable SQL-like case-independent comparisons and conversions */
170extern unsigned char pg_toupper(
unsigned char ch);
171extern unsigned char pg_tolower(
unsigned char ch);
176 * Beginning in v12, we always replace snprintf() and friends with our own
177 * implementation. This symbol is no longer consulted by the core code,
178 * but keep it defined anyway in case any extensions are looking at it.
180 #define USE_REPL_SNPRINTF 1
183 * Versions of libintl >= 0.13 try to replace printf() and friends with
184 * macros to their own versions that understand the %$ format. We do the
185 * same, so disable their macros, if they exist.
223 * We add a pg_ prefix as a warning that the Windows implementations have the
224 * non-standard side-effect of changing the current file position.
226 #define pg_pread pread
227 #define pg_pwrite pwrite
231 * We use __VA_ARGS__ for printf to prevent replacing references to
232 * the "printf" format archetype in format() attribute declarations.
233 * That unfortunately means that taking a function pointer to printf
234 * will not do what we'd wish. (If you need to do that, you must name
235 * pg_printf explicitly.) For printf's sibling functions, use
236 * parameterless macros so that function pointers will work unsurprisingly.
238 #define vsnprintf pg_vsnprintf
239 #define snprintf pg_snprintf
240 #define vsprintf pg_vsprintf
241 #define sprintf pg_sprintf
242 #define vfprintf pg_vfprintf
243 #define fprintf pg_fprintf
244 #define vprintf pg_vprintf
245 #define printf(...) pg_printf(__VA_ARGS__)
247/* This is also provided by snprintf.c */
250/* Replace strerror() with our own, somewhat more robust wrapper */
252 #define strerror pg_strerror
254/* Likewise for strerror_r(); note we prefer the GNU API for that */
256 #define strerror_r pg_strerror_r
257 #define PG_STRERROR_R_BUFLEN 256 /* Recommended buffer size for strerror_r */
259/* Wrap strsignal(), or provide our own version if necessary */
264/* Global variable holding time zone information. */
265#if defined(WIN32) || defined(__CYGWIN__)
266#define TIMEZONE_GLOBAL _timezone
267#define TZNAME_GLOBAL _tzname
269 #define TIMEZONE_GLOBAL timezone
270 #define TZNAME_GLOBAL tzname
273#if defined(WIN32) || defined(__CYGWIN__)
275 * Win32 doesn't have reliable rename/unlink during concurrent access.
277extern int pgrename(
const char *from,
const char *to);
278extern int pgunlink(
const char *path);
280/* Include this first so later includes don't see these defines */
285#define rename(from, to) pgrename(from, to)
286#define unlink(path) pgunlink(path)
287#endif /* defined(WIN32) || defined(__CYGWIN__) */
290 * Win32 also doesn't have symlinks, but we can emulate them with
291 * junction points on newer Win32 versions.
293 * Cygwin has its own symlinks which work on Win95/98/ME where
294 * junction points don't, so use those instead. We have no way of
295 * knowing what type of system Cygwin binaries will be run on.
296 * Note: Some CYGWIN includes might #define WIN32.
298#if defined(WIN32) && !defined(__CYGWIN__)
299extern int pgsymlink(
const char *oldpath,
const char *newpath);
300extern int pgreadlink(
const char *path,
char *
buf,
size_t size);
302#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
303#define readlink(path, buf, size) pgreadlink(path, buf, size)
306extern bool rmtree(
const char *path,
bool rmtopdir);
308#if defined(WIN32) && !defined(__CYGWIN__)
311 * We want the 64-bit variant of lseek().
313 * For Visual Studio, this must be after <io.h> to avoid messing up its
314 * lseek() and _lseeki64() function declarations.
316 * For MinGW there is already a macro, so we have to undefine it (depending on
317 * _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
318 * count on that being set).
321#define lseek(a,b,c) _lseeki64((a),(b),(c))
324 * We want the 64-bit variant of chsize(). It sets errno and also returns it,
325 * so convert non-zero result to -1 to match POSIX.
327 * Prevent MinGW from declaring functions, and undefine its macro before we
331#define FTRUNCATE_DEFINED
335#define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1)
338 * open() and fopen() replacements to allow deletion of open files and
339 * passing of other special options.
341#define O_DIRECT 0x80000000
342extern HANDLE pgwin32_open_handle(
const char *,
int,
bool);
343extern int pgwin32_open(
const char *,
int,...);
344extern FILE *pgwin32_fopen(
const char *,
const char *);
345#define open(a,b,c) pgwin32_open(a,b,c)
346#define fopen(a,b) pgwin32_fopen(a,b)
349 * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
350 * to use our popen wrapper, rather than plain _popen, so override that. For
351 * consistency, use our version of pclose, too.
361 * system() and popen() replacements to enclose the command in an extra
364extern int pgwin32_system(
const char *command);
365extern FILE *pgwin32_popen(
const char *command,
const char *
type);
367#define system(a) pgwin32_system(a)
368#define popen(a,b) pgwin32_popen(a,b)
369#define pclose(a) _pclose(a)
374 * Win32 requires a special close for sockets and pipes, while on Unix
375 * close() does them all.
377 #define closesocket close
381 * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
382 * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
383 * crashes outright if "parameter validation" is enabled. Therefore, in
384 * places where we'd like to select line-buffered mode, we fall back to
385 * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
386 * directly in order to implement this behavior.
389 #define PG_IOLBF _IOLBF
391#define PG_IOLBF _IONBF
395 * Default "extern" declarations or macro substitutes for library routines.
396 * When necessary, these routines are provided by files in src/port/.
399/* Type to use with fseeko/ftello */
400#ifndef WIN32 /* WIN32 is handled in port/win32_port.h */
401 #define pgoff_t off_t
404#ifndef HAVE_GETPEEREID
405/* On Windows, Perl might have incompatible definitions of uid_t and gid_t. */
406#ifndef PLPERL_HAVE_UID_GID
412 * Glibc doesn't use the builtin for clang due to a *gcc* bug in a version
413 * newer than the gcc compatibility clang claims to have. This would cause a
414 * *lot* of superfluous function calls, therefore revert when using clang. In
415 * C++ there's issues with libc++ (not libstdc++), so disable as well.
417#if defined(__clang__) && !defined(__cplusplus)
418/* needs to be separate to not confuse other compilers */
419#if __has_builtin(__builtin_isinf)
420/* need to include before, to avoid getting overwritten */
423#define isinf __builtin_isinf
424#endif /* __has_builtin(isinf) */
425#endif /* __clang__ && !__cplusplus */
427#ifndef HAVE_EXPLICIT_BZERO
431#ifdef HAVE_BUGGY_STRTOF
432extern float pg_strtof(
const char *nptr,
char **endptr);
433#define strtof(a,b) (pg_strtof((a),(b)))
437/* src/port/win32link.c */
438extern int link(
const char *src,
const char *dst);
442extern char *
mkdtemp(
char *path);
445#ifndef HAVE_INET_ATON
448extern int inet_aton(
const char *cp,
struct in_addr *addr);
451#if !HAVE_DECL_STRLCAT
452extern size_t strlcat(
char *dst,
const char *src,
size_t siz);
455#if !HAVE_DECL_STRLCPY
456extern size_t strlcpy(
char *dst,
const char *src,
size_t siz);
459#if !HAVE_DECL_STRNLEN
460extern size_t strnlen(
const char *
str,
size_t maxlen);
464extern char *
strsep(
char **stringp,
const char *delim);
467#if !HAVE_DECL_TIMINGSAFE_BCMP
472 * Callers should use the qsort() macro defined below instead of calling
473 * pg_qsort() directly.
475 extern void pg_qsort(
void *base,
size_t nel,
size_t elsize,
476 int (*
cmp) (
const void *,
const void *));
479 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
483 extern void qsort_arg(
void *base,
size_t nel,
size_t elsize,
490 size_t nmemb,
size_t size,
491 int (*compar) (
const void *,
const void *,
void *),
494/* port/pg_localeconv_r.c */
500/* port/chklocale.c */
503#if defined(WIN32) && !defined(FRONTEND)
504extern int pg_codepage_to_encoding(UINT cp);
507/* port/inet_net_ntop.c */
509 char *dst,
size_t size);
511/* port/pg_strong_random.c */
516 * pg_backend_random used to be a wrapper for pg_strong_random before
517 * Postgres 12 for the backend code.
519 #define pg_backend_random pg_strong_random
521/* port/pgcheckdir.c */
527/* port/pqsignal.c (see also interfaces/libpq/legacy-pqsignal.c) */
529#define pqsignal pqsignal_fe
531 #define pqsignal pqsignal_be
539/* common/wait_error.c */
546 * Interfaces that we assume all Unix system have. We retain individual macros
547 * for better documentation.
549 * For symlink-related functions, there is often no need to test these macros,
550 * because we provided basic support on Windows that can work with absolute
551 * paths to directories. Code that wants to test for complete symlink support
552 * (including relative paths and non-directories) should be conditional on
553 * HAVE_READLINK or HAVE_SYMLINK.
556 #define HAVE_GETRLIMIT 1
558 #define HAVE_POLL_H 1
559 #define HAVE_READLINK 1
560 #define HAVE_SETSID 1
561 #define HAVE_SHM_OPEN 1
562 #define HAVE_SYMLINK 1
565#endif /* PG_PORT_H */
#define pg_attribute_printf(f, a)
char my_exec_path[MAXPGPATH]
static char * lc_monetary
void cleanup_path(char *path)
void get_share_path(const char *my_exec_path, char *ret_path)
char * wait_result_to_str(int exitstatus)
int find_my_exec(const char *argv0, char *retpath)
char * strsep(char **stringp, const char *delim)
int int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
void get_pkglib_path(const char *my_exec_path, char *ret_path)
void get_locale_path(const char *my_exec_path, char *ret_path)
int wait_result_to_exit_code(int exit_status)
void join_path_components(char *ret_path, const char *head, const char *tail)
int int int int int int int pg_vprintf(const char *fmt, va_list args) pg_attribute_printf(1
void get_man_path(const char *my_exec_path, char *ret_path)
void pg_usleep(long microsec)
int pg_mkdir_p(char *path, int omode)
bool get_home_path(char *ret_path)
void get_include_path(const char *my_exec_path, char *ret_path)
int pg_strfromd(char *str, size_t count, int precision, double value)
void(* pqsigfunc)(SIGNAL_ARGS)
bool pg_strong_random(void *buf, size_t len)
bool path_is_prefix_of_path(const char *path1, const char *path2)
int validate_exec(const char *path)
char * last_dir_separator(const char *filename)
char * pipe_read_line(char *cmd)
int pg_strcasecmp(const char *s1, const char *s2)
bool wait_result_is_signal(int exit_status, int signum)
void pgfnames_cleanup(char **filenames)
void * bsearch_arg(const void *key, const void *base0, size_t nmemb, size_t size, int(*compar)(const void *, const void *, void *), void *arg)
char * escape_single_quotes_ascii(const char *src)
bool wait_result_is_any_signal(int exit_status, bool include_command_not_found)
bool path_is_relative_and_below_cwd(const char *path)
char * pg_inet_net_ntop(int af, const void *src, int bits, char *dst, size_t size)
void get_lib_path(const char *my_exec_path, char *ret_path)
const char * pg_strsignal(int signum)
char * first_path_var_separator(const char *pathlist)
char * pg_strerror_r(int errnum, char *buf, size_t buflen)
int(* qsort_arg_comparator)(const void *a, const void *b, void *arg)
int int int int int int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2
void canonicalize_path(char *path)
int pg_qsort_strcmp(const void *a, const void *b)
void get_parent_directory(char *path)
int pg_check_dir(const char *dir)
bool pg_set_noblock(pgsocket sock)
bool pg_set_block(pgsocket sock)
unsigned char pg_toupper(unsigned char ch)
int pg_localeconv_r(const char *lc_monetary, const char *lc_numeric, struct lconv *output)
void explicit_bzero(void *buf, size_t len)
int int int int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2
void get_etc_path(const char *my_exec_path, char *ret_path)
void canonicalize_path_enc(char *path, int encoding)
int pclose_check(FILE *stream)
void qsort_arg(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
void set_pglocale_pgservice(const char *argv0, const char *app)
void pg_strong_random_init(void)
int inet_aton(const char *cp, struct in_addr *addr)
bool path_contains_parent_reference(const char *path)
void make_native_path(char *filename)
char * make_absolute_path(const char *path)
unsigned char pg_tolower(unsigned char ch)
char * mkdtemp(char *path)
const char * get_progname(const char *argv0)
char * first_dir_separator(const char *filename)
bool rmtree(const char *path, bool rmtopdir)
void get_doc_path(const char *my_exec_path, char *ret_path)
int find_other_exec(const char *argv0, const char *target, const char *versionstr, char *retpath)
unsigned char pg_ascii_tolower(unsigned char ch)
int timingsafe_bcmp(const void *b1, const void *b2, size_t len)
int pg_get_encoding_from_locale(const char *ctype, bool write_message)
char * pg_strerror(int errnum)
int int int int int int int int pg_printf(const char *fmt,...) pg_attribute_printf(1
unsigned char pg_ascii_toupper(unsigned char ch)
void pg_qsort(void *base, size_t nel, size_t elsize, int(*cmp)(const void *, const void *))
void get_html_path(const char *my_exec_path, char *ret_path)
void get_pkginclude_path(const char *my_exec_path, char *ret_path)
int int int int int pg_vfprintf(FILE *stream, const char *fmt, va_list args) pg_attribute_printf(2
int int int pg_vsprintf(char *str, const char *fmt, va_list args) pg_attribute_printf(2
size_t strnlen(const char *str, size_t maxlen)
void qsort_interruptible(void *base, size_t nel, size_t elsize, qsort_arg_comparator cmp, void *arg)
int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args) pg_attribute_printf(3
size_t strlcat(char *dst, const char *src, size_t siz)
bool has_drive_prefix(const char *path)
size_t strlcpy(char *dst, const char *src, size_t siz)
int pg_strncasecmp(const char *s1, const char *s2, size_t n)
void get_includeserver_path(const char *my_exec_path, char *ret_path)
char ** pgfnames(const char *path)
void pg_localeconv_free(struct lconv *lconv)
int getpeereid(int sock, uid_t *uid, gid_t *gid)
static int cmp(const chr *x, const chr *y, size_t len)
float pg_strtof(const char *nptr, char **endptr)
int pgreadlink(const char *path, char *buf, size_t size)
int pgsymlink(const char *oldpath, const char *newpath)
int link(const char *src, const char *dst)