1 /* Copyright 2012-present Facebook, Inc.
2  * Licensed under the Apache License, Version 2.0 */
3 
4 #ifndef WATCHMAN_SYSTEM_H
5 #define WATCHMAN_SYSTEM_H
6 
7 #ifndef _GNU_SOURCE
8 #define _GNU_SOURCE 1
9 #endif
10 #define __STDC_LIMIT_MACROS
11 #define __STDC_FORMAT_MACROS
12 #include "config.h"
13 
14 #ifdef WATCHMAN_FACEBOOK_INTERNAL
15 #include "common/base/BuildInfo.h"
16 #undef PACKAGE_VERSION
17 #define PACKAGE_VERSION BuildInfo_kTimeISO8601
18 #define WATCHMAN_BUILD_INFO BuildInfo_kUpstreamRevision
19 #endif
20 
21 #include <assert.h>
22 #if HAVE_UNISTD_H
23 #include <unistd.h>
24 #endif
25 #include <ctype.h>
26 #include <stdint.h>
27 #include <sys/stat.h>
28 #if HAVE_SYS_INOTIFY_H
29 # include <sys/inotify.h>
30 #endif
31 #if HAVE_SYS_EVENT_H
32 # include <sys/event.h>
33 #endif
34 #if HAVE_PORT_H
35 # include <port.h>
36 #endif
37 #include <signal.h>
38 #include <errno.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <stdio.h>
42 #include <sys/types.h>
43 #include <stdbool.h>
44 #include <sys/time.h>
45 #include <time.h>
46 #ifndef _WIN32
47 #include <grp.h>
48 #include <libgen.h>
49 #endif
50 #include <inttypes.h>
51 #include <limits.h>
52 #ifndef _WIN32
53 #include <sys/socket.h>
54 #include <sys/un.h>
55 #endif
56 #include <fcntl.h>
57 #if defined(__linux__) && !defined(O_CLOEXEC)
58 # define O_CLOEXEC   02000000 /* set close_on_exec, from asm/fcntl.h */
59 #endif
60 #ifndef O_CLOEXEC
61 # define O_CLOEXEC 0
62 #endif
63 #ifndef _WIN32
64 #include <poll.h>
65 #include <sys/wait.h>
66 #endif
67 #ifdef HAVE_PCRE_H
68 # include <pcre.h>
69 #endif
70 #ifdef HAVE_EXECINFO_H
71 # include <execinfo.h>
72 #endif
73 #ifndef _WIN32
74 #include <sys/uio.h>
75 #include <pwd.h>
76 #include <sysexits.h>
77 #endif
78 #include <spawn.h>
79 #include <stddef.h>
80 #ifdef HAVE_SYS_PARAM_H
81 # include <sys/param.h>
82 #endif
83 #ifdef HAVE_SYS_RESOURCE_H
84 # include <sys/resource.h>
85 #endif
86 
87 #ifdef _WIN32
88 # define PRIsize_t "Iu"
89 #else
90 # define PRIsize_t "zu"
91 #endif
92 
93 #if defined(__clang__)
94 # if __has_feature(address_sanitizer)
95 #  define WATCHMAN_ASAN 1
96 # endif
97 #elif defined (__GNUC__) && \
98       (((__GNUC__ == 4) && (__GNUC_MINOR__ >= 8)) || (__GNUC__ >= 5)) && \
99       __SANITIZE_ADDRESS__
100 # define WATCHMAN_ASAN 1
101 #endif
102 
103 #ifndef WATCHMAN_ASAN
104 # define WATCHMAN_ASAN 0
105 #endif
106 
107 #ifdef HAVE_CORESERVICES_CORESERVICES_H
108 # include <CoreServices/CoreServices.h>
109 # if __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ < 1070
110 #  define HAVE_FSEVENTS 0
111 # else
112 #  define HAVE_FSEVENTS 1
113 # endif
114 #endif
115 
116 // We make use of constructors to glue together modules
117 // without maintaining static lists of things in the build
118 // configuration.  These are helpers to make this work
119 // more portably
120 #ifdef _WIN32
121 #pragma section(".CRT$XCU", read)
122 # define w_ctor_fn_type(sym) void __cdecl sym(void)
123 # define w_ctor_fn_reg(sym) \
124   static __declspec(allocate(".CRT$XCU")) \
125     void (__cdecl * w_paste1(sym, _reg))(void) = sym;
126 #else
127 # define w_ctor_fn_type(sym) \
128   __attribute__((constructor)) void sym(void)
129 # define w_ctor_fn_reg(sym) /* not needed */
130 #endif
131 
132 /* sane, reasonably large filename size that we'll use
133  * throughout; POSIX seems to define smallish buffers
134  * that seem risky */
135 #define WATCHMAN_NAME_MAX   4096
136 
137 // rpmbuild may enable fortify which turns on
138 // warn_unused_result on a number of system functions.
139 // This gives us a reasonably clean way to suppress
140 // these warnings when we're using stack protection.
141 #if __USE_FORTIFY_LEVEL > 0
142 # define ignore_result(x) \
143   do { __typeof__(x) _res = x; (void)_res; } while(0)
144 #elif _MSC_VER >= 1400
145 # define ignore_result(x) \
146   do { int _res = (int)x; (void)_res; } while(0)
147 #else
148 # define ignore_result(x) x
149 #endif
150 
151 // self-documenting hint to the compiler that we didn't use it
152 #define unused_parameter(x)  (void)x
153 
154 #ifdef __cplusplus
155 extern "C" {
156 #endif
157 
158 #ifndef _WIN32
159 // Not explicitly exported on Darwin, so we get to define it.
160 extern char **environ;
161 #endif
162 
163 #ifdef __cplusplus
164 }
165 #endif
166 
167 #endif
168 
169 /* vim:ts=2:sw=2:et:
170  */
171