sys/subr_rman: fix some issues
[dragonfly.git] / usr.sbin / ppp / exec.c
blobde0cf9cfc9e42d2e1b3d509c950e0fb8a204ae6d
1 /*-
2 * Copyright (c) 1999 Brian Somers <brian@Awfulhak.org>
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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
24 * SUCH DAMAGE.
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>
32 #include <sys/un.h>
34 #include <errno.h>
35 #include <fcntl.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/wait.h>
40 #include <sys/uio.h>
41 #include <termios.h>
42 #include <unistd.h>
44 #include "layer.h"
45 #include "defs.h"
46 #include "mbuf.h"
47 #include "log.h"
48 #include "timer.h"
49 #include "lqr.h"
50 #include "hdlc.h"
51 #include "throughput.h"
52 #include "fsm.h"
53 #include "lcp.h"
54 #include "ccp.h"
55 #include "link.h"
56 #include "async.h"
57 #include "descriptor.h"
58 #include "physical.h"
59 #include "mp.h"
60 #include "chat.h"
61 #include "command.h"
62 #include "auth.h"
63 #include "chap.h"
64 #include "cbcp.h"
65 #include "datalink.h"
66 #include "id.h"
67 #include "exec.h"
69 static struct device execdevice = {
70 EXEC_DEVICE,
71 "exec",
73 { CD_NOTREQUIRED, 0 },
74 NULL,
75 NULL,
76 NULL,
77 NULL,
78 NULL,
79 NULL,
80 NULL,
81 NULL,
82 NULL,
83 NULL,
84 NULL,
85 NULL,
86 NULL,
87 NULL
90 struct device *
91 exec_iov2device(int type, struct physical *p, struct iovec *iov,
92 int *niov, int maxiov __unused, int *auxfd __unused,
93 int *nauxfd __unused)
95 if (type == EXEC_DEVICE) {
96 free(iov[(*niov)++].iov_base);
97 physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE);
98 return &execdevice;
101 return NULL;
104 struct device *
105 exec_Create(struct physical *p)
107 if (p->fd < 0 && *p->name.full == '!') {
108 int fids[2], type;
110 p->fd--; /* We own the device but maybe can't use it - change fd */
111 type = physical_IsSync(p) ? SOCK_DGRAM : SOCK_STREAM;
113 if (socketpair(AF_UNIX, type, PF_UNSPEC, fids) < 0)
114 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n",
115 strerror(errno));
116 else {
117 static int child_status; /* This variable is abused ! */
118 int stat, argc, i, ret, wret, pidpipe[2];
119 pid_t pid, realpid;
120 char *argv[MAXARGS];
122 stat = fcntl(fids[0], F_GETFL, 0);
123 if (stat > 0) {
124 stat |= O_NONBLOCK;
125 fcntl(fids[0], F_SETFL, stat);
127 realpid = getpid();
128 if (pipe(pidpipe) == -1) {
129 log_Printf(LogPHASE, "Unable to pipe for line exec: %s\n",
130 strerror(errno));
131 close(fids[1]);
132 } else switch ((pid = fork())) {
133 case -1:
134 log_Printf(LogPHASE, "Unable to fork for line exec: %s\n",
135 strerror(errno));
136 close(pidpipe[0]);
137 close(pidpipe[1]);
138 close(fids[1]);
139 break;
141 case 0:
142 close(pidpipe[0]);
143 close(fids[0]);
144 timer_TermService();
145 #ifndef NOSUID
146 setuid(ID0realuid());
147 #endif
149 child_status = 0;
150 switch ((pid = vfork())) {
151 case 0:
152 close(pidpipe[1]);
153 break;
155 case -1:
156 ret = errno;
157 log_Printf(LogPHASE, "Unable to vfork to drop parent: %s\n",
158 strerror(errno));
159 close(pidpipe[1]);
160 _exit(ret);
162 default:
163 write(pidpipe[1], &pid, sizeof pid);
164 close(pidpipe[1]);
165 _exit(child_status); /* The error from exec() ! */
168 log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base);
170 if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv),
171 PARSE_REDUCE|PARSE_NOHASH)) < 0) {
172 log_Printf(LogWARN, "Syntax error in exec command\n");
173 _exit(ESRCH);
176 command_Expand(argv, argc, (char const *const *)argv,
177 p->dl->bundle, 0, realpid);
179 dup2(fids[1], STDIN_FILENO);
180 dup2(fids[1], STDOUT_FILENO);
181 dup2(fids[1], STDERR_FILENO);
182 for (i = getdtablesize(); i > STDERR_FILENO; i--)
183 fcntl(i, F_SETFD, 1);
185 execvp(*argv, argv);
186 child_status = errno; /* Only works for vfork() */
187 printf("execvp failed: %s: %s\r\n", *argv, strerror(child_status));
188 _exit(child_status);
189 break;
191 default:
192 close(pidpipe[1]);
193 close(fids[1]);
194 if (read(pidpipe[0], &p->session_owner, sizeof p->session_owner) !=
195 sizeof p->session_owner)
196 p->session_owner = (pid_t)-1;
197 close(pidpipe[0]);
198 while ((wret = waitpid(pid, &stat, 0)) == -1 && errno == EINTR)
200 if (wret == -1) {
201 log_Printf(LogWARN, "Waiting for child process: %s\n",
202 strerror(errno));
203 close(fids[0]);
204 p->session_owner = (pid_t)-1;
205 break;
206 } else if (WIFSIGNALED(stat)) {
207 log_Printf(LogWARN, "Child process received sig %d !\n",
208 WTERMSIG(stat));
209 close(fids[0]);
210 p->session_owner = (pid_t)-1;
211 break;
212 } else if (WIFSTOPPED(stat)) {
213 log_Printf(LogWARN, "Child process received stop sig %d !\n",
214 WSTOPSIG(stat));
215 /* I guess that's ok.... */
216 } else if ((ret = WEXITSTATUS(stat))) {
217 log_Printf(LogWARN, "Cannot exec \"%s\": %s\n", p->name.base,
218 strerror(ret));
219 close(fids[0]);
220 p->session_owner = (pid_t)-1;
221 break;
223 p->fd = fids[0];
224 log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd);
225 physical_SetupStack(p, execdevice.name, PHYSICAL_NOFORCE);
226 if (p->cfg.cd.necessity != CD_DEFAULT)
227 log_Printf(LogWARN, "Carrier settings ignored\n");
228 return &execdevice;
230 close(fids[0]);
234 return NULL;