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 /* 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 */
99 struct log_flood_entry {
103 static struct log_flood_entry log_flood_array[LOG_FLOODING_ARRAY_SIZE];
104 static int log_flood_entries;
106 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
107 static const char *log_src_filename;
108 static int log_src_linenumber;
110 /* Array to store text to list given a log type */
111 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
112 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
114 /* Array for charachters representing log severity in the log file */
115 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
116 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
118 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
119 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
121 /* =========================================================================
122 Internal function definitions
123 ========================================================================= */
125 /* Hash a log message */
126 static unsigned int hash_message(const char *message)
128 const char *p = message;
129 unsigned int hash = 0, i = 7;
139 static void generate_message_details(char *message_details_buffer,
140 int message_details_buffer_length,
142 enum loglevels loglevel, enum logtypes logtype)
144 char *ptr = message_details_buffer;
146 int len = message_details_buffer_length;
153 gettimeofday(&tv, NULL);
154 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
155 templen = strlen(ptr);
159 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
160 if (templen == -1 || templen >= len)
166 /* Process name && PID */
168 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
169 if (templen == -1 || templen >= len)
175 if ( ! (display_options & logoption_nsrcinfo)) {
176 char *basename = strrchr(log_src_filename, '/');
178 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
180 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
181 if (templen == -1 || templen >= len)
188 if (loglevel >= (num_loglevel_chars - 1))
189 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
191 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
193 if (templen == -1 || templen >= len)
199 if (logtype<num_logtype_strings) {
200 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
201 if (templen == -1 || templen >= len)
207 strncat(ptr, "): ", len);
211 static int get_syslog_equivalent(enum loglevels loglevel)
215 /* The question is we know how bad it is for us,
216 but how should that translate in the syslogs? */
221 case 3: /* warning */
225 case 5: /* information */
232 /* Called by the LOG macro for syslog messages */
233 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
235 if ( !log_config.syslog_opened ) {
236 openlog(log_config.processname,
237 log_config.syslog_display_options,
238 log_config.syslog_facility);
239 log_config.syslog_opened = true;
242 syslog(get_syslog_equivalent(loglevel), "%s", message);
245 static void log_init(void)
247 syslog_setup(log_info,
249 logoption_ndelay | logoption_pid,
253 static void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
259 if (type_configs[logtype].set) {
260 if (type_configs[logtype].fd != -1)
261 close(type_configs[logtype].fd);
262 type_configs[logtype].fd = -1;
263 type_configs[logtype].level = -1;
264 type_configs[logtype].set = false;
266 /* if disabling default also set all "default using" levels to 0 */
267 if (logtype == logtype_default) {
268 while (logtype != logtype_end_of_list_marker) {
269 if ( ! (type_configs[logtype].set))
270 type_configs[logtype].level = -1;
279 if (NULL == filename)
282 /* Resetting existing config ? */
283 if (type_configs[logtype].set) {
284 if (type_configs[logtype].fd != -1)
285 close(type_configs[logtype].fd);
286 type_configs[logtype].fd = -1;
287 type_configs[logtype].level = -1;
288 type_configs[logtype].set = false;
289 type_configs[logtype].syslog = false;
291 /* Reset configs using default */
292 if (logtype == logtype_default) {
294 while (typeiter != logtype_end_of_list_marker) {
295 if (type_configs[typeiter].set == false) {
296 type_configs[typeiter].level = -1;
297 type_configs[typeiter].syslog = false;
305 type_configs[logtype].level = loglevel;
307 /* Open log file as OPEN_LOGS_AS_UID*/
309 /* Is it /dev/tty ? */
310 if (strcmp(filename, "/dev/tty") == 0) {
311 type_configs[logtype].fd = 1; /* stdout */
313 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
314 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
315 char *tmp = strdup(filename);
316 type_configs[logtype].fd = mkstemp(tmp);
320 process_uid = geteuid();
322 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
326 type_configs[logtype].fd = open(filename,
327 O_CREAT | O_WRONLY | O_APPEND,
328 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
330 if (seteuid(process_uid) == -1) {
331 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
337 /* Check for error opening/creating logfile */
338 if (type_configs[logtype].fd == -1) {
339 type_configs[logtype].level = -1;
340 type_configs[logtype].set = false;
344 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
345 type_configs[logtype].set = true;
346 log_config.inited = true;
348 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
349 /* which then uses the default logtype setup if it isn't setup itself: */
350 /* we just copy the loglevel from default to all logtypes that are not setup. */
351 /* In "make_log_entry" we then check for the logtypes if they arent setup */
352 /* and use default then. We must provide accessible values for all logtypes */
353 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
355 if (logtype == logtype_default) {
357 while (typeiter != logtype_end_of_list_marker) {
358 if ( ! (type_configs[typeiter].set))
359 type_configs[typeiter].level = loglevel;
364 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
365 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
368 /* Setup syslog logging */
369 static void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
373 * this currently doesn't care if logtype is already logging to a file.
374 * Fortunately currently there's no way a user could trigger this as afpd.conf
375 * is not re-read on SIGHUP.
378 type_configs[logtype].level = loglevel;
379 type_configs[logtype].set = true;
380 type_configs[logtype].syslog = true;
381 log_config.syslog_display_options = display_options;
382 log_config.syslog_facility = facility;
384 /* Setting default logging? Then set all logtype not set individually */
385 if (logtype == logtype_default) {
387 while (typeiter != logtype_end_of_list_marker) {
388 if ( ! (type_configs[typeiter].set)) {
389 type_configs[typeiter].level = loglevel;
390 type_configs[typeiter].syslog = true;
396 log_config.inited = 1;
398 LOG(log_info, logtype_logger, "Set syslog logging to level: %s",
399 arr_loglevel_strings[loglevel]);
403 * If filename == NULL its for syslog logging, otherwise its for file-logging.
404 * "unsetuplog" calls with loglevel == NULL.
405 * loglevel == NULL means:
406 * if logtype == default
409 * set to default logging
411 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
413 unsigned int typenum, levelnum;
416 for( typenum=0; typenum < num_logtype_strings; typenum++) {
417 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
420 if (typenum >= num_logtype_strings) {
425 if (loglevel == NULL) {
428 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
429 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
432 if (levelnum >= num_loglevel_strings) {
437 /* is this a syslog setup or a filelog setup ? */
438 if (filename == NULL) {
440 syslog_setup(levelnum,
442 logoption_ndelay | logoption_pid,
445 /* this must be a filelog */
446 log_setup(filename, levelnum, typenum);
452 /* =========================================================================
453 Global function definitions
454 ========================================================================= */
456 /* This function sets up the processname */
457 void set_processname(const char *processname)
459 strncpy(log_config.processname, processname, 15);
460 log_config.processname[15] = 0;
463 /* -------------------------------------------------------------------------
464 make_log_entry has 1 main flaws:
465 The message in its entirity, must fit into the tempbuffer.
466 So it must be shorter than MAXLOGSIZE
467 ------------------------------------------------------------------------- */
468 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
469 const char *file, int line, char *message, ...)
471 /* fn is not reentrant but is used in signal handler
472 * with LOGGER it's a little late source name and line number
473 * are already changed. */
474 static int inlog = 0;
476 char temp_buffer[MAXLOGSIZE];
477 char log_details_buffer[MAXLOGSIZE];
486 if (!log_config.inited) {
490 if (type_configs[logtype].syslog) {
491 if (type_configs[logtype].level >= loglevel) {
492 /* Initialise the Messages and send it to syslog */
493 va_start(args, message);
494 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
496 temp_buffer[MAXLOGSIZE -1] = 0;
497 make_syslog_entry(loglevel, logtype, temp_buffer);
503 /* logging to a file */
505 log_src_filename = file;
506 log_src_linenumber = line;
508 /* Check if requested logtype is setup */
509 if (type_configs[logtype].set)
511 fd = type_configs[logtype].fd;
513 /* No: use default */
514 fd = type_configs[logtype_default].fd;
517 /* no where to send the output, give up */
521 /* Initialise the Messages */
522 va_start(args, message);
523 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
527 if (len ==-1 || len >= MAXLOGSIZE -1) {
528 /* vsnprintf hit the buffer size*/
529 temp_buffer[MAXLOGSIZE-2] = '\n';
530 temp_buffer[MAXLOGSIZE-1] = 0;
533 temp_buffer[len] = '\n';
534 temp_buffer[len+1] = 0;
537 if (type_configs[logtype].level >= log_debug)
538 goto log; /* bypass flooding checks */
540 /* Prevent flooding: hash the message and check if we got the same one recently */
541 int hash = hash_message(temp_buffer) + log_src_linenumber;
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\n",
556 LOG_FLOODING_MAXCOUNT - 1);
557 write(fd, log_details_buffer, strlen(log_details_buffer));
559 if ((i + 1) == LOG_FLOODING_ARRAY_SIZE) {
560 /* last array element, just decrement count */
564 /* move array elements down */
565 for (int j = i + 1; j != LOG_FLOODING_ARRAY_SIZE ; j++)
566 log_flood_array[j-1] = log_flood_array[j];
570 if (log_flood_array[i].count < LOG_FLOODING_MINCOUNT)
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 >= LOG_FLOODING_MINCOUNT) {
582 /* reusing log_details_buffer */
583 sprintf(log_details_buffer, "message repeated %i times\n",
584 log_flood_array[0].count - LOG_FLOODING_MINCOUNT + 1);
585 write(fd, log_details_buffer, strlen(log_details_buffer));
587 for (int i = 1; i < LOG_FLOODING_ARRAY_SIZE; i++) {
588 log_flood_array[i-1] = log_flood_array[i];
592 log_flood_array[log_flood_entries].count = 1;
593 log_flood_array[log_flood_entries].hash = hash;
597 if ( ! log_config.console) {
598 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
599 type_configs[logtype].set ?
600 type_configs[logtype].display_options :
601 type_configs[logtype_default].display_options,
604 /* If default wasnt setup its fd is -1 */
605 iov[0].iov_base = log_details_buffer;
606 iov[0].iov_len = strlen(log_details_buffer);
607 iov[1].iov_base = temp_buffer;
608 iov[1].iov_len = strlen(temp_buffer);
611 write(fd, temp_buffer, strlen(temp_buffer));
618 void setuplog(const char *logstr, const char *logfile)
621 char *logtype, *loglevel;
624 save = ptr = strdup(logstr);
627 while (*ptr && isspace(*ptr))
631 ptr = strpbrk(ptr, ":");
638 while (*ptr && !isspace(*ptr))
642 setuplog_internal(loglevel, logtype, filename);