Ejemplo n.º 1
0
EventBase::EventBase(const char* method)
{
    // enable locking for libevent structure
    if (evthread_use_pthreads())
    {
        throw std::exception();
    }

#ifdef DEBUG
    evthread_enable_lock_debuging();
    event_enable_debug_mode();
#endif

    struct event_config *config;
    config = event_config_new();

    int i = 0;
    const char** availMethods = event_get_supported_methods();

    for (i = 0; availMethods[i] != NULL; i++)
    {
        if (strcmp(availMethods[i], method)) {
            event_config_avoid_method(config, availMethods[i]);
        }
    }
    base_ = event_base_new_with_config(config);

    if (!base_)
    {
        throw BadBaseException();
    }
    event_base_get_method(base_);
    event_config_free(config);
}
Ejemplo n.º 2
0
int
main(int argc, const char **argv)
{
#ifdef _WIN32
	WORD wVersionRequested;
	WSADATA wsaData;
	int	err;

	wVersionRequested = MAKEWORD(2, 2);

	err = WSAStartup(wVersionRequested, &wsaData);
#endif

#ifndef _WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return 1;
#endif

#ifdef _WIN32
	tinytest_skip(testgroups, "http/connection_retry");
#endif

#ifndef _EVENT_DISABLE_THREAD_SUPPORT
	if (!getenv("EVENT_NO_DEBUG_LOCKS"))
		evthread_enable_lock_debuging();
#endif

	if (tinytest_main(argc,argv,testgroups))
		return 1;

	return 0;
}
Ejemplo n.º 3
0
int
main(int argc, char **argv)
{
	int i,j;
	double ratio;

#ifdef WIN32
	WORD wVersionRequested = MAKEWORD(2,2);
	WSADATA wsaData;

	(void) WSAStartup(wVersionRequested, &wsaData);
#endif

#ifndef WIN32
	if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
		return 1;
#endif
	for (i = 1; i < argc; ++i) {
		for (j = 0; options[j].name; ++j) {
			if (!strcmp(argv[i],options[j].name)) {
				if (handle_option(argc,argv,&i,&options[j])<0)
					return 1;
				goto again;
			}
		}
		fprintf(stderr, "Unknown option '%s'\n", argv[i]);
		usage();
		return 1;
	again:
		;
	}
	if (cfg_help) {
		usage();
		return 0;
	}

	cfg_tick.tv_sec = cfg_tick_msec / 1000;
	cfg_tick.tv_usec = (cfg_tick_msec % 1000)*1000;

	seconds_per_tick = ratio = cfg_tick_msec / 1000.0;

	cfg_connlimit *= ratio;
	cfg_grouplimit *= ratio;

	{
		struct timeval tv;
		evutil_gettimeofday(&tv, NULL);
#ifdef WIN32
		srand(tv.tv_usec);
#else
		srandom(tv.tv_usec);
#endif
	}

#ifndef _EVENT_DISABLE_THREAD_SUPPORT
	evthread_enable_lock_debuging();
#endif

	return test_ratelimiting();
}
Ejemplo n.º 4
0
int
main (int argc, char** argv)
{
	CDServer* server;
	int       opt;
	bool      noFork    = false;
	bool      debugging = false;
	char*     config    = NULL;

	static const char* configSearchPath[] = {
		"%s/.craftd/craftd.conf", // %s is replaced with $HOME
		"/etc/craftd/craftd.conf",
		"/usr/local/etc/craftd/craftd.conf",
		"craftd.conf", // Current working directory (for devs)
		NULL
	};

	CDDefaultLogger = CDConsoleLogger;

	LOG(LOG_INFO, "%s " CRAFTD_NOTICE_MESSAGE, argv[0]);

	while ((opt = getopt(argc, argv, "c:dhnv")) != -1) {
		switch (opt) {
			case 'd': {  // debugging mode
				debugging = true;
			} break;

			case 'v': { // print version
				exit(EXIT_SUCCESS); // Version header already printed
			} break;

			case 'n': { // don't fork or daemonize, use stdout for logging
				noFork = true;
			} break;

			case 'c': { // use the specified config file
				config = optarg;
			} break;

			case 'h': // print help message
			default: {
				fprintf(stderr, "\nUsage: %s [OPTION]...\n"
					"-c <conf file>    specify a conf file location\n"
					"-d                enable verbose debugging messages\n"
					"-h                display this help and exit\n"
					"-n                don't fork/daemonize (overrides config file)\n"
					"-v                output version information and exit\n"
					"\n"
					"For complete documentation, visit the wiki.\n\n", argv[0]);

				exit((opt == 'h') ? EXIT_SUCCESS : EXIT_FAILURE);
			}
		}
	}

	if (!config) {
		char         path[FILENAME_MAX] = { 0 };
		const char** current            = configSearchPath;

		do {
			snprintf(path, FILENAME_MAX, *current, getenv("HOME"));

			current++;
		} while (*current != NULL && !CD_PathExists(path));

		if (!CD_PathExists(path)) {
			CD_abort("The config file could not be found");
		}
		else {
			config = path;
		}
	}

	if (!CD_IsReadable(config)) {
		CD_abort("%s could not be read", config);
	}

	#ifdef WIN32
		evthread_use_windows_threads();
	#else
		evthread_use_pthreads();
	#endif

	if (debugging) {
		evthread_enable_lock_debuging();
	}

	CDMainServer = server = CD_CreateServer(config);

	if (!server) {
		CD_abort("Server couldn't be instantiated");
	}

	/* By default, mask debugging messages */
	if (!debugging) {
		server->logger.setlogmask(LOG_MASK(LOG_DEBUG));
	}

	CD_RunServer(server);

	LOG(LOG_INFO, "Exiting.");
	LOG_CLOSE();

	CD_DestroyServer(server);
}
Ejemplo n.º 5
0
/*
 * The actual main function.
 */
