12 #include <sys/param.h> 13 #include <sys/types.h> 16 #include <sys/utsname.h> 31 #include <qb/qbdefs.h> 37 #ifdef QB_FEATURE_LOG_HIRES_TIMESTAMPS 38 #define TIMESTAMP_FORMAT_SPEC "%%T" 41 #define TIMESTAMP_FORMAT_SPEC "%%t" 49 static unsigned int crm_log_priority = LOG_NOTICE;
50 static GLogFunc glib_log_default = NULL;
52 static gboolean crm_tracing_enabled(
void);
55 crm_glib_handler(
const gchar * log_domain, GLogLevelFlags
flags,
const gchar * message,
58 int log_level = LOG_WARNING;
59 GLogLevelFlags msg_level = (
flags & G_LOG_LEVEL_MASK);
60 static struct qb_log_callsite *glib_cs = NULL;
62 if (glib_cs == NULL) {
63 glib_cs = qb_log_callsite_get(__func__, __FILE__,
"glib-handler",
69 case G_LOG_LEVEL_CRITICAL:
74 crm_abort(__FILE__, __func__, __LINE__, message, TRUE, TRUE);
78 case G_LOG_LEVEL_ERROR:
81 case G_LOG_LEVEL_MESSAGE:
82 log_level = LOG_NOTICE;
84 case G_LOG_LEVEL_INFO:
87 case G_LOG_LEVEL_DEBUG:
88 log_level = LOG_DEBUG;
91 case G_LOG_LEVEL_WARNING:
92 case G_LOG_FLAG_RECURSION:
93 case G_LOG_FLAG_FATAL:
94 case G_LOG_LEVEL_MASK:
95 log_level = LOG_WARNING;
99 do_crm_log(log_level,
"%s: %s", log_domain, message);
103 # define NAME_MAX 256 115 crm_trigger_blackbox(
int nsig)
117 if(nsig == SIGTRAP) {
127 if (glib_log_default != NULL) {
128 g_log_set_default_handler(glib_log_default, NULL);
135 set_format_string(
int method,
const char *
daemon)
137 if (method == QB_LOG_SYSLOG) {
139 crm_extended_logging(method, QB_FALSE);
140 qb_log_format_set(method,
"%g %p: %b");
148 if (method > QB_LOG_STDERR) {
150 const char *nodename =
"localhost";
152 if (
uname(&res) == 0) {
153 nodename = res.nodename;
157 offset += snprintf(fmt + offset,
FMT_MAX - offset,
159 nodename,
daemon, (
unsigned long) getpid());
163 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"(%%n");
164 if (crm_tracing_enabled()) {
166 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"@%%f:%%l");
168 offset += snprintf(fmt + offset,
FMT_MAX - offset,
")");
171 offset += snprintf(fmt + offset,
FMT_MAX - offset,
" %%g\t%%p: %%b");
174 qb_log_format_set(method, fmt);
178 #define DEFAULT_LOG_FILE CRM_LOG_DIR "/pacemaker.log" 181 logfile_disabled(
const char *filename)
197 chown_logfile(
const char *filename,
int logfd)
205 if (fstat(logfd, &
st) < 0) {
214 crm_warn(
"Not changing '%s' ownership because user information " 218 if ((
st.st_gid == pcmk_gid)
219 && ((
st.st_mode & S_IRWXG) == (S_IRGRP|S_IWGRP))) {
222 if (fchown(logfd, pcmk_uid, pcmk_gid) < 0) {
223 crm_warn(
"Couldn't change '%s' ownership to user %s gid %d: %s",
231 chmod_logfile(
const char *filename,
int logfd)
233 const char *modestr = getenv(
"PCMK_logfile_mode");
234 mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
236 if (modestr != NULL) {
237 long filemode_l = strtol(modestr, NULL, 8);
239 if ((filemode_l != LONG_MIN) && (filemode_l != LONG_MAX)) {
240 filemode = (mode_t) filemode_l;
243 if ((filemode != 0) && (fchmod(logfd, filemode) < 0)) {
244 crm_warn(
"Couldn't change '%s' mode to %04o: %s",
245 filename, filemode,
strerror(errno));
251 set_logfile_permissions(
const char *filename, FILE *logfile)
253 if (geteuid() == 0) {
254 int logfd = fileno(logfile);
255 int rc = chown_logfile(filename, logfd);
260 chmod_logfile(filename, logfd);
267 enable_logfile(
int fd)
269 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_TRUE);
271 qb_log_ctl(fd, QB_LOG_CONF_FILE_SYNC, 1);
274 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 276 qb_log_ctl(fd, QB_LOG_CONF_MAX_LINE_LEN, 800);
283 disable_logfile(
int fd)
285 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_FALSE);
289 setenv_logfile(
const char *filename)
314 FILE *logfile = NULL;
315 bool is_default =
false;
317 static int default_fd = -1;
318 static bool have_logfile =
false;
321 if (filename == NULL) {
329 if (logfile_disabled(filename)) {
335 if (is_default && (default_fd >= 0)) {
340 logfile = fopen(filename,
"a");
341 if (logfile == NULL) {
343 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " uid=%u gid=%u",
344 filename,
strerror(
rc), geteuid(), getegid());
348 rc = set_logfile_permissions(filename, logfile);
350 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " permissions",
358 fd = qb_log_file_open(filename);
360 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " qb_log_file_open",
367 setenv_logfile(filename);
369 }
else if (default_fd >= 0) {
370 crm_notice(
"Switching logging to %s", filename);
371 disable_logfile(default_fd);
374 crm_notice(
"Additional logging available in %s", filename);
380 static int blackbox_trigger = 0;
381 static volatile char *blackbox_file_prefix = NULL;
384 blackbox_logger(int32_t t,
struct qb_log_callsite *cs,
log_time_t timestamp,
387 if(cs && cs->priority < LOG_ERR) {
395 crm_control_blackbox(
int nsig,
bool enable)
399 if (blackbox_file_prefix == NULL) {
400 pid_t
pid = getpid();
405 (
unsigned long)
pid);
408 if (enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
409 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 5 * 1024 * 1024);
410 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
413 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
414 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_TRUE);
417 crm_notice(
"Initiated blackbox recorder: %s", blackbox_file_prefix);
428 blackbox_trigger = qb_log_custom_open(blackbox_logger, NULL, NULL, NULL);
429 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_ENABLED, QB_TRUE);
430 crm_trace(
"Trigger: %d is %d %d", blackbox_trigger,
431 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_STATE_GET, 0), QB_LOG_STATE_ENABLED);
435 }
else if (!enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) == QB_LOG_STATE_ENABLED) {
436 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
439 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
440 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_FALSE);
448 crm_control_blackbox(nsig, TRUE);
454 crm_control_blackbox(nsig, FALSE);
470 static volatile int counter = 1;
471 static volatile time_t last = 0;
474 time_t now = time(NULL);
476 if (blackbox_file_prefix == NULL) {
485 if (nsig == 0 && now == last) {
490 snprintf(buffer,
NAME_MAX,
"%s.%d", blackbox_file_prefix, counter++);
491 if (nsig == SIGTRAP) {
492 crm_notice(
"Blackbox dump requested, please see %s for contents", buffer);
496 "Problem detected at %s:%d (%s), please see %s for additional details",
497 cs->function, cs->lineno, cs->filename, buffer);
499 crm_notice(
"Problem detected, please see %s for additional details", buffer);
503 qb_log_blackbox_write_to_file(buffer);
508 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
509 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
517 qb_log_blackbox_write_to_file((
const char *)blackbox_file_prefix);
518 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
525 crm_quark_to_string(uint32_t tag)
527 const char *text = g_quark_to_string(tag);
536 crm_log_filter_source(
int source,
const char *trace_files,
const char *trace_fns,
537 const char *trace_fmts,
const char *trace_tags,
const char *trace_blackbox,
538 struct qb_log_callsite *cs)
540 if (qb_log_ctl(source, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
544 qb_bit_set(cs->targets, source);
546 }
else if (source == blackbox_trigger && blackbox_trigger > 0) {
548 if (cs->priority <= LOG_ERR) {
549 qb_bit_set(cs->targets, source);
551 }
else if (trace_blackbox) {
554 if (strstr(trace_blackbox, key) != NULL) {
555 qb_bit_set(cs->targets, source);
560 }
else if (source == QB_LOG_SYSLOG) {
561 if (cs->priority <= crm_log_priority && cs->priority <=
crm_log_level) {
562 qb_bit_set(cs->targets, source);
566 qb_bit_set(cs->targets, source);
567 }
else if (trace_files && strstr(trace_files, cs->filename) != NULL) {
568 qb_bit_set(cs->targets, source);
569 }
else if (trace_fns && strstr(trace_fns, cs->function) != NULL) {
570 qb_bit_set(cs->targets, source);
571 }
else if (trace_fmts && strstr(trace_fmts, cs->format) != NULL) {
572 qb_bit_set(cs->targets, source);
573 }
else if (trace_tags
576 qb_bit_set(cs->targets, source);
581 crm_log_filter(
struct qb_log_callsite *cs)
584 static int need_init = 1;
585 static const char *trace_fns = NULL;
586 static const char *trace_tags = NULL;
587 static const char *trace_fmts = NULL;
588 static const char *trace_files = NULL;
589 static const char *trace_blackbox = NULL;
593 trace_fns = getenv(
"PCMK_trace_functions");
594 trace_fmts = getenv(
"PCMK_trace_formats");
595 trace_tags = getenv(
"PCMK_trace_tags");
596 trace_files = getenv(
"PCMK_trace_files");
597 trace_blackbox = getenv(
"PCMK_trace_blackbox");
599 if (trace_tags != NULL) {
602 const char *offset = NULL;
603 const char *next = trace_tags;
608 snprintf(token,
sizeof(token),
"%.*s", (
int)(next - offset), offset);
610 tag = g_quark_from_string(token);
611 crm_info(
"Created GQuark %u from token '%s' in '%s'", tag, token, trace_tags);
617 }
while (next != NULL && next[0] != 0);
622 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
623 crm_log_filter_source(lpc, trace_files, trace_fns, trace_fmts, trace_tags, trace_blackbox,
631 gboolean refilter = FALSE;
637 if (cs->priority != level) {
638 cs->priority = level;
642 if (cs->tags != tags) {
651 if (cs->targets == 0) {
660 static gboolean log = TRUE;
665 (
"Enabling callsites based on priority=%d, files=%s, functions=%s, formats=%s, tags=%s",
666 crm_log_level, getenv(
"PCMK_trace_files"), getenv(
"PCMK_trace_functions"),
667 getenv(
"PCMK_trace_formats"), getenv(
"PCMK_trace_tags"));
669 qb_log_filter_fn_set(crm_log_filter);
673 crm_tracing_enabled(
void)
677 }
else if (getenv(
"PCMK_trace_files") || getenv(
"PCMK_trace_functions")
678 || getenv(
"PCMK_trace_formats") || getenv(
"PCMK_trace_tags")) {
685 crm_priority2int(
const char *
name)
687 struct syslog_names {
691 static struct syslog_names p_names[] = {
692 {
"emerg", LOG_EMERG},
693 {
"alert", LOG_ALERT},
696 {
"warning", LOG_WARNING},
697 {
"notice", LOG_NOTICE},
699 {
"debug", LOG_DEBUG},
704 for (lpc = 0;
name != NULL && p_names[lpc].name != NULL; lpc++) {
706 return p_names[lpc].priority;
709 return crm_log_priority;
714 set_identity(
const char *entity,
int argc,
char **argv)
720 if (entity != NULL) {
723 }
else if ((argc > 0) && (argv != NULL)) {
724 char *
mutable = strdup(argv[0]);
725 char *modified = basename(
mutable);
727 if (strstr(modified,
"lt-") == modified) {
748 int32_t qb_facility = 0;
750 static bool have_logging = FALSE;
752 if(have_logging == FALSE) {
758 crm_trace_nonlog = g_quark_from_static_string(
"Pacemaker non-logging tracepoint");
761 umask(S_IWGRP | S_IWOTH | S_IROTH);
764 glib_log_default = g_log_set_default_handler(crm_glib_handler, NULL);
767 g_log_set_always_fatal((GLogLevelFlags) 0);
772 set_identity(entity, argc, argv);
774 qb_facility = qb_log_facility2int(
"local0");
779 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
780 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 782 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_MAX_LINE_LEN, 256);
788 qb_log_tags_stringify_fn_set(crm_quark_to_string);
789 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
790 qb_log_ctl(lpc, QB_LOG_CONF_THREADED, QB_FALSE);
791 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_ELLIPSIS 793 qb_log_ctl(lpc, QB_LOG_CONF_ELLIPSIS, QB_TRUE);
802 int argc,
char **argv, gboolean quiet)
804 const char *syslog_priority = NULL;
806 const char *f_copy = facility;
819 if (facility == NULL) {
833 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, qb_log_facility2int(facility));
843 if (syslog_priority) {
844 crm_log_priority = crm_priority2int(syslog_priority);
846 qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE,
"*",
851 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
878 crm_trace(
"Quiet: %d, facility %s", quiet, f_copy);
888 const char *user = getenv(
"USER");
891 crm_trace(
"Not switching to corefile directory for %s", user);
899 struct passwd *pwent = getpwuid(user);
902 crm_perror(LOG_ERR,
"Cannot get name for uid: %d", user);
905 crm_trace(
"Don't change active directory for regular user: %s", pwent->pw_name);
907 }
else if (chdir(base) < 0) {
908 crm_perror(LOG_INFO,
"Cannot change active directory to %s", base);
911 crm_info(
"Changed active directory to %s", base);
916 snprintf(path, 512,
"%s-%lu",
crm_system_name, (
unsigned long) getpid());
919 crm_info(
"Changed active directory to %s/%s/%s", base, pwent->pw_name, path);
962 if (enable && qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
963 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
966 }
else if (enable == FALSE) {
967 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
983 if (qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0)
984 != QB_LOG_STATE_ENABLED) {
1009 static bool logged =
false;
1010 gchar *arg_string = NULL;
1012 if ((argc == 0) || (argv == NULL) || logged) {
1016 arg_string = g_strjoinv(
" ", argv);
1025 const char *next = NULL;
1026 const char *offset = NULL;
1032 if (output == NULL) {
1036 output =
"-- empty --";
1044 (
int)(next - offset), offset);
1049 }
while (next != NULL && next[0] != 0);
1057 for (
int i = 0; i < verbosity; i++) {
void pcmk__cli_init_logging(const char *name, unsigned int verbosity)
#define crm_notice(fmt, args...)
void crm_log_preinit(const char *entity, int argc, char **argv)
gboolean mainloop_add_signal(int sig, void(*dispatch)(int sig))
void crm_enable_blackbox(int nsig)
unsigned int get_crm_log_level(void)
void crm_disable_blackbox(int nsig)
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
int pcmk__add_logfile(const char *filename)
Add a file to be used as a Pacemaker detail log.
gboolean crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
void pcmk__set_env_option(const char *option, const char *value)
Set or unset a Pacemaker environment variable option.
#define CRM_LOG_ASSERT(expr)
void crm_write_blackbox(int nsig, struct qb_log_callsite *cs)
#define do_crm_log_alias(level, file, function, line, fmt, args...)
Log a message as if it came from a different code location.
int pcmk_daemon_user(uid_t *uid, gid_t *gid)
Get user and group IDs of pacemaker daemon user.
const char * pcmk_rc_str(int rc)
Get a user-friendly description of a return code.
char * strerror(int errnum)
const char * pcmk__env_option(const char *option)
Deprecated Pacemaker logging API.
Wrappers for and extensions to glib mainloop.
gboolean crm_add_logfile(const char *filename)
void crm_log_args(int argc, char **argv)
Log the command line (once)
#define crm_warn(fmt, args...)
gboolean crm_log_init(const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr, int argc, char **argv, gboolean quiet)
int daemon(int nochdir, int noclose)
#define crm_debug(fmt, args...)
unsigned int crm_log_level
void crm_enable_stderr(int enable)
#define crm_trace(fmt, args...)
#define do_crm_log(level, fmt, args...)
Log a message.
int setenv(const char *name, const char *value, int why)
char * crm_strdup_printf(char const *format,...) G_GNUC_PRINTF(1
unsigned int crm_trace_nonlog
sighandler_t crm_signal_handler(int sig, sighandler_t dispatch)
#define TIMESTAMP_FORMAT_SPEC
void crm_update_callsites(void)
int pcmk_legacy2rc(int legacy_rc)
void crm_log_output_fn(const char *file, const char *function, int line, int level, const char *prefix, const char *output)
#define crm_perror(level, fmt, args...)
Send a system error message to both the log and stderr.
gboolean crm_log_cli_init(const char *entity)
char * strchrnul(const char *s, int c_in)
unsigned int set_crm_log_level(unsigned int level)
void crm_abort(const char *file, const char *function, int line, const char *condition, gboolean do_core, gboolean do_fork)
void crm_bump_log_level(int argc, char **argv)
Make logging more verbose.
#define crm_info(fmt, args...)
void crm_log_deinit(void)
bool pcmk__env_option_enabled(const char *daemon, const char *option)