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 { \
64 "end_of_list_marker"} \
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_sl */
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)
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);
322 process_uid = geteuid();
324 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
328 type_configs[logtype].fd = open(filename,
329 O_CREAT | O_WRONLY | O_APPEND,
330 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
332 if (seteuid(process_uid) == -1) {
333 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
339 /* Check for error opening/creating logfile */
340 if (type_configs[logtype].fd == -1) {
341 type_configs[logtype].level = -1;
342 type_configs[logtype].set = false;
346 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
347 type_configs[logtype].set = true;
348 log_config.inited = true;
350 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
351 /* which then uses the default logtype setup if it isn't setup itself: */
352 /* we just copy the loglevel from default to all logtypes that are not setup. */
353 /* In "make_log_entry" we then check for the logtypes if they arent setup */
354 /* and use default then. We must provide accessible values for all logtypes */
355 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
357 if (logtype == logtype_default) {
359 while (typeiter != logtype_end_of_list_marker) {
360 if ( ! (type_configs[typeiter].set))
361 type_configs[typeiter].level = loglevel;
366 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
367 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
370 /* Setup syslog logging */
371 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
375 * this currently doesn't care if logtype is already logging to a file.
376 * Fortunately currently there's no way a user could trigger this as afpd.conf
377 * is not re-read on SIGHUP.
380 type_configs[logtype].level = loglevel;
381 type_configs[logtype].set = true;
382 type_configs[logtype].syslog = true;
383 log_config.syslog_display_options = display_options;
384 log_config.syslog_facility = facility;
386 /* Setting default logging? Then set all logtype not set individually */
387 if (logtype == logtype_default) {
389 while (typeiter != logtype_end_of_list_marker) {
390 if ( ! (type_configs[typeiter].set)) {
391 type_configs[typeiter].level = loglevel;
392 type_configs[typeiter].syslog = true;
398 log_config.inited = 1;
400 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
401 arr_loglevel_strings[loglevel]);
405 * If filename == NULL its for syslog logging, otherwise its for file-logging.
406 * "unsetuplog" calls with loglevel == NULL.
407 * loglevel == NULL means:
408 * if logtype == default
411 * set to default logging
413 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
415 unsigned int typenum, levelnum;
418 for( typenum=0; typenum < num_logtype_strings; typenum++) {
419 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
422 if (typenum >= num_logtype_strings) {
427 if (loglevel == NULL) {
430 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
431 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
434 if (levelnum >= num_loglevel_strings) {
439 /* is this a syslog setup or a filelog setup ? */
440 if (filename == NULL) {
442 syslog_setup(levelnum,
444 logoption_ndelay | logoption_pid,
447 /* this must be a filelog */
448 log_setup(filename, levelnum, typenum);
454 /* =========================================================================
455 Global function definitions
456 ========================================================================= */
458 /* This function sets up the processname */
459 void set_processname(const char *processname)
461 strncpy(log_config.processname, processname, 15);
462 log_config.processname[15] = 0;
465 /* -------------------------------------------------------------------------
466 make_log_entry has 1 main flaws:
467 The message in its entirity, must fit into the tempbuffer.
468 So it must be shorter than MAXLOGSIZE
469 ------------------------------------------------------------------------- */
470 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
471 const char *file, int line, char *message, ...)
473 /* fn is not reentrant but is used in signal handler
474 * with LOGGER it's a little late source name and line number
475 * are already changed. */
476 static int inlog = 0;
478 char temp_buffer[MAXLOGSIZE];
479 char log_details_buffer[MAXLOGSIZE];
488 if (!log_config.inited) {
492 if (type_configs[logtype].syslog) {
493 if (type_configs[logtype].level >= loglevel) {
494 /* Initialise the Messages and send it to syslog */
495 va_start(args, message);
496 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
498 temp_buffer[MAXLOGSIZE -1] = 0;
499 make_syslog_entry(loglevel, logtype, temp_buffer);
505 /* logging to a file */
507 log_src_filename = file;
508 log_src_linenumber = line;
510 /* Check if requested logtype is setup */
511 if (type_configs[logtype].set)
513 fd = type_configs[logtype].fd;
515 /* No: use default */
516 fd = type_configs[logtype_default].fd;
519 /* no where to send the output, give up */
523 /* Initialise the Messages */
524 va_start(args, message);
525 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
529 if (len ==-1 || len >= MAXLOGSIZE -1) {
530 /* vsnprintf hit the buffer size*/
531 temp_buffer[MAXLOGSIZE-2] = '\n';
532 temp_buffer[MAXLOGSIZE-1] = 0;
535 temp_buffer[len] = '\n';
536 temp_buffer[len+1] = 0;
539 if (type_configs[logtype].level >= log_debug)
540 goto log; /* bypass flooding checks */
542 /* Prevent flooding: hash the message and check if we got the same one recently */
543 int hash = hash_message(temp_buffer) + log_src_linenumber;
545 /* Search for the same message by hash */
546 for (int i = log_flood_entries - 1; i >= 0; i--) {
547 if (log_flood_array[i].hash == hash) {
549 /* found same message */
550 log_flood_array[i].count++;
552 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
553 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
554 /* yes, log it and remove from array */
556 /* reusing log_details_buffer */
557 sprintf(log_details_buffer, "message repeated %i times\n",
558 LOG_FLOODING_MAXCOUNT - 1);
559 write(fd, log_details_buffer, strlen(log_details_buffer));
561 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
562 /* last array element, just decrement count */
566 /* move array elements down */
567 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
568 log_flood_array[j-1] = log_flood_array[j];
572 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
580 /* No matching message found, add this message to array*/
581 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
582 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
583 if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
584 /* reusing log_details_buffer */
585 sprintf(log_details_buffer, "message repeated %i times\n",
586 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
587 write(fd, log_details_buffer, strlen(log_details_buffer));
589 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
590 log_flood_array[i-1] = log_flood_array[i];
594 log_flood_array[log_flood_entries].count = 1;
595 log_flood_array[log_flood_entries].hash = hash;
599 if ( ! log_config.console) {
600 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
601 type_configs[logtype].set ?
602 type_configs[logtype].display_options :
603 type_configs[logtype_default].display_options,
606 /* If default wasnt setup its fd is -1 */
607 iov[0].iov_base = log_details_buffer;
608 iov[0].iov_len = strlen(log_details_buffer);
609 iov[1].iov_base = temp_buffer;
610 iov[1].iov_len = strlen(temp_buffer);
613 write(fd, temp_buffer, strlen(temp_buffer));
620 void setuplog(const char *logstr, const char *logfile)
623 char *logtype, *loglevel;
626 save = ptr = strdup(logstr);
628 ptr = strtok(ptr, ", ");
632 while (*ptr && isspace(*ptr))
636 ptr = strpbrk(ptr, ":");
643 while (*ptr && !isspace(*ptr))
647 setuplog_internal(loglevel, logtype, logfile);
650 ptr = strtok(NULL, ", ");