| 1164 | } |
| 1165 | |
| 1166 | static char *opt_start_daemon(struct lightningd *ld) |
| 1167 | { |
| 1168 | int fds[2]; |
| 1169 | int exitcode, pid; |
| 1170 | |
| 1171 | /* Already a daemon? OK. */ |
| 1172 | if (ld->daemon_parent_fd != -1) |
| 1173 | return NULL; |
| 1174 | |
| 1175 | if (pipe(fds) != 0) |
| 1176 | err(1, "Creating pipe to talk to --daemon"); |
| 1177 | |
| 1178 | pid = fork(); |
| 1179 | if (pid == -1) |
| 1180 | err(1, "Fork failed for --daemon"); |
| 1181 | |
| 1182 | if (pid == 0) { |
| 1183 | /* Child returns, continues as normal. */ |
| 1184 | close(fds[0]); |
| 1185 | ld->daemon_parent_fd = fds[1]; |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | |
| 1189 | /* OK, we are the parent. We exit with status told to us by |
| 1190 | * child. */ |
| 1191 | close(fds[1]); |
| 1192 | if (read(fds[0], &exitcode, sizeof(exitcode)) == sizeof(exitcode)) |
| 1193 | _exit(exitcode); |
| 1194 | /* It died before writing exitcode (presumably 0), so we grab it */ |
| 1195 | waitpid(pid, &exitcode, 0); |
| 1196 | if (WIFEXITED(exitcode)) |
| 1197 | _exit(WEXITSTATUS(exitcode)); |
| 1198 | errx(1, "Died with signal %u", WTERMSIG(exitcode)); |
| 1199 | } |
| 1200 | |
| 1201 | static bool opt_show_msat(char *buf, size_t len, const struct amount_msat *msat) |
| 1202 | { |