diff --git a/exec/logsys.c b/exec/logsys.c index dc1d101a..b6b16cb8 100644 --- a/exec/logsys.c +++ b/exec/logsys.c @@ -1,734 +1,752 @@ /* * Copyright (c) 2002-2004 MontaVista Software, Inc. * Copyright (c) 2006-2010 Red Hat, Inc. * * Author: Steven Dake (sdake@redhat.com) * Author: Lon Hohberger (lhh@redhat.com) * Author: Fabio M. Di Nitto (fdinitto@redhat.com) * * All rights reserved. * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the MontaVista Software, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include /* * syslog prioritynames, facility names to value mapping * Some C libraries build this in to their headers, but it is non-portable * so logsys supplies its own version. */ struct syslog_names { const char *c_name; int c_val; }; static struct syslog_names prioritynames[] = { { "alert", LOG_ALERT }, { "crit", LOG_CRIT }, { "debug", LOG_DEBUG }, { "emerg", LOG_EMERG }, { "err", LOG_ERR }, { "error", LOG_ERR }, { "info", LOG_INFO }, { "notice", LOG_NOTICE }, { "warning", LOG_WARNING }, { NULL, -1 } }; static struct syslog_names facilitynames[] = { { "auth", LOG_AUTH }, { "cron", LOG_CRON }, { "daemon", LOG_DAEMON }, { "kern", LOG_KERN }, { "lpr", LOG_LPR }, { "mail", LOG_MAIL }, { "news", LOG_NEWS }, { "syslog", LOG_SYSLOG }, { "user", LOG_USER }, { "uucp", LOG_UUCP }, { "local0", LOG_LOCAL0 }, { "local1", LOG_LOCAL1 }, { "local2", LOG_LOCAL2 }, { "local3", LOG_LOCAL3 }, { "local4", LOG_LOCAL4 }, { "local5", LOG_LOCAL5 }, { "local6", LOG_LOCAL6 }, { "local7", LOG_LOCAL7 }, { NULL, -1 } }; #define MAX_FILES_PER_SUBSYS 16 /* * need unlogical order to preserve 64bit alignment */ struct logsys_logger { char subsys[LOGSYS_MAX_SUBSYS_NAMELEN]; /* subsystem name */ char *logfile; /* log to file */ unsigned int mode; /* subsystem mode */ unsigned int debug; /* debug on|off */ int syslog_priority; /* priority */ int logfile_priority; /* priority to file */ int init_status; /* internal field to handle init queues for subsystems */ int32_t target_id; char *files[MAX_FILES_PER_SUBSYS]; int32_t file_idx; int32_t dirty; }; /* values for logsys_logger init_status */ #define LOGSYS_LOGGER_INIT_DONE 0 #define LOGSYS_LOGGER_NEEDS_INIT 1 static int logsys_system_needs_init = LOGSYS_LOGGER_NEEDS_INIT; static struct logsys_logger logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT + 1]; static pthread_mutex_t logsys_config_mutex = PTHREAD_MUTEX_INITIALIZER; static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode); static void _logsys_config_apply_per_file(int32_t s, const char *filename); static void _logsys_config_apply_per_subsys(int32_t s); static char *format_buffer=NULL; static int _logsys_config_subsys_get_unlocked (const char *subsys) { unsigned int i; if (!subsys) { return LOGSYS_MAX_SUBSYS_COUNT; } for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { if (strcmp (logsys_loggers[i].subsys, subsys) == 0) { return i; } } return (-1); } /* * we need a version that can work when somebody else is already * holding a config mutex lock or we will never get out of here */ static int logsys_config_file_set_unlocked ( int subsysid, const char **error_string, const char *file) { static char error_string_response[512]; int i; if (logsys_loggers[subsysid].target_id > 0) { /* TODO close file logsys_filter_apply(subsysid, QB_LOG_FILTER_REMOVE, logsys_loggers[subsysid].target_id); */ } logsys_loggers[subsysid].dirty = QB_TRUE; - if ((file == NULL) || - (strcmp(logsys_loggers[subsysid].subsys, "") == 0)) { + if (file == NULL) { return (0); } if (strlen(file) >= PATH_MAX) { snprintf (error_string_response, sizeof(error_string_response), "%s: logfile name exceed maximum system filename lenght\n", logsys_loggers[subsysid].subsys); *error_string = error_string_response; return (-1); } for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { if ((logsys_loggers[i].logfile != NULL) && (strcmp (logsys_loggers[i].logfile, file) == 0) && (i != subsysid)) { /* we have found another subsys with this config file * so add a filter */ logsys_loggers[subsysid].target_id = logsys_loggers[i].target_id; return (0); } } logsys_loggers[subsysid].logfile = strdup(file); if (logsys_loggers[subsysid].logfile == NULL) { snprintf (error_string_response, sizeof(error_string_response), "Unable to allocate memory for logfile '%s'\n", file); *error_string = error_string_response; return (-1); } if (logsys_loggers[subsysid].target_id > 0) { /* no one else is using this close it */ qb_log_file_close(logsys_loggers[subsysid].target_id); } logsys_loggers[subsysid].target_id = qb_log_file_open(file); if (logsys_loggers[subsysid].target_id < 0) { int err = logsys_loggers[subsysid].target_id; char error_str[LOGSYS_MAX_PERROR_MSG_LEN]; const char *error_ptr; error_ptr = qb_strerror_r(err, error_str, sizeof(error_str)); free(logsys_loggers[subsysid].logfile); logsys_loggers[subsysid].logfile = NULL; snprintf (error_string_response, sizeof(error_string_response), "Can't open logfile '%s' for reason: %s (%d).\n", file, error_ptr, err); *error_string = error_string_response; return (-1); } + qb_log_format_set(logsys_loggers[subsysid].target_id, format_buffer); return (0); } static void logsys_subsys_init ( const char *subsys, int subsysid) { if (logsys_system_needs_init == LOGSYS_LOGGER_NEEDS_INIT) { logsys_loggers[subsysid].init_status = LOGSYS_LOGGER_NEEDS_INIT; } else { logsys_loggers[subsysid].mode = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].mode; logsys_loggers[subsysid].debug = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].debug; logsys_loggers[subsysid].syslog_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].syslog_priority; logsys_loggers[subsysid].logfile_priority = logsys_loggers[LOGSYS_MAX_SUBSYS_COUNT].logfile_priority; logsys_loggers[subsysid].init_status = LOGSYS_LOGGER_INIT_DONE; } strncpy (logsys_loggers[subsysid].subsys, subsys, sizeof (logsys_loggers[subsysid].subsys)); logsys_loggers[subsysid].subsys[ sizeof (logsys_loggers[subsysid].subsys) - 1] = '\0'; logsys_loggers[subsysid].file_idx = 0; } static const char *_logsys_tags_stringify(uint32_t tags) { if (tags == QB_LOG_TAG_LIBQB_MSG) { return "QB"; } else { return logsys_loggers[tags].subsys; } } /* * Internal API - exported */ int _logsys_system_setup( const char *mainsystem, unsigned int mode, int syslog_facility, int syslog_priority) { int i; int32_t fidx; char tempsubsys[LOGSYS_MAX_SUBSYS_NAMELEN]; if ((mainsystem == NULL) || (strlen(mainsystem) >= LOGSYS_MAX_SUBSYS_NAMELEN)) { return -1; } i = LOGSYS_MAX_SUBSYS_COUNT; pthread_mutex_lock (&logsys_config_mutex); snprintf(logsys_loggers[i].subsys, LOGSYS_MAX_SUBSYS_NAMELEN, "%s", mainsystem); logsys_loggers[i].mode = mode; logsys_loggers[i].debug = 0; logsys_loggers[i].file_idx = 0; logsys_loggers[i].logfile_priority = syslog_priority; logsys_loggers[i].syslog_priority = syslog_priority; qb_log_init(mainsystem, syslog_facility, syslog_priority); if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_STDERR) { qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); } else { qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE); } if (logsys_loggers[i].mode & LOGSYS_MODE_OUTPUT_SYSLOG) { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE); } else { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); } qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_TRACE); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 4096); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE); logsys_format_set(NULL); qb_log_tags_stringify_fn_set(_logsys_tags_stringify); logsys_loggers[i].init_status = LOGSYS_LOGGER_INIT_DONE; logsys_system_needs_init = LOGSYS_LOGGER_INIT_DONE; for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) { if ((strcmp (logsys_loggers[i].subsys, "") != 0) && (logsys_loggers[i].init_status == LOGSYS_LOGGER_NEEDS_INIT)) { fidx = logsys_loggers[i].file_idx; strncpy (tempsubsys, logsys_loggers[i].subsys, sizeof (tempsubsys)); tempsubsys[sizeof (tempsubsys) - 1] = '\0'; logsys_subsys_init(tempsubsys, i); logsys_loggers[i].file_idx = fidx; _logsys_config_mode_set_unlocked(i, logsys_loggers[i].mode); _logsys_config_apply_per_subsys(i); } } pthread_mutex_unlock (&logsys_config_mutex); return (0); } static void _logsys_subsys_filename_add (int32_t s, const char *filename) { int i; if (filename == NULL) { return; } assert(logsys_loggers[s].file_idx < MAX_FILES_PER_SUBSYS); assert(logsys_loggers[s].file_idx >= 0); for (i = 0; i < logsys_loggers[s].file_idx; i++) { if (strcmp(logsys_loggers[s].files[i], filename) == 0) { return; } } logsys_loggers[s].files[logsys_loggers[s].file_idx++] = strdup(filename); if (logsys_system_needs_init == LOGSYS_LOGGER_INIT_DONE) { _logsys_config_apply_per_file(s, filename); } } int _logsys_subsys_create (const char *subsys, const char *filename) { int i; if ((subsys == NULL) || (strlen(subsys) >= LOGSYS_MAX_SUBSYS_NAMELEN)) { return -1; } pthread_mutex_lock (&logsys_config_mutex); i = _logsys_config_subsys_get_unlocked (subsys); if ((i > -1) && (i < LOGSYS_MAX_SUBSYS_COUNT)) { _logsys_subsys_filename_add(i, filename); pthread_mutex_unlock (&logsys_config_mutex); return i; } for (i = 0; i < LOGSYS_MAX_SUBSYS_COUNT; i++) { if (strcmp (logsys_loggers[i].subsys, "") == 0) { logsys_subsys_init(subsys, i); _logsys_subsys_filename_add(i, filename); break; } } if (i >= LOGSYS_MAX_SUBSYS_COUNT) { i = -1; } pthread_mutex_unlock (&logsys_config_mutex); return i; } int _logsys_config_subsys_get (const char *subsys) { unsigned int i; pthread_mutex_lock (&logsys_config_mutex); i = _logsys_config_subsys_get_unlocked (subsys); pthread_mutex_unlock (&logsys_config_mutex); return i; } static int32_t _logsys_config_mode_set_unlocked(int32_t subsysid, uint32_t new_mode) { if ( logsys_loggers[subsysid].mode == new_mode) { return 0; } if (logsys_loggers[subsysid].target_id > 0) { qb_log_ctl(logsys_loggers[subsysid].target_id, QB_LOG_CONF_ENABLED, (new_mode & LOGSYS_MODE_OUTPUT_FILE)); } if (subsysid == LOGSYS_MAX_SUBSYS_COUNT) { qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, (new_mode & LOGSYS_MODE_OUTPUT_STDERR)); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, (new_mode & LOGSYS_MODE_OUTPUT_SYSLOG)); } logsys_loggers[subsysid].mode = new_mode; return 0; } int logsys_config_mode_set (const char *subsys, unsigned int mode) { int i; pthread_mutex_lock (&logsys_config_mutex); if (subsys != NULL) { i = _logsys_config_subsys_get_unlocked (subsys); if (i >= 0) { i = _logsys_config_mode_set_unlocked(i, mode); } } else { for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { _logsys_config_mode_set_unlocked(i, mode); } i = 0; } pthread_mutex_unlock (&logsys_config_mutex); return i; } unsigned int logsys_config_mode_get (const char *subsys) { int i; i = _logsys_config_subsys_get (subsys); if (i < 0) { return i; } return logsys_loggers[i].mode; } int logsys_config_file_set ( const char *subsys, const char **error_string, const char *file) { int i; int res; pthread_mutex_lock (&logsys_config_mutex); if (subsys != NULL) { i = _logsys_config_subsys_get_unlocked (subsys); if (i < 0) { res = i; } else { res = logsys_config_file_set_unlocked(i, error_string, file); } } else { for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { res = logsys_config_file_set_unlocked(i, error_string, file); if (res < 0) { break; } } } pthread_mutex_unlock (&logsys_config_mutex); return res; } int logsys_format_set (const char *format) { int ret = 0; + int i; int c; int w; int reminder; char syslog_format[128]; if (format_buffer) { free(format_buffer); format_buffer = NULL; } format_buffer = strdup(format ? format : "%7p [%6g] %b"); if (format_buffer == NULL) { ret = -1; } qb_log_format_set(QB_LOG_STDERR, format_buffer); + for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { + if (logsys_loggers[i].target_id > 0) { + qb_log_format_set(logsys_loggers[i].target_id, format_buffer); + } + } + /* * This just goes through and remove %t and %p from * the format string for syslog. */ w = 0; memset(syslog_format, '\0', sizeof(syslog_format)); for (c = 0; c < strlen(format_buffer); c++) { if (format_buffer[c] == '%') { reminder = c; for (c++; c < strlen(format_buffer); c++) { if (isdigit(format_buffer[c])) { continue; } if (format_buffer[c] == 't' || format_buffer[c] == 'p') { c++; } else { c = reminder; } break; } } syslog_format[w] = format_buffer[c]; w++; } // printf("normal_format: %s\n", format_buffer); // printf("syslog_format: %s\n", syslog_format); qb_log_format_set(QB_LOG_SYSLOG, syslog_format); return ret; } char *logsys_format_get (void) { return format_buffer; } int logsys_config_syslog_facility_set ( const char *subsys, unsigned int facility) { return qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, facility); } int logsys_config_syslog_priority_set ( const char *subsys, unsigned int priority) { int i; pthread_mutex_lock (&logsys_config_mutex); if (subsys != NULL) { i = _logsys_config_subsys_get_unlocked (subsys); if (i >= 0) { logsys_loggers[i].syslog_priority = priority; logsys_loggers[i].dirty = QB_TRUE; i = 0; } } else { for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { logsys_loggers[i].syslog_priority = priority; logsys_loggers[i].dirty = QB_TRUE; } i = 0; } pthread_mutex_unlock (&logsys_config_mutex); return i; } int logsys_config_logfile_priority_set ( const char *subsys, unsigned int priority) { int i; pthread_mutex_lock (&logsys_config_mutex); if (subsys != NULL) { i = _logsys_config_subsys_get_unlocked (subsys); if (i >= 0) { logsys_loggers[i].logfile_priority = priority; logsys_loggers[i].dirty = QB_TRUE; i = 0; } } else { for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { logsys_loggers[i].logfile_priority = priority; logsys_loggers[i].dirty = QB_TRUE; } i = 0; } pthread_mutex_unlock (&logsys_config_mutex); return i; } static void _logsys_config_apply_per_file(int32_t s, const char *filename) { uint32_t syslog_priority = logsys_loggers[s].syslog_priority; uint32_t logfile_priority = logsys_loggers[s].logfile_priority; qb_log_filter_ctl(s, QB_LOG_TAG_SET, QB_LOG_FILTER_FILE, filename, LOG_TRACE); qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FILE, filename, LOG_TRACE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FILE, filename, LOG_TRACE); + if (logsys_loggers[s].target_id > 0) { + qb_log_filter_ctl(logsys_loggers[s].target_id, + QB_LOG_FILTER_REMOVE, + QB_LOG_FILTER_FILE, filename, LOG_TRACE); + } if (logsys_loggers[s].debug) { syslog_priority = LOG_DEBUG; logfile_priority = LOG_DEBUG; } qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, - QB_LOG_FILTER_FILE, filename, - syslog_priority); + QB_LOG_FILTER_FILE, filename, + syslog_priority); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, - QB_LOG_FILTER_FILE, filename, - logfile_priority); + QB_LOG_FILTER_FILE, filename, + logfile_priority); + if (logsys_loggers[s].target_id > 0) { + qb_log_filter_ctl(logsys_loggers[s].target_id, + QB_LOG_FILTER_ADD, + QB_LOG_FILTER_FILE, filename, + logfile_priority); + } } static void _logsys_config_apply_per_subsys(int32_t s) { int32_t f; for (f = 0; f < logsys_loggers[s].file_idx; f++) { _logsys_config_apply_per_file(s, logsys_loggers[s].files[f]); } logsys_loggers[s].dirty = QB_FALSE; } void logsys_config_apply(void) { int32_t s; for (s = 0; s <= LOGSYS_MAX_SUBSYS_COUNT; s++) { if (strcmp(logsys_loggers[s].subsys, "") == 0) { continue; } _logsys_config_apply_per_subsys(s); } } int logsys_config_debug_set ( const char *subsys, unsigned int debug) { int i; pthread_mutex_lock (&logsys_config_mutex); if (subsys != NULL) { i = _logsys_config_subsys_get_unlocked (subsys); if (i >= 0) { logsys_loggers[i].dirty = QB_TRUE; logsys_loggers[i].debug = debug; i = 0; } } else { for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { logsys_loggers[i].debug = debug; logsys_loggers[i].dirty = QB_TRUE; } i = 0; } pthread_mutex_unlock (&logsys_config_mutex); return i; } int logsys_facility_id_get (const char *name) { unsigned int i; for (i = 0; facilitynames[i].c_name != NULL; i++) { if (strcasecmp(name, facilitynames[i].c_name) == 0) { return (facilitynames[i].c_val); } } return (-1); } const char *logsys_facility_name_get (unsigned int facility) { unsigned int i; for (i = 0; facilitynames[i].c_name != NULL; i++) { if (facility == facilitynames[i].c_val) { return (facilitynames[i].c_name); } } return (NULL); } int logsys_priority_id_get (const char *name) { unsigned int i; for (i = 0; prioritynames[i].c_name != NULL; i++) { if (strcasecmp(name, prioritynames[i].c_name) == 0) { return (prioritynames[i].c_val); } } return (-1); } const char *logsys_priority_name_get (unsigned int priority) { unsigned int i; for (i = 0; prioritynames[i].c_name != NULL; i++) { if (priority == prioritynames[i].c_val) { return (prioritynames[i].c_name); } } return (NULL); } diff --git a/exec/mainconfig.c b/exec/mainconfig.c index 2db9a53d..d4064282 100644 --- a/exec/mainconfig.c +++ b/exec/mainconfig.c @@ -1,766 +1,766 @@ /* * Copyright (c) 2002-2005 MontaVista Software, Inc. * Copyright (c) 2006-2009 Red Hat, Inc. * * All rights reserved. * * Author: Steven Dake (sdake@redhat.com) * * This software licensed under BSD license, the text of which follows: * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * - Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * - Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * - Neither the name of the MontaVista Software, Inc. nor the names of its * contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF * THE POSSIBILITY OF SUCH DAMAGE. */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "util.h" #include "mainconfig.h" static char error_string_response[512]; static struct objdb_iface_ver0 *global_objdb; DECLARE_LIST_INIT(uidgid_list_head); /* This just makes the code below a little neater */ static inline int objdb_get_string ( const struct objdb_iface_ver0 *objdb, hdb_handle_t object_service_handle, const char *key, char **value) { int res; *value = NULL; if ( !(res = objdb->object_key_get (object_service_handle, key, strlen (key), (void *)value, NULL))) { if (*value) { return 0; } } return -1; } static inline void objdb_get_int ( const struct objdb_iface_ver0 *objdb, hdb_handle_t object_service_handle, char *key, unsigned int *intvalue) { char *value = NULL; if (!objdb->object_key_get (object_service_handle, key, strlen (key), (void *)&value, NULL)) { if (value) { *intvalue = atoi(value); } } } /** * insert_into_buffer * @target_buffer: a buffer where to write results * @bufferlen: tell us the size of the buffer to avoid overflows * @entry: entry that needs to be added to the buffer * @after: can either be NULL or set to a string. * if NULL, @entry is prependend to logsys_format_get buffer. * if set, @entry is added immediately after @after. * * Since the function is specific to logsys_format_get handling, it is implicit * that source is logsys_format_get(); * * In case of failure, target_buffer could be left dirty. So don't trust * any data leftover in it. * * Searching for "after" assumes that there is only entry of "after" * in the source. Afterall we control the string here and for logging format * it makes little to no sense to have duplicate format entries. * * Returns: 0 on success, -1 on failure **/ static int insert_into_buffer( char *target_buffer, size_t bufferlen, const char *entry, const char *after) { const char *current_format = NULL; current_format = logsys_format_get(); /* if the entry is already in the format we don't add it again */ if (strstr(current_format, entry) != NULL) { return -1; } /* if there is no "after", simply prepend the requested entry * otherwise go for beautiful string manipulation.... */ if (!after) { if (snprintf(target_buffer, bufferlen - 1, "%s%s", entry, current_format) >= bufferlen - 1) { return -1; } } else { const char *afterpos; size_t afterlen; size_t templen; /* check if after is contained in the format * and afterlen has a meaning or return an error */ afterpos = strstr(current_format, after); afterlen = strlen(after); if ((!afterpos) || (!afterlen)) { return -1; } templen = afterpos - current_format + afterlen; if (snprintf(target_buffer, templen + 1, "%s", current_format) >= bufferlen - 1) { return -1; } if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ), "%s%s", entry, current_format + templen) >= bufferlen - ( templen + 1 )) { return -1; } } return 0; } /* * format set is the only global specific option that * doesn't apply at system/subsystem level. */ static int corosync_main_config_format_set ( struct objdb_iface_ver0 *objdb, hdb_handle_t object_handle, const char **error_string) { const char *error_reason; char new_format_buffer[PATH_MAX]; char *value; int err = 0; if (!objdb_get_string (objdb,object_handle, "fileline", &value)) { if (strcmp (value, "on") == 0) { if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), " %f:%l", "g]")) { err = logsys_format_set(new_format_buffer); } else if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%f:%l", NULL)) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for fileline"; goto parse_error; } } if (!objdb_get_string (objdb,object_handle, "function_name", &value)) { if (strcmp (value, "on") == 0) { if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%n:", "f:")) { err = logsys_format_set(new_format_buffer); } else if (!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), " %n", "g]")) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for function_name"; goto parse_error; } } if (!objdb_get_string (objdb,object_handle, "timestamp", &value)) { if (strcmp (value, "on") == 0) { if(!insert_into_buffer(new_format_buffer, sizeof(new_format_buffer), "%t ", NULL)) { err = logsys_format_set(new_format_buffer); } } else if (strcmp (value, "off") == 0) { /* nothing to do here */ } else { error_reason = "unknown value for timestamp"; goto parse_error; } } if (err) { error_reason = "exhausted virtual memory"; goto parse_error; } return (0); parse_error: *error_string = error_reason; return (-1); } static int corosync_main_config_log_destination_set ( struct objdb_iface_ver0 *objdb, hdb_handle_t object_handle, const char *subsys, const char **error_string, const char *objdb_key, unsigned int mode_mask, char deprecated, const char *replacement) { static char formatted_error_reason[128]; char *value; unsigned int mode; if (!objdb_get_string (objdb, object_handle, objdb_key, &value)) { if (deprecated) { log_printf(LOGSYS_LEVEL_WARNING, "Warning: the %s config paramater has been obsoleted." " See corosync.conf man page %s directive.", objdb_key, replacement); } mode = logsys_config_mode_get (subsys); if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) { mode |= mode_mask; if (logsys_config_mode_set(subsys, mode) < 0) { sprintf (formatted_error_reason, "unable to set mode %s", objdb_key); *error_string = formatted_error_reason; return -1; } } else if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) { mode &= ~mode_mask; if (logsys_config_mode_set(subsys, mode) < 0) { sprintf (formatted_error_reason, "unable to unset mode %s", objdb_key); *error_string = formatted_error_reason; return -1; } } else { sprintf (formatted_error_reason, "unknown value for %s", objdb_key); *error_string = formatted_error_reason; return -1; } } return 0; } static int corosync_main_config_set ( struct objdb_iface_ver0 *objdb, hdb_handle_t object_handle, const char *subsys, const char **error_string) { const char *error_reason = error_string_response; char *value; int mode; /* * this bit abuses the internal logsys exported API * to guarantee that all configured subsystems are * initialized too. * * using this approach avoids some headaches caused * by IPC and TOTEM that have a special logging * handling requirements */ if (subsys != NULL) { if (_logsys_subsys_create(subsys, NULL) < 0) { error_reason = "unable to create new logging subsystem"; goto parse_error; } } mode = logsys_config_mode_get(subsys); if (mode < 0) { error_reason = "unable to get mode"; goto parse_error; } - if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason, - "to_logfile", LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0) - goto parse_error; - if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason, "to_stderr", LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0) goto parse_error; if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason, "to_syslog", LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0) goto parse_error; if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason, "to_file", LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0) goto parse_error; if (!objdb_get_string (objdb,object_handle, "syslog_facility", &value)) { int syslog_facility; syslog_facility = logsys_facility_id_get(value); if (syslog_facility < 0) { error_reason = "unknown syslog facility specified"; goto parse_error; } if (logsys_config_syslog_facility_set(subsys, syslog_facility) < 0) { error_reason = "unable to set syslog facility"; goto parse_error; } } if (!objdb_get_string (objdb,object_handle, "syslog_level", &value)) { int syslog_priority; log_printf(LOGSYS_LEVEL_WARNING, "Warning: the syslog_level config paramater has been obsoleted." " See corosync.conf man page syslog_priority directive."); syslog_priority = logsys_priority_id_get(value); if (syslog_priority < 0) { error_reason = "unknown syslog level specified"; goto parse_error; } if (logsys_config_syslog_priority_set(subsys, syslog_priority) < 0) { error_reason = "unable to set syslog level"; goto parse_error; } } if (!objdb_get_string (objdb,object_handle, "syslog_priority", &value)) { int syslog_priority; syslog_priority = logsys_priority_id_get(value); if (syslog_priority < 0) { error_reason = "unknown syslog priority specified"; goto parse_error; } if (logsys_config_syslog_priority_set(subsys, syslog_priority) < 0) { error_reason = "unable to set syslog priority"; goto parse_error; } } if (!objdb_get_string (objdb,object_handle, "logfile", &value)) { if (logsys_config_file_set (subsys, error_string, value) < 0) { goto parse_error; } } + if (corosync_main_config_log_destination_set (objdb, object_handle, subsys, &error_reason, + "to_logfile", LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0) + goto parse_error; + if (!objdb_get_string (objdb,object_handle, "logfile_priority", &value)) { int logfile_priority; logfile_priority = logsys_priority_id_get(value); if (logfile_priority < 0) { error_reason = "unknown logfile priority specified"; goto parse_error; } if (logsys_config_logfile_priority_set(subsys, logfile_priority) < 0) { error_reason = "unable to set logfile priority"; goto parse_error; } } if (!objdb_get_string (objdb, object_handle, "debug", &value)) { if (strcmp (value, "on") == 0) { if (logsys_config_debug_set (subsys, 1) < 0) { error_reason = "unable to set debug on"; goto parse_error; } } else if (strcmp (value, "off") == 0) { if (logsys_config_debug_set (subsys, 0) < 0) { error_reason = "unable to set debug off"; goto parse_error; } } else { error_reason = "unknown value for debug"; goto parse_error; } } return (0); parse_error: *error_string = error_reason; return (-1); } static int corosync_main_config_read_logging ( struct objdb_iface_ver0 *objdb, const char **error_string) { hdb_handle_t object_service_handle; hdb_handle_t object_logger_subsys_handle; hdb_handle_t object_find_handle; hdb_handle_t object_find_logsys_handle; const char *error_reason; char *value; objdb->object_find_create ( OBJECT_PARENT_HANDLE, "logging", strlen ("logging"), &object_find_handle); if (objdb->object_find_next ( object_find_handle, &object_service_handle) == 0) { /* format set is supported only for toplevel */ if (corosync_main_config_format_set (objdb, object_service_handle, &error_reason) < 0) { goto parse_error; } if (corosync_main_config_set (objdb, object_service_handle, NULL, &error_reason) < 0) { goto parse_error; } /* we will need 2 of these to compensate for new logging * config format */ objdb->object_find_create ( object_service_handle, "logger_subsys", strlen ("logger_subsys"), &object_find_logsys_handle); while (objdb->object_find_next ( object_find_logsys_handle, &object_logger_subsys_handle) == 0) { if (!objdb_get_string (objdb, object_logger_subsys_handle, "subsys", &value)) { if (corosync_main_config_set (objdb, object_logger_subsys_handle, value, &error_reason) < 0) { goto parse_error; } } else { error_reason = "subsys required for logger directive"; goto parse_error; } } objdb->object_find_destroy (object_find_logsys_handle); objdb->object_find_create ( object_service_handle, "logging_daemon", strlen ("logging_daemon"), &object_find_logsys_handle); while (objdb->object_find_next ( object_find_logsys_handle, &object_logger_subsys_handle) == 0) { if (!objdb_get_string (objdb, object_logger_subsys_handle, "name", &value)) { if (strcmp(value, "corosync") == 0) { if (!objdb_get_string (objdb, object_logger_subsys_handle, "subsys", &value)) { if (corosync_main_config_set (objdb, object_logger_subsys_handle, value, &error_reason) < 0) { goto parse_error; } } else { if (corosync_main_config_set (objdb, object_logger_subsys_handle, NULL, &error_reason) < 0) { goto parse_error; } } } } else { error_reason = "name required for logging_daemon directive"; goto parse_error; } } objdb->object_find_destroy (object_find_logsys_handle); } objdb->object_find_destroy (object_find_handle); logsys_config_apply(); return 0; parse_error: *error_string = error_reason; return (-1); } static int uid_determine (const char *req_user) { int pw_uid = 0; struct passwd passwd; struct passwd* pwdptr = &passwd; struct passwd* temp_pwd_pt; char *pwdbuffer; int pwdlinelen; pwdlinelen = sysconf (_SC_GETPW_R_SIZE_MAX); if (pwdlinelen == -1) { pwdlinelen = 256; } pwdbuffer = malloc (pwdlinelen); if ((getpwnam_r (req_user, pwdptr, pwdbuffer, pwdlinelen, &temp_pwd_pt)) != 0) { log_printf (LOGSYS_LEVEL_ERROR, "ERROR: The '%s' user is not found in /etc/passwd, please read the documentation.\n", req_user); corosync_exit_error (AIS_DONE_UID_DETERMINE); } pw_uid = passwd.pw_uid; free (pwdbuffer); return pw_uid; } static int gid_determine (const char *req_group) { int ais_gid = 0; struct group group; struct group * grpptr = &group; struct group * temp_grp_pt; char *grpbuffer; int grplinelen; grplinelen = sysconf (_SC_GETGR_R_SIZE_MAX); if (grplinelen == -1) { grplinelen = 256; } grpbuffer = malloc (grplinelen); if ((getgrnam_r (req_group, grpptr, grpbuffer, grplinelen, &temp_grp_pt)) != 0) { log_printf (LOGSYS_LEVEL_ERROR, "ERROR: The '%s' group is not found in /etc/group, please read the documentation.\n", req_group); corosync_exit_error (AIS_DONE_GID_DETERMINE); } ais_gid = group.gr_gid; free (grpbuffer); return ais_gid; } static void main_objdb_reload_notify(objdb_reload_notify_type_t type, int flush, void *priv_data_pt) { const char *error_string; if (type == OBJDB_RELOAD_NOTIFY_END) { /* * Reload the logsys configuration */ if (logsys_format_set(NULL) == -1) { fprintf (stderr, "Unable to setup logging format.\n"); } corosync_main_config_read_logging(global_objdb, &error_string); } } static void add_logsys_config_notification( struct objdb_iface_ver0 *objdb) { global_objdb = objdb; objdb->object_track_start(OBJECT_PARENT_HANDLE, 1, NULL, NULL, NULL, main_objdb_reload_notify, NULL); } static int corosync_main_config_read_uidgid ( struct objdb_iface_ver0 *objdb, const char **error_string) { hdb_handle_t object_find_handle; hdb_handle_t object_service_handle; char *value; int uid, gid; struct uidgid_item *ugi; objdb->object_find_create ( OBJECT_PARENT_HANDLE, "uidgid", strlen ("uidgid"), &object_find_handle); while (objdb->object_find_next ( object_find_handle, &object_service_handle) == 0) { uid = -1; gid = -1; if (!objdb_get_string (objdb,object_service_handle, "uid", &value)) { uid = uid_determine(value); } if (!objdb_get_string (objdb,object_service_handle, "gid", &value)) { gid = gid_determine(value); } if (uid > -1 || gid > -1) { ugi = malloc (sizeof (*ugi)); if (ugi == NULL) { _corosync_out_of_memory_error(); } ugi->uid = uid; ugi->gid = gid; list_init (&ugi->list); list_add (&ugi->list, &uidgid_list_head); } } objdb->object_find_destroy (object_find_handle); return 0; } int corosync_main_config_read ( struct objdb_iface_ver0 *objdb, const char **error_string) { const char *error_reason = error_string_response; if (corosync_main_config_read_logging(objdb, error_string) < 0) { error_reason = *error_string; goto parse_error; } corosync_main_config_read_uidgid (objdb, error_string); add_logsys_config_notification(objdb); return 0; parse_error: snprintf (error_string_response, sizeof(error_string_response), "parse error in config: %s.\n", error_reason); *error_string = error_string_response; return (-1); } int corosync_main_config_compatibility_read ( struct objdb_iface_ver0 *objdb, enum cs_sync_mode *minimum_sync_mode, const char **error_string) { const char *error_reason = error_string_response; char *value; *minimum_sync_mode = CS_SYNC_V1; if (!objdb_get_string (objdb, OBJECT_PARENT_HANDLE, "compatibility", &value)) { if (strcmp (value, "whitetank") == 0) { *minimum_sync_mode = CS_SYNC_V1; } else if (strcmp (value, "none") == 0) { *minimum_sync_mode = CS_SYNC_V2; } else { snprintf (error_string_response, sizeof (error_string_response), "Invalid compatibility option '%s' specified, must be none or whitetank.\n", value); goto parse_error; } } return 0; parse_error: *error_string = error_reason; return (-1); }