X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?p=netatalk.git;a=blobdiff_plain;f=libatalk%2Futil%2Flogger.c;h=c8a2a0a8acd2845c43bfbd088e81b7a496855a1b;hp=e13ada1750a8019900f9856ea6f8c902a7746768;hb=4111aba41c36a99bfd7eb7e987b24314735cdd10;hpb=465246e257d9aff9855e3e35d8fd5983db932b45 diff --git a/libatalk/util/logger.c b/libatalk/util/logger.c index e13ada17..c8a2a0a8 100644 --- a/libatalk/util/logger.c +++ b/libatalk/util/logger.c @@ -1,3 +1,4 @@ + #ifdef HAVE_CONFIG_H #include "config.h" #endif @@ -27,31 +28,29 @@ Netatalk 2001 (c) #include #include #include +#include -#include #include - #include - -#define OPEN_LOGS_AS_UID 0 +#include #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0])) #define MAXLOGSIZE 512 #define LOGLEVEL_STRING_IDENTIFIERS { \ - "LOG_NOTHING", \ - "LOG_SEVERE", \ - "LOG_ERROR", \ - "LOG_WARN", \ - "LOG_NOTE", \ - "LOG_INFO", \ - "LOG_DEBUG", \ - "LOG_DEBUG6", \ - "LOG_DEBUG7", \ - "LOG_DEBUG8", \ - "LOG_DEBUG9", \ - "LOG_MAXDEBUG"} + "NOTHING", \ + "SEVERE", \ + "ERROR", \ + "WARN", \ + "NOTE", \ + "INFO", \ + "DEBUG", \ + "DEBUG6", \ + "DEBUG7", \ + "DEBUG8", \ + "DEBUG9", \ + "MAXDEBUG"} /* these are the string identifiers corresponding to each logtype */ #define LOGTYPE_STRING_IDENTIFIERS { \ @@ -91,16 +90,20 @@ UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = { DEFAULT_LOG_CONFIG /* logtype_uams */ }; +static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility); + /* We use this in order to track the last n log messages in order to prevent flooding */ #define LOG_FLOODING_MINCOUNT 5 /* this controls after how many consecutive messages must be detected before we start to hide them */ #define LOG_FLOODING_MAXCOUNT 1000 /* this controls after how many consecutive messages we force a "repeated x times" message */ #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */ + struct log_flood_entry { int count; unsigned int hash; }; + static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE]; static int log_flood_entries; @@ -137,56 +140,6 @@ static unsigned int hash_message(const char *message) return hash; } -/* - * If filename == NULL its for syslog logging, otherwise its for file-logging. - * "unsetuplog" calls with loglevel == NULL. - * loglevel == NULL means: - * if logtype == default - * disable logging - * else - * set to default logging - */ -static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename) -{ - unsigned int typenum, levelnum; - - /* Parse logtype */ - for( typenum=0; typenum < num_logtype_strings; typenum++) { - if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0) - break; - } - if (typenum >= num_logtype_strings) { - return; - } - - /* Parse loglevel */ - if (loglevel == NULL) { - levelnum = 0; - } else { - for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) { - if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0) - break; - } - if (levelnum >= num_loglevel_strings) { - return; - } - } - - /* is this a syslog setup or a filelog setup ? */ - if (filename == NULL) { - /* must be syslog */ - syslog_setup(levelnum, - typenum, - logoption_ndelay | logoption_pid, - logfacility_daemon); - } else { - /* this must be a filelog */ - log_setup(filename, levelnum, typenum); - } - - return; -} - static void generate_message_details(char *message_details_buffer, int message_details_buffer_length, int display_options, @@ -280,11 +233,20 @@ static int get_syslog_equivalent(enum loglevels loglevel) } } -/* ========================================================================= - Global function definitions - ========================================================================= */ +/* Called by the LOG macro for syslog messages */ +static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message) +{ + if ( !log_config.syslog_opened ) { + openlog(log_config.processname, + log_config.syslog_display_options, + log_config.syslog_facility); + log_config.syslog_opened = true; + } -void log_init(void) + syslog(get_syslog_equivalent(loglevel), "%s", message); +} + +static void log_init(void) { syslog_setup(log_info, logtype_default, @@ -292,10 +254,8 @@ void log_init(void) logfacility_daemon); } -void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype) +static void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype) { - uid_t process_uid; - if (loglevel == 0) { /* Disable */ if (type_configs[logtype].set) { @@ -359,21 +319,11 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt free(tmp); } else { - process_uid = geteuid(); - if (process_uid) { - if (seteuid(OPEN_LOGS_AS_UID) == -1) { - process_uid = 0; - } - } + become_root(); type_configs[logtype].fd = open(filename, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (process_uid) { - if (seteuid(process_uid) == -1) { - LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno)); - exit(EXITERR_SYS); - } - } + become_root(); } /* Check for error opening/creating logfile */ @@ -408,7 +358,7 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt } /* Setup syslog logging */ -void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility) +static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility) { /* * FIXME: @@ -437,14 +387,64 @@ void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int log_config.inited = 1; - LOG(log_note, logtype_logger, "Set syslog logging to level: %s", + LOG(log_info, logtype_logger, "Set syslog logging to level: %s", arr_loglevel_strings[loglevel]); } -void log_close(void) +/* + * If filename == NULL its for syslog logging, otherwise its for file-logging. + * "unsetuplog" calls with loglevel == NULL. + * loglevel == NULL means: + * if logtype == default + * disable logging + * else + * set to default logging + */ +static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename) { + unsigned int typenum, levelnum; + + /* Parse logtype */ + for( typenum=0; typenum < num_logtype_strings; typenum++) { + if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0) + break; + } + if (typenum >= num_logtype_strings) { + return; + } + + /* Parse loglevel */ + if (loglevel == NULL) { + levelnum = 0; + } else { + for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) { + if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0) + break; + } + if (levelnum >= num_loglevel_strings) { + return; + } + } + + /* is this a syslog setup or a filelog setup ? */ + if (filename == NULL) { + /* must be syslog */ + syslog_setup(levelnum, + typenum, + logoption_ndelay | logoption_pid, + logfacility_daemon); + } else { + /* this must be a filelog */ + log_setup(filename, levelnum, typenum); + } + + return; } +/* ========================================================================= + Global function definitions + ========================================================================= */ + /* This function sets up the processname */ void set_processname(const char *processname) { @@ -452,19 +452,6 @@ void set_processname(const char *processname) log_config.processname[15] = 0; } -/* Called by the LOG macro for syslog messages */ -static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message) -{ - if ( !log_config.syslog_opened ) { - openlog(log_config.processname, - log_config.syslog_display_options, - log_config.syslog_facility); - log_config.syslog_opened = true; - } - - syslog(get_syslog_equivalent(loglevel), "%s", message); -} - /* ------------------------------------------------------------------------- make_log_entry has 1 main flaws: The message in its entirity, must fit into the tempbuffer. @@ -542,7 +529,6 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, if (type_configs[logtype].level >= log_debug) goto log; /* bypass flooding checks */ - goto log; /* Prevent flooding: hash the message and check if we got the same one recently */ int hash = hash_message(temp_buffer) + log_src_linenumber; @@ -621,57 +607,39 @@ exit: inlog = 0; } - -void setuplog(const char *logstr) +void setuplog(const char *logstr, const char *logfile) { - char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL; - ptr = strdup(logstr); - ptrbak = ptr; - - /* logtype */ - logtype = ptr; - - /* get loglevel */ - ptr = strpbrk(ptr, " \t"); - if (ptr) { - *ptr++ = 0; - while (*ptr && isspace(*ptr)) - ptr++; - loglevel = ptr; + char *ptr, *save; + char *logtype, *loglevel; + char c; + + save = ptr = strdup(logstr); - /* get filename */ - ptr = strpbrk(ptr, " \t"); - if (ptr) { - *ptr++ = 0; + ptr = strtok(ptr, ", "); + + while (ptr) { + while (*ptr) { while (*ptr && isspace(*ptr)) ptr++; + + logtype = ptr; + ptr = strpbrk(ptr, ":"); + if (!ptr) + break; + *ptr = 0; + + ptr++; + loglevel = ptr; + while (*ptr && !isspace(*ptr)) + ptr++; + c = *ptr; + *ptr = 0; + setuplog_internal(loglevel, logtype, logfile); + *ptr = c; } - filename = ptr; - if (filename && *filename == 0) - filename = NULL; + ptr = strtok(NULL, ", "); } - /* finally call setuplog, filename can be NULL */ - setuplog_internal(loglevel, logtype, filename); - - free(ptrbak); + free(save); } -void unsetuplog(const char *logstr) -{ - char *str, *logtype, *filename; - - str = strdup(logstr); - - /* logtype */ - logtype = str; - - /* get filename, can be NULL */ - strtok(str, " \t"); - filename = strtok(NULL, " \t"); - - /* finally call setuplog, filename can be NULL */ - setuplog_internal(NULL, str, filename); - - free(str); -}