void logging_init_defaults(int debug, int daemonize, const char *logfile) { int level = SYSLOGLEVEL; int32_t filetarget; if (debug) { level = LOG_DEBUG; } qb_log_init(PACKAGE "d", SYSLOGFACILITY, level); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE); if (debug) { qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_PRIORITY_BUMP, LOG_INFO - LOG_DEBUG); } /* * initialize stderr output only if we are not forking in background */ if (!daemonize) { qb_log_format_set(QB_LOG_STDERR, "%t %N [%p]: %b"); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION, "*", level); } filetarget = qb_log_file_open(logfile); qb_log_ctl(filetarget, QB_LOG_CONF_ENABLED, QB_TRUE); qb_log_format_set(filetarget, "%t %N [%p]: %b"); qb_log_filter_ctl(filetarget, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FUNCTION, "*", level); qb_log_thread_start(); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, QB_TRUE); qb_log_ctl(filetarget, QB_LOG_CONF_THREADED, QB_TRUE); }
gboolean crm_add_logfile(const char *filename) { struct stat parent; int fd = 0, rc = 0; FILE *logfile = NULL; char *parent_dir = NULL; char *filename_cp; static gboolean have_logfile = FALSE; if (filename == NULL && have_logfile == FALSE) { filename = "/var/log/pacemaker.log"; } if (filename == NULL) { return FALSE; /* Nothing to do */ } /* Check the parent directory */ filename_cp = strdup(filename); parent_dir = dirname(filename_cp); rc = stat(parent_dir, &parent); if (rc != 0) { crm_err("Directory '%s' does not exist: logging to '%s' is disabled", parent_dir, filename); free(filename_cp); return FALSE; } free(filename_cp); errno = 0; logfile = fopen(filename, "a"); if(logfile == NULL) { crm_err("%s (%d): Logging to '%s' as uid=%u, gid=%u is disabled", pcmk_strerror(errno), errno, filename, geteuid(), getegid()); return FALSE; } /* Check/Set permissions if we're root */ if (geteuid() == 0) { struct stat st; uid_t pcmk_uid = 0; gid_t pcmk_gid = 0; gboolean fix = FALSE; int logfd = fileno(logfile); rc = fstat(logfd, &st); if (rc < 0) { crm_perror(LOG_WARNING, "Cannot stat %s", filename); fclose(logfile); return FALSE; } if(crm_user_lookup(CRM_DAEMON_USER, &pcmk_uid, &pcmk_gid) == 0) { if (st.st_gid != pcmk_gid) { /* Wrong group */ fix = TRUE; } else if ((st.st_mode & S_IRWXG) != (S_IRGRP | S_IWGRP)) { /* Not read/writable by the correct group */ fix = TRUE; } } if (fix) { rc = fchown(logfd, pcmk_uid, pcmk_gid); if (rc < 0) { crm_warn("Cannot change the ownership of %s to user %s and gid %d", filename, CRM_DAEMON_USER, pcmk_gid); } rc = fchmod(logfd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP); if (rc < 0) { crm_warn("Cannot change the mode of %s to rw-rw----", filename); } fprintf(logfile, "Set r/w permissions for uid=%d, gid=%d on %s\n", pcmk_uid, pcmk_gid, filename); if (fflush(logfile) < 0 || fsync(logfd) < 0) { crm_err("Couldn't write out logfile: %s", filename); } } } /* Close and reopen with libqb */ fclose(logfile); fd = qb_log_file_open(filename); if (fd < 0) { crm_perror(LOG_WARNING, "Couldn't send additional logging to %s", filename); return FALSE; } crm_notice("Additional logging available in %s", filename); qb_log_ctl(fd, QB_LOG_CONF_ENABLED, QB_TRUE); /* qb_log_ctl(fd, QB_LOG_CONF_FILE_SYNC, 1); Turn on synchronous writes */ /* Enable callsites */ crm_update_callsites(); have_logfile = TRUE; return TRUE; }
int32_t main(int32_t argc, char *argv[]) { const char *options = "vhteobdf:"; int32_t opt; int32_t tracer; int32_t do_stderr = QB_FALSE; int32_t do_stdout = QB_FALSE; int32_t do_dump_blackbox = QB_FALSE; char *logfile = NULL; int32_t log_fd = -1; while ((opt = getopt(argc, argv, options)) != -1) { switch (opt) { case 'd': do_dump_blackbox = QB_TRUE; break; case 't': do_threaded = QB_TRUE; break; case 'e': do_stderr = QB_TRUE; break; case 'o': do_stdout = QB_TRUE; break; case 'b': do_blackbox = QB_TRUE; break; case 'f': logfile = optarg; break; case 'v': _log_priority++; break; case 'h': default: show_usage(argv[0]); exit(0); break; } } if (do_dump_blackbox) { qb_log_blackbox_print_from_file("simple-log.fdata"); exit(0); } signal(SIGSEGV, sigsegv_handler); qb_log_init("simple-log", LOG_USER, LOG_INFO); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_THREADED, do_threaded); qb_log_tags_stringify_fn_set(my_tags_stringify); if (do_stderr) { qb_log_filter_fn_set(m_filter); qb_log_format_set(QB_LOG_STDERR, "[%p] %4g: %f:%l %b"); qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE); tracer = qb_log_custom_open(trace_logger, NULL, NULL, NULL); qb_log_ctl(tracer, QB_LOG_CONF_ENABLED, QB_TRUE); qb_log_format_set(tracer, "%4g: %n() %b"); qb_log_filter_ctl2(tracer, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, LOG_TRACE, 200); } if (do_stdout) { qb_log_filter_ctl2(QB_LOG_STDOUT, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, LOG_ALERT, QB_MIN(LOG_DEBUG, _log_priority)); qb_log_format_set(QB_LOG_STDOUT, "[%p] %4g: %f:%l %b"); qb_log_ctl(QB_LOG_STDOUT, QB_LOG_CONF_ENABLED, QB_TRUE); } if (do_blackbox) { qb_log_filter_ctl(QB_LOG_BLACKBOX, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*", LOG_DEBUG); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 4096); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_THREADED, QB_FALSE); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE); } if (logfile) { log_fd = qb_log_file_open(logfile); qb_log_filter_ctl(log_fd, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, __FILE__, _log_priority); qb_log_format_set(log_fd, "[%N] %t %n() [%p] %b"); qb_log_ctl(log_fd, QB_LOG_CONF_THREADED, do_threaded); qb_log_ctl(log_fd, QB_LOG_CONF_ENABLED, QB_TRUE); } if (do_threaded) { qb_log_thread_start(); } qb_log(LOG_DEBUG, "hello"); qb_log(LOG_INFO, "this is an info"); qb_log(LOG_NOTICE, "hello - notice?"); func_one(); func_two(); qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE); qb_log(LOG_WARNING, "no syslog"); qb_log(LOG_ERR, "no syslog"); #if 0 // test blackbox logfile = NULL; logfile[5] = 'a'; #endif if (do_blackbox) { qb_log_blackbox_write_to_file("simple-log.fdata"); qb_log_blackbox_print_from_file("simple-log.fdata"); qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE); } qb_log_fini(); return 0; }
/* * we need a version that can work when somebody else is already * holding a config mutex lock or we will never get out of here */ static int logsys_config_file_set_unlocked ( int subsysid, const char **error_string, const char *file) { static char error_string_response[512]; int i; char file_format[128]; if (logsys_loggers[subsysid].target_id > 0) { int32_t f; for (f = 0; f < logsys_loggers[subsysid].file_idx; f++) { qb_log_filter_ctl(logsys_loggers[subsysid].target_id, QB_LOG_FILTER_REMOVE, QB_LOG_FILTER_FILE, logsys_loggers[subsysid].files[f], LOG_TRACE); } } logsys_loggers[subsysid].dirty = QB_TRUE; if (file == NULL) { return (0); } if (logsys_loggers[subsysid].target_id > 0 && logsys_loggers[subsysid].logfile != NULL && strcmp(file, logsys_loggers[subsysid].logfile) == 0) { return (0); } if (strlen(file) >= PATH_MAX) { snprintf (error_string_response, sizeof(error_string_response), "%s: logfile name exceed maximum system filename length", logsys_loggers[subsysid].subsys); *error_string = error_string_response; return (-1); } if (logsys_loggers[subsysid].logfile != NULL) { free(logsys_loggers[subsysid].logfile); logsys_loggers[subsysid].logfile = NULL; } logsys_loggers[subsysid].logfile = strdup(file); if (logsys_loggers[subsysid].logfile == NULL) { snprintf (error_string_response, sizeof(error_string_response), "Unable to allocate memory for logfile '%s'", file); *error_string = error_string_response; return (-1); } for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { if ((logsys_loggers[i].logfile != NULL) && (strcmp (logsys_loggers[i].logfile, file) == 0) && (i != subsysid)) { /* we have found another subsys with this config file * so add a filter */ logsys_loggers[subsysid].target_id = logsys_loggers[i].target_id; return (0); } } if (logsys_loggers[subsysid].target_id > 0) { int num_using_current = 0; for (i = 0; i <= LOGSYS_MAX_SUBSYS_COUNT; i++) { if (logsys_loggers[subsysid].target_id == logsys_loggers[i].target_id) { num_using_current++; } } if (num_using_current == 1) { /* no one else is using this close it */ qb_log_file_close(logsys_loggers[subsysid].target_id); } } logsys_loggers[subsysid].target_id = qb_log_file_open(file); if (logsys_loggers[subsysid].target_id < 0) { int err = -logsys_loggers[subsysid].target_id; char error_str[LOGSYS_MAX_PERROR_MSG_LEN]; const char *error_ptr; error_ptr = qb_strerror_r(err, error_str, sizeof(error_str)); free(logsys_loggers[subsysid].logfile); logsys_loggers[subsysid].logfile = NULL; snprintf (error_string_response, sizeof(error_string_response), "Can't open logfile '%s' for reason: %s (%d)", file, error_ptr, err); *error_string = error_string_response; return (-1); } logsys_file_format_get(file_format, 128); qb_log_format_set(logsys_loggers[subsysid].target_id, file_format); qb_log_ctl(logsys_loggers[subsysid].target_id, QB_LOG_CONF_ENABLED, (logsys_loggers[subsysid].mode & LOGSYS_MODE_OUTPUT_FILE)); if (logsys_thread_started) { qb_log_ctl(logsys_loggers[subsysid].target_id, QB_LOG_CONF_THREADED, QB_TRUE); } return (0); }