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 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
95 static const char *log_src_filename;
96 static int log_src_linenumber;
98 /* Array to store text to list given a log type */
99 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
100 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
102 /* Array for charachters representing log severity in the log file */
103 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
104 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
106 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
107 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
109 /* =========================================================================
110 Internal function definitions
111 ========================================================================= */
114 * If filename == NULL its for syslog logging, otherwise its for file-logging.
115 * "unsetuplog" calls with loglevel == NULL.
116 * loglevel == NULL means:
117 * if logtype == default
120 * set to default logging
122 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
124 unsigned int typenum, levelnum;
127 for( typenum=0; typenum < num_logtype_strings; typenum++) {
128 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
131 if (typenum >= num_logtype_strings) {
136 if (loglevel == NULL) {
139 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
140 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
143 if (levelnum >= num_loglevel_strings) {
148 /* is this a syslog setup or a filelog setup ? */
149 if (filename == NULL) {
151 syslog_setup(levelnum,
153 logoption_ndelay | logoption_pid,
156 /* this must be a filelog */
157 log_setup(filename, levelnum, typenum);
163 static void generate_message_details(char *message_details_buffer,
164 int message_details_buffer_length,
166 enum loglevels loglevel, enum logtypes logtype)
168 char *ptr = message_details_buffer;
170 int len = message_details_buffer_length;
177 gettimeofday(&tv, NULL);
178 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
179 templen = strlen(ptr);
183 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
184 if (templen == -1 || templen >= len)
190 /* Process name && PID */
192 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
193 if (templen == -1 || templen >= len)
199 if ( ! (display_options & logoption_nsrcinfo)) {
200 char *basename = strrchr(log_src_filename, '/');
202 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
204 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
205 if (templen == -1 || templen >= len)
212 if (loglevel >= (num_loglevel_chars - 1))
213 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
215 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
217 if (templen == -1 || templen >= len)
223 if (logtype<num_logtype_strings) {
224 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
225 if (templen == -1 || templen >= len)
231 strncat(ptr, "): ", len);
235 static int get_syslog_equivalent(enum loglevels loglevel)
239 /* The question is we know how bad it is for us,
240 but how should that translate in the syslogs? */
245 case 3: /* warning */
249 case 5: /* information */
256 /* =========================================================================
257 Global function definitions
258 ========================================================================= */
262 syslog_setup(log_info,
264 logoption_ndelay | logoption_pid,
268 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
274 if (type_configs[logtype].set) {
275 if (type_configs[logtype].fd != -1)
276 close(type_configs[logtype].fd);
277 type_configs[logtype].fd = -1;
278 type_configs[logtype].level = -1;
279 type_configs[logtype].set = false;
281 /* if disabling default also set all "default using" levels to 0 */
282 if (logtype == logtype_default) {
283 while (logtype != logtype_end_of_list_marker) {
284 if ( ! (type_configs[logtype].set))
285 type_configs[logtype].level = -1;
294 if (NULL == filename)
297 /* Resetting existing config ? */
298 if (type_configs[logtype].set) {
299 if (type_configs[logtype].fd != -1)
300 close(type_configs[logtype].fd);
301 type_configs[logtype].fd = -1;
302 type_configs[logtype].level = -1;
303 type_configs[logtype].set = false;
304 type_configs[logtype].syslog = false;
306 /* Reset configs using default */
307 if (logtype == logtype_default) {
309 while (typeiter != logtype_end_of_list_marker) {
310 if (type_configs[typeiter].set == false) {
311 type_configs[typeiter].level = -1;
312 type_configs[typeiter].syslog = false;
320 type_configs[logtype].level = loglevel;
322 /* Open log file as OPEN_LOGS_AS_UID*/
324 /* Is it /dev/tty ? */
325 if (strcmp(filename, "/dev/tty") == 0) {
326 type_configs[logtype].fd = 1; /* stdout */
328 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
329 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
330 char *tmp = strdup(filename);
331 type_configs[logtype].fd = mkstemp(tmp);
335 process_uid = geteuid();
337 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
341 type_configs[logtype].fd = open(filename,
342 O_CREAT | O_WRONLY | O_APPEND,
343 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
345 if (seteuid(process_uid) == -1) {
346 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
352 /* Check for error opening/creating logfile */
353 if (type_configs[logtype].fd == -1) {
354 type_configs[logtype].level = -1;
355 type_configs[logtype].set = false;
359 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
360 type_configs[logtype].set = true;
361 log_config.inited = true;
363 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
364 /* which then uses the default logtype setup if it isn't setup itself: */
365 /* we just copy the loglevel from default to all logtypes that are not setup. */
366 /* In "make_log_entry" we then check for the logtypes if they arent setup */
367 /* and use default then. We must provide accessible values for all logtypes */
368 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
370 if (logtype == logtype_default) {
372 while (typeiter != logtype_end_of_list_marker) {
373 if ( ! (type_configs[typeiter].set))
374 type_configs[typeiter].level = loglevel;
379 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
380 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
383 /* Setup syslog logging */
384 void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
388 * this currently doesn't care if logtype is already logging to a file.
389 * Fortunately currently there's no way a user could trigger this as afpd.conf
390 * is not re-read on SIGHUP.
393 type_configs[logtype].level = loglevel;
394 type_configs[logtype].set = true;
395 type_configs[logtype].syslog = true;
396 log_config.syslog_display_options = display_options;
397 log_config.syslog_facility = facility;
399 /* Setting default logging? Then set all logtype not set individually */
400 if (logtype == logtype_default) {
402 while (typeiter != logtype_end_of_list_marker) {
403 if ( ! (type_configs[typeiter].set)) {
404 type_configs[typeiter].level = loglevel;
405 type_configs[typeiter].syslog = true;
411 log_config.inited = 1;
413 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
414 arr_loglevel_strings[loglevel]);
421 /* This function sets up the processname */
422 void set_processname(const char *processname)
424 strncpy(log_config.processname, processname, 15);
425 log_config.processname[15] = 0;
428 /* Called by the LOG macro for syslog messages */
429 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
431 if ( !log_config.syslog_opened ) {
432 openlog(log_config.processname,
433 log_config.syslog_display_options,
434 log_config.syslog_facility);
435 log_config.syslog_opened = true;
438 syslog(get_syslog_equivalent(loglevel), "%s", message);
441 /* -------------------------------------------------------------------------
442 make_log_entry has 1 main flaws:
443 The message in its entirity, must fit into the tempbuffer.
444 So it must be shorter than MAXLOGSIZE
445 ------------------------------------------------------------------------- */
446 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
447 const char *file, int line, char *message, ...)
449 /* fn is not reentrant but is used in signal handler
450 * with LOGGER it's a little late source name and line number
451 * are already changed. */
452 static int inlog = 0;
454 char temp_buffer[MAXLOGSIZE];
455 char log_details_buffer[MAXLOGSIZE];
464 if (!log_config.inited) {
468 if (type_configs[logtype].syslog) {
469 if (type_configs[logtype].level >= loglevel) {
470 /* Initialise the Messages and send it to syslog */
471 va_start(args, message);
472 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
474 temp_buffer[MAXLOGSIZE -1] = 0;
475 make_syslog_entry(loglevel, logtype, temp_buffer);
481 /* logging to a file */
483 log_src_filename = file;
484 log_src_linenumber = line;
486 /* Check if requested logtype is setup */
487 if (type_configs[logtype].set)
489 fd = type_configs[logtype].fd;
491 /* No: use default */
492 fd = type_configs[logtype_default].fd;
495 /* no where to send the output, give up */
499 /* Initialise the Messages */
500 va_start(args, message);
501 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
505 if (len ==-1 || len >= MAXLOGSIZE -1) {
506 /* vsnprintf hit the buffer size*/
507 temp_buffer[MAXLOGSIZE-2] = '\n';
508 temp_buffer[MAXLOGSIZE-1] = 0;
511 temp_buffer[len] = '\n';
512 temp_buffer[len+1] = 0;
515 if ( ! log_config.console) {
516 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
517 type_configs[logtype].set ?
518 type_configs[logtype].display_options :
519 type_configs[logtype_default].display_options,
522 /* If default wasnt setup its fd is -1 */
523 iov[0].iov_base = log_details_buffer;
524 iov[0].iov_len = strlen(log_details_buffer);
525 iov[1].iov_base = temp_buffer;
526 iov[1].iov_len = strlen(temp_buffer);
529 write(fd, temp_buffer, strlen(temp_buffer));
536 void setuplog(const char *logstr)
538 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
539 ptr = strdup(logstr);
546 ptr = strpbrk(ptr, " \t");
549 while (*ptr && isspace(*ptr))
554 ptr = strpbrk(ptr, " \t");
557 while (*ptr && isspace(*ptr))
561 if (filename && *filename == 0)
565 /* finally call setuplog, filename can be NULL */
566 setuplog_internal(loglevel, logtype, filename);
571 void unsetuplog(const char *logstr)
573 char *str, *logtype, *filename;
575 str = strdup(logstr);
580 /* get filename, can be NULL */
582 filename = strtok(NULL, " \t");
584 /* finally call setuplog, filename can be NULL */
585 setuplog_internal(NULL, str, filename);