]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/util/logger.c
Fix fce merge conflict
[netatalk.git] / libatalk / util / logger.c
index 37be21bbc001a8a875870fa8991a8a95efbf9ce2..eb98f45b290d5817461b90498a399bfb5034bbf0 100644 (file)
@@ -56,14 +56,13 @@ Netatalk 2001 (c)
 /* these are the string identifiers corresponding to each logtype */
 #define LOGTYPE_STRING_IDENTIFIERS { \
   "Default",                         \
-  "Core",                            \
   "Logger",                          \
   "CNID",                            \
   "AFPDaemon",                       \
+  "DSI",                             \
   "ATalkDaemon",                     \
   "PAPDaemon",                       \
-  "UAMSDaemon",                      \
-  "Console",                         \
+  "UAMS",                            \
   "end_of_list_marker"}              \
 
 /* =========================================================================
@@ -77,23 +76,34 @@ log_config_t log_config = { 0 };
    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}
 
 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_dsi */
     DEFAULT_LOG_CONFIG, /* logtype_atalkd */
     DEFAULT_LOG_CONFIG, /* logtype_papd */
-    DEFAULT_LOG_CONFIG, /* logtype_uams */
-    DEFAULT_LOG_CONFIG  /* logtype_console */
+    DEFAULT_LOG_CONFIG /* logtype_uams */
 };
 
+/* 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;
@@ -113,6 +123,20 @@ static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_string
    Internal function definitions
    ========================================================================= */
 
+/* Hash a log message */
+static unsigned int hash_message(const char *message)
+{
+    const char *p = message;
+    unsigned int hash = 0, i = 7;
+
+    while (*p) {
+        hash += *p * i;
+        i++;
+        p++;
+    }
+    return hash;
+}
+
 /*
  * If filename == NULL its for syslog logging, otherwise its for file-logging.
  * "unsetuplog" calls with loglevel == NULL.
@@ -319,12 +343,6 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt
         }
     }
 
-    /* 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;
 
@@ -344,8 +362,7 @@ void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logt
         process_uid = geteuid();
         if (process_uid) {
             if (seteuid(OPEN_LOGS_AS_UID) == -1) {
-                /* XXX failing silently */
-                return;
+                process_uid = 0;
             }
         }
         type_configs[logtype].fd = open(filename,
@@ -503,7 +520,7 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
 
     if (fd < 0) {
         /* no where to send the output, give up */
-        return;
+        goto exit;
     }
 
     /* Initialise the Messages */
@@ -522,6 +539,66 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
         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 ?
@@ -539,6 +616,7 @@ void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
         write(fd, temp_buffer, strlen(temp_buffer));
     }
 
+exit:
     inlog = 0;
 }