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" 50 static unsigned int crm_log_priority = LOG_NOTICE;
51 static GLogFunc glib_log_default = NULL;
54 static gboolean crm_tracing_enabled(
void);
57 crm_glib_handler(
const gchar * log_domain, GLogLevelFlags
flags,
const gchar * message,
60 int log_level = LOG_WARNING;
61 GLogLevelFlags msg_level = (
flags & G_LOG_LEVEL_MASK);
62 static struct qb_log_callsite *glib_cs = NULL;
64 if (glib_cs == NULL) {
65 glib_cs = qb_log_callsite_get(__func__, __FILE__,
"glib-handler",
70 case G_LOG_LEVEL_CRITICAL:
75 crm_abort(__FILE__, __func__, __LINE__, message, TRUE, TRUE);
79 case G_LOG_LEVEL_ERROR:
82 case G_LOG_LEVEL_MESSAGE:
83 log_level = LOG_NOTICE;
85 case G_LOG_LEVEL_INFO:
88 case G_LOG_LEVEL_DEBUG:
89 log_level = LOG_DEBUG;
92 case G_LOG_LEVEL_WARNING:
93 case G_LOG_FLAG_RECURSION:
94 case G_LOG_FLAG_FATAL:
95 case G_LOG_LEVEL_MASK:
96 log_level = LOG_WARNING;
100 do_crm_log(log_level,
"%s: %s", log_domain, message);
104 # define NAME_MAX 256 116 crm_trigger_blackbox(
int nsig)
118 if(nsig == SIGTRAP) {
128 if (glib_log_default != NULL) {
129 g_log_set_default_handler(glib_log_default, NULL);
148 set_format_string(
int method,
const char *
daemon, pid_t use_pid,
149 const char *use_nodename)
151 if (method == QB_LOG_SYSLOG) {
153 crm_extended_logging(method, QB_FALSE);
154 qb_log_format_set(method,
"%g %p: %b");
162 if (method > QB_LOG_STDERR) {
164 offset += snprintf(fmt + offset,
FMT_MAX - offset,
166 use_nodename,
daemon, (
unsigned long) use_pid);
170 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"(%%n");
171 if (crm_tracing_enabled()) {
173 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"@%%f:%%l");
175 offset += snprintf(fmt + offset,
FMT_MAX - offset,
")");
178 offset += snprintf(fmt + offset,
FMT_MAX - offset,
" %%g\t%%p: %%b");
181 qb_log_format_set(method, fmt);
185 #define DEFAULT_LOG_FILE CRM_LOG_DIR "/pacemaker.log" 188 logfile_disabled(
const char *filename)
204 chown_logfile(
const char *filename,
int logfd)
212 if (fstat(logfd, &
st) < 0) {
221 crm_warn(
"Not changing '%s' ownership because user information " 225 if ((
st.st_gid == pcmk_gid)
226 && ((
st.st_mode & S_IRWXG) == (S_IRGRP|S_IWGRP))) {
229 if (fchown(logfd, pcmk_uid, pcmk_gid) < 0) {
230 crm_warn(
"Couldn't change '%s' ownership to user %s gid %d: %s",
238 chmod_logfile(
const char *filename,
int logfd)
240 const char *modestr = getenv(
"PCMK_logfile_mode");
241 mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
243 if (modestr != NULL) {
244 long filemode_l = strtol(modestr, NULL, 8);
246 if ((filemode_l != LONG_MIN) && (filemode_l != LONG_MAX)) {
247 filemode = (mode_t) filemode_l;
250 if ((filemode != 0) && (fchmod(logfd, filemode) < 0)) {
251 crm_warn(
"Couldn't change '%s' mode to %04o: %s",
252 filename, filemode,
strerror(errno));
258 set_logfile_permissions(
const char *filename, FILE *logfile)
260 if (geteuid() == 0) {
261 int logfd = fileno(logfile);
262 int rc = chown_logfile(filename, logfd);
267 chmod_logfile(filename, logfd);
274 enable_logfile(
int fd)
276 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_TRUE);
278 qb_log_ctl(fd, QB_LOG_CONF_FILE_SYNC, 1);
281 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 283 qb_log_ctl(fd, QB_LOG_CONF_MAX_LINE_LEN, 800);
290 disable_logfile(
int fd)
292 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_FALSE);
296 setenv_logfile(
const char *filename)
321 FILE *logfile = NULL;
322 bool is_default =
false;
324 static int default_fd = -1;
325 static bool have_logfile =
false;
328 if (filename == NULL) {
336 if (logfile_disabled(filename)) {
342 if (is_default && (default_fd >= 0)) {
347 logfile = fopen(filename,
"a");
348 if (logfile == NULL) {
350 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " uid=%u gid=%u",
351 filename,
strerror(rc), geteuid(), getegid());
355 rc = set_logfile_permissions(filename, logfile);
357 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " permissions",
365 fd = qb_log_file_open(filename);
367 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " qb_log_file_open",
374 setenv_logfile(filename);
376 }
else if (default_fd >= 0) {
377 crm_notice(
"Switching logging to %s", filename);
378 disable_logfile(default_fd);
381 crm_notice(
"Additional logging available in %s", filename);
398 if (log_files == NULL) {
402 for (gchar **fname = log_files; *fname != NULL; fname++) {
406 out->
err(out,
"Logging to %s is disabled: %s",
412 static int blackbox_trigger = 0;
413 static volatile char *blackbox_file_prefix = NULL;
416 blackbox_logger(int32_t t,
struct qb_log_callsite *cs,
log_time_t timestamp,
419 if(cs && cs->priority < LOG_ERR) {
427 crm_control_blackbox(
int nsig,
bool enable)
431 if (blackbox_file_prefix == NULL) {
432 pid_t
pid = getpid();
437 (
unsigned long)
pid);
440 if (enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
441 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 5 * 1024 * 1024);
442 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
445 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
446 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_TRUE);
449 crm_notice(
"Initiated blackbox recorder: %s", blackbox_file_prefix);
460 blackbox_trigger = qb_log_custom_open(blackbox_logger, NULL, NULL, NULL);
461 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_ENABLED, QB_TRUE);
462 crm_trace(
"Trigger: %d is %d %d", blackbox_trigger,
463 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_STATE_GET, 0), QB_LOG_STATE_ENABLED);
467 }
else if (!enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) == QB_LOG_STATE_ENABLED) {
468 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
471 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
472 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_FALSE);
480 crm_control_blackbox(nsig, TRUE);
486 crm_control_blackbox(nsig, FALSE);
502 static volatile int counter = 1;
503 static volatile time_t last = 0;
506 time_t now = time(NULL);
508 if (blackbox_file_prefix == NULL) {
517 if (nsig == 0 && now == last) {
522 snprintf(buffer,
NAME_MAX,
"%s.%d", blackbox_file_prefix, counter++);
523 if (nsig == SIGTRAP) {
524 crm_notice(
"Blackbox dump requested, please see %s for contents", buffer);
528 "Problem detected at %s:%d (%s), please see %s for additional details",
529 cs->function, cs->lineno, cs->filename, buffer);
531 crm_notice(
"Problem detected, please see %s for additional details", buffer);
535 qb_log_blackbox_write_to_file(buffer);
540 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
541 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
549 qb_log_blackbox_write_to_file((
const char *)blackbox_file_prefix);
550 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
557 crm_quark_to_string(uint32_t tag)
559 const char *text = g_quark_to_string(tag);
568 crm_log_filter_source(
int source,
const char *trace_files,
const char *trace_fns,
569 const char *trace_fmts,
const char *trace_tags,
const char *trace_blackbox,
570 struct qb_log_callsite *cs)
572 if (qb_log_ctl(source, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
576 qb_bit_set(cs->targets, source);
578 }
else if (source == blackbox_trigger && blackbox_trigger > 0) {
580 if (cs->priority <= LOG_ERR) {
581 qb_bit_set(cs->targets, source);
583 }
else if (trace_blackbox) {
586 if (strstr(trace_blackbox, key) != NULL) {
587 qb_bit_set(cs->targets, source);
592 }
else if (source == QB_LOG_SYSLOG) {
593 if (cs->priority <= crm_log_priority && cs->priority <=
crm_log_level) {
594 qb_bit_set(cs->targets, source);
598 qb_bit_set(cs->targets, source);
599 }
else if (trace_files && strstr(trace_files, cs->filename) != NULL) {
600 qb_bit_set(cs->targets, source);
601 }
else if (trace_fns && strstr(trace_fns, cs->function) != NULL) {
602 qb_bit_set(cs->targets, source);
603 }
else if (trace_fmts && strstr(trace_fmts, cs->format) != NULL) {
604 qb_bit_set(cs->targets, source);
605 }
else if (trace_tags
608 qb_bit_set(cs->targets, source);
613 crm_log_filter(
struct qb_log_callsite *cs)
616 static int need_init = 1;
617 static const char *trace_fns = NULL;
618 static const char *trace_tags = NULL;
619 static const char *trace_fmts = NULL;
620 static const char *trace_files = NULL;
621 static const char *trace_blackbox = NULL;
625 trace_fns = getenv(
"PCMK_trace_functions");
626 trace_fmts = getenv(
"PCMK_trace_formats");
627 trace_tags = getenv(
"PCMK_trace_tags");
628 trace_files = getenv(
"PCMK_trace_files");
629 trace_blackbox = getenv(
"PCMK_trace_blackbox");
631 if (trace_tags != NULL) {
634 const char *offset = NULL;
635 const char *next = trace_tags;
640 snprintf(token,
sizeof(token),
"%.*s", (
int)(next - offset), offset);
642 tag = g_quark_from_string(token);
643 crm_info(
"Created GQuark %u from token '%s' in '%s'", tag, token, trace_tags);
649 }
while (next != NULL && next[0] != 0);
654 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
655 crm_log_filter_source(lpc, trace_files, trace_fns, trace_fmts, trace_tags, trace_blackbox,
663 gboolean refilter = FALSE;
669 if (cs->priority != level) {
670 cs->priority = level;
674 if (cs->tags != tags) {
683 if (cs->targets == 0) {
692 static gboolean log = TRUE;
697 (
"Enabling callsites based on priority=%d, files=%s, functions=%s, formats=%s, tags=%s",
698 crm_log_level, getenv(
"PCMK_trace_files"), getenv(
"PCMK_trace_functions"),
699 getenv(
"PCMK_trace_formats"), getenv(
"PCMK_trace_tags"));
701 qb_log_filter_fn_set(crm_log_filter);
705 crm_tracing_enabled(
void)
709 }
else if (getenv(
"PCMK_trace_files") || getenv(
"PCMK_trace_functions")
710 || getenv(
"PCMK_trace_formats") || getenv(
"PCMK_trace_tags")) {
717 crm_priority2int(
const char *
name)
719 struct syslog_names {
723 static struct syslog_names p_names[] = {
724 {
"emerg", LOG_EMERG},
725 {
"alert", LOG_ALERT},
728 {
"warning", LOG_WARNING},
729 {
"notice", LOG_NOTICE},
731 {
"debug", LOG_DEBUG},
736 for (lpc = 0;
name != NULL && p_names[lpc].name != NULL; lpc++) {
738 return p_names[lpc].priority;
741 return crm_log_priority;
762 set_identity(
const char *entity,
int argc,
char *
const *argv)
768 if (entity != NULL) {
771 }
else if ((argc > 0) && (argv != NULL)) {
772 char *
mutable = strdup(argv[0]);
773 char *modified = basename(
mutable);
775 if (strstr(modified,
"lt-") == modified) {
797 int32_t qb_facility = 0;
798 pid_t
pid = getpid();
799 const char *nodename =
"localhost";
800 static bool have_logging =
false;
811 crm_trace_nonlog = g_quark_from_static_string(
"Pacemaker non-logging tracepoint");
814 umask(S_IWGRP | S_IWOTH | S_IROTH);
817 glib_log_default = g_log_set_default_handler(crm_glib_handler, NULL);
820 g_log_set_always_fatal((GLogLevelFlags) 0);
825 set_identity(entity, argc, argv);
827 qb_facility = qb_log_facility2int(
"local0");
832 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
833 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 835 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_MAX_LINE_LEN, 256);
837 if (
uname(memset(&res, 0,
sizeof(res))) == 0 && *res.nodename !=
'\0') {
838 nodename = res.nodename;
844 qb_log_tags_stringify_fn_set(crm_quark_to_string);
845 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
846 qb_log_ctl(lpc, QB_LOG_CONF_THREADED, QB_FALSE);
847 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_ELLIPSIS 849 qb_log_ctl(lpc, QB_LOG_CONF_ELLIPSIS, QB_TRUE);
862 setlocale(LC_ALL,
"");
877 int argc,
char **argv, gboolean quiet)
879 const char *syslog_priority = NULL;
881 const char *f_copy = facility;
894 if (facility == NULL) {
908 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, qb_log_facility2int(facility));
918 if (syslog_priority) {
919 crm_log_priority = crm_priority2int(syslog_priority);
921 qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE,
"*",
926 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
953 crm_trace(
"Quiet: %d, facility %s", quiet, f_copy);
963 const char *user = getenv(
"USER");
966 crm_trace(
"Not switching to corefile directory for %s", user);
973 struct passwd *pwent = getpwuid(user);
976 crm_perror(LOG_ERR,
"Cannot get name for uid: %d", user);
979 crm_trace(
"Don't change active directory for regular user: %s", pwent->pw_name);
1001 }
else if (!quiet) {
1026 if (enable && qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
1027 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
1030 }
else if (enable == FALSE) {
1031 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
1047 if (qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0)
1048 != QB_LOG_STATE_ENABLED) {
1073 static bool logged =
false;
1074 gchar *arg_string = NULL;
1076 if ((argc == 0) || (argv == NULL) || logged) {
1080 arg_string = g_strjoinv(
" ", argv);
1089 const char *next = NULL;
1090 const char *offset = NULL;
1096 if (output == NULL) {
1100 output =
"-- empty --";
1108 (
int)(next - offset), offset);
1113 }
while (next != NULL && next[0] != 0);
1121 for (
int i = 0; i < verbosity; i++) {
1142 do_crm_log(level,
"%s%sNo data to dump as XML",
1143 pcmk__s(text,
""), pcmk__str_empty(text)?
"" :
" ");
1146 if (logger_out == NULL) {
1166 if (logger_out != NULL) {
#define CRM_CHECK(expr, failure_action)
void pcmk__cli_init_logging(const char *name, unsigned int verbosity)
#define crm_notice(fmt, args...)
#define PCMK__ENV_LOGFILE
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.
Include the opening tag of an XML element, and include XML comments.
void crm_xml_init(void)
Initialize the CRM XML subsystem.
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)
#define bindtextdomain(Domainname, Dirname)
#define do_crm_log_alias(level, file, function, line, fmt, args...)
Log a message as if it came from a different code location.
#define bind_textdomain_codeset(Domainname, Codeset)
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)
void pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level)
const char * pcmk__env_option(const char *option)
Deprecated Pacemaker logging API.
Wrappers for and extensions to glib mainloop.
Include indentation and newlines.
gboolean crm_add_logfile(const char *filename)
void crm_log_preinit(const char *entity, int argc, char *const *argv)
Initializes the logging system and defaults to the least verbose output level.
void pcmk__free_common_logger(void)
void crm_log_args(int argc, char **argv)
Log the command line (once)
#define textdomain(Domainname)
#define PCMK__ENV_BLACKBOX
#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...)
Include the children of an XML element.
unsigned int crm_log_level
void crm_enable_stderr(int enable)
char * pcmk__our_nodename
Node name of the local node.
#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
int(*) int(*) void(* err)(pcmk__output_t *out, const char *format,...) G_GNUC_PRINTF(2
unsigned int crm_trace_nonlog
sighandler_t crm_signal_handler(int sig, sighandler_t dispatch)
void pcmk_log_xml_impl(uint8_t level, const char *text, const xmlNode *xml)
Log XML line-by-line in a formatted fashion.
#define TIMESTAMP_FORMAT_SPEC
void(* finish)(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy_dest)
void crm_update_callsites(void)
#define PCMK__ENV_LOGPRIORITY
int pcmk_legacy2rc(int legacy_rc)
void crm_write_blackbox(int nsig, const struct qb_log_callsite *cs)
void pcmk__output_free(pcmk__output_t *out)
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)
int pcmk__xml_show(pcmk__output_t *out, const char *prefix, const xmlNode *data, int depth, uint32_t options)
Include the closing tag of an XML element.
This structure contains everything that makes up a single output formatter.
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 pcmk__add_logfiles(gchar **log_files, pcmk__output_t *out)
Add multiple additional log files.
void crm_bump_log_level(int argc, char **argv)
Make logging more verbose.
#define crm_info(fmt, args...)
void crm_log_deinit(void)
int pcmk__log_output_new(pcmk__output_t **out)
bool pcmk__env_option_enabled(const char *daemon, const char *option)
#define PCMK__ENV_LOGFACILITY