Exemplo n.º 1
0
void
mowgli_program_opts_parse(const mowgli_program_opts_t *opts, size_t opts_size, int *argc, char ***argv)
{
	mowgli_getopt_option_t *g_opts;
	const char *shortops;
	int c;
	size_t i;
	int opt_index;

	return_if_fail(opts != NULL);
	return_if_fail(opts_size > 0);
	return_if_fail(argc != NULL);
	return_if_fail(argv != NULL);

	g_opts = mowgli_program_opts_convert(opts, opts_size);
	shortops = mowgli_program_opts_compute_optstr(opts, opts_size);

	for (;;)
	{
		const mowgli_program_opts_t *opt = NULL;

		c = mowgli_getopt_long(*argc, *argv, shortops, g_opts, &opt_index);

		if (c == -1)
			break;

		switch (c)
		{
		case 0:

			/* long-option was provided, resolve it. */
			opt = &opts[g_opts[opt_index].iflag];
			break;
		default:

			for (i = 0; i < opts_size; i++)
				if (opts[i].smallopt == c)
				{
					opt = &opts[i];
					break;
				}

			break;
		}

		mowgli_program_opts_dispatch(opt, mowgli_optarg);
	}

	mowgli_free(g_opts);
}
Exemplo n.º 2
0
Arquivo: sigyn.c Projeto: alyx/sigyn
void parse_commandline_options(int argc, char **argv)
{
    int r;

    mowgli_getopt_option_t long_opts[] = {
      { NULL, 0, NULL, 0, 0 },
    };

    while ((r = mowgli_getopt_long(argc, argv, "c:hvnd:", long_opts, NULL)) != -1)
    {
        switch (r)
        {
	        case 'h':
                printf("usage: sigyn [-hv] [-c config]\n\n"
                " -c <path>   Specify a configuration file for sigyn to use\n"
                " -h          Print this message and exit\n"
                " -d <level>  Run in foreground and print debug (1=all, 2=debug)\n"
                " -v          Print the version information and exit\n"
                " -n          Prevents Sigyn from forking into the background\n");
                exit(EXIT_SUCCESS);
                break;
            case 'v':
                printf("%s by Alyx Wolcott.\n", PACKAGE_STRING);
                exit(EXIT_SUCCESS);
                break;
            case 'c':
                config_file = mowgli_strdup(mowgli_optarg);
                break;
            case 'd':
                me.debug = atoi(mowgli_optarg);
                break;
            case 'n':
                should_fork = false;
                break;
            default:
                printf("\nusage: sigyn [-hv] [-d debug level] [-c config]\n");
                exit(EXIT_SUCCESS);
                break;
        }
    }
    if (config_file == NULL)
        config_file = mowgli_strdup(SYSCONFDIR "/sigyn.conf");
}
Exemplo n.º 3
0
int atheme_main(int argc, char *argv[])
{
	int daemonize_pipe[2];
	bool have_conf = false;
	bool have_log = false;
	bool have_datadir = false;
	char buf[32];
	int pid, r;
	FILE *pid_file;
	const char *pidfilename = RUNDIR "/atheme.pid";
	char *log_p = NULL;
	mowgli_getopt_option_t long_opts[] = {
		{ NULL, 0, NULL, 0, 0 },
	};

	atheme_bootstrap();

	/* do command-line options */
	while ((r = mowgli_getopt_long(argc, argv, "c:dhrl:np:D:v", long_opts, NULL)) != -1)
	{
		switch (r)
		{
		  case 'c':
			  config_file = sstrdup(mowgli_optarg);
			  have_conf = true;
			  break;
		  case 'd':
			  log_force = true;
			  break;
		  case 'h':
			  print_help();
			  exit(EXIT_SUCCESS);
			  break;
		  case 'r':
			  readonly = true;
			  break;
		  case 'l':
			  log_p = sstrdup(mowgli_optarg);
			  have_log = true;
			  break;
		  case 'n':
			  runflags |= RF_LIVE;
			  break;
		  case 'p':
			  pidfilename = mowgli_optarg;
			  break;
		  case 'D':
			  datadir = mowgli_optarg;
			  have_datadir = true;
			  break;
		  case 'v':
			  print_version();
			  exit(EXIT_SUCCESS);
			  break;
		  default:
			  printf("usage: atheme [-dhnvr] [-c conf] [-l logfile] [-p pidfile]\n");
			  exit(EXIT_FAILURE);
			  break;
		}
	}

	if (!have_conf)
		config_file = sstrdup(SYSCONFDIR "/atheme.conf");

	if (!have_log)
		log_p = sstrdup(LOGDIR "/atheme.log");

	if (!have_datadir)
		datadir = sstrdup(DATADIR);

	cold_start = true;

	runflags |= RF_STARTING;

	atheme_init(argv[0], log_p);

	slog(LG_INFO, "%s is starting up...", PACKAGE_STRING);

	/* check for pid file */
#ifndef MOWGLI_OS_WIN
	if ((pid_file = fopen(pidfilename, "r")))
	{
		if (fgets(buf, 32, pid_file))
		{
			pid = atoi(buf);

			if (!kill(pid, 0))
			{
				fprintf(stderr, "atheme: daemon is already running\n");
				exit(EXIT_FAILURE);
			}
		}

		fclose(pid_file);
	}
#endif

	if (!(runflags & RF_LIVE))
		daemonize(daemonize_pipe);

	atheme_setup();

	conf_init();
	if (!conf_parse(config_file))
	{
		slog(LG_ERROR, "Error loading config file %s, aborting",
				config_file);
		exit(EXIT_FAILURE);
	}

	if (config_options.languagefile)
	{
		slog(LG_DEBUG, "Using language: %s", config_options.languagefile);
		if (!conf_parse(config_options.languagefile))
			slog(LG_INFO, "Error loading language file %s, continuing",
					config_options.languagefile);
	}

	if (!backend_loaded && authservice_loaded)
	{
		slog(LG_ERROR, "atheme: no backend modules loaded, see your configuration file.");
		exit(EXIT_FAILURE);
	}

	/* we've done the critical startup steps now */
	cold_start = false;

	/* load our db */
	if (db_load)
		db_load(NULL);
	else if (backend_loaded)
	{
		slog(LG_ERROR, "atheme: backend module does not provide db_load()!");
		exit(EXIT_FAILURE);
	}
	db_check();

#ifdef HAVE_GETPID
	/* write pid */
	if ((pid_file = fopen(pidfilename, "w")))
	{
		fprintf(pid_file, "%d\n", getpid());
		fclose(pid_file);
	}
	else
	{
		fprintf(stderr, "atheme: unable to write pid file\n");
		exit(EXIT_FAILURE);
	}
#endif

	/* detach from terminal */
	if ((runflags & RF_LIVE) || !detach_console(daemonize_pipe))
	{
#ifdef HAVE_GETPID
		slog(LG_INFO, "pid %d", getpid());
#endif
		slog(LG_INFO, "running in foreground mode from %s", PREFIX);
	}

	/* no longer starting */
	runflags &= ~RF_STARTING;

	/* we probably have a few open already... */
	me.maxfd = 3;

	/* DB commit interval is configurable */
	if (db_save && !readonly)
		mowgli_timer_add(base_eventloop, "db_save", db_save, NULL, config_options.commit_interval);

	/* check expires every hour */
	mowgli_timer_add(base_eventloop, "expire_check", expire_check, NULL, 3600);

	/* check k/x/q line expires every minute */
	mowgli_timer_add(base_eventloop, "kline_expire", kline_expire, NULL, 60);
	mowgli_timer_add(base_eventloop, "xline_expire", xline_expire, NULL, 60);
	mowgli_timer_add(base_eventloop, "qline_expire", qline_expire, NULL, 60);

	/* check authcookie expires every ten minutes */
	mowgli_timer_add(base_eventloop, "authcookie_expire", authcookie_expire, NULL, 600);

	/* reseed rng a little every five minutes */
	mowgli_timer_add(base_eventloop, "rng_reseed", rng_reseed, NULL, 293);

	me.connected = false;
	uplink_connect();

	/* main loop */
	io_loop();

	/* we're shutting down */
	hook_call_shutdown();

	if (db_save && !readonly)
		db_save(NULL);

	remove(pidfilename);
	errno = 0;
	if (curr_uplink != NULL && curr_uplink->conn != NULL)
		sendq_flush(curr_uplink->conn);
	connection_close_all();

	me.connected = false;

	/* should we restart? */
	if (runflags & RF_RESTART)
	{
		slog(LG_INFO, "main(): restarting");

#ifdef HAVE_EXECVE
		execv(BINDIR "/atheme-services", argv);
#endif
	}

	slog(LG_INFO, "main(): shutting down");

	mowgli_eventloop_destroy(base_eventloop);
	log_shutdown();

	return 0;
}