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 ========================================================================= */
238 log_setup(LOGFILEPATH, log_note, logtype_default);
240 syslog_setup(log_note, 0,
241 log_config.syslog_display_options,
242 log_config.facility);
246 void log_setup(const char *filename, enum loglevels loglevel, enum logtypes logtype)
252 if (file_configs[logtype].set) {
253 if (file_configs[logtype].filename) {
254 free(file_configs[logtype].filename);
255 file_configs[logtype].filename = NULL;
257 close(file_configs[logtype].fd);
258 file_configs[logtype].fd = -1;
259 file_configs[logtype].level = 0;
260 file_configs[logtype].set = 0;
262 /* if disabling default also set all "default using" levels to 0 */
263 if (logtype == logtype_default) {
264 while (logtype != logtype_end_of_list_marker) {
265 if ( ! (file_configs[logtype].set))
266 file_configs[logtype].level = 0;
276 if (NULL == filename)
279 /* Resetting existing config ? */
280 if (file_configs[logtype].set && file_configs[logtype].filename) {
281 free(file_configs[logtype].filename);
282 file_configs[logtype].filename = NULL;
283 close(file_configs[logtype].fd);
284 file_configs[logtype].fd = -1;
285 file_configs[logtype].level = 0;
286 file_configs[logtype].set = 0;
290 file_configs[logtype].filename = strdup(filename);
291 file_configs[logtype].level = loglevel;
294 /* Open log file as OPEN_LOGS_AS_UID*/
295 process_uid = geteuid();
297 seteuid(OPEN_LOGS_AS_UID);
298 file_configs[logtype].fd = open( file_configs[logtype].filename,
299 O_CREAT | O_WRONLY | O_APPEND,
300 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
302 seteuid(process_uid);
304 /* Check for error opening/creating logfile */
305 if (-1 == file_configs[logtype].fd) {
306 free(file_configs[logtype].filename);
307 file_configs[logtype].filename = NULL;
308 file_configs[logtype].level = -1;
309 file_configs[logtype].set = 0;
313 fcntl(file_configs[logtype].fd, F_SETFD, FD_CLOEXEC);
314 file_configs[logtype].set = 1;
315 log_config.filelogging = 1;
316 log_config.inited = 1;
318 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
319 /* which then uses the default logtype setup if it isn't setup itself: */
320 /* we just copy the loglevel from default to all logtypes that are not setup. */
321 /* In "make_log_entry" we then check for the logtypes if they arent setup */
322 /* and use default then. We must provide accessible values for all logtypes */
323 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
325 if (logtype == logtype_default) {
326 while (logtype != logtype_end_of_list_marker) {
327 if ( ! (file_configs[logtype].set))
328 file_configs[logtype].level = loglevel;
331 logtype = logtype_default;
334 LOG(log_debug, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
335 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
338 /* logtype is ignored, it's just one for all */
339 void syslog_setup(int loglevel, enum logtypes logtype _U_,
340 int display_options, int facility)
342 log_config.syslog_level = loglevel;
343 log_config.syslog_display_options = display_options;
344 log_config.facility = facility;
346 log_config.inited = 1;
348 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
349 arr_loglevel_strings[loglevel]);
356 /* This function sets up the processname */
357 void set_processname(const char *processname)
359 strncpy(log_config.processname, processname, 15);
360 log_config.processname[15] = 0;
363 /* -------------------------------------------------------------------------
364 make_log_entry has 1 main flaws:
365 The message in its entirity, must fit into the tempbuffer.
366 So it must be shorter than MAXLOGSIZE
367 ------------------------------------------------------------------------- */
368 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
371 /* fn is not reentrant but is used in signal handler
372 * with LOGGER it's a little late source name and line number
373 * are already changed. */
374 static int inlog = 0;
376 char temp_buffer[MAXLOGSIZE];
377 char log_details_buffer[MAXLOGSIZE];
385 /* Initialise the Messages */
386 va_start(args, message);
387 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
391 if (len >= MAXLOGSIZE)
392 /* vsnprintf hit the buffer size*/
393 temp_buffer[MAXLOGSIZE-2] = '\n';
395 temp_buffer[len] = '\n';
396 temp_buffer[len+1] = 0;
399 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
400 file_configs[logtype].set ?
401 file_configs[logtype].display_options :
402 file_configs[logtype_default].display_options,
405 /* Check if requested logtype is setup */
406 if (file_configs[logtype].set)
408 fd = file_configs[logtype].fd;
410 /* No: use default */
411 fd = file_configs[logtype_default].fd;
413 /* If default wasnt setup its fd is -1 */
415 iov[0].iov_base = log_details_buffer;
416 iov[0].iov_len = strlen(log_details_buffer);
417 iov[1].iov_base = temp_buffer;
418 iov[1].iov_len = strlen(temp_buffer);
425 /* Called by the LOG macro for syslog messages */
426 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
429 char log_buffer[MAXLOGSIZE];
430 /* fn is not reentrant but is used in signal handler
431 * with LOGGER it's a little late source name and line number
432 * are already changed.
434 static int inlog = 0;
440 if ( ! (log_config.syslog_opened) ) {
441 openlog(log_config.processname, log_config.syslog_display_options,
442 log_config.facility);
443 log_config.syslog_opened = 1;
446 /* Initialise the Messages */
447 va_start(args, message);
448 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
451 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
456 void setuplog(const char *logstr)
458 char *ptr, *ptrbak, *logtype, *loglevel, *filename;
459 ptr = strdup(logstr);
466 ptr = strpbrk(ptr, " \t");
469 while (*ptr && isspace(*ptr))
474 ptr = strpbrk(ptr, " \t");
477 while (*ptr && isspace(*ptr))
483 /* finally call setuplog, filename can be NULL */
484 setuplog_internal(logtype, loglevel, filename);
489 void unsetuplog(const char *logstr)
491 char *str, *logtype, *filename;
493 str = strdup(logstr);
498 /* get filename, can be NULL */
500 filename = strtok(NULL, " \t");
502 /* finally call setuplog, filename can be NULL */
503 setuplog_internal(str, NULL, filename);