[Toxine-cvs] CVS: toxine/src errors.c,1.4,1.5 errors.h,1.3,1.4 main.c,1.40,1.41
Brought to you by:
f1rmb
From: Daniel Caujolle-B. <f1...@us...> - 2004-07-18 22:27:11
|
Update of /cvsroot/toxine/toxine/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7743 Modified Files: errors.c errors.h main.c Log Message: avoid deadlock with getppid() by forking. Index: errors.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/errors.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- errors.c 1 Jun 2004 21:49:51 -0000 1.4 +++ errors.c 18 Jul 2004 22:27:02 -0000 1.5 @@ -33,6 +33,9 @@ { "Success.", "TOX_ERR_SUCCESS", TOX_ERR_SUCCESS }, + { "fork() failed.", + "TOX_ERR_FORK", TOX_ERR_FORK + }, { "Deadlock occured. Generally this returned value is set when watchdog was triggered.", "TOX_ERR_DEADLOCK", TOX_ERR_DEADLOCK }, Index: errors.h =================================================================== RCS file: /cvsroot/toxine/toxine/src/errors.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -r1.3 -r1.4 --- errors.h 27 May 2004 21:23:43 -0000 1.3 +++ errors.h 18 Jul 2004 22:27:02 -0000 1.4 @@ -24,6 +24,7 @@ typedef enum { TOX_ERR_SUCCESS, /* Everything was fine */ + TOX_ERR_FORK, /* Unable to fork() */ TOX_ERR_DEADLOCK, /* Deadlock happened */ TOX_ERR_SIGSEGV, /* SIGSEGV received */ TOX_ERR_SIGINT, /* SIGINT received */ Index: main.c =================================================================== RCS file: /cvsroot/toxine/toxine/src/main.c,v retrieving revision 1.40 retrieving revision 1.41 diff -u -r1.40 -r1.41 --- main.c 18 Jul 2004 20:31:40 -0000 1.40 +++ main.c 18 Jul 2004 22:27:02 -0000 1.41 @@ -32,6 +32,7 @@ #include <errno.h> #include <unistd.h> #include <sys/types.h> +#include <sys/wait.h> #include <sys/stat.h> #include <string.h> #include <fcntl.h> @@ -283,10 +284,7 @@ } -/* - * ... - */ -int main(int argc, char **argv) { +static int _real_main(int argc, char **argv) { int c = '?'; int option_index = 0; int interactive = 1; @@ -300,7 +298,7 @@ xine_get_version(&major, &minor, &sub); fprintf(stderr, "Require xine library version 1.0.0, found %d.%d.%d.\n", major, minor, sub); - exit(TOX_ERR_WRONG_VERSION); + return TOX_ERR_WRONG_VERSION; } tox = (toxine_t *) xine_xmalloc(sizeof(toxine_t)); @@ -340,7 +338,8 @@ } else { perr("Script filename is required to -S/--script option\n"); - exit(TOX_ERR_BAD_CMDLINE); + error_code_set(tox, TOX_ERR_BAD_CMDLINE); + goto __end; } break; @@ -357,7 +356,8 @@ if((tox->msg_fd = open(ofile, O_WRONLY | O_CREAT | O_TRUNC | O_SYNC, 0644)) < 0) { perr("Cannot open %s: %s\n", ofile, strerror(errno)); - exit(TOX_ERR_INTERNAL); + error_code_set(tox, TOX_ERR_INTERNAL); + goto __end; } } @@ -387,7 +387,8 @@ case 'v': /* Display version and exit*/ toxine_show_version(tox); - exit(TOX_ERR_SUCCESS); + error_code_set(tox, TOX_ERR_SUCCESS); + goto __end; break; case ERROR_CODES: @@ -397,19 +398,23 @@ } else toxine_print_error_codes(tox); - exit(TOX_ERR_SUCCESS); + + error_code_set(tox, TOX_ERR_SUCCESS); + goto __end; break; case 'h': /* Display usage */ case '?': toxine_print_usage(); - exit(TOX_ERR_SUCCESS); + error_code_set(tox, TOX_ERR_SUCCESS); + goto __end; break; default: toxine_print_usage(); perr("Invalid argument %d => exit\n",c); - exit(TOX_ERR_BAD_CMDLINE); + error_code_set(tox, TOX_ERR_BAD_CMDLINE); + goto __end; } } @@ -417,7 +422,8 @@ if((!tox->script.in_use) && (!tox->command.execute)) { perr("You can't use message redirection in prompt mode.\n"); toxine_release(tox); - exit(TOX_ERR_BAD_CMDLINE); + error_code_set(tox, TOX_ERR_BAD_CMDLINE); + goto __end; } if(dup2(tox->msg_fd, STDOUT_FILENO) < 0) @@ -460,8 +466,57 @@ tox->interactive = interactive ? 1 : 0; toxine_run(tox, argc - optind, &argv[optind]); + + + __end: + last_error = tox->last_error; toxine_release(tox); return last_error; } + +/* + * ... + */ +int main(int argc, char **argv) { + int pid, status, ret; + + if(getuid() == 0) { + fprintf(stderr, "You cannot run this program as root.\n"); + exit(TOX_ERR_INTERNAL); + } + + pid = fork(); + switch(pid) { + case -1: + fprintf(stderr, "fork() failed: %s\n", strerror(errno)); + exit(TOX_ERR_FORK); + break; + + case 0: /* Child process */ + ret = _real_main(argc, argv); + exit(ret); + break; + + default: /* Parent */ + if(waitpid(pid, &status, 0) == -1) { + switch(errno) { + case EINTR: + case ENOMEM: + ret = TOX_ERR_FORK; + break; + default: + ret = TOX_ERR_SUCCESS; + break; + } + } + else { + ret = WEXITSTATUS(status); + break; + } + break; + } + + return ret; +} |