Example #1
0
    void test_sql_quote()
    {
        assert_equal(sql_quote("a perfectly innocuous string"), "a perfectly innocuous string");
        assert_equal(sql_quote("\\foo;"), "\\\\foo;");
        assert_equal(sql_quote("'"), "\\'");
        assert_equal(sql_quote("%_blas\r\n'foo\"\\''"), "\\%\\_blas\\r\\n\\'foo\\\"\\\\\\'\\'");
        assert_equal(sql_quote(""), "");
        assert_equal(sql_quote("1234567890"), "1234567890");
        assert_equal(sql_quote("\r\n\b\t\032_%"), "\\r\\n\\b\\t\\Z\\_\\%");

        assert_equal("a perfectly innocuous string", sql_unquote("a perfectly innocuous string"));
        assert_equal("\\foo;", sql_unquote("\\\\foo;"));
        assert_equal("'", sql_unquote("\\'"));
        assert_equal("%_blas\r\n'foo\"\\''", sql_unquote("\\%\\_blas\\r\\n\\'foo\\\"\\\\\\'\\'"));
        assert_equal("", sql_unquote(""));
        assert_equal("1234567890", sql_unquote("1234567890"));
        assert_equal("\r\n\b\t\032_%", sql_unquote("\\r\\n\\b\\t\\Z\\_\\%"));
    }
Example #2
0
int main(int argc, char **argv)
{
	int i, result;

	progname = strrchr(argv[0], '/');
	progname = progname ? progname + 1 : argv[0];

	is_module = 0;
	ipc_init_struct();
	gettimeofday(&self.start, NULL);


	/*
	 * Solaris doesn't support MSG_NOSIGNAL, so
	 * we ignore SIGPIPE globally instead
	 */
	signal(SIGPIPE, SIG_IGN);

	for (i = 1; i < argc; i++) {
		char *opt, *arg = argv[i];

		if (*arg != '-') {
			if (!merlin_conf) {
				merlin_conf = arg;
				continue;
			}
			goto unknown_argument;
		}

		if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
			usage(NULL);
		if (!strcmp(arg, "-k") || !strcmp(arg, "--kill")) {
			killing = 1;
			continue;
		}
		if (!strcmp(arg, "-d") || !strcmp(arg, "--debug")) {
			debug++;
			continue;
		}

		if ((opt = strchr(arg, '=')))
			opt++;
		else if (i < argc - 1)
			opt = argv[i + 1];
		else
			usage("Unknown argument, or argument '%s' requires a parameter", arg);

		i++;
		if (!strcmp(arg, "--config") || !strcmp(arg, "-c")) {
			merlin_conf = opt;
			continue;
		}
		unknown_argument:
		usage("Unknown argument: %s", arg);
	}

	if (!merlin_conf)
		usage("No config-file specified\n");

	if (!grok_config(merlin_conf)) {
		fprintf(stderr, "%s contains errors. Bailing out\n", merlin_conf);
		return 1;
	}

	if (!pidfile)
		pidfile = "/var/run/merlin.pid";

	if (killing)
		return kill_daemon(pidfile);

	if (use_database && !import_program) {
		lwarn("Using database, but no import program configured. Are you sure about this?");
		lwarn("If not, make sure you specify the import_program directive in");
		lwarn("the \"daemon\" section of your merlin configuration file");
	}

	ipc.action = ipc_action_handler;
	result = ipc_init();
	if (result < 0) {
		printf("Failed to initalize ipc socket: %s\n", strerror(errno));
		return 1;
	}
	if (net_init() < 0) {
		printf("Failed to initialize networking: %s\n", strerror(errno));
		return 1;
	}

	if (!debug) {
		if (daemonize(merlin_user, NULL, pidfile, 0) < 0)
			exit(EXIT_FAILURE);

		/*
		 * we'll leak these file-descriptors, but that
		 * doesn't really matter as we just want accidental
		 * output to go somewhere where it'll be ignored
		 */
		fclose(stdin);
		open("/dev/null", O_RDONLY);
		fclose(stdout);
		open("/dev/null", O_WRONLY);
		fclose(stderr);
		open("/dev/null", O_WRONLY);
	}

	signal(SIGINT, clean_exit);
	signal(SIGTERM, clean_exit);
	signal(SIGUSR1, sigusr_handler);
	signal(SIGUSR2, sigusr_handler);

	sql_init();
	if (use_database) {
		sql_query("TRUNCATE TABLE program_status");
		sql_query("INSERT INTO program_status(instance_id, instance_name, is_running) "
		          "VALUES(0, 'Local Nagios daemon', 0)");
		for (i = 0; i < (int)num_nodes; i++) {
			char *node_name;
			merlin_node *node = noc_table[i];

			sql_quote(node->name, &node_name);
			sql_query("INSERT INTO program_status(instance_id, instance_name, is_running) "
			          "VALUES(%d, %s, 0)", node->id + 1, node_name);
			safe_free(node_name);
		}
	}
	state_init();
	linfo("Merlin daemon %s successfully initialized", merlin_version);
	polling_loop();

	clean_exit(0);

	return 0;
}