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_info, 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*/
362 /* Is it /dev/tty ? */
363 if (strcmp(file_configs[logtype].filename, "/dev/tty") == 0) {
364 file_configs[logtype].fd = 1; /* stdout */
366 /* Does it end in "XXXXXX" ? */
367 } else if (strcmp(file_configs[logtype].filename + strlen(file_configs[logtype].filename) - 6, "XXXXXX") == 0) {
368 /* debug reguest via SIGINT */
369 file_configs[logtype].fd = mkstemp(file_configs[logtype].filename);
371 process_uid = geteuid();
373 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
374 /* XXX failing silently */
378 file_configs[logtype].fd = open( file_configs[logtype].filename,
379 O_CREAT | O_WRONLY | O_APPEND,
380 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
382 if (seteuid(process_uid) == -1) {
383 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
389 /* Check for error opening/creating logfile */
390 if (-1 == file_configs[logtype].fd) {
391 free(file_configs[logtype].filename);
392 file_configs[logtype].filename = NULL;
393 file_configs[logtype].level = -1;
394 file_configs[logtype].set = 0;
398 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
399 file_configs[logtype].set = 1;
400 log_config.filelogging = 1;
401 log_config.inited = 1;
403 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
404 /* which then uses the default logtype setup if it isn't setup itself: */
405 /* we just copy the loglevel from default to all logtypes that are not setup. */
406 /* In "make_log_entry" we then check for the logtypes if they arent setup */
407 /* and use default then. We must provide accessible values for all logtypes */
408 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
410 if (logtype == logtype_default) {
412 while (typeiter != logtype_end_of_list_marker) {
413 if ( ! (file_configs[typeiter].set))
414 file_configs[typeiter].level = loglevel;
419 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
420 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
423 /* logtype is ignored, it's just one for all */
424 void syslog_setup(int loglevel, enum logtypes logtype _U_,
425 int display_options, int facility)
427 log_config.syslog_level = loglevel;
428 log_config.syslog_display_options = display_options;
429 log_config.facility = facility;
431 log_config.inited = 1;
433 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
434 arr_loglevel_strings[loglevel]);
441 /* This function sets up the processname */
442 void set_processname(const char *processname)
444 strncpy(log_config.processname, processname, 15);
445 log_config.processname[15] = 0;
448 /* Called by the LOG macro for syslog messages */
449 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
451 if ( !log_config.syslog_opened ) {
452 openlog(log_config.processname, log_config.syslog_display_options,
453 log_config.facility);
454 log_config.syslog_opened = 1;
457 syslog(get_syslog_equivalent(loglevel), "%s", message);
460 /* -------------------------------------------------------------------------
461 make_log_entry has 1 main flaws:
462 The message in its entirity, must fit into the tempbuffer.
463 So it must be shorter than MAXLOGSIZE
464 ------------------------------------------------------------------------- */
465 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
466 const char *file, int line, char *message, ...)
468 /* fn is not reentrant but is used in signal handler
469 * with LOGGER it's a little late source name and line number
470 * are already changed. */
471 static int inlog = 0;
473 char temp_buffer[MAXLOGSIZE];
474 char log_details_buffer[MAXLOGSIZE];
483 if (!log_config.inited) {
487 if (file_configs[logtype].level >= loglevel) {
488 log_src_filename = file;
489 log_src_linenumber = line;
491 else if (!log_config.filelogging && log_config.syslog_level >= loglevel) {
492 /* Initialise the Messages */
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);
506 /* Check if requested logtype is setup */
507 if (file_configs[logtype].set)
509 fd = file_configs[logtype].fd;
511 /* No: use default */
512 fd = file_configs[logtype_default].fd;
515 /* no where to send the output, give up */
519 /* Initialise the Messages */
520 va_start(args, message);
521 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
525 if (len ==-1 || len >= MAXLOGSIZE -1) {
526 /* vsnprintf hit the buffer size*/
527 temp_buffer[MAXLOGSIZE-2] = '\n';
528 temp_buffer[MAXLOGSIZE-1] = 0;
531 temp_buffer[len] = '\n';
532 temp_buffer[len+1] = 0;
535 if ( ! log_config.console) {
536 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
537 file_configs[logtype].set ?
538 file_configs[logtype].display_options :
539 file_configs[logtype_default].display_options,
542 /* If default wasnt setup its fd is -1 */
543 iov[0].iov_base = log_details_buffer;
544 iov[0].iov_len = strlen(log_details_buffer);
545 iov[1].iov_base = temp_buffer;
546 iov[1].iov_len = strlen(temp_buffer);
549 write(fd, temp_buffer, strlen(temp_buffer));
556 void setuplog(const char *logstr)
558 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
559 ptr = strdup(logstr);
566 ptr = strpbrk(ptr, " \t");
569 while (*ptr && isspace(*ptr))
574 ptr = strpbrk(ptr, " \t");
577 while (*ptr && isspace(*ptr))
581 if (filename && *filename == 0)
585 /* finally call setuplog, filename can be NULL */
586 setuplog_internal(loglevel, logtype, filename);
591 void unsetuplog(const char *logstr)
593 char *str, *logtype, *filename;
595 str = strdup(logstr);
600 /* get filename, can be NULL */
602 filename = strtok(NULL, " \t");
604 /* finally call setuplog, filename can be NULL */
605 setuplog_internal(NULL, str, filename);