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_atalkd */
88 DEFAULT_LOG_CONFIG, /* logtype_papd */
89 DEFAULT_LOG_CONFIG /* logtype_uams */
92 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility);
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 */
101 struct log_flood_entry {
106 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
107 static int log_flood_entries;
109 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
110 static const char *log_src_filename;
111 static int log_src_linenumber;
113 /* Array to store text to list given a log type */
114 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
115 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
117 /* Array for charachters representing log severity in the log file */
118 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
119 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
121 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
122 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
124 /* =========================================================================
125 Internal function definitions
126 ========================================================================= */
128 /* Hash a log message */
129 static unsigned int hash_message(const char *message)
131 const char *p = message;
132 unsigned int hash = 0, i = 7;
142 static void generate_message_details(char *message_details_buffer,
143 int message_details_buffer_length,
145 enum loglevels loglevel, enum logtypes logtype)
147 char *ptr = message_details_buffer;
149 int len = message_details_buffer_length;
156 gettimeofday(&tv, NULL);
157 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
158 templen = strlen(ptr);
162 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
163 if (templen == -1 || templen >= len)
169 /* Process name && PID */
171 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
172 if (templen == -1 || templen >= len)
178 if ( ! (display_options & logoption_nsrcinfo)) {
179 char *basename = strrchr(log_src_filename, '/');
181 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
183 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
184 if (templen == -1 || templen >= len)
191 if (loglevel >= (num_loglevel_chars - 1))
192 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
194 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
196 if (templen == -1 || templen >= len)
202 if (logtype<num_logtype_strings) {
203 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
204 if (templen == -1 || templen >= len)
210 strncat(ptr, "): ", len);
214 static int get_syslog_equivalent(enum loglevels loglevel)
218 /* The question is we know how bad it is for us,
219 but how should that translate in the syslogs? */
224 case 3: /* warning */
228 case 5: /* information */
235 /* Called by the LOG macro for syslog messages */
236 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
238 if ( !log_config.syslog_opened ) {
239 openlog(log_config.processname,
240 log_config.syslog_display_options,
241 log_config.syslog_facility);
242 log_config.syslog_opened = true;
245 syslog(get_syslog_equivalent(loglevel), "%s", message);
248 static void log_init(void)
250 syslog_setup(log_info,
252 logoption_ndelay | logoption_pid,
256 static void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
262 if (type_configs[logtype].set) {
263 if (type_configs[logtype].fd != -1)
264 close(type_configs[logtype].fd);
265 type_configs[logtype].fd = -1;
266 type_configs[logtype].level = -1;
267 type_configs[logtype].set = false;
269 /* if disabling default also set all "default using" levels to 0 */
270 if (logtype == logtype_default) {
271 while (logtype != logtype_end_of_list_marker) {
272 if ( ! (type_configs[logtype].set))
273 type_configs[logtype].level = -1;
282 if (NULL == filename)
285 /* Resetting existing config ? */
286 if (type_configs[logtype].set) {
287 if (type_configs[logtype].fd != -1)
288 close(type_configs[logtype].fd);
289 type_configs[logtype].fd = -1;
290 type_configs[logtype].level = -1;
291 type_configs[logtype].set = false;
292 type_configs[logtype].syslog = false;
294 /* Reset configs using default */
295 if (logtype == logtype_default) {
297 while (typeiter != logtype_end_of_list_marker) {
298 if (type_configs[typeiter].set == false) {
299 type_configs[typeiter].level = -1;
300 type_configs[typeiter].syslog = false;
308 type_configs[logtype].level = loglevel;
310 /* Open log file as OPEN_LOGS_AS_UID*/
312 /* Is it /dev/tty ? */
313 if (strcmp(filename, "/dev/tty") == 0) {
314 type_configs[logtype].fd = 1; /* stdout */
316 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
317 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
318 char *tmp = strdup(filename);
319 type_configs[logtype].fd = mkstemp(tmp);
323 process_uid = geteuid();
325 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
329 type_configs[logtype].fd = open(filename,
330 O_CREAT | O_WRONLY | O_APPEND,
331 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
333 if (seteuid(process_uid) == -1) {
334 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
340 /* Check for error opening/creating logfile */
341 if (type_configs[logtype].fd == -1) {
342 type_configs[logtype].level = -1;
343 type_configs[logtype].set = false;
347 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
348 type_configs[logtype].set = true;
349 log_config.inited = true;
351 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
352 /* which then uses the default logtype setup if it isn't setup itself: */
353 /* we just copy the loglevel from default to all logtypes that are not setup. */
354 /* In "make_log_entry" we then check for the logtypes if they arent setup */
355 /* and use default then. We must provide accessible values for all logtypes */
356 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
358 if (logtype == logtype_default) {
360 while (typeiter != logtype_end_of_list_marker) {
361 if ( ! (type_configs[typeiter].set))
362 type_configs[typeiter].level = loglevel;
367 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
368 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
371 /* Setup syslog logging */
372 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
376 * this currently doesn't care if logtype is already logging to a file.
377 * Fortunately currently there's no way a user could trigger this as afpd.conf
378 * is not re-read on SIGHUP.
381 type_configs[logtype].level = loglevel;
382 type_configs[logtype].set = true;
383 type_configs[logtype].syslog = true;
384 log_config.syslog_display_options = display_options;
385 log_config.syslog_facility = facility;
387 /* Setting default logging? Then set all logtype not set individually */
388 if (logtype == logtype_default) {
390 while (typeiter != logtype_end_of_list_marker) {
391 if ( ! (type_configs[typeiter].set)) {
392 type_configs[typeiter].level = loglevel;
393 type_configs[typeiter].syslog = true;
399 log_config.inited = 1;
401 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
402 arr_loglevel_strings[loglevel]);
406 * If filename == NULL its for syslog logging, otherwise its for file-logging.
407 * "unsetuplog" calls with loglevel == NULL.
408 * loglevel == NULL means:
409 * if logtype == default
412 * set to default logging
414 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
416 unsigned int typenum, levelnum;
419 for( typenum=0; typenum < num_logtype_strings; typenum++) {
420 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
423 if (typenum >= num_logtype_strings) {
428 if (loglevel == NULL) {
431 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
432 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
435 if (levelnum >= num_loglevel_strings) {
440 /* is this a syslog setup or a filelog setup ? */
441 if (filename == NULL) {
443 syslog_setup(levelnum,
445 logoption_ndelay | logoption_pid,
448 /* this must be a filelog */
449 log_setup(filename, levelnum, typenum);
455 /* =========================================================================
456 Global function definitions
457 ========================================================================= */
459 /* This function sets up the processname */
460 void set_processname(const char *processname)
462 strncpy(log_config.processname, processname, 15);
463 log_config.processname[15] = 0;
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 if (type_configs[logtype].level >= log_debug)
541 goto log; /* bypass flooding checks */
543 /* Prevent flooding: hash the message and check if we got the same one recently */
544 int hash = hash_message(temp_buffer) + log_src_linenumber;
546 /* Search for the same message by hash */
547 for (int i = log_flood_entries - 1; i >= 0; i--) {
548 if (log_flood_array[i].hash == hash) {
550 /* found same message */
551 log_flood_array[i].count++;
553 /* Check if that message has reached LOG_FLOODING_MAXCOUNT */
554 if (log_flood_array[i].count >= LOG_FLOODING_MAXCOUNT) {
555 /* yes, log it and remove from array */
557 /* reusing log_details_buffer */
558 sprintf(log_details_buffer, "message repeated %i times\n",
559 LOG_FLOODING_MAXCOUNT - 1);
560 write(fd, log_details_buffer, strlen(log_details_buffer));
562 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
563 /* last array element, just decrement count */
567 /* move array elements down */
568 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
569 log_flood_array[j-1] = log_flood_array[j];
573 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
581 /* No matching message found, add this message to array*/
582 if (log_flood_entries == LOG_FLOODING_ARRAY_SIZE) {
583 /* array is full, discard oldest entry printing "message repeated..." if count > 1 */
584 if (log_flood_array[0].count >= LOG_FLOODING_MINCOUNT) {
585 /* reusing log_details_buffer */
586 sprintf(log_details_buffer, "message repeated %i times\n",
587 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
588 write(fd, log_details_buffer, strlen(log_details_buffer));
590 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
591 log_flood_array[i-1] = log_flood_array[i];
595 log_flood_array[log_flood_entries].count = 1;
596 log_flood_array[log_flood_entries].hash = hash;
600 if ( ! log_config.console) {
601 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
602 type_configs[logtype].set ?
603 type_configs[logtype].display_options :
604 type_configs[logtype_default].display_options,
607 /* If default wasnt setup its fd is -1 */
608 iov[0].iov_base = log_details_buffer;
609 iov[0].iov_len = strlen(log_details_buffer);
610 iov[1].iov_base = temp_buffer;
611 iov[1].iov_len = strlen(temp_buffer);
614 write(fd, temp_buffer, strlen(temp_buffer));
621 void setuplog(const char *logstr, const char *logfile)
624 char *logtype, *loglevel;
627 save = ptr = strdup(logstr);
629 ptr = strtok(ptr, ", ");
633 while (*ptr && isspace(*ptr))
637 ptr = strpbrk(ptr, ":");
644 while (*ptr && !isspace(*ptr))
648 setuplog_internal(loglevel, logtype, logfile);
651 ptr = strtok(NULL, ", ");