int
sntp_main (
	int argc,
	char **argv,
	const char *sntpVersion
	)
{
	int			i;
	int			exitcode;
	int			optct;
	struct event_config *	evcfg;

	/* Initialize logging system - sets up progname */
	sntp_init_logging(argv[0]);

	if (!libevent_version_ok())
		exit(EX_SOFTWARE);

	init_lib();
	init_auth();

	optct = ntpOptionProcess(&sntpOptions, argc, argv);
	argc -= optct;
	argv += optct;


	debug = OPT_VALUE_SET_DEBUG_LEVEL;

	TRACE(2, ("init_lib() done, %s%s\n",
		  (ipv4_works)
		      ? "ipv4_works "
		      : "",
		  (ipv6_works)
		      ? "ipv6_works "
		      : ""));
	ntpver = OPT_VALUE_NTPVERSION;
	steplimit = OPT_VALUE_STEPLIMIT / 1e3;
	gap.tv_usec = max(0, OPT_VALUE_GAP * 1000);
	gap.tv_usec = min(gap.tv_usec, 999999);

	if (HAVE_OPT(LOGFILE))
		open_logfile(OPT_ARG(LOGFILE));

	msyslog(LOG_INFO, "%s", sntpVersion);

	if (0 == argc && !HAVE_OPT(BROADCAST) && !HAVE_OPT(CONCURRENT)) {
		printf("%s: Must supply at least one of -b hostname, -c hostname, or hostname.\n",
		       progname);
		exit(EX_USAGE);
	}


	/*
	** Eventually, we probably want:
	** - separate bcst and ucst timeouts (why?)
	** - multiple --timeout values in the commandline
	*/

	response_timeout = OPT_VALUE_TIMEOUT;
	response_tv.tv_sec = response_timeout;
	response_tv.tv_usec = 0;

	/* IPv6 available? */
	if (isc_net_probeipv6() != ISC_R_SUCCESS) {
		ai_fam_pref = AF_INET;
		TRACE(1, ("No ipv6 support available, forcing ipv4\n"));
	} else {
		/* Check for options -4 and -6 */
		if (HAVE_OPT(IPV4))
			ai_fam_pref = AF_INET;
		else if (HAVE_OPT(IPV6))
			ai_fam_pref = AF_INET6;
	}

	/* TODO: Parse config file if declared */

	/*
	** Init the KOD system.
	** For embedded systems with no writable filesystem,
	** -K /dev/null can be used to disable KoD storage.
	*/
	kod_init_kod_db(OPT_ARG(KOD), FALSE);

	// HMS: Should we use arg-defalt for this too?
	if (HAVE_OPT(KEYFILE))
		auth_init(OPT_ARG(KEYFILE), &keys);

	/*
	** Considering employing a variable that prevents functions of doing
	** anything until everything is initialized properly
	**
	** HMS: What exactly does the above mean?
	*/
	event_set_log_callback(&sntp_libevent_log_cb);
	if (debug > 0)
		event_enable_debug_mode();
#ifdef WORK_THREAD
	evthread_use_pthreads();
	/* we use libevent from main thread only, locks should be academic */
	if (debug > 0)
		evthread_enable_lock_debuging();
#endif
	evcfg = event_config_new();
	if (NULL == evcfg) {
		printf("%s: event_config_new() failed!\n", progname);
		return -1;
	}
#ifndef HAVE_SOCKETPAIR
	event_config_require_features(evcfg, EV_FEATURE_FDS);
#endif
	/* all libevent calls are from main thread */
	/* event_config_set_flag(evcfg, EVENT_BASE_FLAG_NOLOCK); */
	base = event_base_new_with_config(evcfg);
	event_config_free(evcfg);
	if (NULL == base) {
		printf("%s: event_base_new() failed!\n", progname);
		return -1;
	}

	/* wire into intres resolver */
	worker_per_query = TRUE;
	addremove_io_fd = &sntp_addremove_fd;

	open_sockets();

	if (HAVE_OPT(BROADCAST)) {
		int		cn = STACKCT_OPT(  BROADCAST );
		const char **	cp = STACKLST_OPT( BROADCAST );

		while (cn-- > 0) {
			handle_lookup(*cp, CTX_BCST);
			cp++;
		}
	}

	if (HAVE_OPT(CONCURRENT)) {
		int		cn = STACKCT_OPT( CONCURRENT );
		const char **	cp = STACKLST_OPT( CONCURRENT );

		while (cn-- > 0) {
			handle_lookup(*cp, CTX_UCST | CTX_CONC);
			cp++;
		}
	}

	for (i = 0; i < argc; ++i)
		handle_lookup(argv[i], CTX_UCST);

	gettimeofday_cached(base, &start_tv);
	event_base_dispatch(base);
	event_base_free(base);

	if (!time_adjusted &&
	    (ENABLED_OPT(STEP) || ENABLED_OPT(SLEW)))
		exitcode = 1;
	else
		exitcode = 0;

	return exitcode;
}