port.h 17 KB

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