2 * shell_cmd() takes a shell command after %<character> substitutions. The
3 * command is executed by a /bin/sh child process, with standard input,
4 * standard output and standard error connected to /dev/null.
6 * Diagnostics are reported through syslog(3).
8 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
12 static char sccsid[] = "@(#) shell_cmd.c 1.5 94/12/28 17:42:44";
15 /* System libraries. */
17 #include <sys/types.h>
18 #include <sys/param.h>
33 /* Forward declarations. */
35 static void do_child();
37 /* shell_cmd - execute shell command */
39 void shell_cmd(command)
46 * Most of the work is done within the child process, to minimize the
47 * risk of damage to the parent.
50 switch (child_pid = fork()) {
52 tcpd_warn("cannot fork: %m");
58 while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
63 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
65 static void do_child(command)
72 * Systems with POSIX sessions may send a SIGHUP to grandchildren if the
73 * child exits first. This is sick, sessions were invented for terminals.
76 signal(SIGHUP, SIG_IGN);
78 /* Set up new stdin, stdout, stderr, and exec the shell command. */
80 for (tmp_fd = 0; tmp_fd < 3; tmp_fd++)
82 if (open("/dev/null", 2) != 0) {
83 error = "open /dev/null: %m";
84 } else if (dup(0) != 1 || dup(0) != 2) {
87 (void) execl("/bin/sh", "sh", "-c", command, (char *) 0);
88 error = "execl /bin/sh: %m";
91 /* Something went wrong. We MUST terminate the child process. */