~ The normal advice for daemons is to move into the root directory, so you * don't prevent unmounting whatever filesystem you happen to start in. * * But we define every path relative to our (~/.lightning) data dir, so we * make sure we stay there. The rest of this is taken from ccan/daemonize, * which was based on W. Richard Stevens' advice in Programming in The Unix * Environment. */
| 683 | * Environment. |
| 684 | */ |
| 685 | static void complete_daemonize(struct lightningd *ld) |
| 686 | { |
| 687 | int ok_status = 0; |
| 688 | |
| 689 | /* Don't hold files open. */ |
| 690 | close(STDIN_FILENO); |
| 691 | close(STDOUT_FILENO); |
| 692 | close(STDERR_FILENO); |
| 693 | |
| 694 | /* Many routines write to stderr; that can cause chaos if used |
| 695 | * for something else, so set it here. */ |
| 696 | if (open("/dev/null", O_WRONLY) != 0) |
| 697 | fatal("Could not open /dev/null: %s", strerror(errno)); |
| 698 | if (dup2(0, STDERR_FILENO) != STDERR_FILENO) |
| 699 | fatal("Could not dup /dev/null for stderr: %s", strerror(errno)); |
| 700 | close(0); |
| 701 | |
| 702 | /* Session leader so ^C doesn't whack us. */ |
| 703 | if (setsid() == (pid_t)-1) |
| 704 | fatal("Could not setsid: %s", strerror(errno)); |
| 705 | |
| 706 | /* Discard our parent's old-fashioned umask prejudices. */ |
| 707 | ld->initial_umask = umask(0); |
| 708 | |
| 709 | /* OK, parent, you can exit(0) now. */ |
| 710 | write_all(ld->daemon_parent_fd, &ok_status, sizeof(ok_status)); |
| 711 | close(ld->daemon_parent_fd); |
| 712 | } |
| 713 | |
| 714 | /*~ It's pretty standard behaviour (especially for daemons) to create and |
| 715 | * file-lock a pidfile. This not only prevents accidentally running multiple |