2003-04-02 07:20:12 +02:00
|
|
|
/*
|
|
|
|
* spawnvp function
|
|
|
|
*
|
|
|
|
* Copyright 2003 Dimitrie O. Paun
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library 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
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-04-02 07:20:12 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include "wine/port.h"
|
|
|
|
|
2019-12-16 11:59:46 +01:00
|
|
|
#if !defined(HAVE__SPAWNVP) && (!defined(_WIN32) || defined(__CYGWIN__))
|
2013-04-09 13:50:40 +02:00
|
|
|
|
2003-04-02 07:20:12 +02:00
|
|
|
#include <errno.h>
|
2003-05-21 20:34:29 +02:00
|
|
|
#include <signal.h>
|
2007-11-19 14:57:27 +01:00
|
|
|
#include <stdlib.h>
|
2003-04-02 07:20:12 +02:00
|
|
|
#ifdef HAVE_SYS_WAIT_H
|
|
|
|
#include <sys/wait.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-09 13:50:40 +02:00
|
|
|
int _spawnvp(int mode, const char *cmdname, const char *const argv[])
|
2003-04-02 07:20:12 +02:00
|
|
|
{
|
2011-11-30 23:49:37 +01:00
|
|
|
int pid, status, wret;
|
2003-04-02 07:20:12 +02:00
|
|
|
|
2003-05-21 20:34:29 +02:00
|
|
|
if (mode == _P_OVERLAY)
|
|
|
|
{
|
2003-10-15 05:35:54 +02:00
|
|
|
execvp(cmdname, (char **)argv);
|
2007-11-19 14:57:27 +01:00
|
|
|
/* if we get here it failed */
|
2008-12-27 20:06:28 +01:00
|
|
|
#ifdef ENOTSUP
|
|
|
|
if (errno != ENOTSUP) /* exec fails on MacOS if the process has multiple threads */
|
|
|
|
#endif
|
|
|
|
return -1;
|
2003-05-21 20:34:29 +02:00
|
|
|
}
|
2003-04-02 07:20:12 +02:00
|
|
|
|
2003-05-21 20:34:29 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
2011-11-30 23:49:37 +01:00
|
|
|
/* in child */
|
|
|
|
if (mode == _P_DETACH)
|
|
|
|
{
|
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) _exit(1);
|
|
|
|
else if (pid > 0) _exit(0);
|
|
|
|
/* else in grandchild */
|
|
|
|
}
|
|
|
|
|
2011-11-30 23:48:33 +01:00
|
|
|
signal( SIGPIPE, SIG_DFL );
|
2003-10-15 05:35:54 +02:00
|
|
|
execvp(cmdname, (char **)argv);
|
2003-05-21 20:34:29 +02:00
|
|
|
_exit(1);
|
|
|
|
}
|
2003-04-02 07:20:12 +02:00
|
|
|
|
2011-11-30 23:49:37 +01:00
|
|
|
if (pid == -1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (mode == _P_OVERLAY) exit(0);
|
2007-11-19 14:57:27 +01:00
|
|
|
|
2011-11-30 23:49:37 +01:00
|
|
|
if (mode == _P_WAIT || mode == _P_DETACH)
|
2003-05-21 20:34:29 +02:00
|
|
|
{
|
|
|
|
while (pid != (wret = waitpid(pid, &status, 0)))
|
|
|
|
if (wret == -1 && errno != EINTR) break;
|
|
|
|
|
2011-11-30 23:49:37 +01:00
|
|
|
if (pid == wret && WIFEXITED(status))
|
|
|
|
{
|
|
|
|
if (mode == _P_WAIT)
|
|
|
|
pid = WEXITSTATUS(status);
|
|
|
|
else /* mode == _P_DETACH */
|
|
|
|
if (WEXITSTATUS(status) != 0) /* child couldn't fork grandchild */
|
|
|
|
pid = -1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (mode == _P_WAIT)
|
|
|
|
pid = 255; /* abnormal exit with an abort or an interrupt */
|
|
|
|
else /* mode == _P_DETACH */
|
|
|
|
pid = -1;
|
|
|
|
}
|
2003-05-21 20:34:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return pid;
|
2003-04-02 07:20:12 +02:00
|
|
|
}
|
2013-04-09 13:50:40 +02:00
|
|
|
|
|
|
|
#endif /* HAVE__SPAWNVP */
|