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

Contents of /trunk/src/sandbox_utils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 280 - (show annotations) (download) (as text)
Wed Jul 5 16:51:12 2006 UTC (8 years, 1 month ago) by azarah
File MIME type: text/x-csrc
File size: 3726 byte(s)
Use librcutil.

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 long file_length(int fd)
98 {
99 struct stat st;
100 int retval;
101
102 retval = fstat(fd, &st);
103 if (-1 == retval)
104 return 0;
105
106 return (st.st_size);
107 }
108
109 #endif /* OUTSIDE_LIBSANDBOX */
110
111 bool is_env_on (const char *env)
112 {
113 if ((NULL != env) && (NULL != getenv(env)) &&
114 ((0 == strncasecmp(getenv(env), "1", 1)) ||
115 (0 == strncasecmp(getenv(env), "true", 4)) ||
116 (0 == strncasecmp(getenv(env), "yes", 3))))
117 return TRUE;
118
119 return FALSE;
120 }
121
122 bool is_env_off (const char *env)
123 {
124 if ((NULL != env) && (NULL != getenv(env)) &&
125 ((0 == strncasecmp(getenv(env), "0", 1)) ||
126 (0 == strncasecmp(getenv(env), "false", 5)) ||
127 (0 == strncasecmp(getenv(env), "no", 2))))
128 return TRUE;
129
130 return FALSE;
131 }
132
133 #ifndef OUTSIDE_LIBSANDBOX
134
135 char * gstrndup (const char *str, size_t size)
136 {
137 char *new_str = NULL;
138 size_t len;
139
140 if (NULL == str)
141 return NULL;
142
143 /* Check lenght of str without breaching the size limit */
144 for (len = 0; (len < size) && ('\0' != str[len]); len++);
145
146 new_str = malloc (len + 1);
147 if (NULL == new_str)
148 return NULL;
149
150 /* Make sure our string is NULL terminated */
151 new_str[len] = '\0';
152
153 return (char *) memcpy (new_str, str, len);
154 }
155
156 char *
157 gbasename (const char *path)
158 {
159 char *new_path = NULL;
160
161 if (NULL == path)
162 return NULL;
163
164 /* Copied from glibc */
165 new_path = strrchr (path, '/');
166 return new_path ? new_path + 1 : (char *) path;
167 }
168
169 #endif /* !OUTSIDE_LIBSANDBOX */
170
171 // 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