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 { \
67 "end_of_list_marker"} \
69 /* =========================================================================
71 ========================================================================= */
75 int inited; /* file log config initialized ? */
76 int filelogging; /* Any level set to filelogging ? */
77 /* Deactivates syslog logging */
79 int syslog_opened; /* syslog opened ? */
80 int facility; /* syslog facility to use */
81 int syslog_display_options;
82 enum loglevels syslog_level; /* Log Level to send to syslog */
83 int console; /* if logging to console from a cli util */
86 /* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
88 int set; /* set individually ? yes: changing default
89 * doesnt change it. no: it changes it.*/
90 char *filename; /* Name of file */
91 int fd; /* logfiles fd */
92 enum loglevels level; /* Log Level to put in this file */
96 /* =========================================================================
98 ========================================================================= */
100 /* Main log config container */
101 static log_config_t log_config = {
102 0, /* Initialized ? 0 = no */
103 0, /* No filelogging setup yet */
104 {0}, /* processname */
105 0, /* syslog opened ? */
106 logfacility_daemon, /* syslog facility to use */
107 logoption_ndelay|logoption_pid, /* logging options for syslog */
108 0, /* log level for syslog */
109 0 /* not logging to console */
112 /* Default log config: log nothing to files.
113 0: not set individually
117 0: Display options */
118 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
120 static filelog_conf_t file_configs[logtype_end_of_list_marker] = {
121 DEFAULT_LOG_CONFIG, /* logtype_default */
122 DEFAULT_LOG_CONFIG, /* logtype_core */
123 DEFAULT_LOG_CONFIG, /* logtype_logger */
124 DEFAULT_LOG_CONFIG, /* logtype_cnid */
125 DEFAULT_LOG_CONFIG, /* logtype_afpd */
126 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
127 DEFAULT_LOG_CONFIG, /* logtype_papd */
128 DEFAULT_LOG_CONFIG, /* logtype_uams */
129 DEFAULT_LOG_CONFIG /* logtype_console */
132 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
133 static const char *log_src_filename;
134 static int log_src_linenumber;
136 /* Array to store text to list given a log type */
137 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
138 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
140 /* Array for charachters representing log severity in the log file */
141 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
142 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
144 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
145 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
147 /* =========================================================================
148 Internal function definitions
149 ========================================================================= */
152 * If filename == NULL its for syslog logging, otherwise its for file-logging.
153 * "unsetuplog" calls with loglevel == NULL.
154 * loglevel == NULL means:
155 * if logtype == default
158 * set to default logging
161 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
162 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
164 unsigned int typenum, levelnum;
167 for( typenum=0; typenum < num_logtype_strings; typenum++) {
168 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
171 if (typenum >= num_logtype_strings) {
176 if (loglevel == NULL) {
179 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
180 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
183 if (levelnum >= num_loglevel_strings) {
188 /* is this a syslog setup or a filelog setup ? */
189 if (filename == NULL) {
191 syslog_setup(levelnum, 0,
192 log_config.syslog_display_options,
193 log_config.facility);
195 /* this must be a filelog */
196 log_setup(filename, levelnum, typenum);
202 static void generate_message_details(char *message_details_buffer,
203 int message_details_buffer_length,
205 enum loglevels loglevel, enum logtypes logtype)
207 char *ptr = message_details_buffer;
209 int len = message_details_buffer_length;
216 gettimeofday(&tv, NULL);
217 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
218 templen = strlen(ptr);
222 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
223 if (templen == -1 || templen >= len)
229 /* Process name && PID */
231 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
232 if (templen == -1 || templen >= len)
238 if ( ! (display_options & logoption_nsrcinfo)) {
239 char *basename = strrchr(log_src_filename, '/');
241 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
243 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
244 if (templen == -1 || templen >= len)
251 if (loglevel >= (num_loglevel_chars - 1))
252 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
254 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
256 if (templen == -1 || templen >= len)
262 if (logtype<num_logtype_strings) {
263 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
264 if (templen == -1 || templen >= len)
270 strncat(ptr, "): ", len);
274 static int get_syslog_equivalent(enum loglevels loglevel)
278 /* The question is we know how bad it is for us,
279 but how should that translate in the syslogs? */
284 case 3: /* warning */
288 case 5: /* information */
295 /* =========================================================================
296 Global function definitions
297 ========================================================================= */
301 syslog_setup(log_note, 0,
302 log_config.syslog_display_options,
303 log_config.facility);
306 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
312 if (file_configs[logtype].set) {
313 if (file_configs[logtype].filename) {
314 free(file_configs[logtype].filename);
315 file_configs[logtype].filename = NULL;
317 close(file_configs[logtype].fd);
318 file_configs[logtype].fd = -1;
319 file_configs[logtype].level = 0;
320 file_configs[logtype].set = 0;
322 /* if disabling default also set all "default using" levels to 0 */
323 if (logtype == logtype_default) {
324 while (logtype != logtype_end_of_list_marker) {
325 if ( ! (file_configs[logtype].set))
326 file_configs[logtype].level = 0;
336 if (NULL == filename)
339 /* Resetting existing config ? */
340 if (file_configs[logtype].set && file_configs[logtype].filename) {
341 free(file_configs[logtype].filename);
342 file_configs[logtype].filename = NULL;
343 close(file_configs[logtype].fd);
344 file_configs[logtype].fd = -1;
345 file_configs[logtype].level = 0;
346 file_configs[logtype].set = 0;
349 /* Check if logging to a console */
350 if (logtype == logtype_console) {
351 log_config.console = 1;
352 logtype = logtype_default;
356 file_configs[logtype].filename = strdup(filename);
357 file_configs[logtype].level = loglevel;
360 /* Open log file as OPEN_LOGS_AS_UID*/
361 /* Is it /dev/tty ? */
362 if (strcmp(file_configs[logtype].filename, "/dev/tty") == 0) {
363 file_configs[logtype].fd = 1; /* stdout */
365 process_uid = geteuid();
367 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
368 /* XXX failing silently */
372 file_configs[logtype].fd = open( file_configs[logtype].filename,
373 O_CREAT | O_WRONLY | O_APPEND,
374 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
376 if (seteuid(process_uid) == -1) {
377 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
383 /* Check for error opening/creating logfile */
384 if (-1 == file_configs[logtype].fd) {
385 free(file_configs[logtype].filename);
386 file_configs[logtype].filename = NULL;
387 file_configs[logtype].level = -1;
388 file_configs[logtype].set = 0;
392 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
393 file_configs[logtype].set = 1;
394 log_config.filelogging = 1;
395 log_config.inited = 1;
397 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
398 /* which then uses the default logtype setup if it isn't setup itself: */
399 /* we just copy the loglevel from default to all logtypes that are not setup. */
400 /* In "make_log_entry" we then check for the logtypes if they arent setup */
401 /* and use default then. We must provide accessible values for all logtypes */
402 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
404 if (logtype == logtype_default) {
406 while (typeiter != logtype_end_of_list_marker) {
407 if ( ! (file_configs[typeiter].set))
408 file_configs[typeiter].level = loglevel;
413 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
414 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
417 /* logtype is ignored, it's just one for all */
418 void syslog_setup(int loglevel, enum logtypes logtype _U_,
419 int display_options, int facility)
421 log_config.syslog_level = loglevel;
422 log_config.syslog_display_options = display_options;
423 log_config.facility = facility;
425 log_config.inited = 1;
427 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
428 arr_loglevel_strings[loglevel]);
435 /* This function sets up the processname */
436 void set_processname(const char *processname)
438 strncpy(log_config.processname, processname, 15);
439 log_config.processname[15] = 0;
442 /* Called by the LOG macro for syslog messages */
443 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
445 if ( !log_config.syslog_opened ) {
446 openlog(log_config.processname, log_config.syslog_display_options,
447 log_config.facility);
448 log_config.syslog_opened = 1;
451 syslog(get_syslog_equivalent(loglevel), "%s", message);
454 /* -------------------------------------------------------------------------
455 make_log_entry has 1 main flaws:
456 The message in its entirity, must fit into the tempbuffer.
457 So it must be shorter than MAXLOGSIZE
458 ------------------------------------------------------------------------- */
459 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
460 const char *file, int line, char *message, ...)
462 /* fn is not reentrant but is used in signal handler
463 * with LOGGER it's a little late source name and line number
464 * are already changed. */
465 static int inlog = 0;
467 char temp_buffer[MAXLOGSIZE];
468 char log_details_buffer[MAXLOGSIZE];
477 if (!log_config.inited) {
481 if (file_configs[logtype].level >= loglevel) {
482 log_src_filename = file;
483 log_src_linenumber = line;
485 else if (!log_config.filelogging && log_config.syslog_level >= loglevel) {
486 /* Initialise the Messages */
487 va_start(args, message);
488 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
490 temp_buffer[MAXLOGSIZE -1] = 0;
491 make_syslog_entry(loglevel, logtype, temp_buffer);
500 /* Check if requested logtype is setup */
501 if (file_configs[logtype].set)
503 fd = file_configs[logtype].fd;
505 /* No: use default */
506 fd = file_configs[logtype_default].fd;
509 /* no where to send the output, give up */
513 /* Initialise the Messages */
514 va_start(args, message);
515 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
519 if (len ==-1 || len >= MAXLOGSIZE -1) {
520 /* vsnprintf hit the buffer size*/
521 temp_buffer[MAXLOGSIZE-2] = '\n';
522 temp_buffer[MAXLOGSIZE-1] = 0;
525 temp_buffer[len] = '\n';
526 temp_buffer[len+1] = 0;
529 if ( ! log_config.console) {
530 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
531 file_configs[logtype].set ?
532 file_configs[logtype].display_options :
533 file_configs[logtype_default].display_options,
536 /* If default wasnt setup its fd is -1 */
537 iov[0].iov_base = log_details_buffer;
538 iov[0].iov_len = strlen(log_details_buffer);
539 iov[1].iov_base = temp_buffer;
540 iov[1].iov_len = strlen(temp_buffer);
543 write(fd, temp_buffer, strlen(temp_buffer));
550 void setuplog(const char *logstr)
552 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
553 ptr = strdup(logstr);
560 ptr = strpbrk(ptr, " \t");
563 while (*ptr && isspace(*ptr))
568 ptr = strpbrk(ptr, " \t");
571 while (*ptr && isspace(*ptr))
575 if (filename && *filename == 0)
579 /* finally call setuplog, filename can be NULL */
580 setuplog_internal(loglevel, logtype, filename);
585 void unsetuplog(const char *logstr)
587 char *str, *logtype, *filename;
589 str = strdup(logstr);
594 /* get filename, can be NULL */
596 filename = strtok(NULL, " \t");
598 /* finally call setuplog, filename can be NULL */
599 setuplog_internal(NULL, str, filename);