spawn.c 2.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24
 */

#include "config.h"
#include "wine/port.h"

#include <errno.h>
25
#include <signal.h>
26
#include <stdlib.h>
27 28 29 30 31 32 33 34
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#include <sys/stat.h>
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif

35
#ifndef HAVE_SPAWNVP
36
int spawnvp(int mode, const char *cmdname, const char *const argv[])
37 38 39
{
#ifndef HAVE__SPAWNVP
    int pid = 0, status, wret;
40
    struct sigaction dfl_act, old_act;
41

42 43
    if (mode == _P_OVERLAY)
    {
44
        execvp(cmdname, (char **)argv);
45
        /* if we get here it failed */
46 47 48 49
#ifdef ENOTSUP
        if (errno != ENOTSUP)  /* exec fails on MacOS if the process has multiple threads */
#endif
            return -1;
50
    }
51

52 53 54
    dfl_act.sa_handler = SIG_DFL;
    dfl_act.sa_flags = 0;
    sigemptyset( &dfl_act.sa_mask );
55

56
    if (mode == _P_WAIT) sigaction( SIGCHLD, &dfl_act, &old_act );
57

58 59 60 61
    pid = fork();
    if (pid == 0)
    {
        sigaction( SIGPIPE, &dfl_act, NULL );
62
        execvp(cmdname, (char **)argv);
63 64
        _exit(1);
    }
65

66 67
    if (pid != -1 && mode == _P_OVERLAY) exit(0);

68 69 70 71 72 73 74 75 76 77 78
    if (pid != -1 && mode == _P_WAIT)
    {
        while (pid != (wret = waitpid(pid, &status, 0)))
            if (wret == -1 && errno != EINTR) break;

        if (pid == wret && WIFEXITED(status)) pid = WEXITSTATUS(status);
        else pid = 255; /* abnormal exit with an abort or an interrupt */
    }

    if (mode == _P_WAIT) sigaction( SIGCHLD, &old_act, NULL );
    return pid;
79 80 81 82
#else   /* HAVE__SPAWNVP */
    return _spawnvp(mode, cmdname, argv);
#endif  /* HAVE__SPAWNVP */
}
83
#endif  /* HAVE_SPAWNVP */