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

Contents of /trunk/src/sandbox_futils.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 196 - (show annotations) (download) (as text)
Thu Dec 1 11:33:20 2005 UTC (8 years, 9 months ago) by azarah
File MIME type: text/x-csrc
File size: 3502 byte(s)
Wrap mknod() as well.  Misc cleanups.
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 <stdio.h>
12 #include <stdlib.h>
13 #include <limits.h>
14 #include <string.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <libgen.h>
19
20 #include "config.h"
21 #include "localdecls.h"
22
23 #include "sandbox.h"
24
25 void get_sandbox_lib(char *path)
26 {
27 #ifdef SB_HAVE_MULTILIB
28 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
29 #else
30 snprintf(path, SB_PATH_MAX, "%s/%s", LIBSANDBOX_PATH, LIB_NAME);
31 if (0 >= exists(path)) {
32 snprintf(path, SB_PATH_MAX, "%s", LIB_NAME);
33 }
34 #endif
35 }
36
37 #ifdef OUTSIDE_LIBSANDBOX
38
39 void get_sandbox_rc(char *path)
40 {
41 snprintf(path, SB_PATH_MAX, "%s/%s", SANDBOX_BASHRC_PATH, BASHRC_NAME);
42 }
43
44 void get_sandbox_log(char *path)
45 {
46 char *sandbox_log_env = NULL;
47
48 sandbox_log_env = getenv(ENV_SANDBOX_LOG);
49
50 /* THIS CHUNK BREAK THINGS BY DOING THIS:
51 * SANDBOX_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
52 */
53 if ((NULL != sandbox_log_env) &&
54 (NULL != strchr(sandbox_log_env, '/')))
55 sandbox_log_env = NULL;
56
57 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
58 SANDBOX_LOG_LOCATION, LOG_FILE_PREFIX,
59 (sandbox_log_env == NULL ? "" : sandbox_log_env),
60 (sandbox_log_env == NULL ? "" : "-"),
61 getpid(), LOG_FILE_EXT);
62 }
63
64 void get_sandbox_debug_log(char *path)
65 {
66 char *sandbox_debug_log_env = NULL;
67
68 sandbox_debug_log_env = getenv(ENV_SANDBOX_DEBUG_LOG);
69
70 /* THIS CHUNK BREAK THINGS BY DOING THIS:
71 * SANDBOX_DEBUG_LOG=/tmp/sandbox-app-admin/superadduser-1.0.7-11063.log
72 */
73 if ((NULL != sandbox_debug_log_env) &&
74 (NULL != strchr(sandbox_debug_log_env, '/')))
75 sandbox_debug_log_env = NULL;
76
77 snprintf(path, SB_PATH_MAX, "%s%s%s%s%d%s",
78 SANDBOX_LOG_LOCATION, DEBUG_LOG_FILE_PREFIX,
79 (sandbox_debug_log_env == NULL ? "" : sandbox_debug_log_env),
80 (sandbox_debug_log_env == NULL ? "" : "-"),
81 getpid(), LOG_FILE_EXT);
82 }
83
84 int get_tmp_dir(char *path)
85 {
86 if (NULL == realpath(getenv(ENV_TMPDIR) ? getenv(ENV_TMPDIR)
87 : TMPDIR,
88 path)) {
89 if (NULL == realpath(TMPDIR, path))
90 return -1;
91 }
92
93 return 0;
94 }
95
96 #endif /* OUTSIDE_LIBSANDBOX */
97
98 int exists(const char *pathname)
99 {
100 struct stat buf;
101 int retval;
102
103 if ((NULL == pathname) || (0 == strlen(pathname)))
104 return 0;
105
106 retval = lstat(pathname, &buf);
107 if (-1 != retval)
108 return 1;
109 /* Some or other error occurred */
110 if (ENOENT != errno)
111 return -1;
112
113 return 0;
114 }
115
116 #ifdef OUTSIDE_LIBSANDBOX
117
118 int is_file(const char *pathname)
119 {
120 struct stat buf;
121 int retval;
122
123 if ((NULL == pathname) || (0 == strlen(pathname)))
124 return 0;
125
126 retval = lstat(pathname, &buf);
127 if ((-1 != retval) && (S_ISREG(buf.st_mode)))
128 return 1;
129 /* Some or other error occurred */
130 if ((-1 == retval) && (ENOENT != errno))
131 return -1;
132
133 return 0;
134 }
135
136 int is_dir(const char *pathname, int follow_link)
137 {
138 struct stat buf;
139 int retval;
140
141 if ((NULL == pathname) || (0 == strlen(pathname)))
142 return 0;
143
144 retval = follow_link ? stat(pathname, &buf) : lstat(pathname, &buf);
145 if ((-1 != retval) && (S_ISDIR(buf.st_mode)))
146 return 1;
147 /* Some or other error occurred */
148 if ((-1 == retval) && (ENOENT != errno))
149 return -1;
150
151 return 0;
152 }
153
154 long file_length(int fd)
155 {
156 struct stat st;
157 int retval;
158
159 retval = fstat(fd, &st);
160 if (-1 == retval)
161 return 0;
162
163 return (st.st_size);
164 }
165
166 #endif /* OUTSIDE_LIBSANDBOX */
167
168
169 // 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