2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/usr.sbin/ppp/exec.c,v 1.18.2.6 2002/09/01 02:12:26 brian Exp $
27 * $DragonFly: src/usr.sbin/ppp/exec.c,v 1.2 2003/06/17 04:30:00 dillon Exp $
30 #include <sys/param.h>
31 #include <sys/socket.h>
51 #include "throughput.h"
57 #include "descriptor.h"
69 static struct device execdevice
= {
73 { CD_NOTREQUIRED
, 0 },
91 exec_iov2device(int type
, struct physical
*p
, struct iovec
*iov
,
92 int *niov
, int maxiov
, int *auxfd
, int *nauxfd
)
94 if (type
== EXEC_DEVICE
) {
95 free(iov
[(*niov
)++].iov_base
);
96 physical_SetupStack(p
, execdevice
.name
, PHYSICAL_NOFORCE
);
104 exec_Create(struct physical
*p
)
106 if (p
->fd
< 0 && *p
->name
.full
== '!') {
109 p
->fd
--; /* We own the device but maybe can't use it - change fd */
110 type
= physical_IsSync(p
) ? SOCK_DGRAM
: SOCK_STREAM
;
112 if (socketpair(AF_UNIX
, type
, PF_UNSPEC
, fids
) < 0)
113 log_Printf(LogPHASE
, "Unable to create pipe for line exec: %s\n",
116 static int child_status
; /* This variable is abused ! */
117 int stat
, argc
, i
, ret
, wret
, pidpipe
[2];
121 stat
= fcntl(fids
[0], F_GETFL
, 0);
124 fcntl(fids
[0], F_SETFL
, stat
);
127 if (pipe(pidpipe
) == -1) {
128 log_Printf(LogPHASE
, "Unable to pipe for line exec: %s\n",
131 } else switch ((pid
= fork())) {
133 log_Printf(LogPHASE
, "Unable to fork for line exec: %s\n",
145 setuid(ID0realuid());
149 switch ((pid
= vfork())) {
156 log_Printf(LogPHASE
, "Unable to vfork to drop parent: %s\n",
162 write(pidpipe
[1], &pid
, sizeof pid
);
164 _exit(child_status
); /* The error from exec() ! */
167 log_Printf(LogDEBUG
, "Exec'ing ``%s''\n", p
->name
.base
);
169 if ((argc
= MakeArgs(p
->name
.base
, argv
, VECSIZE(argv
),
170 PARSE_REDUCE
|PARSE_NOHASH
)) < 0) {
171 log_Printf(LogWARN
, "Syntax error in exec command\n");
175 command_Expand(argv
, argc
, (char const *const *)argv
,
176 p
->dl
->bundle
, 0, realpid
);
178 dup2(fids
[1], STDIN_FILENO
);
179 dup2(fids
[1], STDOUT_FILENO
);
180 dup2(fids
[1], STDERR_FILENO
);
181 for (i
= getdtablesize(); i
> STDERR_FILENO
; i
--)
182 fcntl(i
, F_SETFD
, 1);
185 child_status
= errno
; /* Only works for vfork() */
186 printf("execvp failed: %s: %s\r\n", *argv
, strerror(child_status
));
193 if (read(pidpipe
[0], &p
->session_owner
, sizeof p
->session_owner
) !=
194 sizeof p
->session_owner
)
195 p
->session_owner
= (pid_t
)-1;
197 while ((wret
= waitpid(pid
, &stat
, 0)) == -1 && errno
== EINTR
)
200 log_Printf(LogWARN
, "Waiting for child process: %s\n",
203 p
->session_owner
= (pid_t
)-1;
205 } else if (WIFSIGNALED(stat
)) {
206 log_Printf(LogWARN
, "Child process received sig %d !\n",
209 p
->session_owner
= (pid_t
)-1;
211 } else if (WIFSTOPPED(stat
)) {
212 log_Printf(LogWARN
, "Child process received stop sig %d !\n",
214 /* I guess that's ok.... */
215 } else if ((ret
= WEXITSTATUS(stat
))) {
216 log_Printf(LogWARN
, "Cannot exec \"%s\": %s\n", p
->name
.base
,
219 p
->session_owner
= (pid_t
)-1;
223 log_Printf(LogDEBUG
, "Using descriptor %d for child\n", p
->fd
);
224 physical_SetupStack(p
, execdevice
.name
, PHYSICAL_NOFORCE
);
225 if (p
->cfg
.cd
.necessity
!= CD_DEFAULT
)
226 log_Printf(LogWARN
, "Carrier settings ignored\n");