/[path-sandbox]/trunk/libsandbox/wrappers.c
Gentoo

Contents of /trunk/libsandbox/wrappers.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 370 - (show annotations) (download) (as text)
Sun Nov 9 10:29:58 2008 UTC (5 years, 8 months ago) by vapier
File MIME type: text/x-csrc
File size: 2095 byte(s)
consolidate all random system includes into headers.h
1 /*
2 * wrappers.c
3 *
4 * Function wrapping functions.
5 *
6 * Copyright 1999-2008 Gentoo Foundation
7 * Licensed under the GPL-2
8 *
9 * Partly Copyright (C) 1998-9 Pancrazio `Ezio' de Mauro <p@demauro.net>,
10 * as some of the InstallWatch code was used.
11 */
12
13 #include "headers.h"
14 #include "sbutil.h"
15 #include "libsandbox.h"
16 #include "wrappers.h"
17
18 #if !defined(BROKEN_RTLD_NEXT) && defined(HAVE_RTLD_NEXT)
19 # define USE_RTLD_NEXT
20 #endif
21
22 /* Macro to check if a wrapper is defined, if not
23 * then try to resolve it again. */
24 #define check_dlsym(_name, _symname, _symver) \
25 { \
26 int old_errno = errno; \
27 if (NULL == _name) \
28 _name = get_dlsym(_symname, _symver); \
29 errno = old_errno; \
30 }
31
32 static void *libc_handle = NULL;
33
34 extern char sandbox_lib[SB_PATH_MAX];
35 extern int sandbox_on;
36
37 /* Need to include the function wrappers here, as they are needed below */
38 #include "symbols.h"
39
40
41 void *get_dlsym(const char *symname, const char *symver)
42 {
43 void *symaddr = NULL;
44
45 if (NULL == libc_handle) {
46 #if !defined(USE_RTLD_NEXT)
47 libc_handle = dlopen(LIBC_VERSION, RTLD_LAZY);
48 if (!libc_handle) {
49 fprintf(stderr, "libsandbox: Can't dlopen libc: %s\n",
50 dlerror());
51 exit(EXIT_FAILURE);
52 }
53 #else
54 libc_handle = RTLD_NEXT;
55 #endif
56 }
57
58 if (NULL == symver)
59 symaddr = dlsym(libc_handle, symname);
60 else
61 symaddr = dlvsym(libc_handle, symname, symver);
62 if (!symaddr) {
63 fprintf(stderr, "libsandbox: Can't resolve %s: %s\n",
64 symname, dlerror());
65 exit(EXIT_FAILURE);
66 }
67
68 return symaddr;
69 }
70
71 int libsb_open(const char *pathname, int flags, ...)
72 {
73 va_list ap;
74 int mode = 0;
75 int result = -1;
76
77 if (flags & O_CREAT) {
78 va_start(ap, flags);
79 mode = va_arg(ap, int);
80 va_end(ap);
81 }
82
83 check_dlsym(true_open_DEFAULT, symname_open_DEFAULT,
84 symver_open_DEFAULT);
85 if (flags & O_CREAT)
86 result = true_open_DEFAULT(pathname, flags, mode);
87 else
88 result = true_open_DEFAULT(pathname, flags);
89
90 return result;
91 }
92
93 char *libsb_getcwd(char *buf, size_t size)
94 {
95 check_dlsym(true_getcwd_DEFAULT, symname_getcwd_DEFAULT,
96 symver_getcwd_DEFAULT);
97
98 return true_getcwd_DEFAULT(buf, size);
99 }

  ViewVC Help
Powered by ViewVC 1.1.20