• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * dhcpcd - DHCP client daemon
3  * Copyright (c) 2006-2012 Roy Marples <roy@marples.name>
4  * All rights reserved
5 
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 /* This implementation of posix_spawn is only suitable for the needs of dhcpcd
29  * but it could easily be extended to other applications. */
30 
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 
34 #include <errno.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 
40 #include "../common.h"
41 #include "posix_spawn.h"
42 
43 #ifndef _NSIG
44 #ifdef _SIG_MAXSIG
45 #define _NSIG _SIG_MAXSIG + 1
46 #else
47 /* Guess */
48 #define _NSIG SIGPWR + 1
49 #endif
50 #endif
51 
52 extern char **environ;
53 
54 static int
posix_spawnattr_handle(const posix_spawnattr_t * attrp)55 posix_spawnattr_handle(const posix_spawnattr_t *attrp)
56 {
57 	struct sigaction sa;
58 	int i;
59 
60 	if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGMASK)
61 		sigprocmask(SIG_SETMASK, &attrp->posix_attr_sigmask, NULL);
62 
63 	if (attrp->posix_attr_flags & POSIX_SPAWN_SETSIGDEF) {
64 		memset(&sa, 0, sizeof(sa));
65 		sa.sa_handler = SIG_DFL;
66 		for (i = 1; i < _NSIG; i++) {
67 			if (sigismember(&attrp->posix_attr_sigdefault, i)) {
68 				if (sigaction(i, &sa, NULL) == -1)
69 					return -1;
70 			}
71 		}
72 	}
73 
74 	return 0;
75 }
76 
77 inline static int
is_vfork_safe(short int flags)78 is_vfork_safe(short int flags)
79 {
80 	return !(flags & (POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK));
81 }
82 
83 int
posix_spawn(pid_t * pid,const char * path,const posix_spawn_file_actions_t * file_actions,const posix_spawnattr_t * attrp,char * const argv[],char * const envp[])84 posix_spawn(pid_t *pid, const char *path,
85 	const posix_spawn_file_actions_t *file_actions,
86 	const posix_spawnattr_t *attrp,
87 	char *const argv[], char *const envp[])
88 {
89 	short int flags;
90 	pid_t p;
91 	volatile int error;
92 
93 	error = 0;
94 	flags = attrp ? attrp->posix_attr_flags : 0;
95 	if (file_actions == NULL && is_vfork_safe(flags))
96 		p = vfork();
97 	else
98 #ifdef THERE_IS_NO_FORK
99 		return ENOSYS;
100 #else
101 		p = fork();
102 #endif
103 	switch (p) {
104 	case -1:
105 		return errno;
106 	case 0:
107 		if (attrp) {
108 			error = posix_spawnattr_handle(attrp);
109 			if (error)
110 				_exit(127);
111 		}
112 		execve(path, argv, envp);
113 		error = errno;
114 		_exit(127);
115 	default:
116 		if (error != 0)
117 			waitpid(p, NULL, WNOHANG);
118 		else if (pid != NULL)
119 			*pid = p;
120 		return error;
121 	}
122 }
123 
124 int
posix_spawnattr_init(posix_spawnattr_t * attr)125 posix_spawnattr_init(posix_spawnattr_t *attr)
126 {
127 
128 	memset(attr, 0, sizeof(*attr));
129 	attr->posix_attr_flags = 0;
130 	sigprocmask(0, NULL, &attr->posix_attr_sigmask);
131 	sigemptyset(&attr->posix_attr_sigdefault);
132 	return 0;
133 }
134 
135 int
posix_spawnattr_setflags(posix_spawnattr_t * attr,short flags)136 posix_spawnattr_setflags(posix_spawnattr_t *attr, short flags)
137 {
138 
139 	attr->posix_attr_flags = flags;
140 	return 0;
141 }
142 
143 int
posix_spawnattr_setsigmask(posix_spawnattr_t * attr,const sigset_t * sigmask)144 posix_spawnattr_setsigmask(posix_spawnattr_t *attr, const sigset_t *sigmask)
145 {
146 
147 	attr->posix_attr_sigmask = *sigmask;
148 	return 0;
149 }
150 
151 int
posix_spawnattr_setsigdefault(posix_spawnattr_t * attr,const sigset_t * sigmask)152 posix_spawnattr_setsigdefault(posix_spawnattr_t *attr, const sigset_t *sigmask)
153 {
154 
155 	attr->posix_attr_sigdefault = *sigmask;
156 	return 0;
157 }
158