1/*-------------------------------------------------------------------------
2 *
3 * port.h
4 * Header for src/port/ compatibility functions.
5 *
6 * Portions Copyright (c) 1996-2019, 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#include <netdb.h>
18#include <pwd.h>
19
20/*
21 * Windows has enough specialized port stuff that we push most of it off
22 * into another file.
23 * Note: Some CYGWIN includes might #define WIN32.
24 */
25#if defined(WIN32) && !defined(__CYGWIN__)
26#include "port/win32_port.h"
27#endif
28
29/* socket has a different definition on WIN32 */
30#ifndef WIN32
31typedef int pgsocket;
32
33#define PGINVALID_SOCKET (-1)
34#else
35typedef SOCKET pgsocket;
36
37#define PGINVALID_SOCKET INVALID_SOCKET
38#endif
39
40/* non-blocking */
41extern bool pg_set_noblock(pgsocket sock);
42extern bool pg_set_block(pgsocket sock);
43
44/* Portable path handling for Unix/Win32 (in path.c) */
45
46extern bool has_drive_prefix(const char *filename);
47extern char *first_dir_separator(const char *filename);
48extern char *last_dir_separator(const char *filename);
49extern char *first_path_var_separator(const char *pathlist);
50extern void join_path_components(char *ret_path,
51 const char *head, const char *tail);
52extern void canonicalize_path(char *path);
53extern void make_native_path(char *path);
54extern void cleanup_path(char *path);
55extern bool path_contains_parent_reference(const char *path);
56extern bool path_is_relative_and_below_cwd(const char *path);
57extern bool path_is_prefix_of_path(const char *path1, const char *path2);
58extern char *make_absolute_path(const char *path);
59extern const char *get_progname(const char *argv0);
60extern void get_share_path(const char *my_exec_path, char *ret_path);
61extern void get_etc_path(const char *my_exec_path, char *ret_path);
62extern void get_include_path(const char *my_exec_path, char *ret_path);
63extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
64extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
65extern void get_lib_path(const char *my_exec_path, char *ret_path);
66extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
67extern void get_locale_path(const char *my_exec_path, char *ret_path);
68extern void get_doc_path(const char *my_exec_path, char *ret_path);
69extern void get_html_path(const char *my_exec_path, char *ret_path);
70extern void get_man_path(const char *my_exec_path, char *ret_path);
71extern bool get_home_path(char *ret_path);
72extern void get_parent_directory(char *path);
73
74/* common/pgfnames.c */
75extern char **pgfnames(const char *path);
76extern void pgfnames_cleanup(char **filenames);
77
78/*
79 * is_absolute_path
80 *
81 * By making this a macro we avoid needing to include path.c in libpq.
82 */
83#ifndef WIN32
84#define IS_DIR_SEP(ch) ((ch) == '/')
85
86#define is_absolute_path(filename) \
87( \
88 IS_DIR_SEP((filename)[0]) \
89)
90#else
91#define IS_DIR_SEP(ch) ((ch) == '/' || (ch) == '\\')
92
93/* See path_is_relative_and_below_cwd() for how we handle 'E:abc'. */
94#define is_absolute_path(filename) \
95( \
96 IS_DIR_SEP((filename)[0]) || \
97 (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
98 IS_DIR_SEP((filename)[2])) \
99)
100#endif
101
102/* Portable locale initialization (in exec.c) */
103extern void set_pglocale_pgservice(const char *argv0, const char *app);
104
105/* Portable way to find binaries (in exec.c) */
106extern int find_my_exec(const char *argv0, char *retpath);
107extern int find_other_exec(const char *argv0, const char *target,
108 const char *versionstr, char *retpath);
109
110/* Doesn't belong here, but this is used with find_other_exec(), so... */
111#define PG_BACKEND_VERSIONSTR "postgres (PostgreSQL) " PG_VERSION "\n"
112
113
114#if defined(WIN32) || defined(__CYGWIN__)
115#define EXE ".exe"
116#else
117#define EXE ""
118#endif
119
120#if defined(WIN32) && !defined(__CYGWIN__)
121#define DEVNULL "nul"
122#else
123#define DEVNULL "/dev/null"
124#endif
125
126/* Portable delay handling */
127extern void pg_usleep(long microsec);
128
129/* Portable SQL-like case-independent comparisons and conversions */
130extern int pg_strcasecmp(const char *s1, const char *s2);
131extern int pg_strncasecmp(const char *s1, const char *s2, size_t n);
132extern unsigned char pg_toupper(unsigned char ch);
133extern unsigned char pg_tolower(unsigned char ch);
134extern unsigned char pg_ascii_toupper(unsigned char ch);
135extern unsigned char pg_ascii_tolower(unsigned char ch);
136
137/*
138 * Beginning in v12, we always replace snprintf() and friends with our own
139 * implementation. This symbol is no longer consulted by the core code,
140 * but keep it defined anyway in case any extensions are looking at it.
141 */
142#define USE_REPL_SNPRINTF 1
143
144/*
145 * Versions of libintl >= 0.13 try to replace printf() and friends with
146 * macros to their own versions that understand the %$ format. We do the
147 * same, so disable their macros, if they exist.
148 */
149#ifdef vsnprintf
150#undef vsnprintf
151#endif
152#ifdef snprintf
153#undef snprintf
154#endif
155#ifdef vsprintf
156#undef vsprintf
157#endif
158#ifdef sprintf
159#undef sprintf
160#endif
161#ifdef vfprintf
162#undef vfprintf
163#endif
164#ifdef fprintf
165#undef fprintf
166#endif
167#ifdef vprintf
168#undef vprintf
169#endif
170#ifdef printf
171#undef printf
172#endif
173
174extern int pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
175extern int pg_snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3, 4);
176extern int pg_vsprintf(char *str, const char *fmt, va_list args);
177extern int pg_sprintf(char *str, const char *fmt,...) pg_attribute_printf(2, 3);
178extern int pg_vfprintf(FILE *stream, const char *fmt, va_list args);
179extern int pg_fprintf(FILE *stream, const char *fmt,...) pg_attribute_printf(2, 3);
180extern int pg_vprintf(const char *fmt, va_list args);
181extern int pg_printf(const char *fmt,...) pg_attribute_printf(1, 2);
182
183/*
184 * We use __VA_ARGS__ for printf to prevent replacing references to
185 * the "printf" format archetype in format() attribute declarations.
186 * That unfortunately means that taking a function pointer to printf
187 * will not do what we'd wish. (If you need to do that, you must name
188 * pg_printf explicitly.) For printf's sibling functions, use
189 * parameterless macros so that function pointers will work unsurprisingly.
190 */
191#define vsnprintf pg_vsnprintf
192#define snprintf pg_snprintf
193#define vsprintf pg_vsprintf
194#define sprintf pg_sprintf
195#define vfprintf pg_vfprintf
196#define fprintf pg_fprintf
197#define vprintf pg_vprintf
198#define printf(...) pg_printf(__VA_ARGS__)
199
200/* This is also provided by snprintf.c */
201extern int pg_strfromd(char *str, size_t count, int precision, double value);
202
203/* Replace strerror() with our own, somewhat more robust wrapper */
204extern char *pg_strerror(int errnum);
205#define strerror pg_strerror
206
207/* Likewise for strerror_r(); note we prefer the GNU API for that */
208extern char *pg_strerror_r(int errnum, char *buf, size_t buflen);
209#define strerror_r pg_strerror_r
210#define PG_STRERROR_R_BUFLEN 256 /* Recommended buffer size for strerror_r */
211
212/* Wrap strsignal(), or provide our own version if necessary */
213extern const char *pg_strsignal(int signum);
214
215/* Portable prompt handling */
216extern void simple_prompt(const char *prompt, char *destination, size_t destlen,
217 bool echo);
218
219extern int pclose_check(FILE *stream);
220
221/* Global variable holding time zone information. */
222#if defined(WIN32) || defined(__CYGWIN__)
223#define TIMEZONE_GLOBAL _timezone
224#define TZNAME_GLOBAL _tzname
225#else
226#define TIMEZONE_GLOBAL timezone
227#define TZNAME_GLOBAL tzname
228#endif
229
230#if defined(WIN32) || defined(__CYGWIN__)
231/*
232 * Win32 doesn't have reliable rename/unlink during concurrent access.
233 */
234extern int pgrename(const char *from, const char *to);
235extern int pgunlink(const char *path);
236
237/* Include this first so later includes don't see these defines */
238#ifdef _MSC_VER
239#include <io.h>
240#endif
241
242#define rename(from, to) pgrename(from, to)
243#define unlink(path) pgunlink(path)
244#endif /* defined(WIN32) || defined(__CYGWIN__) */
245
246/*
247 * Win32 also doesn't have symlinks, but we can emulate them with
248 * junction points on newer Win32 versions.
249 *
250 * Cygwin has its own symlinks which work on Win95/98/ME where
251 * junction points don't, so use those instead. We have no way of
252 * knowing what type of system Cygwin binaries will be run on.
253 * Note: Some CYGWIN includes might #define WIN32.
254 */
255#if defined(WIN32) && !defined(__CYGWIN__)
256extern int pgsymlink(const char *oldpath, const char *newpath);
257extern int pgreadlink(const char *path, char *buf, size_t size);
258extern bool pgwin32_is_junction(const char *path);
259
260#define symlink(oldpath, newpath) pgsymlink(oldpath, newpath)
261#define readlink(path, buf, size) pgreadlink(path, buf, size)
262#endif
263
264extern bool rmtree(const char *path, bool rmtopdir);
265
266#if defined(WIN32) && !defined(__CYGWIN__)
267
268/*
269 * open() and fopen() replacements to allow deletion of open files and
270 * passing of other special options.
271 */
272#define O_DIRECT 0x80000000
273extern int pgwin32_open(const char *, int,...);
274extern FILE *pgwin32_fopen(const char *, const char *);
275#define open(a,b,c) pgwin32_open(a,b,c)
276#define fopen(a,b) pgwin32_fopen(a,b)
277
278/*
279 * Mingw-w64 headers #define popen and pclose to _popen and _pclose. We want
280 * to use our popen wrapper, rather than plain _popen, so override that. For
281 * consistency, use our version of pclose, too.
282 */
283#ifdef popen
284#undef popen
285#endif
286#ifdef pclose
287#undef pclose
288#endif
289
290/*
291 * system() and popen() replacements to enclose the command in an extra
292 * pair of quotes.
293 */
294extern int pgwin32_system(const char *command);
295extern FILE *pgwin32_popen(const char *command, const char *type);
296
297#define system(a) pgwin32_system(a)
298#define popen(a,b) pgwin32_popen(a,b)
299#define pclose(a) _pclose(a)
300
301/* New versions of MingW have gettimeofday, old mingw and msvc don't */
302#ifndef HAVE_GETTIMEOFDAY
303/* Last parameter not used */
304extern int gettimeofday(struct timeval *tp, struct timezone *tzp);
305#endif
306#else /* !WIN32 */
307
308/*
309 * Win32 requires a special close for sockets and pipes, while on Unix
310 * close() does them all.
311 */
312#define closesocket close
313#endif /* WIN32 */
314
315/*
316 * On Windows, setvbuf() does not support _IOLBF mode, and interprets that
317 * as _IOFBF. To add insult to injury, setvbuf(file, NULL, _IOFBF, 0)
318 * crashes outright if "parameter validation" is enabled. Therefore, in
319 * places where we'd like to select line-buffered mode, we fall back to
320 * unbuffered mode instead on Windows. Always use PG_IOLBF not _IOLBF
321 * directly in order to implement this behavior.
322 */
323#ifndef WIN32
324#define PG_IOLBF _IOLBF
325#else
326#define PG_IOLBF _IONBF
327#endif
328
329/*
330 * Default "extern" declarations or macro substitutes for library routines.
331 * When necessary, these routines are provided by files in src/port/.
332 */
333#ifndef HAVE_CRYPT
334extern char *crypt(const char *key, const char *setting);
335#endif
336
337/* WIN32 handled in port/win32_port.h */
338#ifndef WIN32
339#define pgoff_t off_t
340#ifdef __NetBSD__
341extern int fseeko(FILE *stream, off_t offset, int whence);
342extern off_t ftello(FILE *stream);
343#endif
344#endif
345
346extern double pg_erand48(unsigned short xseed[3]);
347extern long pg_lrand48(void);
348extern long pg_jrand48(unsigned short xseed[3]);
349extern void pg_srand48(long seed);
350
351#ifndef HAVE_FLS
352extern int fls(int mask);
353#endif
354
355#ifndef HAVE_FSEEKO
356#define fseeko(a, b, c) fseek(a, b, c)
357#define ftello(a) ftell(a)
358#endif
359
360#if !defined(HAVE_GETPEEREID) && !defined(WIN32)
361extern int getpeereid(int sock, uid_t *uid, gid_t *gid);
362#endif
363
364#ifndef HAVE_ISINF
365extern int isinf(double x);
366#else
367/*
368 * Glibc doesn't use the builtin for clang due to a *gcc* bug in a version
369 * newer than the gcc compatibility clang claims to have. This would cause a
370 * *lot* of superfluous function calls, therefore revert when using clang. In
371 * C++ there's issues with libc++ (not libstdc++), so disable as well.
372 */
373#if defined(__clang__) && !defined(__cplusplus)
374/* needs to be separate to not confuse other compilers */
375#if __has_builtin(__builtin_isinf)
376/* need to include before, to avoid getting overwritten */
377#include <math.h>
378#undef isinf
379#define isinf __builtin_isinf
380#endif /* __has_builtin(isinf) */
381#endif /* __clang__ && !__cplusplus */
382#endif /* !HAVE_ISINF */
383
384#ifndef HAVE_STRTOF
385extern float strtof(const char *nptr, char **endptr);
386#endif
387
388#ifdef HAVE_BUGGY_STRTOF
389extern float pg_strtof(const char *nptr, char **endptr);
390#define strtof(a,b) (pg_strtof((a),(b)))
391#endif
392
393#ifndef HAVE_MKDTEMP
394extern char *mkdtemp(char *path);
395#endif
396
397#ifndef HAVE_RINT
398extern double rint(double x);
399#endif
400
401#ifndef HAVE_INET_ATON
402#include <netinet/in.h>
403#include <arpa/inet.h>
404extern int inet_aton(const char *cp, struct in_addr *addr);
405#endif
406
407/*
408 * Windows and older Unix don't have pread(2) and pwrite(2). We have
409 * replacement functions, but they have slightly different semantics so we'll
410 * use a name with a pg_ prefix to avoid confusion.
411 */
412#ifdef HAVE_PREAD
413#define pg_pread pread
414#else
415extern ssize_t pg_pread(int fd, void *buf, size_t nbyte, off_t offset);
416#endif
417
418#ifdef HAVE_PWRITE
419#define pg_pwrite pwrite
420#else
421extern ssize_t pg_pwrite(int fd, const void *buf, size_t nbyte, off_t offset);
422#endif
423
424#if !HAVE_DECL_STRLCAT
425extern size_t strlcat(char *dst, const char *src, size_t siz);
426#endif
427
428#if !HAVE_DECL_STRLCPY
429extern size_t strlcpy(char *dst, const char *src, size_t siz);
430#endif
431
432#if !HAVE_DECL_STRNLEN
433extern size_t strnlen(const char *str, size_t maxlen);
434#endif
435
436#if !defined(HAVE_RANDOM)
437extern long random(void);
438#endif
439
440#ifndef HAVE_UNSETENV
441extern void unsetenv(const char *name);
442#endif
443
444#ifndef HAVE_SRANDOM
445extern void srandom(unsigned int seed);
446#endif
447
448#ifndef HAVE_SSL_GET_CURRENT_COMPRESSION
449#define SSL_get_current_compression(x) 0
450#endif
451
452#ifndef HAVE_DLOPEN
453extern void *dlopen(const char *file, int mode);
454extern void *dlsym(void *handle, const char *symbol);
455extern int dlclose(void *handle);
456extern char *dlerror(void);
457#endif
458
459/*
460 * In some older systems, the RTLD_NOW flag isn't defined and the mode
461 * argument to dlopen must always be 1.
462 */
463#if !HAVE_DECL_RTLD_NOW
464#define RTLD_NOW 1
465#endif
466
467/*
468 * The RTLD_GLOBAL flag is wanted if available, but it doesn't exist
469 * everywhere. If it doesn't exist, set it to 0 so it has no effect.
470 */
471#if !HAVE_DECL_RTLD_GLOBAL
472#define RTLD_GLOBAL 0
473#endif
474
475/* thread.h */
476#ifndef WIN32
477extern int pqGetpwuid(uid_t uid, struct passwd *resultbuf, char *buffer,
478 size_t buflen, struct passwd **result);
479#endif
480
481extern int pqGethostbyname(const char *name,
482 struct hostent *resultbuf,
483 char *buffer, size_t buflen,
484 struct hostent **result,
485 int *herrno);
486
487extern void pg_qsort(void *base, size_t nel, size_t elsize,
488 int (*cmp) (const void *, const void *));
489extern int pg_qsort_strcmp(const void *a, const void *b);
490
491#define qsort(a,b,c,d) pg_qsort(a,b,c,d)
492
493typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
494
495extern void qsort_arg(void *base, size_t nel, size_t elsize,
496 qsort_arg_comparator cmp, void *arg);
497
498/* port/chklocale.c */
499extern int pg_get_encoding_from_locale(const char *ctype, bool write_message);
500
501#if defined(WIN32) && !defined(FRONTEND)
502extern int pg_codepage_to_encoding(UINT cp);
503#endif
504
505/* port/inet_net_ntop.c */
506extern char *inet_net_ntop(int af, const void *src, int bits,
507 char *dst, size_t size);
508
509/* port/pg_strong_random.c */
510extern bool pg_strong_random(void *buf, size_t len);
511
512/*
513 * pg_backend_random used to be a wrapper for pg_strong_random before
514 * Postgres 12 for the backend code.
515 */
516#define pg_backend_random pg_strong_random
517
518/* port/pgcheckdir.c */
519extern int pg_check_dir(const char *dir);
520
521/* port/pgmkdirp.c */
522extern int pg_mkdir_p(char *path, int omode);
523
524/* port/pqsignal.c */
525typedef void (*pqsigfunc) (int signo);
526extern pqsigfunc pqsignal(int signo, pqsigfunc func);
527#ifndef WIN32
528extern pqsigfunc pqsignal_no_restart(int signo, pqsigfunc func);
529#else
530#define pqsignal_no_restart(signo, func) pqsignal(signo, func)
531#endif
532
533/* port/quotes.c */
534extern char *escape_single_quotes_ascii(const char *src);
535
536/* common/wait_error.c */
537extern char *wait_result_to_str(int exit_status);
538extern bool wait_result_is_signal(int exit_status, int signum);
539extern bool wait_result_is_any_signal(int exit_status, bool include_command_not_found);
540
541#endif /* PG_PORT_H */
542