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_MINCOUNT 5 /* this controls after how many consecutive messages must be detected
96 before we start to hide them */
97 #define LOG_FLOODING_MAXCOUNT 1000 /* this controls after how many consecutive messages we force a
98 "repeated x times" message */
99 #define LOG_FLOODING_ARRAY_SIZE 3 /* this contols how many messages in flow we track */
100 struct log_flood_entry {
104 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
105 static int log_flood_entries;
107 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
108 static const char *log_src_filename;
109 static int log_src_linenumber;
111 /* Array to store text to list given a log type */
112 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
113 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
115 /* Array for charachters representing log severity in the log file */
116 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
117 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
119 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
120 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
122 /* =========================================================================
123 Internal function definitions
124 ========================================================================= */
126 /* Hash a log message */
127 static unsigned int hash_message(const char *message)
129 const char *p = message;
130 unsigned int hash = 0, i = 7;
141 * If filename == NULL its for syslog logging, otherwise its for file-logging.
142 * "unsetuplog" calls with loglevel == NULL.
143 * loglevel == NULL means:
144 * if logtype == default
147 * set to default logging
149 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
151 unsigned int typenum, levelnum;
154 for( typenum=0; typenum < num_logtype_strings; typenum++) {
155 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
158 if (typenum >= num_logtype_strings) {
163 if (loglevel == NULL) {
166 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
167 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
170 if (levelnum >= num_loglevel_strings) {
175 /* is this a syslog setup or a filelog setup ? */
176 if (filename == NULL) {
178 syslog_setup(levelnum,
180 logoption_ndelay | logoption_pid,
183 /* this must be a filelog */
184 log_setup(filename, levelnum, typenum);
190 static void generate_message_details(char *message_details_buffer,
191 int message_details_buffer_length,
193 enum loglevels loglevel, enum logtypes logtype)
195 char *ptr = message_details_buffer;
197 int len = message_details_buffer_length;
204 gettimeofday(&tv, NULL);
205 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
206 templen = strlen(ptr);
210 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
211 if (templen == -1 || templen >= len)
217 /* Process name && PID */
219 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
220 if (templen == -1 || templen >= len)
226 if ( ! (display_options & logoption_nsrcinfo)) {
227 char *basename = strrchr(log_src_filename, '/');
229 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
231 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
232 if (templen == -1 || templen >= len)
239 if (loglevel >= (num_loglevel_chars - 1))
240 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
242 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
244 if (templen == -1 || templen >= len)
250 if (logtype<num_logtype_strings) {
251 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
252 if (templen == -1 || templen >= len)
258 strncat(ptr, "): ", len);
262 static int get_syslog_equivalent(enum loglevels loglevel)
266 /* The question is we know how bad it is for us,
267 but how should that translate in the syslogs? */
272 case 3: /* warning */
276 case 5: /* information */
283 /* =========================================================================
284 Global function definitions
285 ========================================================================= */
289 syslog_setup(log_info,
291 logoption_ndelay | logoption_pid,
295 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
301 if (type_configs[logtype].set) {
302 if (type_configs[logtype].fd != -1)
303 close(type_configs[logtype].fd);
304 type_configs[logtype].fd = -1;
305 type_configs[logtype].level = -1;
306 type_configs[logtype].set = false;
308 /* if disabling default also set all "default using" levels to 0 */
309 if (logtype == logtype_default) {
310 while (logtype != logtype_end_of_list_marker) {
311 if ( ! (type_configs[logtype].set))
312 type_configs[logtype].level = -1;
321 if (NULL == filename)
324 /* Resetting existing config ? */
325 if (type_configs[logtype].set) {
326 if (type_configs[logtype].fd != -1)
327 close(type_configs[logtype].fd);
328 type_configs[logtype].fd = -1;
329 type_configs[logtype].level = -1;
330 type_configs[logtype].set = false;
331 type_configs[logtype].syslog = false;
333 /* Reset configs using default */
334 if (logtype == logtype_default) {
336 while (typeiter != logtype_end_of_list_marker) {
337 if (type_configs[typeiter].set == false) {
338 type_configs[typeiter].level = -1;
339 type_configs[typeiter].syslog = false;
347 type_configs[logtype].level = loglevel;
349 /* Open log file as OPEN_LOGS_AS_UID*/
351 /* Is it /dev/tty ? */
352 if (strcmp(filename, "/dev/tty") == 0) {
353 type_configs[logtype].fd = 1; /* stdout */
355 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
356 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
357 char *tmp = strdup(filename);
358 type_configs[logtype].fd = mkstemp(tmp);
362 process_uid = geteuid();
364 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
368 type_configs[logtype].fd = open(filename,
369 O_CREAT | O_WRONLY | O_APPEND,
370 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
372 if (seteuid(process_uid) == -1) {
373 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
379 /* Check for error opening/creating logfile */
380 if (type_configs[logtype].fd == -1) {
381 type_configs[logtype].level = -1;
382 type_configs[logtype].set = false;
386 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
387 type_configs[logtype].set = true;
388 log_config.inited = true;
390 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
391 /* which then uses the default logtype setup if it isn't setup itself: */
392 /* we just copy the loglevel from default to all logtypes that are not setup. */
393 /* In "make_log_entry" we then check for the logtypes if they arent setup */
394 /* and use default then. We must provide accessible values for all logtypes */
395 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
397 if (logtype == logtype_default) {
399 while (typeiter != logtype_end_of_list_marker) {
400 if ( ! (type_configs[typeiter].set))
401 type_configs[typeiter].level = loglevel;
406 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
407 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
410 /* Setup syslog logging */
411 void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
415 * this currently doesn't care if logtype is already logging to a file.
416 * Fortunately currently there's no way a user could trigger this as afpd.conf
417 * is not re-read on SIGHUP.
420 type_configs[logtype].level = loglevel;
421 type_configs[logtype].set = true;
422 type_configs[logtype].syslog = true;
423 log_config.syslog_display_options = display_options;
424 log_config.syslog_facility = facility;
426 /* Setting default logging? Then set all logtype not set individually */
427 if (logtype == logtype_default) {
429 while (typeiter != logtype_end_of_list_marker) {
430 if ( ! (type_configs[typeiter].set)) {
431 type_configs[typeiter].level = loglevel;
432 type_configs[typeiter].syslog = true;
438 log_config.inited = 1;
440 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
441 arr_loglevel_strings[loglevel]);
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 /* Called by the LOG macro for syslog messages */
456 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
458 if ( !log_config.syslog_opened ) {
459 openlog(log_config.processname,
460 log_config.syslog_display_options,
461 log_config.syslog_facility);
462 log_config.syslog_opened = true;
465 syslog(get_syslog_equivalent(loglevel), "%s", message);
468 /* -------------------------------------------------------------------------
469 make_log_entry has 1 main flaws:
470 The message in its entirity, must fit into the tempbuffer.
471 So it must be shorter than MAXLOGSIZE
472 ------------------------------------------------------------------------- */
473 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
474 const char *file, int line, char *message, ...)
476 /* fn is not reentrant but is used in signal handler
477 * with LOGGER it's a little late source name and line number
478 * are already changed. */
479 static int inlog = 0;
481 char temp_buffer[MAXLOGSIZE];
482 char log_details_buffer[MAXLOGSIZE];
491 if (!log_config.inited) {
495 if (type_configs[logtype].syslog) {
496 if (type_configs[logtype].level >= loglevel) {
497 /* Initialise the Messages and send it to syslog */
498 va_start(args, message);
499 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
501 temp_buffer[MAXLOGSIZE -1] = 0;
502 make_syslog_entry(loglevel, logtype, temp_buffer);
508 /* logging to a file */
510 log_src_filename = file;
511 log_src_linenumber = line;
513 /* Check if requested logtype is setup */
514 if (type_configs[logtype].set)
516 fd = type_configs[logtype].fd;
518 /* No: use default */
519 fd = type_configs[logtype_default].fd;
522 /* no where to send the output, give up */
526 /* Initialise the Messages */
527 va_start(args, message);
528 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
532 if (len ==-1 || len >= MAXLOGSIZE -1) {
533 /* vsnprintf hit the buffer size*/
534 temp_buffer[MAXLOGSIZE-2] = '\n';
535 temp_buffer[MAXLOGSIZE-1] = 0;
538 temp_buffer[len] = '\n';
539 temp_buffer[len+1] = 0;
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));
621 void setuplog(const char *logstr)
623 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
624 ptr = strdup(logstr);
631 ptr = strpbrk(ptr, " \t");
634 while (*ptr && isspace(*ptr))
639 ptr = strpbrk(ptr, " \t");
642 while (*ptr && isspace(*ptr))
646 if (filename && *filename == 0)
650 /* finally call setuplog, filename can be NULL */
651 setuplog_internal(loglevel, logtype, filename);
656 void unsetuplog(const char *logstr)
658 char *str, *logtype, *filename;
660 str = strdup(logstr);
665 /* get filename, can be NULL */
667 filename = strtok(NULL, " \t");
669 /* finally call setuplog, filename can be NULL */
670 setuplog_internal(NULL, str, filename);