/[path-sandbox]/trunk/libsbutil/get_sandbox_lib.c
Gentoo

Contents of /trunk/libsbutil/get_sandbox_lib.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (hide annotations) (download) (as text)
Thu Jul 7 21:42:33 2005 UTC (13 years ago) by azarah
Original Path: trunk/sandbox_futils.c
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 ferringb 2 /*
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 azarah 139 #include <libgen.h>
27 ferringb 2
28     #include <grp.h>
29     #include <pwd.h>
30    
31     #include "sandbox.h"
32 ferringb 13 #include "config.h"
33 ferringb 2
34     /* glibc modified getcwd() functions */
35 azarah 43 SB_STATIC char *egetcwd(char *, size_t);
36 ferringb 2
37 azarah 43 SB_STATIC char *get_sandbox_path(char *argv0)
38 ferringb 2 {
39 azarah 92 char path[SB_PATH_MAX];
40 ferringb 2
41     memset(path, 0, sizeof(path));
42     /* ARGV[0] specifies full path */
43     if (argv0[0] == '/') {
44 azarah 117 snprintf(path, SB_PATH_MAX, "%s", argv0);
45 ferringb 2
46     /* ARGV[0] specifies relative path */
47     } else {
48 azarah 92 if (-1 != readlink("/proc/self/exe", path, sizeof(path)))
49     path[sizeof(path) - 1] = '\0';
50     else
51     path[0] = '\0';
52 ferringb 2 }
53    
54     /* Return just directory */
55 azarah 139 return (dirname(path));
56 ferringb 2 }
57    
58 azarah 43 SB_STATIC char *get_sandbox_lib(char *sb_path)
59 ferringb 2 {
60 azarah 92 char path[SB_PATH_MAX];
61 ferringb 2
62     #ifdef SB_HAVE_64BIT_ARCH
63 azarah 30 snprintf(path, sizeof(path), "%s", LIB_NAME);
64 ferringb 2 #else
65 azarah 30 snprintf(path, sizeof(path), "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
66 azarah 139 if (0 >= exists(path)) {
67 ferringb 2 snprintf(path, sizeof(path), "%s%s", sb_path, LIB_NAME);
68     }
69     #endif
70     return (strdup(path));
71     }
72    
73 azarah 43 SB_STATIC char *get_sandbox_rc(char *sb_path)
74 ferringb 2 {
75 azarah 92 char path[SB_PATH_MAX];
76 ferringb 2
77 ferringb 13 snprintf(path, sizeof(path), "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
78 azarah 139 if (0 >= exists(path)) {
79 ferringb 2 snprintf(path, sizeof(path), "%s%s", sb_path, BASHRC_NAME);
80     }
81     return (strdup(path));
82     }
83    
84 azarah 139 SB_STATIC char *get_sandbox_log()
85 ferringb 2 {
86 azarah 92 char path[SB_PATH_MAX];
87 ferringb 2 char *sandbox_log_env = NULL;
88    
89 azarah 97 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
90    
91 ferringb 2 /* THIS CHUNK BREAK THINGS BY DOING THIS:
92     * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
93     */
94 azarah 97 if ((NULL != sandbox_log_env) &&
95     (NULL != strchr(sandbox_log_env, '/')))
96     sandbox_log_env = NULL;
97 ferringb 2
98 azarah 99 snprintf(path, sizeof(path) - 1, "%s%s%s%s%d%s",
99 azarah 134 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
100 azarah 99 (sandbox_log_env == NULL ? "" : sandbox_log_env),
101     (sandbox_log_env == NULL ? "" : "-"),
102     getpid(), LOG_FILE_EXT);
103 ferringb 2 return (strdup(path));
104     }
105    
106 azarah 139 SB_STATIC char *get_sandbox_debug_log()
107 azarah 101 {
108 azarah 97 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 azarah 99 snprintf(path, sizeof(path) - 1, "%s%s%s%s%d%s",
121 azarah 134 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
122 azarah 99 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
123     (sandbox_debug_log_env == NULL ? "" : "-"),
124     getpid(), LOG_FILE_EXT);
125 azarah 97 return (strdup(path));
126     }
127    
128 azarah 139 SB_STATIC int get_tmp_dir(char *tmp_dir)
129 ferringb 2 {
130 azarah 139 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 ferringb 2 }
136    
137 azarah 139 return 0;
138 ferringb 2 }
139    
140 azarah 139 SB_STATIC int exists(const char *pathname)
141 ferringb 2 {
142 azarah 139 struct stat buf;
143     int retval;
144 ferringb 2
145 azarah 139 if ((NULL == pathname) || (0 == strlen(pathname)))
146     return 0;
147 ferringb 2
148 azarah 139 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 ferringb 2
155 azarah 139 return 0;
156 ferringb 2 }
157    
158 azarah 139 SB_STATIC int is_file(const char *pathname)
159 ferringb 2 {
160 azarah 139 struct stat buf;
161     int retval;
162 ferringb 2
163 azarah 139 if ((NULL == pathname) || (0 == strlen(pathname)))
164 ferringb 2 return 0;
165    
166 azarah 139 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 ferringb 2
173 azarah 139 return 0;
174 ferringb 2 }
175    
176 azarah 43 SB_STATIC long file_length(int fd)
177 ferringb 2 {
178     long pos, len;
179 azarah 139
180     pos = lseek(fd, 0L, SEEK_CUR);
181 ferringb 2 len = lseek(fd, 0L, SEEK_END);
182     lseek(fd, pos, SEEK_SET);
183 azarah 139
184 ferringb 2 return (len);
185     }
186    
187    
188 azarah 30 // 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