1 |
/* |
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 |
/* 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 |
{ |
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 |
data = (char *)malloc((len + 1) * sizeof(char)); |
56 |
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 |
break; /* No more PIDs */ |
72 |
|
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 |
pids[0] = (int *)realloc(pids[0], (num_pids + 1) * sizeof(int)); |
81 |
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 |
int load_preload_libs(int fd, char ***preloads) |
98 |
{ |
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 |
data = (char *)malloc((len + 1) * sizeof(char)); |
110 |
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 |
preloads[0] = (char **)realloc(preloads[0], (num_entries + 1) * sizeof(char **)); |
135 |
preloads[0][num_entries] = strdup(ptr); |
136 |
num_entries++; |
137 |
} |
138 |
|
139 |
if (NULL == ptr2) |
140 |
break; /* No more PIDs */ |
141 |
|
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 |
void cleanup() |
154 |
{ |
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 |
|
162 |
/* Generate sandbox pids-file path */ |
163 |
sandbox_pids_file = get_sandbox_pids_file(); |
164 |
|
165 |
/* Remove this sandbox's bash pid from the global pids |
166 |
* file if it has rights to adapt the ld.so.preload file */ |
167 |
if ((1 == preload_adaptable) && (0 == cleaned_up)) { |
168 |
cleaned_up = 1; |
169 |
success = 1; |
170 |
|
171 |
if (print_debug) |
172 |
printf("Cleaning up pids file.\n"); |
173 |
|
174 |
/* Stat the PIDs file, make sure it exists and is a regular file */ |
175 |
if (file_exist(sandbox_pids_file, 1) <= 0) { |
176 |
fprintf(stderr, ">>> pids file is not a regular file\n"); |
177 |
success = 0; |
178 |
/* We should really not fail if the pidsfile is missing here, but |
179 |
* rather just exit cleanly, as there is still some cleanup to do */ |
180 |
return; |
181 |
} |
182 |
|
183 |
pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage"); |
184 |
if (-1 == pids_file) { |
185 |
success = 0; |
186 |
/* Nothing more to do here */ |
187 |
return; |
188 |
} |
189 |
|
190 |
/* Load "still active" pids into an array */ |
191 |
num_of_pids = load_active_pids(pids_file, &pids_array); |
192 |
//printf("pids: %d\r\n", num_of_pids); |
193 |
|
194 |
|
195 |
file_truncate(pids_file); |
196 |
|
197 |
/* if pids are still running, write only the running pids back to the file */ |
198 |
if (num_of_pids > 1) { |
199 |
for (i = 0; i < num_of_pids; i++) { |
200 |
if (pids_array[i] != getpid()) { |
201 |
sprintf(pid_string, "%d\n", pids_array[i]); |
202 |
|
203 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) { |
204 |
perror(">>> pids file write"); |
205 |
success = 0; |
206 |
break; |
207 |
} |
208 |
} |
209 |
} |
210 |
|
211 |
file_close(pids_file); |
212 |
pids_file = -1; |
213 |
} else { |
214 |
|
215 |
file_close(pids_file); |
216 |
pids_file = -1; |
217 |
|
218 |
/* remove the pidsfile, as this was the last sandbox */ |
219 |
unlink(sandbox_pids_file); |
220 |
} |
221 |
|
222 |
if (pids_array != NULL) |
223 |
free(pids_array); |
224 |
pids_array = NULL; |
225 |
} |
226 |
|
227 |
free(sandbox_pids_file); |
228 |
if (0 == success) |
229 |
return; |
230 |
} |
231 |
|
232 |
void stop(int signum) |
233 |
{ |
234 |
if (stop_called == 0) { |
235 |
stop_called = 1; |
236 |
printf("Caught signal %d in pid %d\r\n", signum, getpid()); |
237 |
cleanup(); |
238 |
} else { |
239 |
fprintf(stderr, "Pid %d alreadly caught signal and is still cleaning up\n", getpid()); |
240 |
} |
241 |
} |
242 |
|
243 |
void setenv_sandbox_write(char *home_dir, char *portage_tmp_dir, char *var_tmp_dir, char *tmp_dir) |
244 |
{ |
245 |
char buf[1024]; |
246 |
|
247 |
/* bzero out entire buffer then append trailing 0 */ |
248 |
memset(buf, 0, sizeof(buf)); |
249 |
|
250 |
if (!getenv(ENV_SANDBOX_WRITE)) { |
251 |
/* these could go into make.globals later on */ |
252 |
snprintf(buf, sizeof(buf), |
253 |
"%s:%s/.gconfd/lock:%s/.bash_history:", |
254 |
"/dev/zero:/dev/fd/:/dev/null:/dev/pts/:" |
255 |
"/dev/vc/:/dev/pty:/dev/tty:/tmp/:" |
256 |
"/dev/shm/ngpt:/var/log/scrollkeeper.log:" |
257 |
"/usr/tmp/conftest:/usr/lib/conftest:" |
258 |
"/usr/lib32/conftest:/usr/lib64/conftest:" |
259 |
"/usr/tmp/cf:/usr/lib/cf:/usr/lib32/cf:/usr/lib64/cf", |
260 |
home_dir, home_dir); |
261 |
|
262 |
if (NULL == portage_tmp_dir) { |
263 |
strncat(buf, tmp_dir, sizeof(buf)); |
264 |
strncat(buf, ":", sizeof(buf)); |
265 |
strncat(buf, var_tmp_dir, sizeof(buf)); |
266 |
strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf)); |
267 |
} else { |
268 |
strncat(buf, portage_tmp_dir, sizeof(buf)); |
269 |
strncat(buf, ":", sizeof(buf)); |
270 |
strncat(buf, tmp_dir, sizeof(buf)); |
271 |
strncat(buf, ":", sizeof(buf)); |
272 |
strncat(buf, var_tmp_dir, sizeof(buf)); |
273 |
strncat(buf, ":/tmp/:/var/tmp/", sizeof(buf)); |
274 |
} |
275 |
buf[sizeof(buf) - 1] = '\0'; |
276 |
setenv(ENV_SANDBOX_WRITE, buf, 1); |
277 |
} |
278 |
} |
279 |
|
280 |
void setenv_sandbox_predict(char *home_dir) |
281 |
{ |
282 |
char buf[1024]; |
283 |
|
284 |
memset(buf, 0, sizeof(buf)); |
285 |
|
286 |
if (!getenv(ENV_SANDBOX_PREDICT)) { |
287 |
/* these should go into make.globals later on */ |
288 |
snprintf(buf, sizeof(buf), "%s/.:" |
289 |
"/usr/lib/python2.0/:" |
290 |
"/usr/lib/python2.1/:" |
291 |
"/usr/lib/python2.2/:" |
292 |
"/usr/lib/python2.3/:" |
293 |
"/usr/lib/python2.4/:" |
294 |
"/usr/lib/python2.5/:" |
295 |
"/usr/lib/python3.0/:", |
296 |
home_dir); |
297 |
|
298 |
buf[sizeof(buf) - 1] = '\0'; |
299 |
setenv(ENV_SANDBOX_PREDICT, buf, 1); |
300 |
} |
301 |
} |
302 |
|
303 |
int print_sandbox_log(char *sandbox_log) |
304 |
{ |
305 |
int sandbox_log_file = -1; |
306 |
char *beep_count_env = NULL; |
307 |
int i, color, beep_count = 0; |
308 |
long len = 0; |
309 |
char *buffer = NULL; |
310 |
|
311 |
sandbox_log_file = file_open(sandbox_log, "r", 1, 0664, "portage"); |
312 |
if (-1 == sandbox_log_file) |
313 |
return 0; |
314 |
|
315 |
len = file_length(sandbox_log_file); |
316 |
buffer = (char *)malloc((len + 1) * sizeof(char)); |
317 |
memset(buffer, 0, len + 1); |
318 |
read(sandbox_log_file, buffer, len); |
319 |
file_close(sandbox_log_file); |
320 |
|
321 |
color = ((getenv("NOCOLOR") != NULL) ? 0 : 1); |
322 |
|
323 |
if (color) |
324 |
printf("\e[31;01m"); |
325 |
printf("--------------------------- ACCESS VIOLATION SUMMARY ---------------------------"); |
326 |
if (color) |
327 |
printf("\033[0m"); |
328 |
if (color) |
329 |
printf("\e[31;01m"); |
330 |
printf("\nLOG FILE = \"%s\"", sandbox_log); |
331 |
if (color) |
332 |
printf("\033[0m"); |
333 |
printf("\n\n"); |
334 |
printf("%s", buffer); |
335 |
if (buffer) |
336 |
free(buffer); |
337 |
buffer = NULL; |
338 |
printf("\e[31;01m--------------------------------------------------------------------------------\033[0m\n"); |
339 |
|
340 |
beep_count_env = getenv(ENV_SANDBOX_BEEP); |
341 |
if (beep_count_env) |
342 |
beep_count = atoi(beep_count_env); |
343 |
else |
344 |
beep_count = DEFAULT_BEEP_COUNT; |
345 |
|
346 |
for (i = 0; i < beep_count; i++) { |
347 |
fputc('\a', stderr); |
348 |
if (i < beep_count - 1) |
349 |
sleep(1); |
350 |
} |
351 |
return 1; |
352 |
} |
353 |
|
354 |
int spawn_shell(char *argv_bash[]) |
355 |
{ |
356 |
int pid; |
357 |
int status = 0; |
358 |
int ret = 0; |
359 |
|
360 |
pid = fork(); |
361 |
|
362 |
/* Child's process */ |
363 |
if (0 == pid) { |
364 |
execv(argv_bash[0], argv_bash); |
365 |
return 0; |
366 |
} else if (pid < 0) { |
367 |
return 0; |
368 |
} |
369 |
ret = waitpid(pid, &status, 0); |
370 |
if ((-1 == ret) || (status > 0)) |
371 |
return 0; |
372 |
|
373 |
return 1; |
374 |
} |
375 |
|
376 |
int main(int argc, char **argv) |
377 |
{ |
378 |
int ret = 0, i = 0, success = 1; |
379 |
int sandbox_log_presence = 0; |
380 |
int sandbox_log_file = -1; |
381 |
int pids_file = -1; |
382 |
long len; |
383 |
|
384 |
int *pids_array = NULL; |
385 |
int num_of_pids = 0; |
386 |
|
387 |
// char run_arg[255]; |
388 |
char portage_tmp_dir[PATH_MAX]; |
389 |
char var_tmp_dir[PATH_MAX]; |
390 |
char tmp_dir[PATH_MAX]; |
391 |
char sandbox_log[255]; |
392 |
char sandbox_debug_log[255]; |
393 |
char sandbox_dir[255]; |
394 |
char sandbox_lib[255]; |
395 |
char *sandbox_pids_file; |
396 |
char sandbox_rc[255]; |
397 |
char pid_string[255]; |
398 |
char **argv_bash = NULL; |
399 |
|
400 |
char *run_str = "-c"; |
401 |
char *home_dir = NULL; |
402 |
char *tmp_string = NULL; |
403 |
|
404 |
/* Only print info if called with no arguments .... */ |
405 |
if (argc < 2) |
406 |
print_debug = 1; |
407 |
|
408 |
if (print_debug) |
409 |
printf("========================== Gentoo linux path sandbox ===========================\n"); |
410 |
|
411 |
/* check if a sandbox is already running */ |
412 |
if (NULL != getenv(ENV_SANDBOX_ON)) { |
413 |
fprintf(stderr, "Not launching a new sandbox instance\n"); |
414 |
fprintf(stderr, "Another one is already running in this process hierarchy.\n"); |
415 |
exit(1); |
416 |
} else { |
417 |
|
418 |
/* determine the location of all the sandbox support files */ |
419 |
if (print_debug) |
420 |
printf("Detection of the support files.\n"); |
421 |
|
422 |
/* Generate base sandbox path */ |
423 |
tmp_string = get_sandbox_path(argv[0]); |
424 |
strncpy(sandbox_dir, tmp_string, 254); |
425 |
if (tmp_string) |
426 |
free(tmp_string); |
427 |
tmp_string = NULL; |
428 |
strcat(sandbox_dir, "/"); |
429 |
|
430 |
/* Generate sandbox lib path */ |
431 |
tmp_string = get_sandbox_lib(sandbox_dir); |
432 |
strncpy(sandbox_lib, tmp_string, 254); |
433 |
if (tmp_string) |
434 |
free(tmp_string); |
435 |
tmp_string = NULL; |
436 |
|
437 |
/* Generate sandbox pids-file path */ |
438 |
sandbox_pids_file = get_sandbox_pids_file(); |
439 |
|
440 |
/* Generate sandbox bashrc path */ |
441 |
tmp_string = get_sandbox_rc(sandbox_dir); |
442 |
strncpy(sandbox_rc, tmp_string, 254); |
443 |
if (tmp_string) |
444 |
free(tmp_string); |
445 |
tmp_string = NULL; |
446 |
|
447 |
/* verify the existance of required files */ |
448 |
if (print_debug) |
449 |
printf("Verification of the required files.\n"); |
450 |
|
451 |
#ifndef SB_HAVE_64BIT_ARCH |
452 |
if (file_exist(sandbox_lib, 0) <= 0) { |
453 |
fprintf(stderr, "Could not open the sandbox library at '%s'.\n", sandbox_lib); |
454 |
return -1; |
455 |
} |
456 |
#endif |
457 |
if (file_exist(sandbox_rc, 0) <= 0) { |
458 |
fprintf(stderr, "Could not open the sandbox rc file at '%s'.\n", sandbox_rc); |
459 |
return -1; |
460 |
} |
461 |
|
462 |
/* set up the required environment variables */ |
463 |
if (print_debug) |
464 |
printf("Setting up the required environment variables.\n"); |
465 |
|
466 |
/* Generate sandbox log full path */ |
467 |
tmp_string = get_sandbox_log(); |
468 |
strncpy(sandbox_log, tmp_string, 254); |
469 |
if (tmp_string) |
470 |
free(tmp_string); |
471 |
tmp_string = NULL; |
472 |
|
473 |
setenv(ENV_SANDBOX_LOG, sandbox_log, 1); |
474 |
|
475 |
sprintf(pid_string, "%d", getpid()); |
476 |
snprintf(sandbox_debug_log, sizeof(sandbox_debug_log), "%s%s%s", |
477 |
DEBUG_LOG_FILE_PREFIX, pid_string, LOG_FILE_EXT); |
478 |
setenv(ENV_SANDBOX_DEBUG_LOG, sandbox_debug_log, 1); |
479 |
|
480 |
home_dir = getenv("HOME"); |
481 |
if (!home_dir) { |
482 |
home_dir = "/tmp"; |
483 |
setenv("HOME", home_dir, 1); |
484 |
} |
485 |
|
486 |
if (NULL == realpath(getenv("PORTAGE_TMPDIR") ? getenv("PORTAGE_TMPDIR") |
487 |
: "/var/tmp/portage", |
488 |
portage_tmp_dir)) { |
489 |
perror(">>> get portage_tmp_dir"); |
490 |
exit(1); |
491 |
} |
492 |
if (NULL == realpath("/var/tmp", var_tmp_dir)) { |
493 |
perror(">>> get var_tmp_dir"); |
494 |
exit(1); |
495 |
} |
496 |
if (NULL == realpath("/tmp", tmp_dir)) { |
497 |
perror(">>> get tmp_dir"); |
498 |
exit(1); |
499 |
} |
500 |
|
501 |
setenv(ENV_SANDBOX_DIR, sandbox_dir, 1); |
502 |
setenv(ENV_SANDBOX_LIB, sandbox_lib, 1); |
503 |
setenv(ENV_SANDBOX_BASHRC, sandbox_rc, 1); |
504 |
if ((NULL != getenv("LD_PRELOAD")) && |
505 |
/* FIXME: for now, do not use current LD_PRELOAD if |
506 |
* it contains libtsocks, as it breaks sandbox, bug #91541. |
507 |
*/ |
508 |
(NULL == strstr(getenv("LD_PRELOAD"), "libtsocks"))) { |
509 |
tmp_string = malloc(strlen(getenv("LD_PRELOAD")) + |
510 |
strlen(sandbox_lib) + 2); |
511 |
if (NULL == tmp_string) { |
512 |
perror(">>> Out of memory (LD_PRELOAD)"); |
513 |
exit(1); |
514 |
} |
515 |
strncpy(tmp_string, sandbox_lib, strlen(sandbox_lib)); |
516 |
strncat(tmp_string, " ", 1); |
517 |
strncat(tmp_string, getenv("LD_PRELOAD"), strlen(getenv("LD_PRELOAD"))); |
518 |
setenv("LD_PRELOAD", tmp_string, 1); |
519 |
free(tmp_string); |
520 |
} else { |
521 |
setenv("LD_PRELOAD", sandbox_lib, 1); |
522 |
} |
523 |
|
524 |
if (!getenv(ENV_SANDBOX_DENY)) |
525 |
setenv(ENV_SANDBOX_DENY, LD_PRELOAD_FILE, 1); |
526 |
|
527 |
if (!getenv(ENV_SANDBOX_READ)) |
528 |
setenv(ENV_SANDBOX_READ, "/", 1); |
529 |
|
530 |
/* Set up Sandbox Write path */ |
531 |
setenv_sandbox_write(home_dir, portage_tmp_dir, var_tmp_dir, tmp_dir); |
532 |
setenv_sandbox_predict(home_dir); |
533 |
|
534 |
setenv(ENV_SANDBOX_ON, "1", 0); |
535 |
|
536 |
/* if the portage temp dir was present, cd into it */ |
537 |
if (NULL != portage_tmp_dir) |
538 |
chdir(portage_tmp_dir); |
539 |
|
540 |
argv_bash = (char **)malloc(6 * sizeof(char *)); |
541 |
argv_bash[0] = strdup("/bin/bash"); |
542 |
argv_bash[1] = strdup("-rcfile"); |
543 |
argv_bash[2] = strdup(sandbox_rc); |
544 |
|
545 |
if (argc < 2) |
546 |
argv_bash[3] = NULL; |
547 |
else |
548 |
argv_bash[3] = strdup(run_str); /* "-c" */ |
549 |
|
550 |
argv_bash[4] = NULL; /* strdup(run_arg); */ |
551 |
argv_bash[5] = NULL; |
552 |
|
553 |
if (argc >= 2) { |
554 |
for (i = 1; i < argc; i++) { |
555 |
if (NULL == argv_bash[4]) |
556 |
len = 0; |
557 |
else |
558 |
len = strlen(argv_bash[4]); |
559 |
|
560 |
argv_bash[4] = (char *)realloc(argv_bash[4], (len + strlen(argv[i]) + 2) * sizeof(char)); |
561 |
|
562 |
if (0 == len) |
563 |
argv_bash[4][0] = 0; |
564 |
if (1 != i) |
565 |
strcat(argv_bash[4], " "); |
566 |
|
567 |
strcat(argv_bash[4], argv[i]); |
568 |
} |
569 |
} |
570 |
|
571 |
/* set up the required signal handlers */ |
572 |
signal(SIGHUP, &stop); |
573 |
signal(SIGINT, &stop); |
574 |
signal(SIGQUIT, &stop); |
575 |
signal(SIGTERM, &stop); |
576 |
|
577 |
/* this one should NEVER be set in ebuilds, as it is the one |
578 |
* private thing libsandbox.so use to test if the sandbox |
579 |
* should be active for this pid, or not. |
580 |
* |
581 |
* azarah (3 Aug 2002) |
582 |
*/ |
583 |
|
584 |
setenv("SANDBOX_ACTIVE", "armedandready", 1); |
585 |
|
586 |
/* Load our PID into PIDs file */ |
587 |
success = 1; |
588 |
if (file_exist(sandbox_pids_file, 1) < 0) { |
589 |
success = 0; |
590 |
fprintf(stderr, ">>> %s is not a regular file\n", sandbox_pids_file); |
591 |
} else { |
592 |
pids_file = file_open(sandbox_pids_file, "r+", 1, 0664, "portage"); |
593 |
if (-1 == pids_file) |
594 |
success = 0; |
595 |
} |
596 |
if (1 == success) { |
597 |
/* Grab still active pids */ |
598 |
num_of_pids = load_active_pids(pids_file, &pids_array); |
599 |
|
600 |
/* Zero out file */ |
601 |
file_truncate(pids_file); |
602 |
|
603 |
/* Output active pids, and append our pid */ |
604 |
for (i = 0; i < num_of_pids + 1; i++) { |
605 |
/* Time for our entry */ |
606 |
if (i == num_of_pids) |
607 |
sprintf(pid_string, "%d\n", getpid()); |
608 |
else |
609 |
sprintf(pid_string, "%d\n", pids_array[i]); |
610 |
|
611 |
if (write(pids_file, pid_string, strlen(pid_string)) != strlen(pid_string)) { |
612 |
perror(">>> pids file write"); |
613 |
success = 0; |
614 |
break; |
615 |
} |
616 |
} |
617 |
/* Clean pids_array */ |
618 |
if (pids_array) |
619 |
free(pids_array); |
620 |
pids_array = NULL; |
621 |
num_of_pids = 0; |
622 |
|
623 |
/* We're done with the pids file */ |
624 |
file_close(pids_file); |
625 |
} |
626 |
|
627 |
/* Something went wrong, bail out */ |
628 |
if (0 == success) { |
629 |
perror(">>> pids file write"); |
630 |
exit(1); |
631 |
} |
632 |
|
633 |
/* STARTING PROTECTED ENVIRONMENT */ |
634 |
if (print_debug) { |
635 |
printf("The protected environment has been started.\n"); |
636 |
printf("--------------------------------------------------------------------------------\n"); |
637 |
} |
638 |
|
639 |
if (print_debug) |
640 |
printf("Shell being started in forked process.\n"); |
641 |
|
642 |
/* Start Bash */ |
643 |
if (!spawn_shell(argv_bash)) { |
644 |
if (print_debug) |
645 |
fprintf(stderr, ">>> shell process failed to spawn\n"); |
646 |
success = 0; |
647 |
} |
648 |
|
649 |
/* Free bash stuff */ |
650 |
for (i = 0; i < 6; i++) { |
651 |
if (argv_bash[i]) |
652 |
free(argv_bash[i]); |
653 |
argv_bash[i] = NULL; |
654 |
} |
655 |
if (argv_bash) |
656 |
free(argv_bash); |
657 |
argv_bash = NULL; |
658 |
|
659 |
if (print_debug) |
660 |
printf("Cleaning up sandbox process\n"); |
661 |
|
662 |
cleanup(); |
663 |
|
664 |
if (print_debug) { |
665 |
printf("========================== Gentoo linux path sandbox ===========================\n"); |
666 |
printf("The protected environment has been shut down.\n"); |
667 |
} |
668 |
|
669 |
if (file_exist(sandbox_log, 0)) { |
670 |
sandbox_log_presence = 1; |
671 |
success = 1; |
672 |
if (!print_sandbox_log(sandbox_log)) |
673 |
success = 0; |
674 |
|
675 |
#if 0 |
676 |
if (!success) |
677 |
exit(1); |
678 |
#endif |
679 |
|
680 |
sandbox_log_file = -1; |
681 |
} else if (print_debug) { |
682 |
printf("--------------------------------------------------------------------------------\n"); |
683 |
} |
684 |
|
685 |
free(sandbox_pids_file); |
686 |
|
687 |
if ((sandbox_log_presence) || (!success)) |
688 |
return 1; |
689 |
else |
690 |
return 0; |
691 |
} |
692 |
} |
693 |
|
694 |
// vim:noexpandtab noai:cindent ai |