5 /* =========================================================================
7 logger.c was written by Simon Bazley (sibaz@sibaz.com)
9 I believe libatalk is released under the L/GPL licence.
10 Just incase, it is, thats the licence I'm applying to this file.
13 ========================================================================= */
21 #include <sys/types.h>
32 #include <atalk/util.h>
33 #include <atalk/logger.h>
35 #define OPEN_LOGS_AS_UID 0
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 { \
65 "end_of_list_marker"} \
67 /* =========================================================================
69 ========================================================================= */
71 /* Main log config container */
72 log_config_t log_config = { 0 };
74 /* Default log config: log nothing to files.
78 log_none: no logging by default
80 #define DEFAULT_LOG_CONFIG {0, 0, -1, log_none, 0}
82 UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
83 DEFAULT_LOG_CONFIG, /* logtype_default */
84 DEFAULT_LOG_CONFIG, /* logtype_logger */
85 DEFAULT_LOG_CONFIG, /* logtype_cnid */
86 DEFAULT_LOG_CONFIG, /* logtype_afpd */
87 DEFAULT_LOG_CONFIG, /* logtype_dsi */
88 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
89 DEFAULT_LOG_CONFIG, /* logtype_papd */
90 DEFAULT_LOG_CONFIG /* logtype_uams */
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 */
99 struct log_flood_entry {
103 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
104 static int log_flood_entries;
106 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
107 static const char *log_src_filename;
108 static int log_src_linenumber;
110 /* Array to store text to list given a log type */
111 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
112 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
114 /* Array for charachters representing log severity in the log file */
115 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
116 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
118 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
119 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
121 /* =========================================================================
122 Internal function definitions
123 ========================================================================= */
125 /* Hash a log message */
126 static unsigned int hash_message(const char *message)
128 const char *p = message;
129 unsigned int hash = 0, i = 7;
140 * If filename == NULL its for syslog logging, otherwise its for file-logging.
141 * "unsetuplog" calls with loglevel == NULL.
142 * loglevel == NULL means:
143 * if logtype == default
146 * set to default logging
148 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
150 unsigned int typenum, levelnum;
153 for( typenum=0; typenum < num_logtype_strings; typenum++) {
154 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
157 if (typenum >= num_logtype_strings) {
162 if (loglevel == NULL) {
165 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
166 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
169 if (levelnum >= num_loglevel_strings) {
174 /* is this a syslog setup or a filelog setup ? */
175 if (filename == NULL) {
177 syslog_setup(levelnum,
179 logoption_ndelay | logoption_pid,
182 /* this must be a filelog */
183 log_setup(filename, levelnum, typenum);
189 static void generate_message_details(char *message_details_buffer,
190 int message_details_buffer_length,
192 enum loglevels loglevel, enum logtypes logtype)
194 char *ptr = message_details_buffer;
196 int len = message_details_buffer_length;
203 gettimeofday(&tv, NULL);
204 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
205 templen = strlen(ptr);
209 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
210 if (templen == -1 || templen >= len)
216 /* Process name && PID */
218 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
219 if (templen == -1 || templen >= len)
225 if ( ! (display_options & logoption_nsrcinfo)) {
226 char *basename = strrchr(log_src_filename, '/');
228 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
230 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
231 if (templen == -1 || templen >= len)
238 if (loglevel >= (num_loglevel_chars - 1))
239 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
241 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
243 if (templen == -1 || templen >= len)
249 if (logtype<num_logtype_strings) {
250 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
251 if (templen == -1 || templen >= len)
257 strncat(ptr, "): ", len);
261 static int get_syslog_equivalent(enum loglevels loglevel)
265 /* The question is we know how bad it is for us,
266 but how should that translate in the syslogs? */
271 case 3: /* warning */
275 case 5: /* information */
282 /* =========================================================================
283 Global function definitions
284 ========================================================================= */
288 syslog_setup(log_info,
290 logoption_ndelay | logoption_pid,
294 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
300 if (type_configs[logtype].set) {
301 if (type_configs[logtype].fd != -1)
302 close(type_configs[logtype].fd);
303 type_configs[logtype].fd = -1;
304 type_configs[logtype].level = -1;
305 type_configs[logtype].set = false;
307 /* if disabling default also set all "default using" levels to 0 */
308 if (logtype == logtype_default) {
309 while (logtype != logtype_end_of_list_marker) {
310 if ( ! (type_configs[logtype].set))
311 type_configs[logtype].level = -1;
320 if (NULL == filename)
323 /* Resetting existing config ? */
324 if (type_configs[logtype].set) {
325 if (type_configs[logtype].fd != -1)
326 close(type_configs[logtype].fd);
327 type_configs[logtype].fd = -1;
328 type_configs[logtype].level = -1;
329 type_configs[logtype].set = false;
330 type_configs[logtype].syslog = false;
332 /* Reset configs using default */
333 if (logtype == logtype_default) {
335 while (typeiter != logtype_end_of_list_marker) {
336 if (type_configs[typeiter].set == false) {
337 type_configs[typeiter].level = -1;
338 type_configs[typeiter].syslog = false;
346 type_configs[logtype].level = loglevel;
348 /* Open log file as OPEN_LOGS_AS_UID*/
350 /* Is it /dev/tty ? */
351 if (strcmp(filename, "/dev/tty") == 0) {
352 type_configs[logtype].fd = 1; /* stdout */
354 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
355 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
356 char *tmp = strdup(filename);
357 type_configs[logtype].fd = mkstemp(tmp);
361 process_uid = geteuid();
363 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
367 type_configs[logtype].fd = open(filename,
368 O_CREAT | O_WRONLY | O_APPEND,
369 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
371 if (seteuid(process_uid) == -1) {
372 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
378 /* Check for error opening/creating logfile */
379 if (type_configs[logtype].fd == -1) {
380 type_configs[logtype].level = -1;
381 type_configs[logtype].set = false;
385 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
386 type_configs[logtype].set = true;
387 log_config.inited = true;
389 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
390 /* which then uses the default logtype setup if it isn't setup itself: */
391 /* we just copy the loglevel from default to all logtypes that are not setup. */
392 /* In "make_log_entry" we then check for the logtypes if they arent setup */
393 /* and use default then. We must provide accessible values for all logtypes */
394 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
396 if (logtype == logtype_default) {
398 while (typeiter != logtype_end_of_list_marker) {
399 if ( ! (type_configs[typeiter].set))
400 type_configs[typeiter].level = loglevel;
405 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
406 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
409 /* Setup syslog logging */
410 void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
414 * this currently doesn't care if logtype is already logging to a file.
415 * Fortunately currently there's no way a user could trigger this as afpd.conf
416 * is not re-read on SIGHUP.
419 type_configs[logtype].level = loglevel;
420 type_configs[logtype].set = true;
421 type_configs[logtype].syslog = true;
422 log_config.syslog_display_options = display_options;
423 log_config.syslog_facility = facility;
425 /* Setting default logging? Then set all logtype not set individually */
426 if (logtype == logtype_default) {
428 while (typeiter != logtype_end_of_list_marker) {
429 if ( ! (type_configs[typeiter].set)) {
430 type_configs[typeiter].level = loglevel;
431 type_configs[typeiter].syslog = true;
437 log_config.inited = 1;
439 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
440 arr_loglevel_strings[loglevel]);
447 /* This function sets up the processname */
448 void set_processname(const char *processname)
450 strncpy(log_config.processname, processname, 15);
451 log_config.processname[15] = 0;
454 /* Called by the LOG macro for syslog messages */
455 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
457 if ( !log_config.syslog_opened ) {
458 openlog(log_config.processname,
459 log_config.syslog_display_options,
460 log_config.syslog_facility);
461 log_config.syslog_opened = true;
464 syslog(get_syslog_equivalent(loglevel), "%s", message);
467 /* -------------------------------------------------------------------------
468 make_log_entry has 1 main flaws:
469 The message in its entirity, must fit into the tempbuffer.
470 So it must be shorter than MAXLOGSIZE
471 ------------------------------------------------------------------------- */
472 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
473 const char *file, int line, char *message, ...)
475 /* fn is not reentrant but is used in signal handler
476 * with LOGGER it's a little late source name and line number
477 * are already changed. */
478 static int inlog = 0;
480 char temp_buffer[MAXLOGSIZE];
481 char log_details_buffer[MAXLOGSIZE];
490 if (!log_config.inited) {
494 if (type_configs[logtype].syslog) {
495 if (type_configs[logtype].level >= loglevel) {
496 /* Initialise the Messages and send it to syslog */
497 va_start(args, message);
498 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
500 temp_buffer[MAXLOGSIZE -1] = 0;
501 make_syslog_entry(loglevel, logtype, temp_buffer);
507 /* logging to a file */
509 log_src_filename = file;
510 log_src_linenumber = line;
512 /* Check if requested logtype is setup */
513 if (type_configs[logtype].set)
515 fd = type_configs[logtype].fd;
517 /* No: use default */
518 fd = type_configs[logtype_default].fd;
521 /* no where to send the output, give up */
525 /* Initialise the Messages */
526 va_start(args, message);
527 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
531 if (len ==-1 || len >= MAXLOGSIZE -1) {
532 /* vsnprintf hit the buffer size*/
533 temp_buffer[MAXLOGSIZE-2] = '\n';
534 temp_buffer[MAXLOGSIZE-1] = 0;
537 temp_buffer[len] = '\n';
538 temp_buffer[len+1] = 0;
541 if (type_configs[logtype].level >= log_debug)
542 goto log; /* bypass flooding checks */
544 /* Prevent flooding: hash the message and check if we got the same one recently */
545 int hash = hash_message(temp_buffer) + log_src_linenumber;
547 /* Search for the same message by hash */
548 for (int i = log_flood_entries - 1; i >= 0; i--) {
549 if (log_flood_array[i].hash == hash) {
551 /* found same message */
552 log_flood_array[i].count++;
554 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
555 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
556 /* yes, log it and remove from array */
558 /* reusing log_details_buffer */
559 sprintf(log_details_buffer, "message repeated %i times\n",
560 LOG_FLOODING_MAXCOUNT - 1);
561 write(fd, log_details_buffer, strlen(log_details_buffer));
563 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
564 /* last array element, just decrement count */
568 /* move array elements down */
569 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
570 log_flood_array[j-1] = log_flood_array[j];
574 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
582 /* No matching message found, add this message to array*/
583 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
584 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
585 if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
586 /* reusing log_details_buffer */
587 sprintf(log_details_buffer, "message repeated %i times\n",
588 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
589 write(fd, log_details_buffer, strlen(log_details_buffer));
591 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
592 log_flood_array[i-1] = log_flood_array[i];
596 log_flood_array[log_flood_entries].count = 1;
597 log_flood_array[log_flood_entries].hash = hash;
601 if ( ! log_config.console) {
602 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
603 type_configs[logtype].set ?
604 type_configs[logtype].display_options :
605 type_configs[logtype_default].display_options,
608 /* If default wasnt setup its fd is -1 */
609 iov[0].iov_base = log_details_buffer;
610 iov[0].iov_len = strlen(log_details_buffer);
611 iov[1].iov_base = temp_buffer;
612 iov[1].iov_len = strlen(temp_buffer);
615 write(fd, temp_buffer, strlen(temp_buffer));
623 void setuplog(const char *logstr)
625 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
626 ptr = strdup(logstr);
633 ptr = strpbrk(ptr, " \t");
636 while (*ptr && isspace(*ptr))
641 ptr = strpbrk(ptr, " \t");
644 while (*ptr && isspace(*ptr))
648 if (filename && *filename == 0)
652 /* finally call setuplog, filename can be NULL */
653 setuplog_internal(loglevel, logtype, filename);
658 void unsetuplog(const char *logstr)
660 char *str, *logtype, *filename;
662 str = strdup(logstr);
667 /* get filename, can be NULL */
669 filename = strtok(NULL, " \t");
671 /* finally call setuplog, filename can be NULL */
672 setuplog_internal(NULL, str, filename);