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

Contents of /trunk/src/sandbox.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 70 - (hide annotations) (download) (as text)
Tue May 3 21:43:50 2005 UTC (9 years, 4 months ago) by azarah
Original Path: trunk/sandbox.c
File MIME type: text/x-csrc
File size: 20926 byte(s)
Fixup sandbox and sandbox.bashrc to call bash with the proper .bashrc.

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     int preload_adaptable = 1;
37     int cleaned_up = 0;
38     int print_debug = 0;
39     int stop_called = 0;
40    
41 azarah 30 /* Read pids file, and load active pids into an array. Return number of pids in array */
42     int load_active_pids(int fd, int **pids)
43 ferringb 2 {
44     char *data = NULL;
45     char *ptr = NULL, *ptr2 = NULL;
46     int my_pid;
47     int num_pids = 0;
48     long len;
49    
50     pids[0] = NULL;
51    
52     len = file_length(fd);
53    
54     /* Allocate and zero datablock to read pids file */
55 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
56 ferringb 2 memset(data, 0, len + 1);
57    
58     /* Start at beginning of file */
59     lseek(fd, 0L, SEEK_SET);
60    
61     /* read entire file into a buffer */
62     read(fd, data, len);
63    
64     ptr = data;
65    
66     /* Loop and read all pids */
67     while (1) {
68     /* Find new line */
69     ptr2 = strchr(ptr, '\n');
70     if (ptr2 == NULL)
71 azarah 30 break; /* No more PIDs */
72 ferringb 2
73     /* Clear the \n. And ptr should have a null-terminated decimal string */
74     ptr2[0] = 0;
75    
76     my_pid = atoi(ptr);
77    
78     /* If the PID is still alive, add it to our array */
79     if ((0 != my_pid) && (0 == kill(my_pid, 0))) {
80 azarah 30 pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int));
81 ferringb 2 pids[0][num_pids] = my_pid;
82     num_pids++;
83     }
84    
85     /* Put ptr past the NULL we just wrote */
86     ptr = ptr2 + 1;
87     }
88    
89     if (data)
90     free(data);
91     data = NULL;
92    
93     return num_pids;
94     }
95    
96     /* Read ld.so.preload file, and loads dirs into an array. Return number of entries in array */
97 azarah 30 int load_preload_libs(int fd, char ***preloads)
98 ferringb 2 {
99     char *data = NULL;
100     char *ptr = NULL, *ptr2 = NULL;
101     int num_entries = 0;
102     long len;
103    
104     preloads[0] = NULL;
105    
106     len = file_length(fd);
107    
108     /* Allocate and zero datablock to read pids file */
109 azarah 30 data = (char *)malloc((len + 1) * sizeof(char));
110 ferringb 2 memset(data, 0, len + 1);
111    
112     /* Start at beginning of file */
113     lseek(fd, 0L, SEEK_SET);
114    
115     /* read entire file into a buffer */
116     read(fd, data, len);
117    
118     ptr = data;
119    
120     /* Loop and read all pids */
121     while (1) {
122     /* Find new line */
123     ptr2 = strchr(ptr, '\n');
124    
125     /* Clear the \n. And ptr should have a null-terminated decimal string
126     * Don't break from the loop though because the last line may not
127     * terminated with a \n
128     */
129     if (NULL != ptr2)
130     ptr2[0] = 0;
131    
132     /* If listing does not match our libname, add it to the array */
133     if ((strlen(ptr)) && (NULL == strstr(ptr, LIB_NAME))) {
134 azarah 30 preloads[0] = (char **)realloc(preloads[0], (num_entries + 1) * sizeof(char **));
135 ferringb 2 preloads[0][num_entries] = strdup(ptr);
136     num_entries++;
137     }
138    
139     if (NULL == ptr2)
140 azarah 30 break; /* No more PIDs */
141 ferringb 2
142     /* Put ptr past the NULL we just wrote */
143     ptr = ptr2 + 1;
144     }
145    
146     if (data)
147     free(data);
148     data = NULL;
149    
150     return num_entries;
151     }
152    
153 azarah 30 void cleanup()
154 ferringb 2 {
155     int i = 0;
156     int success = 1;
157     int pids_file = -1, num_of_pids = 0;
158     int *pids_array = NULL;
159     char pid_string[255];
160     char *sandbox_pids_file;
161     #ifdef USE_LD_SO_PRELOAD
162     int preload_file = -1, num_of_preloads = 0;
163     char preload_entry[255];
164     char **preload_array = NULL;
165     #endif
166    
167     /* Generate sandbox pids-file path */
168     sandbox_pids_file = get_sandbox_pids_file();
169    
170     /* Remove this sandbox's bash pid from the global pids
171     * file if it has rights to adapt the ld.so.preload file */
172     if ((1 == preload_adaptable) && (0 == cleaned_up)) {
173     cleaned_up = 1;
174     success = 1;
175    
176     if (print_debug)
177     printf("Cleaning up pids file.\n");
178    
179     /* Stat the PIDs file, make sure it exists and is a regular file */
180     if (file_exist(sandbox_pids_file, 1) <= 0) {
181 ferringb 34 fprintf(stderr, ">>> pids file is not a regular file\n");
182 ferringb 2 success = 0;
183     /* We should really not fail if the pidsfile is missing here, but
184     * rather just exit cleanly, as there is still some cleanup to do */
185     return;
186     }
187    
188     pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
189     if (-1 == pids_file) {
190     success = 0;
191     /* Nothing more to do here */
192     return;
193     }
194    
195     /* Load "still active" pids into an array */
196     num_of_pids = load_active_pids(pids_file, &pids_array);
197     //printf("pids: %d\r\n", num_of_pids);
198    
199     #ifdef USE_LD_SO_PRELOAD
200     /* clean the /etc/ld.so.preload file if no other sandbox
201     * processes are running anymore */
202     if (1 == num_of_pids) {
203     success = 1;
204    
205     if (print_debug)
206     printf("Cleaning up /etc/ld.so.preload.\n");
207    
208     preload_file = file_open("/etc/ld.so.preload", "r+", 1, 0644);
209     if (-1 != preload_file) {
210     /* Load all the preload libraries into an array */
211     num_of_preloads = load_preload_libs(preload_file, &preload_array);
212     //printf("num preloads: %d\r\n", num_of_preloads);
213     /* Clear file */
214     file_truncate(preload_file);
215    
216     /* store the other preload libraries back into the /etc/ld.so.preload file */
217     if (num_of_preloads > 0) {
218     for (i = 0; i < num_of_preloads; i++) {
219     sprintf(preload_entry, "%s\n", preload_array[i]);
220 azarah 30 if (write(preload_file, preload_entry, strlen(preload_entry)) != strlen(preload_entry)) {
221 ferringb 2 perror(">>> /etc/ld.so.preload file write");
222     success = 0;
223     break;
224     }
225     }
226     }
227    
228     /* Free memory used to store preload array */
229     for (i = 0; i < num_of_preloads; i++) {
230     if (preload_array[i])
231     free(preload_array[i]);
232     preload_array[i] = NULL;
233     }
234     if (preload_array)
235     free(preload_array);
236     preload_array = NULL;
237    
238     file_close(preload_file);
239     preload_file = -1;
240     }
241     }
242     #endif
243    
244     file_truncate(pids_file);
245    
246     /* if pids are still running, write only the running pids back to the file */
247     if (num_of_pids > 1) {
248     for (i = 0; i < num_of_pids; i++) {
249     if (pids_array[i] != getpid()) {
250     sprintf(pid_string, "%d\n", pids_array[i]);
251    
252 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
253 ferringb 2 perror(">>> pids file write");
254     success = 0;
255     break;
256     }
257     }
258     }
259    
260     file_close(pids_file);
261     pids_file = -1;
262     } else {
263    
264     file_close(pids_file);
265     pids_file = -1;
266    
267     /* remove the pidsfile, as this was the last sandbox */
268     unlink(sandbox_pids_file);
269     }
270    
271     if (pids_array != NULL)
272     free(pids_array);
273     pids_array = NULL;
274     }
275    
276     free(sandbox_pids_file);
277     if (0 == success)
278     return;
279     }
280    
281 azarah 30 void stop(int signum)
282 ferringb 2 {
283     if (stop_called == 0) {
284     stop_called = 1;
285     printf("Caught signal %d in pid %d\r\n", signum, getpid());
286 azarah 30 cleanup();
287 ferringb 2 } else {
288     fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid());
289     }
290     }
291    
292 azarah 30 void setenv_sandbox_write(char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir)
293 ferringb 2 {
294     char buf[1024];
295 azarah 30
296 ferringb 2 /* bzero out entire buffer then append trailing 0 */
297     memset(buf, 0, sizeof(buf));
298    
299     if (!getenv(ENV_SANDBOX_WRITE)) {
300     /* these could go into make.globals later on */
301     snprintf(buf, sizeof(buf),
302 azarah 30 "%s:%s/.gconfd/lock:%s/.bash_history:",
303     "/dev/zero:/dev/fd/:/dev/null:/dev/pts/:"
304 azarah 65 "/dev/vc/:/dev/pty:/dev/tty:/tmp/:"
305 azarah 30 "/dev/shm/ngpt:/var/log/scrollkeeper.log:"
306     "/usr/tmp/conftest:/usr/lib/conftest:"
307     "/usr/lib32/conftest:/usr/lib64/conftest:"
308     "/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf",
309     home_dir, home_dir);
310 ferringb 2
311     if (NULL == portage_tmp_dir) {
312     strncat(buf, tmp_dir, sizeof(buf));
313     strncat(buf, ":", sizeof(buf));
314     strncat(buf, var_tmp_dir, sizeof(buf));
315     strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf));
316     } else {
317     strncat(buf, portage_tmp_dir, sizeof(buf));
318     strncat(buf, ":", sizeof(buf));
319     strncat(buf, tmp_dir, sizeof(buf));
320     strncat(buf, ":", sizeof(buf));
321     strncat(buf, var_tmp_dir, sizeof(buf));
322     strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf));
323     }
324     buf[sizeof(buf) - 1] = '\0';
325     setenv(ENV_SANDBOX_WRITE, buf, 1);
326     }
327     }
328    
329 azarah 30 void setenv_sandbox_predict(char *home_dir)
330 ferringb 2 {
331     char buf[1024];
332    
333     memset(buf, 0, sizeof(buf));
334    
335     if (!getenv(ENV_SANDBOX_PREDICT)) {
336     /* these should go into make.globals later on */
337 azarah 30 snprintf(buf, sizeof(buf), "%s/.:"
338     "/usr/lib/python2.0/:"
339     "/usr/lib/python2.1/:"
340     "/usr/lib/python2.2/:"
341     "/usr/lib/python2.3/:"
342     "/usr/lib/python2.4/:"
343     "/usr/lib/python2.5/:"
344     "/usr/lib/python3.0/:",
345     home_dir);
346 ferringb 2
347     buf[sizeof(buf) - 1] = '\0';
348     setenv(ENV_SANDBOX_PREDICT, buf, 1);
349     }
350     }
351    
352 azarah 30 int print_sandbox_log(char *sandbox_log)
353 ferringb 2 {
354     int sandbox_log_file = -1;
355     char *beep_count_env = NULL;
356     int i, color, beep_count = 0;
357     long len = 0;
358     char *buffer = NULL;
359    
360     sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage");
361     if (-1 == sandbox_log_file)
362     return 0;
363    
364     len = file_length(sandbox_log_file);
365 azarah 30 buffer = (char *)malloc((len + 1) * sizeof(char));
366 ferringb 2 memset(buffer, 0, len + 1);
367     read(sandbox_log_file, buffer, len);
368     file_close(sandbox_log_file);
369    
370 azarah 30 color = ((getenv("NOCOLOR") != NULL) ? 0 : 1);
371 ferringb 2
372 azarah 30 if (color)
373     printf("\e[31;01m");
374 ferringb 2 printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------");
375 azarah 30 if (color)
376     printf("\033[0m");
377     if (color)
378     printf("\e[31;01m");
379 ferringb 2 printf("\nLOG FILE = \"%s\"", sandbox_log);
380 azarah 30 if (color)
381     printf("\033[0m");
382 ferringb 2 printf("\n\n");
383     printf("%s", buffer);
384     if (buffer)
385     free(buffer);
386     buffer = NULL;
387 azarah 30 printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n");
388 ferringb 2
389     beep_count_env = getenv(ENV_SANDBOX_BEEP);
390     if (beep_count_env)
391     beep_count = atoi(beep_count_env);
392     else
393     beep_count = DEFAULT_BEEP_COUNT;
394    
395     for (i = 0; i < beep_count; i++) {
396     fputc('\a', stderr);
397     if (i < beep_count - 1)
398     sleep(1);
399     }
400     return 1;
401     }
402    
403 azarah 30 int spawn_shell(char *argv_bash[])
404 ferringb 2 {
405     #ifdef USE_SYSTEM_SHELL
406     int i = 0;
407     char *sh = NULL;
408     int first = 1;
409     int ret;
410     long len = 0;
411    
412     while (1) {
413     if (NULL == argv_bash[i])
414     break;
415     if (NULL != sh)
416     len = strlen(sh);
417 azarah 30 sh = (char *)realloc(sh, len + strlen(argv_bash[i]) + 5);
418 ferringb 2 if (first) {
419     sh[0] = 0;
420     first = 0;
421     }
422     strcat(sh, "\"");
423     strcat(sh, argv_bash[i]);
424     strcat(sh, "\" ");
425    
426     //printf("%s\n", argv_bash[i]);
427     i++;
428     }
429     printf("%s\n", sh);
430     ret = system(sh);
431     if (sh)
432     free(sh);
433     sh = NULL;
434    
435     if (-1 == ret)
436     return 0;
437     return 1;
438    
439     #else
440     # ifndef NO_FORK
441     int pid;
442     int status = 0;
443     int ret = 0;
444    
445     pid = fork();
446    
447     /* Child's process */
448     if (0 == pid) {
449     # endif
450     execv(argv_bash[0], argv_bash);
451     # ifndef NO_FORK
452     return 0;
453     } else if (pid < 0) {
454     return 0;
455     }
456     ret = waitpid(pid, &status, 0);
457     if ((-1 == ret) || (status > 0))
458     return 0;
459     # endif
460     return 1;
461     #endif
462     }
463    
464 azarah 30 int main(int argc, char **argv)
465 ferringb 2 {
466 azarah 35 int ret = 0, i = 0, success = 1;
467 ferringb 2 #ifdef USE_LD_SO_PRELOAD
468     int preload_file = -1;
469     #endif
470     int sandbox_log_presence = 0;
471     int sandbox_log_file = -1;
472     int pids_file = -1;
473     long len;
474    
475     int *pids_array = NULL;
476     int num_of_pids = 0;
477    
478     // char run_arg[255];
479     char portage_tmp_dir[PATH_MAX];
480     char var_tmp_dir[PATH_MAX];
481     char tmp_dir[PATH_MAX];
482     char sandbox_log[255];
483     char sandbox_debug_log[255];
484     char sandbox_dir[255];
485     char sandbox_lib[255];
486     char *sandbox_pids_file;
487     char sandbox_rc[255];
488     char pid_string[255];
489     char **argv_bash = NULL;
490    
491     char *run_str = "-c";
492     char *home_dir = NULL;
493     char *tmp_string = NULL;
494     #ifdef USE_LD_SO_PRELOAD
495     char **preload_array = NULL;
496     int num_of_preloads = 0;
497     #endif
498    
499     /* Only print info if called with no arguments .... */
500     if (argc < 2)
501     print_debug = 1;
502    
503     if (print_debug)
504 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
505 ferringb 2
506     /* check if a sandbox is already running */
507     if (NULL != getenv(ENV_SANDBOX_ON)) {
508 azarah 30 fprintf(stderr, "Not launching a new sandbox instance\n");
509     fprintf(stderr, "Another one is already running in this process hierarchy.\n");
510 ferringb 2 exit(1);
511     } else {
512    
513     /* determine the location of all the sandbox support files */
514     if (print_debug)
515     printf("Detection of the support files.\n");
516    
517     /* Generate base sandbox path */
518     tmp_string = get_sandbox_path(argv[0]);
519     strncpy(sandbox_dir, tmp_string, 254);
520     if (tmp_string)
521     free(tmp_string);
522     tmp_string = NULL;
523     strcat(sandbox_dir, "/");
524    
525     /* Generate sandbox lib path */
526     tmp_string = get_sandbox_lib(sandbox_dir);
527     strncpy(sandbox_lib, tmp_string, 254);
528     if (tmp_string)
529     free(tmp_string);
530     tmp_string = NULL;
531    
532     /* Generate sandbox pids-file path */
533     sandbox_pids_file = get_sandbox_pids_file();
534    
535     /* Generate sandbox bashrc path */
536     tmp_string = get_sandbox_rc(sandbox_dir);
537     strncpy(sandbox_rc, tmp_string, 254);
538     if (tmp_string)
539     free(tmp_string);
540     tmp_string = NULL;
541    
542     /* verify the existance of required files */
543     if (print_debug)
544     printf("Verification of the required files.\n");
545    
546     #ifndef SB_HAVE_64BIT_ARCH
547     if (file_exist(sandbox_lib, 0) <= 0) {
548 azarah 30 fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib);
549 ferringb 2 return -1;
550 azarah 30 }
551 ferringb 2 #endif
552     if (file_exist(sandbox_rc, 0) <= 0) {
553 azarah 30 fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc);
554 ferringb 2 return -1;
555     }
556     #ifdef USE_LD_SO_PRELOAD
557     /* ensure that the /etc/ld.so.preload file contains an entry for the sandbox lib */
558     if (print_debug)
559     printf("Setting up the ld.so.preload file.\n");
560    
561     /* check if the /etc/ld.so.preload is a regular file */
562     if (file_exist("/etc/ld.so.preload", 1) < 0) {
563     fprintf(stderr, ">>> /etc/ld.so.preload file is not a regular file\n");
564     exit(1);
565     }
566    
567     if (getuid() == 0) {
568     /* Our r+ also will create the file if it doesn't exist */
569     preload_file = file_open("/etc/ld.so.preload", "r+", 1, 0644);
570     if (-1 == preload_file) {
571     preload_adaptable = 0;
572 azarah 30 /* exit(1);*/
573 ferringb 2 }
574     } else {
575     /* Avoid permissions warnings if we're not root */
576     preload_adaptable = 0;
577     }
578    
579     /* Only update /etc/ld.so.preload if we can write to it ... */
580     if (1 == preload_adaptable) {
581     /* Load entries of preload table */
582     num_of_preloads = load_preload_libs(preload_file, &preload_array);
583    
584     /* Zero out our ld.so.preload file */
585     file_truncate(preload_file);
586    
587     /* Write contents of preload file */
588     for (i = 0; i < num_of_preloads + 1; i++) {
589     /* First entry should be our sandbox library */
590     if (0 == i) {
591 azarah 30 if (write(preload_file, sandbox_lib, strlen(sandbox_lib)) != strlen(sandbox_lib)) {
592 ferringb 2 perror(">>> /etc/ld.so.preload file write");
593     success = 0;
594     break;
595     }
596     } else {
597     /* Output all other preload entries */
598 azarah 30 if (write(preload_file, preload_array[i - 1],
599     strlen(preload_array[i - 1])) != strlen(preload_array[i - 1])) {
600 ferringb 2 perror(">>> /etc/ld.so.preload file write");
601     success = 0;
602     break;
603     }
604     }
605     /* Don't forget the return character after each line! */
606     if (1 != write(preload_file, "\n", 1)) {
607     perror(">>> /etc/ld.so.preload file write");
608     success = 0;
609     break;
610     }
611     }
612    
613     for (i = 0; i < num_of_preloads; i++) {
614     if (preload_array[i])
615     free(preload_array[i]);
616     preload_array[i] = NULL;
617     }
618     if (preload_array)
619     free(preload_array);
620     num_of_preloads = 0;
621     preload_array = NULL;
622     }
623    
624     /* That's all we needed to do with the preload file */
625     if (0 < preload_file)
626     file_close(preload_file);
627     preload_file = -1;
628     #endif
629    
630     /* set up the required environment variables */
631     if (print_debug)
632     printf("Setting up the required environment variables.\n");
633    
634     /* Generate sandbox log full path */
635     tmp_string = get_sandbox_log();
636     strncpy(sandbox_log, tmp_string, 254);
637     if (tmp_string)
638     free(tmp_string);
639     tmp_string = NULL;
640    
641     setenv(ENV_SANDBOX_LOG, sandbox_log, 1);
642    
643 azarah 35 sprintf(pid_string, "%d", getpid());
644 ferringb 2 snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s",
645 azarah 30 DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT);
646 ferringb 2 setenv(ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log, 1);
647    
648     home_dir = getenv("HOME");
649     if (!home_dir) {
650     home_dir = "/tmp";
651     setenv("HOME", home_dir, 1);
652     }
653    
654 azarah 35 if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR")
655     : "/var/tmp/portage",
656     portage_tmp_dir)) {
657     perror(">>> get portage_tmp_dir");
658     exit(1);
659 ferringb 2 }
660 azarah 35 if (NULL == realpath("/var/tmp", var_tmp_dir)) {
661     perror(">>> get var_tmp_dir");
662     exit(1);
663     }
664     if (NULL == realpath("/tmp", tmp_dir)) {
665     perror(">>> get tmp_dir");
666     exit(1);
667     }
668 ferringb 2
669     setenv(ENV_SANDBOX_DIR, sandbox_dir, 1);
670     setenv(ENV_SANDBOX_LIB, sandbox_lib, 1);
671 azarah 70 setenv(ENV_SANDBOX_BASHRC, sandbox_rc, 1);
672 ferringb 2 setenv("LD_PRELOAD", sandbox_lib, 1);
673    
674     if (!getenv(ENV_SANDBOX_DENY))
675     setenv(ENV_SANDBOX_DENY, LD_PRELOAD_FILE, 1);
676    
677     if (!getenv(ENV_SANDBOX_READ))
678     setenv(ENV_SANDBOX_READ, "/", 1);
679    
680     /* Set up Sandbox Write path */
681     setenv_sandbox_write(home_dir, portage_tmp_dir, var_tmp_dir, tmp_dir);
682     setenv_sandbox_predict(home_dir);
683    
684     setenv(ENV_SANDBOX_ON, "1", 0);
685    
686     /* if the portage temp dir was present, cd into it */
687     if (NULL != portage_tmp_dir)
688     chdir(portage_tmp_dir);
689    
690 azarah 30 argv_bash = (char **)malloc(6 * sizeof(char *));
691 ferringb 2 argv_bash[0] = strdup("/bin/bash");
692     argv_bash[1] = strdup("-rcfile");
693     argv_bash[2] = strdup(sandbox_rc);
694    
695     if (argc < 2)
696     argv_bash[3] = NULL;
697     else
698     argv_bash[3] = strdup(run_str); /* "-c" */
699    
700 azarah 30 argv_bash[4] = NULL; /* strdup(run_arg); */
701 ferringb 2 argv_bash[5] = NULL;
702    
703     if (argc >= 2) {
704     for (i = 1; i < argc; i++) {
705     if (NULL == argv_bash[4])
706     len = 0;
707     else
708     len = strlen(argv_bash[4]);
709    
710 azarah 30 argv_bash[4] = (char *)realloc(argv_bash[4], (len + strlen(argv[i]) + 2) * sizeof(char));
711 ferringb 2
712     if (0 == len)
713     argv_bash[4][0] = 0;
714     if (1 != i)
715     strcat(argv_bash[4], " ");
716    
717     strcat(argv_bash[4], argv[i]);
718     }
719     }
720    
721     /* set up the required signal handlers */
722     signal(SIGHUP, &stop);
723     signal(SIGINT, &stop);
724     signal(SIGQUIT, &stop);
725     signal(SIGTERM, &stop);
726    
727     /* this one should NEVER be set in ebuilds, as it is the one
728     * private thing libsandbox.so use to test if the sandbox
729     * should be active for this pid, or not.
730     *
731     * azarah (3 Aug 2002)
732     */
733    
734     setenv("SANDBOX_ACTIVE", "armedandready", 1);
735    
736     /* Load our PID into PIDs file */
737     success = 1;
738     if (file_exist(sandbox_pids_file, 1) < 0) {
739     success = 0;
740     fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file);
741     } else {
742     pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage");
743     if (-1 == pids_file)
744     success = 0;
745     }
746     if (1 == success) {
747     /* Grab still active pids */
748     num_of_pids = load_active_pids(pids_file, &pids_array);
749    
750     /* Zero out file */
751     file_truncate(pids_file);
752    
753     /* Output active pids, and append our pid */
754     for (i = 0; i < num_of_pids + 1; i++) {
755     /* Time for our entry */
756     if (i == num_of_pids)
757     sprintf(pid_string, "%d\n", getpid());
758     else
759     sprintf(pid_string, "%d\n", pids_array[i]);
760    
761 azarah 30 if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) {
762 ferringb 2 perror(">>> pids file write");
763     success = 0;
764     break;
765     }
766     }
767     /* Clean pids_array */
768     if (pids_array)
769     free(pids_array);
770     pids_array = NULL;
771     num_of_pids = 0;
772    
773     /* We're done with the pids file */
774     file_close(pids_file);
775     }
776    
777     /* Something went wrong, bail out */
778     if (0 == success) {
779     perror(">>> pids file write");
780     exit(1);
781     }
782    
783     /* STARTING PROTECTED ENVIRONMENT */
784     if (print_debug) {
785     printf("The protected environment has been started.\n");
786 azarah 30 printf("--------------------------------------------------------------------------------\n");
787 ferringb 2 }
788    
789     if (print_debug)
790     printf("Shell being started in forked process.\n");
791    
792     /* Start Bash */
793     if (!spawn_shell(argv_bash)) {
794     if (print_debug)
795     fprintf(stderr, ">>> shell process failed to spawn\n");
796     success = 0;
797     }
798    
799     /* Free bash stuff */
800     for (i = 0; i < 6; i++) {
801     if (argv_bash[i])
802     free(argv_bash[i]);
803     argv_bash[i] = NULL;
804     }
805     if (argv_bash)
806     free(argv_bash);
807     argv_bash = NULL;
808    
809     if (print_debug)
810     printf("Cleaning up sandbox process\n");
811    
812     cleanup();
813    
814     if (print_debug) {
815 azarah 30 printf("========================== Gentoo linux path sandbox ===========================\n");
816 ferringb 2 printf("The protected environment has been shut down.\n");
817     }
818    
819     if (file_exist(sandbox_log, 0)) {
820     sandbox_log_presence = 1;
821     success = 1;
822     if (!print_sandbox_log(sandbox_log))
823     success = 0;
824    
825     #if 0
826     if (!success)
827     exit(1);
828     #endif
829    
830     sandbox_log_file = -1;
831     } else if (print_debug) {
832 azarah 30 printf("--------------------------------------------------------------------------------\n");
833 ferringb 2 }
834    
835 azarah 35 free(sandbox_pids_file);
836    
837 ferringb 2 if ((sandbox_log_presence) || (!success))
838     return 1;
839     else
840     return 0;
841     }
842     }
843    
844 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