/* If daemonization is configured, then starts daemonization, by forking and * returning in the child process. The parent process hangs around until the * child lets it know either that it completed startup successfully (by calling * daemon_complete()) or that it failed to start up (by exiting with a nonzero * exit code). */ void daemonize_start(void) { daemonize_fd = -1; if (detach) { if (fork_and_wait_for_startup(&daemonize_fd) > 0) { /* Running in parent process. */ exit(0); } /* Running in daemon or monitor process. */ } if (monitor) { int saved_daemonize_fd = daemonize_fd; pid_t daemon_pid; daemon_pid = fork_and_wait_for_startup(&daemonize_fd); if (daemon_pid > 0) { /* Running in monitor process. */ fork_notify_startup(saved_daemonize_fd); close_standard_fds(); monitor_daemon(daemon_pid); } /* Running in daemon process. */ } make_pidfile(); /* Make sure that the unixctl commands for vlog get registered in a * daemon, even before the first log message. */ vlog_init(); }
/* If daemonization is configured, then this function does traditional Unix * daemonization behavior: join a new session, chdir to the root (if not * disabled), and close the standard file descriptors. * * It only makes sense to call this function as part of an implementation of a * special daemon subprocess. A normal daemon should just call * daemonize_complete(). */ static void daemonize_post_detach(void) { if (detach) { if (chdir_) { ignore(chdir("/")); } close_standard_fds(); } }
/* If daemonization is configured, then starts daemonization, by forking and * returning in the child process. The parent process hangs around until the * child lets it know either that it completed startup successfully (by calling * daemon_complete()) or that it failed to start up (by exiting with a nonzero * exit code). */ void daemonize_start(bool access_datapath) { assert_single_threaded(); daemonize_fd = -1; if (switch_user) { daemon_become_new_user__(access_datapath); switch_user = false; } if (detach) { pid_t pid; if (fork_and_wait_for_startup(&daemonize_fd, &pid)) { VLOG_FATAL("could not detach from foreground session"); } if (pid > 0) { /* Running in parent process. */ exit(0); } /* Running in daemon or monitor process. */ setsid(); } if (monitor) { int saved_daemonize_fd = daemonize_fd; pid_t daemon_pid; if (fork_and_wait_for_startup(&daemonize_fd, &daemon_pid)) { VLOG_FATAL("could not initiate process monitoring"); } if (daemon_pid > 0) { /* Running in monitor process. */ fork_notify_startup(saved_daemonize_fd); if (detach) { close_standard_fds(); } monitor_daemon(daemon_pid); } /* Running in daemon process. */ } forbid_forking("running in daemon process"); if (pidfile) { make_pidfile(); } /* Make sure that the unixctl commands for vlog get registered in a * daemon, even before the first log message. */ vlog_init(); }
/* If daemonization is configured, then this function notifies the parent * process that the child process has completed startup successfully. */ void daemonize_complete(void) { fork_notify_startup(daemonize_fd); if (detach) { setsid(); if (chdir_) { ignore(chdir("/")); } close_standard_fds(); } }
void daemonize_complete(void) { /* If running as a child because '--detach' option was specified, * communicate with the parent to inform that the child is ready. */ if (detached) { int error; close_standard_fds(); error = WriteFile(write_handle, "a", 1, NULL, NULL); if (!error) { VLOG_FATAL("Failed to communicate with the parent (%s)", ovs_lasterror_to_string()); } } service_complete(); }
int main(int argc, char *argv[]) { /* Check to see if the user is running us as root, which is a nono */ if (!geteuid()) { fprintf(stderr, "ERROR: This server won't run as root/superuser\n"); return -1; } /* Setup corefile size immediately after boot -kre */ setup_corefile(); /* Save server boot time right away, so getrusage works correctly */ set_time(); /* It's not random, but it ought to be a little harder to guess */ init_genrand(SystemTime.tv_sec ^ (SystemTime.tv_usec | (getpid() << 20))); dlinkAdd(&me, &me.node, &global_client_list); ConfigGeneral.dpath = DPATH; ConfigGeneral.spath = SPATH; ConfigGeneral.mpath = MPATH; ConfigGeneral.configfile = CPATH; /* Server configuration file */ ConfigGeneral.klinefile = KPATH; /* Server kline file */ ConfigGeneral.glinefile = GPATH; /* Server gline file */ ConfigGeneral.xlinefile = XPATH; /* Server xline file */ ConfigGeneral.dlinefile = DLPATH; /* dline file */ ConfigGeneral.resvfile = RESVPATH; /* resv file */ myargv = argv; umask(077); /* umask 077: u=rwx,g=,o= */ parseargs(&argc, &argv, myopts); if (printVersion) { printf("ircd: version %s(%s)\n", ircd_version, serno); exit(EXIT_SUCCESS); } if (chdir(ConfigGeneral.dpath)) { perror("chdir"); exit(EXIT_FAILURE); } ssl_init(); if (!server_state.foreground) { make_daemon(); close_standard_fds(); /* this needs to be before init_netio()! */ } else print_startup(getpid()); setup_signals(); /* We need this to initialise the fd array before anything else */ fdlist_init(); log_set_file(LOG_TYPE_IRCD, 0, logFileName); init_netio(); /* This needs to be setup early ! -- adrian */ /* Check if there is pidfile and daemon already running */ check_pidfile(pidFileName); mp_pool_init(); init_dlink_nodes(); init_isupport(); dbuf_init(); hash_init(); ipcache_init(); client_init(); class_init(); whowas_init(); watch_init(); auth_init(); /* Initialise the auth code */ init_resolver(); /* Needs to be setup before the io loop */ modules_init(); read_conf_files(1); /* cold start init conf files */ init_uid(); initialize_server_capabs(); /* Set up default_server_capabs */ initialize_global_set_options(); /* Has to be called after read_conf_files() */ channel_init(); read_links_file(); motd_init(); user_usermodes_init(); #ifdef HAVE_LIBGEOIP geoip_ctx = GeoIP_new(GEOIP_MEMORY_CACHE); #endif if (EmptyString(ConfigServerInfo.sid)) { ilog(LOG_TYPE_IRCD, "ERROR: No server id specified in serverinfo block."); exit(EXIT_FAILURE); } strlcpy(me.id, ConfigServerInfo.sid, sizeof(me.id)); if (EmptyString(ConfigServerInfo.name)) { ilog(LOG_TYPE_IRCD, "ERROR: No server name specified in serverinfo block."); exit(EXIT_FAILURE); } strlcpy(me.name, ConfigServerInfo.name, sizeof(me.name)); /* serverinfo{} description must exist. If not, error out.*/ if (EmptyString(ConfigServerInfo.description)) { ilog(LOG_TYPE_IRCD, "ERROR: No server description specified in serverinfo block."); exit(EXIT_FAILURE); } strlcpy(me.info, ConfigServerInfo.description, sizeof(me.info)); me.from = &me; me.servptr = &me; me.connection->lasttime = CurrentTime; me.connection->since = CurrentTime; me.connection->firsttime = CurrentTime; SetMe(&me); make_server(&me); hash_add_id(&me); hash_add_client(&me); dlinkAdd(&me, make_dlink_node(), &global_server_list); load_kline_database(); load_dline_database(); load_gline_database(); load_xline_database(); load_resv_database(); load_all_modules(1); load_conf_modules(); load_core_modules(1); write_pidfile(pidFileName); ilog(LOG_TYPE_IRCD, "Server Ready"); event_addish(&event_cleanup_glines, NULL); event_addish(&event_cleanup_tklines, NULL); /* We want try_connections to be called as soon as possible now! -- adrian */ /* No, 'cause after a restart it would cause all sorts of nick collides */ event_addish(&event_try_connections, NULL); /* Setup the timeout check. I'll shift it later :) -- adrian */ event_add(&event_comm_checktimeouts, NULL); event_addish(&event_save_all_databases, NULL); if (ConfigServerHide.links_delay > 0) { event_write_links_file.when = ConfigServerHide.links_delay; event_addish(&event_write_links_file, NULL); } else ConfigServerHide.links_disabled = 1; if (splitmode) event_addish(&splitmode_event, NULL); io_loop(); return 0; }