| 415 | } |
| 416 | |
| 417 | static struct command_result *reap_child(struct router_child *child) |
| 418 | { |
| 419 | int child_status; |
| 420 | struct timerel time_delta; |
| 421 | const char *err; |
| 422 | enum jsonrpc_errcode ecode; |
| 423 | |
| 424 | waitpid(child->pid, &child_status, 0); |
| 425 | time_delta = timemono_between(time_mono(), child->start); |
| 426 | |
| 427 | /* log the time of computation */ |
| 428 | cmd_log(tmpctx, child->cmd, LOG_DBG, "get_routes %s %" PRIu64 " ms", |
| 429 | WEXITSTATUS(child_status) != 0 ? "failed after" : "completed in", |
| 430 | time_to_msec(time_delta)); |
| 431 | |
| 432 | if (WIFSIGNALED(child_status)) { |
| 433 | err = tal_fmt(tmpctx, "child died with signal %u", |
| 434 | WTERMSIG(child_status)); |
| 435 | goto fail_broken; |
| 436 | } |
| 437 | |
| 438 | /* This is how it indicates an error message */ |
| 439 | if (WEXITSTATUS(child_status) != 0 && child->reply_bytes) { |
| 440 | if (child->reply_bytes <= sizeof(ecode)) { |
| 441 | plugin_log(child->cmd->plugin, LOG_BROKEN, "Truncated child reply (%zu) bytes, exited %i", |
| 442 | child->reply_bytes, WEXITSTATUS(child_status)); |
| 443 | ecode = LIGHTNINGD; |
| 444 | err = "Truncated child result"; |
| 445 | } else { |
| 446 | memcpy(&ecode, child->reply_buf, sizeof(ecode)); |
| 447 | err = tal_strndup(child, |
| 448 | child->reply_buf + sizeof(ecode), |
| 449 | child->reply_bytes - sizeof(ecode)); |
| 450 | } |
| 451 | goto fail; |
| 452 | } |
| 453 | if (child->reply_bytes == 0) { |
| 454 | err = tal_fmt(child, "child produced no output (exited %i)?", |
| 455 | WEXITSTATUS(child_status)); |
| 456 | goto fail_broken; |
| 457 | } |
| 458 | |
| 459 | /* Frees child, since it's a child of cmd */ |
| 460 | return command_finish_rawstr(child->cmd, |
| 461 | child->reply_buf, child->reply_bytes); |
| 462 | |
| 463 | fail_broken: |
| 464 | plugin_log(child->cmd->plugin, LOG_BROKEN, "%s", err); |
| 465 | ecode = LIGHTNINGD; |
| 466 | fail: |
| 467 | assert(err); |
| 468 | /* Frees child, since it's a child of cmd */ |
| 469 | return command_fail(child->cmd, ecode, "%s", err); |
| 470 | } |
| 471 | |
| 472 | /* Last one out finalizes */ |
| 473 | static void log_closed(struct io_conn *conn, struct router_child *child) |
no test coverage detected