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>
30 #include <atalk/boolean.h>
33 #include <atalk/logger.h>
36 #define OPEN_LOGS_AS_UID 0
38 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
40 /* =========================================================================
42 ========================================================================= */
44 /* Main log config container, must be globally visible */
45 log_config_t log_config = {
46 0, /* Initialized ? 0 = no */
47 0, /* No filelogging setup yet */
48 {0}, /* processname */
49 0, /* syslog opened ? */
50 logfacility_daemon, /* syslog facility to use */
51 logoption_ndelay|logoption_pid, /* logging options for syslog */
52 0 /* log level for syslog */
55 /* Default log config: log nothing to files.
56 0: not set individually
61 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
63 filelog_conf_t file_configs[logtype_end_of_list_marker] = {
64 DEFAULT_LOG_CONFIG, /* logtype_default */
65 DEFAULT_LOG_CONFIG, /* logtype_core */
66 DEFAULT_LOG_CONFIG, /* logtype_logger */
67 DEFAULT_LOG_CONFIG, /* logtype_cnid */
68 DEFAULT_LOG_CONFIG, /* logtype_afpd */
69 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
70 DEFAULT_LOG_CONFIG, /* logtype_papd */
71 DEFAULT_LOG_CONFIG /* logtype_uams */
74 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
75 char *log_src_filename;
76 int log_src_linenumber;
78 /* Array to store text to list given a log type */
79 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
80 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
82 /* Array for charachters representing log severity in the log file */
83 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
84 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
86 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
87 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
89 /* =========================================================================
90 Internal function definitions
91 ========================================================================= */
94 * If filename == NULL its for syslog logging, otherwise its for file-logging.
95 * "unsetuplog" calls with loglevel == NULL.
96 * loglevel == NULL means:
97 * if logtype == default
100 * set to default logging
103 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
104 static void setuplog_internal(const char *loglevel, const char *logtype, const char *filename)
106 int typenum, levelnum;
109 for( typenum=0; typenum < num_logtype_strings; typenum++) {
110 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
113 if (typenum >= num_logtype_strings) {
118 if (loglevel == NULL) {
121 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
122 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
125 if (levelnum >= num_loglevel_strings) {
130 /* is this a syslog setup or a filelog setup ? */
131 if (filename == NULL) {
133 syslog_setup(levelnum, 0,
134 log_config.syslog_display_options,
135 log_config.facility);
137 /* this must be a filelog */
138 log_setup(filename, levelnum, typenum);
144 static void generate_message_details(char *message_details_buffer,
145 int message_details_buffer_length,
147 enum loglevels loglevel, enum logtypes logtype)
149 char *ptr = message_details_buffer;
151 int len = message_details_buffer_length;
158 gettimeofday(&tv, NULL);
159 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
160 templen = strlen(ptr);
164 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
169 strncpy(ptr, log_config.processname, len);
170 templen = strlen(ptr);
176 templen = snprintf(ptr, len, "[%d]", pid);
181 if ( ! (display_options & logoption_nsrcinfo)) {
182 char *basename = strrchr(log_src_filename, '/');
184 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
186 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
194 if (loglevel >= (num_loglevel_chars - 1))
195 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
197 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
202 if (logtype<num_logtype_strings) {
203 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
208 strncat(ptr, "): ", len);
211 static int get_syslog_equivalent(enum loglevels loglevel)
215 /* The question is we know how bad it is for us,
216 but how should that translate in the syslogs? */
221 case 3: /* warning */
225 case 5: /* information */
232 /* =========================================================================
233 Global function definitions
234 ========================================================================= */
238 syslog_setup(log_note, 0,
239 log_config.syslog_display_options,
240 log_config.facility);
243 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
249 if (file_configs[logtype].set) {
250 if (file_configs[logtype].filename) {
251 free(file_configs[logtype].filename);
252 file_configs[logtype].filename = NULL;
254 close(file_configs[logtype].fd);
255 file_configs[logtype].fd = -1;
256 file_configs[logtype].level = 0;
257 file_configs[logtype].set = 0;
259 /* if disabling default also set all "default using" levels to 0 */
260 if (logtype == logtype_default) {
261 while (logtype != logtype_end_of_list_marker) {
262 if ( ! (file_configs[logtype].set))
263 file_configs[logtype].level = 0;
273 if (NULL == filename)
276 /* Resetting existing config ? */
277 if (file_configs[logtype].set && file_configs[logtype].filename) {
278 free(file_configs[logtype].filename);
279 file_configs[logtype].filename = NULL;
280 close(file_configs[logtype].fd);
281 file_configs[logtype].fd = -1;
282 file_configs[logtype].level = 0;
283 file_configs[logtype].set = 0;
287 file_configs[logtype].filename = strdup(filename);
288 file_configs[logtype].level = loglevel;
291 /* Open log file as OPEN_LOGS_AS_UID*/
292 process_uid = geteuid();
294 seteuid(OPEN_LOGS_AS_UID);
295 file_configs[logtype].fd = open( file_configs[logtype].filename,
296 O_CREAT | O_WRONLY | O_APPEND,
297 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
299 seteuid(process_uid);
301 /* Check for error opening/creating logfile */
302 if (-1 == file_configs[logtype].fd) {
303 free(file_configs[logtype].filename);
304 file_configs[logtype].filename = NULL;
305 file_configs[logtype].level = -1;
306 file_configs[logtype].set = 0;
310 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
311 file_configs[logtype].set = 1;
312 log_config.filelogging = 1;
313 log_config.inited = 1;
315 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
316 /* which then uses the default logtype setup if it isn't setup itself: */
317 /* we just copy the loglevel from default to all logtypes that are not setup. */
318 /* In "make_log_entry" we then check for the logtypes if they arent setup */
319 /* and use default then. We must provide accessible values for all logtypes */
320 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
322 if (logtype == logtype_default) {
323 while (logtype != logtype_end_of_list_marker) {
324 if ( ! (file_configs[logtype].set))
325 file_configs[logtype].level = loglevel;
328 logtype = logtype_default;
331 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
332 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
335 /* logtype is ignored, it's just one for all */
336 void syslog_setup(int loglevel, enum logtypes logtype _U_,
337 int display_options, int facility)
339 log_config.syslog_level = loglevel;
340 log_config.syslog_display_options = display_options;
341 log_config.facility = facility;
343 log_config.inited = 1;
345 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
346 arr_loglevel_strings[loglevel]);
353 /* This function sets up the processname */
354 void set_processname(const char *processname)
356 strncpy(log_config.processname, processname, 15);
357 log_config.processname[15] = 0;
360 /* -------------------------------------------------------------------------
361 make_log_entry has 1 main flaws:
362 The message in its entirity, must fit into the tempbuffer.
363 So it must be shorter than MAXLOGSIZE
364 ------------------------------------------------------------------------- */
365 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
368 /* fn is not reentrant but is used in signal handler
369 * with LOGGER it's a little late source name and line number
370 * are already changed. */
371 static int inlog = 0;
373 char temp_buffer[MAXLOGSIZE];
374 char log_details_buffer[MAXLOGSIZE];
381 /* Check if requested logtype is setup */
382 if (file_configs[logtype].set)
384 fd = file_configs[logtype].fd;
386 /* No: use default */
387 fd = file_configs[logtype_default].fd;
390 /* no where to send the output, give up */
395 /* Initialise the Messages */
396 va_start(args, message);
397 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
401 if (len >= MAXLOGSIZE)
402 /* vsnprintf hit the buffer size*/
403 temp_buffer[MAXLOGSIZE-2] = '\n';
405 temp_buffer[len] = '\n';
406 temp_buffer[len+1] = 0;
409 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
410 file_configs[logtype].set ?
411 file_configs[logtype].display_options :
412 file_configs[logtype_default].display_options,
416 /* If default wasnt setup its fd is -1 */
417 iov[0].iov_base = log_details_buffer;
418 iov[0].iov_len = strlen(log_details_buffer);
419 iov[1].iov_base = temp_buffer;
420 iov[1].iov_len = strlen(temp_buffer);
426 /* Called by the LOG macro for syslog messages */
427 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message, ...)
430 char log_buffer[MAXLOGSIZE];
431 /* fn is not reentrant but is used in signal handler
432 * with LOGGER it's a little late source name and line number
433 * are already changed.
435 static int inlog = 0;
441 if ( ! (log_config.syslog_opened) ) {
442 openlog(log_config.processname, log_config.syslog_display_options,
443 log_config.facility);
444 log_config.syslog_opened = 1;
447 /* Initialise the Messages */
448 va_start(args, message);
449 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
452 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
457 void setuplog(const char *logstr)
459 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
460 ptr = strdup(logstr);
467 ptr = strpbrk(ptr, " \t");
470 while (*ptr && isspace(*ptr))
475 ptr = strpbrk(ptr, " \t");
478 while (*ptr && isspace(*ptr))
484 /* finally call setuplog, filename can be NULL */
485 setuplog_internal(loglevel, logtype, filename);
490 void unsetuplog(const char *logstr)
492 char *str, *logtype, *filename;
494 str = strdup(logstr);
499 /* get filename, can be NULL */
501 filename = strtok(NULL, " \t");
503 /* finally call setuplog, filename can be NULL */
504 setuplog_internal(NULL, str, filename);