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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 283 - (hide annotations) (download) (as text)
Thu Jul 6 08:49:13 2006 UTC (8 years, 5 months ago) by azarah
File MIME type: text/x-csrc
File size: 17153 byte(s)
Remove last libsandbox-only functions in favour for ones provided
by librcutil.  Setup log_domain properly for debugging.

1 ferringb 2 /*
2     ** Path sandbox for the gentoo linux portage package system, initially
3     ** based on the ROCK Linux Wrapper for getting a list of created files
4     **
5     ** to integrate with bash, bash should have been built like this
6     **
7     ** ./configure --prefix=<prefix> --host=<host> --without-gnu-malloc
8     **
9     ** it's very important that the --enable-static-link option is NOT specified
10     **
11     ** Copyright (C) 2001 Geert Bevin, Uwyn, http://www.uwyn.com
12     ** Distributed under the terms of the GNU General Public License, v2 or later
13     ** Author : Geert Bevin <gbevin@uwyn.com>
14     ** $Header$
15     */
16    
17     /* #define _GNU_SOURCE */
18    
19     #include <errno.h>
20     #include <signal.h>
21     #include <stdio.h>
22     #include <stdlib.h>
23     #include <limits.h>
24     #include <string.h>
25     #include <sys/wait.h>
26 azarah 261 #include <signal.h>
27 ferringb 2 #include <unistd.h>
28     #include <fcntl.h>
29 azarah 142
30 ferringb 2 #include "sandbox.h"
31 azarah 280 #include "rcscripts/rcutil.h"
32 ferringb 2
33 azarah 101 struct sandbox_info_t {
34     char sandbox_log[SB_PATH_MAX];
35     char sandbox_debug_log[SB_PATH_MAX];
36     char sandbox_lib[SB_PATH_MAX];
37     char sandbox_rc[SB_PATH_MAX];
38 azarah 157 char work_dir[SB_PATH_MAX];
39 azarah 101 char var_tmp_dir[SB_PATH_MAX];
40     char tmp_dir[SB_PATH_MAX];
41     char *home_dir;
42     } sandbox_info_t;
43 ferringb 2
44 azarah 99 static int print_debug = 0;
45    
46 azarah 267 volatile static int stop_called = 0;
47 azarah 261 volatile static pid_t child_pid = 0;
48    
49 azarah 283 static char log_domain[] = "sandbox";
50    
51 azarah 216 extern char **environ;
52    
53 azarah 141 int sandbox_setup(struct sandbox_info_t *sandbox_info)
54 azarah 101 {
55 azarah 165 if (NULL != getenv(ENV_PORTAGE_TMPDIR)) {
56     /* Portage handle setting SANDBOX_WRITE itself. */
57     sandbox_info->work_dir[0] = '\0';
58 azarah 157 } else {
59 azarah 224 if (NULL == getcwd(sandbox_info->work_dir, SB_PATH_MAX)) {
60 azarah 157 perror("sandbox: Failed to get current directory");
61     return -1;
62     }
63 azarah 101 }
64    
65 azarah 152 /* Do not resolve symlinks, etc .. libsandbox will handle that. */
66 azarah 282 if (!rc_is_dir(VAR_TMPDIR, TRUE)) {
67 azarah 145 perror("sandbox: Failed to get var_tmp_dir");
68 azarah 101 return -1;
69     }
70 azarah 152 snprintf(sandbox_info->var_tmp_dir, SB_PATH_MAX, "%s", VAR_TMPDIR);
71 azarah 139
72 azarah 101 if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
73 azarah 145 perror("sandbox: Failed to get tmp_dir");
74 azarah 101 return -1;
75     }
76 azarah 158 setenv(ENV_TMPDIR, sandbox_info->tmp_dir, 1);
77 azarah 101
78     sandbox_info->home_dir = getenv("HOME");
79     if (!sandbox_info->home_dir) {
80 azarah 158 sandbox_info->home_dir = sandbox_info->tmp_dir;
81 azarah 101 setenv("HOME", sandbox_info->home_dir, 1);
82     }
83    
84     /* Generate sandbox lib path */
85 azarah 140 get_sandbox_lib(sandbox_info->sandbox_lib);
86 azarah 101
87     /* Generate sandbox bashrc path */
88 azarah 140 get_sandbox_rc(sandbox_info->sandbox_rc);
89 azarah 101
90     /* Generate sandbox log full path */
91 azarah 140 get_sandbox_log(sandbox_info->sandbox_log);
92 azarah 282 if (rc_file_exists(sandbox_info->sandbox_log)) {
93 azarah 146 if (-1 == unlink(sandbox_info->sandbox_log)) {
94     perror("sandbox: Could not unlink old log file");
95 azarah 147 return -1;
96 azarah 146 }
97     }
98 azarah 101
99     /* Generate sandbox debug log full path */
100 azarah 140 get_sandbox_debug_log(sandbox_info->sandbox_debug_log);
101 azarah 282 if (rc_file_exists(sandbox_info->sandbox_debug_log)) {
102 azarah 146 if (-1 == unlink(sandbox_info->sandbox_debug_log)) {
103     perror("sandbox: Could not unlink old debug log file");
104 azarah 147 return -1;
105 azarah 146 }
106     }
107 azarah 101
108     return 0;
109     }
110    
111 azarah 88 int print_sandbox_log(char *sandbox_log)
112     {
113     int sandbox_log_file = -1;
114     char *beep_count_env = NULL;
115     int i, color, beep_count = 0;
116     long len = 0;
117     char *buffer = NULL;
118    
119 azarah 282 if (!rc_is_file(sandbox_log, FALSE)) {
120 azarah 145 perror("sandbox: Log file is not a regular file");
121 azarah 88 return 0;
122 azarah 139 }
123    
124     sandbox_log_file = open(sandbox_log, O_RDONLY);
125     if (-1 == sandbox_log_file) {
126 azarah 145 perror("sandbox: Could not open Log file");
127 azarah 139 return 0;
128     }
129 azarah 88
130     len = file_length(sandbox_log_file);
131     buffer = (char *)malloc((len + 1) * sizeof(char));
132     memset(buffer, 0, len + 1);
133     read(sandbox_log_file, buffer, len);
134 azarah 139 close(sandbox_log_file);
135 azarah 88
136 azarah 263 color = ((is_env_on(ENV_NOCOLOR)) ? 0 : 1);
137 azarah 88
138 azarah 279 SB_EERROR(color,
139 azarah 144 "--------------------------- ACCESS VIOLATION SUMMARY ---------------------------",
140     "\n");
141 azarah 279 SB_EERROR(color, "LOG FILE = \"%s\"", "\n\n", sandbox_log);
142 azarah 144 fprintf(stderr, "%s", buffer);
143     if (NULL != buffer)
144 azarah 88 free(buffer);
145 azarah 279 SB_EERROR(color,
146 azarah 144 "--------------------------------------------------------------------------------",
147     "\n");
148 azarah 88
149     beep_count_env = getenv(ENV_SANDBOX_BEEP);
150     if (beep_count_env)
151     beep_count = atoi(beep_count_env);
152     else
153     beep_count = DEFAULT_BEEP_COUNT;
154    
155     for (i = 0; i < beep_count; i++) {
156     fputc('\a', stderr);
157     if (i < beep_count - 1)
158     sleep(1);
159     }
160 azarah 139
161 azarah 88 return 1;
162     }
163    
164 azarah 30 void stop(int signum)
165 ferringb 2 {
166 azarah 267 if (0 == stop_called) {
167 ferringb 2 stop_called = 1;
168 azarah 145 printf("sandbox: Caught signal %d in pid %d\n",
169     signum, getpid());
170 ferringb 2 } else {
171 azarah 145 fprintf(stderr,
172     "sandbox: Signal already caught and busy still cleaning up!\n");
173 ferringb 2 }
174     }
175    
176 azarah 268 void usr1_handler(int signum, siginfo_t *siginfo, void *ucontext)
177     {
178     if (0 == stop_called) {
179     stop_called = 1;
180     printf("sandbox: Caught signal %d in pid %d\n",
181     signum, getpid());
182    
183     /* FIXME: This is really bad form, as we should kill the whole process
184     * tree, but currently that is too much work and not worth the
185     * effort. Thus we only kill the calling process and our child
186     * for now.
187     */
188     if (siginfo->si_pid > 0)
189     kill(siginfo->si_pid, SIGKILL);
190     kill(child_pid, SIGKILL);
191     } else {
192     fprintf(stderr,
193     "sandbox: Signal already caught and busy still cleaning up!\n");
194     }
195     }
196    
197 azarah 159 int get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
198 ferringb 2 {
199 azarah 159 int retval = 0;
200    
201 azarah 89 /* bzero out entire buffer then append trailing 0 */
202 azarah 93 memset(buf, 0, SB_BUF_LEN);
203 azarah 30
204 azarah 89 /* these could go into make.globals later on */
205 azarah 159 retval = snprintf(buf, SB_BUF_LEN,
206 azarah 89 "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
207 azarah 266 "/dev/zero:/dev/null:/dev/full:/dev/fd:/proc/self/fd:/dev/pts/:"
208 azarah 155 "/dev/vc/:/dev/pty:/dev/tty:/dev/tts:/dev/console:"
209 azarah 204 "/dev/shm:/dev/shm/ngpt:/var/log/scrollkeeper.log:"
210 azarah 89 "/usr/tmp/conftest:/usr/lib/conftest:"
211     "/usr/lib32/conftest:/usr/lib64/conftest:"
212     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
213 azarah 101 sandbox_info->home_dir, sandbox_info->home_dir,
214 azarah 165 sandbox_info->work_dir[0] != '\0' ? sandbox_info->work_dir
215     : "",
216 azarah 101 sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
217     "/tmp/:/var/tmp/");
218 azarah 159 if (SB_BUF_LEN <= retval) {
219     errno = EMSGSIZE;
220     perror("sandbox: Failed to generate SANDBOX_WRITE");
221     return -1;
222     }
223    
224     return 0;
225 azarah 89 }
226    
227 azarah 159 int get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
228 azarah 89 {
229 azarah 159 int retval = 0;
230 ferringb 2 /* bzero out entire buffer then append trailing 0 */
231 azarah 93 memset(buf, 0, SB_BUF_LEN);
232 ferringb 2
233 azarah 89 /* these should go into make.globals later on */
234 azarah 159 retval = snprintf(buf, SB_BUF_LEN, "%s/.:"
235 azarah 89 "/usr/lib/python2.0/:"
236     "/usr/lib/python2.1/:"
237     "/usr/lib/python2.2/:"
238     "/usr/lib/python2.3/:"
239     "/usr/lib/python2.4/:"
240     "/usr/lib/python2.5/:"
241 azarah 126 "/usr/lib/python3.0/:"
242     "/var/db/aliases.db:"
243     "/var/db/netgroup.db:"
244     "/var/db/netmasks.db:"
245     "/var/db/ethers.db:"
246     "/var/db/rpc.db:"
247     "/var/db/protocols.db:"
248     "/var/db/services.db:"
249     "/var/db/networks.db:"
250     "/var/db/hosts.db:"
251     "/var/db/group.db:"
252 azarah 131 "/var/db/passwd.db",
253 azarah 101 sandbox_info->home_dir);
254 azarah 159 if (SB_BUF_LEN <= retval) {
255     errno = EMSGSIZE;
256     perror("sandbox: Failed to generate SANDBOX_PREDICT");
257     return -1;
258     }
259    
260     return 0;
261 azarah 89 }
262 ferringb 2
263 azarah 124 int sandbox_setenv(char **env, const char *name, const char *val) {
264 azarah 89 char **tmp_env = env;
265     char *tmp_string = NULL;
266 azarah 159 int retval = 0;
267 azarah 89
268 azarah 100 /* XXX: We add the new variable to the end (no replacing). If this
269     * is changed, we need to fix sandbox_setup_environ() below */
270 azarah 89 while (NULL != *tmp_env)
271     tmp_env++;
272    
273     /* strlen(name) + strlen(val) + '=' + '\0' */
274 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
275     * a real leak that will cause issues. */
276 azarah 89 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
277     if (NULL == tmp_string) {
278 azarah 145 perror("sandbox: Out of memory (sandbox_setenv)");
279     exit(EXIT_FAILURE);
280 ferringb 2 }
281 azarah 89
282 azarah 159 retval = snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
283     name, val);
284 azarah 89 *tmp_env = tmp_string;
285    
286     return 0;
287 ferringb 2 }
288    
289 azarah 89 /* We setup the environment child side only to prevent issues with
290     * setting LD_PRELOAD parent side */
291 azarah 265 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info, bool interactive)
292 ferringb 2 {
293 azarah 89 int env_size = 0;
294 azarah 100 int have_ld_preload = 0;
295 azarah 89
296     char **new_environ;
297     char **env_ptr = environ;
298 azarah 124 char sandbox_write_envvar[SB_BUF_LEN];
299     char sandbox_predict_envvar[SB_BUF_LEN];
300 azarah 89 char *ld_preload_envvar = NULL;
301 azarah 100 char *orig_ld_preload_envvar = NULL;
302 azarah 261 char sb_pid[64];
303 ferringb 2
304 azarah 89 /* Unset these, as its easier than replacing when setting up our
305     * new environment below */
306 azarah 193 unsetenv(ENV_SANDBOX_ON);
307 azarah 261 unsetenv(ENV_SANDBOX_PID);
308 azarah 89 unsetenv(ENV_SANDBOX_LIB);
309     unsetenv(ENV_SANDBOX_BASHRC);
310     unsetenv(ENV_SANDBOX_LOG);
311     unsetenv(ENV_SANDBOX_DEBUG_LOG);
312 azarah 261 unsetenv(ENV_SANDBOX_ACTIVE);
313 azarah 89
314 azarah 100 if (NULL != getenv(ENV_LD_PRELOAD)) {
315     have_ld_preload = 1;
316     orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
317    
318 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
319     * than a real leak that will cause issues. */
320 azarah 100 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
321 azarah 101 strlen(sandbox_info->sandbox_lib) + 2,
322     sizeof(char *));
323 azarah 89 if (NULL == ld_preload_envvar)
324     return NULL;
325 azarah 100 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
326 azarah 101 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
327     sandbox_info->sandbox_lib, orig_ld_preload_envvar);
328 azarah 89 } else {
329 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
330     * than a real leak that will cause issues. */
331 azarah 280 ld_preload_envvar = rc_strndup(sandbox_info->sandbox_lib,
332 azarah 101 strlen(sandbox_info->sandbox_lib));
333 azarah 89 if (NULL == ld_preload_envvar)
334     return NULL;
335     }
336 azarah 100 /* Do not unset this, as strange things might happen */
337     /* unsetenv(ENV_LD_PRELOAD); */
338 ferringb 2
339 azarah 89 while (NULL != *env_ptr) {
340     env_size++;
341     env_ptr++;
342     }
343 ferringb 2
344 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
345     * a real leak that will cause issues. */
346 azarah 89 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
347     if (NULL == new_environ)
348     return NULL;
349    
350 azarah 261 snprintf(sb_pid, sizeof(sb_pid), "%i", getpid());
351    
352 azarah 89 /* First add our new variables to the beginning - this is due to some
353     * weirdness that I cannot remember */
354 azarah 193 sandbox_setenv(new_environ, ENV_SANDBOX_ON, "1");
355 azarah 261 sandbox_setenv(new_environ, ENV_SANDBOX_PID, sb_pid);
356 azarah 101 sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
357     sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
358     sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
359     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
360     sandbox_info->sandbox_debug_log);
361 azarah 265 /* Is this an interactive session? */
362     if (interactive)
363     sandbox_setenv(new_environ, ENV_SANDBOX_INTRACTV, "1");
364 azarah 263 /* Just set the these if not already set so that is_env_on() work */
365     if (!getenv(ENV_SANDBOX_VERBOSE))
366     sandbox_setenv(new_environ, ENV_SANDBOX_VERBOSE, "1");
367     if (!getenv(ENV_SANDBOX_DEBUG))
368     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG, "0");
369     if (!getenv(ENV_NOCOLOR))
370 azarah 269 sandbox_setenv(new_environ, ENV_NOCOLOR, "no");
371 azarah 100 /* If LD_PRELOAD was not set, set it here, else do it below */
372     if (1 != have_ld_preload)
373     sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
374 azarah 89
375     if (!getenv(ENV_SANDBOX_DENY))
376     sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
377    
378     if (!getenv(ENV_SANDBOX_READ))
379     sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
380    
381 azarah 159 if (-1 == get_sandbox_write_envvar(sandbox_write_envvar, sandbox_info))
382     return NULL;
383 azarah 89 if (!getenv(ENV_SANDBOX_WRITE))
384     sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
385    
386 azarah 159 if (-1 == get_sandbox_predict_envvar(sandbox_predict_envvar, sandbox_info))
387     return NULL;
388 azarah 89 if (!getenv(ENV_SANDBOX_PREDICT))
389     sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
390    
391 azarah 175 /* Make sure our bashrc gets preference */
392     sandbox_setenv(new_environ, ENV_BASH_ENV, sandbox_info->sandbox_rc);
393    
394 azarah 89 /* This one should NEVER be set in ebuilds, as it is the one
395     * private thing libsandbox.so use to test if the sandbox
396     * should be active for this pid, or not.
397     *
398     * azarah (3 Aug 2002)
399     */
400    
401 azarah 173 sandbox_setenv(new_environ, ENV_SANDBOX_ACTIVE, SANDBOX_ACTIVE);
402 azarah 89
403     env_size = 0;
404     while (NULL != new_environ[env_size])
405     env_size++;
406    
407     /* Now add the rest */
408     env_ptr = environ;
409     while (NULL != *env_ptr) {
410 azarah 100 if ((1 == have_ld_preload) &&
411     (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
412     /* If LD_PRELOAD was set, and this is it in the original
413     * environment, replace it with our new copy */
414 azarah 101 /* XXX: The following works as it just add whatever as
415     * the last variable to nev_environ */
416 azarah 100 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
417     ld_preload_envvar);
418     else
419     new_environ[env_size + (env_ptr - environ)] = *env_ptr;
420 azarah 89 env_ptr++;
421 ferringb 2 }
422 azarah 89
423     return new_environ;
424 ferringb 2 }
425    
426 azarah 145 int spawn_shell(char *argv_bash[], char *env[], int debug)
427 ferringb 2 {
428     int status = 0;
429     int ret = 0;
430    
431 azarah 261 child_pid = fork();
432 ferringb 2
433     /* Child's process */
434 azarah 261 if (0 == child_pid) {
435 azarah 89 execve(argv_bash[0], argv_bash, env);
436 ferringb 2 return 0;
437 azarah 261 } else if (child_pid < 0) {
438 azarah 145 if (debug)
439     fprintf(stderr, "Process failed to spawn!\n");
440 ferringb 2 return 0;
441     }
442 azarah 261 ret = waitpid(child_pid, &status, 0);
443 azarah 145 if ((-1 == ret) || (status > 0)) {
444     if (debug)
445     fprintf(stderr, "Process returned with failed exit status!\n");
446 ferringb 2 return 0;
447 azarah 145 }
448 azarah 86
449 ferringb 2 return 1;
450     }
451    
452 azarah 30 int main(int argc, char **argv)
453 ferringb 2 {
454 azarah 268 struct sigaction act_new;
455    
456 azarah 101 int i = 0, success = 1;
457 ferringb 2 int sandbox_log_presence = 0;
458     long len;
459    
460 azarah 101 struct sandbox_info_t sandbox_info;
461 ferringb 2
462 azarah 89 char **sandbox_environ;
463 ferringb 2 char **argv_bash = NULL;
464    
465     char *run_str = "-c";
466    
467 azarah 283 rc_log_domain(log_domain);
468    
469 ferringb 2 /* Only print info if called with no arguments .... */
470     if (argc < 2)
471     print_debug = 1;
472    
473     if (print_debug)
474 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
475 ferringb 2
476     /* check if a sandbox is already running */
477 azarah 184 if (NULL != getenv(ENV_SANDBOX_ACTIVE)) {
478 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
479     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
480 azarah 145 exit(EXIT_FAILURE);
481     }
482 ferringb 2
483 azarah 145 /* determine the location of all the sandbox support files */
484     if (print_debug)
485     printf("Detection of the support files.\n");
486 ferringb 2
487 azarah 145 if (-1 == sandbox_setup(&sandbox_info)) {
488 azarah 147 fprintf(stderr, "sandbox: Failed to setup sandbox.");
489 azarah 145 exit(EXIT_FAILURE);
490     }
491    
492     /* verify the existance of required files */
493     if (print_debug)
494     printf("Verification of the required files.\n");
495 ferringb 2
496 azarah 181 #ifndef SB_HAVE_MULTILIB
497 azarah 282 if (!rc_file_exists(sandbox_info.sandbox_lib)) {
498 azarah 145 perror("sandbox: Could not open the sandbox library");
499     exit(EXIT_FAILURE);
500     }
501 ferringb 2 #endif
502 azarah 282 if (!rc_file_exists(sandbox_info.sandbox_rc)) {
503 azarah 145 perror("sandbox: Could not open the sandbox rc file");
504     exit(EXIT_FAILURE);
505     }
506 ferringb 2
507 azarah 145 /* set up the required environment variables */
508     if (print_debug)
509     printf("Setting up the required environment variables.\n");
510 ferringb 2
511 azarah 145 /* Setup the child environment stuff */
512 azarah 265 sandbox_environ = sandbox_setup_environ(&sandbox_info, print_debug);
513 azarah 145 if (NULL == sandbox_environ) {
514     perror("sandbox: Out of memory (environ)");
515     exit(EXIT_FAILURE);
516     }
517 ferringb 2
518 azarah 165 /* If not in portage, cd into it work directory */
519     if ('\0' != sandbox_info.work_dir[0])
520 azarah 157 chdir(sandbox_info.work_dir);
521 ferringb 2
522 azarah 145 argv_bash = (char **)malloc(6 * sizeof(char *));
523     argv_bash[0] = strdup("/bin/bash");
524     argv_bash[1] = strdup("-rcfile");
525     argv_bash[2] = strdup(sandbox_info.sandbox_rc);
526 ferringb 2
527 azarah 145 if (argc < 2)
528     argv_bash[3] = NULL;
529     else
530     argv_bash[3] = strdup(run_str); /* "-c" */
531 ferringb 2
532 azarah 145 argv_bash[4] = NULL; /* strdup(run_arg); */
533     argv_bash[5] = NULL;
534 ferringb 2
535 azarah 145 if (argc >= 2) {
536     for (i = 1; i < argc; i++) {
537     if (NULL == argv_bash[4])
538     len = 0;
539     else
540     len = strlen(argv_bash[4]);
541 ferringb 2
542 azarah 145 argv_bash[4] = (char *)realloc(argv_bash[4],
543     (len + strlen(argv[i]) + 2) * sizeof(char));
544 ferringb 2
545 azarah 145 if (0 == len)
546     argv_bash[4][0] = 0;
547     if (1 != i)
548     strcat(argv_bash[4], " ");
549 ferringb 2
550 azarah 145 strcat(argv_bash[4], argv[i]);
551 ferringb 2 }
552 azarah 145 }
553 ferringb 2
554 azarah 145 /* set up the required signal handlers */
555     signal(SIGHUP, &stop);
556     signal(SIGINT, &stop);
557     signal(SIGQUIT, &stop);
558     signal(SIGTERM, &stop);
559 azarah 268 act_new.sa_sigaction = usr1_handler;
560     sigemptyset (&act_new.sa_mask);
561     act_new.sa_flags = SA_SIGINFO | SA_RESTART;
562     sigaction (SIGUSR1, &act_new, NULL);
563 ferringb 2
564 azarah 145 /* STARTING PROTECTED ENVIRONMENT */
565     if (print_debug) {
566     printf("The protected environment has been started.\n");
567     printf("--------------------------------------------------------------------------------\n");
568     }
569 ferringb 2
570 azarah 145 if (print_debug)
571     printf("Process being started in forked instance.\n");
572 ferringb 2
573 azarah 145 /* Start Bash */
574     if (!spawn_shell(argv_bash, sandbox_environ, print_debug))
575     success = 0;
576 ferringb 2
577 azarah 145 /* Free bash stuff */
578     for (i = 0; i < 6; i++) {
579     if (argv_bash[i])
580     free(argv_bash[i]);
581     argv_bash[i] = NULL;
582     }
583     if (argv_bash)
584     free(argv_bash);
585     argv_bash = NULL;
586 ferringb 2
587 azarah 145 if (print_debug)
588     printf("Cleaning up sandbox process\n");
589 ferringb 2
590 azarah 145 if (print_debug) {
591     printf("========================== Gentoo linux path sandbox ===========================\n");
592     printf("The protected environment has been shut down.\n");
593     }
594 ferringb 2
595 azarah 282 if (rc_file_exists(sandbox_info.sandbox_log)) {
596 azarah 145 sandbox_log_presence = 1;
597     print_sandbox_log(sandbox_info.sandbox_log);
598     } else if (print_debug) {
599     printf("--------------------------------------------------------------------------------\n");
600     }
601 ferringb 2
602 azarah 145 if ((sandbox_log_presence) || (!success))
603     return 1;
604     else
605     return 0;
606 ferringb 2 }
607    
608 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