corosync  2.3.4
logconfig.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2002-2005 MontaVista Software, Inc.
3  * Copyright (c) 2006-2011 Red Hat, Inc.
4  *
5  * All rights reserved.
6  *
7  * Author: Steven Dake (sdake@redhat.com)
8  * Jan Friesse (jfriesse@redhat.com)
9  *
10  * This software licensed under BSD license, the text of which follows:
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * - Redistributions of source code must retain the above copyright notice,
16  * this list of conditions and the following disclaimer.
17  * - Redistributions in binary form must reproduce the above copyright notice,
18  * this list of conditions and the following disclaimer in the documentation
19  * and/or other materials provided with the distribution.
20  * - Neither the name of the MontaVista Software, Inc. nor the names of its
21  * contributors may be used to endorse or promote products derived from this
22  * software without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
34  * THE POSSIBILITY OF SUCH DAMAGE.
35  */
36 
37 #include "config.h"
38 
39 #include <corosync/totem/totem.h>
40 #include <corosync/logsys.h>
41 #ifdef LOGCONFIG_USE_ICMAP
42 #include <corosync/icmap.h>
43 #define MAP_KEYNAME_MAXLEN ICMAP_KEYNAME_MAXLEN
44 #define map_get_string(key_name, value) icmap_get_string(key_name, value)
45 #else
46 #include <corosync/cmap.h>
48 static const char *main_logfile;
49 #define MAP_KEYNAME_MAXLEN CMAP_KEYNAME_MAXLEN
50 #define map_get_string(key_name, value) cmap_get_string(cmap_handle, key_name, value)
51 #endif
52 
53 #include "util.h"
54 #include "logconfig.h"
55 
56 static char error_string_response[512];
57 
79 static int insert_into_buffer(
80  char *target_buffer,
81  size_t bufferlen,
82  const char *entry,
83  const char *after)
84 {
85  const char *current_format = NULL;
86 
87  current_format = logsys_format_get();
88 
89  /* if the entry is already in the format we don't add it again */
90  if (strstr(current_format, entry) != NULL) {
91  return -1;
92  }
93 
94  /* if there is no "after", simply prepend the requested entry
95  * otherwise go for beautiful string manipulation.... </sarcasm> */
96  if (!after) {
97  if (snprintf(target_buffer, bufferlen - 1, "%s%s",
98  entry,
99  current_format) >= bufferlen - 1) {
100  return -1;
101  }
102  } else {
103  const char *afterpos;
104  size_t afterlen;
105  size_t templen;
106 
107  /* check if after is contained in the format
108  * and afterlen has a meaning or return an error */
109  afterpos = strstr(current_format, after);
110  afterlen = strlen(after);
111  if ((!afterpos) || (!afterlen)) {
112  return -1;
113  }
114 
115  templen = afterpos - current_format + afterlen;
116  if (snprintf(target_buffer, templen + 1, "%s", current_format)
117  >= bufferlen - 1) {
118  return -1;
119  }
120  if (snprintf(target_buffer + templen, bufferlen - ( templen + 1 ),
121  "%s%s", entry, current_format + templen)
122  >= bufferlen - ( templen + 1 )) {
123  return -1;
124  }
125  }
126  return 0;
127 }
128 
129 /*
130  * format set is the only global specific option that
131  * doesn't apply at system/subsystem level.
132  */
133 static int corosync_main_config_format_set (
134  const char **error_string)
135 {
136  const char *error_reason;
137  char new_format_buffer[PATH_MAX];
138  char *value = NULL;
139  int err = 0;
140 
141  if (map_get_string("logging.fileline", &value) == CS_OK) {
142  if (strcmp (value, "on") == 0) {
143  if (!insert_into_buffer(new_format_buffer,
144  sizeof(new_format_buffer),
145  " %f:%l", "g]")) {
146  err = logsys_format_set(new_format_buffer);
147  } else
148  if (!insert_into_buffer(new_format_buffer,
149  sizeof(new_format_buffer),
150  "%f:%l", NULL)) {
151  err = logsys_format_set(new_format_buffer);
152  }
153  } else
154  if (strcmp (value, "off") == 0) {
155  /* nothing to do here */
156  } else {
157  error_reason = "unknown value for fileline";
158  free(value);
159  goto parse_error;
160  }
161 
162  free(value);
163  }
164 
165  if (err) {
166  error_reason = "not enough memory to set logging format buffer";
167  goto parse_error;
168  }
169 
170  if (map_get_string("logging.function_name", &value) == CS_OK) {
171  if (strcmp (value, "on") == 0) {
172  if (!insert_into_buffer(new_format_buffer,
173  sizeof(new_format_buffer),
174  "%n:", "f:")) {
175  err = logsys_format_set(new_format_buffer);
176  } else
177  if (!insert_into_buffer(new_format_buffer,
178  sizeof(new_format_buffer),
179  " %n", "g]")) {
180  err = logsys_format_set(new_format_buffer);
181  }
182  } else
183  if (strcmp (value, "off") == 0) {
184  /* nothing to do here */
185  } else {
186  error_reason = "unknown value for function_name";
187  free(value);
188  goto parse_error;
189  }
190 
191  free(value);
192  }
193 
194  if (err) {
195  error_reason = "not enough memory to set logging format buffer";
196  goto parse_error;
197  }
198 
199  if (map_get_string("logging.timestamp", &value) == CS_OK) {
200  if (strcmp (value, "on") == 0) {
201  if(!insert_into_buffer(new_format_buffer,
202  sizeof(new_format_buffer),
203  "%t ", NULL)) {
204  err = logsys_format_set(new_format_buffer);
205  }
206  } else
207  if (strcmp (value, "off") == 0) {
208  /* nothing to do here */
209  } else {
210  error_reason = "unknown value for timestamp";
211  free(value);
212  goto parse_error;
213  }
214 
215  free(value);
216  }
217 
218  if (err) {
219  error_reason = "not enough memory to set logging format buffer";
220  goto parse_error;
221  }
222 
223  return (0);
224 
225 parse_error:
226  *error_string = error_reason;
227 
228  return (-1);
229 }
230 
231 static int corosync_main_config_log_destination_set (
232  const char *path,
233  const char *key,
234  const char *subsys,
235  const char **error_string,
236  unsigned int mode_mask,
237  char deprecated,
238  const char *replacement)
239 {
240  static char formatted_error_reason[128];
241  char *value = NULL;
242  unsigned int mode;
243  char key_name[MAP_KEYNAME_MAXLEN];
244 
245  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, key);
246  if (map_get_string(key_name, &value) == CS_OK) {
247  if (deprecated) {
249  "Warning: the %s config paramater has been obsoleted."
250  " See corosync.conf man page %s directive.",
251  key, replacement);
252  }
253 
254  mode = logsys_config_mode_get (subsys);
255 
256  if (strcmp (value, "yes") == 0 || strcmp (value, "on") == 0) {
257  mode |= mode_mask;
258  if (logsys_config_mode_set(subsys, mode) < 0) {
259  sprintf (formatted_error_reason, "unable to set mode %s", key);
260  goto parse_error;
261  }
262  } else
263  if (strcmp (value, "no") == 0 || strcmp (value, "off") == 0) {
264  mode &= ~mode_mask;
265  if (logsys_config_mode_set(subsys, mode) < 0) {
266  sprintf (formatted_error_reason, "unable to unset mode %s", key);
267  goto parse_error;
268  }
269  } else {
270  sprintf (formatted_error_reason, "unknown value for %s", key);
271  goto parse_error;
272  }
273  }
274 
275  free(value);
276  return (0);
277 
278 parse_error:
279  *error_string = formatted_error_reason;
280  free(value);
281  return (-1);
282 }
283 
284 static int corosync_main_config_set (
285  const char *path,
286  const char *subsys,
287  const char **error_string)
288 {
289  const char *error_reason = error_string_response;
290  char *value = NULL;
291  int mode;
292  char key_name[MAP_KEYNAME_MAXLEN];
293 
294  /*
295  * this bit abuses the internal logsys exported API
296  * to guarantee that all configured subsystems are
297  * initialized too.
298  *
299  * using this approach avoids some headaches caused
300  * by IPC and TOTEM that have a special logging
301  * handling requirements
302  */
303  if (subsys != NULL) {
304  if (_logsys_subsys_create(subsys, NULL) < 0) {
305  error_reason = "unable to create new logging subsystem";
306  goto parse_error;
307  }
308  }
309 
310  mode = logsys_config_mode_get(subsys);
311  if (mode < 0) {
312  error_reason = "unable to get mode";
313  goto parse_error;
314  }
315 
316  if (corosync_main_config_log_destination_set (path, "to_stderr", subsys, &error_reason,
317  LOGSYS_MODE_OUTPUT_STDERR, 0, NULL) != 0)
318  goto parse_error;
319 
320  if (corosync_main_config_log_destination_set (path, "to_syslog", subsys, &error_reason,
321  LOGSYS_MODE_OUTPUT_SYSLOG, 0, NULL) != 0)
322  goto parse_error;
323 
324  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_facility");
325  if (map_get_string(key_name, &value) == CS_OK) {
326  int syslog_facility;
327 
328  syslog_facility = qb_log_facility2int(value);
329  if (syslog_facility < 0) {
330  error_reason = "unknown syslog facility specified";
331  goto parse_error;
332  }
334  syslog_facility) < 0) {
335  error_reason = "unable to set syslog facility";
336  goto parse_error;
337  }
338 
339  free(value);
340  }
341 
342  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_level");
343  if (map_get_string(key_name, &value) == CS_OK) {
344  int syslog_priority;
345 
347  "Warning: the syslog_level config paramater has been obsoleted."
348  " See corosync.conf man page syslog_priority directive.");
349 
350  syslog_priority = logsys_priority_id_get(value);
351  free(value);
352 
353  if (syslog_priority < 0) {
354  error_reason = "unknown syslog level specified";
355  goto parse_error;
356  }
358  syslog_priority) < 0) {
359  error_reason = "unable to set syslog level";
360  goto parse_error;
361  }
362  }
363 
364  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "syslog_priority");
365  if (map_get_string(key_name, &value) == CS_OK) {
366  int syslog_priority;
367 
368  syslog_priority = logsys_priority_id_get(value);
369  free(value);
370  if (syslog_priority < 0) {
371  error_reason = "unknown syslog priority specified";
372  goto parse_error;
373  }
375  syslog_priority) < 0) {
376  error_reason = "unable to set syslog priority";
377  goto parse_error;
378  }
379  }
380 
381 #ifdef LOGCONFIG_USE_ICMAP
382  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile");
383  if (map_get_string(key_name, &value) == CS_OK) {
384  if (logsys_config_file_set (subsys, &error_reason, value) < 0) {
385  goto parse_error;
386  }
387  free(value);
388  }
389 #else
390  if (!subsys) {
391  if (logsys_config_file_set (subsys, &error_reason, main_logfile) < 0) {
392  goto parse_error;
393  }
394  }
395 #endif
396 
397  if (corosync_main_config_log_destination_set (path, "to_file", subsys, &error_reason,
398  LOGSYS_MODE_OUTPUT_FILE, 1, "to_logfile") != 0)
399  goto parse_error;
400 
401  if (corosync_main_config_log_destination_set (path, "to_logfile", subsys, &error_reason,
402  LOGSYS_MODE_OUTPUT_FILE, 0, NULL) != 0)
403  goto parse_error;
404 
405  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "logfile_priority");
406  if (map_get_string(key_name, &value) == CS_OK) {
407  int logfile_priority;
408 
409  logfile_priority = logsys_priority_id_get(value);
410  free(value);
411  if (logfile_priority < 0) {
412  error_reason = "unknown logfile priority specified";
413  goto parse_error;
414  }
416  logfile_priority) < 0) {
417  error_reason = "unable to set logfile priority";
418  goto parse_error;
419  }
420  }
421 
422  snprintf(key_name, MAP_KEYNAME_MAXLEN, "%s.%s", path, "debug");
423  if (map_get_string(key_name, &value) == CS_OK) {
424  if (strcmp (value, "trace") == 0) {
425  if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_TRACE) < 0) {
426  error_reason = "unable to set debug trace";
427  free(value);
428  goto parse_error;
429  }
430  } else
431  if (strcmp (value, "on") == 0) {
432  if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_ON) < 0) {
433  error_reason = "unable to set debug on";
434  free(value);
435  goto parse_error;
436  }
437  } else
438  if (strcmp (value, "off") == 0) {
439  if (logsys_config_debug_set (subsys, LOGSYS_DEBUG_OFF) < 0) {
440  error_reason = "unable to set debug off";
441  free(value);
442  goto parse_error;
443  }
444  } else {
445  error_reason = "unknown value for debug";
446  free(value);
447  goto parse_error;
448  }
449  free(value);
450  }
451 
452  return (0);
453 
454 parse_error:
455  *error_string = error_reason;
456 
457  return (-1);
458 }
459 
460 static int corosync_main_config_read_logging (
461  const char **error_string)
462 {
463  const char *error_reason;
464 #ifdef LOGCONFIG_USE_ICMAP
465  icmap_iter_t iter;
466  const char *key_name;
467 #else
468  cmap_iter_handle_t iter;
469  char key_name[CMAP_KEYNAME_MAXLEN];
470 #endif
471  char key_subsys[MAP_KEYNAME_MAXLEN];
472  char key_item[MAP_KEYNAME_MAXLEN];
473  int res;
474 
475  /* format set is supported only for toplevel */
476  if (corosync_main_config_format_set(&error_reason) < 0) {
477  goto parse_error;
478  }
479 
480  if (corosync_main_config_set ("logging", NULL, &error_reason) < 0) {
481  goto parse_error;
482  }
483 
484  /*
485  * we will need 2 of these to compensate for new logging
486  * config format
487  */
488 #ifdef LOGCONFIG_USE_ICMAP
489  iter = icmap_iter_init("logging.logger_subsys.");
490  while ((key_name = icmap_iter_next(iter, NULL, NULL)) != NULL) {
491 #else
492  cmap_iter_init(cmap_handle, "logging.logger_subsys.", &iter);
493  while ((cmap_iter_next(cmap_handle, iter, key_name, NULL, NULL)) == CS_OK) {
494 #endif
495  res = sscanf(key_name, "logging.logger_subsys.%[^.].%s", key_subsys, key_item);
496 
497  if (res != 2) {
498  continue ;
499  }
500 
501  if (strcmp(key_item, "subsys") != 0) {
502  continue ;
503  }
504 
505  snprintf(key_item, MAP_KEYNAME_MAXLEN, "logging.logger_subsys.%s", key_subsys);
506 
507  if (corosync_main_config_set(key_item, key_subsys, &error_reason) < 0) {
508  goto parse_error;
509  }
510  }
511 #ifdef LOGCONFIG_USE_ICMAP
512  icmap_iter_finalize(iter);
513 #else
515 #endif
516 
518  return 0;
519 
520 parse_error:
521  *error_string = error_reason;
522 
523  return (-1);
524 }
525 
526 #ifdef LOGCONFIG_USE_ICMAP
527 static void main_logging_notify(
528  int32_t event,
529  const char *key_name,
530  struct icmap_notify_value new_val,
531  struct icmap_notify_value old_val,
532  void *user_data)
533 #else
534 static void main_logging_notify(
535  cmap_handle_t cmap_handle_unused,
536  cmap_handle_t cmap_track_handle_unused,
537  int32_t event,
538  const char *key_name,
539  struct cmap_notify_value new_val,
540  struct cmap_notify_value old_val,
541  void *user_data)
542 #endif
543 {
544  const char *error_string;
545  static int reload_in_progress = 0;
546 
547  /* If a full reload happens then suspend updates for individual keys until
548  * it's all completed
549  */
550  if (strcmp(key_name, "config.reload_in_progress") == 0) {
551  if (*(uint8_t *)new_val.data == 1) {
552  reload_in_progress = 1;
553  } else {
554  reload_in_progress = 0;
555  }
556  }
557  if (reload_in_progress) {
558  log_printf(LOGSYS_LEVEL_DEBUG, "Ignoring key change, reload in progress. %s\n", key_name);
559  return;
560  }
561 
562  /*
563  * Reload the logsys configuration
564  */
565  if (logsys_format_set(NULL) == -1) {
566  fprintf (stderr, "Unable to setup logging format.\n");
567  }
568  corosync_main_config_read_logging(&error_string);
569 }
570 
571 #ifdef LOGCONFIG_USE_ICMAP
572 static void add_logsys_config_notification(void)
573 {
574  icmap_track_t icmap_track = NULL;
575 
576  icmap_track_add("logging.",
578  main_logging_notify,
579  NULL,
580  &icmap_track);
581 
582  icmap_track_add("config.reload_in_progress",
584  main_logging_notify,
585  NULL,
586  &icmap_track);
587 }
588 #else
589 static void add_logsys_config_notification(void)
590 {
591  cmap_track_handle_t cmap_track;
592 
593  cmap_track_add(cmap_handle, "logging.",
595  main_logging_notify,
596  NULL,
597  &cmap_track);
598 
599  cmap_track_add(cmap_handle, "config.reload_in_progress",
601  main_logging_notify,
602  NULL,
603  &cmap_track);
604 }
605 #endif
606 
608 #ifndef LOGCONFIG_USE_ICMAP
609  cmap_handle_t cmap_h,
610  const char *default_logfile,
611 #endif
612  const char **error_string)
613 {
614  const char *error_reason = error_string_response;
615 
616 #ifndef LOGCONFIG_USE_ICMAP
617  if (!cmap_h) {
618  error_reason = "No cmap handle";
619  return (-1);
620  }
621  if (!default_logfile) {
622  error_reason = "No default logfile";
623  return (-1);
624  }
625  cmap_handle = cmap_h;
626  main_logfile = default_logfile;
627 #endif
628 
629  if (corosync_main_config_read_logging(error_string) < 0) {
630  error_reason = *error_string;
631  goto parse_error;
632  }
633 
634  add_logsys_config_notification();
635 
636  return 0;
637 
638 parse_error:
639  snprintf (error_string_response, sizeof(error_string_response),
640  "parse error in config: %s.\n",
641  error_reason);
642 
643  *error_string = error_string_response;
644  return (-1);
645 }
#define LOGSYS_DEBUG_TRACE
Definition: logsys.h:92
#define CMAP_TRACK_DELETE
Definition: cmap.h:79
cs_error_t cmap_track_add(cmap_handle_t handle, const char *key_name, int32_t track_type, cmap_notify_fn_t notify_fn, void *user_data, cmap_track_handle_t *cmap_track_handle)
Definition: lib/cmap.c:933
const char * icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
Definition: icmap.c:1103
#define MAP_KEYNAME_MAXLEN
Definition: logconfig.c:49
#define CMAP_TRACK_MODIFY
Definition: cmap.h:80
uint32_t value
void icmap_iter_finalize(icmap_iter_t iter)
Definition: icmap.c:1124
cs_error_t cmap_iter_next(cmap_handle_t handle, cmap_iter_handle_t iter_handle, char key_name[], size_t *value_len, cmap_value_types_t *type)
Return next item in iterator iter.
Definition: lib/cmap.c:836
cmap_handle_t cmap_handle
Definition: sam.c:136
#define CMAP_TRACK_ADD
Definition: cmap.h:78
#define map_get_string(key_name, value)
Definition: logconfig.c:50
#define CMAP_KEYNAME_MAXLEN
Definition: cmap.h:69
int logsys_config_file_set(const char *subsys, const char **error_string, const char *file)
Definition: logsys.c:540
cs_error_t cmap_iter_init(cmap_handle_t handle, const char *prefix, cmap_iter_handle_t *cmap_iter_handle)
Initialize iterator with given prefix.
Definition: lib/cmap.c:781
int _logsys_subsys_create(const char *subsys, const char *filename)
Definition: logsys.c:436
#define LOGSYS_DEBUG_ON
Definition: logsys.h:91
#define log_printf(level, format, args...)
Definition: logsys.h:217
#define LOGSYS_MODE_OUTPUT_FILE
Definition: logsys.h:58
cs_error_t cmap_iter_finalize(cmap_handle_t handle, cmap_iter_handle_t iter_handle)
Finalize iterator.
Definition: lib/cmap.c:894
#define ICMAP_TRACK_DELETE
Definition: icmap.h:77
#define LOGSYS_LEVEL_WARNING
Definition: logsys.h:71
#define ICMAP_TRACK_MODIFY
Definition: icmap.h:78
int logsys_config_debug_set(const char *subsys, unsigned int value)
Definition: logsys.c:782
int logsys_config_syslog_facility_set(const char *subsys, unsigned int facility)
Definition: logsys.c:649
int logsys_config_mode_set(const char *subsys, unsigned int mode)
Definition: logsys.c:506
void * user_data
Definition: sam.c:126
char * logsys_format_get(void)
Definition: logsys.c:644
#define LOGSYS_MODE_OUTPUT_SYSLOG
Definition: logsys.h:60
#define ICMAP_TRACK_ADD
Definition: icmap.h:76
#define LOGSYS_DEBUG_OFF
Definition: logsys.h:90
int corosync_log_config_read(cmap_handle_t cmap_h, const char *default_logfile, const char **error_string)
Definition: logconfig.c:607
#define LOGSYS_LEVEL_DEBUG
Definition: logsys.h:74
uint64_t cmap_track_handle_t
Definition: cmap.h:64
int logsys_config_syslog_priority_set(const char *subsys, unsigned int priority)
Definition: logsys.c:656
#define CMAP_TRACK_PREFIX
Definition: cmap.h:86
void logsys_config_apply(void)
Definition: logsys.c:770
int logsys_priority_id_get(const char *name)
Definition: logsys.c:808
uint64_t cmap_iter_handle_t
Definition: cmap.h:59
const void * data
Definition: cmap.h:113
unsigned int logsys_config_mode_get(const char *subsys)
Definition: logsys.c:528
icmap_iter_t icmap_iter_init(const char *prefix)
Definition: icmap.c:1097
int logsys_config_logfile_priority_set(const char *subsys, unsigned int priority)
Definition: logsys.c:683
uint64_t cmap_handle_t
Definition: cmap.h:54
#define LOGSYS_MODE_OUTPUT_STDERR
Definition: logsys.h:59
qb_map_iter_t * icmap_iter_t
Definition: icmap.h:121
cs_error_t icmap_track_add(const char *key_name, int32_t track_type, icmap_notify_fn_t notify_fn, void *user_data, icmap_track_t *icmap_track)
Definition: icmap.c:1167
int logsys_format_set(const char *format)
Definition: logsys.c:586
#define ICMAP_TRACK_PREFIX
Definition: icmap.h:84