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

Contents of /trunk/sandbox_futils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 140 - (show annotations) (download) (as text)
Thu Jul 7 22:01:14 2005 UTC (8 years, 9 months ago) by azarah
File MIME type: text/x-csrc
File size: 3949 byte(s)
Redo the interface of the get_* functions so that we do not leak memory.
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 void get_sandbox_path(char *argv0, char *path)
38 {
39 memset(path, 0, sizeof(path));
40 /* ARGV[0] specifies full path */
41 if ((NULL != argv0) && (argv0[0] == '/')) {
42 snprintf(path, SB_PATH_MAX, "%s", argv0);
43
44 /* ARGV[0] specifies relative path */
45 } else {
46 if (-1 != readlink("/proc/self/exe", path, SB_PATH_MAX))
47 path[SB_PATH_MAX - 1] = '\0';
48 else
49 path[0] = '\0';
50 }
51
52 /* Return just directory */
53 dirname(path);
54 }
55
56 SB_STATIC void get_sandbox_lib(char *path)
57 {
58 #ifdef SB_HAVE_64BIT_ARCH
59 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
60 #else
61 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
62 if (0 >= exists(path)) {
63 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
64 }
65 #endif
66 }
67
68 SB_STATIC void get_sandbox_rc(char *path)
69 {
70 char sb_path[SB_PATH_MAX];
71
72 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
73 if (0 >= exists(path)) {
74 get_sandbox_path(NULL, sb_path);
75 snprintf(path, SB_PATH_MAX, "%s%s", sb_path, BASHRC_NAME);
76 }
77 }
78
79 SB_STATIC void get_sandbox_log(char *path)
80 {
81 char *sandbox_log_env = NULL;
82
83 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
84
85 /* THIS CHUNK BREAK THINGS BY DOING THIS:
86 * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
87 */
88 if ((NULL != sandbox_log_env) &&
89 (NULL != strchr(sandbox_log_env, '/')))
90 sandbox_log_env = NULL;
91
92 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
93 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
94 (sandbox_log_env == NULL ? "" : sandbox_log_env),
95 (sandbox_log_env == NULL ? "" : "-"),
96 getpid(), LOG_FILE_EXT);
97 }
98
99 SB_STATIC void get_sandbox_debug_log(char *path)
100 {
101 char *sandbox_debug_log_env = NULL;
102
103 sandbox_debug_log_env = getenv(ENV_SANDBOX_DEBUG_LOG);
104
105 /* THIS CHUNK BREAK THINGS BY DOING THIS:
106 * SANDBOX_DEBUG_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
107 */
108 if ((NULL != sandbox_debug_log_env) &&
109 (NULL != strchr(sandbox_debug_log_env, '/')))
110 sandbox_debug_log_env = NULL;
111
112 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
113 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
114 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
115 (sandbox_debug_log_env == NULL ? "" : "-"),
116 getpid(), LOG_FILE_EXT);
117 }
118
119 SB_STATIC int get_tmp_dir(char *path)
120 {
121 if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
122 : TMPDIR,
123 path)) {
124 if (NULL == realpath(TMPDIR, path))
125 return -1;
126 }
127
128 return 0;
129 }
130
131 SB_STATIC int exists(const char *pathname)
132 {
133 struct stat buf;
134 int retval;
135
136 if ((NULL == pathname) || (0 == strlen(pathname)))
137 return 0;
138
139 retval = lstat(pathname, &buf);
140 if (-1 != retval)
141 return 1;
142 /* Some or other error occurred */
143 if (ENOENT != errno)
144 return -1;
145
146 return 0;
147 }
148
149 SB_STATIC int is_file(const char *pathname)
150 {
151 struct stat buf;
152 int retval;
153
154 if ((NULL == pathname) || (0 == strlen(pathname)))
155 return 0;
156
157 retval = lstat(pathname, &buf);
158 if ((-1 != retval) && (S_ISREG(buf.st_mode)))
159 return 1;
160 /* Some or other error occurred */
161 if ((-1 == retval) && (ENOENT != errno))
162 return -1;
163
164 return 0;
165 }
166
167 SB_STATIC long file_length(int fd)
168 {
169 long pos, len;
170
171 pos = lseek(fd, 0L, SEEK_CUR);
172 len = lseek(fd, 0L, SEEK_END);
173 lseek(fd, pos, SEEK_SET);
174
175 return (len);
176 }
177
178
179 // 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