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;
59 static gboolean crm_tracing_enabled(
void);
62 crm_glib_handler(
const gchar * log_domain, GLogLevelFlags
flags,
const gchar * message,
65 int log_level = LOG_WARNING;
66 GLogLevelFlags msg_level = (
flags & G_LOG_LEVEL_MASK);
67 static struct qb_log_callsite *glib_cs = NULL;
69 if (glib_cs == NULL) {
70 glib_cs = qb_log_callsite_get(__func__, __FILE__,
"glib-handler",
75 case G_LOG_LEVEL_CRITICAL:
80 crm_abort(__FILE__, __func__, __LINE__, message, TRUE, TRUE);
84 case G_LOG_LEVEL_ERROR:
87 case G_LOG_LEVEL_MESSAGE:
88 log_level = LOG_NOTICE;
90 case G_LOG_LEVEL_INFO:
93 case G_LOG_LEVEL_DEBUG:
94 log_level = LOG_DEBUG;
97 case G_LOG_LEVEL_WARNING:
98 case G_LOG_FLAG_RECURSION:
99 case G_LOG_FLAG_FATAL:
100 case G_LOG_LEVEL_MASK:
101 log_level = LOG_WARNING;
105 do_crm_log(log_level,
"%s: %s", log_domain, message);
109 # define NAME_MAX 256 121 crm_trigger_blackbox(
int nsig)
123 if(nsig == SIGTRAP) {
133 if (glib_log_default != NULL) {
134 g_log_set_default_handler(glib_log_default, NULL);
153 set_format_string(
int method,
const char *daemon, pid_t use_pid,
154 const char *use_nodename)
156 if (method == QB_LOG_SYSLOG) {
158 crm_extended_logging(method, QB_FALSE);
159 qb_log_format_set(method,
"%g %p: %b");
167 if (method > QB_LOG_STDERR) {
169 offset += snprintf(fmt + offset,
FMT_MAX - offset,
171 use_nodename, daemon, (
unsigned long) use_pid);
175 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"(%%n");
176 if (crm_tracing_enabled()) {
178 offset += snprintf(fmt + offset,
FMT_MAX - offset,
"@%%f:%%l");
180 offset += snprintf(fmt + offset,
FMT_MAX - offset,
")");
183 offset += snprintf(fmt + offset,
FMT_MAX - offset,
" %%g\t%%p: %%b");
186 qb_log_format_set(method, fmt);
190 #define DEFAULT_LOG_FILE CRM_LOG_DIR "/pacemaker.log" 193 logfile_disabled(
const char *filename)
209 chown_logfile(
const char *filename,
int logfd)
217 if (fstat(logfd, &
st) < 0) {
226 crm_warn(
"Not changing '%s' ownership because user information " 230 if ((
st.st_gid == pcmk_gid)
231 && ((
st.st_mode & S_IRWXG) == (S_IRGRP|S_IWGRP))) {
234 if (fchown(logfd, pcmk_uid, pcmk_gid) < 0) {
235 crm_warn(
"Couldn't change '%s' ownership to user %s gid %d: %s",
243 chmod_logfile(
const char *filename,
int logfd)
246 mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
248 if (modestr != NULL) {
249 long filemode_l = strtol(modestr, NULL, 8);
251 if ((filemode_l != LONG_MIN) && (filemode_l != LONG_MAX)) {
252 filemode = (mode_t) filemode_l;
255 if ((filemode != 0) && (fchmod(logfd, filemode) < 0)) {
256 crm_warn(
"Couldn't change '%s' mode to %04o: %s",
257 filename, filemode, strerror(errno));
263 set_logfile_permissions(
const char *filename, FILE *logfile)
265 if (geteuid() == 0) {
266 int logfd = fileno(logfile);
267 int rc = chown_logfile(filename, logfd);
272 chmod_logfile(filename, logfd);
279 enable_logfile(
int fd)
281 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_TRUE);
283 qb_log_ctl(fd, QB_LOG_CONF_FILE_SYNC, 1);
286 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 288 qb_log_ctl(fd, QB_LOG_CONF_MAX_LINE_LEN, 800);
295 disable_logfile(
int fd)
297 qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_FALSE);
301 setenv_logfile(
const char *filename)
326 FILE *logfile = NULL;
327 bool is_default =
false;
329 static int default_fd = -1;
330 static bool have_logfile =
false;
333 if (filename == NULL) {
341 if (logfile_disabled(filename)) {
347 if (is_default && (default_fd >= 0)) {
352 logfile = fopen(filename,
"a");
353 if (logfile == NULL) {
355 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " uid=%u gid=%u",
356 filename, strerror(rc), geteuid(), getegid());
360 rc = set_logfile_permissions(filename, logfile);
362 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " permissions",
363 filename, strerror(rc));
370 fd = qb_log_file_open(filename);
372 crm_warn(
"Logging to '%s' is disabled: %s " CRM_XS " qb_log_file_open",
373 filename, strerror(-fd));
379 setenv_logfile(filename);
381 }
else if (default_fd >= 0) {
382 crm_notice(
"Switching logging to %s", filename);
383 disable_logfile(default_fd);
386 crm_notice(
"Additional logging available in %s", filename);
403 if (log_files == NULL) {
407 for (gchar **fname = log_files; *fname != NULL; fname++) {
411 out->
err(out,
"Logging to %s is disabled: %s",
417 static int blackbox_trigger = 0;
418 static volatile char *blackbox_file_prefix = NULL;
421 blackbox_logger(int32_t t,
struct qb_log_callsite *cs,
log_time_t timestamp,
424 if(cs && cs->priority < LOG_ERR) {
432 crm_control_blackbox(
int nsig,
bool enable)
436 if (blackbox_file_prefix == NULL) {
437 pid_t
pid = getpid();
442 (
unsigned long)
pid);
445 if (enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
446 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 5 * 1024 * 1024);
447 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
450 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
451 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_TRUE);
454 crm_notice(
"Initiated blackbox recorder: %s", blackbox_file_prefix);
465 blackbox_trigger = qb_log_custom_open(blackbox_logger, NULL, NULL, NULL);
466 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_ENABLED, QB_TRUE);
467 crm_trace(
"Trigger: %d is %d %d", blackbox_trigger,
468 qb_log_ctl(blackbox_trigger, QB_LOG_CONF_STATE_GET, 0), QB_LOG_STATE_ENABLED);
472 }
else if (!enable && qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET, 0) == QB_LOG_STATE_ENABLED) {
473 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
476 for (lpc = QB_LOG_BLACKBOX; lpc < QB_LOG_TARGET_MAX; lpc++) {
477 qb_log_ctl(lpc, QB_LOG_CONF_FILE_SYNC, QB_FALSE);
485 crm_control_blackbox(nsig, TRUE);
491 crm_control_blackbox(nsig, FALSE);
507 static volatile int counter = 1;
508 static volatile time_t last = 0;
511 time_t now = time(NULL);
513 if (blackbox_file_prefix == NULL) {
522 if (nsig == 0 && now == last) {
527 snprintf(buffer,
NAME_MAX,
"%s.%d", blackbox_file_prefix, counter++);
528 if (nsig == SIGTRAP) {
529 crm_notice(
"Blackbox dump requested, please see %s for contents", buffer);
533 "Problem detected at %s:%d (%s), please see %s for additional details",
534 cs->function, cs->lineno, cs->filename, buffer);
536 crm_notice(
"Problem detected, please see %s for additional details", buffer);
540 qb_log_blackbox_write_to_file(buffer);
545 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
546 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
554 qb_log_blackbox_write_to_file((
const char *)blackbox_file_prefix);
555 qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
562 crm_quark_to_string(uint32_t tag)
564 const char *text = g_quark_to_string(tag);
573 crm_log_filter_source(
int source,
const char *trace_files,
const char *trace_fns,
574 const char *trace_fmts,
const char *trace_tags,
const char *trace_blackbox,
575 struct qb_log_callsite *cs)
577 if (qb_log_ctl(source, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
581 qb_bit_set(cs->targets, source);
583 }
else if (source == blackbox_trigger && blackbox_trigger > 0) {
585 if (cs->priority <= LOG_ERR) {
586 qb_bit_set(cs->targets, source);
588 }
else if (trace_blackbox) {
591 if (strstr(trace_blackbox, key) != NULL) {
592 qb_bit_set(cs->targets, source);
597 }
else if (source == QB_LOG_SYSLOG) {
598 if (cs->priority <= crm_log_priority && cs->priority <=
crm_log_level) {
599 qb_bit_set(cs->targets, source);
603 qb_bit_set(cs->targets, source);
604 }
else if (trace_files && strstr(trace_files, cs->filename) != NULL) {
605 qb_bit_set(cs->targets, source);
606 }
else if (trace_fns && strstr(trace_fns, cs->function) != NULL) {
607 qb_bit_set(cs->targets, source);
608 }
else if (trace_fmts && strstr(trace_fmts, cs->format) != NULL) {
609 qb_bit_set(cs->targets, source);
610 }
else if (trace_tags
613 qb_bit_set(cs->targets, source);
617 #ifndef HAVE_STRCHRNUL 622 strchrnul(
const char *s,
int c)
624 while ((*s != c) && (*s !=
'\0')) {
632 crm_log_filter(
struct qb_log_callsite *cs)
635 static int need_init = 1;
636 static const char *trace_fns = NULL;
637 static const char *trace_tags = NULL;
638 static const char *trace_fmts = NULL;
639 static const char *trace_files = NULL;
640 static const char *trace_blackbox = NULL;
650 if (trace_tags != NULL) {
653 const char *offset = NULL;
654 const char *next = trace_tags;
658 next = strchrnul(offset,
',');
659 snprintf(token,
sizeof(token),
"%.*s", (
int)(next - offset), offset);
661 tag = g_quark_from_string(token);
662 crm_info(
"Created GQuark %u from token '%s' in '%s'", tag, token, trace_tags);
668 }
while (next != NULL && next[0] != 0);
673 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
674 crm_log_filter_source(lpc, trace_files, trace_fns, trace_fmts, trace_tags, trace_blackbox,
682 gboolean refilter = FALSE;
688 if (cs->priority != level) {
689 cs->priority = level;
693 if (cs->tags != tags) {
702 if (cs->targets == 0) {
711 static gboolean log = TRUE;
716 (
"Enabling callsites based on priority=%d, files=%s, functions=%s, formats=%s, tags=%s",
722 qb_log_filter_fn_set(crm_log_filter);
726 crm_tracing_enabled(
void)
736 crm_priority2int(
const char *
name)
738 struct syslog_names {
742 static struct syslog_names p_names[] = {
743 {
"emerg", LOG_EMERG},
744 {
"alert", LOG_ALERT},
747 {
"warning", LOG_WARNING},
748 {
"notice", LOG_NOTICE},
750 {
"debug", LOG_DEBUG},
755 for (lpc = 0;
name != NULL && p_names[lpc].name != NULL; lpc++) {
757 return p_names[lpc].priority;
760 return crm_log_priority;
781 set_identity(
const char *entity,
int argc,
char *
const *argv)
787 if (entity != NULL) {
790 }
else if ((argc > 0) && (argv != NULL)) {
791 char *
mutable = strdup(argv[0]);
792 char *modified = basename(
mutable);
794 if (strstr(modified,
"lt-") == modified) {
815 int32_t qb_facility = 0;
816 pid_t
pid = getpid();
817 const char *nodename =
"localhost";
818 static bool have_logging =
false;
829 crm_trace_nonlog = g_quark_from_static_string(
"Pacemaker non-logging tracepoint");
832 umask(S_IWGRP | S_IWOTH | S_IROTH);
835 glib_log_default = g_log_set_default_handler(crm_glib_handler, NULL);
838 g_log_set_always_fatal((GLogLevelFlags) 0);
843 set_identity(entity, argc, argv);
845 qb_facility = qb_log_facility2int(
"local0");
850 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
851 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN 853 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_MAX_LINE_LEN, 256);
855 if (
uname(memset(&res, 0,
sizeof(res))) == 0 && *res.nodename !=
'\0') {
856 nodename = res.nodename;
862 qb_log_tags_stringify_fn_set(crm_quark_to_string);
863 for (lpc = QB_LOG_SYSLOG; lpc < QB_LOG_TARGET_MAX; lpc++) {
864 qb_log_ctl(lpc, QB_LOG_CONF_THREADED, QB_FALSE);
865 #ifdef HAVE_qb_log_conf_QB_LOG_CONF_ELLIPSIS 867 qb_log_ctl(lpc, QB_LOG_CONF_ELLIPSIS, QB_TRUE);
880 setlocale(LC_ALL,
"");
894 crm_log_init(
const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr,
895 int argc,
char **argv, gboolean quiet)
897 const char *syslog_priority = NULL;
899 const char *f_copy = facility;
912 if (facility == NULL) {
926 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, qb_log_facility2int(facility));
936 if (syslog_priority) {
937 crm_log_priority = crm_priority2int(syslog_priority);
939 qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE,
"*",
944 qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
971 crm_trace(
"Quiet: %d, facility %s", quiet, f_copy);
981 const char *user = getenv(
"USER");
984 crm_trace(
"Not switching to corefile directory for %s", user);
991 struct passwd *pwent = getpwuid(user);
994 crm_perror(LOG_ERR,
"Cannot get name for uid: %d", user);
997 crm_trace(
"Don't change active directory for regular user: %s", pwent->pw_name);
1019 }
else if (!quiet) {
1044 if (enable && qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
1045 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
1048 }
else if (enable == FALSE) {
1049 qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
1065 if (qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0)
1066 != QB_LOG_STATE_ENABLED) {
1091 static bool logged =
false;
1092 gchar *arg_string = NULL;
1094 if ((argc == 0) || (argv == NULL) || logged) {
1098 arg_string = g_strjoinv(
" ", argv);
1107 const char *next = NULL;
1108 const char *offset = NULL;
1114 if (output == NULL) {
1118 output =
"-- empty --";
1124 next = strchrnul(offset,
'\n');
1126 (
int)(next - offset), offset);
1131 }
while (next != NULL && next[0] != 0);
1139 for (
int i = 0; i < verbosity; i++) {
1162 uint32_t tags, uint8_t level,
const char *text,
const xmlNode *xml)
1165 do_crm_log(level,
"%s%sNo data to dump as XML",
1166 pcmk__s(text,
""), pcmk__str_empty(text)?
"" :
" ");
1169 if (logger_out == NULL) {
1199 uint32_t tags, uint8_t level,
const xmlNode *xml)
1206 if (logger_out == NULL) {
1230 uint32_t tags, uint8_t level,
const xmlNode *patchset)
1232 if (patchset == NULL) {
1237 if (logger_out == NULL) {
1242 logger_out->
message(logger_out,
"xml-patchset", patchset);
1253 if (logger_out != NULL) {
#define CRM_CHECK(expr, failure_action)
void pcmk__set_config_error_handler(pcmk__config_error_func error_handler, void *error_context)
void pcmk__cli_init_logging(const char *name, unsigned int verbosity)
pcmk__config_warning_func pcmk__config_warning_handler
#define crm_notice(fmt, args...)
#define PCMK__ENV_LOGFILE
#define PCMK__ENV_TRACE_TAGS
gboolean mainloop_add_signal(int sig, void(*dispatch)(int sig))
void crm_enable_blackbox(int nsig)
#define PCMK__ENV_TRACE_FUNCTIONS
unsigned int get_crm_log_level(void)
void crm_disable_blackbox(int nsig)
void(*) typedef void(* pcmk__config_warning_func)(void *ctx, const char *msg,...) G_GNUC_PRINTF(2
bool pcmk__strcase_any_of(const char *s,...) G_GNUC_NULL_TERMINATED
int(* message)(pcmk__output_t *out, const char *message_id,...)
#define PCMK__ENV_TRACE_FORMATS
void * pcmk__config_error_context
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)
#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.
void pcmk__output_set_log_filter(pcmk__output_t *out, const char *file, const char *function, uint32_t line, uint32_t tags)
#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.
void pcmk__output_set_log_level(pcmk__output_t *out, uint8_t log_level)
void * pcmk__config_warning_context
#define PCMK__ENV_SERVICE
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 pcmk__log_xml_patchset_as(const char *file, const char *function, uint32_t line, uint32_t tags, uint8_t level, const xmlNode *patchset)
pcmk__config_error_func pcmk__config_error_handler
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)
void pcmk__set_env_option(const char *option, const char *value, bool compat)
Set or unset a Pacemaker environment variable option.
#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)
void pcmk__log_xml_changes_as(const char *file, const char *function, uint32_t line, uint32_t tags, uint8_t level, const xmlNode *xml)
#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.
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)
#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)
#define PCMK__ENV_LOGFILE_MODE
#define pcmk__str_copy(str)
void(* pcmk__config_error_func)(void *ctx, const char *msg,...) G_GNUC_PRINTF(2
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 PCMK__ENV_TRACE_BLACKBOX
void pcmk_log_xml_as(const char *file, const char *function, uint32_t line, uint32_t tags, uint8_t level, const char *text, const xmlNode *xml)
Log XML line-by-line in a formatted fashion.
#define PCMK__ENV_TRACE_FILES
#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)
void pcmk__set_config_warning_handler(pcmk__config_warning_func warning_handler, void *warning_context)
Include the closing tag of an XML element.
int pcmk__xml_show_changes(pcmk__output_t *out, const xmlNode *xml)
This structure contains everything that makes up a single output formatter.
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