Exemplo n.º 1
0
/**
 * Deinit the service
 */
static void
service_deinit(struct daemon* daemon, struct config_file* cfg)
{
	daemon_cleanup(daemon);
	config_delete(cfg);
	daemon_delete(daemon);
}
Exemplo n.º 2
0
/**
 * Run the daemon. 
 * @param cfgfile: the config file name.
 * @param cmdline_verbose: verbosity resulting from commandline -v.
 *    These increase verbosity as specified in the config file.
 * @param debug_mode: if set, do not daemonize.
 * @param log_default_identity: Default identity to report in logs
 */
static void 
run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, const char* log_default_identity)
{
	struct config_file* cfg = NULL;
	struct daemon* daemon = NULL;
	int done_setup = 0;

	if(!(daemon = daemon_init()))
		fatal_exit("alloc failure");
	while(!daemon->need_to_exit) {
		if(done_setup)
			verbose(VERB_OPS, "Restart of %s.", PACKAGE_STRING);
		else	verbose(VERB_OPS, "Start of %s.", PACKAGE_STRING);

		/* config stuff */
		if(!(cfg = config_create()))
			fatal_exit("Could not alloc config defaults");
		if(!config_read(cfg, cfgfile, daemon->chroot)) {
			if(errno != ENOENT)
				fatal_exit("Could not read config file: %s",
					cfgfile);
			log_warn("Continuing with default config settings");
		}
		apply_settings(daemon, cfg, cmdline_verbose, debug_mode, log_default_identity);
		if(!done_setup)
			config_lookup_uid(cfg);
	
		/* prepare */
		if(!daemon_open_shared_ports(daemon))
			fatal_exit("could not open ports");
		if(!done_setup) { 
			perform_setup(daemon, cfg, debug_mode, &cfgfile);
			done_setup = 1; 
		} else {
			/* reopen log after HUP to facilitate log rotation */
			if(!cfg->use_syslog)
				log_init(cfg->logfile, 0, cfg->chrootdir);
		}
		/* work */
		daemon_fork(daemon);

		/* clean up for restart */
		verbose(VERB_ALGO, "cleanup.");
		daemon_cleanup(daemon);
		config_delete(cfg);
	}
	verbose(VERB_ALGO, "Exit cleanup.");
	/* this unlink may not work if the pidfile is located outside
	 * of the chroot/workdir or we no longer have permissions */
	if(daemon->pidfile) {
		int fd;
		/* truncate pidfile */
		fd = open(daemon->pidfile, O_WRONLY | O_TRUNC, 0644);
		if(fd != -1)
			close(fd);
		/* delete pidfile */
		unlink(daemon->pidfile);
	}
	daemon_delete(daemon);
}
Exemplo n.º 3
0
static void daemon_error(const char *msg)
{
   DEBUG_MSG("daemon_error: %s", msg);
   
   /* open the exit log file */
   daemon_cleanup();
   
   fprintf(stdout, "%s\n", msg);
   
   return;
}
Exemplo n.º 4
0
/**
 * The main function for the service.
 * Called by the services API when starting unbound on windows in background.
 * Arguments could have been present in the string 'path'.
 * @param argc: nr args
 * @param argv: arg text.
 */
