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 { \
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 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility);
95 /* We use this in order to track the last n log messages in order to prevent flooding */
96 #define LOG_FLOODING_MINCOUNT 5 /* this controls after how many consecutive messages must be detected
97 before we start to hide them */
98 #define LOG_FLOODING_MAXCOUNT 1000 /* this controls after how many consecutive messages we force a
99 "repeated x times" message */
100 #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */
102 struct log_flood_entry {
107 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
108 static int log_flood_entries;
110 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
111 static const char *log_src_filename;
112 static int log_src_linenumber;
114 /* Array to store text to list given a log type */
115 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
116 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
118 /* Array for charachters representing log severity in the log file */
119 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
120 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
122 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
123 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
125 /* =========================================================================
126 Internal function definitions
127 ========================================================================= */
129 /* Hash a log message */
130 static unsigned int hash_message(const char *message)
132 const char *p = message;
133 unsigned int hash = 0, i = 7;
143 static void generate_message_details(char *message_details_buffer,
144 int message_details_buffer_length,
146 enum loglevels loglevel, enum logtypes logtype)
148 char *ptr = message_details_buffer;
150 int len = message_details_buffer_length;
157 gettimeofday(&tv, NULL);
158 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
159 templen = strlen(ptr);
163 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
164 if (templen == -1 || templen >= len)
170 /* Process name && PID */
172 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
173 if (templen == -1 || templen >= len)
179 if ( ! (display_options & logoption_nsrcinfo)) {
180 char *basename = strrchr(log_src_filename, '/');
182 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
184 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
185 if (templen == -1 || templen >= len)
192 if (loglevel >= (num_loglevel_chars - 1))
193 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
195 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
197 if (templen == -1 || templen >= len)
203 if (logtype<num_logtype_strings) {
204 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
205 if (templen == -1 || templen >= len)
211 strncat(ptr, "): ", len);
215 static int get_syslog_equivalent(enum loglevels loglevel)
219 /* The question is we know how bad it is for us,
220 but how should that translate in the syslogs? */
225 case 3: /* warning */
229 case 5: /* information */
236 /* Called by the LOG macro for syslog messages */
237 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
239 if ( !log_config.syslog_opened ) {
240 openlog(log_config.processname,
241 log_config.syslog_display_options,
242 log_config.syslog_facility);
243 log_config.syslog_opened = true;
246 syslog(get_syslog_equivalent(loglevel), "%s", message);
249 static void log_init(void)
251 syslog_setup(log_info,
253 logoption_ndelay | logoption_pid,
257 static void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
261 if (type_configs[logtype].set) {
262 if (type_configs[logtype].fd != -1)
263 close(type_configs[logtype].fd);
264 type_configs[logtype].fd = -1;
265 type_configs[logtype].level = -1;
266 type_configs[logtype].set = false;
268 /* if disabling default also set all "default using" levels to 0 */
269 if (logtype == logtype_default) {
270 while (logtype != logtype_end_of_list_marker) {
271 if ( ! (type_configs[logtype].set))
272 type_configs[logtype].level = -1;
281 if (NULL == filename)
284 /* Resetting existing config ? */
285 if (type_configs[logtype].set) {
286 if (type_configs[logtype].fd != -1)
287 close(type_configs[logtype].fd);
288 type_configs[logtype].fd = -1;
289 type_configs[logtype].level = -1;
290 type_configs[logtype].set = false;
291 type_configs[logtype].syslog = false;
293 /* Reset configs using default */
294 if (logtype == logtype_default) {
296 while (typeiter != logtype_end_of_list_marker) {
297 if (type_configs[typeiter].set == false) {
298 type_configs[typeiter].level = -1;
299 type_configs[typeiter].syslog = false;
307 type_configs[logtype].level = loglevel;
309 /* Open log file as OPEN_LOGS_AS_UID*/
311 /* Is it /dev/tty ? */
312 if (strcmp(filename, "/dev/tty") == 0) {
313 type_configs[logtype].fd = 1; /* stdout */
315 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
316 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
317 char *tmp = strdup(filename);
318 type_configs[logtype].fd = mkstemp(tmp);
323 type_configs[logtype].fd = open(filename,
324 O_CREAT | O_WRONLY | O_APPEND,
325 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
329 /* Check for error opening/creating logfile */
330 if (type_configs[logtype].fd == -1) {
331 type_configs[logtype].level = -1;
332 type_configs[logtype].set = false;
336 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
337 type_configs[logtype].set = true;
338 log_config.inited = true;
340 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
341 /* which then uses the default logtype setup if it isn't setup itself: */
342 /* we just copy the loglevel from default to all logtypes that are not setup. */
343 /* In "make_log_entry" we then check for the logtypes if they arent setup */
344 /* and use default then. We must provide accessible values for all logtypes */
345 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
347 if (logtype == logtype_default) {
349 while (typeiter != logtype_end_of_list_marker) {
350 if ( ! (type_configs[typeiter].set))
351 type_configs[typeiter].level = loglevel;
356 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
357 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
360 /* Setup syslog logging */
361 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
365 * this currently doesn't care if logtype is already logging to a file.
366 * Fortunately currently there's no way a user could trigger this as afpd.conf
367 * is not re-read on SIGHUP.
370 type_configs[logtype].level = loglevel;
371 type_configs[logtype].set = true;
372 type_configs[logtype].syslog = true;
373 log_config.syslog_display_options = display_options;
374 log_config.syslog_facility = facility;
376 /* Setting default logging? Then set all logtype not set individually */
377 if (logtype == logtype_default) {
379 while (typeiter != logtype_end_of_list_marker) {
380 if ( ! (type_configs[typeiter].set)) {
381 type_configs[typeiter].level = loglevel;
382 type_configs[typeiter].syslog = true;
388 log_config.inited = 1;
390 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
391 arr_loglevel_strings[loglevel]);
395 * If filename == NULL its for syslog logging, otherwise its for file-logging.
396 * "unsetuplog" calls with loglevel == NULL.
397 * loglevel == NULL means:
398 * if logtype == default
401 * set to default logging
403 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
405 unsigned int typenum, levelnum;
408 for( typenum=0; typenum < num_logtype_strings; typenum++) {
409 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
412 if (typenum >= num_logtype_strings) {
417 if (loglevel == NULL) {
420 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
421 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
424 if (levelnum >= num_loglevel_strings) {
429 /* is this a syslog setup or a filelog setup ? */
430 if (filename == NULL) {
432 syslog_setup(levelnum,
434 logoption_ndelay | logoption_pid,
437 /* this must be a filelog */
438 log_setup(filename, levelnum, typenum);
444 /* =========================================================================
445 Global function definitions
446 ========================================================================= */
448 /* This function sets up the processname */
449 void set_processname(const char *processname)
451 strncpy(log_config.processname, processname, 15);
452 log_config.processname[15] = 0;
455 /* -------------------------------------------------------------------------
456 make_log_entry has 1 main flaws:
457 The message in its entirity, must fit into the tempbuffer.
458 So it must be shorter than MAXLOGSIZE
459 ------------------------------------------------------------------------- */
460 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
461 const char *file, int line, char *message, ...)
463 /* fn is not reentrant but is used in signal handler
464 * with LOGGER it's a little late source name and line number
465 * are already changed. */
466 static int inlog = 0;
468 char temp_buffer[MAXLOGSIZE];
469 char log_details_buffer[MAXLOGSIZE];
478 if (!log_config.inited) {
482 if (type_configs[logtype].syslog) {
483 if (type_configs[logtype].level >= loglevel) {
484 /* Initialise the Messages and send it to syslog */
485 va_start(args, message);
486 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
488 temp_buffer[MAXLOGSIZE -1] = 0;
489 make_syslog_entry(loglevel, logtype, temp_buffer);
495 /* logging to a file */
497 log_src_filename = file;
498 log_src_linenumber = line;
500 /* Check if requested logtype is setup */
501 if (type_configs[logtype].set)
503 fd = type_configs[logtype].fd;
505 /* No: use default */
506 fd = type_configs[logtype_default].fd;
509 /* no where to send the output, give up */
513 /* Initialise the Messages */
514 va_start(args, message);
515 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
519 if (len ==-1 || len >= MAXLOGSIZE -1) {
520 /* vsnprintf hit the buffer size*/
521 temp_buffer[MAXLOGSIZE-2] = '\n';
522 temp_buffer[MAXLOGSIZE-1] = 0;
525 temp_buffer[len] = '\n';
526 temp_buffer[len+1] = 0;
529 if (type_configs[logtype].level >= log_debug)
530 goto log; /* bypass flooding checks */
532 /* Prevent flooding: hash the message and check if we got the same one recently */
533 int hash = hash_message(temp_buffer) + log_src_linenumber;
535 /* Search for the same message by hash */
536 for (int i = log_flood_entries - 1; i >= 0; i--) {
537 if (log_flood_array[i].hash == hash) {
539 /* found same message */
540 log_flood_array[i].count++;
542 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
543 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
544 /* yes, log it and remove from array */
546 /* reusing log_details_buffer */
547 sprintf(log_details_buffer, "message repeated %i times\n",
548 LOG_FLOODING_MAXCOUNT - 1);
549 write(fd, log_details_buffer, strlen(log_details_buffer));
551 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
552 /* last array element, just decrement count */
556 /* move array elements down */
557 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
558 log_flood_array[j-1] = log_flood_array[j];
562 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
570 /* No matching message found, add this message to array*/
571 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
572 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
573 if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
574 /* reusing log_details_buffer */
575 sprintf(log_details_buffer, "message repeated %i times\n",
576 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
577 write(fd, log_details_buffer, strlen(log_details_buffer));
579 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
580 log_flood_array[i-1] = log_flood_array[i];
584 log_flood_array[log_flood_entries].count = 1;
585 log_flood_array[log_flood_entries].hash = hash;
589 if ( ! log_config.console) {
590 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
591 type_configs[logtype].set ?
592 type_configs[logtype].display_options :
593 type_configs[logtype_default].display_options,
596 /* If default wasnt setup its fd is -1 */
597 iov[0].iov_base = log_details_buffer;
598 iov[0].iov_len = strlen(log_details_buffer);
599 iov[1].iov_base = temp_buffer;
600 iov[1].iov_len = strlen(temp_buffer);
603 write(fd, temp_buffer, strlen(temp_buffer));
610 void setuplog(const char *logstr, const char *logfile)
613 char *logtype, *loglevel;
616 save = ptr = strdup(logstr);
618 ptr = strtok(ptr, ", ");
622 while (*ptr && isspace(*ptr))
626 ptr = strpbrk(ptr, ":");
633 while (*ptr && !isspace(*ptr))
637 setuplog_internal(loglevel, logtype, logfile);
640 ptr = strtok(NULL, ", ");