/[path-sandbox]/trunk/src/sandbox_utils.c
Gentoo

Contents of /trunk/src/sandbox_utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 263 - (show annotations) (download) (as text)
Tue Jul 4 14:58:16 2006 UTC (8 years, 3 months ago) by azarah
File MIME type: text/x-csrc
File size: 4722 byte(s)
Add is_env_off().  Also check for true/false. Use is_env_*.

1 /*
2 * Copyright (C) 2002 Brad House <brad@mainstreetsoftworks.com>
3 * Distributed under the terms of the GNU General Public License, v2 or later
4 * Author: Brad House <brad@mainstreetsoftworks.com>
5 * Author: Martin Schlemmer <azarah@gentoo.org>
6 *
7 * $Header$
8 *
9 */
10
11 #include <errno.h>
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <limits.h>
15 #include <string.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18 #include <fcntl.h>
19 #include <libgen.h>
20
21 #include "config.h"
22 #include "localdecls.h"
23
24 #include "sandbox.h"
25
26 void get_sandbox_lib(char *path)
27 {
28 #ifdef SB_HAVE_MULTILIB
29 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
30 #else
31 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
32 if (0 >= exists(path)) {
33 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
34 }
35 #endif
36 }
37
38 #ifdef OUTSIDE_LIBSANDBOX
39
40 void get_sandbox_rc(char *path)
41 {
42 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
43 }
44
45 void get_sandbox_log(char *path)
46 {
47 char *sandbox_log_env = NULL;
48
49 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
50
51 /* THIS CHUNK BREAK THINGS BY DOING THIS:
52 * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
53 */
54 if ((NULL != sandbox_log_env) &&
55 (NULL != strchr(sandbox_log_env, '/')))
56 sandbox_log_env = NULL;
57
58 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
59 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
60 (sandbox_log_env == NULL ? "" : sandbox_log_env),
61 (sandbox_log_env == NULL ? "" : "-"),
62 getpid(), LOG_FILE_EXT);
63 }
64
65 void get_sandbox_debug_log(char *path)
66 {
67 char *sandbox_debug_log_env = NULL;
68
69 sandbox_debug_log_env = getenv(ENV_SANDBOX_DEBUG_LOG);
70
71 /* THIS CHUNK BREAK THINGS BY DOING THIS:
72 * SANDBOX_DEBUG_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
73 */
74 if ((NULL != sandbox_debug_log_env) &&
75 (NULL != strchr(sandbox_debug_log_env, '/')))
76 sandbox_debug_log_env = NULL;
77
78 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
79 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
80 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
81 (sandbox_debug_log_env == NULL ? "" : "-"),
82 getpid(), LOG_FILE_EXT);
83 }
84
85 int get_tmp_dir(char *path)
86 {
87 if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
88 : TMPDIR,
89 path)) {
90 if (NULL == realpath(TMPDIR, path))
91 return -1;
92 }
93
94 return 0;
95 }
96
97 #endif /* OUTSIDE_LIBSANDBOX */
98
99 int exists(const char *pathname)
100 {
101 struct stat buf;
102 int retval;
103
104 if ((NULL == pathname) || (0 == strlen(pathname)))
105 return 0;
106
107 retval = lstat(pathname, &buf);
108 if (-1 != retval)
109 return 1;
110 /* Some or other error occurred */
111 if (ENOENT != errno)
112 return -1;
113
114 return 0;
115 }
116
117 #ifdef OUTSIDE_LIBSANDBOX
118
119 int is_file(const char *pathname)
120 {
121 struct stat buf;
122 int retval;
123
124 if ((NULL == pathname) || (0 == strlen(pathname)))
125 return 0;
126
127 retval = lstat(pathname, &buf);
128 if ((-1 != retval) && (S_ISREG(buf.st_mode)))
129 return 1;
130 /* Some or other error occurred */
131 if ((-1 == retval) && (ENOENT != errno))
132 return -1;
133
134 return 0;
135 }
136
137 int is_dir(const char *pathname, int follow_link)
138 {
139 struct stat buf;
140 int retval;
141
142 if ((NULL == pathname) || (0 == strlen(pathname)))
143 return 0;
144
145 retval = follow_link ? stat(pathname, &buf) : lstat(pathname, &buf);
146 if ((-1 != retval) && (S_ISDIR(buf.st_mode)))
147 return 1;
148 /* Some or other error occurred */
149 if ((-1 == retval) && (ENOENT != errno))
150 return -1;
151
152 return 0;
153 }
154
155 long file_length(int fd)
156 {
157 struct stat st;
158 int retval;
159
160 retval = fstat(fd, &st);
161 if (-1 == retval)
162 return 0;
163
164 return (st.st_size);
165 }
166
167 #endif /* OUTSIDE_LIBSANDBOX */
168
169 bool is_env_on (const char *env)
170 {
171 if ((NULL != env) && (NULL != getenv(env)) &&
172 ((0 == strncasecmp(getenv(env), "1", 1)) ||
173 (0 == strncasecmp(getenv(env), "true", 4)) ||
174 (0 == strncasecmp(getenv(env), "yes", 3))))
175 return true;
176
177 return false;
178 }
179
180 bool is_env_off (const char *env)
181 {
182 if ((NULL != env) && (NULL != getenv(env)) &&
183 ((0 == strncasecmp(getenv(env), "0", 1)) ||
184 (0 == strncasecmp(getenv(env), "false", 5)) ||
185 (0 == strncasecmp(getenv(env), "no", 2))))
186 return true;
187
188 return false;
189 }
190
191 char * gstrndup (const char *str, size_t size)
192 {
193 char *new_str = NULL;
194 size_t len;
195
196 if (NULL == str)
197 return NULL;
198
199 /* Check lenght of str without breaching the size limit */
200 for (len = 0; (len < size) && ('\0' != str[len]); len++);
201
202 new_str = malloc (len + 1);
203 if (NULL == new_str)
204 return NULL;
205
206 /* Make sure our string is NULL terminated */
207 new_str[len] = '\0';
208
209 return (char *) memcpy (new_str, str, len);
210 }
211
212 char *
213 gbasename (const char *path)
214 {
215 char *new_path = NULL;
216
217 if (NULL == path)
218 return NULL;
219
220 /* Copied from glibc */
221 new_path = strrchr (path, '/');
222 return new_path ? new_path + 1 : (char *) path;
223 }
224
225
226 // vim:noexpandtab noai:cindent ai

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20