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]);
200 if (templen == -1 || templen >= len)
206 if (logtype<num_logtype_strings) {
207 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
208 if (templen == -1 || templen >= len)
214 strncat(ptr, "): ", len);
218 static int get_syslog_equivalent(enum loglevels loglevel)
222 /* The question is we know how bad it is for us,
223 but how should that translate in the syslogs? */
228 case 3: /* warning */
232 case 5: /* information */
239 /* =========================================================================
240 Global function definitions
241 ========================================================================= */
245 syslog_setup(log_note, 0,
246 log_config.syslog_display_options,
247 log_config.facility);
250 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
256 if (file_configs[logtype].set) {
257 if (file_configs[logtype].filename) {
258 free(file_configs[logtype].filename);
259 file_configs[logtype].filename = NULL;
261 close(file_configs[logtype].fd);
262 file_configs[logtype].fd = -1;
263 file_configs[logtype].level = 0;
264 file_configs[logtype].set = 0;
266 /* if disabling default also set all "default using" levels to 0 */
267 if (logtype == logtype_default) {
268 while (logtype != logtype_end_of_list_marker) {
269 if ( ! (file_configs[logtype].set))
270 file_configs[logtype].level = 0;
280 if (NULL == filename)
283 /* Resetting existing config ? */
284 if (file_configs[logtype].set && file_configs[logtype].filename) {
285 free(file_configs[logtype].filename);
286 file_configs[logtype].filename = NULL;
287 close(file_configs[logtype].fd);
288 file_configs[logtype].fd = -1;
289 file_configs[logtype].level = 0;
290 file_configs[logtype].set = 0;
294 file_configs[logtype].filename = strdup(filename);
295 file_configs[logtype].level = loglevel;
298 /* Open log file as OPEN_LOGS_AS_UID*/
299 process_uid = geteuid();
301 if (seteuid(OPEN_LOGS_AS_UID) == -1) {
302 /* XXX failing silently */
306 file_configs[logtype].fd = open( file_configs[logtype].filename,
307 O_CREAT | O_WRONLY | O_APPEND,
308 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
310 if (seteuid(process_uid) == -1) {
311 LOG(log_error, logtype_logger, "can't seteuid back %s", strerror(errno));
316 /* Check for error opening/creating logfile */
317 if (-1 == file_configs[logtype].fd) {
318 free(file_configs[logtype].filename);
319 file_configs[logtype].filename = NULL;
320 file_configs[logtype].level = -1;
321 file_configs[logtype].set = 0;
325 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
326 file_configs[logtype].set = 1;
327 log_config.filelogging = 1;
328 log_config.inited = 1;
330 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
331 /* which then uses the default logtype setup if it isn't setup itself: */
332 /* we just copy the loglevel from default to all logtypes that are not setup. */
333 /* In "make_log_entry" we then check for the logtypes if they arent setup */
334 /* and use default then. We must provide accessible values for all logtypes */
335 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
337 if (logtype == logtype_default) {
338 while (logtype != logtype_end_of_list_marker) {
339 if ( ! (file_configs[logtype].set))
340 file_configs[logtype].level = loglevel;
343 logtype = logtype_default;
346 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
347 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
350 /* logtype is ignored, it's just one for all */
351 void syslog_setup(int loglevel, enum logtypes logtype _U_,
352 int display_options, int facility)
354 log_config.syslog_level = loglevel;
355 log_config.syslog_display_options = display_options;
356 log_config.facility = facility;
358 log_config.inited = 1;
360 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
361 arr_loglevel_strings[loglevel]);
368 /* This function sets up the processname */
369 void set_processname(const char *processname)
371 strncpy(log_config.processname, processname, 15);
372 log_config.processname[15] = 0;
375 /* -------------------------------------------------------------------------
376 make_log_entry has 1 main flaws:
377 The message in its entirity, must fit into the tempbuffer.
378 So it must be shorter than MAXLOGSIZE
379 ------------------------------------------------------------------------- */
380 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
383 /* fn is not reentrant but is used in signal handler
384 * with LOGGER it's a little late source name and line number
385 * are already changed. */
386 static int inlog = 0;
388 char temp_buffer[MAXLOGSIZE];
389 char log_details_buffer[MAXLOGSIZE];
396 /* Check if requested logtype is setup */
397 if (file_configs[logtype].set)
399 fd = file_configs[logtype].fd;
401 /* No: use default */
402 fd = file_configs[logtype_default].fd;
405 /* no where to send the output, give up */
410 /* Initialise the Messages */
411 va_start(args, message);
412 len = vsnprintf(temp_buffer, MAXLOGSIZE -1, message, args);
416 if (len ==-1 || len >= MAXLOGSIZE -1) {
417 /* vsnprintf hit the buffer size*/
418 temp_buffer[MAXLOGSIZE-2] = '\n';
419 temp_buffer[MAXLOGSIZE-1] = 0;
422 temp_buffer[len] = '\n';
423 temp_buffer[len+1] = 0;
426 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
427 file_configs[logtype].set ?
428 file_configs[logtype].display_options :
429 file_configs[logtype_default].display_options,
433 /* If default wasnt setup its fd is -1 */
434 iov[0].iov_base = log_details_buffer;
435 iov[0].iov_len = strlen(log_details_buffer);
436 iov[1].iov_base = temp_buffer;
437 iov[1].iov_len = strlen(temp_buffer);
443 /* Called by the LOG macro for syslog messages */
444 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype _U_, char *message, ...)
447 char log_buffer[MAXLOGSIZE];
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.
452 static int inlog = 0;
458 if ( ! (log_config.syslog_opened) ) {
459 openlog(log_config.processname, log_config.syslog_display_options,
460 log_config.facility);
461 log_config.syslog_opened = 1;
464 /* Initialise the Messages */
465 va_start(args, message);
466 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
468 log_buffer[MAXLOGSIZE -1] = 0;
469 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
474 void setuplog(const char *logstr)
476 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
477 ptr = strdup(logstr);
484 ptr = strpbrk(ptr, " \t");
487 while (*ptr && isspace(*ptr))
492 ptr = strpbrk(ptr, " \t");
495 while (*ptr && isspace(*ptr))
503 /* finally call setuplog, filename can be NULL */
504 setuplog_internal(loglevel, logtype, filename);
509 void unsetuplog(const char *logstr)
511 char *str, *logtype, *filename;
513 str = strdup(logstr);
518 /* get filename, can be NULL */
520 filename = strtok(NULL, " \t");
522 /* finally call setuplog, filename can be NULL */
523 setuplog_internal(NULL, str, filename);