#include <sys/cdefs.h>
#ifndef lint
#if 0
static char sccsid[] = "@(#) shell_cmd.c 1.5 94/12/28 17:42:44";
#else
__RCSID("$NetBSD: shell_cmd.c,v 1.7 2012/03/22 22:59:43 joerg Exp $");
#endif
#endif
#include <sys/types.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <syslog.h>
#include <string.h>
#include "tcpd.h"
static void do_child(char *) __dead;
void
shell_cmd(char *command)
{
int child_pid;
int wait_pid;
switch (child_pid = fork()) {
case -1:
tcpd_warn("cannot fork: %m");
break;
case 00:
do_child(command);
default:
while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
;
}
}
static void
do_child(char *command)
{
int tmp_fd;
signal(SIGHUP, SIG_IGN);
for (tmp_fd = 0; tmp_fd < 3; tmp_fd++)
(void) close(tmp_fd);
if (open("/dev/null", 2) != 0) {
tcpd_warn("open /dev/null: %m");
} else if (dup(0) != 1 || dup(0) != 2) {
tcpd_warn("dup: %m");
} else {
(void) execl("/bin/sh", "sh", "-c", command, (char *) 0);
tcpd_warn("execl /bin/sh: %m");
}
_exit(0);
}