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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 100 - (hide annotations) (download) (as text)
Fri May 13 14:18:05 2005 UTC (9 years, 3 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 18986 byte(s)
Various LD_PRELOAD cleanups.  Do not unset LD_PRELOAD for parent.

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