+
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <time.h>
#include <ctype.h>
#include <errno.h>
+#include <stdbool.h>
-#include <atalk/boolean.h>
#include <atalk/util.h>
-
#include <atalk/logger.h>
-
-#define OPEN_LOGS_AS_UID 0
+#include <atalk/unix.h>
#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 { \
"Default", \
- "Core", \
"Logger", \
"CNID", \
"AFPDaemon", \
- "ATalkDaemon", \
- "PAPDaemon", \
- "UAMSDaemon", \
- "Console", \
- "end_of_list_marker"} \
+ "DSI", \
+ "UAMS", \
+ "FCE", \
+ "ad", \
+ "Spotlight", \
+ "end_of_list_marker"}
/* =========================================================================
Config
0: set ?
0: syslog ?
-1: logfiles fd
- log_maxdebug: force first LOG call to call make_log_entry which
- then calls log_init because "inited" is still 0
+ log_none: no logging by default
0: Display options */
-#define DEFAULT_LOG_CONFIG {0, 0, -1, log_maxdebug, 0}
+#define DEFAULT_LOG_CONFIG {0, 0, -1, log_none, 0}
-logtype_conf_t type_configs[logtype_end_of_list_marker] = {
+UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
DEFAULT_LOG_CONFIG, /* logtype_default */
- DEFAULT_LOG_CONFIG, /* logtype_core */
DEFAULT_LOG_CONFIG, /* logtype_logger */
DEFAULT_LOG_CONFIG, /* logtype_cnid */
DEFAULT_LOG_CONFIG, /* logtype_afpd */
- DEFAULT_LOG_CONFIG, /* logtype_atalkd */
- DEFAULT_LOG_CONFIG, /* logtype_papd */
+ DEFAULT_LOG_CONFIG, /* logtype_dsi */
DEFAULT_LOG_CONFIG, /* logtype_uams */
- DEFAULT_LOG_CONFIG /* logtype_console */
+ DEFAULT_LOG_CONFIG, /* logtype_fce */
+ DEFAULT_LOG_CONFIG, /* logtype_ad */
+ DEFAULT_LOG_CONFIG /* logtype_sl */
};
+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;
+
/* These are used by the LOG macro to store __FILE__ and __LINE__ */
static const char *log_src_filename;
static int log_src_linenumber;
Internal function definitions
========================================================================= */
-/*
- * 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)
+/* Hash a log message */
+static unsigned int hash_message(const char *message)
{
- unsigned int typenum, levelnum;
+ const char *p = message;
+ unsigned int hash = 0, i = 7;
- /* 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);
+ while (*p) {
+ hash += *p * i;
+ i++;
+ p++;
}
-
- return;
+ return hash;
}
static void generate_message_details(char *message_details_buffer,
}
}
-/* =========================================================================
- 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,
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) {
}
}
- /* Check if logging to a console */
- if (logtype == logtype_console) {
- log_config.console = 1;
- logtype = logtype_default;
- }
-
/* Set new values */
type_configs[logtype].level = loglevel;
free(tmp);
} else {
- process_uid = geteuid();
- if (process_uid) {
- if (seteuid(OPEN_LOGS_AS_UID) == -1) {
- /* XXX failing silently */
- return;
- }
- }
+ 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 */
}
/* 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:
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)
{
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.
if (fd < 0) {
/* no where to send the output, give up */
- return;
+ goto exit;
}
/* Initialise the Messages */
temp_buffer[len+1] = 0;
}
+ if (type_configs[logtype].level >= log_debug)
+ goto log; /* bypass flooding checks */
+
+ /* Prevent flooding: hash the message and check if we got the same one recently */
+ int hash = hash_message(temp_buffer) + log_src_linenumber;
+
+ /* Search for the same message by hash */
+ for (int i = log_flood_entries - 1; i >= 0; i--) {
+ if (log_flood_array[i].hash == hash) {
+
+ /* found same message */
+ log_flood_array[i].count++;
+
+ /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
+ if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
+ /* yes, log it and remove from array */
+
+ /* reusing log_details_buffer */
+ sprintf(log_details_buffer, "message repeated %i times\n",
+ LOG_FLOODING_MAXCOUNT - 1);
+ write(fd, log_details_buffer, strlen(log_details_buffer));
+
+ if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
+ /* last array element, just decrement count */
+ log_flood_entries--;
+ goto exit;
+ }
+ /* move array elements down */
+ for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
+ log_flood_array[j-1] = log_flood_array[j];
+ log_flood_entries--;
+ }
+
+ if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
+ /* log it */
+ goto log;
+ /* discard it */
+ goto exit;
+ } /* if */
+ } /* for */
+
+ /* No matching message found, add this message to array*/
+ if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
+ /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
+ if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
+ /* reusing log_details_buffer */
+ sprintf(log_details_buffer, "message repeated %i times\n",
+ log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
+ write(fd, log_details_buffer, strlen(log_details_buffer));
+ }
+ for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
+ log_flood_array[i-1] = log_flood_array[i];
+ }
+ log_flood_entries--;
+ }
+ log_flood_array[log_flood_entries].count = 1;
+ log_flood_array[log_flood_entries].hash = hash;
+ log_flood_entries++;
+
+log:
if ( ! log_config.console) {
generate_message_details(log_details_buffer, sizeof(log_details_buffer),
type_configs[logtype].set ?
loglevel, logtype);
/* If default wasnt setup its fd is -1 */
- 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);
+ write(fd, log_details_buffer, strlen(log_details_buffer));
+ write(fd, temp_buffer, strlen(temp_buffer));
} else {
write(fd, temp_buffer, strlen(temp_buffer));
}
+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;
- /* get filename */
- ptr = strpbrk(ptr, " \t");
- if (ptr) {
- *ptr++ = 0;
+ save = ptr = strdup(logstr);
+
+ 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);
-}