#include <sys/param.h>
#include <sys/queue.h>
#include <sys/wait.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <paths.h>
#include "common.h"
extern char **environ;
struct pid {
SLIST_ENTRY(pid) next;
FILE *outfp;
pid_t pid;
char *command;
};
static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
#define ARGV_LEN 42
FILE *
auto_popen(const char *argv0, ...)
{
va_list ap;
struct pid *cur, *p;
pid_t pid;
int error, i, nullfd, outfds[2];
char *arg, *argv[ARGV_LEN], *command;
nullfd = open(_PATH_DEVNULL, O_RDWR, 0);
if (nullfd < 0)
log_err(1, "cannot open %s", _PATH_DEVNULL);
error = pipe(outfds);
if (error != 0)
log_err(1, "pipe");
cur = malloc(sizeof(struct pid));
if (cur == NULL)
log_err(1, "malloc");
argv[0] = checked_strdup(argv0);
command = argv[0];
va_start(ap, argv0);
for (i = 1;; i++) {
if (i >= ARGV_LEN)
log_errx(1, "too many arguments to auto_popen");
arg = va_arg(ap, char *);
argv[i] = arg;
if (arg == NULL)
break;
command = concat(command, ' ', arg);
}
va_end(ap);
cur->command = checked_strdup(command);
switch (pid = fork()) {
case -1:
log_err(1, "fork");
case 0:
dup2(nullfd, STDIN_FILENO);
dup2(outfds[1], STDOUT_FILENO);
close(nullfd);
close(outfds[0]);
close(outfds[1]);
SLIST_FOREACH(p, &pidlist, next)
close(fileno(p->outfp));
execvp(argv[0], argv);
log_err(1, "failed to execute %s", argv[0]);
}
log_debugx("executing \"%s\" as pid %d", command, pid);
cur->outfp = fdopen(outfds[0], "r");
close(nullfd);
close(outfds[1]);
cur->pid = pid;
SLIST_INSERT_HEAD(&pidlist, cur, next);
return (cur->outfp);
}
int
auto_pclose(FILE *iop)
{
struct pid *cur, *last = NULL;
int status;
pid_t pid;
SLIST_FOREACH(cur, &pidlist, next) {
if (cur->outfp == iop)
break;
last = cur;
}
if (cur == NULL) {
return (-1);
}
if (last == NULL)
SLIST_REMOVE_HEAD(&pidlist, next);
else
SLIST_REMOVE_AFTER(last, next);
fclose(cur->outfp);
do {
pid = wait4(cur->pid, &status, 0, NULL);
} while (pid == -1 && errno == EINTR);
if (WIFSIGNALED(status)) {
log_warnx("\"%s\", pid %d, terminated with signal %d",
cur->command, pid, WTERMSIG(status));
return (status);
}
if (WEXITSTATUS(status) != 0) {
log_warnx("\"%s\", pid %d, terminated with exit status %d",
cur->command, pid, WEXITSTATUS(status));
return (status);
}
log_debugx("\"%s\", pid %d, terminated gracefully", cur->command, pid);
free(cur->command);
free(cur);
return (pid == -1 ? -1 : status);
}