1 /* $NetBSD: shell_cmd.c,v 1.7 2012/03/22 22:59:43 joerg Exp $ */
4 * shell_cmd() takes a shell command after %<character> substitutions. The
5 * command is executed by a /bin/sh child process, with standard input,
6 * standard output and standard error connected to /dev/null.
8 * Diagnostics are reported through syslog(3).
10 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
13 #include <sys/cdefs.h>
16 static char sccsid
[] = "@(#) shell_cmd.c 1.5 94/12/28 17:42:44";
18 __RCSID("$NetBSD: shell_cmd.c,v 1.7 2012/03/22 22:59:43 joerg Exp $");
22 /* System libraries. */
24 #include <sys/types.h>
25 #include <sys/param.h>
39 /* Forward declarations. */
41 static void do_child(char *) __dead
;
43 /* shell_cmd - execute shell command */
46 shell_cmd(char *command
)
52 * Most of the work is done within the child process, to minimize the
53 * risk of damage to the parent.
56 switch (child_pid
= fork()) {
58 tcpd_warn("cannot fork: %m");
64 while ((wait_pid
= wait((int *) 0)) != -1 && wait_pid
!= child_pid
)
69 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
72 do_child(char *command
)
77 * Systems with POSIX sessions may send a SIGHUP to grandchildren if the
78 * child exits first. This is sick, sessions were invented for terminals.
81 signal(SIGHUP
, SIG_IGN
);
83 /* Set up new stdin, stdout, stderr, and exec the shell command. */
85 for (tmp_fd
= 0; tmp_fd
< 3; tmp_fd
++)
87 if (open("/dev/null", 2) != 0) {
88 tcpd_warn("open /dev/null: %m");
89 } else if (dup(0) != 1 || dup(0) != 2) {
92 (void) execl("/bin/sh", "sh", "-c", command
, (char *) 0);
93 tcpd_warn("execl /bin/sh: %m");
96 /* Something went wrong. We MUST terminate the child process. */