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 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)
263 if (type_configs[logtype].set) {
264 if (type_configs[logtype].fd != -1)
265 close(type_configs[logtype].fd);
266 type_configs[logtype].fd = -1;
267 type_configs[logtype].level = -1;
268 type_configs[logtype].set = false;
270 /* if disabling default also set all "default using" levels to 0 */
271 if (logtype == logtype_default) {
272 while (logtype != logtype_end_of_list_marker) {
273 if ( ! (type_configs[logtype].set))
274 type_configs[logtype].level = -1;
283 if (NULL == filename)
286 /* Resetting existing config ? */
287 if (type_configs[logtype].set) {
288 if (type_configs[logtype].fd != -1)
289 close(type_configs[logtype].fd);
290 type_configs[logtype].fd = -1;
291 type_configs[logtype].level = -1;
292 type_configs[logtype].set = false;
293 type_configs[logtype].syslog = false;
295 /* Reset configs using default */
296 if (logtype == logtype_default) {
298 while (typeiter != logtype_end_of_list_marker) {
299 if (type_configs[typeiter].set == false) {
300 type_configs[typeiter].level = -1;
301 type_configs[typeiter].syslog = false;
309 type_configs[logtype].level = loglevel;
311 /* Open log file as OPEN_LOGS_AS_UID*/
313 /* Is it /dev/tty ? */
314 if (strcmp(filename, "/dev/tty") == 0) {
315 type_configs[logtype].fd = 1; /* stdout */
317 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
318 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
319 char *tmp = strdup(filename);
320 type_configs[logtype].fd = mkstemp(tmp);
324 process_uid = geteuid();
326 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
330 type_configs[logtype].fd = open(filename,
331 O_CREAT | O_WRONLY | O_APPEND,
332 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
334 if (seteuid(process_uid) == -1) {
335 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
341 /* Check for error opening/creating logfile */
342 if (type_configs[logtype].fd == -1) {
343 type_configs[logtype].level = -1;
344 type_configs[logtype].set = false;
348 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
349 type_configs[logtype].set = true;
350 log_config.inited = true;
352 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
353 /* which then uses the default logtype setup if it isn't setup itself: */
354 /* we just copy the loglevel from default to all logtypes that are not setup. */
355 /* In "make_log_entry" we then check for the logtypes if they arent setup */
356 /* and use default then. We must provide accessible values for all logtypes */
357 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
359 if (logtype == logtype_default) {
361 while (typeiter != logtype_end_of_list_marker) {
362 if ( ! (type_configs[typeiter].set))
363 type_configs[typeiter].level = loglevel;
368 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
369 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
372 /* Setup syslog logging */
373 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
377 * this currently doesn't care if logtype is already logging to a file.
378 * Fortunately currently there's no way a user could trigger this as afpd.conf
379 * is not re-read on SIGHUP.
382 type_configs[logtype].level = loglevel;
383 type_configs[logtype].set = true;
384 type_configs[logtype].syslog = true;
385 log_config.syslog_display_options = display_options;
386 log_config.syslog_facility = facility;
388 /* Setting default logging? Then set all logtype not set individually */
389 if (logtype == logtype_default) {
391 while (typeiter != logtype_end_of_list_marker) {
392 if ( ! (type_configs[typeiter].set)) {
393 type_configs[typeiter].level = loglevel;
394 type_configs[typeiter].syslog = true;
400 log_config.inited = 1;
402 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
403 arr_loglevel_strings[loglevel]);
407 * If filename == NULL its for syslog logging, otherwise its for file-logging.
408 * "unsetuplog" calls with loglevel == NULL.
409 * loglevel == NULL means:
410 * if logtype == default
413 * set to default logging
415 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
417 unsigned int typenum, levelnum;
420 for( typenum=0; typenum < num_logtype_strings; typenum++) {
421 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
424 if (typenum >= num_logtype_strings) {
429 if (loglevel == NULL) {
432 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
433 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
436 if (levelnum >= num_loglevel_strings) {
441 /* is this a syslog setup or a filelog setup ? */
442 if (filename == NULL) {
444 syslog_setup(levelnum,
446 logoption_ndelay | logoption_pid,
449 /* this must be a filelog */
450 log_setup(filename, levelnum, typenum);
456 /* =========================================================================
457 Global function definitions
458 ========================================================================= */
460 /* This function sets up the processname */
461 void set_processname(const char *processname)
463 strncpy(log_config.processname, processname, 15);
464 log_config.processname[15] = 0;
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));
622 void setuplog(const char *logstr, const char *logfile)
625 char *logtype, *loglevel;
628 save = ptr = strdup(logstr);
630 ptr = strtok(ptr, ", ");
634 while (*ptr && isspace(*ptr))
638 ptr = strpbrk(ptr, ":");
645 while (*ptr && !isspace(*ptr))
649 setuplog_internal(loglevel, logtype, logfile);
652 ptr = strtok(NULL, ", ");