/** * infinoted_startup_new: * @error: Location to store error information, if any. * * Creates parameters for starting an infinote daemon. This involves option * parsing, reading config files, reading or creating data for TLS * (private key and certificate). * * Returns: A new #InfinotedStartup. Free with infinoted_startup_free(). */ InfinotedStartup* infinoted_startup_new(int* argc, char*** argv, GError** error) { InfinotedStartup* startup; if(!inf_init(error)) return NULL; startup = g_slice_new(InfinotedStartup); startup->options = NULL; startup->log = NULL; startup->private_key = NULL; startup->certificates = NULL; startup->credentials = NULL; startup->sasl_context = NULL; if(infinoted_startup_load(startup, argc, argv, error) == FALSE) { infinoted_startup_free(startup); return NULL; } startup->keepalive.mask = INF_KEEPALIVE_ENABLED | INF_KEEPALIVE_TIME | INF_KEEPALIVE_INTERVAL; startup->keepalive.enabled = TRUE; startup->keepalive.time = 60; startup->keepalive.interval = 5; return startup; }
static gboolean infinoted_main(int argc, char* argv[], GError** error) { InfinotedStartup* startup; gboolean result; startup = infinoted_startup_new(&argc, &argv, error); if(startup == NULL) return FALSE; result = infinoted_main_run(startup, error); infinoted_startup_free(startup); return result; }
/* Takes ownership of startup */ static gboolean infinoted_main_run(InfinotedStartup* startup, GError** error) { InfinotedRun* run; InfinotedSignal* sig; #ifdef LIBINFINITY_HAVE_LIBDAEMON mode_t prev_umask; pid_t pid; int saved_errno; #endif /* infinoted_run_new() takes ownership of startup */ run = infinoted_run_new(startup, error); if(run == NULL) { infinoted_startup_free(startup); return FALSE; } #ifdef LIBINFINITY_HAVE_LIBDAEMON if(startup->options->daemonize) { prev_umask = umask(0777); if(daemon_retval_init() == -1) { infinoted_run_free(run); return FALSE; /* libdaemon already wrote an error message */ } pid = daemon_fork(); if(pid < 0) { /* Translators: fork as in "fork into the background" */ infinoted_util_set_errno_error(error, errno, _("Failed to fork")); infinoted_run_free(run); daemon_retval_done(); return FALSE; } else if(pid > 0) { infinoted_run_free(run); saved_errno = daemon_retval_wait(5); if(saved_errno == 0) { return TRUE; } if(saved_errno == -1) { infinoted_util_set_errno_error(error, errno, _("Failed to wait for daemonized child's return value")); return FALSE; } else { /* on -1, the child process would have subtracted one from * errno before passing it back to us. */ if(saved_errno < 0) ++saved_errno; infinoted_util_set_errno_error( error, saved_errno, _("Failed to create PID file")); return FALSE; } } else { infinoted_util_daemon_set_global_pid_file_proc(); if(daemon_pid_file_create() != 0) { infinoted_util_daemon_set_local_pid_file_proc(); if(daemon_pid_file_create() != 0) { if(daemon_pid_file_create() != 0) { saved_errno = errno; infinoted_util_set_errno_error( error, saved_errno, _("Failed to create PID file") ); if(saved_errno < 0) --saved_errno; daemon_retval_send(saved_errno); infinoted_run_free(run); return FALSE; } } } daemon_retval_send(0); } /* libdaemon sets the umask to either 0777 (< 0.14) or 0077 (>= 0.14). * We don't want either of that, to make sure the directory tree is * always readable by us and potentially by others (for example, a * webserver providing read access to the documents). Therefore, reset * the umask here to what it previously was, so the system administrator * can define the umask by setting it before launching infinoted. * See also http://gobby.0x539.de/trac/ticket/617. */ umask(prev_umask); } #endif sig = infinoted_signal_register(run); /* Now start the server. It can later be stopped by signals. */ infinoted_run_start(run); infinoted_signal_unregister(sig); #ifdef LIBINFINITY_HAVE_LIBDAEMON /* startup might be invalid at this point in case a config reload happened, * so use run->startup instead (which is revalidated by config reload). */ if(run->startup->options->daemonize) daemon_pid_file_remove(); #endif infinoted_run_free(run); return TRUE; }