Revert erase relations before checking whether we already added it
[platform/upstream/rpm.git] / system.h
1 /**
2  * \file system.h
3  */
4
5 #ifndef H_SYSTEM
6 #define H_SYSTEM
7
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11
12 #include <sys/types.h>
13
14 #include <sys/stat.h>
15 #include <stdio.h>
16
17 #ifdef HAVE_SYS_PARAM_H
18 #include <sys/param.h>
19 #endif
20
21 /* <unistd.h> should be included before any preprocessor test
22    of _POSIX_VERSION.  */
23 #ifdef HAVE_UNISTD_H
24 #include <unistd.h>
25 #if !defined(__GLIBC__)
26 #ifdef __APPLE__
27 #include <crt_externs.h>
28 #define environ (*_NSGetEnviron())
29 #else
30 extern char ** environ;
31 #endif /* __APPLE__ */
32 #endif
33 #endif
34
35 #if TIME_WITH_SYS_TIME
36 # include <sys/time.h>
37 # include <time.h>
38 #else
39 # if HAVE_SYS_TIME_H
40 #  include <sys/time.h>
41 # else
42 #  include <time.h>
43 # endif
44 #endif
45
46 /* Since major is a function on SVR4, we can't use `ifndef major'.  */
47 #if MAJOR_IN_MKDEV
48 #include <sys/mkdev.h>
49 #define HAVE_MAJOR
50 #endif
51 #if MAJOR_IN_SYSMACROS
52 #include <sys/sysmacros.h>
53 #define HAVE_MAJOR
54 #endif
55 #ifdef major                    /* Might be defined in sys/types.h.  */
56 #define HAVE_MAJOR
57 #endif
58
59 #ifndef HAVE_MAJOR
60 #define major(dev)  (((dev) >> 8) & 0xff)
61 #define minor(dev)  ((dev) & 0xff)
62 #define makedev(maj, min)  (((maj) << 8) | (min))
63 #endif
64 #undef HAVE_MAJOR
65
66 #ifdef HAVE_UTIME_H
67 #include <utime.h>
68 #endif
69
70 #ifdef HAVE_STRING_H
71 # if !STDC_HEADERS && HAVE_MEMORY_H
72 #  include <memory.h>
73 # endif
74 # include <string.h>
75 #else
76 # include <strings.h>
77 char *memchr ();
78 #endif
79
80 #if !defined(HAVE_STPCPY)
81 char * stpcpy(char * dest, const char * src);
82 #endif
83
84 #if !defined(HAVE_STPNCPY)
85 char * stpncpy(char * dest, const char * src, size_t n);
86 #endif
87
88 #include <errno.h>
89 #ifndef errno
90 extern int errno;
91 #endif
92
93 #if HAVE_ERROR && HAVE_ERROR_H
94 #include <error.h>
95 #endif
96
97 #if HAVE___SECURE_GETENV
98 #define getenv(_s)      __secure_getenv(_s)
99 #endif
100
101 #ifdef STDC_HEADERS
102 /* FIX: shrug */
103 #define getopt system_getopt
104 #include <stdlib.h>
105 #undef getopt
106 #else /* not STDC_HEADERS */
107 char *getenv (const char *name);
108 #if ! HAVE_REALPATH
109 char *realpath(const char *path, char resolved_path []);
110 #endif
111 #endif /* STDC_HEADERS */
112
113 /* XXX solaris2.5.1 has not */
114 #if !defined(EXIT_FAILURE)
115 #define EXIT_FAILURE    1
116 #endif
117
118 #ifdef HAVE_FCNTL_H
119 #include <fcntl.h>
120 #else
121 #include <sys/file.h>
122 #endif
123
124 #if !defined(SEEK_SET)
125 #define SEEK_SET 0
126 #define SEEK_CUR 1
127 #define SEEK_END 2
128 #endif
129 #if !defined(F_OK) 
130 #define F_OK 0
131 #define X_OK 1
132 #define W_OK 2
133 #define R_OK 4
134 #endif
135
136 #ifdef HAVE_DIRENT_H
137 # include <dirent.h>
138 # define NLENGTH(direct) (strlen((direct)->d_name))
139 #else /* not HAVE_DIRENT_H */
140 # define dirent direct
141 # define NLENGTH(direct) ((direct)->d_namlen)
142 # ifdef HAVE_SYS_NDIR_H
143 #  include <sys/ndir.h>
144 # endif /* HAVE_SYS_NDIR_H */
145 # ifdef HAVE_SYS_DIR_H
146 #  include <sys/dir.h>
147 # endif /* HAVE_SYS_DIR_H */
148 # ifdef HAVE_NDIR_H
149 #  include <ndir.h>
150 # endif /* HAVE_NDIR_H */
151 #endif /* HAVE_DIRENT_H */
152
153 #include <ctype.h>
154
155 #if HAVE_SYS_MMAN_H
156 #include <sys/mman.h>
157 #endif
158
159 /* XXX FIXME: popt on sunos4.1.3: <sys/resource.h> requires <sys/time.h> */
160 #if HAVE_SYS_RESOURCE_H && HAVE_SYS_TIME_H
161 #include <sys/resource.h>
162 #endif
163
164 #if HAVE_SYS_UTSNAME_H
165 #include <sys/utsname.h>
166 #endif
167
168 #if HAVE_SYS_WAIT_H
169 #include <sys/wait.h>
170 #endif
171
172 #if HAVE_GETOPT_H
173 #include <getopt.h>
174 #endif
175
176 #if HAVE_GRP_H
177 #include <grp.h>
178 #endif
179
180 #if HAVE_LIMITS_H
181 #include <limits.h>
182 #endif
183
184 #ifndef PATH_MAX
185 #ifdef _POSIX_PATH_MAX
186 #define PATH_MAX _POSIX_PATH_MAX
187 #elif defined MAXPATHLEN
188 #define PATH_MAX MAXPATHLEN
189 #else
190 #define PATH_MAX 256
191 #endif
192 #endif
193
194 #if HAVE_ERR_H
195 #include <err.h>
196 #endif
197
198 #if HAVE_MALLOC_H 
199 #include <malloc.h>
200 #endif
201
202 #if WITH_SELINUX
203 #include <selinux/selinux.h>
204 #else
205 typedef char * security_context_t;
206
207 #define freecon(_c)
208
209 #define getfilecon(_fn, _c)     (-1)
210 #define lgetfilecon(_fn, _c)    (-1)
211 #define fgetfilecon(_fd, _c)    (-1)
212
213 #define setfilecon(_fn, _c)     (-1)
214 #define lsetfilecon(_fn, _c)    (-1)
215 #define fsetfilecon(_fd, _c)    (-1)
216
217 #define security_check_context(_c)      (0)
218
219 #define is_selinux_enabled()    (0)
220
221 #define matchpathcon_init(_fn)                  (-1)
222 #define matchpathcon_fini()                     (0)
223 #define matchpathcon(_fn, _fm, _c)              (-1)
224
225 #define rpm_execcon(_v, _fn, _av, _envp)        (0)
226 #endif
227
228 /**
229  * Wrapper to free(3), permit NULL, return NULL.
230  * @param p             memory to free
231  * @return              NULL always
232  */
233 static inline
234 void * _free(void * p)
235 {
236     if (p != NULL)      free(p);
237     return NULL;
238 }
239
240 #if WITH_CAP
241 #include <sys/capability.h>
242 #else
243 typedef void * cap_t;
244 #endif
245
246 #if WITH_ACL
247 #include <acl/libacl.h>
248 #endif
249
250 /**
251  * Wrapper to free(3), permit NULL, return NULL. 
252  * For documenting cases where const is used to protect long-lived 
253  * non-const data that's supposed to be freed.
254  * @param p             memory to free
255  * @return              NULL always
256  */
257 static inline
258 void * _constfree(const void * p)
259 {
260     if (p != NULL)      free((void *)p);
261     return NULL;
262 }
263
264 /* FIX: these are macros */
265 /**
266  */
267 void * xmalloc (size_t size);
268
269 /**
270  */
271 void * xcalloc (size_t nmemb, size_t size);
272
273 /**
274  * @todo Annotate ptr with returned/out.
275  */
276 void * xrealloc (void * ptr,
277                                         size_t size);
278
279 /**
280  */
281 char * xstrdup (const char *str);
282
283 /**
284  */
285 void * vmefail(size_t size);
286
287 #if HAVE_MCHECK_H
288 #include <mcheck.h>
289 /* Memory allocation via macro defs to get meaningful locations from mtrace() */
290 #if defined(__GNUC__)
291 #define xmalloc(_size)          (malloc(_size) ? : vmefail(_size))
292 #define xcalloc(_nmemb, _size)  (calloc((_nmemb), (_size)) ? : vmefail(_size))
293 #define xrealloc(_ptr, _size)   (realloc((_ptr), (_size)) ? : vmefail(_size))
294 #define xstrdup(_str)   (strcpy((malloc(strlen(_str)+1) ? : vmefail(strlen(_str)+1)), (_str)))
295 #endif  /* defined(__GNUC__) */
296 #endif  /* HAVE_MCHECK_H */
297
298 /* Retrofit glibc __progname */
299 #if defined __GLIBC__ && __GLIBC__ >= 2
300 #if __GLIBC_MINOR__ >= 1
301 #define __progname      __assert_program_name
302 #endif
303 #define setprogname(pn)
304 #else
305 #define __progname      program_name
306 #define setprogname(pn) \
307   { if ((__progname = strrchr(pn, '/')) != NULL) __progname++; \
308     else __progname = pn;               \
309   }
310 #endif
311 extern const char *__progname;
312
313 #if HAVE_NETDB_H
314 #include <netdb.h>
315 #endif
316
317 #if HAVE_PWD_H
318 #include <pwd.h>
319 #endif
320
321 /* Take care of NLS matters.  */
322
323 #if HAVE_LOCALE_H
324 # include <locale.h>
325 #endif
326 #if !HAVE_SETLOCALE
327 # define setlocale(Category, Locale) /* empty */
328 #endif
329
330 #if ENABLE_NLS
331 # include <libintl.h>
332 # define _(Text) dgettext (PACKAGE, Text)
333 #else
334 # undef bindtextdomain
335 # define bindtextdomain(Domain, Directory) /* empty */
336 # undef textdomain
337 # define textdomain(Domain) /* empty */
338 # define _(Text) Text
339 # undef dgettext
340 # define dgettext(DomainName, Text) Text
341 #endif
342
343 #define N_(Text) Text
344
345 /* ============== from misc/miscfn.h */
346
347 #if !defined(USE_GNU_GLOB) 
348 #if HAVE_FNMATCH_H
349 #include <fnmatch.h>
350 #endif
351
352 #if HAVE_GLOB_H 
353 #include <glob.h>
354 #endif
355 #else
356 #include "misc/glob.h"
357 #include "misc/fnmatch.h"
358 #endif
359
360 #if ! HAVE_S_IFSOCK
361 #define S_IFSOCK (0xc000)
362 #endif
363
364 #if ! HAVE_S_ISLNK
365 #define S_ISLNK(mode) ((mode & 0xf000) == S_IFLNK)
366 #endif
367
368 #if ! HAVE_S_ISSOCK
369 #define S_ISSOCK(mode) ((mode & 0xf000) == S_IFSOCK)
370 #endif
371
372 #if NEED_STRINGS_H
373 #include <strings.h>
374 #endif
375
376 #if NEED_MYREALLOC
377 #define realloc(ptr,size) myrealloc(ptr,size)
378 extern void *myrealloc(void *, size_t);
379 #endif
380
381 #if ! HAVE_SETENV
382 extern int setenv(const char *name, const char *value, int replace);
383 extern void unsetenv(const char *name);
384 #endif
385
386 #if HAVE_SYS_SOCKET_H
387 #include <sys/types.h>
388 #include <sys/socket.h>
389 #endif
390
391 #if HAVE_POLL_H
392 #include <poll.h>
393 #else
394 #if HAVE_SYS_SELECT_H
395 #include <sys/select.h>
396 #endif
397 #endif
398
399 /* Solaris <= 2.6 limits getpass return to only 8 chars */
400 #if HAVE_GETPASSPHRASE
401 #define getpass getpassphrase
402 #endif
403
404 #if ! HAVE_LCHOWN
405 #define lchown chown
406 #endif
407
408 #if HAVE_GETMNTINFO || HAVE_GETMNTINFO_R || HAVE_MNTCTL
409 # define GETMNTENT_ONE 0
410 # define GETMNTENT_TWO 0
411 # if HAVE_SYS_MNTCTL_H
412 #  include <sys/mntctl.h>
413 # endif
414 # if HAVE_SYS_VMOUNT_H
415 #  include <sys/vmount.h>
416 # endif
417 # if HAVE_SYS_MOUNT_H
418 #  include <sys/mount.h>
419 # endif
420 #elif HAVE_MNTENT_H || !(HAVE_GETMNTENT) || HAVE_STRUCT_MNTTAB
421 # if HAVE_MNTENT_H
422 #  include <stdio.h>
423 #  include <mntent.h>
424 #  define our_mntent struct mntent
425 #  define our_mntdir mnt_dir
426 # elif HAVE_STRUCT_MNTTAB
427 #  include <stdio.h>
428 #  include <mnttab.h>
429    struct our_mntent {
430        char * our_mntdir;
431    };
432    struct our_mntent *getmntent(FILE *filep);
433 #  define our_mntent struct our_mntent
434 # else
435 #  include <stdio.h>
436    struct our_mntent {
437        char * our_mntdir;
438    };
439    struct our_mntent *getmntent(FILE *filep);
440 #  define our_mntent struct our_mntent
441 # endif
442 # define GETMNTENT_ONE 1
443 # define GETMNTENT_TWO 0
444 #elif HAVE_SYS_MNTTAB_H
445 # include <stdio.h>
446 # include <sys/mnttab.h>
447 # define GETMNTENT_ONE 0
448 # define GETMNTENT_TWO 1
449 # define our_mntent struct mnttab
450 # define our_mntdir mnt_mountp
451 #else /* if !HAVE_MNTCTL */
452 # error Neither mntent.h, mnttab.h, or mntctl() exists. I cannot build on this system.
453 #endif
454
455 #ifndef MOUNTED
456 #define MOUNTED "/etc/mnttab"
457 #endif
458
459 #endif  /* H_SYSTEM */