/[path-sandbox]/trunk/sandbox_futils.c
Gentoo

Contents of /trunk/sandbox_futils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (show annotations) (download) (as text)
Thu Jul 7 21:42:33 2005 UTC (8 years, 9 months ago) by azarah
File MIME type: text/x-csrc
File size: 4113 byte(s)
Major cleanup of sandbox_futils.c.  Removed most of the functions as we now
write to /var/log/sandbox/, so in theory do not need all that.

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 *
6 * $Header$
7 *
8 */
9
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <signal.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <limits.h>
16 #include <string.h>
17 #include <stdarg.h>
18 #include <sys/file.h>
19 #include <sys/stat.h>
20 #include <sys/time.h>
21 #include <sys/types.h>
22 #include <sys/resource.h>
23 #include <sys/wait.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <libgen.h>
27
28 #include <grp.h>
29 #include <pwd.h>
30
31 #include "sandbox.h"
32 #include "config.h"
33
34 /* glibc modified getcwd() functions */
35 SB_STATIC char *egetcwd(char *, size_t);
36
37 SB_STATIC char *get_sandbox_path(char *argv0)
38 {
39 char path[SB_PATH_MAX];
40
41 memset(path, 0, sizeof(path));
42 /* ARGV[0] specifies full path */
43 if (argv0[0] == '/') {
44 snprintf(path, SB_PATH_MAX, "%s", argv0);
45
46 /* ARGV[0] specifies relative path */
47 } else {
48 if (-1 != readlink("/proc/self/exe", path, sizeof(path)))
49 path[sizeof(path) - 1] = '\0';
50 else
51 path[0] = '\0';
52 }
53
54 /* Return just directory */
55 return (dirname(path));
56 }
57
58 SB_STATIC char *get_sandbox_lib(char *sb_path)
59 {
60 char path[SB_PATH_MAX];
61
62 #ifdef SB_HAVE_64BIT_ARCH
63 snprintf(path, sizeof(path), "%s", LIB_NAME);
64 #else
65 snprintf(path, sizeof(path), "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
66 if (0 >= exists(path)) {
67 snprintf(path, sizeof(path), "%s%s", sb_path, LIB_NAME);
68 }
69 #endif
70 return (strdup(path));
71 }
72
73 SB_STATIC char *get_sandbox_rc(char *sb_path)
74 {
75 char path[SB_PATH_MAX];
76
77 snprintf(path, sizeof(path), "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
78 if (0 >= exists(path)) {
79 snprintf(path, sizeof(path), "%s%s", sb_path, BASHRC_NAME);
80 }
81 return (strdup(path));
82 }
83
84 SB_STATIC char *get_sandbox_log()
85 {
86 char path[SB_PATH_MAX];
87 char *sandbox_log_env = NULL;
88
89 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
90
91 /* THIS CHUNK BREAK THINGS BY DOING THIS:
92 * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
93 */
94 if ((NULL != sandbox_log_env) &&
95 (NULL != strchr(sandbox_log_env, '/')))
96 sandbox_log_env = NULL;
97
98 snprintf(path, sizeof(path) - 1, "%s%s%s%s%d%s",
99 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
100 (sandbox_log_env == NULL ? "" : sandbox_log_env),
101 (sandbox_log_env == NULL ? "" : "-"),
102 getpid(), LOG_FILE_EXT);
103 return (strdup(path));
104 }
105
106 SB_STATIC char *get_sandbox_debug_log()
107 {
108 char path[SB_PATH_MAX];
109 char *sandbox_debug_log_env = NULL;
110
111 sandbox_debug_log_env = getenv(ENV_SANDBOX_DEBUG_LOG);
112
113 /* THIS CHUNK BREAK THINGS BY DOING THIS:
114 * SANDBOX_DEBUG_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
115 */
116 if ((NULL != sandbox_debug_log_env) &&
117 (NULL != strchr(sandbox_debug_log_env, '/')))
118 sandbox_debug_log_env = NULL;
119
120 snprintf(path, sizeof(path) - 1, "%s%s%s%s%d%s",
121 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
122 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
123 (sandbox_debug_log_env == NULL ? "" : "-"),
124 getpid(), LOG_FILE_EXT);
125 return (strdup(path));
126 }
127
128 SB_STATIC int get_tmp_dir(char *tmp_dir)
129 {
130 if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
131 : TMPDIR,
132 tmp_dir)) {
133 if (NULL == realpath(TMPDIR, tmp_dir))
134 return -1;
135 }
136
137 return 0;
138 }
139
140 SB_STATIC int exists(const char *pathname)
141 {
142 struct stat buf;
143 int retval;
144
145 if ((NULL == pathname) || (0 == strlen(pathname)))
146 return 0;
147
148 retval = lstat(pathname, &buf);
149 if (-1 != retval)
150 return 1;
151 /* Some or other error occurred */
152 if (ENOENT != errno)
153 return -1;
154
155 return 0;
156 }
157
158 SB_STATIC int is_file(const char *pathname)
159 {
160 struct stat buf;
161 int retval;
162
163 if ((NULL == pathname) || (0 == strlen(pathname)))
164 return 0;
165
166 retval = lstat(pathname, &buf);
167 if ((-1 != retval) && (S_ISREG(buf.st_mode)))
168 return 1;
169 /* Some or other error occurred */
170 if ((-1 == retval) && (ENOENT != errno))
171 return -1;
172
173 return 0;
174 }
175
176 SB_STATIC long file_length(int fd)
177 {
178 long pos, len;
179
180 pos = lseek(fd, 0L, SEEK_CUR);
181 len = lseek(fd, 0L, SEEK_END);
182 lseek(fd, pos, SEEK_SET);
183
184 return (len);
185 }
186
187
188 // 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