/[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 280 - (hide annotations) (download) (as text)
Wed Jul 5 16:51:12 2006 UTC (12 years, 1 month ago) by azarah
Original Path: trunk/src/sandbox_utils.c
File MIME type: text/x-csrc
File size: 3726 byte(s)
Use librcutil.

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 azarah 216 * Author: Martin Schlemmer <azarah@gentoo.org>
6 ferringb 2 *
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 azarah 139 #include <libgen.h>
20 ferringb 2
21 ferringb 13 #include "config.h"
22 azarah 191 #include "localdecls.h"
23 ferringb 2
24 azarah 191 #include "sandbox.h"
25 ferringb 2
26 azarah 189 void get_sandbox_lib(char *path)
27 ferringb 2 {
28 azarah 181 #ifdef SB_HAVE_MULTILIB
29 azarah 140 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
30 ferringb 2 #else
31 azarah 140 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
32 azarah 139 if (0 >= exists(path)) {
33 azarah 140 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
34 ferringb 2 }
35     #endif
36     }
37    
38 azarah 143 #ifdef OUTSIDE_LIBSANDBOX
39    
40 azarah 189 void get_sandbox_rc(char *path)
41 ferringb 2 {
42 azarah 140 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
43 ferringb 2 }
44    
45 azarah 189 void get_sandbox_log(char *path)
46 ferringb 2 {
47     char *sandbox_log_env = NULL;
48    
49 azarah 97 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
50    
51 ferringb 2 /* THIS CHUNK BREAK THINGS BY DOING THIS:
52     * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
53     */
54 azarah 97 if ((NULL != sandbox_log_env) &&
55     (NULL != strchr(sandbox_log_env, '/')))
56     sandbox_log_env = NULL;
57 ferringb 2
58 azarah 140 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
59 azarah 134 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
60 azarah 99 (sandbox_log_env == NULL ? "" : sandbox_log_env),
61     (sandbox_log_env == NULL ? "" : "-"),
62     getpid(), LOG_FILE_EXT);
63 ferringb 2 }
64    
65 azarah 189 void get_sandbox_debug_log(char *path)
66 azarah 101 {
67 azarah 97 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 azarah 140 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
79 azarah 134 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
80 azarah 99 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
81     (sandbox_debug_log_env == NULL ? "" : "-"),
82     getpid(), LOG_FILE_EXT);
83 azarah 97 }
84    
85 azarah 189 int get_tmp_dir(char *path)
86 ferringb 2 {
87 azarah 139 if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
88     : TMPDIR,
89 azarah 140 path)) {
90     if (NULL == realpath(TMPDIR, path))
91 azarah 139 return -1;
92 ferringb 2 }
93    
94 azarah 139 return 0;
95 ferringb 2 }
96    
97 azarah 189 long file_length(int fd)
98 ferringb 2 {
99 azarah 195 struct stat st;
100     int retval;
101 azarah 139
102 azarah 195 retval = fstat(fd, &st);
103 azarah 196 if (-1 == retval)
104 azarah 195 return 0;
105 azarah 139
106 azarah 195 return (st.st_size);
107 ferringb 2 }
108    
109 azarah 143 #endif /* OUTSIDE_LIBSANDBOX */
110 ferringb 2
111 azarah 260 bool is_env_on (const char *env)
112     {
113     if ((NULL != env) && (NULL != getenv(env)) &&
114     ((0 == strncasecmp(getenv(env), "1", 1)) ||
115 azarah 263 (0 == strncasecmp(getenv(env), "true", 4)) ||
116 azarah 260 (0 == strncasecmp(getenv(env), "yes", 3))))
117 azarah 280 return TRUE;
118 azarah 260
119 azarah 280 return FALSE;
120 azarah 260 }
121    
122 azarah 263 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 azarah 280 return TRUE;
129 azarah 263
130 azarah 280 return FALSE;
131 azarah 263 }
132    
133 azarah 280 #ifndef OUTSIDE_LIBSANDBOX
134    
135 azarah 216 char * gstrndup (const char *str, size_t size)
136     {
137     char *new_str = NULL;
138     size_t len;
139 azarah 143
140 azarah 216 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 azarah 280 #endif /* !OUTSIDE_LIBSANDBOX */
170 azarah 216
171 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