Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * port.h
4 : * Header for src/port/ compatibility functions.
5 : *
6 : * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7 : * Portions Copyright (c) 1994, Regents of the University of California
8 : *
9 : * src/include/port.h
10 : *
11 : *-------------------------------------------------------------------------
12 : */
13 : #ifndef PG_PORT_H
14 : #define PG_PORT_H
15 :
16 : #include <ctype.h>
17 :
18 : /*
19 : * Windows has enough specialized port stuff that we push most of it off
20 : * into another file.
21 : * Note: Some CYGWIN includes might #define WIN32.
22 : */
23 : #if defined(WIN32) && !defined(__CYGWIN__)
24 : #include "port/win32_port.h"
25 : #endif
26 :
27 : /* socket has a different definition on WIN32 */
28 : #ifndef WIN32
29 : typedef int pgsocket;
30 :
31 : #define PGINVALID_SOCKET (-1)
32 : #else
33 : typedef SOCKET pgsocket;
34 :
35 : #define PGINVALID_SOCKET INVALID_SOCKET
36 : #endif
37 :
38 : /* if platform lacks socklen_t, we assume this will work */
39 : #ifndef HAVE_SOCKLEN_T
40 : typedef unsigned int socklen_t;
41 : #endif
42 :
43 : /* non-blocking */
44 : extern bool pg_set_noblock(pgsocket sock);
45 : extern bool pg_set_block(pgsocket sock);
46 :
47 : /* Portable path handling for Unix/Win32 (in path.c) */
48 :
49 : extern bool has_drive_prefix(const char *path);
50 : extern char *first_dir_separator(const char *filename);
51 : extern char *last_dir_separator(const char *filename);
52 : extern char *first_path_var_separator(const char *pathlist);
53 : extern void join_path_components(char *ret_path,
54 : const char *head, const char *tail);
55 : extern void canonicalize_path(char *path);
56 : extern void canonicalize_path_enc(char *path, int encoding);
57 : extern void make_native_path(char *filename);
58 : extern void cleanup_path(char *path);
59 : extern bool path_contains_parent_reference(const char *path);
60 : extern bool path_is_relative_and_below_cwd(const char *path);
61 : extern bool path_is_prefix_of_path(const char *path1, const char *path2);
62 : extern char *make_absolute_path(const char *path);
63 : extern const char *get_progname(const char *argv0);
64 : extern void get_share_path(const char *my_exec_path, char *ret_path);
65 : extern void get_etc_path(const char *my_exec_path, char *ret_path);
66 : extern void get_include_path(const char *my_exec_path, char *ret_path);
67 : extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
68 : extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
69 : extern void get_lib_path(const char *my_exec_path, char *ret_path);
70 : extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
71 : extern void get_locale_path(const char *my_exec_path, char *ret_path);
72 : extern void get_doc_path(const char *my_exec_path, char *ret_path);
73 : extern void get_html_path(const char *my_exec_path, char *ret_path);
74 : extern void get_man_path(const char *my_exec_path, char *ret_path);
75 : extern bool get_home_path(char *ret_path);
76 : extern void get_parent_directory(char *path);
77 :
78 : /* common/pgfnames.c */
79 : extern char **pgfnames(const char *path);
80 : extern void pgfnames_cleanup(char **filenames);
81 :
82 : #define IS_NONWINDOWS_DIR_SEP(ch) ((ch) == '/')
83 : #define is_nonwindows_absolute_path(filename) \
84 : ( \
85 : IS_NONWINDOWS_DIR_SEP((filename)[0]) \
86 : )
87 :
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) \
91 : ( \
92 : IS_WINDOWS_DIR_SEP((filename)[0]) || \
93 : (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
94 : IS_WINDOWS_DIR_SEP((filename)[2])) \
95 : )
96 :
97 : /*
98 : * is_absolute_path and IS_DIR_SEP
99 : *
100 : * By using macros here we avoid needing to include path.c in libpq.
101 : */
102 : #ifndef WIN32
103 : #define IS_DIR_SEP(ch) IS_NONWINDOWS_DIR_SEP(ch)
104 : #define is_absolute_path(filename) is_nonwindows_absolute_path(filename)
105 : #else
106 : #define IS_DIR_SEP(ch) IS_WINDOWS_DIR_SEP(ch)
107 : #define is_absolute_path(filename) is_windows_absolute_path(filename)
108 : #endif
109 :
110 : /*
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:".
115 : *
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.
121 : */
122 : #define ALL_CONNECTION_FAILURE_ERRNOS \
123 : EPIPE: \
124 : case ECONNRESET: \
125 : case ECONNABORTED: \
126 : case EHOSTDOWN: \
127 : case EHOSTUNREACH: \
128 : case ENETDOWN: \
129 : case ENETRESET: \
130 : case ENETUNREACH: \
131 : case ETIMEDOUT
132 :
133 : /* Portable locale initialization (in exec.c) */
134 : extern void set_pglocale_pgservice(const char *argv0, const char *app);
135 :
136 : /* Portable way to find and execute binaries (in exec.c) */
137 : extern int validate_exec(const char *path);
138 : extern int find_my_exec(const char *argv0, char *retpath);
139 : extern int find_other_exec(const char *argv0, const char *target,
140 : const char *versionstr, char *retpath);
141 : extern char *pipe_read_line(char *cmd);
142 :
143 : /* Doesn't belong here, but this is used with find_other_exec(), so... */
144 : #define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
145 :
146 : #ifdef EXEC_BACKEND
147 : /* Disable ASLR before exec, for developer builds only (in exec.c) */
148 : extern int pg_disable_aslr(void);
149 : #endif
150 :
151 :
152 : #if defined(WIN32) || defined(__CYGWIN__)
153 : #define EXE ".exe"
154 : #else
155 : #define EXE ""
156 : #endif
157 :
158 : #if defined(WIN32) && !defined(__CYGWIN__)
159 : #define DEVNULL "nul"
160 : #else
161 : #define DEVNULL "/dev/null"
162 : #endif
163 :
164 : /* Portable delay handling */
165 : extern void pg_usleep(long microsec);
166 :
167 : /* Portable SQL-like case-independent comparisons and conversions */
168 : extern int pg_strcasecmp(const char *s1, const char *s2);
169 : extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
170 : extern unsigned char pg_toupper(unsigned char ch);
171 : extern unsigned char pg_tolower(unsigned char ch);
172 :
173 : /*
174 : * Fold a character to upper case, following C/POSIX locale rules.
175 : */
176 : static inline unsigned char
177 334692 : pg_ascii_toupper(unsigned char ch)
178 : {
179 334692 : if (ch >= 'a' && ch <= 'z')
180 114770 : ch += 'A' - 'a';
181 334692 : return ch;
182 : }
183 :
184 : /*
185 : * Fold a character to lower case, following C/POSIX locale rules.
186 : */
187 : static inline unsigned char
188 67774 : pg_ascii_tolower(unsigned char ch)
189 : {
190 67774 : if (ch >= 'A' && ch <= 'Z')
191 6916 : ch += 'a' - 'A';
192 67774 : return ch;
193 : }
194 :
195 :
196 : /*
197 : * Beginning in v12, we always replace snprintf() and friends with our own
198 : * implementation. This symbol is no longer consulted by the core code,
199 : * but keep it defined anyway in case any extensions are looking at it.
200 : */
201 : #define USE_REPL_SNPRINTF 1
202 :
203 : /*
204 : * Versions of libintl >= 0.13 try to replace printf() and friends with
205 : * macros to their own versions that understand the %$ format. We do the
206 : * same, so disable their macros, if they exist.
207 : */
208 : #ifdef vsnprintf
209 : #undef vsnprintf
210 : #endif
211 : #ifdef snprintf
212 : #undef snprintf
213 : #endif
214 : #ifdef vsprintf
215 : #undef vsprintf
216 : #endif
217 : #ifdef sprintf
218 : #undef sprintf
219 : #endif
220 : #ifdef vfprintf
221 : #undef vfprintf
222 : #endif
223 : #ifdef fprintf
224 : #undef fprintf
225 : #endif
226 : #ifdef vprintf
227 : #undef vprintf
228 : #endif
229 : #ifdef printf
230 : #undef printf
231 : #endif
232 :
233 : extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args) pg_attribute_printf(3, 0);
234 : extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
235 : extern int pg_vsprintf(char *str, const char *fmt, va_list args) pg_attribute_printf(2, 0);
236 : extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3);
237 : extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args) pg_attribute_printf(2, 0);
238 : extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
239 : extern int pg_vprintf(const char *fmt, va_list args) pg_attribute_printf(1, 0);
240 : extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
241 :
242 : #ifndef WIN32
243 : /*
244 : * We add a pg_ prefix as a warning that the Windows implementations have the
245 : * non-standard side-effect of changing the current file position.
246 : */
247 : #define pg_pread pread
248 : #define pg_pwrite pwrite
249 : #endif
250 :
251 : /*
252 : * We use __VA_ARGS__ for printf to prevent replacing references to
253 : * the "printf" format archetype in format() attribute declarations.
254 : * That unfortunately means that taking a function pointer to printf
255 : * will not do what we'd wish. (If you need to do that, you must name
256 : * pg_printf explicitly.) For printf's sibling functions, use
257 : * parameterless macros so that function pointers will work unsurprisingly.
258 : */
259 : #define vsnprintf pg_vsnprintf
260 : #define snprintf pg_snprintf
261 : #define vsprintf pg_vsprintf
262 : #define sprintf pg_sprintf
263 : #define vfprintf pg_vfprintf
264 : #define fprintf pg_fprintf
265 : #define vprintf pg_vprintf
266 : #define printf(...) pg_printf(__VA_ARGS__)
267 :
268 : /* This is also provided by snprintf.c */
269 : extern int pg_strfromd(char *str, size_t count, int precision, double value);
270 :
271 : /* Replace strerror() with our own, somewhat more robust wrapper */
272 : extern char *pg_strerror(int errnum);
273 : #define strerror pg_strerror
274 :
275 : /* Likewise for strerror_r(); note we prefer the GNU API for that */
276 : extern char *pg_strerror_r(int errnum, char *buf, size_t buflen);
277 : #define strerror_r pg_strerror_r
278 : #define PG_STRERROR_R_BUFLEN 256 /* Recommended buffer size for strerror_r */
279 :
280 : /* Wrap strsignal(), or provide our own version if necessary */
281 : extern const char *pg_strsignal(int signum);
282 :
283 : extern int pclose_check(FILE *stream);
284 :
285 : /* Global variable holding time zone information. */
286 : #if defined(WIN32) || defined(__CYGWIN__)
287 : #define TIMEZONE_GLOBAL _timezone
288 : #define TZNAME_GLOBAL _tzname
289 : #else
290 : #define TIMEZONE_GLOBAL timezone
291 : #define TZNAME_GLOBAL tzname
292 : #endif
293 :
294 : #if defined(WIN32) || defined(__CYGWIN__)
295 : /*
296 : * Win32 doesn't have reliable rename/unlink during concurrent access.
297 : */
298 : extern int pgrename(const char *from, const char *to);
299 : extern int pgunlink(const char *path);
300 :
301 : /* Include this first so later includes don't see these defines */
302 : #ifdef _MSC_VER
303 : #include <io.h>
304 : #endif
305 :
306 : #define rename(from, to) pgrename(from, to)
307 : #define unlink(path) pgunlink(path)
308 : #endif /* defined(WIN32) || defined(__CYGWIN__) */
309 :
310 : /*
311 : * Win32 also doesn't have symlinks, but we can emulate them with
312 : * junction points on newer Win32 versions.
313 : *
314 : * Cygwin has its own symlinks which work on Win95/98/ME where
315 : * junction points don't, so use those instead. We have no way of
316 : * knowing what type of system Cygwin binaries will be run on.
317 : * Note: Some CYGWIN includes might #define WIN32.
318 : */
319 : #if defined(WIN32) && !defined(__CYGWIN__)
320 : extern int pgsymlink(const char *oldpath, const char *newpath);
321 : extern int pgreadlink(const char *path, char *buf, size_t size);
322 :
323 : #define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
324 : #define readlink(path, buf, size) pgreadlink(path, buf, size)
325 : #endif
326 :
327 : extern bool rmtree(const char *path, bool rmtopdir);
328 :
329 : #if defined(WIN32) && !defined(__CYGWIN__)
330 :
331 : /*
332 : * We want the 64-bit variant of lseek().
333 : *
334 : * For Visual Studio, this must be after <io.h> to avoid messing up its
335 : * lseek() and _lseeki64() function declarations.
336 : *
337 : * For MinGW there is already a macro, so we have to undefine it (depending on
338 : * _FILE_OFFSET_BITS, it may point at its own lseek64, but we don't want to
339 : * count on that being set).
340 : */
341 : #undef lseek
342 : #define lseek(a,b,c) _lseeki64((a),(b),(c))
343 :
344 : /*
345 : * We want the 64-bit variant of chsize(). It sets errno and also returns it,
346 : * so convert non-zero result to -1 to match POSIX.
347 : *
348 : * Prevent MinGW from declaring functions, and undefine its macro before we
349 : * define our own.
350 : */
351 : #ifndef _MSC_VER
352 : #define FTRUNCATE_DEFINED
353 : #include <unistd.h>
354 : #undef ftruncate
355 : #endif
356 : #define ftruncate(a,b) (_chsize_s((a),(b)) == 0 ? 0 : -1)
357 :
358 : /*
359 : * open() and fopen() replacements to allow deletion of open files and
360 : * passing of other special options.
361 : */
362 : extern HANDLE pgwin32_open_handle(const char *, int, bool);
363 : extern int pgwin32_open(const char *, int,...);
364 : extern FILE *pgwin32_fopen(const char *, const char *);
365 : #define open(a,b,c) pgwin32_open(a,b,c)
366 : #define fopen(a,b) pgwin32_fopen(a,b)
367 :
368 : /*
369 : * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
370 : * to use our popen wrapper, rather than plain _popen, so override that. For
371 : * consistency, use our version of pclose, too.
372 : */
373 : #ifdef popen
374 : #undef popen
375 : #endif
376 : #ifdef pclose
377 : #undef pclose
378 : #endif
379 :
380 : /*
381 : * system() and popen() replacements to enclose the command in an extra
382 : * pair of quotes.
383 : */
384 : extern int pgwin32_system(const char *command);
385 : extern FILE *pgwin32_popen(const char *command, const char *type);
386 :
387 : #define system(a) pgwin32_system(a)
388 : #define popen(a,b) pgwin32_popen(a,b)
389 : #define pclose(a) _pclose(a)
390 :
391 : #else /* !WIN32 */
392 :
393 : /*
394 : * Win32 requires a special close for sockets and pipes, while on Unix
395 : * close() does them all.
396 : */
397 : #define closesocket close
398 : #endif /* WIN32 */
399 :
400 : /*
401 : * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
402 : * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
403 : * crashes outright if "parameter validation" is enabled. Therefore, in
404 : * places where we'd like to select line-buffered mode, we fall back to
405 : * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
406 : * directly in order to implement this behavior.
407 : */
408 : #ifndef WIN32
409 : #define PG_IOLBF _IOLBF
410 : #else
411 : #define PG_IOLBF _IONBF
412 : #endif
413 :
414 : /*
415 : * Default "extern" declarations or macro substitutes for library routines.
416 : * When necessary, these routines are provided by files in src/port/.
417 : */
418 :
419 : /* Type to use with fseeko/ftello */
420 : #ifndef WIN32 /* WIN32 is handled in port/win32_port.h */
421 : typedef off_t pgoff_t;
422 : #endif
423 :
424 : #ifndef HAVE_GETPEEREID
425 : /* On Windows, Perl might have incompatible definitions of uid_t and gid_t. */
426 : #ifndef PLPERL_HAVE_UID_GID
427 : extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
428 : #endif
429 : #endif
430 :
431 : /*
432 : * Glibc doesn't use the builtin for clang due to a *gcc* bug in a version
433 : * newer than the gcc compatibility clang claims to have. This would cause a
434 : * *lot* of superfluous function calls, therefore revert when using clang. In
435 : * C++ there's issues with libc++ (not libstdc++), so disable as well.
436 : */
437 : #if defined(__clang__) && !defined(__cplusplus)
438 : /* needs to be separate to not confuse other compilers */
439 : #if __has_builtin(__builtin_isinf)
440 : /* need to include before, to avoid getting overwritten */
441 : #include <math.h>
442 : #undef isinf
443 : #define isinf __builtin_isinf
444 : #endif /* __has_builtin(isinf) */
445 : #endif /* __clang__ && !__cplusplus */
446 :
447 : #ifndef HAVE_EXPLICIT_BZERO
448 : extern void explicit_bzero(void *buf, size_t len);
449 : #endif
450 :
451 : #ifdef HAVE_BUGGY_STRTOF
452 : extern float pg_strtof(const char *nptr, char **endptr);
453 : #define strtof(a,b) (pg_strtof((a),(b)))
454 : #endif
455 :
456 : #ifdef WIN32
457 : /* src/port/win32link.c */
458 : extern int link(const char *src, const char *dst);
459 : #endif
460 :
461 : #ifndef HAVE_MKDTEMP
462 : extern char *mkdtemp(char *path);
463 : #endif
464 :
465 : #ifndef HAVE_INET_ATON
466 : #include <netinet/in.h>
467 : #include <arpa/inet.h>
468 : extern int inet_aton(const char *cp, struct in_addr *addr);
469 : #endif
470 :
471 : #if !HAVE_DECL_STRLCAT
472 : extern size_t strlcat(char *dst, const char *src, size_t siz);
473 : #endif
474 :
475 : #if !HAVE_DECL_STRLCPY
476 : extern size_t strlcpy(char *dst, const char *src, size_t siz);
477 : #endif
478 :
479 : #if !HAVE_DECL_STRNLEN
480 : extern size_t strnlen(const char *str, size_t maxlen);
481 : #endif
482 :
483 : #if !HAVE_DECL_STRSEP
484 : extern char *strsep(char **stringp, const char *delim);
485 : #endif
486 :
487 : #if !HAVE_DECL_TIMINGSAFE_BCMP
488 : extern int timingsafe_bcmp(const void *b1, const void *b2, size_t len);
489 : #endif
490 :
491 : /*
492 : * Callers should use the qsort() macro defined below instead of calling
493 : * pg_qsort() directly.
494 : */
495 : extern void pg_qsort(void *base, size_t nel, size_t elsize,
496 : int (*cmp) (const void *, const void *));
497 : extern int pg_qsort_strcmp(const void *a, const void *b);
498 :
499 : #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
500 :
501 : typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
502 :
503 : extern void qsort_arg(void *base, size_t nel, size_t elsize,
504 : qsort_arg_comparator cmp, void *arg);
505 :
506 : extern void qsort_interruptible(void *base, size_t nel, size_t elsize,
507 : qsort_arg_comparator cmp, void *arg);
508 :
509 : extern void *bsearch_arg(const void *key, const void *base0,
510 : size_t nmemb, size_t size,
511 : int (*compar) (const void *, const void *, void *),
512 : void *arg);
513 :
514 : /* port/pg_localeconv_r.c */
515 : extern int pg_localeconv_r(const char *lc_monetary,
516 : const char *lc_numeric,
517 : struct lconv *output);
518 : extern void pg_localeconv_free(struct lconv *lconv);
519 :
520 : /* port/chklocale.c */
521 : extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
522 :
523 : #if defined(WIN32) && !defined(FRONTEND)
524 : extern int pg_codepage_to_encoding(UINT cp);
525 : #endif
526 :
527 : /* port/inet_net_ntop.c */
528 : extern char *pg_inet_net_ntop(int af, const void *src, int bits,
529 : char *dst, size_t size);
530 :
531 : /* port/pg_strong_random.c */
532 : extern void pg_strong_random_init(void);
533 : extern bool pg_strong_random(void *buf, size_t len);
534 :
535 : /*
536 : * pg_backend_random used to be a wrapper for pg_strong_random before
537 : * Postgres 12 for the backend code.
538 : */
539 : #define pg_backend_random pg_strong_random
540 :
541 : /* port/pgcheckdir.c */
542 : extern int pg_check_dir(const char *dir);
543 :
544 : /* port/pgmkdirp.c */
545 : extern int pg_mkdir_p(char *path, int omode);
546 :
547 : /* port/pqsignal.c (see also interfaces/libpq/legacy-pqsignal.c) */
548 : #ifdef FRONTEND
549 : #define pqsignal pqsignal_fe
550 : #else
551 : #define pqsignal pqsignal_be
552 : #endif
553 : typedef void (*pqsigfunc) (SIGNAL_ARGS);
554 : extern void pqsignal(int signo, pqsigfunc func);
555 :
556 : /* port/quotes.c */
557 : extern char *escape_single_quotes_ascii(const char *src);
558 :
559 : /* common/wait_error.c */
560 : extern char *wait_result_to_str(int exitstatus);
561 : extern bool wait_result_is_signal(int exit_status, int signum);
562 : extern bool wait_result_is_any_signal(int exit_status, bool include_command_not_found);
563 : extern int wait_result_to_exit_code(int exit_status);
564 :
565 : /*
566 : * Interfaces that we assume all Unix system have. We retain individual macros
567 : * for better documentation.
568 : *
569 : * For symlink-related functions, there is often no need to test these macros,
570 : * because we provided basic support on Windows that can work with absolute
571 : * paths to directories. Code that wants to test for complete symlink support
572 : * (including relative paths and non-directories) should be conditional on
573 : * HAVE_READLINK or HAVE_SYMLINK.
574 : */
575 : #ifndef WIN32
576 : #define HAVE_GETRLIMIT 1
577 : #define HAVE_POLL 1
578 : #define HAVE_POLL_H 1
579 : #define HAVE_READLINK 1
580 : #define HAVE_SETSID 1
581 : #define HAVE_SHM_OPEN 1
582 : #define HAVE_SYMLINK 1
583 : #endif
584 :
585 : #endif /* PG_PORT_H */
|