10 /* Process handling - subprocesses, signals, etc. */
12 static bool_t signal_handling=False;
13 static sigset_t emptyset, fullset;
14 static sigset_t registered,pending;
19 process_callback_fn *cb;
25 static struct child *children=NULL;
29 signal_notify_fn *notify;
31 struct signotify *next;
34 static struct signotify *sigs=NULL;
36 static int spw,spr; /* file descriptors for signal notification pipe */
38 static void set_default_signals(void);
40 /* Long-lived subprocesses can only be started once we've started
41 signal processing so that we can catch SIGCHLD for them and report
42 their exit status using the callback function. We block SIGCHLD
43 until signal processing has begun. */
44 pid_t makesubproc(process_entry_fn *entry, process_callback_fn *cb,
45 void *est, void *cst, cstring_t desc)
50 c=safe_malloc(sizeof(*c),"makesubproc");
55 if (!signal_handling) {
56 fatal("makesubproc called before signal handling started");
61 set_default_signals();
62 sigprocmask(SIG_SETMASK,&emptyset,NULL);
66 fatal_perror("makesubproc (%s): fork",desc);
75 static signal_notify_fn sigchld_handler;
76 static void sigchld_handler(void *st, int signum)
78 struct child *i,*n,**p;
81 process_callback_fn *cb;
86 struct work *w=NULL, *nw;
90 for (i=children; i; i=i->next) {
91 rv=waitpid(i->pid,&status,WNOHANG);
93 fatal_perror("sigchld_handler: waitpid");
98 nw=safe_malloc(sizeof(*nw),"sigchld_handler");
108 /* Remove all the finished tasks from the list of children */
109 for (i=children, p=&children; i; i=n) {
119 /* Notify as appropriate, then free the list */
121 w->cb(w->cst,w->pid,w->status);
128 int sys_cmd(const char *path, const char *arg, ...)
136 /* Parent -> wait for child */
138 rc = waitpid(c,&rv,0);
139 } while(rc < 0 && errno == EINTR);
141 fatal_perror("sys_cmd: waitpid for %s", path);
142 if (rc != c) /* OS has gone mad */
143 fatal("sys_cmd: waitpid for %s returned wrong process ID!",
146 /* If the command failed reporting its exit status */
148 Message(M_ERR, "sys_cmd(%s,%s,...) exited with status %d\n",
149 path, arg, WEXITSTATUS(rv));
150 else if(WIFSIGNALED(rv))
151 Message(M_ERR, "sys_cmd(%s,%s,...) exited with signal %d (%s)%s\n",
152 path, arg, WTERMSIG(rv), strsignal(WTERMSIG(rv)),
153 WCOREDUMP(rv) ? " - core dumped" : "");
155 Message(M_ERR, "sys_cmd(%s,%s,...) exited with wstat %#x\n",
161 /* Child -> exec command */
162 /* Really we ought to strcpy() the arguments into the args array,
163 since the arguments are const char *. Since we'll exit anyway
164 if the execvp() fails this seems somewhat pointless, and
165 increases the chance of the child process failing before it
168 args[0]=(char *)arg; /* program name */
170 while ((args[i++]=va_arg(ap,char *)));
172 fprintf(stderr, "sys_cmd(%s,%s,...): %s\n", path, arg, strerror(errno));
176 fatal_perror("sys_cmd(%s,%s,...)", path, arg);
182 static beforepoll_fn signal_beforepoll;
183 static int signal_beforepoll(void *st, struct pollfd *fds, int *nfds_io,
192 fds[0].events=POLLIN;
196 /* Bodge to work around Ubuntu's strict header files */
197 static void discard(int anything) {}
199 static afterpoll_fn signal_afterpoll;
200 static void signal_afterpoll(void *st, struct pollfd *fds, int nfds)
206 if (nfds && (fds->revents & POLLIN)) {
207 discard(read(spr,buf,16));
208 /* We don't actually care what we read; as
209 long as there was at least one byte
210 (which there was) we'll pick up the
211 signals in the pending set */
213 /* We reset 'pending' before processing any of the signals
214 that were pending so that we don't miss any signals that
215 are delivered partway-through processing (all we assume
216 about signal notification routines is that they handle all
217 the work available at their _start_ and only optionally any
218 work that arrives part-way through their execution). */
219 sigprocmask(SIG_SETMASK,&fullset,&old);
221 sigemptyset(&pending);
222 sigprocmask(SIG_SETMASK,&old,NULL);
224 for (n=sigs; n; n=n->next)
225 if (sigismember(&todo,n->signum))
226 n->notify(n->cst,n->signum);
230 static void set_default_signals(void)
237 for (n=sigs; n; n=n->next)
238 if (!sigismember(&done,n->signum)) {
239 sigaddset(&done,n->signum);
240 sa.sa_handler=SIG_DFL;
243 sigaction(n->signum,&sa,NULL);
247 static void signal_handler(int signum)
251 sigaddset(&pending,signum);
252 /* XXX the write() may set errno, which can make the main program fail.
253 However, signal handlers aren't allowed to modify anything which
254 is not of type sig_atomic_t. The world is broken. */
255 /* I have decided to save and restore errno anyway; on most
256 architectures on which secnet can run modifications to errno
257 will be atomic, and it seems to be the lesser of the two
260 discard(write(spw,&thing,1));
261 /* We don't care if this fails (i.e. the pipe
262 is full) because the service routine will
263 spot the pending signal anyway */
267 static void register_signal_handler(struct signotify *s)
272 if (!signal_handling) return;
274 if (sigismember(®istered,s->signum)) return;
275 sigaddset(®istered,s->signum);
277 sa.sa_handler=signal_handler;
280 rv=sigaction(s->signum,&sa,NULL);
282 fatal_perror("register_signal_handler: sigaction(%d)",s->signum);
286 void request_signal_notification(int signum, signal_notify_fn *notify,
292 s=safe_malloc(sizeof(*s),"request_signal_notification");
297 sigprocmask(SIG_SETMASK,&fullset,&old);
299 register_signal_handler(s);
300 sigprocmask(SIG_SETMASK,&old,NULL);
303 void start_signal_handling(void)
308 sigemptyset(&emptyset);
309 sigfillset(&fullset);
310 sigemptyset(®istered);
311 sigemptyset(&pending);
314 fatal_perror("start_signal_handling: pipe");
318 if (fcntl(spw, F_SETFL, fcntl(spw, F_GETFL)|O_NONBLOCK)==-1) {
319 fatal_perror("start_signal_handling: fcntl(O_NONBLOCK)");
322 register_for_poll(NULL,signal_beforepoll,signal_afterpoll,1,"signal");
323 signal_handling=True;
325 /* Register signal handlers for all the signals we're interested in */
326 for (i=sigs; i; i=i->next) {
327 register_signal_handler(i);
330 request_signal_notification(SIGCHLD,sigchld_handler,NULL);