chiark / gitweb /
Up to halfway down rhs p.12, inc builtin services, but not working yet.
[userv.git] / process.c
1 /*
2  * userv - process.c
3  * daemon code to process one request (is parent of service process)
4  *
5  * Copyright (C)1996-1997 Ian Jackson
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with userv; if not, write to the Free Software
19  * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21
22 /* We do some horrible asynchronous stuff with signals.
23  *
24  * The following objects &c. are used in signal handlers and so
25  * must be protected by calls to blocksignals if they are used in
26  * the main program:
27  *  the syslog() family of calls, and the associated
28  *    syslogopenfacility variable
29  *  swfile (stdio stream)
30  *
31  * The following objects are used in the main program unprotected
32  * and so must not be used in signal handlers:
33  *  srfile
34  *
35  * child and childtokill are used for communication between the
36  * main thread and the signal handlers; none of the signal handlers
37  * return so errno is OK too.
38  */
39
40 #include <stdio.h>
41 #include <stdarg.h>
42 #include <unistd.h>
43 #include <wait.h>
44 #include <assert.h>
45 #include <signal.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <errno.h>
49 #include <syslog.h>
50 #include <pwd.h>
51 #include <grp.h>
52 #include <ctype.h>
53 #include <limits.h>
54 #include <sys/types.h>
55 #include <sys/fcntl.h>
56 #include <sys/stat.h>
57 #include <sys/socket.h>
58 #include <sys/un.h>
59
60 #include "config.h"
61 #include "common.h"
62 #include "daemon.h"
63 #include "lib.h"
64 #include "tokens.h"
65
66 /* NB: defaults for the execution state are not set here, but in
67  * the RESET_CONFIGURATION #define in daemon.h. */
68 struct request_msg request_mbuf;
69 struct keyvaluepair *defvararray;
70 struct fdstate *fdarray;
71 int fdarraysize, fdarrayused;
72 int restfdwantstate= tokv_word_rejectfd, restfdwantrw;
73 int service_ngids;
74 char **argarray;
75 char *serviceuser, *service, *logname, *cwd;
76 char *overridedata, *userrcfile;
77 char *serviceuser_dir, *serviceuser_shell, *callinguser_shell;
78 gid_t *calling_gids, *service_gids;
79 uid_t serviceuser_uid=-1;
80 const char **calling_groups, **service_groups;
81 char *execpath, **execargs;
82 int execute;
83 int setenvironment, suppressargs, disconnecthup;
84 builtinserviceexec_fnt *execbuiltin;
85 int syslogopenfacility=-1;
86
87 static FILE *swfile, *srfile;
88 static pid_t child=-1, childtokill=-1;
89 static pid_t mypid;
90
91 /* Function shared with servexec.c: */
92
93 int synchread(int fd, int ch) {
94   char synchmsg;
95   int r;
96   
97   for (;;) {
98     r= read(fd,&synchmsg,1);
99     if (r==1) break;
100     if (r==0) { errno= ECONNRESET; return -1; }
101     assert(r<0);
102     if (errno!=EINTR) return -1;
103   };
104   assert(synchmsg==ch);
105   return 0;
106 }
107
108 const char *defaultpath(void) {
109   return serviceuser_uid ? DEFAULTPATH_USER : DEFAULTPATH_ROOT;
110 }
111
112 /* General-purpose functions; these do nothing special about signals */
113
114 static void blocksignals(void) {
115   int r;
116   sigset_t set;
117
118   sigemptyset(&set);
119   sigaddset(&set,SIGCHLD);
120   sigaddset(&set,SIGPIPE);
121   r= sigprocmask(SIG_BLOCK,&set,0); assert(!r);
122 }
123
124 static void xfwriteerror(void) {
125   if (errno != EPIPE) syscallerror("writing to client");
126   blocksignals();
127   ensurelogopen(USERVD_LOGFACILITY);
128   syslog(LOG_DEBUG,"client went away (broken pipe)");
129   disconnect(8);
130 }
131
132 static void xfwrite(const void *p, size_t sz, FILE *file) {
133   size_t nr;
134   nr= fwrite(p,1,sz,file);
135   if (nr != sz) xfwriteerror();
136 }
137
138 static void xfflush(FILE *file) {
139   if (fflush(file)) xfwriteerror();
140 }
141
142 /* Functions which may be called only from the main thread.  These may
143  * use main-thread objects and must block signals before using signal
144  * handler objects.
145  */
146
147 static void xfread(void *p, size_t sz) {
148   size_t nr;
149   nr= fread(p,1,sz,srfile); if (nr == sz) return;
150   if (ferror(srfile)) syscallerror("reading from client");
151   blocksignals();
152   assert(feof(srfile));
153   syslog(LOG_DEBUG,"client went away (unexpected EOF)");
154   swfile= 0;
155   disconnect(8);
156 }
157
158 static char *xfreadsetstring(int l) {
159   char *s;
160   assert(l<=MAX_GENERAL_STRING);
161   s= xmalloc(l+1);
162   xfread(s,sizeof(*s)*l);
163   s[l]= 0;
164   return s;
165 }
166
167 static char *xfreadstring(void) {
168   int l;
169   xfread(&l,sizeof(l));
170   return xfreadsetstring(l);
171 }
172
173 static void getevent(struct event_msg *event_r) {
174   int fd;
175   
176   for (;;) {
177     xfread(event_r,sizeof(struct event_msg));
178     switch (event_r->type) {
179     case et_closereadfd:
180       fd= event_r->data.closereadfd.fd;
181       assert(fd<fdarrayused);
182       if (fdarray[fd].holdfd!=-1) {
183         if (close(fdarray[fd].holdfd)) syscallerror("cannot close holding fd");
184         fdarray[fd].holdfd= -1;
185       }
186       break;
187     case et_disconnect:
188       blocksignals();
189       syslog(LOG_DEBUG,"client disconnected");
190       disconnect(4);
191     default:
192       return;
193     }
194   }
195 }
196
197 /* Functions which may be called either from signal handlers or from
198  * the main thread.  They block signals in case they are on the main
199  * thread, and may only use signal handler objects.  None of them
200  * return.  If they did they'd have to restore the signal mask.
201  */
202
203 void miscerror(const char *what) {
204   blocksignals();
205   syslog(LOG_ERR,"failure: %s",what);
206   disconnect(16);
207 }
208
209 void syscallerror(const char *what) {
210   int e;
211
212   e= errno;
213   blocksignals();
214   syslog(LOG_ERR,"system call failure: %s: %s",what,strerror(e));
215   disconnect(18);
216 }
217
218 /* Functions which may be called from signal handlers.  These
219  * may use signal-handler objects.  The main program may only
220  * call them with signals blocked, and they may not use any
221  * main-thread objects.
222  */
223
224 void ensurelogopen(int wantfacility) {
225   if (syslogopenfacility==wantfacility) return;
226   if (syslogopenfacility!=-1) closelog();
227   openlog(USERVD_LOGIDENT,LOG_NDELAY|LOG_PID,wantfacility);
228   syslogopenfacility= wantfacility;
229 }
230
231 void NONRETURNING disconnect(int exitstatus) {
232   /* This function can sometimes indirectly call itself (eg,
233    * xfwrite, syscallerror can cause it to be called).  So, all
234    * the global variables indicating need for action are reset
235    * before the action is taken so that if it fails it isn't
236    * attempted again.
237    */
238   struct progress_msg progress_mbuf;
239   FILE *swfilereal;
240   pid_t orgtokill;
241   int r;
242   
243   if (childtokill!=-1 && disconnecthup) {
244     orgtokill= childtokill;
245     childtokill= -1;
246     if (disconnecthup) {
247       r= kill(-orgtokill,SIGHUP);
248       if (r && errno!=EPERM && errno!=ESRCH)
249         syscallerror("sending SIGHUP to service process group");
250     }
251     child= -1;
252   }
253   if (swfile) {
254     swfilereal= swfile;
255     swfile= 0;
256     memset(&progress_mbuf,0,sizeof(progress_mbuf));
257     progress_mbuf.magic= PROGRESS_MAGIC;
258     progress_mbuf.type= pt_failed;
259     xfwrite(&progress_mbuf,sizeof(progress_mbuf),swfilereal);
260     xfflush(swfilereal);
261   }
262
263   _exit(exitstatus);
264 }
265
266 static void NONRETURNING sighandler_chld(int ignored) {
267   struct progress_msg progress_mbuf;
268   int status;
269   pid_t returned;
270
271   returned= wait3(&status,WNOHANG,0);
272   if (returned==-1) syscallerror("wait for child failed");
273   if (!returned) syscallerror("spurious sigchld");
274   if (returned!=child) syscallerror("spurious child process");
275   child= childtokill= -1;
276
277   memset(&progress_mbuf,0,sizeof(progress_mbuf));
278   progress_mbuf.magic= PROGRESS_MAGIC;
279   progress_mbuf.type= pt_terminated;
280   progress_mbuf.data.terminated.status= status;
281   xfwrite(&progress_mbuf,sizeof(progress_mbuf),swfile);
282   xfflush(swfile);
283
284   syslog(LOG_DEBUG,"service completed (status %d %d)",(status>>8)&0x0ff,status&0x0ff);
285   _exit(0);
286 }
287
288 /* Functions which are called only during setup, before
289  * the signal asynchronicity starts.  They can do anything they like.
290  */
291
292 void ensurefdarray(int fd) {
293   if (fd < fdarrayused) return;
294   if (fd >= fdarraysize) {
295     fdarraysize= ((fd+2)<<1);
296     fdarray= xrealloc(fdarray,sizeof(struct fdstate)*fdarraysize);
297   }
298   while (fd >= fdarrayused) {
299     fdarray[fdarrayused].iswrite= -1;
300     fdarray[fdarrayused].realfd= -1;
301     fdarray[fdarrayused].holdfd= -1;
302     fdarray[fdarrayused].wantstate= restfdwantstate;
303     fdarray[fdarrayused].wantrw= restfdwantrw;
304     fdarrayused++;
305   }
306 }
307
308 static void NONRETURNING generalfailure(const char *prefix, int reserveerrno,
309                                         int errnoval, const char *fmt, va_list al) {
310   char errmsg[MAX_ERRMSG_LEN];
311
312   if (prefix) {
313     strnycpy(errmsg,prefix,sizeof(errmsg));
314     strnytcat(errmsg,": ",sizeof(errmsg));
315   } else {
316     errmsg[0]= 0;
317   }
318   vsnytprintfcat(errmsg,sizeof(errmsg)-reserveerrno,fmt,al);
319   if (reserveerrno) {
320     strnytcat(errmsg,": ",sizeof(errmsg));
321     strnytcat(errmsg,strerror(errnoval),sizeof(errmsg));
322   }
323   senderrmsgstderr(errmsg);
324   syslog(LOG_DEBUG,"service failed (%s)",errmsg);
325   disconnect(12);
326 }
327
328 static void NONRETURNPRINTFFORMAT(1,2) failure(const char *fmt, ...) {
329   va_list al;
330
331   va_start(al,fmt);
332   generalfailure(0,0,0,fmt,al);
333 }  
334
335 static void NONRETURNPRINTFFORMAT(1,2) syscallfailure(const char *fmt, ...) {
336   va_list al;
337   int e;
338
339   e= errno;
340   va_start(al,fmt);
341   generalfailure("system call failed",ERRMSG_RESERVE_ERRNO,e,fmt,al);
342 }
343
344 void senderrmsgstderr(const char *errmsg) {
345   struct progress_msg progress_mbuf;
346   unsigned long ul;
347   int l;
348
349   l= strlen(errmsg);
350   memset(&progress_mbuf,0,sizeof(progress_mbuf));
351   progress_mbuf.magic= PROGRESS_MAGIC;
352   progress_mbuf.type= pt_errmsg;
353   progress_mbuf.data.errmsg.messagelen= l;
354   xfwrite(&progress_mbuf,sizeof(progress_mbuf),swfile);
355   xfwrite(errmsg,l,swfile);
356   ul= PROGRESS_ERRMSG_END_MAGIC;
357   xfwrite(&ul,sizeof(ul),swfile);
358   xfflush(swfile);
359 }
360
361 static void getgroupnames(int ngids, gid_t *list, const char ***names_r) {
362   const char **names;
363   struct group *cgrp;
364   int i;
365   
366   names= xmalloc(sizeof(char*)*ngids);
367   for (i=0; i<ngids; i++) {
368     cgrp= getgrgid(list[i]);
369     if (!cgrp) miscerror("get group entry");
370     names[i]= xstrsave(cgrp->gr_name);
371   }
372   *names_r= names;
373 }
374   
375 /* The per-request main program and its subfunctions. */
376
377 static void setup_comms(int sfd) {
378   static char swbuf[BUFSIZ];
379   static char srbuf[BUFSIZ];
380
381   struct sigaction sig;
382   
383   ensurelogopen(USERVD_LOGFACILITY);
384   syslog(LOG_DEBUG,"call connected");
385
386   mypid= getpid(); if (mypid == -1) syscallerror("getpid");
387
388   sig.sa_handler= SIG_IGN;
389   sigemptyset(&sig.sa_mask);
390   sig.sa_flags= 0;
391   if (sigaction(SIGPIPE,&sig,0)) syscallerror("cannot ignore sigpipe");
392
393   srfile= fdopen(sfd,"r");
394   if (!srfile) syscallerror("turn socket fd into reading FILE*");
395   if (setvbuf(srfile,srbuf,_IOFBF,sizeof(srbuf)))
396     syscallerror("set buffering on socket reads");
397
398   swfile= fdopen(sfd,"w");
399   if (!swfile) syscallerror("turn socket fd into writing FILE*");
400   if (setvbuf(swfile,swbuf,_IOFBF,sizeof(swbuf)))
401     syscallerror("set buffering on socket writes");
402 }
403
404 static void send_opening(void) {
405   struct opening_msg opening_mbuf;
406
407   memset(&opening_mbuf,0,sizeof(opening_mbuf));
408   opening_mbuf.magic= OPENING_MAGIC;
409   memcpy(opening_mbuf.protocolchecksumversion,protocolchecksumversion,PCSUMSIZE);
410   opening_mbuf.serverpid= mypid;
411   xfwrite(&opening_mbuf,sizeof(opening_mbuf),swfile);
412   xfflush(swfile);
413 }
414
415 static void receive_request(void) {
416   int i, fd;
417   unsigned long ul;
418
419   xfread(&request_mbuf,sizeof(request_mbuf));
420   serviceuser= xfreadsetstring(request_mbuf.serviceuserlen);
421   service= xfreadsetstring(request_mbuf.servicelen);
422   logname= xfreadsetstring(request_mbuf.lognamelen);
423   cwd= xfreadsetstring(request_mbuf.cwdlen);
424   if (request_mbuf.overridelen >= 0) {
425     assert(request_mbuf.overridelen <= MAX_OVERRIDE_LEN);
426     overridedata= xfreadsetstring(request_mbuf.overridelen);
427   } else {
428     assert(request_mbuf.overridelen == -1);
429     overridedata= 0;
430   }
431   assert(request_mbuf.ngids <= MAX_GIDS);
432   calling_gids= xmalloc(sizeof(gid_t)*request_mbuf.ngids);
433   xfread(calling_gids,sizeof(gid_t)*request_mbuf.ngids);
434
435   fdarraysize= 4; fdarray= xmalloc(sizeof(struct fdstate)*fdarraysize);
436   fdarrayused= 1; fdarray[0].iswrite= -1;
437   fdarray[0].wantstate= tokv_word_rejectfd;
438   assert(request_mbuf.nreadfds+request_mbuf.nwritefds <= MAX_ALLOW_FD+1);
439   for (i=0; i<request_mbuf.nreadfds+request_mbuf.nwritefds; i++) {
440     xfread(&fd,sizeof(int));
441     assert(fd <= MAX_ALLOW_FD);
442     ensurefdarray(fd);
443     assert(fdarray[fd].iswrite == -1);
444     fdarray[fd].iswrite= (i>=request_mbuf.nreadfds);
445   }
446
447   assert(request_mbuf.nargs <= MAX_ARGSDEFVAR);
448   argarray= xmalloc(sizeof(char*)*(request_mbuf.nargs));
449   for (i=0; i<request_mbuf.nargs; i++) argarray[i]= xfreadstring();
450   assert(request_mbuf.nvars <= MAX_ARGSDEFVAR);
451   defvararray= xmalloc(sizeof(struct keyvaluepair)*request_mbuf.nvars);
452   for (i=0; i<request_mbuf.nvars; i++) {
453     defvararray[i].key= xfreadstring();
454     assert(defvararray[i].key[0]);
455     defvararray[i].value= xfreadstring();
456   }
457   xfread(&ul,sizeof(ul));
458   assert(ul == REQUEST_END_MAGIC);
459 }
460
461 static void establish_pipes(void) {
462   int fd, tempfd;
463   char pipepathbuf[PIPEMAXLEN+2];
464   
465   for (fd=0; fd<fdarrayused; fd++) {
466     if (fdarray[fd].iswrite == -1) continue;
467     pipepathbuf[sizeof(pipepathbuf)-2]= 0;
468     snyprintf(pipepathbuf,sizeof(pipepathbuf),PIPEFORMAT,
469               (unsigned long)request_mbuf.clientpid,(unsigned long)mypid,fd);
470     assert(!pipepathbuf[sizeof(pipepathbuf)-2]);
471     tempfd= open(pipepathbuf,O_RDWR);
472     if (tempfd<0) syscallerror("prelim open pipe");
473     if (fdarray[fd].iswrite) {
474       fdarray[fd].holdfd= -1;
475       fdarray[fd].realfd= open(pipepathbuf, O_WRONLY);
476     } else {
477       fdarray[fd].holdfd= open(pipepathbuf, O_WRONLY);
478       if (fdarray[fd].holdfd<0) syscallerror("hold open pipe");
479       fdarray[fd].realfd= open(pipepathbuf, O_RDONLY);
480     }
481     if (fdarray[fd].realfd<0) syscallerror("real open pipe");
482     if (unlink(pipepathbuf)) syscallerror("unlink pipe");
483     if (close(tempfd)) syscallerror("close prelim fd onto pipe");
484   }
485 }
486
487 static void lookup_uidsgids(void) {
488   struct passwd *pw;
489
490   pw= getpwnam(logname);
491   if (!pw) miscerror("look up calling user");
492   assert(!strcmp(pw->pw_name,logname));
493   callinguser_shell= xstrsave(pw->pw_shell);
494
495   pw= getpwnam(serviceuser);
496   if (!pw) miscerror("look up service user");
497   assert(!strcmp(pw->pw_name,serviceuser));
498   serviceuser_dir= xstrsave(nondebug_serviceuserdir(pw->pw_dir));
499   serviceuser_shell= xstrsave(pw->pw_shell);
500   serviceuser_uid= pw->pw_uid;
501   
502   if (initgroups(pw->pw_name,pw->pw_gid)) syscallerror("initgroups");
503   if (setreuid(pw->pw_uid,pw->pw_uid)) syscallerror("setreuid 1");
504   if (setreuid(pw->pw_uid,pw->pw_uid)) syscallerror("setreuid 2");
505   if (pw->pw_uid)
506     if (!setreuid(pw->pw_uid,0)) miscerror("setreuid 3 unexpectedly succeeded");
507   if (errno != EPERM) syscallerror("setreuid 3 failed in unexpected way");
508
509   service_ngids= getgroups(0,0); if (service_ngids == -1) syscallerror("getgroups(0,0)");
510   if (service_ngids > MAX_GIDS) miscerror("service user is in far too many groups");
511   service_gids= xmalloc(sizeof(gid_t)*(service_ngids+1));
512   service_gids[0]= pw->pw_gid;
513   if (getgroups(service_ngids,service_gids+1) != service_ngids)
514     syscallerror("getgroups(size,list)");
515
516   getgroupnames(service_ngids,service_gids,&service_groups);
517   getgroupnames(request_mbuf.ngids,calling_gids,&calling_groups);
518 }
519
520 static void check_find_executable(void) {
521   int r, partsize;
522   char *part, *exectry;
523   const char *string, *delim, *nextstring;
524   struct stat stab;
525
526   switch (execute) {
527   case tokv_word_reject:
528     failure("request rejected");
529   case tokv_word_execute:
530     r= stat(execpath,&stab);
531     if (r) syscallfailure("checking for executable `%s'",execpath);
532     break;
533   case tokv_word_executefromdirectory:
534     r= stat(execpath,&stab);
535     if (r) syscallfailure("checking for executable in directory, `%s'",execpath);
536     break;
537   case tokv_word_executebuiltin:
538     break;
539   case tokv_word_executefrompath:
540     if (strchr(service,'/')) {
541       r= stat(service,&stab);
542       if (r) syscallfailure("execute-from-path (contains slash)"
543                             " cannot check for executable `%s'",service);
544       execpath= service;
545     } else {
546       string= getenv("PATH");
547       if (!string) string= defaultpath();
548       while (string) {
549         delim= strchr(string,':');
550         if (delim) {
551           if (delim-string > MAX_GENERAL_STRING)
552             failure("execute-from-path, but PATH component too long");
553           partsize= delim-string;
554           nextstring= delim+1;
555         } else {
556           partsize= strlen(string);
557           nextstring= 0;
558         }
559         part= xstrsubsave(string,partsize);
560         exectry= part[0] ? xstrcat3save(part,"/",service) : xstrsave(service);
561         free(part);
562         r= stat(exectry,&stab);
563         if (!r) { execpath= exectry; break; }
564         free(exectry);
565         string= nextstring;
566       }
567       if (!execpath) failure("execute-from-path, but program `%s' not found",service);
568     }
569     break;
570   default:
571     abort();
572   }
573 }
574
575 static void check_fds(void) {
576   int fd;
577   
578   assert(fdarrayused>=2);
579   if (!(fdarray[2].wantstate == tokv_word_requirefd ||
580         fdarray[2].wantstate == tokv_word_allowfd) ||
581       fdarray[2].wantrw != tokv_word_write)
582     failure("must have stderr (fd 2), but file descriptor setup in "
583             "configuration does not have it or not for writing");
584
585   for (fd=0; fd<fdarrayused; fd++) {
586     switch (fdarray[fd].wantstate) {
587     case tokv_word_rejectfd:
588       if (fdarray[fd].realfd != -1)
589         failure("file descriptor %d provided but rejected",fd);
590       break;
591     case tokv_word_ignorefd:
592       if (fdarray[fd].realfd != -1)
593         if (close(fdarray[fd].realfd))
594           syscallfailure("close unwanted file descriptor %d",fd);
595       fdarray[fd].realfd= -1;
596       break;
597     case tokv_word_nullfd:
598       if (fdarray[fd].realfd != -1) close(fdarray[fd].realfd);
599       fdarray[fd].realfd= open("/dev/null",
600                                fdarray[fd].iswrite == -1 ? O_RDWR :
601                                fdarray[fd].iswrite ? O_WRONLY : O_RDONLY);
602       if (fdarray[fd].realfd<0)
603         syscallfailure("cannot open /dev/null for null fd");
604       break;
605     case tokv_word_requirefd:
606       if (fdarray[fd].realfd == -1)
607         failure("file descriptor %d not provided but required",fd);
608       /* fall through */
609     case tokv_word_allowfd:
610       if (fdarray[fd].realfd == -1) {
611         fdarray[fd].iswrite= (fdarray[fd].wantrw == tokv_word_write);
612         fdarray[fd].realfd= open("/dev/null",fdarray[fd].iswrite ? O_WRONLY : O_RDONLY);
613         if (fdarray[fd].realfd<0)
614           syscallfailure("cannot open /dev/null for allowed but not provided fd");
615       } else {
616         if (fdarray[fd].iswrite) {
617           if (fdarray[fd].wantrw != tokv_word_write)
618             failure("file descriptor %d provided write, wanted read",fd);
619         } else {
620           if (fdarray[fd].wantrw != tokv_word_read)
621             failure("file descriptor %d provided read, wanted write",fd);
622         }
623       }
624     }
625   }
626 }
627
628 static void send_progress_ok(void) {
629   struct progress_msg progress_mbuf;
630
631   memset(&progress_mbuf,0,sizeof(progress_mbuf));
632   progress_mbuf.magic= PROGRESS_MAGIC;
633   progress_mbuf.type= pt_ok;
634   xfwrite(&progress_mbuf,sizeof(progress_mbuf),swfile);
635   xfflush(swfile);
636 }
637
638 static void fork_service_synch(void) {
639   pid_t newchild;
640   struct sigaction sig;
641   int r, synchsocket[2];
642   char synchmsg;
643
644   r= socketpair(AF_UNIX,SOCK_STREAM,0,synchsocket);
645   if (r) syscallerror("cannot create socket for synch");
646
647   sig.sa_handler= sighandler_chld;
648   sigemptyset(&sig.sa_mask);
649   sigaddset(&sig.sa_mask,SIGCHLD);
650   sig.sa_flags= 0;
651   if (sigaction(SIGCHLD,&sig,0)) syscallerror("cannot set sigchld handler");
652
653   newchild= fork();
654   if (newchild == -1) syscallerror("cannot fork to invoke service");
655   if (!newchild) execservice(synchsocket,fileno(swfile));
656   childtokill= child= newchild;
657
658   if (close(synchsocket[1])) syscallerror("cannot close other end of synch socket");
659
660   r= synchread(synchsocket[0],'y');
661   if (r) syscallerror("read synch byte from child");
662
663   childtokill= -child;
664
665   synchmsg= 'g';
666   r= write(synchsocket[0],&synchmsg,1);
667   if (r!=1) syscallerror("write synch byte to child");
668
669   if (close(synchsocket[0])) syscallerror("cannot close my end of synch socket");
670 }
671
672 void servicerequest(int sfd) {
673   struct event_msg event_mbuf;
674   int r;
675
676   setup_comms(sfd);
677   send_opening();
678   receive_request();
679   establish_pipes();
680   lookup_uidsgids();
681   debug_dumprequest(mypid);
682
683   if (overridedata)
684     r= parse_string(TOPLEVEL_OVERRIDDEN_CONFIGURATION,
685                     "<builtin toplevel override configuration>",1);
686   else
687     r= parse_string(TOPLEVEL_CONFIGURATION,
688                     "<builtin toplevel configuration>",1);
689   
690   ensurelogopen(USERVD_LOGFACILITY);
691   if (r == tokv_error) failure("error encountered while parsing configuration");
692   assert(r == tokv_quit);
693
694   debug_dumpexecsettings();
695
696   check_find_executable();
697   check_fds();
698   send_progress_ok();
699
700   getevent(&event_mbuf);
701   assert(event_mbuf.type == et_confirm);
702
703   fork_service_synch();
704   
705   getevent(&event_mbuf);
706   abort();
707 }