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