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_maxdebug: force first LOG call to call make_log_entry which
80 then calls log_init because "inited" is still 0
82 #define DEFAULT_LOG_CONFIG {0, 0, -1, log_maxdebug, 0}
84 UAM_MODULE_EXPORT logtype_conf_t type_configs[logtype_end_of_list_marker] = {
85 DEFAULT_LOG_CONFIG, /* logtype_default */
86 DEFAULT_LOG_CONFIG, /* logtype_logger */
87 DEFAULT_LOG_CONFIG, /* logtype_cnid */
88 DEFAULT_LOG_CONFIG, /* logtype_afpd */
89 DEFAULT_LOG_CONFIG, /* logtype_dsi */
90 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
91 DEFAULT_LOG_CONFIG, /* logtype_papd */
92 DEFAULT_LOG_CONFIG /* logtype_uams */
95 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
96 static const char *log_src_filename;
97 static int log_src_linenumber;
99 /* Array to store text to list given a log type */
100 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
101 static const unsigned int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
103 /* Array for charachters representing log severity in the log file */
104 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
105 static const unsigned int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
107 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
108 static const unsigned int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
110 /* =========================================================================
111 Internal function definitions
112 ========================================================================= */
115 * If filename == NULL its for syslog logging, otherwise its for file-logging.
116 * "unsetuplog" calls with loglevel == NULL.
117 * loglevel == NULL means:
118 * if logtype == default
121 * set to default logging
123 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
125 unsigned int typenum, levelnum;
128 for( typenum=0; typenum < num_logtype_strings; typenum++) {
129 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
132 if (typenum >= num_logtype_strings) {
137 if (loglevel == NULL) {
140 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
141 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
144 if (levelnum >= num_loglevel_strings) {
149 /* is this a syslog setup or a filelog setup ? */
150 if (filename == NULL) {
152 syslog_setup(levelnum,
154 logoption_ndelay | logoption_pid,
157 /* this must be a filelog */
158 log_setup(filename, levelnum, typenum);
164 static void generate_message_details(char *message_details_buffer,
165 int message_details_buffer_length,
167 enum loglevels loglevel, enum logtypes logtype)
169 char *ptr = message_details_buffer;
171 int len = message_details_buffer_length;
178 gettimeofday(&tv, NULL);
179 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
180 templen = strlen(ptr);
184 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
185 if (templen == -1 || templen >= len)
191 /* Process name && PID */
193 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
194 if (templen == -1 || templen >= len)
200 if ( ! (display_options & logoption_nsrcinfo)) {
201 char *basename = strrchr(log_src_filename, '/');
203 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
205 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
206 if (templen == -1 || templen >= len)
213 if (loglevel >= (num_loglevel_chars - 1))
214 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
216 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
218 if (templen == -1 || templen >= len)
224 if (logtype<num_logtype_strings) {
225 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
226 if (templen == -1 || templen >= len)
232 strncat(ptr, "): ", len);
236 static int get_syslog_equivalent(enum loglevels loglevel)
240 /* The question is we know how bad it is for us,
241 but how should that translate in the syslogs? */
246 case 3: /* warning */
250 case 5: /* information */
257 /* =========================================================================
258 Global function definitions
259 ========================================================================= */
263 syslog_setup(log_info,
265 logoption_ndelay | logoption_pid,
269 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
275 if (type_configs[logtype].set) {
276 if (type_configs[logtype].fd != -1)
277 close(type_configs[logtype].fd);
278 type_configs[logtype].fd = -1;
279 type_configs[logtype].level = -1;
280 type_configs[logtype].set = false;
282 /* if disabling default also set all "default using" levels to 0 */
283 if (logtype == logtype_default) {
284 while (logtype != logtype_end_of_list_marker) {
285 if ( ! (type_configs[logtype].set))
286 type_configs[logtype].level = -1;
295 if (NULL == filename)
298 /* Resetting existing config ? */
299 if (type_configs[logtype].set) {
300 if (type_configs[logtype].fd != -1)
301 close(type_configs[logtype].fd);
302 type_configs[logtype].fd = -1;
303 type_configs[logtype].level = -1;
304 type_configs[logtype].set = false;
305 type_configs[logtype].syslog = false;
307 /* Reset configs using default */
308 if (logtype == logtype_default) {
310 while (typeiter != logtype_end_of_list_marker) {
311 if (type_configs[typeiter].set == false) {
312 type_configs[typeiter].level = -1;
313 type_configs[typeiter].syslog = false;
321 type_configs[logtype].level = loglevel;
323 /* Open log file as OPEN_LOGS_AS_UID*/
325 /* Is it /dev/tty ? */
326 if (strcmp(filename, "/dev/tty") == 0) {
327 type_configs[logtype].fd = 1; /* stdout */
329 /* Does it end in "XXXXXX" ? debug reguest via SIGINT */
330 } else if (strcmp(filename + strlen(filename) - 6, "XXXXXX") == 0) {
331 char *tmp = strdup(filename);
332 type_configs[logtype].fd = mkstemp(tmp);
336 process_uid = geteuid();
338 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
339 /* XXX failing silently */
343 type_configs[logtype].fd = open(filename,
344 O_CREAT | O_WRONLY | O_APPEND,
345 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
347 if (seteuid(process_uid) == -1) {
348 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
354 /* Check for error opening/creating logfile */
355 if (type_configs[logtype].fd == -1) {
356 type_configs[logtype].level = -1;
357 type_configs[logtype].set = false;
361 fcntl(type_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
362 type_configs[logtype].set = true;
363 log_config.inited = true;
365 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
366 /* which then uses the default logtype setup if it isn't setup itself: */
367 /* we just copy the loglevel from default to all logtypes that are not setup. */
368 /* In "make_log_entry" we then check for the logtypes if they arent setup */
369 /* and use default then. We must provide accessible values for all logtypes */
370 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
372 if (logtype == logtype_default) {
374 while (typeiter != logtype_end_of_list_marker) {
375 if ( ! (type_configs[typeiter].set))
376 type_configs[typeiter].level = loglevel;
381 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
382 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], filename);
385 /* Setup syslog logging */
386 void syslog_setup(int loglevel, enum logtypes logtype, int display_options, int facility)
390 * this currently doesn't care if logtype is already logging to a file.
391 * Fortunately currently there's no way a user could trigger this as afpd.conf
392 * is not re-read on SIGHUP.
395 type_configs[logtype].level = loglevel;
396 type_configs[logtype].set = true;
397 type_configs[logtype].syslog = true;
398 log_config.syslog_display_options = display_options;
399 log_config.syslog_facility = facility;
401 /* Setting default logging? Then set all logtype not set individually */
402 if (logtype == logtype_default) {
404 while (typeiter != logtype_end_of_list_marker) {
405 if ( ! (type_configs[typeiter].set)) {
406 type_configs[typeiter].level = loglevel;
407 type_configs[typeiter].syslog = true;
413 log_config.inited = 1;
415 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
416 arr_loglevel_strings[loglevel]);
423 /* This function sets up the processname */
424 void set_processname(const char *processname)
426 strncpy(log_config.processname, processname, 15);
427 log_config.processname[15] = 0;
430 /* Called by the LOG macro for syslog messages */
431 static void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message)
433 if ( !log_config.syslog_opened ) {
434 openlog(log_config.processname,
435 log_config.syslog_display_options,
436 log_config.syslog_facility);
437 log_config.syslog_opened = true;
440 syslog(get_syslog_equivalent(loglevel), "%s", message);
443 /* -------------------------------------------------------------------------
444 make_log_entry has 1 main flaws:
445 The message in its entirity, must fit into the tempbuffer.
446 So it must be shorter than MAXLOGSIZE
447 ------------------------------------------------------------------------- */
448 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
449 const char *file, int line, char *message, ...)
451 /* fn is not reentrant but is used in signal handler
452 * with LOGGER it's a little late source name and line number
453 * are already changed. */
454 static int inlog = 0;
456 char temp_buffer[MAXLOGSIZE];
457 char log_details_buffer[MAXLOGSIZE];
466 if (!log_config.inited) {
470 if (type_configs[logtype].syslog) {
471 if (type_configs[logtype].level >= loglevel) {
472 /* Initialise the Messages and send it to syslog */
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);
483 /* logging to a file */
485 log_src_filename = file;
486 log_src_linenumber = line;
488 /* Check if requested logtype is setup */
489 if (type_configs[logtype].set)
491 fd = type_configs[logtype].fd;
493 /* No: use default */
494 fd = type_configs[logtype_default].fd;
497 /* no where to send the output, give up */
501 /* Initialise the Messages */
502 va_start(args, message);
503 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
507 if (len ==-1 || len >= MAXLOGSIZE -1) {
508 /* vsnprintf hit the buffer size*/
509 temp_buffer[MAXLOGSIZE-2] = '\n';
510 temp_buffer[MAXLOGSIZE-1] = 0;
513 temp_buffer[len] = '\n';
514 temp_buffer[len+1] = 0;
517 if ( ! log_config.console) {
518 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
519 type_configs[logtype].set ?
520 type_configs[logtype].display_options :
521 type_configs[logtype_default].display_options,
524 /* If default wasnt setup its fd is -1 */
525 iov[0].iov_base = log_details_buffer;
526 iov[0].iov_len = strlen(log_details_buffer);
527 iov[1].iov_base = temp_buffer;
528 iov[1].iov_len = strlen(temp_buffer);
531 write(fd, temp_buffer, strlen(temp_buffer));
538 void setuplog(const char *logstr)
540 char *ptr, *ptrbak, *logtype, *loglevel = NULL, *filename = NULL;
541 ptr = strdup(logstr);
548 ptr = strpbrk(ptr, " \t");
551 while (*ptr && isspace(*ptr))
556 ptr = strpbrk(ptr, " \t");
559 while (*ptr && isspace(*ptr))
563 if (filename && *filename == 0)
567 /* finally call setuplog, filename can be NULL */
568 setuplog_internal(loglevel, logtype, filename);
573 void unsetuplog(const char *logstr)
575 char *str, *logtype, *filename;
577 str = strdup(logstr);
582 /* get filename, can be NULL */
584 filename = strtok(NULL, " \t");
586 /* finally call setuplog, filename can be NULL */
587 setuplog_internal(NULL, str, filename);