X-Git-Url: https://arthur.barton.de/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=libatalk%2Futil%2Flogger.c;h=014e41e958458c5872c72c8a0591c619de9ed96c;hb=22358cea1de24dbdfb2a6550c80f19afd3e808b3;hp=132662b5b0eda56fa25ca675e6d6857193565398;hpb=008eac9cd4113c38090c803b1236335158fcc64c;p=netatalk.git diff --git a/libatalk/util/logger.c b/libatalk/util/logger.c index 132662b5..014e41e9 100644 --- a/libatalk/util/logger.c +++ b/libatalk/util/logger.c @@ -26,8 +26,10 @@ Netatalk 2001 (c) #include #include #include +#include #include +#include #define LOGGER_C #include @@ -101,7 +103,7 @@ static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings); */ /* -[un]setuplog []*/ -static void setuplog_internal(const char *logtype, const char *loglevel, const char *filename) +static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename) { int typenum, levelnum; @@ -162,18 +164,17 @@ static void generate_message_details(char *message_details_buffer, ptr += templen; templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec); + if (templen == -1 || templen >= len) + return; + 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; ptr += templen; @@ -184,7 +185,7 @@ static void generate_message_details(char *message_details_buffer, templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber); else templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber); - if (templen >= len) + if (templen == -1 || templen >= len) return; len -= templen; ptr += templen; @@ -195,17 +196,23 @@ 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; ptr += templen; /* Errortype */ if (logtype= len) + return; len -= templen; ptr += templen; } - + strncat(ptr, "): ", len); + ptr[len -1] = 0; } static int get_syslog_equivalent(enum loglevels loglevel) @@ -290,13 +297,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) { @@ -377,17 +392,32 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, if (inlog) return; - inlog = 1; + /* Check if requested logtype is setup */ + if (file_configs[logtype].set) + /* Yes */ + fd = file_configs[logtype].fd; + else + /* No: use default */ + fd = file_configs[logtype_default].fd; + + if (fd < 0) { + /* no where to send the output, give up */ + 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 >= 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; @@ -399,28 +429,19 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype, file_configs[logtype_default].display_options, loglevel, logtype); - /* Check if requested logtype is setup */ - if (file_configs[logtype].set) - /* Yes */ - fd = file_configs[logtype].fd; - else - /* No: use default */ - fd = file_configs[logtype_default].fd; /* If default wasnt setup its fd is -1 */ - if (fd >= 0) { - iov[0].iov_base = log_details_buffer; - iov[0].iov_len = strlen(log_details_buffer); - iov[1].iov_base = temp_buffer; - iov[1].iov_len = strlen(temp_buffer); - writev( fd, iov, 2); - } + iov[0].iov_base = log_details_buffer; + iov[0].iov_len = strlen(log_details_buffer); + iov[1].iov_base = temp_buffer; + iov[1].iov_len = strlen(temp_buffer); + writev( fd, iov, 2); inlog = 0; } /* Called by the LOG macro for syslog messages */ -void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...) +void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message, ...) { va_list args; char log_buffer[MAXLOGSIZE]; @@ -444,7 +465,7 @@ void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *mes 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; @@ -475,10 +496,12 @@ void setuplog(const char *logstr) ptr++; } filename = ptr; + if (*filename == 0) + filename = NULL; } /* finally call setuplog, filename can be NULL */ - setuplog_internal(logtype, loglevel, filename); + setuplog_internal(loglevel, logtype, filename); free(ptrbak); } @@ -497,7 +520,7 @@ void unsetuplog(const char *logstr) filename = strtok(NULL, " \t"); /* finally call setuplog, filename can be NULL */ - setuplog_internal(str, NULL, filename); + setuplog_internal(NULL, str, filename); free(str); }