"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.17.5/bin/named/log.c" (4 Sep 2020, 5363 Bytes) of package /linux/misc/dns/bind9/9.17.5/bind-9.17.5.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "log.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /*! \file */
   13 
   14 #include <isc/result.h>
   15 
   16 #include <dns/log.h>
   17 
   18 #include <isccfg/log.h>
   19 
   20 #include <ns/log.h>
   21 
   22 #include <named/log.h>
   23 
   24 #ifndef ISC_FACILITY
   25 #define ISC_FACILITY LOG_DAEMON
   26 #endif /* ifndef ISC_FACILITY */
   27 
   28 /*%
   29  * When adding a new category, be sure to add the appropriate
   30  * \#define to <named/log.h> and to update the list in
   31  * bin/check/check-tool.c.
   32  */
   33 static isc_logcategory_t categories[] = { { "", 0 },
   34                       { "unmatched", 0 },
   35                       { NULL, 0 } };
   36 
   37 /*%
   38  * When adding a new module, be sure to add the appropriate
   39  * \#define to <dns/log.h>.
   40  */
   41 static isc_logmodule_t modules[] = {
   42     { "main", 0 }, { "server", 0 }, { "control", 0 }, { NULL, 0 }
   43 };
   44 
   45 isc_result_t
   46 named_log_init(bool safe) {
   47     isc_result_t result;
   48     isc_logconfig_t *lcfg = NULL;
   49 
   50     named_g_categories = categories;
   51     named_g_modules = modules;
   52 
   53     /*
   54      * Setup a logging context.
   55      */
   56     isc_log_create(named_g_mctx, &named_g_lctx, &lcfg);
   57 
   58     /*
   59      * named-checktool.c:setup_logging() needs to be kept in sync.
   60      */
   61     isc_log_registercategories(named_g_lctx, named_g_categories);
   62     isc_log_registermodules(named_g_lctx, named_g_modules);
   63     isc_log_setcontext(named_g_lctx);
   64     dns_log_init(named_g_lctx);
   65     dns_log_setcontext(named_g_lctx);
   66     cfg_log_init(named_g_lctx);
   67     ns_log_init(named_g_lctx);
   68     ns_log_setcontext(named_g_lctx);
   69 
   70     if (safe) {
   71         named_log_setsafechannels(lcfg);
   72     } else {
   73         named_log_setdefaultchannels(lcfg);
   74     }
   75 
   76     result = named_log_setdefaultcategory(lcfg);
   77     if (result != ISC_R_SUCCESS) {
   78         goto cleanup;
   79     }
   80 
   81     return (ISC_R_SUCCESS);
   82 
   83 cleanup:
   84     isc_log_destroy(&named_g_lctx);
   85     isc_log_setcontext(NULL);
   86     dns_log_setcontext(NULL);
   87 
   88     return (result);
   89 }
   90 
   91 void
   92 named_log_setdefaultchannels(isc_logconfig_t *lcfg) {
   93     isc_logdestination_t destination;
   94 
   95     /*
   96      * By default, the logging library makes "default_debug" log to
   97      * stderr.  In BIND, we want to override this and log to named.run
   98      * instead, unless the -g option was given.
   99      */
  100     if (!named_g_logstderr) {
  101         destination.file.stream = NULL;
  102         destination.file.name = "named.run";
  103         destination.file.versions = ISC_LOG_ROLLNEVER;
  104         destination.file.maximum_size = 0;
  105         isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TOFILE,
  106                       ISC_LOG_DYNAMIC, &destination,
  107                       ISC_LOG_PRINTTIME | ISC_LOG_DEBUGONLY);
  108     }
  109 
  110     if (named_g_logfile != NULL) {
  111         destination.file.stream = NULL;
  112         destination.file.name = named_g_logfile;
  113         destination.file.versions = ISC_LOG_ROLLNEVER;
  114         destination.file.maximum_size = 0;
  115         isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
  116                       ISC_LOG_DYNAMIC, &destination,
  117                       ISC_LOG_PRINTTIME |
  118                           ISC_LOG_PRINTCATEGORY |
  119                           ISC_LOG_PRINTLEVEL);
  120     }
  121 
  122 #if ISC_FACILITY != LOG_DAEMON
  123     destination.facility = ISC_FACILITY;
  124     isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
  125                   ISC_LOG_INFO, &destination, 0);
  126 #endif /* if ISC_FACILITY != LOG_DAEMON */
  127 
  128     /*
  129      * Set the initial debug level.
  130      */
  131     isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
  132 }
  133 
  134 void
  135 named_log_setsafechannels(isc_logconfig_t *lcfg) {
  136     isc_logdestination_t destination;
  137 
  138     if (!named_g_logstderr) {
  139         isc_log_createchannel(lcfg, "default_debug", ISC_LOG_TONULL,
  140                       ISC_LOG_DYNAMIC, NULL, 0);
  141 
  142         /*
  143          * Setting the debug level to zero should get the output
  144          * discarded a bit faster.
  145          */
  146         isc_log_setdebuglevel(named_g_lctx, 0);
  147     } else {
  148         isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
  149     }
  150 
  151     if (named_g_logfile != NULL) {
  152         destination.file.stream = NULL;
  153         destination.file.name = named_g_logfile;
  154         destination.file.versions = ISC_LOG_ROLLNEVER;
  155         destination.file.maximum_size = 0;
  156         isc_log_createchannel(lcfg, "default_logfile", ISC_LOG_TOFILE,
  157                       ISC_LOG_DYNAMIC, &destination,
  158                       ISC_LOG_PRINTTIME |
  159                           ISC_LOG_PRINTCATEGORY |
  160                           ISC_LOG_PRINTLEVEL);
  161     }
  162 
  163 #if ISC_FACILITY != LOG_DAEMON
  164     destination.facility = ISC_FACILITY;
  165     isc_log_createchannel(lcfg, "default_syslog", ISC_LOG_TOSYSLOG,
  166                   ISC_LOG_INFO, &destination, 0);
  167 #endif /* if ISC_FACILITY != LOG_DAEMON */
  168 }
  169 
  170 isc_result_t
  171 named_log_setdefaultcategory(isc_logconfig_t *lcfg) {
  172     isc_result_t result = ISC_R_SUCCESS;
  173 
  174     result = isc_log_usechannel(lcfg, "default_debug",
  175                     ISC_LOGCATEGORY_DEFAULT, NULL);
  176     if (result != ISC_R_SUCCESS) {
  177         goto cleanup;
  178     }
  179 
  180     if (!named_g_logstderr) {
  181         if (named_g_logfile != NULL) {
  182             result = isc_log_usechannel(lcfg, "default_logfile",
  183                             ISC_LOGCATEGORY_DEFAULT,
  184                             NULL);
  185         } else if (!named_g_nosyslog) {
  186             result = isc_log_usechannel(lcfg, "default_syslog",
  187                             ISC_LOGCATEGORY_DEFAULT,
  188                             NULL);
  189         }
  190     }
  191 
  192 cleanup:
  193     return (result);
  194 }
  195 
  196 isc_result_t
  197 named_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
  198     isc_result_t result;
  199 
  200     result = isc_log_usechannel(lcfg, "null", NAMED_LOGCATEGORY_UNMATCHED,
  201                     NULL);
  202     return (result);
  203 }
  204 
  205 void
  206 named_log_shutdown(void) {
  207     isc_log_destroy(&named_g_lctx);
  208     isc_log_setcontext(NULL);
  209     dns_log_setcontext(NULL);
  210 }