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 int syslog_level; /* Log Level to send to syslog */
85 /* This stores the config and options for one filelog type (e.g. logger, afpd etc.) */
87 int set; /* set individually ? yes: changing default
88 * doesnt change it. no: it changes it.*/
89 char *filename; /* Name of file */
90 int fd; /* logfiles fd */
91 int level; /* Log Level to put in this file */
95 /* =========================================================================
97 ========================================================================= */
99 /* Main log config container, must be globally visible */
100 static log_config_t log_config = {
101 0, /* Initialized ? 0 = no */
102 0, /* No filelogging setup yet */
103 {0}, /* processname */
104 0, /* syslog opened ? */
105 logfacility_daemon, /* syslog facility to use */
106 logoption_ndelay|logoption_pid, /* logging options for syslog */
107 0 /* log level for syslog */
110 /* Default log config: log nothing to files.
111 0: not set individually
115 0: Display options */
116 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
118 static filelog_conf_t file_configs[logtype_end_of_list_marker] = {
119 DEFAULT_LOG_CONFIG, /* logtype_default */
120 DEFAULT_LOG_CONFIG, /* logtype_core */
121 DEFAULT_LOG_CONFIG, /* logtype_logger */
122 DEFAULT_LOG_CONFIG, /* logtype_cnid */
123 DEFAULT_LOG_CONFIG, /* logtype_afpd */
124 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
125 DEFAULT_LOG_CONFIG, /* logtype_papd */
126 DEFAULT_LOG_CONFIG /* logtype_uams */
129 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
130 static const char *log_src_filename;
131 static int log_src_linenumber;
133 /* Array to store text to list given a log type */
134 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
135 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
137 /* Array for charachters representing log severity in the log file */
138 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
139 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
141 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
142 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
144 /* =========================================================================
145 Internal function definitions
146 ========================================================================= */
149 * If filename == NULL its for syslog logging, otherwise its for file-logging.
150 * "unsetuplog" calls with loglevel == NULL.
151 * loglevel == NULL means:
152 * if logtype == default
155 * set to default logging
158 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
159 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
161 int typenum, levelnum;
164 for( typenum=0; typenum < num_logtype_strings; typenum++) {
165 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
168 if (typenum >= num_logtype_strings) {
173 if (loglevel == NULL) {
176 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
177 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
180 if (levelnum >= num_loglevel_strings) {
185 /* is this a syslog setup or a filelog setup ? */
186 if (filename == NULL) {
188 syslog_setup(levelnum, 0,
189 log_config.syslog_display_options,
190 log_config.facility);
192 /* this must be a filelog */
193 log_setup(filename, levelnum, typenum);
199 static void generate_message_details(char *message_details_buffer,
200 int message_details_buffer_length,
202 enum loglevels loglevel, enum logtypes logtype)
204 char *ptr = message_details_buffer;
206 int len = message_details_buffer_length;
213 gettimeofday(&tv, NULL);
214 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
215 templen = strlen(ptr);
219 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
220 if (templen == -1 || templen >= len)
226 /* Process name && PID */
228 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
229 if (templen == -1 || templen >= len)
235 if ( ! (display_options & logoption_nsrcinfo)) {
236 char *basename = strrchr(log_src_filename, '/');
238 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
240 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
241 if (templen == -1 || templen >= len)
248 if (loglevel >= (num_loglevel_chars - 1))
249 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
251 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
253 if (templen == -1 || templen >= len)
259 if (logtype<num_logtype_strings) {
260 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
261 if (templen == -1 || templen >= len)
267 strncat(ptr, "): ", len);
271 static int get_syslog_equivalent(enum loglevels loglevel)
275 /* The question is we know how bad it is for us,
276 but how should that translate in the syslogs? */
281 case 3: /* warning */
285 case 5: /* information */
292 /* =========================================================================
293 Global function definitions
294 ========================================================================= */
298 syslog_setup(log_note, 0,
299 log_config.syslog_display_options,
300 log_config.facility);
303 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
309 if (file_configs[logtype].set) {
310 if (file_configs[logtype].filename) {
311 free(file_configs[logtype].filename);
312 file_configs[logtype].filename = NULL;
314 close(file_configs[logtype].fd);
315 file_configs[logtype].fd = -1;
316 file_configs[logtype].level = 0;
317 file_configs[logtype].set = 0;
319 /* if disabling default also set all "default using" levels to 0 */
320 if (logtype == logtype_default) {
321 while (logtype != logtype_end_of_list_marker) {
322 if ( ! (file_configs[logtype].set))
323 file_configs[logtype].level = 0;
333 if (NULL == filename)
336 /* Resetting existing config ? */
337 if (file_configs[logtype].set && file_configs[logtype].filename) {
338 free(file_configs[logtype].filename);
339 file_configs[logtype].filename = NULL;
340 close(file_configs[logtype].fd);
341 file_configs[logtype].fd = -1;
342 file_configs[logtype].level = 0;
343 file_configs[logtype].set = 0;
347 file_configs[logtype].filename = strdup(filename);
348 file_configs[logtype].level = loglevel;
351 /* Open log file as OPEN_LOGS_AS_UID*/
352 process_uid = geteuid();
354 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
355 /* XXX failing silently */
359 file_configs[logtype].fd = open( file_configs[logtype].filename,
360 O_CREAT | O_WRONLY | O_APPEND,
361 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
363 if (seteuid(process_uid) == -1) {
364 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
369 /* Check for error opening/creating logfile */
370 if (-1 == file_configs[logtype].fd) {
371 free(file_configs[logtype].filename);
372 file_configs[logtype].filename = NULL;
373 file_configs[logtype].level = -1;
374 file_configs[logtype].set = 0;
378 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
379 file_configs[logtype].set = 1;
380 log_config.filelogging = 1;
381 log_config.inited = 1;
383 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
384 /* which then uses the default logtype setup if it isn't setup itself: */
385 /* we just copy the loglevel from default to all logtypes that are not setup. */
386 /* In "make_log_entry" we then check for the logtypes if they arent setup */
387 /* and use default then. We must provide accessible values for all logtypes */
388 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
390 if (logtype == logtype_default) {
391 while (logtype != logtype_end_of_list_marker) {
392 if ( ! (file_configs[logtype].set))
393 file_configs[logtype].level = loglevel;
396 logtype = logtype_default;
399 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
400 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
403 /* logtype is ignored, it's just one for all */
404 void syslog_setup(int loglevel, enum logtypes logtype _U_,
405 int display_options, int facility)
407 log_config.syslog_level = loglevel;
408 log_config.syslog_display_options = display_options;
409 log_config.facility = facility;
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, log_config.syslog_display_options,
433 log_config.facility);
434 log_config.syslog_opened = 1;
437 syslog(get_syslog_equivalent(loglevel), "%s", message);
440 /* -------------------------------------------------------------------------
441 make_log_entry has 1 main flaws:
442 The message in its entirity, must fit into the tempbuffer.
443 So it must be shorter than MAXLOGSIZE
444 ------------------------------------------------------------------------- */
445 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
446 const char *file, int line, char *message, ...)
448 /* fn is not reentrant but is used in signal handler
449 * with LOGGER it's a little late source name and line number
450 * are already changed. */
451 static int inlog = 0;
453 char temp_buffer[MAXLOGSIZE];
454 char log_details_buffer[MAXLOGSIZE];
463 if (!log_config.inited) {
467 if (file_configs[logtype].level >= loglevel) {
468 log_src_filename = file;
469 log_src_linenumber = line;
471 else if (!log_config.filelogging && log_config.syslog_level >= loglevel) {
472 /* Initialise the Messages */
473 va_start(args, message);
474 vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
476 temp_buffer[MAXLOGSIZE -1] = 0;
477 make_syslog_entry(loglevel, logtype, temp_buffer);
486 /* Check if requested logtype is setup */
487 if (file_configs[logtype].set)
489 fd = file_configs[logtype].fd;
491 /* No: use default */
492 fd = file_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 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
516 file_configs[logtype].set ?
517 file_configs[logtype].display_options :
518 file_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);
533 void setuplog(const char *logstr)
535 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
536 ptr = strdup(logstr);
543 ptr = strpbrk(ptr, " \t");
546 while (*ptr && isspace(*ptr))
551 ptr = strpbrk(ptr, " \t");
554 while (*ptr && isspace(*ptr))
558 if (filename && *filename == 0)
562 /* finally call setuplog, filename can be NULL */
563 setuplog_internal(loglevel, logtype, filename);
568 void unsetuplog(const char *logstr)
570 char *str, *logtype, *filename;
572 str = strdup(logstr);
577 /* get filename, can be NULL */
579 filename = strtok(NULL, " \t");
581 /* finally call setuplog, filename can be NULL */
582 setuplog_internal(NULL, str, filename);