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