1 /* $Id: proc.c 6124 2003-01-14 06:03:29Z rra $
3 ** Process control routines.
8 #include "portable/wait.h"
13 static PROCESS *PROCtable;
14 static int PROCtablesize;
15 static PROCESS PROCnull = { PSfree, 0, 0, 0, 0, 0 };
19 ** Collect dead processes.
30 pid = waitpid(-1, &status, WNOHANG);
35 syslog(L_ERROR, "%s cant wait %m", LogName);
38 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
41 pp->Status = WEXITSTATUS(status);
43 pp->Collected = Now.time;
51 ** Signal handler that collects the processes, then resets the signal.
54 PROCcatchsignal(int s)
58 #ifndef HAVE_SIGACTION
59 xsignal(s, PROCcatchsignal);
67 ** Synchronous version that notifies a site when its process went away.
75 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
76 if (pp->State == PSdead) {
78 SITEprocdied(&Sites[pp->Site], pp - PROCtable, pp);
87 ** Close down all processes.
97 /* What signal are we sending? */
98 sig = Quickly ? SIGKILL : SIGTERM;
100 /* Send the signal to all living processes. */
101 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++) {
102 if (pp->State != PSrunning)
104 if (kill(pp->Pid, sig) < 0 && errno != ESRCH)
105 syslog(L_ERROR, "%s cant kill %s %ld %m",
106 LogName, Quickly ? "KILL" : "TERM", (long) pp->Pid);
109 /* Collect any who might have died. */
111 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
112 if (pp->State == PSdead)
119 ** Stop watching a process -- we don't care about it any more.
122 PROCunwatch(int process)
124 if (process < 0 || process >= PROCtablesize) {
125 syslog(L_ERROR, "%s internal PROCunwatch %d", LogName, process);
128 PROCtable[process].Site = -1;
133 ** Add a pid to the list of processes we watch.
136 PROCwatch(pid_t pid, int site)
141 /* Find a free slot for this process. */
142 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
143 if (pp->State == PSfree)
146 /* Ran out of room -- grow the table. */
147 PROCtable = xrealloc(PROCtable, (PROCtablesize + 20) * sizeof(PROCESS));
148 for (pp = &PROCtable[PROCtablesize], i=20; --i >= 0; pp++)
150 pp = &PROCtable[PROCtablesize];
154 pp->State = PSrunning;
156 pp->Started = Now.time;
158 return pp - PROCtable;
173 PROCtable = xmalloc(PROCtablesize * sizeof(PROCESS));
174 for (pp = PROCtable, i = PROCtablesize; --i >= 0; pp++)
178 xsignal(SIGCHLD, PROCcatchsignal);
179 #endif /* defined(SIGCHLD) */
180 xsignal(SIGPIPE, PROCcatchsignal);