]> granicus.if.org Git - postgresql/blob - src/include/port.h
When reporting the server as not responding, if the hostname was
[postgresql] / src / include / port.h
1 /*-------------------------------------------------------------------------
2  *
3  * port.h
4  *        Header for src/port/ compatibility functions.
5  *
6  * Portions Copyright (c) 1996-2010, 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 /* socket has a different definition on WIN32 */
21 #ifndef WIN32
22 typedef int pgsocket;
23
24 #define PGINVALID_SOCKET (-1)
25 #else
26 typedef SOCKET pgsocket;
27
28 #define PGINVALID_SOCKET INVALID_SOCKET
29 #endif
30
31 /* non-blocking */
32 extern bool pg_set_noblock(pgsocket sock);
33 extern bool pg_set_block(pgsocket sock);
34
35 /* Portable path handling for Unix/Win32 (in path.c) */
36
37 extern char *first_dir_separator(const char *filename);
38 extern char *last_dir_separator(const char *filename);
39 extern char *first_path_separator(const char *pathlist);
40 extern void join_path_components(char *ret_path,
41                                          const char *head, const char *tail);
42 extern void canonicalize_path(char *path);
43 extern void make_native_path(char *path);
44 extern bool path_contains_parent_reference(const char *path);
45 extern bool path_is_prefix_of_path(const char *path1, const char *path2);
46 extern const char *get_progname(const char *argv0);
47 extern void get_share_path(const char *my_exec_path, char *ret_path);
48 extern void get_etc_path(const char *my_exec_path, char *ret_path);
49 extern void get_include_path(const char *my_exec_path, char *ret_path);
50 extern void get_pkginclude_path(const char *my_exec_path, char *ret_path);
51 extern void get_includeserver_path(const char *my_exec_path, char *ret_path);
52 extern void get_lib_path(const char *my_exec_path, char *ret_path);
53 extern void get_pkglib_path(const char *my_exec_path, char *ret_path);
54 extern void get_locale_path(const char *my_exec_path, char *ret_path);
55 extern void get_doc_path(const char *my_exec_path, char *ret_path);
56 extern void get_html_path(const char *my_exec_path, char *ret_path);
57 extern void get_man_path(const char *my_exec_path, char *ret_path);
58 extern bool get_home_path(char *ret_path);
59 extern void get_parent_directory(char *path);
60
61 /* port/dirmod.c */
62 extern char **pgfnames(const char *path);
63 extern void pgfnames_cleanup(char **filenames);
64
65 /*
66  *      is_absolute_path
67  *
68  *      By making this a macro we avoid needing to include path.c in libpq.
69  */
70 #ifndef WIN32
71 #define is_absolute_path(filename) \
72 ( \
73         ((filename)[0] == '/') \
74 )
75 #else
76 #define is_absolute_path(filename) \
77 ( \
78         ((filename)[0] == '/') || \
79         (filename)[0] == '\\' || \
80         (isalpha((unsigned char) ((filename)[0])) && (filename)[1] == ':' && \
81         ((filename)[2] == '\\' || (filename)[2] == '/')) \
82 )
83 #endif
84
85 /* Portable locale initialization (in exec.c) */
86 extern void set_pglocale_pgservice(const char *argv0, const char *app);
87
88 /* Portable way to find binaries (in exec.c) */
89 extern int      find_my_exec(const char *argv0, char *retpath);
90 extern int find_other_exec(const char *argv0, const char *target,
91                                 const char *versionstr, char *retpath);
92
93 /* Windows security token manipulation (in exec.c) */
94 #ifdef WIN32
95 extern BOOL AddUserToTokenDacl(HANDLE hToken);
96 #endif
97
98
99 #if defined(WIN32) || defined(__CYGWIN__)
100 #define EXE ".exe"
101 #else
102 #define EXE ""
103 #endif
104
105 #if defined(WIN32) && !defined(__CYGWIN__)
106 #define DEVNULL "nul"
107 /* "con" does not work from the Msys 1.0.10 console (part of MinGW). */
108 #define DEVTTY  "con"
109 #else
110 #define DEVNULL "/dev/null"
111 #define DEVTTY "/dev/tty"
112 #endif
113
114 /*
115  *      Win32 needs double quotes at the beginning and end of system()
116  *      strings.  If not, it gets confused with multiple quoted strings.
117  *      It also requires double-quotes around the executable name and
118  *      any files used for redirection.  Other args can use single-quotes.
119  *
120  *      Generated using Win32 "CMD /?":
121  *
122  *      1. If all of the following conditions are met, then quote characters
123  *      on the command line are preserved:
124  *
125  *       - no /S switch
126  *       - exactly two quote characters
127  *       - no special characters between the two quote characters, where special
128  *         is one of: &<>()@^|
129  *       - there are one or more whitespace characters between the two quote
130  *         characters
131  *       - the string between the two quote characters is the name of an
132  *         executable file.
133  *
134  *       2. Otherwise, old behavior is to see if the first character is a quote
135  *       character and if so, strip the leading character and remove the last
136  *       quote character on the command line, preserving any text after the last
137  *       quote character.
138  */
139 #if defined(WIN32) && !defined(__CYGWIN__)
140 #define SYSTEMQUOTE "\""
141 #else
142 #define SYSTEMQUOTE ""
143 #endif
144
145 /* Portable delay handling */
146 extern void pg_usleep(long microsec);
147
148 /* Portable SQL-like case-independent comparisons and conversions */
149 extern int      pg_strcasecmp(const char *s1, const char *s2);
150 extern int      pg_strncasecmp(const char *s1, const char *s2, size_t n);
151 extern unsigned char pg_toupper(unsigned char ch);
152 extern unsigned char pg_tolower(unsigned char ch);
153
154 #ifdef USE_REPL_SNPRINTF
155
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 sprintf
168 #undef sprintf
169 #endif
170 #ifdef vfprintf
171 #undef vfprintf
172 #endif
173 #ifdef fprintf
174 #undef fprintf
175 #endif
176 #ifdef printf
177 #undef printf
178 #endif
179
180 extern int      pg_vsnprintf(char *str, size_t count, const char *fmt, va_list args);
181 extern int
182 pg_snprintf(char *str, size_t count, const char *fmt,...)
183 /* This extension allows gcc to check the format string */
184 __attribute__((format(printf, 3, 4)));
185 extern int
186 pg_sprintf(char *str, const char *fmt,...)
187 /* This extension allows gcc to check the format string */
188 __attribute__((format(printf, 2, 3)));
189 extern int      pg_vfprintf(FILE *stream, const char *fmt, va_list args);
190 extern int
191 pg_fprintf(FILE *stream, const char *fmt,...)
192 /* This extension allows gcc to check the format string */
193 __attribute__((format(printf, 2, 3)));
194 extern int
195 pg_printf(const char *fmt,...)
196 /* This extension allows gcc to check the format string */
197 __attribute__((format(printf, 1, 2)));
198
199 /*
200  *      The GCC-specific code below prevents the __attribute__(... 'printf')
201  *      above from being replaced, and this is required because gcc doesn't
202  *      know anything about pg_printf.
203  */
204 #ifdef __GNUC__
205 #define vsnprintf(...)  pg_vsnprintf(__VA_ARGS__)
206 #define snprintf(...)   pg_snprintf(__VA_ARGS__)
207 #define sprintf(...)    pg_sprintf(__VA_ARGS__)
208 #define vfprintf(...)   pg_vfprintf(__VA_ARGS__)
209 #define fprintf(...)    pg_fprintf(__VA_ARGS__)
210 #define printf(...)             pg_printf(__VA_ARGS__)
211 #else
212 #define vsnprintf               pg_vsnprintf
213 #define snprintf                pg_snprintf
214 #define sprintf                 pg_sprintf
215 #define vfprintf                pg_vfprintf
216 #define fprintf                 pg_fprintf
217 #define printf                  pg_printf
218 #endif
219 #endif   /* USE_REPL_SNPRINTF */
220
221 /* Portable prompt handling */
222 extern char *simple_prompt(const char *prompt, int maxlen, bool echo);
223
224 /*
225  *      WIN32 doesn't allow descriptors returned by pipe() to be used in select(),
226  *      so for that platform we use socket() instead of pipe().
227  *      There is some inconsistency here because sometimes we require pg*, like
228  *      pgpipe, but in other cases we define rename to pgrename just on Win32.
229  */
230 #ifndef WIN32
231 /*
232  *      The function prototypes are not supplied because every C file
233  *      includes this file.
234  */
235 #define pgpipe(a)                       pipe(a)
236 #define piperead(a,b,c)         read(a,b,c)
237 #define pipewrite(a,b,c)        write(a,b,c)
238 #else
239 extern int      pgpipe(int handles[2]);
240 extern int      piperead(int s, char *buf, int len);
241
242 #define pipewrite(a,b,c)        send(a,b,c,0)
243
244 #define PG_SIGNAL_COUNT 32
245 #define kill(pid,sig)   pgkill(pid,sig)
246 extern int      pgkill(int pid, int sig);
247 #endif
248
249 extern int      pclose_check(FILE *stream);
250
251 /* Global variable holding time zone information. */
252 #ifndef __CYGWIN__
253 #define TIMEZONE_GLOBAL timezone
254 #define TZNAME_GLOBAL tzname
255 #else
256 #define TIMEZONE_GLOBAL _timezone
257 #define TZNAME_GLOBAL _tzname
258 #endif
259
260 #if defined(WIN32) || defined(__CYGWIN__)
261 /*
262  *      Win32 doesn't have reliable rename/unlink during concurrent access.
263  */
264 extern int      pgrename(const char *from, const char *to);
265 extern int      pgunlink(const char *path);
266
267 /* Include this first so later includes don't see these defines */
268 #ifdef WIN32_ONLY_COMPILER
269 #include <io.h>
270 #endif
271
272 #define rename(from, to)                pgrename(from, to)
273 #define unlink(path)                    pgunlink(path)
274 #endif   /* defined(WIN32) || defined(__CYGWIN__) */
275
276 /*
277  *      Win32 also doesn't have symlinks, but we can emulate them with
278  *      junction points on newer Win32 versions.
279  *
280  *      Cygwin has its own symlinks which work on Win95/98/ME where
281  *      junction points don't, so use those instead.  We have no way of
282  *      knowing what type of system Cygwin binaries will be run on.
283  *              Note: Some CYGWIN includes might #define WIN32.
284  */
285 #if defined(WIN32) && !defined(__CYGWIN__)
286 extern int      pgsymlink(const char *oldpath, const char *newpath);
287
288 #define symlink(oldpath, newpath)       pgsymlink(oldpath, newpath)
289 #endif
290
291 extern bool rmtree(const char *path, bool rmtopdir);
292
293 /*
294  * stat() is not guaranteed to set the st_size field on win32, so we
295  * redefine it to our own implementation that is.
296  *
297  * We must pull in sys/stat.h here so the system header definition
298  * goes in first, and we redefine that, and not the other way around.
299  *
300  * Some frontends don't need the size from stat, so if UNSAFE_STAT_OK
301  * is defined we don't bother with this.
302  */
303 #if defined(WIN32) && !defined(__CYGWIN__) && !defined(UNSAFE_STAT_OK)
304 #include <sys/stat.h>
305 extern int      pgwin32_safestat(const char *path, struct stat * buf);
306
307 #define stat(a,b) pgwin32_safestat(a,b)
308 #endif
309
310 #if defined(WIN32) && !defined(__CYGWIN__)
311
312 /*
313  * open() and fopen() replacements to allow deletion of open files and
314  * passing of other special options.
315  */
316 #define         O_DIRECT        0x80000000
317 extern int      pgwin32_open(const char *, int,...);
318 extern FILE *pgwin32_fopen(const char *, const char *);
319
320 #ifndef FRONTEND
321 #define         open(a,b,c) pgwin32_open(a,b,c)
322 #define         fopen(a,b) pgwin32_fopen(a,b)
323 #endif
324
325 #define popen(a,b) _popen(a,b)
326 #define pclose(a) _pclose(a)
327
328 /* New versions of MingW have gettimeofday, old mingw and msvc don't */
329 #ifndef HAVE_GETTIMEOFDAY
330 /* Last parameter not used */
331 extern int      gettimeofday(struct timeval * tp, struct timezone * tzp);
332 #endif
333 #else                                                   /* !WIN32 */
334
335 /*
336  *      Win32 requires a special close for sockets and pipes, while on Unix
337  *      close() does them all.
338  */
339 #define closesocket close
340 #endif   /* WIN32 */
341
342 /*
343  * Default "extern" declarations or macro substitutes for library routines.
344  * When necessary, these routines are provided by files in src/port/.
345  */
346 #ifndef HAVE_CRYPT
347 extern char *crypt(const char *key, const char *setting);
348 #endif
349
350 /* WIN32 handled in port/win32.h */
351 #ifndef WIN32
352 #define pgoff_t off_t
353 #if defined(__bsdi__) || defined(__NetBSD__)
354 extern int      fseeko(FILE *stream, off_t offset, int whence);
355 extern off_t ftello(FILE *stream);
356 #endif
357 #endif
358
359 #ifndef HAVE_ERAND48
360 /* we assume all of these are present or missing together */
361 extern double erand48(unsigned short xseed[3]);
362 extern long lrand48(void);
363 extern void srand48(long seed);
364 #endif
365
366 #ifndef HAVE_FSEEKO
367 #define fseeko(a, b, c) fseek(a, b, c)
368 #define ftello(a)               ftell(a)
369 #endif
370
371 #ifndef HAVE_GETOPT
372 extern int      getopt(int nargc, char *const * nargv, const char *ostr);
373 #endif
374
375 #ifndef HAVE_ISINF
376 extern int      isinf(double x);
377 #endif
378
379 #ifndef HAVE_RINT
380 extern double rint(double x);
381 #endif
382
383 #ifndef HAVE_INET_ATON
384 #include <netinet/in.h>
385 #include <arpa/inet.h>
386 extern int      inet_aton(const char *cp, struct in_addr * addr);
387 #endif
388
389 #ifndef HAVE_STRDUP
390 extern char *strdup(const char *str);
391 #endif
392
393 #if !HAVE_DECL_STRLCAT
394 extern size_t strlcat(char *dst, const char *src, size_t siz);
395 #endif
396
397 #if !HAVE_DECL_STRLCPY
398 extern size_t strlcpy(char *dst, const char *src, size_t siz);
399 #endif
400
401 #if !defined(HAVE_RANDOM) && !defined(__BORLANDC__)
402 extern long random(void);
403 #endif
404
405 #ifndef HAVE_UNSETENV
406 extern void unsetenv(const char *name);
407 #endif
408
409 #ifndef HAVE_SRANDOM
410 extern void srandom(unsigned int seed);
411 #endif
412
413 /* thread.h */
414 extern char *pqStrerror(int errnum, char *strerrbuf, size_t buflen);
415
416 #if !defined(WIN32) || defined(__CYGWIN__)
417 extern int pqGetpwuid(uid_t uid, struct passwd * resultbuf, char *buffer,
418                    size_t buflen, struct passwd ** result);
419 #endif
420
421 extern int pqGethostbyname(const char *name,
422                                 struct hostent * resultbuf,
423                                 char *buffer, size_t buflen,
424                                 struct hostent ** result,
425                                 int *herrno);
426
427 extern void pg_qsort(void *base, size_t nel, size_t elsize,
428                  int (*cmp) (const void *, const void *));
429
430 #define qsort(a,b,c,d) pg_qsort(a,b,c,d)
431
432 typedef int (*qsort_arg_comparator) (const void *a, const void *b, void *arg);
433
434 extern void qsort_arg(void *base, size_t nel, size_t elsize,
435                   qsort_arg_comparator cmp, void *arg);
436
437 /* port/chklocale.c */
438 extern int      pg_get_encoding_from_locale(const char *ctype);
439
440 /* port/inet_net_ntop.c */
441 extern char *inet_net_ntop(int af, const void *src, int bits,
442                           char *dst, size_t size);
443
444 #endif   /* PG_PORT_H */