Example #1
0
void
usage(const char *msg) {
	if (msg)
		fprintf(stderr, "%s: usage error: %s\n", argv_program, msg);
	nmsg_io_destroy(&ctx.io);
	exit(argv_usage(args, ARGV_USAGE_DEFAULT));
}
static void *
stats_thread(void *user) {
	struct timespec ts = { .tv_sec = STATS_FREQUENCY, .tv_nsec = 0 };

	for (;;) {
		nmsg_timespec_sleep(&ts);
		if (shut_down)
			break;
		print_stats();
	}

	return (NULL);
}

void
setup_threads(void) {
	if (is_live) {
		/* create stats thread */
		assert(pthread_create(&thr_stats, NULL, stats_thread, NULL) == 0);
	}
}

void
cleanup(void) {
	nmsg_io_destroy(&io);
	print_stats();
	argv_cleanup(args);
}
Example #3
0
int main(int argc, char **argv) {
	nmsg_res res;

	/* parse command line arguments */
	argv_process(args, argc, argv);

	if (ctx.debug < 1)
		ctx.debug = 1;
	nmsg_set_debug(ctx.debug);
	res = nmsg_init();
	if (res != nmsg_res_success) {
		fprintf(stderr, "nmsgtool: unable to initialize libnmsg\n");
		return (EXIT_FAILURE);
	}
	if (ctx.debug >= 2)
#ifdef HAVE_LIBXS
		fprintf(stderr, "nmsgtool: version " VERSION "\n");
#else /* HAVE_LIBXS */
		fprintf(stderr, "nmsgtool: version " VERSION " (without libxs support)\n");
#endif /* HAVE_LIBXS */

	/* initialize the nmsg_io engine */
	ctx.io = nmsg_io_init();
	assert(ctx.io != NULL);
	nmsg_io_set_close_fp(ctx.io, io_close);

	/* process arguments and load inputs/outputs into the nmsg_io engine */
	process_args(&ctx);

	setup_signals();

	/* run the nmsg_io engine */
	res = nmsg_io_loop(ctx.io);

	/* cleanup */
	if (ctx.pidfile != NULL) {
		if (unlink(ctx.pidfile) != 0) {
			fprintf(stderr, "nmsgtool: unlink() failed: %s\n",
				strerror(errno));
		}
	}
	nmsg_io_destroy(&ctx.io);
#ifdef HAVE_LIBXS
	if (ctx.xs_ctx)
		xs_term(ctx.xs_ctx);
#endif /* HAVE_LIBXS */
	free(ctx.endline_str);
	argv_cleanup(args);

	return (res);
}