periodic(8): Sync with FreeBSD current
[dragonfly.git] / contrib / tcp_wrappers / shell_cmd.c
blob159af3b234cbc088f4bb62018765f9df73dc24e2
1 /*
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.
5 *
6 * Diagnostics are reported through syslog(3).
7 *
8 * Author: Wietse Venema, Eindhoven University of Technology, The Netherlands.
9 */
11 /* System libraries. */
13 #include <sys/types.h>
14 #include <sys/param.h>
15 #include <sys/wait.h>
16 #include <signal.h>
17 #include <stdio.h>
18 #include <unistd.h>
19 #include <syslog.h>
20 #include <string.h>
21 #include <fcntl.h>
23 extern void exit();
25 /* Local stuff. */
27 #include "tcpd.h"
29 /* Forward declarations. */
31 static void do_child();
33 /* shell_cmd - execute shell command */
35 void shell_cmd(command)
36 char *command;
38 int child_pid;
39 int wait_pid;
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()) {
47 case -1: /* error */
48 tcpd_warn("cannot fork: %m");
49 break;
50 case 00: /* child */
51 do_child(command);
52 /* NOTREACHED */
53 default: /* parent */
54 while ((wait_pid = wait((int *) 0)) != -1 && wait_pid != child_pid)
55 /* void */ ;
59 /* do_child - exec command with { stdin, stdout, stderr } to /dev/null */
61 static void do_child(command)
62 char *command;
64 char *error;
65 int tmp_fd;
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++)
77 (void) close(tmp_fd);
78 if (open("/dev/null", 2) != 0) {
79 error = "open /dev/null: %m";
80 } else if (dup(0) != 1 || dup(0) != 2) {
81 error = "dup: %m";
82 } else {
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);
90 _exit(0);