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.
11 /* System libraries. */
13 #include <sys/types.h>
14 #include <sys/param.h>
29 /* Forward declarations. */
31 static void do_child();
33 /* shell_cmd - execute shell command */
35 void shell_cmd(command
)
42 * Most of the work is done within the child process, to minimize the
43 * risk of damage to the parent.
46 switch (child_pid
= fork()) {
48 tcpd_warn("cannot fork: %m");
54 while ((wait_pid
= wait((int *) 0)) != -1 && wait_pid
!= child_pid
)
59 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
61 static void do_child(command
)
68 * Systems with POSIX sessions may send a SIGHUP to grandchildren if the
69 * child exits first. This is sick, sessions were invented for terminals.
72 signal(SIGHUP
, SIG_IGN
);
74 /* Set up new stdin, stdout, stderr, and exec the shell command. */
76 for (tmp_fd
= 0; tmp_fd
< 3; tmp_fd
++)
78 if (open("/dev/null", 2) != 0) {
79 error
= "open /dev/null: %m";
80 } else if (dup(0) != 1 || dup(0) != 2) {
83 (void) execl("/bin/sh", "sh", "-c", command
, (char *) 0);
84 error
= "execl /bin/sh: %m";
87 /* Something went wrong. We MUST terminate the child process. */
89 tcpd_warn("%s", error
);