X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=lib_internal%2Fcommand-exec.c;fp=lib_internal%2Fcommand-exec.c;h=9669f67eb280e744b9686a60449573c56e330aa6;hb=3f3cf95f755f3ef1c31ad8e38153deb4ee214c66;hp=0000000000000000000000000000000000000000;hpb=bfa4b37aaa195007a09bc166e8c8563d5a3c2ef1;p=util-vserver.git diff --git a/lib_internal/command-exec.c b/lib_internal/command-exec.c new file mode 100644 index 0000000..9669f67 --- /dev/null +++ b/lib_internal/command-exec.c @@ -0,0 +1,86 @@ +// $Id: command-exec.c,v 1.3 2005/03/22 15:25:54 ensc Exp $ --*- c -*-- + +// Copyright (C) 2004 Enrico Scholz +// +// 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; version 2 of the License. +// +// 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + + +#ifdef HAVE_CONFIG_H +# include +#endif + +#include "command.h" +#include "util.h" + +#include +#include +#include +#include + +static inline bool +initPipes(int p[2]) +{ + return (pipe(p)!=-1 && + fcntl(p[1], F_SETFD, FD_CLOEXEC)!=-1); +} + +bool +Command_exec(struct Command *cmd, bool do_fork) +{ + int p[2]; + + if (cmd->params_style_==parVEC) + Vector_zeroEnd(&cmd->params.v); + + if (!do_fork) + cmd->pid = 0; + else if (!initPipes(p) || + (cmd->pid = fork())==-1) { + cmd->err = errno; + return false; + } + + if (cmd->pid==0) { + char const ** argv = { 0 }; + + if (do_fork) close(p[0]); + + switch (cmd->params_style_) { + case parVEC : argv = cmd->params.v.data; break; + case parDATA : argv = cmd->params.d; break; + default : break; + } + + execv(cmd->filename ? cmd->filename : argv[0], + reinterpret_cast(char **const)(argv)); + cmd->err = errno; + assert(cmd->err != 0); + + if (do_fork) { + TEMP_FAILURE_RETRY(write(p[1], &cmd->err, sizeof(cmd->err))); + _exit(1); // implicates 'close(p[1])' + } + } + else { + close(p[1]); + if (read(p[0], &cmd->err, sizeof(cmd->err))==0) + cmd->err = 0; + else // cleanup zombies + while (wait4(cmd->pid, 0,0,0)==-1 && + (errno==EINTR || errno==EAGAIN)) {}; + close(p[0]); + } + + return cmd->err==0; +}