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>
35 #include <atalk/logger.h>
38 #define OPEN_LOGS_AS_UID 0
40 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
42 /* =========================================================================
44 ========================================================================= */
46 /* Main log config container, must be globally visible */
47 log_config_t log_config = {
48 0, /* Initialized ? 0 = no */
49 0, /* No filelogging setup yet */
50 {0}, /* processname */
51 0, /* syslog opened ? */
52 logfacility_daemon, /* syslog facility to use */
53 logoption_ndelay|logoption_pid, /* logging options for syslog */
54 0 /* log level for syslog */
57 /* Default log config: log nothing to files.
58 0: not set individually
63 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
65 filelog_conf_t file_configs[logtype_end_of_list_marker] = {
66 DEFAULT_LOG_CONFIG, /* logtype_default */
67 DEFAULT_LOG_CONFIG, /* logtype_core */
68 DEFAULT_LOG_CONFIG, /* logtype_logger */
69 DEFAULT_LOG_CONFIG, /* logtype_cnid */
70 DEFAULT_LOG_CONFIG, /* logtype_afpd */
71 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
72 DEFAULT_LOG_CONFIG, /* logtype_papd */
73 DEFAULT_LOG_CONFIG /* logtype_uams */
76 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
77 char *log_src_filename;
78 int log_src_linenumber;
80 /* Array to store text to list given a log type */
81 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
82 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
84 /* Array for charachters representing log severity in the log file */
85 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
86 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
88 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
89 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
91 /* =========================================================================
92 Internal function definitions
93 ========================================================================= */
96 * If filename == NULL its for syslog logging, otherwise its for file-logging.
97 * "unsetuplog" calls with loglevel == NULL.
98 * loglevel == NULL means:
99 * if logtype == default
102 * set to default logging
105 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
106 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
108 int typenum, levelnum;
111 for( typenum=0; typenum < num_logtype_strings; typenum++) {
112 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
115 if (typenum >= num_logtype_strings) {
120 if (loglevel == NULL) {
123 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
124 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
127 if (levelnum >= num_loglevel_strings) {
132 /* is this a syslog setup or a filelog setup ? */
133 if (filename == NULL) {
135 syslog_setup(levelnum, 0,
136 log_config.syslog_display_options,
137 log_config.facility);
139 /* this must be a filelog */
140 log_setup(filename, levelnum, typenum);
146 static void generate_message_details(char *message_details_buffer,
147 int message_details_buffer_length,
149 enum loglevels loglevel, enum logtypes logtype)
151 char *ptr = message_details_buffer;
153 int len = message_details_buffer_length;
160 gettimeofday(&tv, NULL);
161 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
162 templen = strlen(ptr);
166 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
167 if (templen == -1 || templen >= len)
173 /* Process name && PID */
175 templen = snprintf(ptr, len, "%s[%d]", log_config.processname, pid);
176 if (templen == -1 || templen >= len)
182 if ( ! (display_options & logoption_nsrcinfo)) {
183 char *basename = strrchr(log_src_filename, '/');
185 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
187 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
188 if (templen == -1 || templen >= len)
195 if (loglevel >= (num_loglevel_chars - 1))
196 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
198 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
199 if (templen == -1 || templen >= len)
205 if (logtype<num_logtype_strings) {
206 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
207 if (templen == -1 || templen >= len)
213 strncat(ptr, "): ", len);
217 static int get_syslog_equivalent(enum loglevels loglevel)
221 /* The question is we know how bad it is for us,
222 but how should that translate in the syslogs? */
227 case 3: /* warning */
231 case 5: /* information */
238 /* =========================================================================
239 Global function definitions
240 ========================================================================= */
244 syslog_setup(log_note, 0,
245 log_config.syslog_display_options,
246 log_config.facility);
249 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
255 if (file_configs[logtype].set) {
256 if (file_configs[logtype].filename) {
257 free(file_configs[logtype].filename);
258 file_configs[logtype].filename = NULL;
260 close(file_configs[logtype].fd);
261 file_configs[logtype].fd = -1;
262 file_configs[logtype].level = 0;
263 file_configs[logtype].set = 0;
265 /* if disabling default also set all "default using" levels to 0 */
266 if (logtype == logtype_default) {
267 while (logtype != logtype_end_of_list_marker) {
268 if ( ! (file_configs[logtype].set))
269 file_configs[logtype].level = 0;
279 if (NULL == filename)
282 /* Resetting existing config ? */
283 if (file_configs[logtype].set && file_configs[logtype].filename) {
284 free(file_configs[logtype].filename);
285 file_configs[logtype].filename = NULL;
286 close(file_configs[logtype].fd);
287 file_configs[logtype].fd = -1;
288 file_configs[logtype].level = 0;
289 file_configs[logtype].set = 0;
293 file_configs[logtype].filename = strdup(filename);
294 file_configs[logtype].level = loglevel;
297 /* Open log file as OPEN_LOGS_AS_UID*/
298 process_uid = geteuid();
300 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
301 /* XXX failing silently */
305 file_configs[logtype].fd = open( file_configs[logtype].filename,
306 O_CREAT | O_WRONLY | O_APPEND,
307 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
309 if (seteuid(process_uid) == -1) {
310 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
315 /* Check for error opening/creating logfile */
316 if (-1 == file_configs[logtype].fd) {
317 free(file_configs[logtype].filename);
318 file_configs[logtype].filename = NULL;
319 file_configs[logtype].level = -1;
320 file_configs[logtype].set = 0;
324 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
325 file_configs[logtype].set = 1;
326 log_config.filelogging = 1;
327 log_config.inited = 1;
329 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
330 /* which then uses the default logtype setup if it isn't setup itself: */
331 /* we just copy the loglevel from default to all logtypes that are not setup. */
332 /* In "make_log_entry" we then check for the logtypes if they arent setup */
333 /* and use default then. We must provide accessible values for all logtypes */
334 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
336 if (logtype == logtype_default) {
337 while (logtype != logtype_end_of_list_marker) {
338 if ( ! (file_configs[logtype].set))
339 file_configs[logtype].level = loglevel;
342 logtype = logtype_default;
345 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
346 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
349 /* logtype is ignored, it's just one for all */
350 void syslog_setup(int loglevel, enum logtypes logtype _U_,
351 int display_options, int facility)
353 log_config.syslog_level = loglevel;
354 log_config.syslog_display_options = display_options;
355 log_config.facility = facility;
357 log_config.inited = 1;
359 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
360 arr_loglevel_strings[loglevel]);
367 /* This function sets up the processname */
368 void set_processname(const char *processname)
370 strncpy(log_config.processname, processname, 15);
371 log_config.processname[15] = 0;
374 /* -------------------------------------------------------------------------
375 make_log_entry has 1 main flaws:
376 The message in its entirity, must fit into the tempbuffer.
377 So it must be shorter than MAXLOGSIZE
378 ------------------------------------------------------------------------- */
379 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
382 /* fn is not reentrant but is used in signal handler
383 * with LOGGER it's a little late source name and line number
384 * are already changed. */
385 static int inlog = 0;
387 char temp_buffer[MAXLOGSIZE];
388 char log_details_buffer[MAXLOGSIZE];
395 /* Check if requested logtype is setup */
396 if (file_configs[logtype].set)
398 fd = file_configs[logtype].fd;
400 /* No: use default */
401 fd = file_configs[logtype_default].fd;
404 /* no where to send the output, give up */
409 /* Initialise the Messages */
410 va_start(args, message);
411 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
415 if (len ==-1 || len >= MAXLOGSIZE -1) {
416 /* vsnprintf hit the buffer size*/
417 temp_buffer[MAXLOGSIZE-2] = '\n';
418 temp_buffer[MAXLOGSIZE-1] = 0;
421 temp_buffer[len] = '\n';
422 temp_buffer[len+1] = 0;
425 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
426 file_configs[logtype].set ?
427 file_configs[logtype].display_options :
428 file_configs[logtype_default].display_options,
432 /* If default wasnt setup its fd is -1 */
433 iov[0].iov_base = log_details_buffer;
434 iov[0].iov_len = strlen(log_details_buffer);
435 iov[1].iov_base = temp_buffer;
436 iov[1].iov_len = strlen(temp_buffer);
442 /* Called by the LOG macro for syslog messages */
443 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message, ...)
446 char log_buffer[MAXLOGSIZE];
447 /* fn is not reentrant but is used in signal handler
448 * with LOGGER it's a little late source name and line number
449 * are already changed.
451 static int inlog = 0;
457 if ( ! (log_config.syslog_opened) ) {
458 openlog(log_config.processname, log_config.syslog_display_options,
459 log_config.facility);
460 log_config.syslog_opened = 1;
463 /* Initialise the Messages */
464 va_start(args, message);
465 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
467 log_buffer[MAXLOGSIZE -1] = 0;
468 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
473 void setuplog(const char *logstr)
475 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
476 ptr = strdup(logstr);
483 ptr = strpbrk(ptr, " \t");
486 while (*ptr && isspace(*ptr))
491 ptr = strpbrk(ptr, " \t");
494 while (*ptr && isspace(*ptr))
500 /* finally call setuplog, filename can be NULL */
501 setuplog_internal(loglevel, logtype, filename);
506 void unsetuplog(const char *logstr)
508 char *str, *logtype, *filename;
510 str = strdup(logstr);
515 /* get filename, can be NULL */
517 filename = strtok(NULL, " \t");
519 /* finally call setuplog, filename can be NULL */
520 setuplog_internal(NULL, str, filename);