"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/bin/named/log.c" (7 Sep 2020, 6183 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


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 <config.h>
   15 
   16 #include <isc/result.h>
   17 
   18 #include <isccfg/log.h>
   19 
   20 #include <named/log.h>
   21 
   22 #ifndef ISC_FACILITY
   23 #define ISC_FACILITY LOG_DAEMON
   24 #endif
   25 
   26 /*%
   27  * When adding a new category, be sure to add the appropriate
   28  * \#define to <named/log.h> and to update the list in
   29  * bin/check/check-tool.c.
   30  */
   31 static isc_logcategory_t categories[] = {
   32     { "",               0 },
   33     { "client",         0 },
   34     { "network",            0 },
   35     { "update",         0 },
   36     { "queries",            0 },
   37     { "unmatched",          0 },
   38     { "update-security",        0 },
   39     { "query-errors",       0 },
   40     { "trust-anchor-telemetry", 0 },
   41     { NULL,             0 }
   42 };
   43 
   44 /*%
   45  * When adding a new module, be sure to add the appropriate
   46  * \#define to <dns/log.h>.
   47  */
   48 static isc_logmodule_t modules[] = {
   49     { "main",           0 },
   50     { "client",         0 },
   51     { "server",         0 },
   52     { "query",          0 },
   53     { "interfacemgr",       0 },
   54     { "update",         0 },
   55     { "xfer-in",            0 },
   56     { "xfer-out",           0 },
   57     { "notify",         0 },
   58     { "control",            0 },
   59     { "lwresd",         0 },
   60     { NULL,             0 }
   61 };
   62 
   63 isc_result_t
   64 ns_log_init(bool safe) {
   65     isc_result_t result;
   66     isc_logconfig_t *lcfg = NULL;
   67 
   68     ns_g_categories = categories;
   69     ns_g_modules = modules;
   70 
   71     /*
   72      * Setup a logging context.
   73      */
   74     result = isc_log_create(ns_g_mctx, &ns_g_lctx, &lcfg);
   75     if (result != ISC_R_SUCCESS)
   76         return (result);
   77 
   78     /*
   79      * named-checktool.c:setup_logging() needs to be kept in sync.
   80      */
   81     isc_log_registercategories(ns_g_lctx, ns_g_categories);
   82     isc_log_registermodules(ns_g_lctx, ns_g_modules);
   83     isc_log_setcontext(ns_g_lctx);
   84     dns_log_init(ns_g_lctx);
   85     dns_log_setcontext(ns_g_lctx);
   86     cfg_log_init(ns_g_lctx);
   87 
   88     if (safe)
   89         result = ns_log_setsafechannels(lcfg);
   90     else
   91         result = ns_log_setdefaultchannels(lcfg);
   92     if (result != ISC_R_SUCCESS)
   93         goto cleanup;
   94 
   95     result = ns_log_setdefaultcategory(lcfg);
   96     if (result != ISC_R_SUCCESS)
   97         goto cleanup;
   98 
   99     return (ISC_R_SUCCESS);
  100 
  101  cleanup:
  102     isc_log_destroy(&ns_g_lctx);
  103     isc_log_setcontext(NULL);
  104     dns_log_setcontext(NULL);
  105 
  106     return (result);
  107 }
  108 
  109 isc_result_t
  110 ns_log_setdefaultchannels(isc_logconfig_t *lcfg) {
  111     isc_result_t result;
  112     isc_logdestination_t destination;
  113 
  114     /*
  115      * By default, the logging library makes "default_debug" log to
  116      * stderr.  In BIND, we want to override this and log to named.run
  117      * instead, unless the -g option was given.
  118      */
  119     if (! ns_g_logstderr) {
  120         destination.file.stream = NULL;
  121         destination.file.name = "named.run";
  122         destination.file.versions = ISC_LOG_ROLLNEVER;
  123         destination.file.maximum_size = 0;
  124         result = isc_log_createchannel(lcfg, "default_debug",
  125                            ISC_LOG_TOFILE,
  126                            ISC_LOG_DYNAMIC,
  127                            &destination,
  128                            ISC_LOG_PRINTTIME|
  129                            ISC_LOG_DEBUGONLY);
  130         if (result != ISC_R_SUCCESS)
  131             goto cleanup;
  132     }
  133 
  134     if (ns_g_logfile != NULL) {
  135         destination.file.stream = NULL;
  136         destination.file.name = ns_g_logfile;
  137         destination.file.versions = ISC_LOG_ROLLNEVER;
  138         destination.file.maximum_size = 0;
  139         result = isc_log_createchannel(lcfg, "default_logfile",
  140                            ISC_LOG_TOFILE,
  141                            ISC_LOG_DYNAMIC,
  142                            &destination,
  143                            ISC_LOG_PRINTTIME|
  144                            ISC_LOG_PRINTCATEGORY|
  145                            ISC_LOG_PRINTLEVEL);
  146         if (result != ISC_R_SUCCESS)
  147             goto cleanup;
  148     }
  149 
  150 #if ISC_FACILITY != LOG_DAEMON
  151     destination.facility = ISC_FACILITY;
  152     result = isc_log_createchannel(lcfg, "default_syslog",
  153                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
  154                        &destination, 0);
  155     if (result != ISC_R_SUCCESS)
  156         goto cleanup;
  157 #endif
  158 
  159     /*
  160      * Set the initial debug level.
  161      */
  162     isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
  163 
  164     result = ISC_R_SUCCESS;
  165 
  166  cleanup:
  167     return (result);
  168 }
  169 
  170 isc_result_t
  171 ns_log_setsafechannels(isc_logconfig_t *lcfg) {
  172     isc_result_t result;
  173     isc_logdestination_t destination;
  174 
  175     if (! ns_g_logstderr) {
  176         result = isc_log_createchannel(lcfg, "default_debug",
  177                            ISC_LOG_TONULL,
  178                            ISC_LOG_DYNAMIC,
  179                            NULL, 0);
  180         if (result != ISC_R_SUCCESS)
  181             goto cleanup;
  182 
  183         /*
  184          * Setting the debug level to zero should get the output
  185          * discarded a bit faster.
  186          */
  187         isc_log_setdebuglevel(ns_g_lctx, 0);
  188     } else {
  189         isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel);
  190     }
  191 
  192     if (ns_g_logfile != NULL) {
  193         destination.file.stream = NULL;
  194         destination.file.name = ns_g_logfile;
  195         destination.file.versions = ISC_LOG_ROLLNEVER;
  196         destination.file.maximum_size = 0;
  197         result = isc_log_createchannel(lcfg, "default_logfile",
  198                            ISC_LOG_TOFILE,
  199                            ISC_LOG_DYNAMIC,
  200                            &destination,
  201                            ISC_LOG_PRINTTIME|
  202                            ISC_LOG_PRINTCATEGORY|
  203                            ISC_LOG_PRINTLEVEL);
  204         if (result != ISC_R_SUCCESS)
  205             goto cleanup;
  206     }
  207 
  208 #if ISC_FACILITY != LOG_DAEMON
  209     destination.facility = ISC_FACILITY;
  210     result = isc_log_createchannel(lcfg, "default_syslog",
  211                        ISC_LOG_TOSYSLOG, ISC_LOG_INFO,
  212                        &destination, 0);
  213     if (result != ISC_R_SUCCESS)
  214         goto cleanup;
  215 #endif
  216 
  217     result = ISC_R_SUCCESS;
  218 
  219  cleanup:
  220     return (result);
  221 }
  222 
  223 isc_result_t
  224 ns_log_setdefaultcategory(isc_logconfig_t *lcfg) {
  225     isc_result_t result = ISC_R_SUCCESS;
  226 
  227     result = isc_log_usechannel(lcfg, "default_debug",
  228                     ISC_LOGCATEGORY_DEFAULT, NULL);
  229     if (result != ISC_R_SUCCESS)
  230         goto cleanup;
  231 
  232     if (! ns_g_logstderr) {
  233         if (ns_g_logfile != NULL)
  234             result = isc_log_usechannel(lcfg, "default_logfile",
  235                             ISC_LOGCATEGORY_DEFAULT,
  236                             NULL);
  237         else if (! ns_g_nosyslog)
  238             result = isc_log_usechannel(lcfg, "default_syslog",
  239                             ISC_LOGCATEGORY_DEFAULT,
  240                             NULL);
  241     }
  242 
  243  cleanup:
  244     return (result);
  245 }
  246 
  247 isc_result_t
  248 ns_log_setunmatchedcategory(isc_logconfig_t *lcfg) {
  249     isc_result_t result;
  250 
  251     result = isc_log_usechannel(lcfg, "null",
  252                     NS_LOGCATEGORY_UNMATCHED, NULL);
  253     return (result);
  254 }
  255 
  256 void
  257 ns_log_shutdown(void) {
  258     isc_log_destroy(&ns_g_lctx);
  259     isc_log_setcontext(NULL);
  260     dns_log_setcontext(NULL);
  261 }