#include <sys/time.h>
#include <time.h>
#include <ctype.h>
+#include <errno.h>
#include <atalk/boolean.h>
+#include <atalk/util.h>
-#define LOGGER_C
#include <atalk/logger.h>
-#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 */
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 */
};
/* 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;
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;
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;
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<num_logtype_strings) {
templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
+ if (templen == -1 || templen >= len)
+ return;
len -= templen;
ptr += templen;
}
-
+
strncat(ptr, "): ", len);
+ ptr[len -1] = 0;
}
static int get_syslog_equivalent(enum loglevels loglevel)
/* 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) {
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
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 */
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;
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);
-
- 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;
ptr++;
}
filename = ptr;
+ if (filename && *filename == 0)
+ filename = NULL;
}
/* finally call setuplog, filename can be NULL */