X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Flogger.c;h=939d7f4d3caa524d43e87170d7b8faadd8b7f92c;hb=cd24cb50921a6092255bac1f3cf44770cafa423b;hp=60361371c05be64b7e7f311bdf5597117d68d5c5;hpb=247ea1dd3c1194cfd8b5beb86b9a8d5c53d68590;p=netatalk.git diff --git a/libatalk/util/logger.c b/libatalk/util/logger.c index 60361371..939d7f4d 100644 --- a/libatalk/util/logger.c +++ b/libatalk/util/logger.c @@ -26,23 +26,78 @@ Netatalk 2001 (c) #include #include #include +#include #include +#include -#define LOGGER_C #include -#undef LOGGER_C #define OPEN_LOGS_AS_UID 0 #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"} + +/* these are the string identifiers corresponding to each logtype */ +#define LOGTYPE_STRING_IDENTIFIERS { \ + "Default", \ + "Core", \ + "Logger", \ + "CNID", \ + "AFPDaemon", \ + "ATalkDaemon", \ + "PAPDaemon", \ + "UAMSDaemon", \ + \ + "end_of_list_marker"} \ + +/* ========================================================================= + Structure definitions + ========================================================================= */ + +/* Main log config */ +typedef struct { + int inited; /* file log config initialized ? */ + int filelogging; /* Any level set to filelogging ? */ + /* Deactivates syslog logging */ + char processname[16]; + int syslog_opened; /* syslog opened ? */ + int facility; /* syslog facility to use */ + int syslog_display_options; + int syslog_level; /* Log Level to send to syslog */ +} log_config_t; + +/* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */ +typedef struct { + int set; /* set individually ? yes: changing default + * doesnt change it. no: it changes it.*/ + char *filename; /* Name of file */ + int fd; /* logfiles fd */ + int level; /* Log Level to put in this file */ + int display_options; +} filelog_conf_t; + /* ========================================================================= Config ========================================================================= */ /* Main log config container, must be globally visible */ -log_config_t log_config = { +static log_config_t log_config = { 0, /* Initialized ? 0 = no */ 0, /* No filelogging setup yet */ {0}, /* processname */ @@ -60,7 +115,7 @@ log_config_t log_config = { 0: Display options */ #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0} -filelog_conf_t file_configs[logtype_end_of_list_marker] = { +static filelog_conf_t file_configs[logtype_end_of_list_marker] = { DEFAULT_LOG_CONFIG, /* logtype_default */ DEFAULT_LOG_CONFIG, /* logtype_core */ DEFAULT_LOG_CONFIG, /* logtype_logger */ @@ -72,8 +127,8 @@ filelog_conf_t file_configs[logtype_end_of_list_marker] = { }; /* These are used by the LOG macro to store __FILE__ and __LINE__ */ -char *log_src_filename; -int log_src_linenumber; +static const char *log_src_filename; +static int log_src_linenumber; /* Array to store text to list given a log type */ static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS; @@ -168,15 +223,9 @@ static void generate_message_details(char *message_details_buffer, len -= templen; ptr += templen; - /* Process name */ - strncpy(ptr, log_config.processname, len); - templen = strlen(ptr); - len -= templen; - ptr += templen; - - /* PID */ + /* Process name && PID */ pid = getpid(); - templen = snprintf(ptr, len, "[%d]", pid); + templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid); if (templen == -1 || templen >= len) return; len -= templen; @@ -200,6 +249,7 @@ static void generate_message_details(char *message_details_buffer, templen = snprintf(ptr, len, " (D%d:", loglevel - 1); else templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]); + if (templen == -1 || templen >= len) return; len -= templen; @@ -213,8 +263,9 @@ static void generate_message_details(char *message_details_buffer, len -= templen; ptr += templen; } - + strncat(ptr, "): ", len); + ptr[len -1] = 0; } static int get_syslog_equivalent(enum loglevels loglevel) @@ -299,13 +350,21 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt /* Open log file as OPEN_LOGS_AS_UID*/ process_uid = geteuid(); - if (process_uid) - seteuid(OPEN_LOGS_AS_UID); + if (process_uid) { + if (seteuid(OPEN_LOGS_AS_UID) == -1) { + /* XXX failing silently */ + return; + } + } file_configs[logtype].fd = open( file_configs[logtype].filename, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - if (process_uid) - seteuid(process_uid); + if (process_uid) { + if (seteuid(process_uid) == -1) { + LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno)); + exit(EXITERR_SYS); + } + } /* Check for error opening/creating logfile */ if (-1 == file_configs[logtype].fd) { @@ -366,13 +425,25 @@ 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.facility); + log_config.syslog_opened = 1; + } + + syslog(get_syslog_equivalent(loglevel), "%s", message); +} + /* ------------------------------------------------------------------------- make_log_entry has 1 main flaws: The message in its entirity, must fit into the tempbuffer. So it must be shorter than MAXLOGSIZE ------------------------------------------------------------------------- */ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, - char *message, ...) + const char *file, int line, char *message, ...) { /* fn is not reentrant but is used in signal handler * with LOGGER it's a little late source name and line number @@ -387,6 +458,31 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, if (inlog) return; + inlog = 1; + + if (!log_config.inited) { + log_init(); + } + + if (file_configs[logtype].level >= loglevel) { + log_src_filename = file; + log_src_linenumber = line; + } + else if (!log_config.filelogging && log_config.syslog_level >= loglevel) { + /* Initialise the Messages */ + va_start(args, message); + vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args); + va_end(args); + temp_buffer[MAXLOGSIZE -1] = 0; + make_syslog_entry(loglevel, logtype, temp_buffer); + inlog = 0; + return; + } + else { + inlog = 0; + return; + } + /* Check if requested logtype is setup */ if (file_configs[logtype].set) /* Yes */ @@ -400,16 +496,17 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, return; } - inlog = 1; /* Initialise the Messages */ va_start(args, message); - len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args); + len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args); va_end(args); /* Append \n */ - if (len ==-1 || len >= MAXLOGSIZE) + if (len ==-1 || len >= MAXLOGSIZE -1) { /* vsnprintf hit the buffer size*/ temp_buffer[MAXLOGSIZE-2] = '\n'; + temp_buffer[MAXLOGSIZE-1] = 0; + } else { temp_buffer[len] = '\n'; temp_buffer[len+1] = 0; @@ -432,40 +529,10 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, inlog = 0; } -/* Called by the LOG macro for syslog messages */ -void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message, ...) -{ - va_list args; - char log_buffer[MAXLOGSIZE]; - /* fn is not reentrant but is used in signal handler - * with LOGGER it's a little late source name and line number - * are already changed. - */ - static int inlog = 0; - - if (inlog) - return; - inlog = 1; - - if ( ! (log_config.syslog_opened) ) { - openlog(log_config.processname, log_config.syslog_display_options, - log_config.facility); - log_config.syslog_opened = 1; - } - - /* Initialise the Messages */ - va_start(args, message); - vsnprintf(log_buffer, sizeof(log_buffer), message, args); - va_end(args); - log_buffer[MAXLOGSIZE -1] = 0; - syslog(get_syslog_equivalent(loglevel), "%s", log_buffer); - - inlog = 0; -} void setuplog(const char *logstr) { - char *ptr, *ptrbak, *logtype, *loglevel, *filename; + char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL; ptr = strdup(logstr); ptrbak = ptr; @@ -488,6 +555,8 @@ void setuplog(const char *logstr) ptr++; } filename = ptr; + if (filename && *filename == 0) + filename = NULL; } /* finally call setuplog, filename can be NULL */