[mod_cgi] fix pipe_cloexec() when no O_CLOEXEC
[lighttpd.git] / src / lighttpd-angel.c
blob4c629a645cc03afef8f96780c2b3fc681b1da252
1 #include "first.h"
3 /**
4 * angel process for lighttpd
6 * the purpose is the run as root all the time and handle:
7 * - restart on crash
8 * - spawn on HUP to allow graceful restart
9 * - ...
11 * it has to stay safe and small to be trustable
14 #include <sys/wait.h>
16 #include <stdlib.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <time.h>
22 #include <signal.h>
24 #define BINPATH SBIN_DIR"/lighttpd"
26 static siginfo_t last_sigterm_info;
27 static siginfo_t last_sighup_info;
29 static volatile sig_atomic_t start_process = 1;
30 static volatile pid_t pid = -1;
32 #define UNUSED(x) ( (void)(x) )
34 static void sigaction_handler(int sig, siginfo_t *si, void *context) {
35 int exitcode;
37 UNUSED(context);
38 switch (sig) {
39 case SIGINT:
40 case SIGTERM:
41 memcpy(&last_sigterm_info, si, sizeof(*si));
43 /** forward the sig to the child */
44 kill(pid, sig);
45 break;
46 case SIGHUP: /** do a graceful restart */
47 memcpy(&last_sighup_info, si, sizeof(*si));
49 /** do a graceful shutdown on the main process and start a new child */
50 kill(pid, SIGINT);
52 usleep(5 * 1000); /** wait 5 microsec */
54 start_process = 1;
55 break;
56 case SIGCHLD:
57 /** a child died, de-combie it */
58 wait(&exitcode);
59 break;
63 int main(int argc, char **argv) {
64 int is_shutdown = 0;
65 struct sigaction act;
67 UNUSED(argc);
69 /**
70 * we are running as root BEWARE
73 memset(&act, 0, sizeof(act));
74 act.sa_handler = SIG_IGN;
75 sigaction(SIGPIPE, &act, NULL);
76 sigaction(SIGUSR1, &act, NULL);
78 act.sa_sigaction = sigaction_handler;
79 sigemptyset(&act.sa_mask);
80 act.sa_flags = SA_SIGINFO;
82 sigaction(SIGINT, &act, NULL);
83 sigaction(SIGTERM, &act, NULL);
84 sigaction(SIGHUP, &act, NULL);
85 sigaction(SIGALRM, &act, NULL);
86 sigaction(SIGCHLD, &act, NULL);
88 /* check that the compiled in path has the right user,
90 * BEWARE: there is a race between the check here and the exec later
93 while (!is_shutdown) {
94 int exitcode = 0;
96 if (start_process) {
97 pid = fork();
99 if (0 == pid) {
100 /* i'm the child */
102 argv[0] = BINPATH;
104 execvp(BINPATH, argv);
106 exit(1);
107 } else if (-1 == pid) {
108 /** error */
110 return -1;
113 /* I'm the angel */
114 start_process = 0;
117 if ((pid_t)-1 == waitpid(pid, &exitcode, 0)) {
118 switch (errno) {
119 case EINTR:
120 /* someone sent a signal ...
121 * do we have to shutdown or restart the process */
122 break;
123 case ECHILD:
124 /**
125 * make sure we are not in a race between the signal handler
126 * and the process restart */
127 if (!start_process) is_shutdown = 1;
128 break;
129 default:
130 break;
132 } else {
133 /** process went away */
135 if (WIFEXITED(exitcode)) {
136 /** normal exit */
138 is_shutdown = 1;
140 fprintf(stderr, "%s.%d: child (pid=%d) exited normally with exitcode: %d\n",
141 __FILE__, __LINE__,
142 pid,
143 WEXITSTATUS(exitcode));
145 } else if (WIFSIGNALED(exitcode)) {
146 /** got a signal */
148 fprintf(stderr, "%s.%d: child (pid=%d) exited unexpectedly with signal %d, restarting\n",
149 __FILE__, __LINE__,
150 pid,
151 WTERMSIG(exitcode));
153 start_process = 1;
158 return 0;