1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
|
/*BINFMTC:
* fork/exec helper for pbuilder.
* Copyright (C) 2007-2009 Junichi Uekawa
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdarg.h>
#include "parameter.h"
/*
execvp that does fork.
@return < 0 for failure, exit code for other cases.
*/
int forkexecvp (char *const argv[])
{
int ret;
pid_t pid;
int status;
/* DEBUG: */
{
int i=0;
printf(" forking: ");
while(argv[i])
{
printf("%s ", argv[i]);
i++;
}
printf("\n");
}
fflush(NULL);
switch(pid=fork())
{
case 0:
execvp(argv[0], (char*const*)argv);
perror("execvp");
fprintf(stderr, "Could not execute %s\n", argv[0]);
exit(EXIT_FAILURE);
case -1:
/* error condition in fork(); something is really wrong */
perror("fork");
return -1;
default:
/* parent process, waiting for termination */
if (-1==waitpid(pid, &status, 0))
{
perror("waitpid");
fprintf(stderr, "unexpected waitpid error when waiting for process %i with status %x\n",
pid, status);
return -1;
}
if (!WIFEXITED(status))
{
/* something unexpected */
return -1;
}
ret = WEXITSTATUS(status);
}
return ret;
}
/*
execlp that does fork.
NULL-terminated list of parameters.
cf. execl from FreeBSD sources, and glibc posix/execl.c,
and cygwin exec.cc
@return < 0 for failure, exit code for other cases.
*/
int
forkexeclp (const char *path, const char *arg0, ...)
{
int i, ret;
va_list args;
const char *argv[1024];
pid_t pid;
int status;
va_start(args, arg0);
argv[0] = arg0;
i = 1;
printf(" forking: %s ", argv[0]);/* debug message */
do
{
argv[i] = va_arg(args, const char *);
if (argv[i])
printf("%s ", argv[i]); /* debug message */
if ( i >= 1023 )
{
return -1;
}
}
while (argv[i++] != NULL);
va_end (args);
printf("\n"); /* debug message */
fflush(NULL);
switch(pid=fork())
{
case 0:
execvp(path, (char*const*)argv);
perror("pbuilder: execlp");
fprintf(stderr, "Could not execute %s\n", path);
exit(EXIT_FAILURE);
case -1:
/* error condition in fork(); something is really wrong */
perror("pbuilder: fork");
return -1;
default:
/* parent process, waiting for termination */
if (-1==waitpid(pid, &status, 0))
{
perror("waitpid");
return -1;
}
if (!WIFEXITED(status))
{
/* something unexpected */
return -1;
}
ret = WEXITSTATUS(status);
}
return ret;
}
|