exec/exec_fork_background.c

Go to the documentation of this file.
00001 // Copyright (C) 2006-2007 Benedikt Böhm <hollow@gentoo.org>
00002 //
00003 // This program is free software; you can redistribute it and/or
00004 // modify it under the terms of the GNU General Public License
00005 // as published by the Free Software Foundation; either version 2
00006 // of the License, or (at your option) any later version.
00007 //
00008 // This program is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 // GNU General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this program; if not, write to the Free Software
00015 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
00016 
00017 #include <unistd.h>
00018 #include <stdarg.h>
00019 #include <signal.h>
00020 
00021 #include "exec.h"
00022 #include "mem.h"
00023 #include "printf.h"
00024 #include "strtok.h"
00025 
00026 int exec_fork_background(const char *fmt, ...)
00027 {
00028         va_list ap;
00029         va_start(ap, fmt);
00030 
00031         char *cmd;
00032 
00033         if (_lucid_vasprintf(&cmd, fmt, ap) == -1) {
00034                 va_end(ap);
00035                 return -1;
00036         }
00037 
00038         va_end(ap);
00039 
00040         strtok_t _st, *st = &_st;
00041 
00042         if (!strtok_init_str(st, cmd, " ", 0)) {
00043                 mem_free(cmd);
00044                 return -1;
00045         }
00046 
00047         mem_free(cmd);
00048 
00049         int argc    = strtok_count(st);
00050         char **argv = mem_alloc((argc + 1) * sizeof(char *));
00051 
00052         if (!argv) {
00053                 strtok_free(st);
00054                 return -1;
00055         }
00056 
00057         if (strtok_toargv(st, argv) < 1) {
00058                 mem_free(argv);
00059                 strtok_free(st);
00060                 return -1;
00061         }
00062 
00063         pid_t pid;
00064         int i;
00065 
00066         switch ((pid = fork())) {
00067         case -1:
00068                 return -1;
00069 
00070         case 0:
00071                 usleep(200);
00072 
00073                 for (i = 0; i < 100; i++)
00074                         close(i);
00075 
00076                 execvp(argv[0], argv);
00077 
00078         default:
00079                 mem_free(argv);
00080                 strtok_free(st);
00081                 signal(SIGCHLD, SIG_IGN);
00082         }
00083 
00084         return 0;
00085 }

Generated on Tue Jun 19 20:38:26 2007 for lucid by  doxygen 1.5.2