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