xref: /freebsd/usr.sbin/ppp/chat.c (revision 7f3dea244c40159a41ab22da77a434d7c5b5e85a)
1 /*-
2  * Copyright (c) 1998 Brian Somers <brian@Awfulhak.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *	$Id: chat.c,v 1.59 1999/06/26 02:54:36 brian Exp $
27  */
28 
29 #include <sys/param.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <sys/un.h>
34 
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <paths.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <sys/wait.h>
42 #include <termios.h>
43 #include <unistd.h>
44 
45 #include "layer.h"
46 #include "mbuf.h"
47 #include "log.h"
48 #include "defs.h"
49 #include "timer.h"
50 #include "lqr.h"
51 #include "hdlc.h"
52 #include "throughput.h"
53 #include "fsm.h"
54 #include "lcp.h"
55 #include "ccp.h"
56 #include "link.h"
57 #include "async.h"
58 #include "descriptor.h"
59 #include "physical.h"
60 #include "chat.h"
61 #include "mp.h"
62 #include "auth.h"
63 #include "chap.h"
64 #include "slcompress.h"
65 #include "iplist.h"
66 #include "ipcp.h"
67 #include "filter.h"
68 #include "cbcp.h"
69 #include "command.h"
70 #include "datalink.h"
71 #ifndef NORADIUS
72 #include "radius.h"
73 #endif
74 #include "bundle.h"
75 
76 #define BUFLEFT(c) (sizeof (c)->buf - ((c)->bufend - (c)->buf))
77 
78 static void ExecStr(struct physical *, char *, char *, int);
79 static char *ExpandString(struct chat *, const char *, char *, int, int);
80 
81 static void
82 chat_PauseTimer(void *v)
83 {
84   struct chat *c = (struct chat *)v;
85   timer_Stop(&c->pause);
86   c->pause.load = 0;
87 }
88 
89 static void
90 chat_Pause(struct chat *c, u_long load)
91 {
92   timer_Stop(&c->pause);
93   c->pause.load += load;
94   c->pause.func = chat_PauseTimer;
95   c->pause.name = "chat pause";
96   c->pause.arg = c;
97   timer_Start(&c->pause);
98 }
99 
100 static void
101 chat_TimeoutTimer(void *v)
102 {
103   struct chat *c = (struct chat *)v;
104   timer_Stop(&c->timeout);
105   c->TimedOut = 1;
106 }
107 
108 static void
109 chat_SetTimeout(struct chat *c)
110 {
111   timer_Stop(&c->timeout);
112   if (c->TimeoutSec > 0) {
113     c->timeout.load = SECTICKS * c->TimeoutSec;
114     c->timeout.func = chat_TimeoutTimer;
115     c->timeout.name = "chat timeout";
116     c->timeout.arg = c;
117     timer_Start(&c->timeout);
118   }
119 }
120 
121 static char *
122 chat_NextChar(char *ptr, char ch)
123 {
124   for (; *ptr; ptr++)
125     if (*ptr == ch)
126       return ptr;
127     else if (*ptr == '\\')
128       if (*++ptr == '\0')
129         return NULL;
130 
131   return NULL;
132 }
133 
134 static int
135 chat_UpdateSet(struct descriptor *d, fd_set *r, fd_set *w, fd_set *e, int *n)
136 {
137   struct chat *c = descriptor2chat(d);
138   int special, gotabort, gottimeout, needcr;
139   int TimedOut = c->TimedOut;
140   static char arg_term;		/* An empty string */
141 
142   if (c->pause.state == TIMER_RUNNING)
143     return 0;
144 
145   if (TimedOut) {
146     log_Printf(LogCHAT, "Expect timeout\n");
147     if (c->nargptr == NULL)
148       c->state = CHAT_FAILED;
149     else {
150       /* c->state = CHAT_EXPECT; */
151       c->argptr = &arg_term;
152     }
153     c->TimedOut = 0;
154   }
155 
156   if (c->state != CHAT_EXPECT && c->state != CHAT_SEND)
157     return 0;
158 
159   gottimeout = gotabort = 0;
160 
161   if (c->arg < c->argc && (c->arg < 0 || *c->argptr == '\0')) {
162     /* Go get the next string */
163     if (c->arg < 0 || c->state == CHAT_SEND)
164       c->state = CHAT_EXPECT;
165     else
166       c->state = CHAT_SEND;
167 
168     special = 1;
169     while (special && (c->nargptr || c->arg < c->argc - 1)) {
170       if (c->arg < 0 || (!TimedOut && c->state == CHAT_SEND))
171         c->nargptr = NULL;
172 
173       if (c->nargptr != NULL) {
174         /* We're doing expect-send-expect.... */
175         c->argptr = c->nargptr;
176         /* Put the '-' back in case we ever want to rerun our script */
177         c->nargptr[-1] = '-';
178         c->nargptr = chat_NextChar(c->nargptr, '-');
179         if (c->nargptr != NULL)
180           *c->nargptr++ = '\0';
181       } else {
182         int minus;
183 
184         if ((c->argptr = c->argv[++c->arg]) == NULL) {
185           /* End of script - all ok */
186           c->state = CHAT_DONE;
187           return 0;
188         }
189 
190         if (c->state == CHAT_EXPECT) {
191           /* Look for expect-send-expect sequence */
192           c->nargptr = c->argptr;
193           minus = 0;
194           while ((c->nargptr = chat_NextChar(c->nargptr, '-'))) {
195             c->nargptr++;
196             minus++;
197           }
198 
199           if (minus % 2)
200             log_Printf(LogWARN, "chat_UpdateSet: \"%s\": Uneven number of"
201                       " '-' chars, all ignored\n", c->argptr);
202           else if (minus) {
203             c->nargptr = chat_NextChar(c->argptr, '-');
204             *c->nargptr++ = '\0';
205           }
206         }
207       }
208 
209       /*
210        * c->argptr now temporarily points into c->script (via c->argv)
211        * If it's an expect-send-expect sequence, we've just got the correct
212        * portion of that sequence.
213        */
214 
215       needcr = c->state == CHAT_SEND && *c->argptr != '!';
216 
217       /* We leave room for a potential HDLC header in the target string */
218       ExpandString(c, c->argptr, c->exp + 2, sizeof c->exp - 2, needcr);
219 
220       /*
221        * Now read our string.  If it's not a special string, we unset
222        * ``special'' to break out of the loop.
223        */
224       if (gotabort) {
225         if (c->abort.num < MAXABORTS) {
226           int len, i;
227 
228           len = strlen(c->exp+2);
229           for (i = 0; i < c->abort.num; i++)
230             if (len > c->abort.string[i].len) {
231               int last;
232 
233               for (last = c->abort.num; last > i; last--) {
234                 c->abort.string[last].data = c->abort.string[last-1].data;
235                 c->abort.string[last].len = c->abort.string[last-1].len;
236               }
237               break;
238             }
239           c->abort.string[i].len = len;
240           c->abort.string[i].data = (char *)malloc(len+1);
241           memcpy(c->abort.string[i].data, c->exp+2, len+1);
242           c->abort.num++;
243         } else
244           log_Printf(LogERROR, "chat_UpdateSet: too many abort strings\n");
245         gotabort = 0;
246       } else if (gottimeout) {
247         c->TimeoutSec = atoi(c->exp + 2);
248         if (c->TimeoutSec <= 0)
249           c->TimeoutSec = 30;
250         gottimeout = 0;
251       } else if (c->nargptr == NULL && !strcmp(c->exp+2, "ABORT"))
252         gotabort = 1;
253       else if (c->nargptr == NULL && !strcmp(c->exp+2, "TIMEOUT"))
254         gottimeout = 1;
255       else {
256         if (c->exp[2] == '!')
257           ExecStr(c->physical, c->exp + 3, c->exp + 2, sizeof c->exp - 2);
258 
259         if (c->exp[2] == '\0') {
260           /* Empty string, reparse (this may be better as a `goto start') */
261           c->argptr = &arg_term;
262           return chat_UpdateSet(d, r, w, e, n);
263         }
264 
265         special = 0;
266       }
267     }
268 
269     if (special) {
270       if (gottimeout)
271         log_Printf(LogWARN, "chat_UpdateSet: TIMEOUT: Argument expected\n");
272       else if (gotabort)
273         log_Printf(LogWARN, "chat_UpdateSet: ABORT: Argument expected\n");
274 
275       /* End of script - all ok */
276       c->state = CHAT_DONE;
277       return 0;
278     }
279 
280     /* set c->argptr to point in the right place */
281     c->argptr = c->exp + 2;
282     c->arglen = strlen(c->argptr);
283 
284     if (c->state == CHAT_EXPECT) {
285       /* We must check to see if the string's already been found ! */
286       char *begin, *end;
287 
288       end = c->bufend - c->arglen + 1;
289       if (end < c->bufstart)
290         end = c->bufstart;
291       for (begin = c->bufstart; begin < end; begin++)
292         if (!strncmp(begin, c->argptr, c->arglen)) {
293           c->bufstart = begin + c->arglen;
294           c->argptr += c->arglen;
295           c->arglen = 0;
296           /* Continue - we've already read our expect string */
297           return chat_UpdateSet(d, r, w, e, n);
298         }
299 
300       log_Printf(LogCHAT, "Expect(%d): %s\n", c->TimeoutSec, c->argptr);
301       chat_SetTimeout(c);
302     }
303   }
304 
305   /*
306    * We now have c->argptr pointing at what we want to expect/send and
307    * c->state saying what we want to do... we now know what to put in
308    * the fd_set :-)
309    */
310 
311   if (c->state == CHAT_EXPECT)
312     return physical_doUpdateSet(&c->physical->desc, r, NULL, e, n, 1);
313   else
314     return physical_doUpdateSet(&c->physical->desc, NULL, w, e, n, 1);
315 }
316 
317 static int
318 chat_IsSet(struct descriptor *d, const fd_set *fdset)
319 {
320   struct chat *c = descriptor2chat(d);
321   return c->argptr && physical_IsSet(&c->physical->desc, fdset);
322 }
323 
324 static void
325 chat_UpdateLog(struct chat *c, int in)
326 {
327   if (log_IsKept(LogCHAT) || log_IsKept(LogCONNECT)) {
328     /*
329      * If a linefeed appears in the last `in' characters of `c's input
330      * buffer, output from there, all the way back to the last linefeed.
331      * This is called for every read of `in' bytes.
332      */
333     char *ptr, *end, *stop, ch;
334     int level;
335 
336     level = log_IsKept(LogCHAT) ? LogCHAT : LogCONNECT;
337     if (in == -1)
338       end = ptr = c->bufend;
339     else {
340       ptr = c->bufend - in;
341       for (end = c->bufend - 1; end >= ptr; end--)
342         if (*end == '\n')
343           break;
344     }
345 
346     if (end >= ptr) {
347       for (ptr = c->bufend - (in == -1 ? 1 : in + 1); ptr >= c->bufstart; ptr--)
348         if (*ptr == '\n')
349           break;
350       ptr++;
351       stop = NULL;
352       while (stop < end) {
353         if ((stop = memchr(ptr, '\n', end - ptr)) == NULL)
354           stop = end;
355         ch = *stop;
356         *stop = '\0';
357         if (level == LogCHAT || strstr(ptr, "CONNECT"))
358           log_Printf(level, "Received: %s\n", ptr);
359         *stop = ch;
360         ptr = stop + 1;
361       }
362     }
363   }
364 }
365 
366 static void
367 chat_Read(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
368 {
369   struct chat *c = descriptor2chat(d);
370 
371   if (c->state == CHAT_EXPECT) {
372     ssize_t in;
373     char *abegin, *ebegin, *begin, *aend, *eend, *end;
374     int n;
375 
376     /*
377      * XXX - should this read only 1 byte to guarantee that we don't
378      * swallow any ppp talk from the peer ?
379      */
380     in = BUFLEFT(c);
381     if (in > sizeof c->buf / 2)
382       in = sizeof c->buf / 2;
383 
384     in = physical_Read(c->physical, c->bufend, in);
385     if (in <= 0)
386       return;
387 
388     /* `begin' and `end' delimit where we're going to strncmp() from */
389     ebegin = c->bufend - c->arglen + 1;
390     eend = ebegin + in;
391     if (ebegin < c->bufstart)
392       ebegin = c->bufstart;
393 
394     if (c->abort.num) {
395       abegin = c->bufend - c->abort.string[0].len + 1;
396       aend = c->bufend - c->abort.string[c->abort.num-1].len + in + 1;
397       if (abegin < c->bufstart)
398         abegin = c->bufstart;
399     } else {
400       abegin = ebegin;
401       aend = eend;
402     }
403     begin = abegin < ebegin ? abegin : ebegin;
404     end = aend < eend ? eend : aend;
405 
406     c->bufend += in;
407 
408     chat_UpdateLog(c, in);
409 
410     if (c->bufend > c->buf + sizeof c->buf / 2) {
411       /* Shuffle our receive buffer back a bit */
412       int chop;
413 
414       for (chop = begin - c->buf; chop; chop--)
415         if (c->buf[chop] == '\n')
416           /* found some already-logged garbage to remove :-) */
417           break;
418 
419       if (!chop)
420         chop = begin - c->buf;
421 
422       if (chop) {
423         char *from, *to;
424 
425         to = c->buf;
426         from = to + chop;
427         while (from < c->bufend)
428           *to++ = *from++;
429         c->bufstart -= chop;
430         c->bufend -= chop;
431         begin -= chop;
432         end -= chop;
433         abegin -= chop;
434         aend -= chop;
435         ebegin -= chop;
436         eend -= chop;
437       }
438     }
439 
440     for (; begin < end; begin++)
441       if (begin >= ebegin && begin < eend &&
442           !strncmp(begin, c->argptr, c->arglen)) {
443         /* Got it ! */
444         timer_Stop(&c->timeout);
445         if (memchr(begin + c->arglen - 1, '\n',
446             c->bufend - begin - c->arglen + 1) == NULL) {
447           /* force it into the log */
448           end = c->bufend;
449           c->bufend = begin + c->arglen;
450           chat_UpdateLog(c, -1);
451           c->bufend = end;
452         }
453         c->bufstart = begin + c->arglen;
454         c->argptr += c->arglen;
455         c->arglen = 0;
456         break;
457       } else if (begin >= abegin && begin < aend) {
458         for (n = c->abort.num - 1; n >= 0; n--) {
459           if (begin + c->abort.string[n].len > c->bufend)
460             break;
461           if (!strncmp(begin, c->abort.string[n].data,
462                        c->abort.string[n].len)) {
463             if (memchr(begin + c->abort.string[n].len - 1, '\n',
464                 c->bufend - begin - c->abort.string[n].len + 1) == NULL) {
465               /* force it into the log */
466               end = c->bufend;
467               c->bufend = begin + c->abort.string[n].len;
468               chat_UpdateLog(c, -1);
469               c->bufend = end;
470             }
471             c->bufstart = begin + c->abort.string[n].len;
472             c->state = CHAT_FAILED;
473             return;
474           }
475         }
476       }
477   }
478 }
479 
480 static int
481 chat_Write(struct descriptor *d, struct bundle *bundle, const fd_set *fdset)
482 {
483   struct chat *c = descriptor2chat(d);
484   int result = 0;
485 
486   if (c->state == CHAT_SEND) {
487     int wrote;
488 
489     if (strstr(c->argv[c->arg], "\\P"))            /* Don't log the password */
490       log_Printf(LogCHAT, "Send: %s\n", c->argv[c->arg]);
491     else {
492       int sz;
493 
494       sz = c->arglen - 1;
495       while (sz >= 0 && c->argptr[sz] == '\n')
496         sz--;
497       log_Printf(LogCHAT, "Send: %.*s\n", sz + 1, c->argptr);
498     }
499 
500     if (physical_IsSync(c->physical)) {
501       /*
502        * XXX: Fix me
503        * This data should be stuffed down through the link layers
504        */
505       /* There's always room for the HDLC header */
506       c->argptr -= 2;
507       c->arglen += 2;
508       memcpy(c->argptr, "\377\003", 2);	/* Prepend HDLC header */
509     }
510 
511     wrote = physical_Write(c->physical, c->argptr, c->arglen);
512     result = wrote ? 1 : 0;
513     if (wrote == -1) {
514       if (errno != EINTR)
515         log_Printf(LogERROR, "chat_Write: %s\n", strerror(errno));
516       if (physical_IsSync(c->physical)) {
517         c->argptr += 2;
518         c->arglen -= 2;
519       }
520     } else if (wrote < 2 && physical_IsSync(c->physical)) {
521       /* Oops - didn't even write our HDLC header ! */
522       c->argptr += 2;
523       c->arglen -= 2;
524     } else {
525       c->argptr += wrote;
526       c->arglen -= wrote;
527     }
528   }
529 
530   return result;
531 }
532 
533 void
534 chat_Init(struct chat *c, struct physical *p, const char *data, int emptybuf,
535           const char *phone)
536 {
537   c->desc.type = CHAT_DESCRIPTOR;
538   c->desc.UpdateSet = chat_UpdateSet;
539   c->desc.IsSet = chat_IsSet;
540   c->desc.Read = chat_Read;
541   c->desc.Write = chat_Write;
542   c->physical = p;
543 
544   c->state = CHAT_EXPECT;
545 
546   if (data == NULL) {
547     *c->script = '\0';
548     c->argc = 0;
549   } else {
550     strncpy(c->script, data, sizeof c->script - 1);
551     c->script[sizeof c->script - 1] = '\0';
552     c->argc =  MakeArgs(c->script, c->argv, VECSIZE(c->argv));
553   }
554 
555   c->arg = -1;
556   c->argptr = NULL;
557   c->nargptr = NULL;
558 
559   if (emptybuf)
560     c->bufstart = c->bufend = c->buf;
561 
562   c->TimeoutSec = 30;
563   c->TimedOut = 0;
564   c->phone = phone;
565   c->abort.num = 0;
566 
567   memset(&c->pause, '\0', sizeof c->pause);
568   memset(&c->timeout, '\0', sizeof c->timeout);
569 }
570 
571 void
572 chat_Destroy(struct chat *c)
573 {
574   timer_Stop(&c->pause);
575   timer_Stop(&c->timeout);
576   while (c->abort.num)
577     free(c->abort.string[--c->abort.num].data);
578   c->abort.num = 0;
579 }
580 
581 /*
582  *  \c	don't add a cr
583  *  \d  Sleep a little (delay 2 seconds
584  *  \n  Line feed character
585  *  \P  Auth Key password
586  *  \p  pause 0.25 sec
587  *  \r	Carrige return character
588  *  \s  Space character
589  *  \T  Telephone number(s) (defined via `set phone')
590  *  \t  Tab character
591  *  \U  Auth User
592  */
593 static char *
594 ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr)
595 {
596   int len;
597 
598   result[--reslen] = '\0';
599   while (*str && reslen > 0) {
600     switch (*str) {
601     case '\\':
602       str++;
603       switch (*str) {
604       case 'c':
605 	cr = 0;
606 	break;
607       case 'd':		/* Delay 2 seconds */
608         chat_Pause(c, 2 * SECTICKS);
609 	break;
610       case 'p':
611         chat_Pause(c, SECTICKS / 4);
612 	break;		/* Delay 0.25 seconds */
613       case 'n':
614 	*result++ = '\n';
615 	reslen--;
616 	break;
617       case 'r':
618 	*result++ = '\r';
619 	reslen--;
620 	break;
621       case 's':
622 	*result++ = ' ';
623 	reslen--;
624 	break;
625       case 't':
626 	*result++ = '\t';
627 	reslen--;
628 	break;
629       case 'P':
630 	strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
631         len = strlen(result);
632 	reslen -= len;
633 	result += len;
634 	break;
635       case 'T':
636         if (c->phone) {
637           strncpy(result, c->phone, reslen);
638           len = strlen(result);
639           reslen -= len;
640           result += len;
641         }
642 	break;
643       case 'U':
644 	strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
645         len = strlen(result);
646 	reslen -= len;
647 	result += len;
648 	break;
649       default:
650 	reslen--;
651 	*result++ = *str;
652 	break;
653       }
654       if (*str)
655 	str++;
656       break;
657     case '^':
658       str++;
659       if (*str) {
660 	*result++ = *str++ & 0x1f;
661 	reslen--;
662       }
663       break;
664     default:
665       *result++ = *str++;
666       reslen--;
667       break;
668     }
669   }
670   if (--reslen > 0) {
671     if (cr)
672       *result++ = '\r';
673   }
674   if (--reslen > 0)
675     *result++ = '\0';
676   return (result);
677 }
678 
679 static void
680 ExecStr(struct physical *physical, char *command, char *out, int olen)
681 {
682   pid_t pid;
683   int fids[2];
684   char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
685   int stat, nb, argc, i;
686 
687   log_Printf(LogCHAT, "Exec: %s\n", command);
688   argc = MakeArgs(command, vector, VECSIZE(vector));
689   command_Expand(argv, argc, (char const *const *)vector,
690                  physical->dl->bundle, 0, getpid());
691 
692   if (pipe(fids) < 0) {
693     log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
694 	      strerror(errno));
695     *out = '\0';
696     return;
697   }
698   if ((pid = fork()) == 0) {
699     close(fids[0]);
700     timer_TermService();
701     if (fids[1] == STDIN_FILENO)
702       fids[1] = dup(fids[1]);
703     dup2(physical->fd, STDIN_FILENO);
704     dup2(fids[1], STDERR_FILENO);
705     dup2(STDIN_FILENO, STDOUT_FILENO);
706     close(3);
707     if (open(_PATH_TTY, O_RDWR) != 3)
708       open(_PATH_DEVNULL, O_RDWR);	/* Leave it closed if it fails... */
709     for (i = getdtablesize(); i > 3; i--)
710       fcntl(i, F_SETFD, 1);
711     setuid(geteuid());
712     execvp(argv[0], argv);
713     fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
714     _exit(127);
715   } else {
716     char *name = strdup(vector[0]);
717 
718     close(fids[1]);
719     endout = out + olen - 1;
720     startout = out;
721     while (out < endout) {
722       nb = read(fids[0], out, 1);
723       if (nb <= 0)
724 	break;
725       out++;
726     }
727     *out = '\0';
728     close(fids[0]);
729     close(fids[1]);
730     waitpid(pid, &stat, WNOHANG);
731     if (WIFSIGNALED(stat)) {
732       log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
733       free(name);
734       *out = '\0';
735       return;
736     } else if (WIFEXITED(stat)) {
737       switch (WEXITSTATUS(stat)) {
738         case 0:
739           free(name);
740           break;
741         case 127:
742           log_Printf(LogWARN, "%s: %s\n", name, startout);
743           free(name);
744           *out = '\0';
745           return;
746           break;
747         default:
748           log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
749           free(name);
750           *out = '\0';
751           return;
752           break;
753       }
754     } else {
755       log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
756       free(name);
757       *out = '\0';
758       return;
759     }
760   }
761 }
762