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>
31 #include <atalk/boolean.h>
32 #include <atalk/util.h>
34 #include <atalk/logger.h>
36 #define OPEN_LOGS_AS_UID 0
38 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
40 #define MAXLOGSIZE 512
42 #define LOGLEVEL_STRING_IDENTIFIERS { \
56 /* these are the string identifiers corresponding to each logtype */
57 #define LOGTYPE_STRING_IDENTIFIERS { \
66 "end_of_list_marker"} \
68 /* =========================================================================
70 ========================================================================= */
72 /* Main log config container */
73 log_config_t log_config = { 0 };
75 /* Default log config: log nothing to files.
79 log_none: no logging by default
81 #define DEFAULT_LOG_CONFIG {0, 0, -1, log_none, 0}
83 UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
84 DEFAULT_LOG_CONFIG, /* logtype_default */
85 DEFAULT_LOG_CONFIG, /* logtype_logger */
86 DEFAULT_LOG_CONFIG, /* logtype_cnid */
87 DEFAULT_LOG_CONFIG, /* logtype_afpd */
88 DEFAULT_LOG_CONFIG, /* logtype_dsi */
89 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
90 DEFAULT_LOG_CONFIG, /* logtype_papd */
91 DEFAULT_LOG_CONFIG /* logtype_uams */
94 /* We use this in order to track the last n log messages in order to prevent flooding */
95 #define LOG_FLOODING_MAXCOUNT 10 /* this controls after how many consecutive messages we force a
96 "repeated x times" message */
97 #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */
98 struct log_flood_entry {
102 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
103 static int log_flood_entries;
105 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
106 static const char *log_src_filename;
107 static int log_src_linenumber;
109 /* Array to store text to list given a log type */
110 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
111 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
113 /* Array for charachters representing log severity in the log file */
114 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
115 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
117 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
118 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
120 /* =========================================================================
121 Internal function definitions
122 ========================================================================= */
124 /* Hash a log message */
125 static unsigned int hash_message(const char *message)
127 const char *p = message;
128 unsigned int hash = 0, i = 7;
139 * If filename == NULL its for syslog logging, otherwise its for file-logging.
140 * "unsetuplog" calls with loglevel == NULL.
141 * loglevel == NULL means:
142 * if logtype == default
145 * set to default logging
147 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
149 unsigned int typenum, levelnum;
152 for( typenum=0; typenum < num_logtype_strings; typenum++) {
153 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
156 if (typenum >= num_logtype_strings) {
161 if (loglevel == NULL) {
164 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
165 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
168 if (levelnum >= num_loglevel_strings) {
173 /* is this a syslog setup or a filelog setup ? */
174 if (filename == NULL) {
176 syslog_setup(levelnum,
178 logoption_ndelay | logoption_pid,
181 /* this must be a filelog */
182 log_setup(filename, levelnum, typenum);
188 static void generate_message_details(char *message_details_buffer,
189 int message_details_buffer_length,
191 enum loglevels loglevel, enum logtypes logtype)
193 char *ptr = message_details_buffer;
195 int len = message_details_buffer_length;
202 gettimeofday(&tv, NULL);
203 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
204 templen = strlen(ptr);
208 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
209 if (templen == -1 || templen >= len)
215 /* Process name && PID */
217 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
218 if (templen == -1 || templen >= len)
224 if ( ! (display_options & logoption_nsrcinfo)) {
225 char *basename = strrchr(log_src_filename, '/');
227 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
229 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
230 if (templen == -1 || templen >= len)
237 if (loglevel >= (num_loglevel_chars - 1))
238 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
240 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
242 if (templen == -1 || templen >= len)
248 if (logtype<num_logtype_strings) {
249 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
250 if (templen == -1 || templen >= len)
256 strncat(ptr, "): ", len);
260 static int get_syslog_equivalent(enum loglevels loglevel)
264 /* The question is we know how bad it is for us,
265 but how should that translate in the syslogs? */
270 case 3: /* warning */
274 case 5: /* information */
281 /* =========================================================================
282 Global function definitions
283 ========================================================================= */
287 syslog_setup(log_info,
289 logoption_ndelay | logoption_pid,
293 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
299 if (type_configs[logtype].set) {
300 if (type_configs[logtype].fd != -1)
301 close(type_configs[logtype].fd);
302 type_configs[logtype].fd = -1;
303 type_configs[logtype].level = -1;
304 type_configs[logtype].set = false;
306 /* if disabling default also set all "default using" levels to 0 */
307 if (logtype == logtype_default) {
308 while (logtype != logtype_end_of_list_marker) {
309 if ( ! (type_configs[logtype].set))
310 type_configs[logtype].level = -1;
319 if (NULL == filename)
322 /* Resetting existing config ? */
323 if (type_configs[logtype].set) {
324 if (type_configs[logtype].fd != -1)
325 close(type_configs[logtype].fd);
326 type_configs[logtype].fd = -1;
327 type_configs[logtype].level = -1;
328 type_configs[logtype].set = false;
329 type_configs[logtype].syslog = false;
331 /* Reset configs using default */
332 if (logtype == logtype_default) {
334 while (typeiter != logtype_end_of_list_marker) {
335 if (type_configs[typeiter].set == false) {
336 type_configs[typeiter].level = -1;
337 type_configs[typeiter].syslog = false;
345 type_configs[logtype].level = loglevel;
347 /* Open log file as OPEN_LOGS_AS_UID*/
349 /* Is it /dev/tty ? */
350 if (strcmp(filename, "/dev/tty") == 0) {
351 type_configs[logtype].fd = 1; /* stdout */
353 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
354 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
355 char *tmp = strdup(filename);
356 type_configs[logtype].fd = mkstemp(tmp);
360 process_uid = geteuid();
362 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
366 type_configs[logtype].fd = open(filename,
367 O_CREAT | O_WRONLY | O_APPEND,
368 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
370 if (seteuid(process_uid) == -1) {
371 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
377 /* Check for error opening/creating logfile */
378 if (type_configs[logtype].fd == -1) {
379 type_configs[logtype].level = -1;
380 type_configs[logtype].set = false;
384 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
385 type_configs[logtype].set = true;
386 log_config.inited = true;
388 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
389 /* which then uses the default logtype setup if it isn't setup itself: */
390 /* we just copy the loglevel from default to all logtypes that are not setup. */
391 /* In "make_log_entry" we then check for the logtypes if they arent setup */
392 /* and use default then. We must provide accessible values for all logtypes */
393 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
395 if (logtype == logtype_default) {
397 while (typeiter != logtype_end_of_list_marker) {
398 if ( ! (type_configs[typeiter].set))
399 type_configs[typeiter].level = loglevel;
404 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
405 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
408 /* Setup syslog logging */
409 void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
413 * this currently doesn't care if logtype is already logging to a file.
414 * Fortunately currently there's no way a user could trigger this as afpd.conf
415 * is not re-read on SIGHUP.
418 type_configs[logtype].level = loglevel;
419 type_configs[logtype].set = true;
420 type_configs[logtype].syslog = true;
421 log_config.syslog_display_options = display_options;
422 log_config.syslog_facility = facility;
424 /* Setting default logging? Then set all logtype not set individually */
425 if (logtype == logtype_default) {
427 while (typeiter != logtype_end_of_list_marker) {
428 if ( ! (type_configs[typeiter].set)) {
429 type_configs[typeiter].level = loglevel;
430 type_configs[typeiter].syslog = true;
436 log_config.inited = 1;
438 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
439 arr_loglevel_strings[loglevel]);
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 /* Called by the LOG macro for syslog messages */
454 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
456 if ( !log_config.syslog_opened ) {
457 openlog(log_config.processname,
458 log_config.syslog_display_options,
459 log_config.syslog_facility);
460 log_config.syslog_opened = true;
463 syslog(get_syslog_equivalent(loglevel), "%s", message);
466 /* -------------------------------------------------------------------------
467 make_log_entry has 1 main flaws:
468 The message in its entirity, must fit into the tempbuffer.
469 So it must be shorter than MAXLOGSIZE
470 ------------------------------------------------------------------------- */
471 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
472 const char *file, int line, char *message, ...)
474 /* fn is not reentrant but is used in signal handler
475 * with LOGGER it's a little late source name and line number
476 * are already changed. */
477 static int inlog = 0;
479 char temp_buffer[MAXLOGSIZE];
480 char log_details_buffer[MAXLOGSIZE];
489 if (!log_config.inited) {
493 if (type_configs[logtype].syslog) {
494 if (type_configs[logtype].level >= loglevel) {
495 /* Initialise the Messages and send it to syslog */
496 va_start(args, message);
497 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
499 temp_buffer[MAXLOGSIZE -1] = 0;
500 make_syslog_entry(loglevel, logtype, temp_buffer);
506 /* logging to a file */
508 log_src_filename = file;
509 log_src_linenumber = line;
511 /* Check if requested logtype is setup */
512 if (type_configs[logtype].set)
514 fd = type_configs[logtype].fd;
516 /* No: use default */
517 fd = type_configs[logtype_default].fd;
520 /* no where to send the output, give up */
524 /* Initialise the Messages */
525 va_start(args, message);
526 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
530 if (len ==-1 || len >= MAXLOGSIZE -1) {
531 /* vsnprintf hit the buffer size*/
532 temp_buffer[MAXLOGSIZE-2] = '\n';
533 temp_buffer[MAXLOGSIZE-1] = 0;
536 temp_buffer[len] = '\n';
537 temp_buffer[len+1] = 0;
540 /* Prevent flooding: hash the message and check if we got the same one recently */
541 int hash = hash_message(temp_buffer);
543 /* Search for the same message by hash */
544 for (int i = log_flood_entries - 1; i >= 0; i--) {
545 if (log_flood_array[i].hash == hash) {
547 /* found same message */
548 log_flood_array[i].count++;
550 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
551 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
552 /* yes, log it and remove from array */
554 /* reusing log_details_buffer */
555 sprintf(log_details_buffer, "message repeated %i times: ", log_flood_array[i].count);
556 iov[0].iov_base = log_details_buffer;
557 iov[0].iov_len = strlen(log_details_buffer);
558 iov[1].iov_base = temp_buffer;
559 iov[1].iov_len = strlen(temp_buffer);
561 /* Write "message repeated x times: ..." to log */
564 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
565 /* last array element, just decrement count */
569 /* move array elements down */
570 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
571 log_flood_array[j-1] = log_flood_array[j];
578 /* No matching message found, add this message to array*/
579 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
580 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
581 if (log_flood_array[0].count > 1) {
582 /* reusing log_details_buffer */
583 sprintf(log_details_buffer, "message repeated %i times: ", log_flood_array[0].count);
584 iov[0].iov_base = log_details_buffer;
585 iov[0].iov_len = strlen(log_details_buffer);
586 iov[1].iov_base = temp_buffer;
587 iov[1].iov_len = strlen(temp_buffer);
589 /* Write "message repeated x times: ..." to log */
592 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
593 log_flood_array[i-1] = log_flood_array[i];
597 log_flood_array[log_flood_entries-1].count = 1;
598 log_flood_array[log_flood_entries-1].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);