MCPcopy Create free account
hub / github.com/ElementsProject/lightning / main

Function main

lightningd/lightningd.c:868–1292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

866}
867
868int main(int argc, char *argv[])
869{
870 struct lightningd *ld;
871 u32 min_blockheight, max_blockheight;
872 int connectd_gossipd_fd;
873 int stop_fd;
874 struct timers *timers;
875 const char *stop_response;
876 struct htlc_in_map *unconnected_htlcs_in;
877 struct ext_key *bip32_base;
878 int sigchld_rfd;
879 struct io_conn *sigchld_conn;
880 int exit_code = 0;
881 char **orig_argv;
882 bool try_reexec;
883
884 /*~ We fork out new processes very very often; every channel gets its
885 * own process, for example, and we have `hsmd` and `gossipd` and
886 * the plugins as well.
887 * Now, we also keep around several file descriptors (`fd`s), including
888 * file descriptors to communicate with `hsmd` which is a privileged
889 * process with access to private keys and is therefore very sensitive.
890 * Thus, we need to close all file descriptors other than what the
891 * forked-out new process should have ASAP.
892 *
893 * We do this by using the `ccan/closefrom` module, which implements
894 * an emulation for the `closefrom` syscall on BSD and Solaris.
895 * This emulation tries to use the fastest facility available on the
896 * system (`close_range` syscall on Linux 5.9+, snooping through
897 * `/proc/$PID/fd` on many OSs (but requires procps to be mounted),
898 * the actual `closefrom` call if available, etc.).
899 * As a fallback if none of those are available on the system, however,
900 * it just iterates over the theoretical range of possible file
901 * descriptors.
902 *
903 * On some systems, that theoretical range can be very high, up to
904 * `INT_MAX` in the worst case.
905 * If the `closefrom` emulation has to fall back to this loop, it
906 * can be very slow; fortunately, the emulation will also inform
907 * us of that via the `closefrom_may_be_slow` function, and also has
908 * `closefrom_limit` to limit the number of allowed file descriptors
909 * *IF AND ONLY IF* `closefrom_may_be_slow()` is true.
910 *
911 * On systems with a fast `closefrom` then `closefrom_limit` does
912 * nothing.
913 *
914 * Previously we always imposed a limit of 1024 file descriptors
915 * (because we used to always iterate up to limit instead of using
916 * some OS facility, because those were non-portable and needed
917 * code for each OS), until @whitslack went and made >1000 channels
918 * and hit the 1024 limit.
919 */
920 closefrom_limit(4096);
921
922 /*~ What happens in strange locales should stay there. */
923 setup_locale();
924
925 /*~ This sets up SIGCHLD to make sigchld_rfd readable. */

Callers

nothing calls this directly

Calls 15

setup_localeFunction · 0.85
setup_sig_handlersFunction · 0.85
new_lightningdFunction · 0.85
path_joinFunction · 0.85
path_cwdFunction · 0.85
find_daemon_dirFunction · 0.85
errxFunction · 0.85
default_featuresFunction · 0.85
pidfile_createFunction · 0.85
test_subdaemonsFunction · 0.85
init_txfilterFunction · 0.85
log_infoClass · 0.85

Tested by

no test coverage detected