static void 
service_main(DWORD ATTR_UNUSED(argc), LPTSTR* ATTR_UNUSED(argv))
{
	struct config_file* cfg = NULL;
	struct daemon* daemon = NULL;

	service_status_handle = RegisterServiceCtrlHandler(SERVICE_NAME, 
		(LPHANDLER_FUNCTION)hdlr);
	if(!service_status_handle) {
		reportev("Could not RegisterServiceCtrlHandler");
		return;
	}
	
	service_status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
	service_status.dwServiceSpecificExitCode = 0;

	/* see if we have root anchor update enabled */
	call_root_update();

	/* we are now starting up */
	report_status(SERVICE_START_PENDING, NO_ERROR, 3000);
	if(!service_init(0, &daemon, &cfg)) {
		reportev("Could not service_init");
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}

	/* event that gets signalled when we want to quit; it
	 * should get registered in the worker-0 waiting loop. */
	service_stop_event = WSACreateEvent();
	if(service_stop_event == WSA_INVALID_EVENT) {
		log_err("WSACreateEvent: %s", wsa_strerror(WSAGetLastError()));
		reportev("Could not WSACreateEvent");
		report_status(SERVICE_STOPPED, NO_ERROR, 0);
		return;
	}
	if(!WSAResetEvent(service_stop_event)) {
		log_err("WSAResetEvent: %s", wsa_strerror(WSAGetLastError()));
	}

	/* SetServiceStatus SERVICE_RUNNING;*/
	report_status(SERVICE_RUNNING, NO_ERROR, 0);
	verbose(VERB_QUERY, "winservice - init complete");

	/* daemon performs work */
	while(!service_stop_shutdown) {
		daemon_fork(daemon);
		if(!service_stop_shutdown) {
			daemon_cleanup(daemon);
			config_delete(cfg); cfg=NULL;
			if(!service_init(1, &daemon, &cfg)) {
				reportev("Could not service_init");
				report_status(SERVICE_STOPPED, NO_ERROR, 0);
				return;
			}
		}
	}

	/* exit */
	verbose(VERB_ALGO, "winservice - cleanup.");
	report_status(SERVICE_STOP_PENDING, NO_ERROR, 0);
	service_deinit(daemon, cfg);
	free(service_cfgfile);
	if(service_stop_event) (void)WSACloseEvent(service_stop_event);
	verbose(VERB_QUERY, "winservice - full stop");
	report_status(SERVICE_STOPPED, NO_ERROR, 0);
}
Exemplo n.º 5
0
int
main(int argc, char **argv)
{
	char val[4096];
	char listener_name[80];
	char backend_name[80];
	const char *config_file = DEFAULT_CONFIG_FILE;
	config_object_t *config = NULL;
	map_object_t *map = NULL;
	const listener_plugin_t *lp;
	const backend_plugin_t *p;
	listener_context_t listener_ctx = NULL;
	backend_context_t backend_ctx = NULL;
	int debug_set = 0, foreground = 0, wait_for_init = 0;
	int opt, configure = 0;

	config = sc_init();
	map = map_init();

	if (!config || !map) {
		perror("malloc");
		return -1;
	}

	while ((opt = getopt(argc, argv, "Ff:d:cwlh")) != EOF) {
		switch(opt) {
		case 'F':
			printf("Background mode disabled\n");
			foreground = 1;
			break;
		case 'f':
			printf("Using %s\n", optarg);
			config_file = optarg;
			break;
		case 'd':
			debug_set = atoi(optarg);
			break;
		case 'c':
			configure = 1;
			break;
		case 'w':
			wait_for_init = 1;
			break;
		case 'l':
			plugin_dump();
			return 0;
		case 'h':
		case '?':
			usage();
			return 0;
		default:
			return -1;
		}
	}

	if (configure) {
		return do_configure(config, config_file);
	}

	if (sc_parse(config, config_file) != 0) {
		printf("Failed to parse %s\n", config_file);
		return -1;
	}

	if (debug_set) {
		snprintf(val, sizeof(val), "%d", debug_set);
		sc_set(config, "fence_virtd/@debug", val);
	} else {
		if (sc_get(config, "fence_virtd/@debug", val, sizeof(val))==0)
			debug_set = atoi(val);
	}

	dset(debug_set);

	if (!foreground) {
		if (sc_get(config, "fence_virtd/@foreground",
			   val, sizeof(val)) == 0)
			foreground = atoi(val);
	}

	if (!wait_for_init) {
		if (sc_get(config, "fence_virtd/@wait_for_init",
			   val, sizeof(val)) == 0)
			wait_for_init = atoi(val);
		if (!wait_for_init) {
			/* XXX compat */
			if (sc_get(config, "fence_virtd/@wait_for_backend",
				   val, sizeof(val)) == 0)
				wait_for_init = atoi(val);
		}
	}

	if (dget() > 3)
		sc_dump(config, stdout);

	if (sc_get(config, "fence_virtd/@backend", backend_name,
		   sizeof(backend_name))) {
		printf("Failed to determine backend.\n");
		printf("%s\n", val);
		return -1;
	}

	dbg_printf(1, "Backend plugin: %s\n", backend_name);

	if (sc_get(config, "fence_virtd/@listener", listener_name,
		   sizeof(listener_name))) {
		printf("Failed to determine backend.\n");
		printf("%s\n", val);
		return -1;
	}

	dbg_printf(1, "Listener plugin: %s\n", listener_name);

#ifdef _MODULE
	if (sc_get(config, "fence_virtd/@module_path", val,
		   sizeof(val))) {
#ifdef MODULE_PATH
		snprintf(val, sizeof(val), MODULE_PATH);
#else
		printf("Failed to determine module path.\n");
		return -1;
#endif
	}

	dbg_printf(1, "Searching %s for plugins...\n", val);

	opt = plugin_search(val);
	if (opt > 0) {
		dbg_printf(1, "%d plugins found\n", opt);
	} else {
		printf("No plugins found\n");
		return 1;
	}

#endif
	if (dget() > 3)
		plugin_dump();

	lp = plugin_find_listener(listener_name);
	if (!lp) {
		printf("Could not find listener \"%s\"\n", listener_name);
		return 1;
	}

	p = plugin_find_backend(backend_name);
	if (!p) {
		printf("Could not find backend \"%s\"\n", backend_name);
		return 1;
	}

	daemon_init(basename(argv[0]), foreground);
	signal(SIGINT, exit_handler);
	signal(SIGTERM, exit_handler);
	signal(SIGQUIT, exit_handler);

	syslog(LOG_NOTICE, "fence_virtd starting.  Listener: %s  Backend: %s", backend_name, listener_name);

	while (p->init(&backend_ctx, config) < 0) {
		if (!wait_for_init) {
			if (foreground) {
				printf("Backend plugin %s failed to initialize\n",
				       backend_name);
			}
			syslog(LOG_ERR,
			       "Backend plugin %s failed to initialize\n",
			       backend_name);
			return 1;
		}
		sleep(5);
	}

	if (map_load(map, config) < 0) {
		syslog(LOG_WARNING, "Failed to load static maps\n");
	}

	/* only client we have now is mcast (fence_xvm behavior) */
	while (lp->init(&listener_ctx, p->callbacks, config, map,
			backend_ctx) != 0) {
		if (!wait_for_init) {
			if (foreground) {
				printf("Listener plugin %s failed to initialize\n",
				       listener_name);
			}
			syslog(LOG_ERR,
			       "Listener plugin %s failed to initialize\n",
			       listener_name);
			return 1;
		}
		sleep(5);
	}

	while (run && lp->dispatch(listener_ctx, NULL) >= 0);

	syslog(LOG_NOTICE, "fence_virtd shutting down");

	map_release(map);
	sc_release(config);

	lp->cleanup(listener_ctx);
	p->cleanup(backend_ctx);

	daemon_cleanup();

	return 0;
}