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 *logtype, const char *loglevel, 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;
157 gettimeofday(&tv, NULL);
158 strftime(ptr, len, "%b %d %H:%M:%S.", localtime(&tv.tv_sec));
159 templen = strlen(ptr);
163 templen = snprintf(ptr, len, "%06u ", (int)tv.tv_usec);
168 strncpy(ptr, log_config.processname, len);
169 templen = strlen(ptr);
174 pid_t pid = getpid();
175 templen = snprintf(ptr, len, "[%d]", pid);
180 if ( ! (display_options & logoption_nsrcinfo)) {
181 char *basename = strrchr(log_src_filename, '/');
183 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
185 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
193 if (loglevel >= (num_loglevel_chars - 1))
194 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
196 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
201 if (logtype<num_logtype_strings) {
202 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
207 strncat(ptr, "): ", len);
210 int get_syslog_equivalent(enum loglevels loglevel)
214 /* The question is we know how bad it is for us,
215 but how should that translate in the syslogs? */
220 case 3: /* warning */
224 case 5: /* information */
231 /* =========================================================================
232 Global function definitions
233 ========================================================================= */
237 syslog_setup(log_note, 0,
238 log_config.syslog_display_options,
239 log_config.facility);
242 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
248 if (file_configs[logtype].set) {
249 if (file_configs[logtype].filename) {
250 free(file_configs[logtype].filename);
251 file_configs[logtype].filename = NULL;
253 close(file_configs[logtype].fd);
254 file_configs[logtype].fd = -1;
255 file_configs[logtype].level = 0;
256 file_configs[logtype].set = 0;
258 /* if disabling default also set all "default using" levels to 0 */
259 if (logtype == logtype_default) {
260 while (logtype != logtype_end_of_list_marker) {
261 if ( ! (file_configs[logtype].set))
262 file_configs[logtype].level = 0;
272 if (NULL == filename)
275 /* Resetting existing config ? */
276 if (file_configs[logtype].set && file_configs[logtype].filename) {
277 free(file_configs[logtype].filename);
278 file_configs[logtype].filename = NULL;
279 close(file_configs[logtype].fd);
280 file_configs[logtype].fd = -1;
281 file_configs[logtype].level = 0;
282 file_configs[logtype].set = 0;
286 file_configs[logtype].filename = strdup(filename);
287 file_configs[logtype].level = loglevel;
290 /* Open log file as OPEN_LOGS_AS_UID*/
291 process_uid = geteuid();
293 seteuid(OPEN_LOGS_AS_UID);
294 file_configs[logtype].fd = open( file_configs[logtype].filename,
295 O_CREAT | O_WRONLY | O_APPEND,
296 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
298 seteuid(process_uid);
300 /* Check for error opening/creating logfile */
301 if (-1 == file_configs[logtype].fd) {
302 free(file_configs[logtype].filename);
303 file_configs[logtype].filename = NULL;
304 file_configs[logtype].level = -1;
305 file_configs[logtype].set = 0;
309 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
310 file_configs[logtype].set = 1;
311 log_config.filelogging = 1;
312 log_config.inited = 1;
314 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
315 /* which then uses the default logtype setup if it isn't setup itself: */
316 /* we just copy the loglevel from default to all logtypes that are not setup. */
317 /* In "make_log_entry" we then check for the logtypes if they arent setup */
318 /* and use default then. We must provide accessible values for all logtypes */
319 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
321 if (logtype == logtype_default) {
322 while (logtype != logtype_end_of_list_marker) {
323 if ( ! (file_configs[logtype].set))
324 file_configs[logtype].level = loglevel;
327 logtype = logtype_default;
330 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
331 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
334 /* logtype is ignored, it's just one for all */
335 void syslog_setup(int loglevel, enum logtypes logtype _U_,
336 int display_options, int facility)
338 log_config.syslog_level = loglevel;
339 log_config.syslog_display_options = display_options;
340 log_config.facility = facility;
342 log_config.inited = 1;
344 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
345 arr_loglevel_strings[loglevel]);
352 /* This function sets up the processname */
353 void set_processname(const char *processname)
355 strncpy(log_config.processname, processname, 15);
356 log_config.processname[15] = 0;
359 /* -------------------------------------------------------------------------
360 make_log_entry has 1 main flaws:
361 The message in its entirity, must fit into the tempbuffer.
362 So it must be shorter than MAXLOGSIZE
363 ------------------------------------------------------------------------- */
364 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
367 /* fn is not reentrant but is used in signal handler
368 * with LOGGER it's a little late source name and line number
369 * are already changed. */
370 static int inlog = 0;
372 char temp_buffer[MAXLOGSIZE];
373 char log_details_buffer[MAXLOGSIZE];
381 /* Initialise the Messages */
382 va_start(args, message);
383 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
387 if (len >= MAXLOGSIZE)
388 /* vsnprintf hit the buffer size*/
389 temp_buffer[MAXLOGSIZE-2] = '\n';
391 temp_buffer[len] = '\n';
392 temp_buffer[len+1] = 0;
395 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
396 file_configs[logtype].set ?
397 file_configs[logtype].display_options :
398 file_configs[logtype_default].display_options,
401 /* Check if requested logtype is setup */
402 if (file_configs[logtype].set)
404 fd = file_configs[logtype].fd;
406 /* No: use default */
407 fd = file_configs[logtype_default].fd;
409 /* If default wasnt setup its fd is -1 */
411 iov[0].iov_base = log_details_buffer;
412 iov[0].iov_len = strlen(log_details_buffer);
413 iov[1].iov_base = temp_buffer;
414 iov[1].iov_len = strlen(temp_buffer);
421 /* Called by the LOG macro for syslog messages */
422 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
425 char log_buffer[MAXLOGSIZE];
426 /* fn is not reentrant but is used in signal handler
427 * with LOGGER it's a little late source name and line number
428 * are already changed.
430 static int inlog = 0;
436 if ( ! (log_config.syslog_opened) ) {
437 openlog(log_config.processname, log_config.syslog_display_options,
438 log_config.facility);
439 log_config.syslog_opened = 1;
442 /* Initialise the Messages */
443 va_start(args, message);
444 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
447 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
452 void setuplog(const char *logstr)
454 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
455 ptr = strdup(logstr);
462 ptr = strpbrk(ptr, " \t");
465 while (*ptr && isspace(*ptr))
470 ptr = strpbrk(ptr, " \t");
473 while (*ptr && isspace(*ptr))
479 /* finally call setuplog, filename can be NULL */
480 setuplog_internal(logtype, loglevel, filename);
485 void unsetuplog(const char *logstr)
487 char *str, *logtype, *filename;
489 str = strdup(logstr);
494 /* get filename, can be NULL */
496 filename = strtok(NULL, " \t");
498 /* finally call setuplog, filename can be NULL */
499 setuplog_internal(str, NULL, filename);