inline void run_cleanup() { // only call this once, on exit // to make sure valgrind output is clean // http://xmlsoft.org/xmlmem.html xmlCleanupCharEncodingHandlers(); xmlCleanupEncodingAliases(); xmlCleanupGlobals(); xmlCleanupParser(); xmlCleanupThreads(); xmlCleanupInputCallbacks(); xmlCleanupOutputCallbacks(); xmlCleanupMemory(); #if defined(HAVE_CAIRO) // http://cairographics.org/manual/cairo-Error-handling.html#cairo-debug-reset-static-data cairo_debug_reset_static_data(); #endif // http://icu-project.org/apiref/icu4c/uclean_8h.html#a93f27d0ddc7c196a1da864763f2d8920 u_cleanup(); #ifdef MAPNIK_USE_PROJ4 // http://trac.osgeo.org/proj/ticket/149 #if PJ_VERSION >= 480 pj_clear_initcache(); #endif // https://trac.osgeo.org/proj/wiki/ProjAPI#EnvironmentFunctions pj_deallocate_grids(); #endif }
PLIST_API void plist_cleanup(void) { /* free memory from parser initialization */ xmlCleanupCharEncodingHandlers(); xmlDictCleanup(); xmlResetLastError(); xmlCleanupGlobals(); xmlCleanupThreads(); xmlCleanupMemory(); }
/** * Stop engine. * */ void engine_stop(engine_type *engine) { ods_orm_shutdown(); ods_protobuf_shutdown(); if (engine && engine->config) { if (engine->config->pid_filename) { (void)unlink(engine->config->pid_filename); } if (engine->config->clisock_filename) { (void)unlink(engine->config->clisock_filename); } } engine_cleanup(engine); engine = NULL; ods_log_close(); xmlCleanupParser(); xmlCleanupGlobals(); xmlCleanupThreads(); }
/** * Start engine. * */ void engine_start(const char* cfgfile, int cmdline_verbosity, int daemonize, int info, int single_run) { engine_type* engine = NULL; int use_syslog = 0; ods_status zl_changed = ODS_STATUS_UNCHANGED; ods_status status = ODS_STATUS_OK; int close_hsm = 0; ods_log_assert(cfgfile); ods_log_init(NULL, use_syslog, cmdline_verbosity); ods_log_verbose("[%s] starting signer", engine_str); /* initialize */ xmlInitGlobals(); xmlInitParser(); xmlInitThreads(); engine = engine_create(); if (!engine) { ods_fatal_exit("[%s] create failed", engine_str); return; } engine->daemonize = daemonize; /* config */ engine->config = engine_config(engine->allocator, cfgfile, cmdline_verbosity); status = engine_config_check(engine->config); if (status != ODS_STATUS_OK) { ods_log_error("[%s] cfgfile %s has errors", engine_str, cfgfile); goto earlyexit; } if (info) { engine_config_print(stdout, engine->config); /* for debugging */ goto earlyexit; } /* check pidfile */ if (!util_check_pidfile(engine->config->pid_filename)) { exit(1); } /* open log */ ods_log_init(engine->config->log_filename, engine->config->use_syslog, engine->config->verbosity); /* setup */ tzset(); /* for portability */ status = engine_setup(engine); if (status != ODS_STATUS_OK) { ods_log_error("[%s] setup failed: %s", engine_str, ods_status2str(status)); engine->need_to_exit = 1; if (status != ODS_STATUS_WRITE_PIDFILE_ERR) { /* command handler had not yet been started */ engine->cmdhandler_done = 1; } } else { /* setup ok, mark hsm open */ close_hsm = 1; } /* run */ while (engine->need_to_exit == 0) { /* update zone list */ lock_basic_lock(&engine->zonelist->zl_lock); zl_changed = zonelist_update(engine->zonelist, engine->config->zonelist_filename); engine->zonelist->just_removed = 0; engine->zonelist->just_added = 0; engine->zonelist->just_updated = 0; lock_basic_unlock(&engine->zonelist->zl_lock); /* start/reload */ if (engine->need_to_reload) { ods_log_info("[%s] signer reloading", engine_str); engine->need_to_reload = 0; } else { ods_log_info("[%s] signer started", engine_str); zl_changed = engine_recover(engine); } if (zl_changed == ODS_STATUS_OK || zl_changed == ODS_STATUS_UNCHANGED) { engine_update_zones(engine, zl_changed); } engine_run(engine, single_run); } /* shutdown */ ods_log_info("[%s] signer shutdown", engine_str); if (close_hsm) { ods_log_verbose("[%s] close hsm", engine_str); hsm_close(); } if (!engine->cmdhandler_done) { engine_stop_xfrhandler(engine); engine_stop_dnshandler(engine); engine_stop_cmdhandler(engine); } earlyexit: if (engine && engine->config) { if (engine->config->pid_filename) { (void)unlink(engine->config->pid_filename); } if (engine->config->clisock_filename) { (void)unlink(engine->config->clisock_filename); } } tsig_handler_cleanup(); engine_cleanup(engine); engine = NULL; ods_log_close(); xmlCleanupParser(); xmlCleanupGlobals(); xmlCleanupThreads(); return; }
/** * Set up engine. * */ static ods_status engine_setup(engine_type* engine) { ods_status status = ODS_STATUS_OK; struct sigaction action; int result = 0; int sockets[2] = {0,0}; ods_log_debug("[%s] setup signer engine", engine_str); if (!engine || !engine->config) { return ODS_STATUS_ASSERT_ERR; } /* set edns */ edns_init(&engine->edns, EDNS_MAX_MESSAGE_LEN); /* create command handler (before chowning socket file) */ engine->cmdhandler = cmdhandler_create(engine->allocator, engine->config->clisock_filename); if (!engine->cmdhandler) { return ODS_STATUS_CMDHANDLER_ERR; } engine->dnshandler = dnshandler_create(engine->allocator, engine->config->interfaces); engine->xfrhandler = xfrhandler_create(engine->allocator); if (!engine->xfrhandler) { return ODS_STATUS_XFRHANDLER_ERR; } if (engine->dnshandler) { if (socketpair(AF_UNIX, SOCK_DGRAM, 0, sockets) == -1) { return ODS_STATUS_XFRHANDLER_ERR; } engine->xfrhandler->dnshandler.fd = sockets[0]; engine->dnshandler->xfrhandler.fd = sockets[1]; status = dnshandler_listen(engine->dnshandler); if (status != ODS_STATUS_OK) { ods_log_error("[%s] setup: unable to listen to sockets (%s)", engine_str, ods_status2str(status)); } } /* privdrop */ engine->uid = privuid(engine->config->username); engine->gid = privgid(engine->config->group); /* TODO: does piddir exists? */ /* remove the chown stuff: piddir? */ ods_chown(engine->config->pid_filename, engine->uid, engine->gid, 1); ods_chown(engine->config->clisock_filename, engine->uid, engine->gid, 0); ods_chown(engine->config->working_dir, engine->uid, engine->gid, 0); if (engine->config->log_filename && !engine->config->use_syslog) { ods_chown(engine->config->log_filename, engine->uid, engine->gid, 0); } if (engine->config->working_dir && chdir(engine->config->working_dir) != 0) { ods_log_error("[%s] setup: unable to chdir to %s (%s)", engine_str, engine->config->working_dir, strerror(errno)); return ODS_STATUS_CHDIR_ERR; } if (engine_privdrop(engine) != ODS_STATUS_OK) { return ODS_STATUS_PRIVDROP_ERR; } /* daemonize */ if (engine->daemonize) { switch ((engine->pid = fork())) { case -1: /* error */ ods_log_error("[%s] setup: unable to fork daemon (%s)", engine_str, strerror(errno)); return ODS_STATUS_FORK_ERR; case 0: /* child */ break; default: /* parent */ engine_cleanup(engine); engine = NULL; xmlCleanupParser(); xmlCleanupGlobals(); xmlCleanupThreads(); exit(0); } if (setsid() == -1) { ods_log_error("[%s] setup: unable to setsid daemon (%s)", engine_str, strerror(errno)); return ODS_STATUS_SETSID_ERR; } } engine->pid = getpid(); ods_log_verbose("[%s] running as pid %lu", engine_str, (unsigned long) engine->pid); /* catch signals */ signal_set_engine(engine); action.sa_handler = signal_handler; sigfillset(&action.sa_mask); action.sa_flags = 0; sigaction(SIGTERM, &action, NULL); sigaction(SIGHUP, &action, NULL); sigaction(SIGINT, &action, NULL); sigaction(SIGILL, &action, NULL); sigaction(SIGUSR1, &action, NULL); sigaction(SIGALRM, &action, NULL); sigaction(SIGCHLD, &action, NULL); action.sa_handler = SIG_IGN; sigaction(SIGPIPE, &action, NULL); /* set up hsm */ /* LEAK */ result = lhsm_open(engine->config->cfg_filename); if (result != HSM_OK) { return ODS_STATUS_HSM_ERR; } /* create workers/drudgers */ engine_create_workers(engine); engine_create_drudgers(engine); /* start cmd/dns/xfr handlers */ engine_start_cmdhandler(engine); engine_start_dnshandler(engine); engine_start_xfrhandler(engine); tsig_handler_init(engine->allocator); /* write pidfile */ if (util_write_pidfile(engine->config->pid_filename, engine->pid) == -1) { hsm_close(); return ODS_STATUS_WRITE_PIDFILE_ERR; } /* setup done */ return ODS_STATUS_OK; }
/* point-of-entry */ int main(int argc, char **argv) { int ret = -1, c, index, action = ACTION_NONE, tmp; char *action_param = NULL; processor_config_t *config = 0, *config2 = 0; char *conf_file = 0; int load_autoreg = 0; int flag_quiet = 0; int log_to_file = 0; int console_only = 0; int do_test = 0; // do whatever the test of the day is xmlInitParser(); xmlInitThreads(); //initGenericErrorDefaultFunc((xmlGenericErrorFunc *)xml_error_func); /* the getopt values */ static struct option long_options[] = { {"verbose", no_argument, 0, 0}, {"iface", required_argument, 0, 0}, {"threads", required_argument, 0, 0}, {"help", no_argument, 0, 0}, {"version", no_argument, 0, 0}, {"console", no_argument, 0, 0}, #ifdef CONFIG_PYTHON_ENABLED {"shell", no_argument, 0, 0}, {"run", required_argument, 0, 0}, {"no-scripts", no_argument, 0, 0}, #endif #ifdef CONFIG_SIP_ENABLED {"proxy-iface", required_argument, 0, 0}, {"no-mp", no_argument, 0, 0}, {"tunnel-mp", no_argument, 0, 0}, {"force-mp", no_argument, 0, 0}, {"allow-unknown", no_argument, 0, 0}, {"allow-untrusted", no_argument, 0, 0}, #endif {"list-ca", no_argument, 0, 0}, {"import-ca", required_argument, 0, 0}, {"remove-ca", required_argument, 0, 0}, {"list", no_argument, 0, 0}, {"import", required_argument, 0, 0}, {"remove", required_argument, 0, 0}, {"idents", required_argument, 0, 0}, {"log", required_argument, 0, 0}, #ifdef CONFIG_BROADCAST_ENABLED {"bc-addr", required_argument, 0, 0}, #endif #ifdef CONFIG_WEBCONF_ENABLED {"webconf", required_argument, 0, 0}, #endif #ifdef CONFIG_OPENDHT_ENABLED {"opendht", required_argument, 0, 0}, #endif #ifdef CONFIG_HIP_ENABLED {"list-hits", no_argument, 0, 0}, {"rvs", required_argument, 0, 0}, {"provide-rvs", no_argument, 0, 0}, #endif {0, 0, 0, 0} }; #ifdef LOCK_DEBUG debug2_init(); #endif #ifdef REF_DEBUG2 ship_debug_initref(); #endif if (!(config = processor_config_new()) || !(config2 = processor_config_new())) { USER_ERROR("Error loading application\n"); goto err; } if (processor_config_load_defaults(config2)) { USER_ERROR("Error loading default configurations\n"); goto err; } processor_config_get_string(config2, P2PSHIP_CONF_CONF_FILE, &conf_file); opterr = 0; while ((c = getopt_long(argc, argv, "LqvhDVs:c:p:i:Rr:kKt", long_options, &index)) != -1) { if (!c) { if (!strcmp(long_options[index].name, "threads")) { processor_config_set_int(config, P2PSHIP_CONF_WORKER_THREADS, atoi(optarg)); } else if (!strcmp(long_options[index].name, "help")) { c = '?'; } else if (!strcmp(long_options[index].name, "version")) { c = 'V'; } else if (!strcmp(long_options[index].name, "verbose")) { c = 'v'; } else if (!strcmp(long_options[index].name, "iface")) { c = 'i'; } else if (!strcmp(long_options[index].name, "console")) { console_only = 1; #ifdef CONFIG_PYTHON_ENABLED } else if (!strcmp(long_options[index].name, "shell")) { processor_config_set_true(config, P2PSHIP_CONF_START_SHELL); } else if (!strcmp(long_options[index].name, "run")) { processor_config_set_string(config, P2PSHIP_CONF_RUN_SCRIPT, optarg); } else if (!strcmp(long_options[index].name, "no-scripts")) { processor_config_set_false(config, P2PSHIP_CONF_STARTUP_SCRIPTS); #endif #ifdef CONFIG_SIP_ENABLED } else if (!strcmp(long_options[index].name, "proxy-iface")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_PROXY_IFACES, optarg); } else if (!strcmp(long_options[index].name, "no-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_MEDIA_PROXY, "no"); } else if (!strcmp(long_options[index].name, "tunnel-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_TUNNEL_PROXY, "yes"); } else if (!strcmp(long_options[index].name, "force-mp")) { processor_config_set_string(config, P2PSHIP_CONF_SIPP_FORCE_PROXY, "yes"); } else if (!strcmp(long_options[index].name, "allow-unknown")) { processor_config_set_string(config, P2PSHIP_CONF_IDENT_ALLOW_UNKNOWN_REGISTRATIONS, "yes"); } else if (!strcmp(long_options[index].name, "allow-untrusted")) { processor_config_set_string(config, P2PSHIP_CONF_IDENT_ALLOW_UNTRUSTED, "yes"); #endif } else if (!action && !strcmp(long_options[index].name, "list-ca")) { action = ACTION_LIST_CA; } else if (!action && !strcmp(long_options[index].name, "remove-ca")) { action = ACTION_REMOVE_CA; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "import-ca")) { action = ACTION_IMPORT_CA; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "list")) { action = ACTION_LIST; } else if (!action && !strcmp(long_options[index].name, "remove")) { action = ACTION_REMOVE; if (!action_param) action_param = strdup(optarg); } else if (!action && !strcmp(long_options[index].name, "import")) { action = ACTION_IMPORT; if (!action_param) action_param = strdup(optarg); } else if (!strcmp(long_options[index].name, "idents")) { processor_config_set_string(config, P2PSHIP_CONF_IDENTS_FILE, optarg); } else if (!strcmp(long_options[index].name, "log")) { processor_config_set_string(config, P2PSHIP_CONF_LOG_FILE, optarg); #ifdef CONFIG_BROADCAST_ENABLED } else if (!strcmp(long_options[index].name, "bc-addr")) { processor_config_set_string(config, P2PSHIP_CONF_BC_ADDR, optarg); #endif #ifdef CONFIG_OPENDHT_ENABLED } else if (!strcmp(long_options[index].name, "opendht")) { processor_config_set_string(config, P2PSHIP_CONF_OPENDHT_PROXY, optarg); #endif #ifdef CONFIG_HIP_ENABLED } else if (!strcmp(long_options[index].name, "list-hits")) { action = ACTION_LIST_HITS; } else if (!strcmp(long_options[index].name, "rvs")) { processor_config_set_string(config, P2PSHIP_CONF_RVS, optarg); } else if (!strcmp(long_options[index].name, "provide-rvs")) { processor_config_set_string(config, P2PSHIP_CONF_PROVIDE_RVS, "yes"); #endif #ifdef CONFIG_WEBCONF_ENABLED } else if (!strcmp(long_options[index].name, "webconf")) { processor_config_set_string(config, P2PSHIP_CONF_WEBCONF_SS, optarg); #endif } else { c = '?'; } } switch (c) { case 0: /* already processed */ break; case 'v': if (p2pship_log_level > -1) p2pship_log_level++; break; case 'q': flag_quiet = 1; p2pship_log_level = -1; break; case 'D': log_to_file = 1; processor_config_set_string(config, P2PSHIP_CONF_DAEMON, "yes"); break; case 'c': conf_file = optarg; processor_config_set_string(config, P2PSHIP_CONF_CONF_FILE, conf_file); break; case 'i': processor_config_set_string(config, P2PSHIP_CONF_IFACES, optarg); break; #ifdef CONFIG_SIP_ENABLED case 'p': if (sscanf(optarg, "%u", &tmp) != 1) { USER_ERROR("Invalid port %s\n", optarg); return 1; } else { processor_config_set_int(config, P2PSHIP_CONF_SIPP_PROXY_PORT, tmp); } break; #endif case 's': if (sscanf(optarg, "%u", &tmp) != 1) { USER_ERROR("Invalid port %s\n", optarg); return 1; } else { processor_config_set_int(config, P2PSHIP_CONF_SHIP_PORT, tmp); } break; case 'V': print_version(); return 0; case 'R': load_autoreg = 1; break; case 'r': processor_config_set_string(config, P2PSHIP_CONF_AUTOREG_FILE, optarg); break; case 'L': log_to_file = 1; break; case 'k': processor_kill_existing_pid(); break; case 'K': processor_kill_all_existing(); break; case 't': do_test = 1; break; case 'h': case '?': default: print_version(); print_usage(); return 1; } } if (!flag_quiet) print_version(); /* 1. load the defaults (done already), 2. load the conf file, 3. put on the manual overrides */ /* ensure that we have a config file! */ if (ship_ensure_file(conf_file, "# Autocreated\n\n") || processor_config_load(config2, conf_file)) { USER_ERROR("Error processing config file %s\n", conf_file); goto err; } if (processor_config_transfer(config2, config)) { USER_ERROR("Error processing configs\n"); goto err; } /* transfer */ processor_config_free(config); config = config2; config2 = NULL; /* ok, ready to rock! */ processor_config_ensure_configs(config); if (log_to_file) { p2pship_log_file = processor_config_string(config, P2PSHIP_CONF_LOG_FILE); } #ifdef CONFIG_START_GLIB_MAIN_LOOP if (!g_thread_supported()) g_thread_init(NULL); #endif #ifdef CONFIG_START_GTK gdk_threads_init(); gdk_threads_enter(); gtk_init(&argc, &argv); #endif #ifdef CALL_DEBUG calldebug_init(); #endif /* mark starttime for uptime calcs */ time(&p2pship_start); /* register each modules */ ASSERT_ZERO(processor_init(config), err); #ifdef CONFIG_HIP_ENABLED hipapi_register(); #endif #ifdef CONFIG_SIP_ENABLED sipp_register(); #endif ident_addr_register(); ident_register(); #ifdef CONFIG_WEBCONF_ENABLED webconf_register(); #endif #ifdef CONFIG_EXTAPI_ENABLED extapi_register(); #endif #ifdef CONFIG_WEBCACHE_ENABLED webcache_register(); #endif resourceman_register(); olclient_register(); conn_register(); netio_register(); netio_events_register(); netio_ff_register(); netio_man_register(); netio_http_register(); ui_register(); ui_stdin_register(); processor_init_module("ui_stdin", config); #ifdef CONFIG_DBUS_ENABLED dbus_register(); #endif if (!console_only) { #ifdef CONFIG_GTKUI_ENABLED ui_gtk_register(); processor_init_module("ui_gtk", config); #endif } addrbook_register(); #ifdef CONFIG_PYTHON_ENABLED pymod_register(); #endif #ifdef CONFIG_MEDIA_ENABLED media_register(); #endif /* check what we actually should do */ switch (action) { case ACTION_LIST_CA: { /* list ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_data_print_cas(ident_get_cas()); } break; } case ACTION_REMOVE_CA: { /* remove ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_remove_ca(action_param); } break; } case ACTION_LIST: { /* list */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_data_print_idents(ident_get_identities()); } break; } case ACTION_REMOVE: { /* remove */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { ident_remove_ident(action_param); } break; } case ACTION_IMPORT: /* import */ case ACTION_IMPORT_CA: { /* import ca */ if (processor_init_module("ident", config)) { USER_ERROR("Error initializing system\n"); } else { if (ident_import_file(action_param, 1)) { USER_ERROR("Error loading processing file %s\n", action_param); } } break; } #ifdef CONFIG_HIP_ENABLED case ACTION_LIST_HITS: { if (processor_init_module("hipapi", config)) { USER_ERROR("Error initializing system\n"); } else { hipapi_list_hits(); } break; } #endif case ACTION_NONE: default: { struct rlimit rl; int result; #ifdef CONFIG_PYTHON_ENABLED if (processor_config_is_true(config, P2PSHIP_CONF_START_SHELL)) processor_config_set_false(config, P2PSHIP_CONF_DAEMON); #endif /* go daemon (..whee!) */ if (processor_config_is_true(config, P2PSHIP_CONF_DAEMON)) { if (fork()) goto err; } /* check the stack size */ if (!(result = getrlimit(RLIMIT_STACK, &rl))) { const rlim_t stacksize = 32L * 1024L * 1024L; if (rl.rlim_cur < stacksize) { LOG_INFO("increasing stack size to %d\n", stacksize); rl.rlim_cur = stacksize; if (setrlimit(RLIMIT_STACK, &rl)) { LOG_ERROR("could not set new stack size!\n"); } } } else { LOG_ERROR("error checking stack size\n"); } if (processor_init_modules(config) || (load_autoreg && ident_autoreg_load())) { USER_ERROR("Error initializing system\n"); } else { #ifdef REMOTE_DEBUG ship_remote_debug_init(config); #endif /* start the main loop, blocks */ #ifdef REF_DEBUG2 // processor_tasks_add_periodic(ship_debug_reportref, 10000); #endif if (do_test) { LOG_DEBUG("Run tests..\n"); } processor_run(); } } } ret = 0; err: #ifdef REF_DEBUG2 ship_debug_reportref(); #endif processor_close(); freez(action_param); processor_config_free(config); processor_config_free(config2); #ifdef LOCK_DEBUG debug2_close(); #endif #ifdef REF_DEBUG2 ship_debug_closeref(); #endif xmlCleanupThreads(); if (!ret && !flag_quiet) USER_ERROR("ending ok\n"); processor_cleanup_pid(); return ret; }
/** * Set up engine and return the setup status. * */ static ods_status engine_setup_and_return_status(engine_type* engine) { struct sigaction action; int result = 0; int fd; ods_log_debug("[%s] enforcer setup", engine_str); if (!engine || !engine->config) { return ODS_STATUS_ASSERT_ERR; } /* create command handler (before chowning socket file) */ engine->cmdhandler = cmdhandler_create(engine->allocator, engine->config->clisock_filename); if (!engine->cmdhandler) { ods_log_error("[%s] create command handler to %s failed", engine_str, engine->config->clisock_filename); return ODS_STATUS_CMDHANDLER_ERR; } /* privdrop */ engine->uid = privuid(engine->config->username); engine->gid = privgid(engine->config->group); /* TODO: does piddir exists? */ /* remove the chown stuff: piddir? */ ods_chown(engine->config->pid_filename, engine->uid, engine->gid, 1); ods_chown(engine->config->clisock_filename, engine->uid, engine->gid, 0); ods_chown(engine->config->working_dir, engine->uid, engine->gid, 0); if (engine->config->log_filename && !engine->config->use_syslog) { ods_chown(engine->config->log_filename, engine->uid, engine->gid, 0); } if (engine->config->working_dir && chdir(engine->config->working_dir) != 0) { ods_log_error("[%s] chdir to %s failed: %s", engine_str, engine->config->working_dir, strerror(errno)); return ODS_STATUS_CHDIR_ERR; } if (engine_privdrop(engine) != ODS_STATUS_OK) { ods_log_error("[%s] unable to drop privileges", engine_str); return ODS_STATUS_PRIVDROP_ERR; } /* daemonize */ if (engine->daemonize) { switch ((engine->pid = fork())) { case -1: /* error */ ods_log_error("[%s] unable to fork daemon: %s", engine_str, strerror(errno)); return ODS_STATUS_FORK_ERR; case 0: /* child */ if ((fd = open("/dev/null", O_RDWR, 0)) != -1) { (void)dup2(fd, STDIN_FILENO); (void)dup2(fd, STDOUT_FILENO); (void)dup2(fd, STDERR_FILENO); if (fd > 2) (void)close(fd); } break; default: /* parent */ engine_cleanup(engine); engine = NULL; xmlCleanupParser(); xmlCleanupGlobals(); xmlCleanupThreads(); exit(0); } if (setsid() == -1) { ods_log_error("[%s] unable to setsid daemon (%s)", engine_str, strerror(errno)); return ODS_STATUS_SETSID_ERR; } } engine->pid = getpid(); ods_log_verbose("[%s] running as pid %lu", engine_str, (unsigned long) engine->pid); /* catch signals */ signal_set_engine(engine); action.sa_handler = signal_handler; sigfillset(&action.sa_mask); action.sa_flags = 0; sigaction(SIGHUP, &action, NULL); sigaction(SIGTERM, &action, NULL); /* set up hsm */ /* LEAK */ result = hsm_open(engine->config->cfg_filename, hsm_prompt_pin, NULL); if (result != HSM_OK) { ods_log_error("[%s] error initializing libhsm (errno %i)", engine_str, result); return ODS_STATUS_HSM_ERR; } /* create workers */ engine_create_workers(engine); engine_create_drudgers(engine); /* start command handler */ engine_start_cmdhandler(engine); /* write pidfile */ if (util_write_pidfile(engine->config->pid_filename, engine->pid) == -1) { hsm_close(); ods_log_error("[%s] unable to write pid file", engine_str); return ODS_STATUS_WRITE_PIDFILE_ERR; } return ODS_STATUS_OK; }