Esempio n. 1
0
void setup_logging (int verbose, isc_mem_t * mctx, isc_log_t ** logp)
{
    isc_result_t result;

    isc_logdestination_t destination;

    isc_logconfig_t *logconfig = NULL;

    isc_log_t *log = NULL;

    int level;

    if (verbose < 0)
        verbose = 0;
    switch (verbose)
    {
        case 0:
            /*
             * We want to see warnings about things like out-of-zone
             * data in the master file even when not verbose.
             */
            level = ISC_LOG_WARNING;
            break;
        case 1:
            level = ISC_LOG_INFO;
            break;
        default:
            level = ISC_LOG_DEBUG (verbose - 2 + 1);
            break;
    }

    RUNTIME_CHECK (isc_log_create (mctx, &log, &logconfig) == ISC_R_SUCCESS);
    isc_log_setcontext (log);
    dns_log_init (log);
    dns_log_setcontext (log);

    RUNTIME_CHECK (isc_log_settag (logconfig, program) == ISC_R_SUCCESS);

    /*
     * Set up a channel similar to default_stderr except:
     *  - the logging level is passed in
     *  - the program name and logging level are printed
     *  - no time stamp is printed
     */
    destination.file.stream = stderr;
    destination.file.name = NULL;
    destination.file.versions = ISC_LOG_ROLLNEVER;
    destination.file.maximum_size = 0;
    result = isc_log_createchannel (logconfig, "stderr",
                                    ISC_LOG_TOFILEDESC, level, &destination, ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL);
    check_result (result, "isc_log_createchannel()");

    RUNTIME_CHECK (isc_log_usechannel (logconfig, "stderr", NULL, NULL) == ISC_R_SUCCESS);

    *logp = log;
}
Esempio n. 2
0
int
main(int argc, char **argv) {
	isc_boolean_t show_final_mem = ISC_FALSE;
	isc_result_t result = ISC_R_SUCCESS;
	isc_taskmgr_t *taskmgr = NULL;
	isc_task_t *task = NULL;
	isc_log_t *log = NULL;
	isc_logconfig_t *logconfig = NULL;
	isc_logdestination_t logdest;
	cfg_parser_t *pctx = NULL;
	cfg_obj_t *config = NULL;
	const char *keyname = NULL;
	char *p;
	size_t argslen;
	int ch;
	int i;

	result = isc_file_progname(*argv, program, sizeof(program));
	if (result != ISC_R_SUCCESS)
		memcpy(program, "rndc", 5);
	progname = program;

	admin_conffile = RNDC_CONFFILE;
	admin_keyfile = RNDC_KEYFILE;

	result = isc_app_start();
	if (result != ISC_R_SUCCESS)
		fatal("isc_app_start() failed: %s", isc_result_totext(result));

	while ((ch = isc_commandline_parse(argc, argv, "c:k:Mmp:s:Vy:"))
	       != -1) {
		switch (ch) {
		case 'c':
			admin_conffile = isc_commandline_argument;
			break;

		case 'k':
			admin_keyfile = isc_commandline_argument;
			break;

		case 'M':
			isc_mem_debugging = ISC_MEM_DEBUGTRACE;
			break;

		case 'm':
			show_final_mem = ISC_TRUE;
			break;

		case 'p':
			remoteport = atoi(isc_commandline_argument);
			if (remoteport > 65535 || remoteport == 0)
				fatal("port '%s' out of range",
				      isc_commandline_argument);
			break;

		case 's':
			servername = isc_commandline_argument;
			break;
		case 'V':
			verbose = ISC_TRUE;
			break;
		case 'y':
			keyname = isc_commandline_argument;
			break;
		case '?':
			usage(0);
			break;
		default:
			fatal("unexpected error parsing command arguments: "
			      "got %c\n", ch);
			break;
		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

	if (argc < 1)
		usage(1);

	isc_random_get(&serial);

	DO("create memory context", isc_mem_create(0, 0, &mctx));
	DO("create socket manager", isc_socketmgr_create(mctx, &socketmgr));
	DO("create task manager", isc_taskmgr_create(mctx, 1, 0, &taskmgr));
	DO("create task", isc_task_create(taskmgr, 0, &task));

	DO("create logging context", isc_log_create(mctx, &log, &logconfig));
	isc_log_setcontext(log);
	DO("setting log tag", isc_log_settag(logconfig, progname));
	logdest.file.stream = stderr;
	logdest.file.name = NULL;
	logdest.file.versions = ISC_LOG_ROLLNEVER;
	logdest.file.maximum_size = 0;
	DO("creating log channel",
	   isc_log_createchannel(logconfig, "stderr",
		   		 ISC_LOG_TOFILEDESC, ISC_LOG_INFO, &logdest,
				 ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL));
	DO("enabling log channel", isc_log_usechannel(logconfig, "stderr",
						      NULL, NULL));

	parse_config(mctx, log, keyname, &pctx, &config);

	isccc_result_register();

	command = *argv;

	/*
	 * Convert argc/argv into a space-delimited command string
	 * similar to what the user might enter in interactive mode
	 * (if that were implemented).
	 */
	argslen = 0;
	for (i = 0; i < argc; i++)
		argslen += strlen(argv[i]) + 1;

	args = isc_mem_get(mctx, argslen);
	if (args == NULL)
		DO("isc_mem_get", ISC_R_NOMEMORY);

	p = args;
	for (i = 0; i < argc; i++) {
		size_t len = strlen(argv[i]);
		memcpy(p, argv[i], len);
		p += len;
		*p++ = ' ';
	}

	p--;
	*p++ = '\0';
	INSIST(p == args + argslen);

	notify("%s", command);

	if (strcmp(command, "restart") == 0)
		fatal("'%s' is not implemented", command);

	DO("post event", isc_app_onrun(mctx, task, rndc_start, NULL));

	result = isc_app_run();
	if (result != ISC_R_SUCCESS)
		fatal("isc_app_run() failed: %s", isc_result_totext(result));

	if (connects > 0 || sends > 0 || recvs > 0)
		isc_socket_cancel(sock, task, ISC_SOCKCANCEL_ALL);

	isc_task_detach(&task);
	isc_taskmgr_destroy(&taskmgr);
	isc_socketmgr_destroy(&socketmgr);
	isc_log_destroy(&log);
	isc_log_setcontext(NULL);

	cfg_obj_destroy(pctx, &config);
	cfg_parser_destroy(&pctx);

	isc_mem_put(mctx, args, argslen);
	isccc_ccmsg_invalidate(&ccmsg);

	if (show_final_mem)
		isc_mem_stats(mctx, stderr);

	isc_mem_destroy(&mctx);

	if (failed)
		return (1);

	return (0);
}
Esempio n. 3
0
int
main(int argc, char **argv) {
	UNUSED(argc);
	UNUSED(argv);

	rsa = RSA_new();
	e = BN_new();
	pkey = EVP_PKEY_new();

	if ((rsa == NULL) || (e == NULL) || (pkey == NULL) ||
	    !EVP_PKEY_set1_RSA(pkey, rsa)) {
		fprintf(stderr, "fatal error: basic OpenSSL failure\n");
		exit(1);
	}

	/* e = 0x1000000000001 */
	BN_set_bit(e, 0);
	BN_set_bit(e, 48);

	if (RSA_generate_key_ex(rsa, bits, e, NULL)) {
		BN_free(e);
		RSA_free(rsa);
	} else {
		fprintf(stderr,
			"fatal error: RSA_generate_key_ex() fails "
			"at file %s line %d\n",
			__FILE__, __LINE__);
		exit(1);
	}

	dns_result_register();

	CHECK(isc_mem_create(0, 0, &mctx), "isc_mem_create()");
	CHECK(isc_entropy_create(mctx, &ectx), "isc_entropy_create()");
	CHECK(isc_entropy_usebestsource(ectx, &source,
					"../random.data",
					ISC_ENTROPY_KEYBOARDNO),
	      "isc_entropy_usebestsource(\"../random.data\")");
	CHECK(dst_lib_init2(mctx, ectx, NULL, 0), "dst_lib_init2()");
	CHECK(isc_log_create(mctx, &log_, &logconfig), "isc_log_create()");
	isc_log_setcontext(log_);
	dns_log_init(log_);
	dns_log_setcontext(log_);
	CHECK(isc_log_settag(logconfig, "bigkey"), "isc_log_settag()");
	destination.file.stream = stderr;
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;
	CHECK(isc_log_createchannel(logconfig, "stderr",
				    ISC_LOG_TOFILEDESC,
				    level,
				    &destination,
				    ISC_LOG_PRINTTAG | ISC_LOG_PRINTLEVEL),
	      "isc_log_createchannel()");
	CHECK(isc_log_usechannel(logconfig, "stderr", NULL, NULL),
	      "isc_log_usechannel()");
	dns_fixedname_init(&fname);
	name = dns_fixedname_name(&fname);
	isc_buffer_constinit(&buf, "example.", strlen("example."));
	isc_buffer_add(&buf, strlen("example."));
	CHECK(dns_name_fromtext(name, &buf, dns_rootname, 0, NULL),
	      "dns_name_fromtext(\"example.\")");

	CHECK(dst_key_buildinternal(name, DNS_KEYALG_RSASHA1,
				    bits, DNS_KEYOWNER_ZONE,
				    DNS_KEYPROTO_DNSSEC, dns_rdataclass_in,
				    pkey, mctx, &key),
	      "dst_key_buildinternal(...)");

	CHECK(dst_key_tofile(key, DST_TYPE_PRIVATE | DST_TYPE_PUBLIC, NULL),
	      "dst_key_tofile()");
	isc_buffer_init(&buf, filename, sizeof(filename) - 1);
	isc_buffer_clear(&buf);
	CHECK(dst_key_buildfilename(key, 0, NULL, &buf),
	      "dst_key_buildfilename()");
	printf("%s\n", filename);
	dst_key_free(&key);

	isc_log_destroy(&log_);
	isc_log_setcontext(NULL);
	dns_log_setcontext(NULL);
	if (source != NULL)
		isc_entropy_destroysource(&source);
	isc_entropy_detach(&ectx);
	dst_lib_destroy();
	dns_name_destroy();
	isc_mem_destroy(&mctx);
	return (0);
}
Esempio n. 4
0
int
main(int argc, char **argv) {
    const char *progname, *syslog_file, *message;
    int ch, i, file_versions, stderr_line;
    isc_boolean_t show_final_mem = ISC_FALSE;
    isc_log_t *lctx;
    isc_logconfig_t *lcfg;
    isc_mem_t *mctx;
    isc_result_t result;
    isc_logdestination_t destination;
    const isc_logcategory_t *category;
    const isc_logmodule_t *module;

    progname = strrchr(*argv, '/');
    if (progname != NULL)
        progname++;
    else
        progname = *argv;

    syslog_file = SYSLOG_FILE;
    file_versions = FILE_VERSIONS;

    while ((ch = isc_commandline_parse(argc, argv, "ms:r:")) != -1) {
        switch (ch) {
        case 'm':
            show_final_mem = ISC_TRUE;
            break;
        case 's':
            syslog_file = isc_commandline_argument;
            break;
        case 'r':
            file_versions = atoi(isc_commandline_argument);
            if (file_versions < 0 &&
                    file_versions != ISC_LOG_ROLLNEVER &&
                    file_versions != ISC_LOG_ROLLINFINITE) {
                fprintf(stderr, "%s: file rotations must be "
                        "%d (ISC_LOG_ROLLNEVER),\n\t"
                        "%d (ISC_LOG_ROLLINFINITE) "
                        "or > 0\n", progname,
                        ISC_LOG_ROLLNEVER,
                        ISC_LOG_ROLLINFINITE);
                exit(1);
            }
            break;
        case '?':
            fprintf(stderr, usage, progname);
            exit(1);
        }
    }

    argc -= isc_commandline_index;
    argv += isc_commandline_index;

    if (argc > 0) {
        fprintf(stderr, usage, progname);
        exit(1);
    }

    fprintf(stderr, "EXPECT:\n%s%d%s%s%s",
            "8 lines to stderr (first 4 numbered, #3 repeated)\n",
            file_versions == 0 || file_versions == ISC_LOG_ROLLNEVER ? 1 :
            file_versions > 0 ? file_versions + 1 : FILE_VERSIONS + 1,
            " " TEST_FILE " files, and\n",
            "2 lines to syslog\n",
            "lines ending with exclamation marks are errors\n\n");

    isc_log_opensyslog(progname, LOG_PID, LOG_DAEMON);

    mctx = NULL;
    lctx = NULL;
    lcfg = NULL;

    CHECK(isc_mem_create(0, 0, &mctx));
    CHECK(isc_log_create(mctx, &lctx, &lcfg));

    CHECK(isc_log_settag(lcfg, progname));

    isc_log_setcontext(lctx);
    dns_log_init(lctx);
    dns_log_setcontext(lctx);

    /*
     * Test isc_log_categorybyname and isc_log_modulebyname.
     */
    category = isc_log_categorybyname(lctx, "notify");
    if (category != NULL)
        fprintf(stderr, "%s category found. (expected)\n",
                category->name);
    else
        fprintf(stderr, "notify category not found!\n");

    module = isc_log_modulebyname(lctx, "xyzzy");
    if (module != NULL)
        fprintf(stderr, "%s module found!\n", module->name);
    else
        fprintf(stderr, "xyzzy module not found. (expected)\n");

    /*
     * Create a file channel to test file opening, size limiting and
     * version rolling.
     */

    destination.file.name = TEST_FILE;
    destination.file.maximum_size = 1;
    destination.file.versions = file_versions;

    CHECK(isc_log_createchannel(lcfg, "file_test", ISC_LOG_TOFILE,
                                ISC_LOG_INFO, &destination,
                                ISC_LOG_PRINTTIME|
                                ISC_LOG_PRINTTAG|
                                ISC_LOG_PRINTLEVEL|
                                ISC_LOG_PRINTCATEGORY|
                                ISC_LOG_PRINTMODULE));

    /*
     * Create a dynamic debugging channel to a file descriptor.
     */
    destination.file.stream = stderr;

    CHECK(isc_log_createchannel(lcfg, "debug_test", ISC_LOG_TOFILEDESC,
                                ISC_LOG_DYNAMIC, &destination,
                                ISC_LOG_PRINTTIME|
                                ISC_LOG_PRINTLEVEL|
                                ISC_LOG_DEBUGONLY));

    /*
     * Test the usability of the four predefined logging channels.
     */
    CHECK(isc_log_usechannel(lcfg, "default_syslog",
                             DNS_LOGCATEGORY_DATABASE,
                             DNS_LOGMODULE_CACHE));
    CHECK(isc_log_usechannel(lcfg, "default_stderr",
                             DNS_LOGCATEGORY_DATABASE,
                             DNS_LOGMODULE_CACHE));
    CHECK(isc_log_usechannel(lcfg, "default_debug",
                             DNS_LOGCATEGORY_DATABASE,
                             DNS_LOGMODULE_CACHE));
    CHECK(isc_log_usechannel(lcfg, "null",
                             DNS_LOGCATEGORY_DATABASE,
                             NULL));

    /*
     * Use the custom channels.
     */
    CHECK(isc_log_usechannel(lcfg, "file_test",
                             DNS_LOGCATEGORY_GENERAL,
                             DNS_LOGMODULE_DB));

    CHECK(isc_log_usechannel(lcfg, "debug_test",
                             DNS_LOGCATEGORY_GENERAL,
                             DNS_LOGMODULE_RBTDB));

    fprintf(stderr, "\n==> stderr begin\n");

    /*
     * Write to the internal default by testing both a category for which
     * no channel has been specified and a category which was specified
     * but not with the named module.
     */
    stderr_line = 1;

    isc_log_write(lctx, DNS_LOGCATEGORY_SECURITY, DNS_LOGMODULE_RBT,
                  ISC_LOG_CRITICAL, "%s (%d)",
                  "Unspecified category and unspecified module to stderr",
                  stderr_line++);
    isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBT,
                  ISC_LOG_CRITICAL, "%s (%d)",
                  "Specified category and unspecified module to stderr",
                  stderr_line++);

    /*
     * Write to default_syslog, default_stderr and default_debug.
     */
    isc_log_write(lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
                  ISC_LOG_WARNING, "%s (%d twice)",
                  "Using the predefined channels to syslog+stderr",
                  stderr_line++);

    /*
     * Write to predefined null channel.
     */
    isc_log_write(lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_RBTDB,
                  ISC_LOG_INFO, "This is to null and should not appear!");

    /*
     * Reset the internal default to use syslog instead of stderr,
     * and test it.
     */
    CHECK(isc_log_usechannel(lcfg, "default_syslog",
                             ISC_LOGCATEGORY_DEFAULT, NULL));
    isc_log_write(lctx, DNS_LOGCATEGORY_SECURITY, DNS_LOGMODULE_RBT,
                  ISC_LOG_ERROR, "%s%s",
                  "This message to the redefined default category should ",
                  "be second in syslog");
    /*
     * Write to the file channel.
     */
    if (file_versions >= 0 || file_versions == ISC_LOG_ROLLINFINITE) {

        /*
         * If file_versions is 0 or ISC_LOG_ROLLINFINITE, write
         * the "should not appear" and "should be in file" messages
         * to ensure they get rolled.
         */
        if (file_versions <= 0)
            file_versions = FILE_VERSIONS;

        else
            isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL,
                          DNS_LOGMODULE_DB, ISC_LOG_NOTICE,
                          "This should be rolled over "
                          "and not appear!");

        for (i = file_versions - 1; i >= 0; i--)
            isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL,
                          DNS_LOGMODULE_DB, ISC_LOG_NOTICE,
                          "should be in file %d/%d", i,
                          file_versions - 1);

        isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL,
                      DNS_LOGMODULE_DB, ISC_LOG_NOTICE,
                      "should be in base file");
    } else {
        file_versions = FILE_VERSIONS;
        for (i = 1; i <= file_versions; i++)
            isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL,
                          DNS_LOGMODULE_DB, ISC_LOG_NOTICE,
                          "This is message %d in the log file", i);
    }


    /*
     * Write a debugging message to a category that has no
     * debugging channels for the named module.
     */
    isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_DB,
                  ISC_LOG_DEBUG(1),
                  "This debug message should not appear!");

    /*
     * Write debugging messages to a dynamic debugging channel.
     */
    isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                  ISC_LOG_CRITICAL, "This critical message should "
                  "not appear because the debug level is 0!");

    isc_log_setdebuglevel(lctx, 3);

    isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                  ISC_LOG_DEBUG(1), "%s (%d)",
                  "Dynamic debugging to stderr", stderr_line++);
    isc_log_write(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                  ISC_LOG_DEBUG(5),
                  "This debug level is too high and should not appear!");

    /*
     * Test out the duplicate filtering using the debug_test channel.
     */
    isc_log_setduplicateinterval(lcfg, 10);
    message = "This message should appear only once on stderr";

    isc_log_write1(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                   ISC_LOG_CRITICAL, "%s", message);
    isc_log_write1(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                   ISC_LOG_CRITICAL, message);

    isc_log_setduplicateinterval(lcfg, 1);
    message = "This message should appear twice on stderr";

    isc_log_write1(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                   ISC_LOG_CRITICAL, message);
    sleep(2);
    isc_log_write1(lctx, DNS_LOGCATEGORY_GENERAL, DNS_LOGMODULE_RBTDB,
                   ISC_LOG_CRITICAL, message);

    /*
     * Review where everything went.
     * XXXDCL NT
     */
    fputc('\n', stderr);
    system("head " TEST_FILE "*; rm -f " TEST_FILE "*");

    freopen(syslog_file, "r", stdin);
    fprintf(stderr, "\n==> %s <==\n", syslog_file);
    system("tail -2");
    fputc('\n', stderr);

    isc_log_destroy(&lctx);

    if (show_final_mem)
        isc_mem_stats(mctx, stderr);

    return (0);
}
Esempio n. 5
0
int
main(int argc, char **argv) {
	isc_result_t result = ISC_R_SUCCESS;
	isc_boolean_t show_final_mem = ISC_FALSE;
	isc_taskmgr_t *taskmgr = NULL;
	isc_task_t *task = NULL;
	isc_log_t *log = NULL;
	isc_logconfig_t *logconfig = NULL;
	isc_logdestination_t logdest;
	cfg_parser_t *pctx = NULL;
	cfg_obj_t *config = NULL;
	const char *keyname = NULL;
	struct in_addr in;
	struct in6_addr in6;
	char *p;
	size_t argslen;
	int ch;
	int i;

	result = isc_file_progname(*argv, program, sizeof(program));
	if (result != ISC_R_SUCCESS)
		memmove(program, "rndc", 5);
	progname = program;

	admin_conffile = RNDC_CONFFILE;
	admin_keyfile = RNDC_KEYFILE;

	isc_sockaddr_any(&local4);
	isc_sockaddr_any6(&local6);

	result = isc_app_start();
	if (result != ISC_R_SUCCESS)
		fatal("isc_app_start() failed: %s", isc_result_totext(result));

	isc_commandline_errprint = ISC_FALSE;

	while ((ch = isc_commandline_parse(argc, argv, "b:c:hk:Mmp:qrs:Vy:"))
	       != -1) {
		switch (ch) {
		case 'b':
			if (inet_pton(AF_INET, isc_commandline_argument,
				      &in) == 1) {
				isc_sockaddr_fromin(&local4, &in, 0);
				local4set = ISC_TRUE;
			} else if (inet_pton(AF_INET6, isc_commandline_argument,
					     &in6) == 1) {
				isc_sockaddr_fromin6(&local6, &in6, 0);
				local6set = ISC_TRUE;
			}
			break;

		case 'c':
			admin_conffile = isc_commandline_argument;
			c_flag = ISC_TRUE;
			break;

		case 'k':
			admin_keyfile = isc_commandline_argument;
			break;

		case 'M':
			isc_mem_debugging = ISC_MEM_DEBUGTRACE;
			break;

		case 'm':
			show_final_mem = ISC_TRUE;
			break;

		case 'p':
			remoteport = atoi(isc_commandline_argument);
			if (remoteport > 65535 || remoteport == 0)
				fatal("port '%s' out of range",
				      isc_commandline_argument);
			break;

		case 'q':
			quiet = ISC_TRUE;
			break;

		case 'r':
			showresult = ISC_TRUE;
			break;

		case 's':
			servername = isc_commandline_argument;
			break;

		case 'V':
			verbose = ISC_TRUE;
			break;

		case 'y':
			keyname = isc_commandline_argument;
			break;

		case '?':
			if (isc_commandline_option != '?') {
				fprintf(stderr, "%s: invalid argument -%c\n",
					program, isc_commandline_option);
				usage(1);
			}
			/* FALLTHROUGH */
		case 'h':
			usage(0);
			break;
		default:
			fprintf(stderr, "%s: unhandled option -%c\n",
				program, isc_commandline_option);
			exit(1);
		}
	}

	argc -= isc_commandline_index;
	argv += isc_commandline_index;

	if (argc < 1)
		usage(1);

	isc_random_get(&serial);

	DO("create memory context", isc_mem_create(0, 0, &rndc_mctx));
	DO("create socket manager", isc_socketmgr_create(rndc_mctx, &socketmgr));
	DO("create task manager", isc_taskmgr_create(rndc_mctx, 1, 0, &taskmgr));
	DO("create task", isc_task_create(taskmgr, 0, &task));

	DO("create logging context", isc_log_create(rndc_mctx, &log, &logconfig));
	isc_log_setcontext(log);
	DO("setting log tag", isc_log_settag(logconfig, progname));
	logdest.file.stream = stderr;
	logdest.file.name = NULL;
	logdest.file.versions = ISC_LOG_ROLLNEVER;
	logdest.file.maximum_size = 0;
	DO("creating log channel",
	   isc_log_createchannel(logconfig, "stderr",
				 ISC_LOG_TOFILEDESC, ISC_LOG_INFO, &logdest,
				 ISC_LOG_PRINTTAG|ISC_LOG_PRINTLEVEL));
	DO("enabling log channel", isc_log_usechannel(logconfig, "stderr",
						      NULL, NULL));

	parse_config(rndc_mctx, log, keyname, &pctx, &config);

	isccc_result_register();

	command = *argv;

	DO("allocate data buffer",
	   isc_buffer_allocate(rndc_mctx, &databuf, 2048));

	/*
	 * Convert argc/argv into a space-delimited command string
	 * similar to what the user might enter in interactive mode
	 * (if that were implemented).
	 */
	argslen = 0;
	for (i = 0; i < argc; i++)
		argslen += strlen(argv[i]) + 1;

	args = isc_mem_get(rndc_mctx, argslen);
	if (args == NULL)
		DO("isc_mem_get", ISC_R_NOMEMORY);

	p = args;
	for (i = 0; i < argc; i++) {
		size_t len = strlen(argv[i]);
		memmove(p, argv[i], len);
		p += len;
		*p++ = ' ';
	}

	p--;
	*p++ = '\0';
	INSIST(p == args + argslen);

	notify("%s", command);

	if (strcmp(command, "restart") == 0)
		fatal("'%s' is not implemented", command);

	if (nserveraddrs == 0)
		get_addresses(servername, (in_port_t) remoteport);

	DO("post event", isc_app_onrun(rndc_mctx, task, rndc_start, NULL));

	result = isc_app_run();
	if (result != ISC_R_SUCCESS)
		fatal("isc_app_run() failed: %s", isc_result_totext(result));

	if (connects > 0 || sends > 0 || recvs > 0)
		isc_socket_cancel(sock, task, ISC_SOCKCANCEL_ALL);

	isc_task_detach(&task);
	isc_taskmgr_destroy(&taskmgr);
	isc_socketmgr_destroy(&socketmgr);
	isc_log_destroy(&log);
	isc_log_setcontext(NULL);

	cfg_obj_destroy(pctx, &config);
	cfg_parser_destroy(&pctx);

	isc_mem_put(rndc_mctx, args, argslen);
	isccc_ccmsg_invalidate(&ccmsg);

	dns_name_destroy();

	isc_buffer_free(&databuf);

	if (show_final_mem)
		isc_mem_stats(rndc_mctx, stderr);

	isc_mem_destroy(&rndc_mctx);

	if (failed)
		return (1);

	return (0);
}
Esempio n. 6
0
static void
setup_logging(FILE *errout) {
	isc_result_t result;
	isc_logdestination_t destination;
	isc_logconfig_t *logconfig = NULL;

	result = isc_log_create(mctx, &lctx, &logconfig);
	if (result != ISC_R_SUCCESS)
		fatal("Couldn't set up logging");

	isc_log_registercategories(lctx, categories);
	isc_log_registermodules(lctx, modules);
	isc_log_setcontext(lctx);
	dns_log_init(lctx);
	dns_log_setcontext(lctx);
	cfg_log_init(lctx);

	destination.file.stream = errout;
	destination.file.name = NULL;
	destination.file.versions = ISC_LOG_ROLLNEVER;
	destination.file.maximum_size = 0;

	result = isc_log_createchannel(logconfig, "stderr",
				       ISC_LOG_TOFILEDESC, ISC_LOG_DYNAMIC,
				       &destination, ISC_LOG_PRINTPREFIX);
	if (result != ISC_R_SUCCESS)
		fatal("Couldn't set up log channel 'stderr'");

	isc_log_setdebuglevel(lctx, loglevel);

	result = isc_log_settag(logconfig, ";; ");
	if (result != ISC_R_SUCCESS)
		fatal("Couldn't set log tag");

	result = isc_log_usechannel(logconfig, "stderr",
				    ISC_LOGCATEGORY_DEFAULT, NULL);
	if (result != ISC_R_SUCCESS)
		fatal("Couldn't attach to log channel 'stderr'");

	if (resolve_trace && loglevel < 1) {
		result = isc_log_createchannel(logconfig, "resolver",
					       ISC_LOG_TOFILEDESC,
					       ISC_LOG_DEBUG(1),
					       &destination,
					       ISC_LOG_PRINTPREFIX);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't set up log channel 'resolver'");

		result = isc_log_usechannel(logconfig, "resolver",
					    DNS_LOGCATEGORY_RESOLVER,
					    DNS_LOGMODULE_RESOLVER);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't attach to log channel 'resolver'");
	}

	if (validator_trace && loglevel < 3) {
		result = isc_log_createchannel(logconfig, "validator",
					       ISC_LOG_TOFILEDESC,
					       ISC_LOG_DEBUG(3),
					       &destination,
					       ISC_LOG_PRINTPREFIX);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't set up log channel 'validator'");

		result = isc_log_usechannel(logconfig, "validator",
					    DNS_LOGCATEGORY_DNSSEC,
					    DNS_LOGMODULE_VALIDATOR);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't attach to log channel 'validator'");
	}

	if (message_trace && loglevel < 10) {
		result = isc_log_createchannel(logconfig, "messages",
					       ISC_LOG_TOFILEDESC,
					       ISC_LOG_DEBUG(10),
					       &destination,
					       ISC_LOG_PRINTPREFIX);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't set up log channel 'messages'");

		result = isc_log_usechannel(logconfig, "messages",
					    DNS_LOGCATEGORY_RESOLVER,
					    DNS_LOGMODULE_PACKETS);
		if (result != ISC_R_SUCCESS)
			fatal("Couldn't attach to log channel 'messagse'");
	}
}