chat.c revision 54914
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 * $FreeBSD: head/usr.sbin/ppp/chat.c 54914 1999-12-20 20:30:02Z brian $
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
78static void ExecStr(struct physical *, char *, char *, int);
79static char *ExpandString(struct chat *, const char *, char *, int, int);
80
81static void
82chat_PauseTimer(void *v)
83{
84  struct chat *c = (struct chat *)v;
85  timer_Stop(&c->pause);
86  c->pause.load = 0;
87}
88
89static void
90chat_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
100static void
101chat_TimeoutTimer(void *v)
102{
103  struct chat *c = (struct chat *)v;
104  timer_Stop(&c->timeout);
105  c->TimedOut = 1;
106}
107
108static void
109chat_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
121static char *
122chat_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
134static int
135chat_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
317static int
318chat_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
324static void
325chat_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
366static void
367chat_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
480static int
481chat_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
533void
534chat_Init(struct chat *c, struct physical *p)
535{
536  c->desc.type = CHAT_DESCRIPTOR;
537  c->desc.UpdateSet = chat_UpdateSet;
538  c->desc.IsSet = chat_IsSet;
539  c->desc.Read = chat_Read;
540  c->desc.Write = chat_Write;
541  c->physical = p;
542  *c->script = '\0';
543  c->argc = 0;
544  c->arg = -1;
545  c->argptr = NULL;
546  c->nargptr = NULL;
547  c->bufstart = c->bufend = c->buf;
548
549  memset(&c->pause, '\0', sizeof c->pause);
550  memset(&c->timeout, '\0', sizeof c->timeout);
551}
552
553int
554chat_Setup(struct chat *c, const char *data, const char *phone)
555{
556  c->state = CHAT_EXPECT;
557
558  if (data == NULL) {
559    *c->script = '\0';
560    c->argc = 0;
561  } else {
562    strncpy(c->script, data, sizeof c->script - 1);
563    c->script[sizeof c->script - 1] = '\0';
564    c->argc = MakeArgs(c->script, c->argv, VECSIZE(c->argv));
565  }
566
567  c->arg = -1;
568  c->argptr = NULL;
569  c->nargptr = NULL;
570
571  c->TimeoutSec = 30;
572  c->TimedOut = 0;
573  c->phone = phone;
574  c->abort.num = 0;
575
576  timer_Stop(&c->pause);
577  timer_Stop(&c->timeout);
578
579  return c->argc >= 0;
580}
581
582void
583chat_Finish(struct chat *c)
584{
585  timer_Stop(&c->pause);
586  timer_Stop(&c->timeout);
587  while (c->abort.num)
588    free(c->abort.string[--c->abort.num].data);
589  c->abort.num = 0;
590}
591
592void
593chat_Destroy(struct chat *c)
594{
595  chat_Finish(c);
596}
597
598/*
599 *  \c	don't add a cr
600 *  \d  Sleep a little (delay 2 seconds
601 *  \n  Line feed character
602 *  \P  Auth Key password
603 *  \p  pause 0.25 sec
604 *  \r	Carrige return character
605 *  \s  Space character
606 *  \T  Telephone number(s) (defined via `set phone')
607 *  \t  Tab character
608 *  \U  Auth User
609 */
610static char *
611ExpandString(struct chat *c, const char *str, char *result, int reslen, int cr)
612{
613  int len;
614
615  result[--reslen] = '\0';
616  while (*str && reslen > 0) {
617    switch (*str) {
618    case '\\':
619      str++;
620      switch (*str) {
621      case 'c':
622	cr = 0;
623	break;
624      case 'd':		/* Delay 2 seconds */
625        chat_Pause(c, 2 * SECTICKS);
626	break;
627      case 'p':
628        chat_Pause(c, SECTICKS / 4);
629	break;		/* Delay 0.25 seconds */
630      case 'n':
631	*result++ = '\n';
632	reslen--;
633	break;
634      case 'r':
635	*result++ = '\r';
636	reslen--;
637	break;
638      case 's':
639	*result++ = ' ';
640	reslen--;
641	break;
642      case 't':
643	*result++ = '\t';
644	reslen--;
645	break;
646      case 'P':
647	strncpy(result, c->physical->dl->bundle->cfg.auth.key, reslen);
648        len = strlen(result);
649	reslen -= len;
650	result += len;
651	break;
652      case 'T':
653        if (c->phone) {
654          strncpy(result, c->phone, reslen);
655          len = strlen(result);
656          reslen -= len;
657          result += len;
658        }
659	break;
660      case 'U':
661	strncpy(result, c->physical->dl->bundle->cfg.auth.name, reslen);
662        len = strlen(result);
663	reslen -= len;
664	result += len;
665	break;
666      default:
667	reslen--;
668	*result++ = *str;
669	break;
670      }
671      if (*str)
672	str++;
673      break;
674    case '^':
675      str++;
676      if (*str) {
677	*result++ = *str++ & 0x1f;
678	reslen--;
679      }
680      break;
681    default:
682      *result++ = *str++;
683      reslen--;
684      break;
685    }
686  }
687  if (--reslen > 0) {
688    if (cr)
689      *result++ = '\r';
690  }
691  if (--reslen > 0)
692    *result++ = '\0';
693  return (result);
694}
695
696static void
697ExecStr(struct physical *physical, char *command, char *out, int olen)
698{
699  pid_t pid;
700  int fids[2];
701  char *argv[MAXARGS], *vector[MAXARGS], *startout, *endout;
702  int stat, nb, argc, i;
703
704  log_Printf(LogCHAT, "Exec: %s\n", command);
705  if ((argc = MakeArgs(command, vector, VECSIZE(vector))) <= 0) {
706    if (argc < 0)
707      log_Printf(LogWARN, "Syntax error in exec command\n");
708    *out = '\0';
709    return;
710  }
711  command_Expand(argv, argc, (char const *const *)vector,
712                 physical->dl->bundle, 0, getpid());
713
714  if (pipe(fids) < 0) {
715    log_Printf(LogCHAT, "Unable to create pipe in ExecStr: %s\n",
716	      strerror(errno));
717    *out = '\0';
718    return;
719  }
720  if ((pid = fork()) == 0) {
721    close(fids[0]);
722    timer_TermService();
723    if (fids[1] == STDIN_FILENO)
724      fids[1] = dup(fids[1]);
725    dup2(physical->fd, STDIN_FILENO);
726    dup2(fids[1], STDERR_FILENO);
727    dup2(STDIN_FILENO, STDOUT_FILENO);
728    close(3);
729    if (open(_PATH_TTY, O_RDWR) != 3)
730      open(_PATH_DEVNULL, O_RDWR);	/* Leave it closed if it fails... */
731    for (i = getdtablesize(); i > 3; i--)
732      fcntl(i, F_SETFD, 1);
733    setuid(geteuid());
734    execvp(argv[0], argv);
735    fprintf(stderr, "execvp: %s: %s\n", argv[0], strerror(errno));
736    _exit(127);
737  } else {
738    char *name = strdup(vector[0]);
739
740    close(fids[1]);
741    endout = out + olen - 1;
742    startout = out;
743    while (out < endout) {
744      nb = read(fids[0], out, 1);
745      if (nb <= 0)
746	break;
747      out++;
748    }
749    *out = '\0';
750    close(fids[0]);
751    close(fids[1]);
752    waitpid(pid, &stat, WNOHANG);
753    if (WIFSIGNALED(stat)) {
754      log_Printf(LogWARN, "%s: signal %d\n", name, WTERMSIG(stat));
755      free(name);
756      *out = '\0';
757      return;
758    } else if (WIFEXITED(stat)) {
759      switch (WEXITSTATUS(stat)) {
760        case 0:
761          free(name);
762          break;
763        case 127:
764          log_Printf(LogWARN, "%s: %s\n", name, startout);
765          free(name);
766          *out = '\0';
767          return;
768          break;
769        default:
770          log_Printf(LogWARN, "%s: exit %d\n", name, WEXITSTATUS(stat));
771          free(name);
772          *out = '\0';
773          return;
774          break;
775      }
776    } else {
777      log_Printf(LogWARN, "%s: Unexpected exit result\n", name);
778      free(name);
779      *out = '\0';
780      return;
781    }
782  }
783}
784