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
29 #include <sys/param.h>
30 #include <sys/socket.h>
52 #include "throughput.h"
58 #include "descriptor.h"
73 struct device dev; /* What struct physical knows about */
74 int fd_out; /* output descriptor */
77 #define device2exec(d) ((d)->type == EXEC_DEVICE ? (struct execdevice *)d : NULL)
82 return sizeof(struct execdevice);
86 exec_Free(struct physical *p)
88 struct execdevice *dev = device2exec(p->handler);
90 if (dev->fd_out != -1)
96 exec_device2iov(struct device *d, struct iovec *iov, int *niov,
97 int maxiov __unused, int *auxfd, int *nauxfd)
99 struct execdevice *dev;
100 int sz = physical_MaxDeviceSize();
102 iov[*niov].iov_base = d = realloc(d, sz);
104 log_Printf(LogALERT, "Failed to allocate memory: %d\n", sz);
105 AbortProgram(EX_OSERR);
107 iov[*niov].iov_len = sz;
110 dev = device2exec(d);
111 if (dev->fd_out >= 0) {
112 *auxfd = dev->fd_out;
118 exec_RemoveFromSet(struct physical *p, fd_set *r, fd_set *w, fd_set *e)
120 struct execdevice *dev = device2exec(p->handler);
123 p->handler->removefromset = NULL;
124 sets = physical_RemoveFromSet(p, r, w, e);
125 p->handler->removefromset = exec_RemoveFromSet;
127 if (dev->fd_out >= 0) {
128 if (w && FD_ISSET(dev->fd_out, w)) {
129 FD_CLR(dev->fd_out, w);
130 log_Printf(LogTIMER, "%s: fdunset(w) %d\n", p->link.name, dev->fd_out);
133 if (e && FD_ISSET(dev->fd_out, e)) {
134 FD_CLR(dev->fd_out, e);
135 log_Printf(LogTIMER, "%s: fdunset(e) %d\n", p->link.name, dev->fd_out);
144 exec_Write(struct physical *p, const void *v, size_t n)
146 struct execdevice *dev = device2exec(p->handler);
147 int fd = dev->fd_out == -1 ? p->fd : dev->fd_out;
149 return write(fd, v, n);
152 static struct device baseexecdevice = {
156 { CD_NOTREQUIRED, 0 },
174 exec_iov2device(int type, struct physical *p, struct iovec *iov,
175 int *niov, int maxiov __unused, int *auxfd, int *nauxfd)
177 if (type == EXEC_DEVICE) {
178 struct execdevice *dev = (struct execdevice *)iov[(*niov)++].iov_base;
180 dev = realloc(dev, sizeof *dev); /* Reduce to the correct size */
182 log_Printf(LogALERT, "Failed to allocate memory: %d\n",
184 AbortProgram(EX_OSERR);
188 dev->fd_out = *auxfd;
193 /* Refresh function pointers etc */
194 memcpy(&dev->dev, &baseexecdevice, sizeof dev->dev);
196 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
204 exec_UpdateSet(struct fdescriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
206 struct physical *p = descriptor2physical(d);
207 struct execdevice *dev = device2exec(p->handler);
210 if (w && dev->fd_out >= 0) {
211 FD_SET(dev->fd_out, w);
212 log_Printf(LogTIMER, "%s: fdset(w) %d\n", p->link.name, dev->fd_out);
217 if (e && dev->fd_out >= 0) {
218 FD_SET(dev->fd_out, e);
219 log_Printf(LogTIMER, "%s: fdset(e) %d\n", p->link.name, dev->fd_out);
223 if (result && *n <= dev->fd_out)
224 *n = dev->fd_out + 1;
226 return result + physical_doUpdateSet(d, r, w, e, n, 0);
230 exec_IsSet(struct fdescriptor *d, const fd_set *fdset)
232 struct physical *p = descriptor2physical(d);
233 struct execdevice *dev = device2exec(p->handler);
234 int result = dev->fd_out >= 0 && FD_ISSET(dev->fd_out, fdset);
235 result += physical_IsSet(d, fdset);
241 exec_Create(struct physical *p)
243 struct execdevice *dev;
247 if (*p->name.full == '!') {
250 if ((dev = malloc(sizeof *dev)) == NULL) {
251 log_Printf(LogWARN, "%s: Cannot allocate an exec device: %s\n",
252 p->link.name, strerror(errno));
257 p->fd--; /* We own the device but maybe can't use it - change fd */
258 type = physical_IsSync(p) ? SOCK_DGRAM : SOCK_STREAM;
260 if (socketpair(AF_UNIX, type, PF_UNSPEC, fids) < 0) {
261 log_Printf(LogPHASE, "Unable to create pipe for line exec: %s\n",
266 static int child_status; /* This variable is abused ! */
267 int stat, argc, i, ret, wret, pidpipe[2];
271 stat = fcntl(fids[0], F_GETFL, 0);
274 fcntl(fids[0], F_SETFL, stat);
277 if (pipe(pidpipe) == -1) {
278 log_Printf(LogPHASE, "Unable to pipe for line exec: %s\n",
284 } else switch ((pid = fork())) {
286 log_Printf(LogPHASE, "Unable to fork for line exec: %s\n",
299 setuid(ID0realuid());
303 switch ((pid = vfork())) {
310 log_Printf(LogPHASE, "Unable to vfork to drop parent: %s\n",
316 write(pidpipe[1], &pid, sizeof pid);
318 _exit(child_status); /* The error from exec() ! */
321 log_Printf(LogDEBUG, "Exec'ing ``%s''\n", p->name.base);
323 if ((argc = MakeArgs(p->name.base, argv, VECSIZE(argv),
324 PARSE_REDUCE|PARSE_NOHASH)) < 0) {
325 log_Printf(LogWARN, "Syntax error in exec command\n");
329 command_Expand(argv, argc, (char const *const *)argv,
330 p->dl->bundle, 0, realpid);
332 dup2(fids[1], STDIN_FILENO);
333 dup2(fids[1], STDOUT_FILENO);
334 dup2(fids[1], STDERR_FILENO);
335 for (i = getdtablesize(); i > STDERR_FILENO; i--)
336 fcntl(i, F_SETFD, 1);
339 child_status = errno; /* Only works for vfork() */
340 printf("execvp failed: %s: %s\r\n", *argv, strerror(child_status));
347 if (read(pidpipe[0], &p->session_owner, sizeof p->session_owner) !=
348 sizeof p->session_owner)
349 p->session_owner = (pid_t)-1;
351 while ((wret = waitpid(pid, &stat, 0)) == -1 && errno == EINTR)
354 log_Printf(LogWARN, "Waiting for child process: %s\n",
357 p->session_owner = (pid_t)-1;
359 } else if (WIFSIGNALED(stat)) {
360 log_Printf(LogWARN, "Child process received sig %d !\n",
363 p->session_owner = (pid_t)-1;
365 } else if (WIFSTOPPED(stat)) {
366 log_Printf(LogWARN, "Child process received stop sig %d !\n",
368 /* I guess that's ok.... */
369 } else if ((ret = WEXITSTATUS(stat))) {
370 log_Printf(LogWARN, "Cannot exec \"%s\": %s\n", p->name.base,
373 p->session_owner = (pid_t)-1;
377 log_Printf(LogDEBUG, "Using descriptor %d for child\n", p->fd);
384 if (fstat(p->fd, &st) != -1 && (st.st_mode & S_IFIFO)) {
385 if ((dev = malloc(sizeof *dev)) == NULL)
386 log_Printf(LogWARN, "%s: Cannot allocate an exec device: %s\n",
387 p->link.name, strerror(errno));
388 else if (p->fd == STDIN_FILENO) {
389 log_Printf(LogPHASE, "%s: Using stdin/stdout to communicate with "
390 "parent (pipe mode)\n", p->link.name);
391 dev->fd_out = dup(STDOUT_FILENO);
393 /* Hook things up so that we monitor dev->fd_out */
394 p->desc.UpdateSet = exec_UpdateSet;
395 p->desc.IsSet = exec_IsSet;
402 memcpy(&dev->dev, &baseexecdevice, sizeof dev->dev);
403 physical_SetupStack(p, dev->dev.name, PHYSICAL_NOFORCE);
404 if (p->cfg.cd.necessity != CD_DEFAULT)
405 log_Printf(LogWARN, "Carrier settings ignored\n");