| 588 | |
| 589 | |
| 590 | static void destroy_subd(struct subd *sd) |
| 591 | { |
| 592 | int status; |
| 593 | bool fail_if_subd_fails; |
| 594 | |
| 595 | fail_if_subd_fails = IFDEV(sd->ld->dev_subdaemon_fail, false); |
| 596 | list_del_from(&sd->ld->subds, &sd->list); |
| 597 | |
| 598 | /* lightningd may have already done waitpid() */ |
| 599 | if (sd->wstatus != NULL) { |
| 600 | status = *sd->wstatus; |
| 601 | } else { |
| 602 | switch (waitpid(sd->pid, &status, WNOHANG)) { |
| 603 | case 0: |
| 604 | /* If it's an essential daemon, don't kill: we want the |
| 605 | * exit status */ |
| 606 | if (!sd->must_not_exit) { |
| 607 | log_debug(sd->log, |
| 608 | "Status closed, but not exited. Killing"); |
| 609 | kill(sd->pid, SIGKILL); |
| 610 | } |
| 611 | waitpid(sd->pid, &status, 0); |
| 612 | fail_if_subd_fails = false; |
| 613 | break; |
| 614 | case -1: |
| 615 | log_broken(sd->log, "Status closed, but waitpid %i says %s", |
| 616 | sd->pid, strerror(errno)); |
| 617 | status = -1; |
| 618 | break; |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | if (fail_if_subd_fails && WIFSIGNALED(status)) { |
| 623 | log_broken(sd->log, "Subdaemon %s killed with signal %i", |
| 624 | sd->name, WTERMSIG(status)); |
| 625 | exit(1); |
| 626 | } |
| 627 | |
| 628 | /* In case we're freed manually, such as channel_fail_permanent */ |
| 629 | if (sd->conn) |
| 630 | sd->conn = tal_free(sd->conn); |
| 631 | |
| 632 | /* Peer still attached? */ |
| 633 | if (sd->channel) { |
| 634 | /* Don't loop back when we fail it. */ |
| 635 | void *channel = sd->channel; |
| 636 | struct db *db = sd->ld->wallet->db; |
| 637 | bool outer_transaction; |
| 638 | |
| 639 | /* Clear any transient messages in billboard */ |
| 640 | sd->billboardcb(channel, false, NULL); |
| 641 | sd->channel = NULL; |
| 642 | |
| 643 | /* We can be freed both inside msg handling, or spontaneously. */ |
| 644 | outer_transaction = db_in_transaction(db); |
| 645 | if (!outer_transaction) |
| 646 | db_begin_transaction(db); |
| 647 | if (sd->errcb) |
no test coverage detected