5 /* =========================================================================
7 logger.c is part of the utils section in the libatalk library,
8 which is part of the netatalk project.
10 logger.c was written by Simon Bazley (sibaz@sibaz.com)
12 I believe libatalk is released under the L/GPL licence.
13 Just incase, it is, thats the licence I'm applying to this file.
16 ==========================================================================
18 Logger.c is intended as an alternative to syslog for logging
20 ========================================================================= */
28 #include <sys/types.h>
35 #include <atalk/boolean.h>
38 #include <atalk/logger.h>
41 #define OPEN_LOGS_AS_UID 0
43 #define COUNT_ARRAY(array) (sizeof((array))/sizeof((array)[0]))
45 /* =========================================================================
47 ========================================================================= */
49 /* Main log config container, must be globally visible */
50 log_config_t log_config = {
51 0, /* Initialized ? 0 = no */
52 0, /* No filelogging setup yet */
53 {0}, /* processname */
54 0, /* syslog opened ? */
55 logfacility_daemon, /* syslog facility to use */
56 logoption_ndelay|logoption_pid, /* logging options for syslog */
57 0 /* log level for syslog */
60 /* Default log config: log nothing to files.
61 0: not set individually
66 #define DEFAULT_LOG_CONFIG {0, NULL, -1, 0, 0}
68 filelog_conf_t file_configs[logtype_end_of_list_marker] = {
69 DEFAULT_LOG_CONFIG, /* logtype_default */
70 DEFAULT_LOG_CONFIG, /* logtype_core */
71 DEFAULT_LOG_CONFIG, /* logtype_logger */
72 DEFAULT_LOG_CONFIG, /* logtype_cnid */
73 DEFAULT_LOG_CONFIG, /* logtype_afpd */
74 DEFAULT_LOG_CONFIG, /* logtype_atalkd */
75 DEFAULT_LOG_CONFIG, /* logtype_papd */
76 DEFAULT_LOG_CONFIG /* logtype_uams */
79 /* These are used by the LOG macro to store __FILE__ and __LINE__ */
80 char *log_src_filename;
81 int log_src_linenumber;
83 /* Array to store text to list given a log type */
84 static const char *arr_logtype_strings[] = LOGTYPE_STRING_IDENTIFIERS;
85 static const int num_logtype_strings = COUNT_ARRAY(arr_logtype_strings);
87 /* Array for charachters representing log severity in the log file */
88 static const char arr_loglevel_chars[] = {'-','S', 'E', 'W', 'N', 'I', 'D'};
89 static const int num_loglevel_chars = COUNT_ARRAY(arr_loglevel_chars);
91 static const char *arr_loglevel_strings[] = LOGLEVEL_STRING_IDENTIFIERS;
92 static const int num_loglevel_strings = COUNT_ARRAY(arr_loglevel_strings);
94 /* =========================================================================
95 Internal function definitions
96 ========================================================================= */
98 static void generate_message_details(char *message_details_buffer,
99 int message_details_buffer_length,
101 enum loglevels loglevel, enum logtypes logtype)
103 char *ptr = message_details_buffer;
105 int len = message_details_buffer_length;
113 strftime(ptr, len, "%b %d %H:%M:%S ", localtime(&thetime));
114 templen = strlen(ptr);
119 strncpy(ptr, log_config.processname, len);
120 templen = strlen(ptr);
125 pid_t pid = getpid();
126 templen = snprintf(ptr, len, "[%d]", pid);
131 if ( ! (display_options & logoption_nsrcinfo)) {
132 char *basename = strrchr(log_src_filename, '/');
134 templen = snprintf(ptr, len, " {%s:%d}", basename + 1, log_src_linenumber);
136 templen = snprintf(ptr, len, " {%s:%d}", log_src_filename, log_src_linenumber);
144 if (loglevel >= (num_loglevel_chars - 1))
145 templen = snprintf(ptr, len, " (D%d:", loglevel - 1);
147 templen = snprintf(ptr, len, " (%c:", arr_loglevel_chars[loglevel]);
152 if (logtype<num_logtype_strings) {
153 templen = snprintf(ptr, len, "%s", arr_logtype_strings[logtype]);
158 strncat(ptr, "): ", len);
161 int get_syslog_equivalent(enum loglevels loglevel)
165 /* The question is we know how bad it is for us,
166 but how should that translate in the syslogs? */
171 case 3: /* warning */
175 case 5: /* information */
182 /* =========================================================================
183 Global function definitions
184 ========================================================================= */
189 log_setup(LOGFILEPATH, log_note, logtype_default);
191 syslog_setup(log_note, 0,
192 log_config.syslog_display_options,
193 log_config.facility);
197 void log_setup(char *filename, enum loglevels loglevel, enum logtypes logtype)
203 if (file_configs[logtype].set) {
204 if (file_configs[logtype].filename) {
205 free(file_configs[logtype].filename);
206 file_configs[logtype].filename = NULL;
208 close(file_configs[logtype].fd);
209 file_configs[logtype].fd = -1;
210 file_configs[logtype].level = 0;
211 file_configs[logtype].set = 0;
213 /* if disabling default also set all "default using" levels to 0 */
214 if (logtype == logtype_default) {
215 while (logtype != logtype_end_of_list_marker) {
216 if ( ! (file_configs[logtype].set))
217 file_configs[logtype].level = 0;
227 if (NULL == filename)
230 /* Resetting existing config ? */
231 if (file_configs[logtype].set && file_configs[logtype].filename) {
232 free(file_configs[logtype].filename);
233 file_configs[logtype].filename = NULL;
234 close(file_configs[logtype].fd);
235 file_configs[logtype].fd = -1;
236 file_configs[logtype].level = 0;
237 file_configs[logtype].set = 0;
241 file_configs[logtype].filename = strdup(filename);
242 file_configs[logtype].level = loglevel;
245 /* Open log file as OPEN_LOGS_AS_UID*/
246 process_uid = geteuid();
248 seteuid(OPEN_LOGS_AS_UID);
249 file_configs[logtype].fd = open( file_configs[logtype].filename,
250 O_CREAT | O_WRONLY | O_APPEND,
251 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
253 seteuid(process_uid);
255 /* Check for error opening/creating logfile */
256 if (-1 == file_configs[logtype].fd) {
257 free(file_configs[logtype].filename);
258 file_configs[logtype].filename = NULL;
259 file_configs[logtype].level = -1;
260 file_configs[logtype].set = 0;
264 file_configs[logtype].set = 1;
265 log_config.filelogging = 1;
266 log_config.inited = 1;
268 /* Here's how we make it possible to LOG to a logtype like "logtype_afpd" */
269 /* which then uses the default logtype setup if it isn't setup itself: */
270 /* we just copy the loglevel from default to all logtypes that are not setup. */
271 /* In "make_log_entry" we then check for the logtypes if they arent setup */
272 /* and use default then. We must provide accessible values for all logtypes */
273 /* in order to make it easy and fast to check the loglevels in the LOG macro! */
275 if (logtype == logtype_default) {
276 while (logtype != logtype_end_of_list_marker) {
277 if ( ! (file_configs[logtype].set))
278 file_configs[logtype].level = loglevel;
281 logtype = logtype_default;
284 LOG(log_note, logtype_logger, "Setup file logging: type: %s, level: %s, file: %s",
285 arr_logtype_strings[logtype], arr_loglevel_strings[loglevel], file_configs[logtype].filename);
288 /* logtype is ignored, it's just one for all */
289 void syslog_setup(int loglevel, enum logtypes logtype _U_,
290 int display_options, int facility)
292 log_config.syslog_level = loglevel;
293 log_config.syslog_display_options = display_options;
294 log_config.facility = facility;
296 log_config.inited = 1;
298 LOG(log_note, logtype_logger, "Set syslog logging to level: %s",
299 arr_loglevel_strings[loglevel]);
306 /* This function sets up the processname */
307 void set_processname(char *processname)
309 strncpy(log_config.processname, processname, 15);
310 log_config.processname[15] = 0;
313 /* -------------------------------------------------------------------------
314 make_log_entry has 1 main flaws:
315 The message in its entirity, must fit into the tempbuffer.
316 So it must be shorter than MAXLOGSIZE
317 ------------------------------------------------------------------------- */
318 void make_log_entry(enum loglevels loglevel, enum logtypes logtype,
321 /* fn is not reentrant but is used in signal handler
322 * with LOGGER it's a little late source name and line number
323 * are already changed. */
324 static int inlog = 0;
326 char temp_buffer[MAXLOGSIZE];
327 char log_details_buffer[MAXLOGSIZE];
335 /* Initialise the Messages */
336 va_start(args, message);
337 len = vsnprintf(temp_buffer, MAXLOGSIZE - 1, message, args);
341 if (len >= MAXLOGSIZE)
342 /* vsnprintf hit the buffer size*/
343 temp_buffer[MAXLOGSIZE-2] = '\n';
345 temp_buffer[len] = '\n';
346 temp_buffer[len+1] = 0;
349 generate_message_details(log_details_buffer, sizeof(log_details_buffer),
350 file_configs[logtype].set ?
351 file_configs[logtype].display_options :
352 file_configs[logtype_default].display_options,
355 /* Check if requested logtype is setup */
356 if (file_configs[logtype].set)
358 fd = file_configs[logtype].fd;
360 /* No: use default */
361 fd = file_configs[logtype_default].fd;
363 /* If default wasnt setup its fd is -1 */
365 iov[0].iov_base = log_details_buffer;
366 iov[0].iov_len = strlen(log_details_buffer);
367 iov[1].iov_base = temp_buffer;
368 iov[1].iov_len = strlen(temp_buffer);
375 /* Called by the LOG macro for syslog messages */
376 void make_syslog_entry(enum loglevels loglevel, enum logtypes logtype, char *message, ...)
379 char log_buffer[MAXLOGSIZE];
380 /* fn is not reentrant but is used in signal handler
381 * with LOGGER it's a little late source name and line number
382 * are already changed.
384 static int inlog = 0;
390 if ( ! (log_config.syslog_opened) ) {
391 openlog(log_config.processname, log_config.syslog_display_options,
392 log_config.facility);
393 log_config.syslog_opened = 1;
396 /* Initialise the Messages */
397 va_start(args, message);
398 vsnprintf(log_buffer, sizeof(log_buffer), message, args);
401 syslog(get_syslog_equivalent(loglevel), "%s", log_buffer);
407 * This is called from the afpd.conf parsing code.
408 * If filename == NULL its for syslog logging, otherwise its for file-logging.
409 * "unsetuplog" calls with loglevel == NULL.
410 * loglevel == NULL means:
411 * if logtype == default
414 * set to default logging
417 /* -[un]setuplog <logtype> <loglevel> [<filename>]*/
418 void setuplog(char *logtype, char *loglevel, char *filename)
420 int typenum, levelnum;
423 for( typenum=0; typenum < num_logtype_strings; typenum++) {
424 if (strcasecmp(logtype, arr_logtype_strings[typenum]) == 0)
427 if (typenum >= num_logtype_strings) {
432 if (loglevel == NULL) {
435 for(levelnum=1; levelnum < num_loglevel_strings; levelnum++) {
436 if (strcasecmp(loglevel, arr_loglevel_strings[levelnum]) == 0)
439 if (levelnum >= num_loglevel_strings) {
444 /* is this a syslog setup or a filelog setup ? */
445 if (filename == NULL) {
447 syslog_setup(levelnum, 0,
448 log_config.syslog_display_options,
449 log_config.facility);
451 /* this must be a filelog */
452 log_setup(filename, levelnum, typenum);