6 /* =========================================================================
8 logger.c was written by Simon Bazley (sibaz@sibaz.com)
10 I believe libatalk is released under the L/GPL licence.
11 Just incase, it is, thats the licence I'm applying to this file.
14 ========================================================================= */
22 #include <sys/types.h>
33 #include <atalk/util.h>
34 #include <atalk/logger.h>
35 #include <atalk/unix.h>
37 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
39 #define MAXLOGSIZE 512
41 #define LOGLEVEL_STRING_IDENTIFIERS { \
55 /* these are the string identifiers corresponding to each logtype */
56 #define LOGTYPE_STRING_IDENTIFIERS { \
66 /* =========================================================================
68 ========================================================================= */
70 /* Main log config container */
71 log_config_t log_config = { 0 };
73 /* Default log config: log nothing to files.
77 log_none: no logging by default
79 #define DEFAULT_LOG_CONFIG {0, 0, -1, log_none, 0}
81 UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
82 DEFAULT_LOG_CONFIG, /* logtype_default */
83 DEFAULT_LOG_CONFIG, /* logtype_logger */
84 DEFAULT_LOG_CONFIG, /* logtype_cnid */
85 DEFAULT_LOG_CONFIG, /* logtype_afpd */
86 DEFAULT_LOG_CONFIG, /* logtype_dsi */
87 DEFAULT_LOG_CONFIG, /* logtype_uams */
88 DEFAULT_LOG_CONFIG /* logtype_fce */
91 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility);
93 /* We use this in order to track the last n log messages in order to prevent flooding */
94 #define LOG_FLOODING_MINCOUNT 5 /* this controls after how many consecutive messages must be detected
95 before we start to hide them */
96 #define LOG_FLOODING_MAXCOUNT 1000 /* this controls after how many consecutive messages we force a
97 "repeated x times" message */
98 #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */
100 struct log_flood_entry {
105 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
106 static int log_flood_entries;
108 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
109 static const char *log_src_filename;
110 static int log_src_linenumber;
112 /* Array to store text to list given a log type */
113 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
114 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
116 /* Array for charachters representing log severity in the log file */
117 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
118 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
120 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
121 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
123 /* =========================================================================
124 Internal function definitions
125 ========================================================================= */
127 /* Hash a log message */
128 static unsigned int hash_message(const char *message)
130 const char *p = message;
131 unsigned int hash = 0, i = 7;
141 static void generate_message_details(char *message_details_buffer,
142 int message_details_buffer_length,
144 enum loglevels loglevel, enum logtypes logtype)
146 char *ptr = message_details_buffer;
148 int len = message_details_buffer_length;
155 gettimeofday(&tv, NULL);
156 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
157 templen = strlen(ptr);
161 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
162 if (templen == -1 || templen >= len)
168 /* Process name && PID */
170 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
171 if (templen == -1 || templen >= len)
177 if ( ! (display_options & logoption_nsrcinfo)) {
178 char *basename = strrchr(log_src_filename, '/');
180 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
182 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
183 if (templen == -1 || templen >= len)
190 if (loglevel >= (num_loglevel_chars - 1))
191 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
193 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
195 if (templen == -1 || templen >= len)
201 if (logtype<num_logtype_strings) {
202 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
203 if (templen == -1 || templen >= len)
209 strncat(ptr, "): ", len);
213 static int get_syslog_equivalent(enum loglevels loglevel)
217 /* The question is we know how bad it is for us,
218 but how should that translate in the syslogs? */
223 case 3: /* warning */
227 case 5: /* information */
234 /* Called by the LOG macro for syslog messages */
235 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
237 if ( !log_config.syslog_opened ) {
238 openlog(log_config.processname,
239 log_config.syslog_display_options,
240 log_config.syslog_facility);
241 log_config.syslog_opened = true;
244 syslog(get_syslog_equivalent(loglevel), "%s", message);
247 static void log_init(void)
249 syslog_setup(log_info,
251 logoption_ndelay | logoption_pid,
255 static void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
259 if (type_configs[logtype].set) {
260 if (type_configs[logtype].fd != -1)
261 close(type_configs[logtype].fd);
262 type_configs[logtype].fd = -1;
263 type_configs[logtype].level = -1;
264 type_configs[logtype].set = false;
266 /* if disabling default also set all "default using" levels to 0 */
267 if (logtype == logtype_default) {
268 while (logtype != logtype_end_of_list_marker) {
269 if ( ! (type_configs[logtype].set))
270 type_configs[logtype].level = -1;
279 if (NULL == filename)
282 /* Resetting existing config ? */
283 if (type_configs[logtype].set) {
284 if (type_configs[logtype].fd != -1)
285 close(type_configs[logtype].fd);
286 type_configs[logtype].fd = -1;
287 type_configs[logtype].level = -1;
288 type_configs[logtype].set = false;
289 type_configs[logtype].syslog = false;
291 /* Reset configs using default */
292 if (logtype == logtype_default) {
294 while (typeiter != logtype_end_of_list_marker) {
295 if (type_configs[typeiter].set == false) {
296 type_configs[typeiter].level = -1;
297 type_configs[typeiter].syslog = false;
305 type_configs[logtype].level = loglevel;
307 /* Open log file as OPEN_LOGS_AS_UID*/
309 /* Is it /dev/tty ? */
310 if (strcmp(filename, "/dev/tty") == 0) {
311 type_configs[logtype].fd = 1; /* stdout */
313 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
314 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
315 char *tmp = strdup(filename);
316 type_configs[logtype].fd = mkstemp(tmp);
321 type_configs[logtype].fd = open(filename,
322 O_CREAT | O_WRONLY | O_APPEND,
323 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
327 /* Check for error opening/creating logfile */
328 if (type_configs[logtype].fd == -1) {
329 type_configs[logtype].level = -1;
330 type_configs[logtype].set = false;
334 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
335 type_configs[logtype].set = true;
336 log_config.inited = true;
338 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
339 /* which then uses the default logtype setup if it isn't setup itself: */
340 /* we just copy the loglevel from default to all logtypes that are not setup. */
341 /* In "make_log_entry" we then check for the logtypes if they arent setup */
342 /* and use default then. We must provide accessible values for all logtypes */
343 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
345 if (logtype == logtype_default) {
347 while (typeiter != logtype_end_of_list_marker) {
348 if ( ! (type_configs[typeiter].set))
349 type_configs[typeiter].level = loglevel;
354 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
355 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
358 /* Setup syslog logging */
359 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
363 * this currently doesn't care if logtype is already logging to a file.
364 * Fortunately currently there's no way a user could trigger this as afpd.conf
365 * is not re-read on SIGHUP.
368 type_configs[logtype].level = loglevel;
369 type_configs[logtype].set = true;
370 type_configs[logtype].syslog = true;
371 log_config.syslog_display_options = display_options;
372 log_config.syslog_facility = facility;
374 /* Setting default logging? Then set all logtype not set individually */
375 if (logtype == logtype_default) {
377 while (typeiter != logtype_end_of_list_marker) {
378 if ( ! (type_configs[typeiter].set)) {
379 type_configs[typeiter].level = loglevel;
380 type_configs[typeiter].syslog = true;
386 log_config.inited = 1;
388 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
389 arr_loglevel_strings[loglevel]);
393 * If filename == NULL its for syslog logging, otherwise its for file-logging.
394 * "unsetuplog" calls with loglevel == NULL.
395 * loglevel == NULL means:
396 * if logtype == default
399 * set to default logging
401 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
403 unsigned int typenum, levelnum;
406 for( typenum=0; typenum < num_logtype_strings; typenum++) {
407 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
410 if (typenum >= num_logtype_strings) {
415 if (loglevel == NULL) {
418 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
419 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
422 if (levelnum >= num_loglevel_strings) {
427 /* is this a syslog setup or a filelog setup ? */
428 if (filename == NULL) {
430 syslog_setup(levelnum,
432 logoption_ndelay | logoption_pid,
435 /* this must be a filelog */
436 log_setup(filename, levelnum, typenum);
442 /* =========================================================================
443 Global function definitions
444 ========================================================================= */
446 /* This function sets up the processname */
447 void set_processname(const char *processname)
449 strncpy(log_config.processname, processname, 15);
450 log_config.processname[15] = 0;
453 /* -------------------------------------------------------------------------
454 make_log_entry has 1 main flaws:
455 The message in its entirity, must fit into the tempbuffer.
456 So it must be shorter than MAXLOGSIZE
457 ------------------------------------------------------------------------- */
458 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
459 const char *file, int line, char *message, ...)
461 /* fn is not reentrant but is used in signal handler
462 * with LOGGER it's a little late source name and line number
463 * are already changed. */
464 static int inlog = 0;
466 char temp_buffer[MAXLOGSIZE];
467 char log_details_buffer[MAXLOGSIZE];
476 if (!log_config.inited) {
480 if (type_configs[logtype].syslog) {
481 if (type_configs[logtype].level >= loglevel) {
482 /* Initialise the Messages and send it to syslog */
483 va_start(args, message);
484 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
486 temp_buffer[MAXLOGSIZE -1] = 0;
487 make_syslog_entry(loglevel, logtype, temp_buffer);
493 /* logging to a file */
495 log_src_filename = file;
496 log_src_linenumber = line;
498 /* Check if requested logtype is setup */
499 if (type_configs[logtype].set)
501 fd = type_configs[logtype].fd;
503 /* No: use default */
504 fd = type_configs[logtype_default].fd;
507 /* no where to send the output, give up */
511 /* Initialise the Messages */
512 va_start(args, message);
513 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
517 if (len ==-1 || len >= MAXLOGSIZE -1) {
518 /* vsnprintf hit the buffer size*/
519 temp_buffer[MAXLOGSIZE-2] = '\n';
520 temp_buffer[MAXLOGSIZE-1] = 0;
523 temp_buffer[len] = '\n';
524 temp_buffer[len+1] = 0;
527 if (type_configs[logtype].level >= log_debug)
528 goto log; /* bypass flooding checks */
530 /* Prevent flooding: hash the message and check if we got the same one recently */
531 int hash = hash_message(temp_buffer) + log_src_linenumber;
533 /* Search for the same message by hash */
534 for (int i = log_flood_entries - 1; i >= 0; i--) {
535 if (log_flood_array[i].hash == hash) {
537 /* found same message */
538 log_flood_array[i].count++;
540 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
541 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
542 /* yes, log it and remove from array */
544 /* reusing log_details_buffer */
545 sprintf(log_details_buffer, "message repeated %i times\n",
546 LOG_FLOODING_MAXCOUNT - 1);
547 write(fd, log_details_buffer, strlen(log_details_buffer));
549 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
550 /* last array element, just decrement count */
554 /* move array elements down */
555 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
556 log_flood_array[j-1] = log_flood_array[j];
560 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
568 /* No matching message found, add this message to array*/
569 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
570 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
571 if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
572 /* reusing log_details_buffer */
573 sprintf(log_details_buffer, "message repeated %i times\n",
574 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
575 write(fd, log_details_buffer, strlen(log_details_buffer));
577 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
578 log_flood_array[i-1] = log_flood_array[i];
582 log_flood_array[log_flood_entries].count = 1;
583 log_flood_array[log_flood_entries].hash = hash;
587 if ( ! log_config.console) {
588 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
589 type_configs[logtype].set ?
590 type_configs[logtype].display_options :
591 type_configs[logtype_default].display_options,
594 /* If default wasnt setup its fd is -1 */
595 iov[0].iov_base = log_details_buffer;
596 iov[0].iov_len = strlen(log_details_buffer);
597 iov[1].iov_base = temp_buffer;
598 iov[1].iov_len = strlen(temp_buffer);
601 write(fd, temp_buffer, strlen(temp_buffer));
608 void setuplog(const char *logstr, const char *logfile)
611 char *logtype, *loglevel;
614 save = ptr = strdup(logstr);
616 ptr = strtok(ptr, ", ");
620 while (*ptr && isspace(*ptr))
624 ptr = strpbrk(ptr, ":");
631 while (*ptr && !isspace(*ptr))
635 setuplog_internal(loglevel, logtype, logfile);
638 ptr = strtok(NULL, ", ");