3 * daemon main program, collects request and forks handlers
5 * userv is copyright Ian Jackson and other contributors.
6 * See README for full authorship information.
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with userv; if not, see <http://www.gnu.org/licenses/>.
32 #include <sys/types.h>
34 #include <sys/socket.h>
46 static pid_t checkpid= -1, detachpid= -1;
47 static sig_atomic_t needcheck= 1; /* 2 means we half-expect the server to be down */
49 static void checkstalepipes(void) {
50 /* There is an unimportant race here. If there is a stale pipe but
51 * another pair of processes with the same pids is about to create a
52 * new one we can check that the pipe is stale before they recreate
53 * it but then only remove it afterwards; then we remove the pipe
54 * they're actually using causing that invocation to fail with
55 * ENOENT on the pipe. However, this can only happen if things are
56 * already shafted, because we check for stale pipes at startup
57 * before any children have been started, and then only when a child
58 * dies unhelpfully - and we actually have to have some stale pipes
59 * for the race to exist.
65 unsigned long timediff;
68 if (time(&now) == -1) { syslog(LOG_ERR,"get current time: %m"); return; }
70 if (!dir) { syslog(LOG_ERR,"open directory " VARDIR ": %m"); return; }
71 while ((errno=0, de= readdir(dir))) {
72 if (fnmatch(PIPEPATTERN,de->d_name,FNM_PATHNAME|FNM_PERIOD)) continue;
73 r= lstat(de->d_name,&stab); if (r && errno==ENOENT) continue;
74 if (r) { syslog(LOG_ERR,"could not stat `" VARDIR "/%s': %m",de->d_name); continue; }
75 timediff= (unsigned long)now - (unsigned long)stab.st_ctime;
76 if (timediff >= (~0UL>>1) || timediff < 3600) continue;
77 if (unlink(de->d_name) && errno!=ENOENT)
78 syslog(LOG_ERR,"could not remove stale pipe `%s': %m",de->d_name);
80 if (errno) syslog(LOG_ERR,"read directory " VARDIR ": %m");
81 if (closedir(dir)) syslog(LOG_ERR,"close directory " VARDIR ": %m");
84 static void sighandler_chld(int x) {
90 r= waitpid((pid_t)-1,&status,WNOHANG);
91 if (!r || (r==-1 && errno==ECHILD)) break;
92 if (r==-1) { syslog(LOG_ERR,"wait in sigchild handler gave error: %m"); break; }
94 if (WIFEXITED(status) && WEXITSTATUS(status)==4) _exit(4);
95 fprintf(stderr,"uservd: detaching child failed with unexpected code %d\n",status);
99 if (WIFEXITED(status)) {
100 if (!WEXITSTATUS(status)) {
101 syslog(LOG_WARNING,"no longer the uservd - exiting");
103 } else if (WEXITSTATUS(status)!=1) {
104 syslog(LOG_ERR,"check pid %ld exited with status %d",
105 (long)checkpid,WEXITSTATUS(status));
107 } else if (WIFSIGNALED(status)) {
108 if (WTERMSIG(status) == SIGALRM && !WCOREDUMP(status)) {
109 syslog(LOG_WARNING,"check timed out; no longer the uservd - exiting");
112 syslog(LOG_ERR,"check pid %ld %s due to signal %s",
114 WCOREDUMP(status) ? "dumped core" : "died",
115 strsignal(WTERMSIG(status)));
118 syslog(LOG_ERR,"check pid %ld died due to unknown reason, code %d",
119 (long)checkpid,status);
122 alarm(USERVD_MYSELF_CHECK);
124 if (WIFSIGNALED(status)) {
125 syslog(LOG_ERR,"call pid %ld %s due to signal %s",
127 WCOREDUMP(status) ? "dumped core" : "died",
128 strsignal(WTERMSIG(status)));
129 } else if (!WIFEXITED(status)) {
130 syslog(LOG_ERR,"call pid %ld died due to unknown reason, code %d",
132 } else if (WEXITSTATUS(status)==10) {
134 } else if (WEXITSTATUS(status)>12) {
135 if (WEXITSTATUS(status)>24)
136 syslog(LOG_ERR,"call pid %ld exited with status %d >24",
137 (long)r,WEXITSTATUS(status));
147 static void sighandler_usr1(int x) {
151 static void sighandler_alrm(int x) {
155 static void sighandler_termint(int sig) {
156 syslog(LOG_NOTICE,"terminating due to signal %s",strsignal(sig));
160 static void blocksignals(int how) {
165 sigaddset(&set,SIGCHLD);
166 sigaddset(&set,SIGALRM);
167 sigaddset(&set,SIGTERM);
168 sigaddset(&set,SIGINT);
169 r= sigprocmask(how,&set,0); assert(!r);
172 static void NONRETURNING docheck(int needwanted) {
174 /* This subprocess exits with status 0 if the parent should die,
175 * 1 if it should not, and something else if it fails horribly.
179 struct opening_msg opening_mbuf;
180 struct request_msg request_mbuf;
181 unsigned long endmagic;
182 struct sigaction sig;
183 struct sockaddr_un ssockname;
185 openlog(USERVDCHECK_LOGIDENT,LOG_NDELAY|LOG_PID,USERVD_LOGFACILITY);
187 sigemptyset(&sig.sa_mask);
189 sig.sa_handler= SIG_IGN;
190 if (sigaction(SIGPIPE,&sig,0)) { syslog(LOG_ERR,"ignore sigpipe"); exit(1); }
192 sig.sa_handler= SIG_DFL;
193 if (sigaction(SIGALRM,&sig,0)) { syslog(LOG_ERR,"default sigalarm"); exit(1); }
195 sfd= socket(AF_UNIX,SOCK_STREAM,0);
196 if (!sfd) { syslog(LOG_ERR,"ignore sigpipe"); exit(1); }
198 assert(sizeof(ssockname.sun_path) > sizeof(RENDEZVOUS));
199 ssockname.sun_family= AF_UNIX;
200 strcpy(ssockname.sun_path,RENDEZVOUS);
202 r= connect(sfd,(struct sockaddr*)&ssockname,sizeof(ssockname));
204 if (errno == ECONNREFUSED || errno == ENOENT) {
206 syslog(LOG_WARNING,"real uservd daemon is not running: %m");
209 syslog(LOG_ERR,"unable to connect to uservd daemon: %m"); exit(1);
212 alarm(USERVD_MYSELF_TIMEOUT);
213 remain= sizeof(opening_mbuf); p= (unsigned char*)&opening_mbuf;
215 r= read(sfd,p,remain);
216 if (r<0) { syslog(LOG_ERR,"read from server: %m"); exit(1); }
217 if (r==0) { syslog(LOG_ERR,"unexpected EOF from server"); exit(1); }
220 if (opening_mbuf.magic != OPENING_MAGIC) {
221 syslog(LOG_WARNING,"magic number mismatch");
224 if (memcmp(opening_mbuf.protocolchecksumversion,protocolchecksumversion,PCSUMSIZE)) {
225 syslog(LOG_WARNING,"protocol checksum mismatch");
228 if (opening_mbuf.overlordpid != overlordpid) {
229 syslog(LOG_WARNING,"overlord pid mismatch");
232 memset(&request_mbuf,0,sizeof(request_mbuf));
233 request_mbuf.magic= REQUEST_MAGIC;
234 request_mbuf.clientpid= -1;
235 request_mbuf.serviceuserlen= 0;
236 request_mbuf.servicelen= 0;
237 request_mbuf.loginnamelen= 0;
238 request_mbuf.spoofed= 0;
239 request_mbuf.cwdlen= 0;
240 request_mbuf.overridelen= -1;
241 request_mbuf.callinguid= -1;
242 request_mbuf.ngids= 0;
243 request_mbuf.nreadfds= 0;
244 request_mbuf.nwritefds= 0;
245 request_mbuf.nargs= 0;
246 request_mbuf.nvars= 0;
247 r= write(sfd,&request_mbuf,sizeof(request_mbuf));
248 if (r==sizeof(request_mbuf)) {
249 endmagic= REQUEST_END_MAGIC;
250 r= write(sfd,&endmagic,sizeof(endmagic));
253 syslog(LOG_NOTICE,"uservd[%ld] is running",(long)overlordpid);
258 static void NONRETURNING startupsyscallerr(const char *what) {
260 "uservd: system call failed during startup:\n"
262 what,strerror(errno));
266 int main(int argc, char *const *argv) {
267 int mfd, sfd, nfd, e, r, becomedaemon;
269 struct sigaction sigact;
270 struct sockaddr_un ssockname, csockname;
271 pid_t child, parentpid, sid;
274 abort(); /* Do not disable assertions in this security-critical code ! */
279 if (argv[1] && !strcmp(argv[1],"-daemon")) {
283 if (argc>1) { fputs("usage: uservd [-daemon]\n",stderr); exit(3); }
285 openlog(USERVD_LOGIDENT,LOG_NDELAY|LOG_PID,USERVD_LOGFACILITY);
287 if (chdir(VARDIR)) startupsyscallerr("cannot change to " VARDIR);
290 overlordpid= parentpid= getpid();
291 if (parentpid==-1) startupsyscallerr("cannot getpid");
293 mfd= socket(AF_UNIX,SOCK_STREAM,0);
294 if (mfd<0) startupsyscallerr("cannot create master socket");
296 assert(sizeof(ssockname.sun_path) > sizeof(RENDEZVOUS));
297 ssockname.sun_family= AF_UNIX;
298 strcpy(ssockname.sun_path,RENDEZVOUS);
300 r= bind(mfd,(struct sockaddr*)&ssockname,sizeof(ssockname));
301 if (r) startupsyscallerr("cannot bind master socket");
302 if (listen(mfd,5)) startupsyscallerr("cannot listen on master socket");
304 sigemptyset(&sigact.sa_mask);
305 sigaddset(&sigact.sa_mask,SIGCHLD);
306 sigaddset(&sigact.sa_mask,SIGALRM);
307 sigact.sa_flags= SA_NOCLDSTOP;
309 sigact.sa_handler= sighandler_chld;
310 if (sigaction(SIGCHLD,&sigact,0)) startupsyscallerr("cannot setup sigchld handler");
312 sigact.sa_handler= sighandler_alrm;
313 if (sigaction(SIGALRM,&sigact,0)) startupsyscallerr("cannot setup sigalrm handler");
316 sigact.sa_handler= sighandler_usr1;
317 if (sigaction(SIGUSR1,&sigact,0)) startupsyscallerr("cannot setup sigusr1 handler");
319 detachpid= fork(); if (detachpid==-1) startupsyscallerr("cannot fork to detach");
322 fputs("uservd: pause unexpectedly returned during detach\n",stderr);
325 sigact.sa_handler= SIG_DFL;
326 if (sigaction(SIGUSR1,&sigact,0)) startupsyscallerr("cannot restore sigusr1");
329 sigact.sa_handler= sighandler_termint;
330 if (sigaction(SIGTERM,&sigact,0)) startupsyscallerr("cannot setup sigterm handler");
331 if (sigaction(SIGINT,&sigact,0)) startupsyscallerr("cannot setup sigint handler");
334 nfd= open("/dev/null",O_RDWR);
335 if (nfd<0) startupsyscallerr("cannot open /dev/null");
336 sid= setsid(); if (sid == -1) startupsyscallerr("cannot create new session");
337 overlordpid= getpid();
338 if (overlordpid == -1) startupsyscallerr("getpid after detach");
339 if (dup2(nfd,0)<0 || dup2(nfd,1)<0)
340 startupsyscallerr("cannot dup /dev/null for stdin/out");
341 r= kill(parentpid,SIGUSR1); if (r) startupsyscallerr("send SIGUSR1 to detach");
343 if (r<0) { syslog(LOG_CRIT,"cannot dup /dev/null for stderr: %m"); exit(5); }
347 syslog(LOG_NOTICE,"started");
351 while (checkpid==-1) {
354 if (!checkpid) docheck(needcheck);
356 } else if (errno==EAGAIN) {
357 syslog(LOG_ERR,"fork for check - will wait and retry: %m");
358 alarm(USERVD_CHECKFORK_RETRY);
360 } else if (errno!=EINTR) {
361 syslog(LOG_CRIT,"fork for check: %m"); exit(5);
366 csocklen= sizeof(csockname);
367 blocksignals(SIG_UNBLOCK);
368 sfd= accept(mfd,(struct sockaddr*)&csockname,&csocklen);
370 blocksignals(SIG_BLOCK);
373 if (errno == EINTR) continue;
374 if (errno == ENOMEM || errno == EPROTO || errno == EAGAIN) {
375 syslog(LOG_ERR,"unable to accept connection: %m");
378 syslog(LOG_CRIT,"unable to accept new connections: %m");
382 child= nondebug_fork();
383 if (child == (pid_t)-1) {
384 syslog(LOG_ERR,"unable to fork server: %m");
391 blocksignals(SIG_UNBLOCK);