1 diff --git a/src/posix_spawn.c b/src/posix_spawn.c
5 +++ b/src/posix_spawn.c
8 + * dhcpcd - DHCP client daemon
9 + * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
10 + * All rights reserved
12 + * Redistribution and use in source and binary forms, with or without
13 + * modification, are permitted provided that the following conditions
15 + * 1. Redistributions of source code must retain the above copyright
16 + * notice, this list of conditions and the following disclaimer.
17 + * 2. Redistributions in binary form must reproduce the above copyright
18 + * notice, this list of conditions and the following disclaimer in the
19 + * documentation and/or other materials provided with the distribution.
21 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 +/* This implementation of posix_spawn is only suitable for the needs of dhcpcd
35 + * but it could easily be extended to other applications. */
37 +#include <sys/types.h>
38 +#include <sys/wait.h>
46 +#include "posix_spawn.h"
50 +#define _NSIG _SIG_MAXSIG + 1
53 +#define _NSIG SIGPWR + 1
57 +extern char **environ;
60 +posix_spawnattr_handle(const posix_spawnattr_t *attrp)
62 + struct sigaction sa;
65 + if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGMASK)
66 + sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, NULL);
68 + if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGDEF) {
69 + memset(&sa, 0, sizeof(sa));
70 + sa.sa_handler = SIG_DFL;
71 + for (i = 1; i < _NSIG; i++) {
72 + if (sigismember(&attrp->posix_attr_sigdefault, i)) {
73 + if (sigaction(i, &sa, NULL) == -1)
83 +is_vfork_safe(short int flags)
85 + return !(flags & (POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK));
89 +posix_spawn(pid_t *pid, const char *path,
90 + const posix_spawn_file_actions_t *file_actions,
91 + const posix_spawnattr_t *attrp,
92 + char *const argv[], char *const envp[])
99 + flags = attrp ? attrp->posix_attr_flags : 0;
100 + if (file_actions == NULL && is_vfork_safe(flags))
103 +#ifdef THERE_IS_NO_FORK
113 + error = posix_spawnattr_handle(attrp);
117 + execve(path, argv, envp);
122 + waitpid(p, NULL, WNOHANG);
123 + else if (pid != NULL)
130 +posix_spawnattr_init(posix_spawnattr_t *attr)
133 + memset(attr, 0, sizeof(*attr));
134 + attr->posix_attr_flags = 0;
135 + sigprocmask(0, NULL, &attr->posix_attr_sigmask);
136 + sigemptyset(&attr->posix_attr_sigdefault);
141 +posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags)
144 + attr->posix_attr_flags = flags;
149 +posix_spawnattr_setsigmask(posix_spawnattr_t *attr, const sigset_t *sigmask)
152 + attr->posix_attr_sigmask = *sigmask;
157 +posix_spawnattr_setsigdefault(posix_spawnattr_t *attr, const sigset_t *sigmask)
160 + attr->posix_attr_sigdefault = *sigmask;