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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 105 - (hide annotations) (download) (as text)
Wed Jun 8 13:29:37 2005 UTC (9 years, 4 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 19514 byte(s)
Add /dev/console to write list, bug #38588.
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 <fcntl.h>
21     #include <signal.h>
22     #include <stdio.h>
23     #include <stdlib.h>
24     #include <limits.h>
25     #include <string.h>
26     #include <sys/file.h>
27     #include <sys/stat.h>
28     #include <sys/time.h>
29     #include <sys/types.h>
30     #include <sys/resource.h>
31     #include <sys/wait.h>
32     #include <unistd.h>
33     #include <fcntl.h>
34     #include "sandbox.h"
35    
36 azarah 101 struct sandbox_info_t {
37     char sandbox_log[SB_PATH_MAX];
38     char sandbox_debug_log[SB_PATH_MAX];
39     char sandbox_dir[SB_PATH_MAX];
40     char sandbox_lib[SB_PATH_MAX];
41     char sandbox_rc[SB_PATH_MAX];
42     char *sandbox_pids_file;
43     char portage_tmp_dir[SB_PATH_MAX];
44     char var_tmp_dir[SB_PATH_MAX];
45     char tmp_dir[SB_PATH_MAX];
46     char *home_dir;
47     } sandbox_info_t;
48 ferringb 2
49 azarah 101 static char *tmp_dir;
50    
51 azarah 99 static int cleaned_up = 0;
52     static int print_debug = 0;
53     static int stop_called = 0;
54    
55 azarah 30 /* Read pids file, and load active pids into an array. Return number of pids in array */
56     int load_active_pids(int fd, int **pids)
57 ferringb 2 {
58     char *data = NULL;
59     char *ptr = NULL, *ptr2 = NULL;
60     int my_pid;
61     int num_pids = 0;
62     long len;
63    
64     pids[0] = NULL;
65    
66     len = file_length(fd);
67    
68     /* Allocate and zero datablock to read pids file */
69 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
70 ferringb 2 memset(data, 0, len + 1);
71    
72     /* Start at beginning of file */
73     lseek(fd, 0L, SEEK_SET);
74    
75     /* read entire file into a buffer */
76     read(fd, data, len);
77    
78     ptr = data;
79    
80     /* Loop and read all pids */
81     while (1) {
82     /* Find new line */
83     ptr2 = strchr(ptr, '\n');
84     if (ptr2 == NULL)
85 azarah 30 break; /* No more PIDs */
86 ferringb 2
87     /* Clear the \n. And ptr should have a null-terminated decimal string */
88     ptr2[0] = 0;
89    
90     my_pid = atoi(ptr);
91    
92     /* If the PID is still alive, add it to our array */
93     if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
94 azarah 30 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
95 ferringb 2 pids[0][num_pids] = my_pid;
96     num_pids++;
97     }
98    
99     /* Put ptr past the NULL we just wrote */
100     ptr = ptr2 + 1;
101     }
102    
103     if (data)
104     free(data);
105     data = NULL;
106    
107     return num_pids;
108     }
109    
110 azarah 101 int write_pids_file(struct sandbox_info_t *sandbox_info)
111 ferringb 2 {
112 azarah 101 int pids_file = -1;
113     int *pids_array = NULL;
114     int num_of_pids = 0;
115     int i = 0, success = 1;
116    
117     char pid_string[SB_BUF_LEN];
118    
119     if (file_exist(sandbox_info->sandbox_pids_file, 1) < 0) {
120     fprintf(stderr, ">>> %s is not a regular file\n",
121     sandbox_info->sandbox_pids_file);
122     return -1;
123     } else {
124     pids_file = file_open(sandbox_info->sandbox_pids_file,
125     "r+", 1, 0664, "portage");
126     if (-1 == pids_file)
127     return -1;
128     }
129    
130     /* Grab still active pids */
131     num_of_pids = load_active_pids(pids_file, &pids_array);
132    
133     /* Zero out file */
134     file_truncate(pids_file);
135    
136     /* Output active pids, and append our pid */
137     for (i = 0; i < num_of_pids + 1; i++) {
138     /* Time for our entry */
139     if (i == num_of_pids)
140     sprintf(pid_string, "%d\n", getpid());
141     else
142     sprintf(pid_string, "%d\n", pids_array[i]);
143    
144     if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
145     success = 0;
146     break;
147     }
148     }
149    
150     /* Clean pids_array */
151     if (pids_array)
152     free(pids_array);
153    
154     /* We're done with the pids file */
155     file_close(pids_file);
156    
157     if (!success)
158     return -1;
159    
160     return 0;
161     }
162    
163     int sandbox_setup(char *argv[], struct sandbox_info_t *sandbox_info)
164     {
165     if (NULL == realpath(getenv(ENV_PORTAGE_TMPDIR) ? getenv(ENV_PORTAGE_TMPDIR)
166     : PORTAGE_TMPDIR,
167     sandbox_info->portage_tmp_dir)) {
168     perror(">>> get portage_tmp_dir");
169     return -1;
170     }
171    
172     if (NULL == realpath(VAR_TMPDIR, sandbox_info->var_tmp_dir)) {
173     perror(">>> get var_tmp_dir");
174     return -1;
175     }
176    
177     if (-1 == get_tmp_dir(sandbox_info->tmp_dir)) {
178     perror(">>> get tmp_dir");
179     return -1;
180     }
181     tmp_dir = sandbox_info->tmp_dir;
182    
183     sandbox_info->home_dir = getenv("HOME");
184     if (!sandbox_info->home_dir) {
185     sandbox_info->home_dir = tmp_dir;
186     setenv("HOME", sandbox_info->home_dir, 1);
187     }
188    
189     /* Generate base sandbox path */
190     snprintf(sandbox_info->sandbox_dir, SB_PATH_MAX, "%s/",
191     get_sandbox_path(argv[0]));
192    
193     /* Generate sandbox lib path */
194     snprintf(sandbox_info->sandbox_lib, SB_PATH_MAX, "%s",
195     get_sandbox_lib(sandbox_info->sandbox_dir));
196    
197     /* Generate sandbox pids-file path */
198     sandbox_info->sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
199    
200     /* Generate sandbox bashrc path */
201     snprintf(sandbox_info->sandbox_rc, SB_PATH_MAX, "%s",
202     get_sandbox_rc(sandbox_info->sandbox_dir));
203    
204     /* Generate sandbox log full path */
205     snprintf(sandbox_info->sandbox_log, SB_PATH_MAX, "%s",
206     get_sandbox_log(tmp_dir));
207    
208     /* Generate sandbox debug log full path */
209     snprintf(sandbox_info->sandbox_debug_log, SB_PATH_MAX, "%s",
210     get_sandbox_debug_log(tmp_dir));
211    
212     return 0;
213     }
214    
215     void sandbox_cleanup()
216     {
217 ferringb 2 int i = 0;
218     int success = 1;
219     int pids_file = -1, num_of_pids = 0;
220     int *pids_array = NULL;
221 azarah 93 char pid_string[SB_BUF_LEN];
222 ferringb 2 char *sandbox_pids_file;
223    
224     /* Generate sandbox pids-file path */
225 azarah 99 sandbox_pids_file = get_sandbox_pids_file(tmp_dir);
226 ferringb 2
227     /* Remove this sandbox's bash pid from the global pids
228 azarah 90 * file if we have not already done so */
229     if (0 == cleaned_up) {
230 ferringb 2 cleaned_up = 1;
231     success = 1;
232    
233     if (print_debug)
234     printf("Cleaning up pids file.\n");
235    
236     /* Stat the PIDs file, make sure it exists and is a regular file */
237     if (file_exist(sandbox_pids_file, 1) <= 0) {
238 ferringb 34 fprintf(stderr, ">>> pids file is not a regular file\n");
239 ferringb 2 success = 0;
240     /* We should really not fail if the pidsfile is missing here, but
241     * rather just exit cleanly, as there is still some cleanup to do */
242     return;
243     }
244    
245     pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
246     if (-1 == pids_file) {
247     success = 0;
248     /* Nothing more to do here */
249     return;
250     }
251    
252     /* Load "still active" pids into an array */
253     num_of_pids = load_active_pids(pids_file, &pids_array);
254     //printf("pids: %d\r\n", num_of_pids);
255    
256    
257     file_truncate(pids_file);
258    
259     /* if pids are still running, write only the running pids back to the file */
260     if (num_of_pids > 1) {
261     for (i = 0; i < num_of_pids; i++) {
262     if (pids_array[i] != getpid()) {
263     sprintf(pid_string, "%d\n", pids_array[i]);
264    
265 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
266 ferringb 2 perror(">>> pids file write");
267     success = 0;
268     break;
269     }
270     }
271     }
272    
273     file_close(pids_file);
274     pids_file = -1;
275     } else {
276    
277     file_close(pids_file);
278     pids_file = -1;
279    
280     /* remove the pidsfile, as this was the last sandbox */
281     unlink(sandbox_pids_file);
282     }
283    
284     if (pids_array != NULL)
285     free(pids_array);
286     pids_array = NULL;
287     }
288    
289     free(sandbox_pids_file);
290     }
291    
292 azarah 88 int print_sandbox_log(char *sandbox_log)
293     {
294     int sandbox_log_file = -1;
295     char *beep_count_env = NULL;
296     int i, color, beep_count = 0;
297     long len = 0;
298     char *buffer = NULL;
299    
300     sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
301     if (-1 == sandbox_log_file)
302     return 0;
303    
304     len = file_length(sandbox_log_file);
305     buffer = (char *)malloc((len + 1) * sizeof(char));
306     memset(buffer, 0, len + 1);
307     read(sandbox_log_file, buffer, len);
308     file_close(sandbox_log_file);
309    
310     color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
311    
312     if (color)
313     printf("\e[31;01m");
314     printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
315     if (color)
316     printf("\033[0m");
317     if (color)
318     printf("\e[31;01m");
319     printf("\nLOG FILE = \"%s\"", sandbox_log);
320     if (color)
321     printf("\033[0m");
322     printf("\n\n");
323     printf("%s", buffer);
324     if (buffer)
325     free(buffer);
326     buffer = NULL;
327     printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
328    
329     beep_count_env = getenv(ENV_SANDBOX_BEEP);
330     if (beep_count_env)
331     beep_count = atoi(beep_count_env);
332     else
333     beep_count = DEFAULT_BEEP_COUNT;
334    
335     for (i = 0; i < beep_count; i++) {
336     fputc('\a', stderr);
337     if (i < beep_count - 1)
338     sleep(1);
339     }
340     return 1;
341     }
342    
343 azarah 30 void stop(int signum)
344 ferringb 2 {
345     if (stop_called == 0) {
346     stop_called = 1;
347     printf("Caught signal %d in pid %d\r\n", signum, getpid());
348 azarah 101 sandbox_cleanup();
349 ferringb 2 } else {
350     fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
351     }
352     }
353    
354 azarah 101 void get_sandbox_write_envvar(char *buf, struct sandbox_info_t *sandbox_info)
355 ferringb 2 {
356 azarah 89 /* bzero out entire buffer then append trailing 0 */
357 azarah 93 memset(buf, 0, SB_BUF_LEN);
358 azarah 30
359 azarah 89 /* these could go into make.globals later on */
360     snprintf(buf, SB_BUF_LEN,
361     "%s:%s/.gconfd/lock:%s/.bash_history:%s:%s:%s:%s",
362     "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
363 azarah 105 "/dev/vc/:/dev/pty:/dev/tty:/dev/console:"
364 azarah 89 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
365     "/usr/tmp/conftest:/usr/lib/conftest:"
366     "/usr/lib32/conftest:/usr/lib64/conftest:"
367     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
368 azarah 101 sandbox_info->home_dir, sandbox_info->home_dir,
369     (NULL != sandbox_info->portage_tmp_dir) ? sandbox_info->portage_tmp_dir : tmp_dir,
370     sandbox_info->tmp_dir, sandbox_info->var_tmp_dir,
371     "/tmp/:/var/tmp/");
372 azarah 89 }
373    
374 azarah 101 void get_sandbox_predict_envvar(char *buf, struct sandbox_info_t *sandbox_info)
375 azarah 89 {
376 ferringb 2 /* bzero out entire buffer then append trailing 0 */
377 azarah 93 memset(buf, 0, SB_BUF_LEN);
378 ferringb 2
379 azarah 89 /* these should go into make.globals later on */
380     snprintf(buf, SB_BUF_LEN, "%s/.:"
381     "/usr/lib/python2.0/:"
382     "/usr/lib/python2.1/:"
383     "/usr/lib/python2.2/:"
384     "/usr/lib/python2.3/:"
385     "/usr/lib/python2.4/:"
386     "/usr/lib/python2.5/:"
387     "/usr/lib/python3.0/:",
388 azarah 101 sandbox_info->home_dir);
389 azarah 89 }
390 ferringb 2
391 azarah 89 int sandbox_setenv(char **env, char *name, char *val) {
392     char **tmp_env = env;
393     char *tmp_string = NULL;
394    
395 azarah 100 /* XXX: We add the new variable to the end (no replacing). If this
396     * is changed, we need to fix sandbox_setup_environ() below */
397 azarah 89 while (NULL != *tmp_env)
398     tmp_env++;
399    
400     /* strlen(name) + strlen(val) + '=' + '\0' */
401 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
402     * a real leak that will cause issues. */
403 azarah 89 tmp_string = calloc(strlen(name) + strlen(val) + 2, sizeof(char *));
404     if (NULL == tmp_string) {
405     perror(">>> out of memory (sandbox_setenv)");
406     exit(1);
407 ferringb 2 }
408 azarah 89
409     snprintf(tmp_string, strlen(name) + strlen(val) + 2, "%s=%s",
410     name, val);
411     *tmp_env = tmp_string;
412    
413     return 0;
414 ferringb 2 }
415    
416 azarah 89 /* We setup the environment child side only to prevent issues with
417     * setting LD_PRELOAD parent side */
418 azarah 101 char **sandbox_setup_environ(struct sandbox_info_t *sandbox_info,
419     char *sandbox_write_envvar, char *sandbox_predict_envvar)
420 ferringb 2 {
421 azarah 89 int env_size = 0;
422 azarah 100 int have_ld_preload = 0;
423 azarah 89
424     char **new_environ;
425     char **env_ptr = environ;
426     char *ld_preload_envvar = NULL;
427 azarah 100 char *orig_ld_preload_envvar = NULL;
428 ferringb 2
429 azarah 89 /* Unset these, as its easier than replacing when setting up our
430     * new environment below */
431     unsetenv(ENV_SANDBOX_DIR);
432     unsetenv(ENV_SANDBOX_LIB);
433     unsetenv(ENV_SANDBOX_BASHRC);
434     unsetenv(ENV_SANDBOX_LOG);
435     unsetenv(ENV_SANDBOX_DEBUG_LOG);
436    
437 azarah 100 if (NULL != getenv(ENV_LD_PRELOAD)) {
438     have_ld_preload = 1;
439     orig_ld_preload_envvar = getenv(ENV_LD_PRELOAD);
440    
441 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
442     * than a real leak that will cause issues. */
443 azarah 100 ld_preload_envvar = calloc(strlen(orig_ld_preload_envvar) +
444 azarah 101 strlen(sandbox_info->sandbox_lib) + 2,
445     sizeof(char *));
446 azarah 89 if (NULL == ld_preload_envvar)
447     return NULL;
448 azarah 100 snprintf(ld_preload_envvar, strlen(orig_ld_preload_envvar) +
449 azarah 101 strlen(sandbox_info->sandbox_lib) + 2, "%s %s",
450     sandbox_info->sandbox_lib, orig_ld_preload_envvar);
451 azarah 89 } else {
452 azarah 96 /* FIXME: Should probably free this at some stage - more neatness
453     * than a real leak that will cause issues. */
454 azarah 101 ld_preload_envvar = strndup(sandbox_info->sandbox_lib,
455     strlen(sandbox_info->sandbox_lib));
456 azarah 89 if (NULL == ld_preload_envvar)
457     return NULL;
458     }
459 azarah 100 /* Do not unset this, as strange things might happen */
460     /* unsetenv(ENV_LD_PRELOAD); */
461 ferringb 2
462 azarah 89 while (NULL != *env_ptr) {
463     env_size++;
464     env_ptr++;
465     }
466 ferringb 2
467 azarah 96 /* FIXME: Should probably free this at some stage - more neatness than
468     * a real leak that will cause issues. */
469 azarah 89 new_environ = calloc((env_size + 15 + 1) * sizeof(char *), sizeof(char *));
470     if (NULL == new_environ)
471     return NULL;
472    
473     /* First add our new variables to the beginning - this is due to some
474     * weirdness that I cannot remember */
475 azarah 101 sandbox_setenv(new_environ, ENV_SANDBOX_DIR, sandbox_info->sandbox_dir);
476     sandbox_setenv(new_environ, ENV_SANDBOX_LIB, sandbox_info->sandbox_lib);
477     sandbox_setenv(new_environ, ENV_SANDBOX_BASHRC, sandbox_info->sandbox_rc);
478     sandbox_setenv(new_environ, ENV_SANDBOX_LOG, sandbox_info->sandbox_log);
479     sandbox_setenv(new_environ, ENV_SANDBOX_DEBUG_LOG,
480     sandbox_info->sandbox_debug_log);
481 azarah 100 /* If LD_PRELOAD was not set, set it here, else do it below */
482     if (1 != have_ld_preload)
483     sandbox_setenv(new_environ, ENV_LD_PRELOAD, ld_preload_envvar);
484 azarah 89
485     if (!getenv(ENV_SANDBOX_DENY))
486     sandbox_setenv(new_environ, ENV_SANDBOX_DENY, LD_PRELOAD_FILE);
487    
488     if (!getenv(ENV_SANDBOX_READ))
489     sandbox_setenv(new_environ, ENV_SANDBOX_READ, "/");
490    
491     if (!getenv(ENV_SANDBOX_WRITE))
492     sandbox_setenv(new_environ, ENV_SANDBOX_WRITE, sandbox_write_envvar);
493    
494     if (!getenv(ENV_SANDBOX_PREDICT))
495     sandbox_setenv(new_environ, ENV_SANDBOX_PREDICT, sandbox_predict_envvar);
496    
497     /* This one should NEVER be set in ebuilds, as it is the one
498     * private thing libsandbox.so use to test if the sandbox
499     * should be active for this pid, or not.
500     *
501     * azarah (3 Aug 2002)
502     */
503    
504     sandbox_setenv(new_environ, "SANDBOX_ACTIVE", "armedandready");
505    
506     env_size = 0;
507     while (NULL != new_environ[env_size])
508     env_size++;
509    
510     /* Now add the rest */
511     env_ptr = environ;
512     while (NULL != *env_ptr) {
513 azarah 100 if ((1 == have_ld_preload) &&
514     (strstr(*env_ptr, LD_PRELOAD_EQ) == *env_ptr))
515     /* If LD_PRELOAD was set, and this is it in the original
516     * environment, replace it with our new copy */
517 azarah 101 /* XXX: The following works as it just add whatever as
518     * the last variable to nev_environ */
519 azarah 100 sandbox_setenv(new_environ, ENV_LD_PRELOAD,
520     ld_preload_envvar);
521     else
522     new_environ[env_size + (env_ptr - environ)] = *env_ptr;
523 azarah 89 env_ptr++;
524 ferringb 2 }
525 azarah 89
526     return new_environ;
527 ferringb 2 }
528    
529 azarah 89 int spawn_shell(char *argv_bash[], char *env[])
530 ferringb 2 {
531     int pid;
532     int status = 0;
533     int ret = 0;
534    
535     pid = fork();
536    
537     /* Child's process */
538     if (0 == pid) {
539 azarah 89 execve(argv_bash[0], argv_bash, env);
540 ferringb 2 return 0;
541     } else if (pid < 0) {
542     return 0;
543     }
544     ret = waitpid(pid, &status, 0);
545     if ((-1 == ret) || (status > 0))
546     return 0;
547 azarah 86
548 ferringb 2 return 1;
549     }
550    
551 azarah 30 int main(int argc, char **argv)
552 ferringb 2 {
553 azarah 101 int i = 0, success = 1;
554 ferringb 2 int sandbox_log_presence = 0;
555     long len;
556    
557 azarah 101 struct sandbox_info_t sandbox_info;
558 ferringb 2
559 azarah 89 char **sandbox_environ;
560 azarah 93 char sandbox_write_envvar[SB_BUF_LEN];
561     char sandbox_predict_envvar[SB_BUF_LEN];
562 ferringb 2 char **argv_bash = NULL;
563    
564     char *run_str = "-c";
565     char *home_dir = NULL;
566     char *tmp_string = NULL;
567    
568     /* Only print info if called with no arguments .... */
569     if (argc < 2)
570     print_debug = 1;
571    
572     if (print_debug)
573 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
574 ferringb 2
575     /* check if a sandbox is already running */
576     if (NULL != getenv(ENV_SANDBOX_ON)) {
577 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
578     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
579 ferringb 2 exit(1);
580     } else {
581    
582     /* determine the location of all the sandbox support files */
583     if (print_debug)
584     printf("Detection of the support files.\n");
585    
586 azarah 101 if (-1 == sandbox_setup(argv, &sandbox_info)) {
587     perror(">>> setup");
588 azarah 99 exit(1);
589     }
590 azarah 101
591 ferringb 2 /* verify the existance of required files */
592     if (print_debug)
593     printf("Verification of the required files.\n");
594    
595     #ifndef SB_HAVE_64BIT_ARCH
596 azarah 101 if (file_exist(sandbox_info.sandbox_lib, 0) <= 0) {
597     fprintf(stderr, "Could not open the sandbox library at '%s'.\n",
598     sandbox_info.sandbox_lib);
599 ferringb 2 return -1;
600 azarah 30 }
601 ferringb 2 #endif
602 azarah 101 if (file_exist(sandbox_info.sandbox_rc, 0) <= 0) {
603     fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n",
604     sandbox_info.sandbox_rc);
605 ferringb 2 return -1;
606     }
607    
608     /* set up the required environment variables */
609     if (print_debug)
610     printf("Setting up the required environment variables.\n");
611    
612 azarah 91 /* This one should not be child only, as we check above to see
613     * if we are already running (check sandbox_setup_environ).
614     * This needs to be set before calling sandbox_setup_environ(),
615     * else its not set for the child */
616     setenv(ENV_SANDBOX_ON, "1", 0);
617    
618     /* Setup the child environment stuff */
619 azarah 101 get_sandbox_write_envvar(sandbox_write_envvar, &sandbox_info);
620     get_sandbox_predict_envvar(sandbox_predict_envvar, &sandbox_info);
621     sandbox_environ = sandbox_setup_environ(&sandbox_info,
622 azarah 89 sandbox_write_envvar, sandbox_predict_envvar);
623     if (NULL == sandbox_environ) {
624     perror(">>> out of memory (environ)");
625     exit(1);
626 azarah 71 }
627 ferringb 2
628     /* if the portage temp dir was present, cd into it */
629 azarah 101 if (NULL != sandbox_info.portage_tmp_dir)
630     chdir(sandbox_info.portage_tmp_dir);
631 ferringb 2
632 azarah 30 argv_bash = (char **)malloc(6 * sizeof(char *));
633 ferringb 2 argv_bash[0] = strdup("/bin/bash");
634     argv_bash[1] = strdup("-rcfile");
635 azarah 101 argv_bash[2] = strdup(sandbox_info.sandbox_rc);
636 ferringb 2
637     if (argc < 2)
638     argv_bash[3] = NULL;
639     else
640     argv_bash[3] = strdup(run_str); /* "-c" */
641    
642 azarah 30 argv_bash[4] = NULL; /* strdup(run_arg); */
643 ferringb 2 argv_bash[5] = NULL;
644    
645     if (argc >= 2) {
646     for (i = 1; i < argc; i++) {
647     if (NULL == argv_bash[4])
648     len = 0;
649     else
650     len = strlen(argv_bash[4]);
651    
652 azarah 89 argv_bash[4] = (char *)realloc(argv_bash[4],
653     (len + strlen(argv[i]) + 2) * sizeof(char));
654 ferringb 2
655     if (0 == len)
656     argv_bash[4][0] = 0;
657     if (1 != i)
658     strcat(argv_bash[4], " ");
659    
660     strcat(argv_bash[4], argv[i]);
661     }
662     }
663    
664     /* set up the required signal handlers */
665     signal(SIGHUP, &stop);
666     signal(SIGINT, &stop);
667     signal(SIGQUIT, &stop);
668     signal(SIGTERM, &stop);
669    
670     /* Load our PID into PIDs file */
671 azarah 101 if (-1 == write_pids_file(&sandbox_info)) {
672 ferringb 2 perror(">>> pids file write");
673     exit(1);
674     }
675    
676     /* STARTING PROTECTED ENVIRONMENT */
677     if (print_debug) {
678     printf("The protected environment has been started.\n");
679 azarah 30 printf("--------------------------------------------------------------------------------\n");
680 ferringb 2 }
681    
682     if (print_debug)
683     printf("Shell being started in forked process.\n");
684    
685     /* Start Bash */
686 azarah 89 if (!spawn_shell(argv_bash, sandbox_environ)) {
687 ferringb 2 if (print_debug)
688     fprintf(stderr, ">>> shell process failed to spawn\n");
689     success = 0;
690     }
691    
692     /* Free bash stuff */
693     for (i = 0; i < 6; i++) {
694     if (argv_bash[i])
695     free(argv_bash[i]);
696     argv_bash[i] = NULL;
697     }
698     if (argv_bash)
699     free(argv_bash);
700     argv_bash = NULL;
701    
702     if (print_debug)
703     printf("Cleaning up sandbox process\n");
704    
705 azarah 101 sandbox_cleanup();
706 ferringb 2
707     if (print_debug) {
708 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
709 ferringb 2 printf("The protected environment has been shut down.\n");
710     }
711    
712 azarah 101 if (file_exist(sandbox_info.sandbox_log, 0)) {
713 ferringb 2 sandbox_log_presence = 1;
714     success = 1;
715 azarah 101 if (!print_sandbox_log(sandbox_info.sandbox_log))
716 ferringb 2 success = 0;
717     } else if (print_debug) {
718 azarah 30 printf("--------------------------------------------------------------------------------\n");
719 ferringb 2 }
720    
721 azarah 101 free(sandbox_info.sandbox_pids_file);
722 azarah 35
723 ferringb 2 if ((sandbox_log_presence) || (!success))
724     return 1;
725     else
726     return 0;
727     }
728     }
729    
730 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