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