Coverage Report

Created: 2026-04-04 06:09

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/pacemaker/lib/common/logging.c
Line
Count
Source
1
/*
2
 * Copyright 2004-2026 the Pacemaker project contributors
3
 *
4
 * The version control history for this file may have further details.
5
 *
6
 * This source code is licensed under the GNU Lesser General Public License
7
 * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
8
 */
9
10
#include <crm_internal.h>
11
12
#include <errno.h>                  // errno
13
#include <fcntl.h>                  // open, O_CREAT, O_RDWR, S_*
14
#include <libgen.h>                 // basename
15
#include <signal.h>                 // raise, SIG*
16
#include <stdbool.h>
17
#include <stddef.h>                 // NULL
18
#include <stdint.h>                 // int32_t, uint8_t, uint32_t
19
#include <stdlib.h>                 // free, getenv, strtol
20
#include <string.h>                 // strdup, strerror, strstr
21
#include <syslog.h>                 // LOG_*, syslog
22
#include <sys/stat.h>               // S_*, stat
23
#include <sys/types.h>              // gid_t, mode_t, pid_t, uid_t
24
#include <sys/utsname.h>            // uname, utsname
25
#include <time.h>                   // time, time_t, timespec
26
#include <unistd.h>                 // chdir, close, fchown, geteuid, getpid
27
28
#include <glib.h>                   // g_*, G_*, gboolean, gchar, etc.
29
#include <libxml/tree.h>            // xmlNode
30
#include <qb/qbdefs.h>              // qb_bit_set, QB_FALSE, QB_TRUE
31
#include <qb/qblog.h>               // LOG_TRACE, qb_log_*
32
33
#include <crm_config.h>             // CRM_DAEMON_USER, CRM_*_DIR
34
#include <crm/common/internal.h>    // pcmk__env_*, pcmk__output_*, etc.
35
#include <crm/common/logging.h>     // do_crm_log, CRM_CHECK, etc.
36
#include <crm/common/mainloop.h>    // crm_signal_handler, mainloop_add_signal
37
#include <crm/common/options.h>     // PCMK_VALUE_NONE
38
#include <crm/common/results.h>     // crm_abort, pcmk_rc_*
39
#include <crm/crm.h>                // crm_system_name
40
41
#include "crmcommon_private.h"      // pcmk__add_logfile
42
43
// Use high-resolution (millisecond) timestamps if libqb supports them
44
#ifdef QB_FEATURE_LOG_HIRES_TIMESTAMPS
45
0
#define TIMESTAMP_FORMAT_SPEC "%%T"
46
typedef struct timespec *log_time_t;
47
#else
48
#define TIMESTAMP_FORMAT_SPEC "%%t"
49
typedef time_t log_time_t;
50
#endif
51
52
unsigned int crm_log_level = LOG_INFO;
53
unsigned int crm_trace_nonlog = 0;
54
bool pcmk__is_daemon = false;
55
56
static int blackbox_trigger = 0;
57
static char *blackbox_file_prefix = NULL;
58
59
static gchar **trace_blackbox = NULL;
60
static gchar **trace_files = NULL;
61
static gchar **trace_formats = NULL;
62
static gchar **trace_functions = NULL;
63
static bool tracing_enabled = false;
64
65
static unsigned int crm_log_priority = LOG_NOTICE;
66
static pcmk__output_t *logger_out = NULL;
67
68
pcmk__config_error_func pcmk__config_error_handler = NULL;
69
pcmk__config_warning_func pcmk__config_warning_handler = NULL;
70
void *pcmk__config_error_context = NULL;
71
void *pcmk__config_warning_context = NULL;
72
73
/*!
74
 * \internal
75
 * \brief Mapping of a GLib log domain string to its log handler ID
76
 */
77
struct log_handler_id {
78
    //! Log domain
79
    const char *log_domain;
80
81
    /*!
82
     * Log handler function ID. GLib does not specify the meaning of 0, but
83
     * based on the implementation, a valid ID is always positive unless we set
84
     * UINT_MAX handlers.
85
     */
86
    guint handler_id;
87
};
88
89
// Log domains that we care about, and their handler function IDs once set
90
static struct log_handler_id log_handler_ids[] = {
91
    { G_LOG_DOMAIN, 0 },
92
    { "GLib", 0 },
93
    { "GLib-GIO", 0 },
94
    { "GModule", 0 },
95
    { "GThread", 0 },
96
};
97
98
/*!
99
 * \internal
100
 * \brief Convert a GLib log level to a syslog log level
101
 *
102
 * \param[in] log_level  GLib log level
103
 *
104
 * \return The syslog level corresponding to \p log_level
105
 */
106
static uint8_t
107
log_level_from_glib(GLogLevelFlags log_level)
108
0
{
109
0
    switch (log_level & G_LOG_LEVEL_MASK) {
110
0
        case G_LOG_LEVEL_CRITICAL:
111
0
            return LOG_CRIT;
112
113
0
        case G_LOG_LEVEL_ERROR:
114
0
            return LOG_ERR;
115
116
0
        case G_LOG_LEVEL_MESSAGE:
117
0
            return LOG_NOTICE;
118
119
0
        case G_LOG_LEVEL_INFO:
120
0
            return LOG_INFO;
121
122
0
        case G_LOG_LEVEL_DEBUG:
123
0
            return LOG_DEBUG;
124
125
0
        case G_LOG_LEVEL_WARNING:
126
0
            return LOG_WARNING;
127
128
0
        default:
129
            // Default to LOG_NOTICE for any new or custom GLib log levels
130
0
            return LOG_NOTICE;
131
0
    }
132
0
}
133
134
/*!
135
 * \internal
136
 * \brief Handle a log message from GLib
137
 *
138
 * \param[in] log_domain  Log domain of the message
139
 * \param[in] log_level   Log level of the message (including fatal and
140
 *                        recursion flags)
141
 * \param[in] message     Message to process
142
 * \param[in] user_data   Ignored
143
 */
144
static void
145
handle_glib_message(const gchar *log_domain, GLogLevelFlags log_level,
146
                    const gchar *message, gpointer user_data)
147
148
0
{
149
0
    uint8_t syslog_level = log_level_from_glib(log_level);
150
151
0
    if (syslog_level == LOG_CRIT) {
152
0
        static struct qb_log_callsite *glib_cs = NULL;
153
154
0
        if (glib_cs == NULL) {
155
0
            glib_cs = qb_log_callsite_get(__func__, __FILE__, "glib-handler",
156
0
                                          LOG_DEBUG, __LINE__,
157
0
                                          crm_trace_nonlog);
158
0
        }
159
160
0
        if (!crm_is_callsite_active(glib_cs, LOG_DEBUG, crm_trace_nonlog)) {
161
            // Dump core
162
0
            crm_abort(__FILE__, __func__, __LINE__, message, true, true);
163
0
        }
164
0
    }
165
166
0
    do_crm_log(syslog_level, "%s: %s", log_domain, message);
167
0
}
168
169
/*!
170
 * \internal
171
 * \brief Set \c handle_glib_message() as the handler for each GLib log domain
172
 *
173
 * The handler will be set for all log levels, including fatal and recursive
174
 * messages, for each GLib log domain that we care about.
175
 */
176
static void
177
set_glib_log_handlers(void)
178
0
{
179
0
    for (int i = 0; i < PCMK__NELEM(log_handler_ids); i++) {
180
0
        struct log_handler_id *entry = &log_handler_ids[i];
181
182
0
        entry->handler_id = g_log_set_handler(entry->log_domain,
183
0
                                              G_LOG_LEVEL_MASK
184
0
                                              |G_LOG_FLAG_FATAL
185
0
                                              |G_LOG_FLAG_RECURSION,
186
0
                                              handle_glib_message, NULL);
187
0
    }
188
0
}
189
190
/*!
191
 * \internal
192
 * \brief Remove the handler for each GLib log domain that we care about
193
 */
194
static void
195
remove_glib_log_handlers(void)
196
0
{
197
0
    for (int i = 0; i < PCMK__NELEM(log_handler_ids); i++) {
198
0
        struct log_handler_id *entry = &log_handler_ids[i];
199
200
0
        if (entry->handler_id == 0) {
201
0
            continue;
202
0
        }
203
204
0
        g_log_remove_handler(entry->log_domain, entry->handler_id);
205
0
        entry->handler_id = 0;
206
0
    }
207
0
}
208
209
/*!
210
 * \internal
211
 * \brief Set the log format string based on the given target
212
 *
213
 * \param[in] target     Log target (detail level)
214
 * \param[in] pid        PID of current process
215
 * \param[in] node_name  Local node's name (for use in log output)
216
 */
217
static void
218
set_format_string(int target, pid_t pid, const char *node_name)
219
0
{
220
    /* Format specifiers used here:
221
     * - %b: message
222
     * - %f: file name
223
     * - %g: tag, or "" if none
224
     * - %l: line number
225
     * - %n: function name
226
     * - %p: priority (for example, "error" or "debug")
227
     * - %t: timestamp at seconds resolution
228
     * - %T: timestamp at milliseconds resolution
229
     */
230
0
    GString *fmt = NULL;
231
232
0
    if (target == QB_LOG_SYSLOG) {
233
        // The system log gets a simplified, user-friendly format
234
0
        qb_log_ctl(target, QB_LOG_CONF_EXTENDED, QB_FALSE);
235
0
        qb_log_format_set(target, "%g %p: %b");
236
0
        return;
237
0
    }
238
239
    // Everything else gets more detail, for advanced troubleshooting
240
0
    fmt = g_string_sized_new(256);
241
242
0
    if (target > QB_LOG_STDERR) {
243
        // If logging to a file, prefix with timestamp, node, system, and PID
244
0
        g_string_append_printf(fmt, TIMESTAMP_FORMAT_SPEC " %s %-20s[%lld] ",
245
0
                               node_name, crm_system_name, (long long) pid);
246
0
    }
247
248
    // Add function name (in parentheses)
249
0
    g_string_append(fmt, "(%n");
250
251
0
    if (tracing_enabled) {
252
        // When tracing, add file and line number
253
0
        g_string_append(fmt, "@%f:%l");
254
0
    }
255
256
0
    g_string_append_c(fmt, ')');
257
258
    // Add tag (if any), priority, and actual message
259
0
    g_string_append(fmt, " %g\t%p: %b");
260
261
0
    qb_log_format_set(target, fmt->str);
262
0
    g_string_free(fmt, TRUE);
263
0
}
264
265
0
#define DEFAULT_LOG_FILE CRM_LOG_DIR "/pacemaker.log"
266
267
/*!
268
 * \internal
269
 * \brief Ensure the daemon group owns the log file or has read/write access
270
 *
271
 * If the daemon group (\c haclient by default) doesn't own the log file or
272
 * can't read and write it, change the log file's ownership to the daemon user
273
 * and group.
274
 *
275
 * \param[in] filename  Log file name (for logging only)
276
 * \param[in] logfd     Log file descriptor
277
 *
278
 * \return Standard Pacemaker return code
279
 *
280
 * \todo Should we be less aggressive in changing ownership? The daemon group
281
 *       may have read/write permissions via file ACLs, or the daemon user may
282
 *       have permissions via user ownership, secondary group membership, or
283
 *       file ACLs. Also, even after changing group ownership, the daemon group
284
 *       may not have read/write permissions. Even if it does, we may unset them
285
 *       when applying PCMK__ENV_LOGFILE_MODE in \c chmod_logfile().
286
 */
287
static int
288
chown_logfile(const char *filename, int logfd)
289
0
{
290
0
    uid_t pcmk_uid = 0;
291
0
    gid_t pcmk_gid = 0;
292
0
    struct stat st;
293
0
    int rc = pcmk_rc_ok;
294
295
    // Get the log file's current ownership and permissions
296
0
    if (fstat(logfd, &st) < 0) {
297
0
        rc = errno;
298
0
        pcmk__warn("Logging to '%s' is disabled because fstat() failed: %s",
299
0
                   filename, strerror(rc));
300
0
        return rc;
301
0
    }
302
303
    // Any other errors don't prevent file from being used as log
304
305
0
    rc = pcmk__daemon_user(&pcmk_uid, &pcmk_gid);
306
0
    if (rc != pcmk_rc_ok) {
307
0
        pcmk__warn("Not changing '%s' ownership because user information "
308
0
                   "unavailable: %s",
309
0
                   filename, pcmk_rc_str(rc));
310
0
        return pcmk_rc_ok;
311
0
    }
312
313
0
    if ((st.st_gid == pcmk_gid)
314
0
        && ((st.st_mode & S_IRWXG) == (S_IRGRP|S_IWGRP))) {
315
316
        // Daemon group already owns the log file and has read/write permissions
317
0
        return pcmk_rc_ok;
318
0
    }
319
320
0
    if (fchown(logfd, pcmk_uid, pcmk_gid) < 0) {
321
0
        pcmk__warn("Couldn't change '%s' ownership to user %s gid %d: %s",
322
0
                   filename, CRM_DAEMON_USER, pcmk_gid, strerror(errno));
323
0
    }
324
325
0
    return pcmk_rc_ok;
326
0
}
327
328
/*!
329
 * \internal
330
 * \brief Set log file permissions using environment variable or default value
331
 *
332
 * If the \c PCMK__ENV_LOGFILE_MODE environment variable is set to a valid
333
 * value, this function sets the log file mode to that value. Otherwise, it sets
334
 * the log file mode to 0660.
335
 *
336
 * An error causes a warning to be logged but is otherwise ignored.
337
 *
338
 * \param[in] filename  Log file name (for logging only)
339
 * \param[in] logfd     Log file descriptor
340
 */
341
static void
342
chmod_logfile(const char *filename, int logfd)
343
0
{
344
0
    const char *modestr = pcmk__env_option(PCMK__ENV_LOGFILE_MODE);
345
0
    mode_t filemode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP;
346
347
0
    errno = 0;
348
0
    if (modestr != NULL) {
349
0
        char *end = NULL;
350
0
        const long filemode_l = strtol(modestr, &end, 8);
351
0
        const mode_t mask = S_ISUID|S_ISGID|S_ISVTX|S_IRWXU|S_IRWXG|S_IRWXO;
352
353
0
        if ((errno != 0) || (end == modestr) || !pcmk__str_empty(end)
354
0
            || (((mode_t) filemode_l & ~mask) != 0)) {
355
356
0
            pcmk__warn("Setting '%s' mode to %04o because "
357
0
                       "PCMK_" PCMK__ENV_LOGFILE_MODE " '%s' is invalid",
358
0
                       filename, filemode, modestr);
359
360
0
        } else {
361
0
            filemode = (mode_t) filemode_l;
362
0
        }
363
0
    }
364
365
0
    if (fchmod(logfd, filemode) < 0) {
366
0
        pcmk__warn("Couldn't change '%s' mode to %04o: %s", filename, filemode,
367
0
                   strerror(errno));
368
0
    }
369
0
}
370
371
/*!
372
 * \internal
373
 * \brief If we're root, ensure a log file has correct permissions and ownership
374
 *
375
 * \param[in] filename  Log file name
376
 *
377
 * \return Standard Pacemaker return code
378
 */
379
static int
380
set_logfile_permissions(const char *filename)
381
0
{
382
0
    int fd = 0;
383
0
    int rc = pcmk_rc_ok;
384
385
0
    if (geteuid() != 0) {
386
0
        goto done;
387
0
    }
388
389
0
    fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
390
0
    if (fd < 0) {
391
0
        rc = errno;
392
0
        pcmk__warn("Logging to '%s' is disabled because open() failed as root: "
393
0
                   "%s", filename, strerror(rc));
394
0
        goto done;
395
0
    }
396
397
0
    rc = chown_logfile(filename, fd);
398
0
    if (rc != pcmk_rc_ok) {
399
0
        goto done;
400
0
    }
401
402
0
    chmod_logfile(filename, fd);
403
404
0
done:
405
0
    if (fd >= 0) {
406
0
        close(fd);
407
0
    }
408
0
    return rc;
409
0
}
410
411
/*!
412
 * \internal
413
 * \brief Enable a libqb log target, extend max line length, and apply filter
414
 *
415
 * \param[in] target  Log target (either an <tt>enum qb_log_target_slot</tt> or
416
 *                    the return value of a \c qb_log_file_open() call)
417
 */
418
static void
419
enable_logfile(int32_t target)
420
0
{
421
0
    qb_log_ctl(target, QB_LOG_CONF_ENABLED, QB_TRUE);
422
423
0
#ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN
424
    // Longer than default, for logging long XML lines
425
0
    qb_log_ctl(target, QB_LOG_CONF_MAX_LINE_LEN, 800);
426
0
#endif
427
428
0
    crm_update_callsites();
429
0
}
430
431
/*!
432
 * \internal
433
 * \brief Disable a libqb log target
434
 *
435
 * \param[in] target  Log target (either an <tt>enum qb_log_target_slot</tt> or
436
 *                    the return value of a \c qb_log_file_open() call)
437
 */
438
static inline void
439
disable_logfile(int32_t target)
440
0
{
441
0
    qb_log_ctl(target, QB_LOG_CONF_ENABLED, QB_FALSE);
442
0
}
443
444
static void
445
setenv_logfile(const char *filename)
446
0
{
447
    // Some resource agents will log only if environment variable is set
448
0
    if (pcmk__env_option(PCMK__ENV_LOGFILE) == NULL) {
449
0
        pcmk__set_env_option(PCMK__ENV_LOGFILE, filename, true);
450
0
    }
451
0
}
452
453
/*!
454
 * \internal
455
 * \brief Add a file to be used as a Pacemaker detail log
456
 *
457
 * \param[in] filename  Name of log file to use. \c NULL adds the default log
458
 *                      file if no log file is enabled yet. \c PCMK_VALUE_NONE
459
 *                      or \c "/dev/null" causes this function to do nothing.
460
 *
461
 * \return Standard Pacemaker return code
462
 *
463
 * \note Messages from this function won't be logged to the new log file.
464
 */
465
int
466
pcmk__add_logfile(const char *filename)
467
0
{
468
0
    static int32_t default_target = -1;
469
0
    static bool have_logfile = false;
470
471
0
    int32_t target = 0;
472
0
    int rc = pcmk_rc_ok;
473
0
    bool is_default = false;
474
475
    // No logging to do if the file is "/dev/null" or special value "none"
476
0
    if (pcmk__str_eq(filename, "/dev/null", pcmk__str_none)
477
0
        || pcmk__str_eq(filename, PCMK_VALUE_NONE, pcmk__str_casei)) {
478
479
0
        return pcmk_rc_ok;
480
0
    }
481
482
    // Use default if caller didn't specify (and we don't already have one)
483
0
    if (filename == NULL) {
484
0
        if (have_logfile) {
485
0
            return pcmk_rc_ok;
486
0
        }
487
0
        filename = DEFAULT_LOG_FILE;
488
0
    }
489
490
    // If the caller wants the default and we already have it, we're done
491
0
    is_default = pcmk__str_eq(filename, DEFAULT_LOG_FILE, pcmk__str_none);
492
0
    if (is_default && (default_target >= 0)) {
493
0
        return pcmk_rc_ok;
494
0
    }
495
496
    // If we're root, ensure ownership and permissions are correct
497
0
    rc = set_logfile_permissions(filename);
498
0
    if (rc != pcmk_rc_ok) {
499
        // Warning has already been logged
500
0
        return rc;
501
0
    }
502
503
    // Open as a libqb log file
504
0
    target = qb_log_file_open(filename);
505
0
    if (target < 0) {
506
0
        rc = -target;   // target == -errno
507
0
        pcmk__warn("Logging to '%s' is disabled because qb_log_file_open() "
508
0
                   "failed: %s " QB_XS " uid=%lld gid=%lld",
509
0
                   filename, strerror(rc), (long long) geteuid(),
510
0
                   (long long) getegid());
511
0
        return rc;
512
0
    }
513
514
0
    if (is_default) {
515
0
        default_target = target;
516
0
        setenv_logfile(filename);
517
518
0
    } else if (default_target >= 0) {
519
        /* @TODO Why do we disable logging to the default log file when adding
520
         * another log file? This seems wrong, especially if the default file is
521
         * configured explicitly.
522
         */
523
0
        pcmk__notice("Switching logging to %s", filename);
524
0
        disable_logfile(default_target);
525
0
    }
526
527
0
    pcmk__notice("Additional logging available in %s", filename);
528
0
    enable_logfile(target);
529
0
    have_logfile = true;
530
0
    return pcmk_rc_ok;
531
0
}
532
533
/*!
534
 * \brief Add multiple additional log files
535
 *
536
 * \param[in] log_files  Array of log files to add
537
 * \param[in] out        Output object to use for error reporting
538
 *
539
 * \return Standard Pacemaker return code
540
 */
541
void
542
pcmk__add_logfiles(gchar **log_files, pcmk__output_t *out)
543
0
{
544
0
    if (log_files == NULL) {
545
0
        return;
546
0
    }
547
548
0
    for (gchar **fname = log_files; *fname != NULL; fname++) {
549
0
        int rc = pcmk__add_logfile(*fname);
550
551
0
        if (rc != pcmk_rc_ok) {
552
0
            out->err(out, "Logging to %s is disabled: %s",
553
0
                     *fname, pcmk_rc_str(rc));
554
0
        }
555
0
    }
556
0
}
557
558
/*!
559
 * \internal
560
 * \brief Write out a blackbox (enabling blackboxes if the signal is \c SIGTRAP)
561
 *
562
 * \param[in] nsig  Signal number that was received
563
 *
564
 * \note This is a true signal handler, and so must be async-safe.
565
 */
566
static void
567
enable_and_write_blackbox(int nsig)
568
0
{
569
0
    if (nsig == SIGTRAP) {
570
0
        crm_enable_blackbox(nsig);
571
0
    }
572
573
0
    crm_write_blackbox(nsig, NULL);
574
0
}
575
576
static void
577
blackbox_logger(int32_t t, struct qb_log_callsite *cs, log_time_t timestamp,
578
                const char *msg)
579
0
{
580
0
    if(cs && cs->priority < LOG_ERR) {
581
0
        crm_write_blackbox(SIGTRAP, cs); /* Bypass the over-dumping logic */
582
0
    } else {
583
0
        crm_write_blackbox(0, cs);
584
0
    }
585
0
}
586
587
void
588
crm_enable_blackbox(int nsig)
589
0
{
590
0
    if (qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET,
591
0
                   0) == QB_LOG_STATE_ENABLED) {
592
0
        return;
593
0
    }
594
595
    // Any size change drops existing entries
596
0
    qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_SIZE, 5 * 1024 * 1024);
597
598
    // Setting the size seems to disable the log target
599
0
    qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
600
601
    /* Enable synchronous logging for each target except QB_LOG_SYSLOG and
602
     * QB_LOG_STDERR
603
     */
604
0
    for (int i = QB_LOG_BLACKBOX; i < QB_LOG_TARGET_MAX; i++) {
605
0
        qb_log_ctl(i, QB_LOG_CONF_FILE_SYNC, QB_TRUE);
606
0
    }
607
608
0
    pcmk__notice("Initiated blackbox recorder: %s", blackbox_file_prefix);
609
610
    // Save to disk on abnormal termination
611
0
    crm_signal_handler(SIGSEGV, enable_and_write_blackbox);
612
0
    crm_signal_handler(SIGABRT, enable_and_write_blackbox);
613
0
    crm_signal_handler(SIGILL, enable_and_write_blackbox);
614
0
    crm_signal_handler(SIGBUS, enable_and_write_blackbox);
615
0
    crm_signal_handler(SIGFPE, enable_and_write_blackbox);
616
617
0
    crm_update_callsites();
618
619
0
    blackbox_trigger = qb_log_custom_open(blackbox_logger, NULL, NULL, NULL);
620
0
    qb_log_ctl(blackbox_trigger, QB_LOG_CONF_ENABLED, QB_TRUE);
621
0
    pcmk__trace("Trigger: %d is %d %d", blackbox_trigger,
622
0
                qb_log_ctl(blackbox_trigger, QB_LOG_CONF_STATE_GET, 0),
623
0
                QB_LOG_STATE_ENABLED);
624
625
0
    crm_update_callsites();
626
0
}
627
628
void
629
crm_disable_blackbox(int nsig)
630
0
{
631
0
    if (qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_STATE_GET,
632
0
                   0) != QB_LOG_STATE_ENABLED) {
633
0
        return;
634
0
    }
635
636
0
    qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
637
638
    // Disable synchronous logging again when the blackbox is disabled
639
0
    for (int i = QB_LOG_BLACKBOX; i < QB_LOG_TARGET_MAX; i++) {
640
0
        qb_log_ctl(i, QB_LOG_CONF_FILE_SYNC, QB_FALSE);
641
0
    }
642
0
}
643
644
/*!
645
 * \internal
646
 * \brief Write out a blackbox, if blackboxes are enabled
647
 *
648
 * \param[in] nsig  Signal that was received
649
 * \param[in] cs    libqb callsite
650
 *
651
 * \note This may be called via a true signal handler and so must be async-safe.
652
 * @TODO actually make this async-safe
653
 */
654
void
655
crm_write_blackbox(int nsig, const struct qb_log_callsite *cs)
656
0
{
657
0
    static volatile int counter = 1;
658
0
    static volatile time_t last = 0;
659
660
0
    char *buffer = NULL;
661
0
    int rc = 0;
662
0
    time_t now = time(NULL);
663
664
0
    if (blackbox_file_prefix == NULL) {
665
0
        return;
666
0
    }
667
668
0
    switch (nsig) {
669
0
        case 0:
670
0
        case SIGTRAP:
671
            /* The graceful case - such as assertion failure or user request */
672
673
0
            if (nsig == 0 && now == last) {
674
                /* Prevent over-dumping */
675
0
                return;
676
0
            }
677
678
0
            buffer = pcmk__assert_asprintf("%s.%d", blackbox_file_prefix,
679
0
                                           counter++);
680
0
            if (nsig == SIGTRAP) {
681
0
                pcmk__notice("Blackbox dump requested, please see %s for "
682
0
                             "contents",
683
0
                             buffer);
684
685
0
            } else if (cs) {
686
0
                syslog(LOG_NOTICE,
687
0
                       "Problem detected at %s:%d (%s), please see %s for additional details",
688
0
                       cs->function, cs->lineno, cs->filename, buffer);
689
0
            } else {
690
0
                pcmk__notice("Problem detected, please see %s for additional "
691
0
                             "details",
692
0
                             buffer);
693
0
            }
694
695
0
            last = now;
696
697
0
            rc = qb_log_blackbox_write_to_file(buffer);
698
0
            if (rc < 0) {
699
                // System errno
700
0
                pcmk__err("Failed to write blackbox file %s: %s", buffer,
701
0
                          strerror(-rc));
702
0
            }
703
704
            /* Flush the existing contents
705
             * A size change would also work
706
             */
707
0
            qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
708
0
            qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_TRUE);
709
0
            break;
710
711
0
        default:
712
            /* Do as little as possible, just try to get what we have out
713
             * We logged the filename when the blackbox was enabled
714
             */
715
0
            crm_signal_handler(nsig, SIG_DFL);
716
0
            qb_log_blackbox_write_to_file((const char *)blackbox_file_prefix);
717
0
            qb_log_ctl(QB_LOG_BLACKBOX, QB_LOG_CONF_ENABLED, QB_FALSE);
718
0
            raise(nsig);
719
0
            break;
720
0
    }
721
722
0
    free(buffer);
723
0
}
724
725
static const char *
726
crm_quark_to_string(uint32_t tag)
727
0
{
728
0
    const char *text = g_quark_to_string(tag);
729
730
0
    if (text) {
731
0
        return text;
732
0
    }
733
0
    return "";
734
0
}
735
736
static void
737
crm_log_filter_source(int source, struct qb_log_callsite *cs)
738
0
{
739
0
    if (qb_log_ctl(source, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
740
0
        return;
741
0
    }
742
743
0
    if ((cs->tags != crm_trace_nonlog) && (source == QB_LOG_BLACKBOX)) {
744
        /* Blackbox gets everything if enabled */
745
0
        qb_bit_set(cs->targets, source);
746
0
        return;
747
0
    }
748
749
0
    if ((source == blackbox_trigger) && (blackbox_trigger > 0)) {
750
        /* Should this log message result in the blackbox being dumped */
751
0
        if (cs->priority <= LOG_ERR) {
752
0
            qb_bit_set(cs->targets, source);
753
754
0
        } else if (trace_blackbox != NULL) {
755
0
            char *key = pcmk__assert_asprintf("%s:%d", cs->function,
756
0
                                              cs->lineno);
757
758
0
            if (pcmk__g_strv_contains(trace_blackbox, key)) {
759
0
                qb_bit_set(cs->targets, source);
760
0
            }
761
0
            free(key);
762
0
        }
763
0
        return;
764
0
    }
765
766
0
    if (source == QB_LOG_SYSLOG) {
767
        // No tracing to syslog
768
0
        if ((cs->priority <= crm_log_priority)
769
0
            && (cs->priority <= crm_log_level)) {
770
771
0
            qb_bit_set(cs->targets, source);
772
0
        }
773
0
        return;
774
0
    }
775
776
0
    if (cs->priority <= crm_log_level) {
777
0
        qb_bit_set(cs->targets, source);
778
0
        return;
779
0
    }
780
781
0
    if ((trace_files != NULL)
782
0
        && pcmk__g_strv_contains(trace_files, cs->filename)) {
783
784
0
        qb_bit_set(cs->targets, source);
785
0
        return;
786
0
    }
787
788
0
    if ((trace_functions != NULL)
789
0
        && pcmk__g_strv_contains(trace_functions, cs->function)) {
790
791
0
        qb_bit_set(cs->targets, source);
792
0
        return;
793
0
    }
794
795
0
    if ((trace_formats != NULL)
796
0
        && pcmk__g_strv_contains(trace_formats, cs->format)) {
797
798
0
        qb_bit_set(cs->targets, source);
799
0
        return;
800
0
    }
801
802
0
    if ((cs->tags != 0) && (cs->tags != crm_trace_nonlog)
803
0
        && (g_quark_to_string(cs->tags) != NULL)) {
804
805
0
        qb_bit_set(cs->targets, source);
806
0
    }
807
0
}
808
809
static void
810
crm_log_filter(struct qb_log_callsite *cs)
811
0
{
812
0
    cs->targets = 0;            /* Reset then find targets to enable */
813
0
    for (int i = QB_LOG_SYSLOG; i < QB_LOG_TARGET_MAX; i++) {
814
0
        crm_log_filter_source(i, cs);
815
0
    }
816
0
}
817
818
/*!
819
 * \internal
820
 * \brief Parse environment variables specifying which objects to trace
821
 */
822
static void
823
init_tracing(void)
824
0
{
825
0
    const char *blackbox = pcmk__env_option(PCMK__ENV_TRACE_BLACKBOX);
826
0
    const char *files = pcmk__env_option(PCMK__ENV_TRACE_FILES);
827
0
    const char *formats = pcmk__env_option(PCMK__ENV_TRACE_FORMATS);
828
0
    const char *functions = pcmk__env_option(PCMK__ENV_TRACE_FUNCTIONS);
829
0
    const char *tags = pcmk__env_option(PCMK__ENV_TRACE_TAGS);
830
831
0
    if (blackbox != NULL) {
832
0
        trace_blackbox = g_strsplit(blackbox, ",", 0);
833
0
    }
834
835
0
    if (files != NULL) {
836
0
        trace_files = g_strsplit(files, ",", 0);
837
0
        tracing_enabled = true;
838
0
    }
839
840
0
    if (formats != NULL) {
841
0
        trace_formats = g_strsplit(formats, ",", 0);
842
0
        tracing_enabled = true;
843
0
    }
844
845
0
    if (functions != NULL) {
846
0
        trace_functions = g_strsplit(functions, ",", 0);
847
0
        tracing_enabled = true;
848
0
    }
849
850
0
    if (tags != NULL) {
851
0
        gchar **trace_tags = g_strsplit(tags, ",", 0);
852
853
0
        for (gchar **tag = trace_tags; *tag != NULL; tag++) {
854
0
            if (pcmk__str_empty(*tag)) {
855
0
                continue;
856
0
            }
857
858
0
            pcmk__info("Created GQuark %lld from token '%s' in '%s'",
859
0
                       (long long) g_quark_from_string(*tag), *tag, tags);
860
0
            tracing_enabled = true;
861
0
        }
862
863
        // We have the GQuarks, so we don't need the array anymore
864
0
        g_strfreev(trace_tags);
865
0
    }
866
0
}
867
868
/*!
869
 * \internal
870
 * \brief Free arrays of parsed trace objects
871
 *
872
 * \note GQuarks for trace tags can't be removed.
873
 */
874
static void
875
cleanup_tracing(void)
876
0
{
877
0
    g_clear_pointer(&trace_blackbox, g_strfreev);
878
0
    g_clear_pointer(&trace_files, g_strfreev);
879
0
    g_clear_pointer(&trace_formats, g_strfreev);
880
0
    g_clear_pointer(&trace_functions, g_strfreev);
881
0
    tracing_enabled = false;
882
0
}
883
884
gboolean
885
crm_is_callsite_active(struct qb_log_callsite *cs, uint8_t level, uint32_t tags)
886
19.4k
{
887
19.4k
    gboolean refilter = FALSE;
888
889
19.4k
    if (cs == NULL) {
890
19.4k
        return FALSE;
891
19.4k
    }
892
893
0
    if (cs->priority != level) {
894
0
        cs->priority = level;
895
0
        refilter = TRUE;
896
0
    }
897
898
0
    if (cs->tags != tags) {
899
0
        cs->tags = tags;
900
0
        refilter = TRUE;
901
0
    }
902
903
0
    if (refilter) {
904
0
        crm_log_filter(cs);
905
0
    }
906
907
0
    if (cs->targets == 0) {
908
0
        return FALSE;
909
0
    }
910
0
    return TRUE;
911
0
}
912
913
void
914
crm_update_callsites(void)
915
200
{
916
200
    static bool log = true;
917
918
200
    if (log) {
919
1
        log = false;
920
1
        pcmk__debug("Enabling callsites based on priority=%d, files=%s, "
921
1
                    "functions=%s, formats=%s, tags=%s",
922
1
                    crm_log_level,
923
0
                    pcmk__s(pcmk__env_option(PCMK__ENV_TRACE_FILES), "<null>"),
924
0
                    pcmk__s(pcmk__env_option(PCMK__ENV_TRACE_FUNCTIONS),
925
0
                            "<null>"),
926
0
                    pcmk__s(pcmk__env_option(PCMK__ENV_TRACE_FORMATS),
927
0
                            "<null>"),
928
0
                    pcmk__s(pcmk__env_option(PCMK__ENV_TRACE_TAGS), "<null>"));
929
1
    }
930
200
    qb_log_filter_fn_set(crm_log_filter);
931
200
}
932
933
static int
934
crm_priority2int(const char *name)
935
0
{
936
0
    struct syslog_names {
937
0
        const char *name;
938
0
        int priority;
939
0
    };
940
0
    static struct syslog_names p_names[] = {
941
0
        {"emerg", LOG_EMERG},
942
0
        {"alert", LOG_ALERT},
943
0
        {"crit", LOG_CRIT},
944
0
        {"error", LOG_ERR},
945
0
        {"warning", LOG_WARNING},
946
0
        {"notice", LOG_NOTICE},
947
0
        {"info", LOG_INFO},
948
0
        {"debug", LOG_DEBUG},
949
0
        {NULL, -1}
950
0
    };
951
952
0
    for (int i = 0; (name != NULL) && (p_names[i].name != NULL); i++) {
953
0
        if (pcmk__str_eq(p_names[i].name, name, pcmk__str_none)) {
954
0
            return p_names[i].priority;
955
0
        }
956
0
    }
957
0
    return crm_log_priority;
958
0
}
959
960
961
/*!
962
 * \internal
963
 * \brief Set the identifier for the current process
964
 *
965
 * If the identifier crm_system_name is not already set, then it is set as follows:
966
 * - it is passed to the function via the "entity" parameter, or
967
 * - it is derived from the executable name
968
 *
969
 * The identifier can be used in logs, IPC, and more.
970
 *
971
 * This method also sets the PCMK_service environment variable.
972
 *
973
 * \param[in] entity  If not NULL, will be assigned to the identifier
974
 * \param[in] argc    The number of command line parameters
975
 * \param[in] argv    The command line parameter values
976
 */
977
static void
978
set_identity(const char *entity, int argc, char *const *argv)
979
0
{
980
0
    if (crm_system_name != NULL) {
981
0
        return; // Already set, don't overwrite
982
0
    }
983
984
0
    if (entity != NULL) {
985
0
        crm_system_name = pcmk__str_copy(entity);
986
987
0
    } else if ((argc > 0) && (argv != NULL)) {
988
0
        char *mutable = strdup(argv[0]);
989
0
        char *modified = basename(mutable);
990
991
0
        if (strstr(modified, "lt-") == modified) {
992
0
            modified += 3;
993
0
        }
994
0
        crm_system_name = pcmk__str_copy(modified);
995
0
        free(mutable);
996
997
0
    } else {
998
0
        crm_system_name = pcmk__str_copy("Unknown");
999
0
    }
1000
1001
    // Used by fencing.py.py (in fence-agents)
1002
0
    pcmk__set_env_option(PCMK__ENV_SERVICE, crm_system_name, false);
1003
0
}
1004
1005
void
1006
crm_log_preinit(const char *entity, int argc, char *const *argv)
1007
0
{
1008
    /* Configure libqb logging with nothing turned on */
1009
0
    static bool have_logging = false;
1010
0
    struct utsname res = { 0, };
1011
0
    const pid_t pid = getpid();
1012
0
    const char *nodename = "localhost";
1013
1014
0
    if (have_logging) {
1015
0
        return;
1016
0
    }
1017
1018
0
    have_logging = true;
1019
1020
0
    init_tracing();
1021
1022
    /* @TODO Try to create a more obvious "global Pacemaker initializer"
1023
     * function than crm_log_preinit(), and call pcmk__schema_init() there.
1024
     * See also https://projects.clusterlabs.org/T840.
1025
     */
1026
0
    pcmk__schema_init();
1027
1028
0
    if (crm_trace_nonlog == 0) {
1029
0
        crm_trace_nonlog = g_quark_from_static_string("Pacemaker non-logging tracepoint");
1030
0
    }
1031
1032
0
    umask(S_IWGRP | S_IWOTH | S_IROTH);
1033
1034
0
    set_glib_log_handlers();
1035
1036
    /* glib should not abort for any messages from the Pacemaker domain, but
1037
     * other domains are still free to specify their own behavior.  However,
1038
     * note that G_LOG_LEVEL_ERROR is always fatal regardless of what we do
1039
     * here.
1040
     */
1041
0
    g_log_set_fatal_mask(G_LOG_DOMAIN, 0);
1042
1043
    /* Set crm_system_name, which is used as the logging name. It may also
1044
     * be used for other purposes such as an IPC client name.
1045
     */
1046
0
    set_identity(entity, argc, argv);
1047
1048
0
    qb_log_init(crm_system_name, qb_log_facility2int("local0"), LOG_ERR);
1049
0
    crm_log_level = LOG_CRIT;
1050
1051
    /* Nuke any syslog activity until it's asked for */
1052
0
    qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
1053
0
#ifdef HAVE_qb_log_conf_QB_LOG_CONF_MAX_LINE_LEN
1054
    // Shorter than default, generous for what we *should* send to syslog
1055
0
    qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_MAX_LINE_LEN, 256);
1056
0
#endif
1057
1058
0
    if ((uname(&res) == 0) && !pcmk__str_empty(res.nodename)) {
1059
0
        nodename = res.nodename;
1060
0
    }
1061
1062
    /* Set format strings and disable threading
1063
     * Pacemaker and threads do not mix well (due to the amount of forking)
1064
     */
1065
0
    qb_log_tags_stringify_fn_set(crm_quark_to_string);
1066
0
    for (int i = QB_LOG_SYSLOG; i < QB_LOG_TARGET_MAX; i++) {
1067
0
        qb_log_ctl(i, QB_LOG_CONF_THREADED, QB_FALSE);
1068
0
#ifdef HAVE_qb_log_conf_QB_LOG_CONF_ELLIPSIS
1069
        // End truncated lines with '...'
1070
0
        qb_log_ctl(i, QB_LOG_CONF_ELLIPSIS, QB_TRUE);
1071
0
#endif
1072
0
        set_format_string(i, pid, nodename);
1073
0
    }
1074
1075
#ifdef ENABLE_NLS
1076
    /* Enable translations (experimental). Currently we only have a few
1077
     * proof-of-concept translations for some option help. The goal would be to
1078
     * offer translations for option help and man pages rather than logs or
1079
     * documentation, to reduce the burden of maintaining them.
1080
     */
1081
1082
    // Load locale information for the local host from the environment
1083
    setlocale(LC_ALL, "");
1084
1085
    // Tell gettext where to find Pacemaker message catalogs
1086
    pcmk__assert(bindtextdomain(PACKAGE, PCMK__LOCALE_DIR) != NULL);
1087
1088
    // Tell gettext to use the Pacemaker message catalogs
1089
    pcmk__assert(textdomain(PACKAGE) != NULL);
1090
1091
    // Tell gettext that the translated strings are stored in UTF-8
1092
    bind_textdomain_codeset(PACKAGE, "UTF-8");
1093
#endif
1094
0
}
1095
1096
gboolean
1097
crm_log_init(const char *entity, uint8_t level, gboolean daemon, gboolean to_stderr,
1098
             int argc, char **argv, gboolean quiet)
1099
0
{
1100
0
    const char *syslog_priority = NULL;
1101
0
    const char *facility = pcmk__env_option(PCMK__ENV_LOGFACILITY);
1102
0
    const char *f_copy = facility;
1103
1104
0
    pcmk__is_daemon = daemon;
1105
0
    crm_log_preinit(entity, argc, argv);
1106
1107
0
    if (level > LOG_TRACE) {
1108
0
        level = LOG_TRACE;
1109
0
    }
1110
0
    if(level > crm_log_level) {
1111
0
        crm_log_level = level;
1112
0
    }
1113
1114
    /* Should we log to syslog */
1115
0
    if (facility == NULL) {
1116
0
        if (pcmk__is_daemon) {
1117
0
            facility = "daemon";
1118
0
        } else {
1119
0
            facility = PCMK_VALUE_NONE;
1120
0
        }
1121
0
        pcmk__set_env_option(PCMK__ENV_LOGFACILITY, facility, true);
1122
0
    }
1123
1124
0
    if (pcmk__str_eq(facility, PCMK_VALUE_NONE, pcmk__str_casei)) {
1125
0
        quiet = TRUE;
1126
1127
1128
0
    } else {
1129
0
        qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_FACILITY, qb_log_facility2int(facility));
1130
0
    }
1131
1132
0
    if (pcmk__env_option_enabled(crm_system_name, PCMK__ENV_DEBUG)) {
1133
        /* Override the default setting */
1134
0
        crm_log_level = LOG_DEBUG;
1135
0
    }
1136
1137
    /* What lower threshold do we have for sending to syslog */
1138
0
    syslog_priority = pcmk__env_option(PCMK__ENV_LOGPRIORITY);
1139
0
    if (syslog_priority) {
1140
0
        crm_log_priority = crm_priority2int(syslog_priority);
1141
0
    }
1142
0
    qb_log_filter_ctl(QB_LOG_SYSLOG, QB_LOG_FILTER_ADD, QB_LOG_FILTER_FILE, "*",
1143
0
                      crm_log_priority);
1144
1145
    // Log to syslog unless requested to be quiet
1146
0
    if (!quiet) {
1147
0
        qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_TRUE);
1148
0
    }
1149
1150
    /* Should we log to stderr */ 
1151
0
    if (pcmk__env_option_enabled(crm_system_name, PCMK__ENV_STDERR)) {
1152
        /* Override the default setting */
1153
0
        to_stderr = TRUE;
1154
0
    }
1155
0
    crm_enable_stderr(to_stderr);
1156
1157
    // Log to a file if we're a daemon or user asked for one
1158
0
    {
1159
0
        const char *logfile = pcmk__env_option(PCMK__ENV_LOGFILE);
1160
1161
0
        if (pcmk__is_daemon || (logfile != NULL)) {
1162
            // Daemons always get a log file, unless explicitly set to "none"
1163
0
            pcmk__add_logfile(logfile);
1164
0
        }
1165
0
    }
1166
1167
0
    if (pcmk__is_daemon
1168
0
        && pcmk__env_option_enabled(crm_system_name, PCMK__ENV_BLACKBOX)) {
1169
0
        crm_enable_blackbox(0);
1170
0
    }
1171
1172
    /* Summary */
1173
0
    pcmk__trace("Quiet: %d, facility %s", quiet, f_copy);
1174
0
    pcmk__env_option(PCMK__ENV_LOGFILE);
1175
0
    pcmk__env_option(PCMK__ENV_LOGFACILITY);
1176
1177
0
    crm_update_callsites();
1178
1179
    /* Ok, now we can start logging... */
1180
1181
    // Disable daemon request if user isn't root or Pacemaker daemon user
1182
0
    if (pcmk__is_daemon) {
1183
0
        const char *user = getenv("USER");
1184
1185
0
        if (user != NULL && !pcmk__strcase_any_of(user, "root", CRM_DAEMON_USER, NULL)) {
1186
0
            pcmk__trace("Not switching to corefile directory for %s", user);
1187
0
            pcmk__is_daemon = false;
1188
0
        }
1189
0
    }
1190
1191
0
    if (pcmk__is_daemon) {
1192
0
        char *user = pcmk__uid2username(getuid());
1193
1194
0
        if (user == NULL) {
1195
            // Error already logged
1196
1197
0
        } else if (!pcmk__str_any_of(user, "root", CRM_DAEMON_USER, NULL)) {
1198
0
            pcmk__trace("Don't change active directory for regular user %s",
1199
0
                        user);
1200
1201
0
        } else if (chdir(CRM_CORE_DIR) < 0) {
1202
0
            pcmk__info("Cannot change active directory to " CRM_CORE_DIR ": %s",
1203
0
                       strerror(errno));
1204
1205
0
        } else {
1206
0
            pcmk__info("Changed active directory to " CRM_CORE_DIR);
1207
0
        }
1208
1209
0
        blackbox_file_prefix = pcmk__assert_asprintf(CRM_BLACKBOX_DIR
1210
0
                                                     "/%s-%lld",
1211
0
                                                     crm_system_name,
1212
0
                                                     (long long) getpid());
1213
        /* Original meanings from signal(7)
1214
         *
1215
         * Signal       Value     Action   Comment
1216
         * SIGTRAP        5        Core    Trace/breakpoint trap
1217
         * SIGUSR1     30,10,16    Term    User-defined signal 1
1218
         * SIGUSR2     31,12,17    Term    User-defined signal 2
1219
         *
1220
         * Our usage is as similar as possible
1221
         */
1222
0
        mainloop_add_signal(SIGUSR1, crm_enable_blackbox);
1223
0
        mainloop_add_signal(SIGUSR2, crm_disable_blackbox);
1224
0
        mainloop_add_signal(SIGTRAP, enable_and_write_blackbox);
1225
1226
0
        free(user);
1227
1228
0
    } else if (!quiet) {
1229
0
        crm_log_args(argc, argv);
1230
0
    }
1231
1232
0
    return TRUE;
1233
0
}
1234
1235
/*!
1236
 * \internal
1237
 * \brief Free the logging library's internal data structures
1238
 */
1239
void
1240
crm_log_deinit(void)
1241
0
{
1242
0
    remove_glib_log_handlers();
1243
0
    cleanup_tracing();
1244
1245
0
    if (logger_out != NULL) {
1246
0
        logger_out->finish(logger_out, CRM_EX_OK, true, NULL);
1247
0
        g_clear_pointer(&logger_out, pcmk__output_free);
1248
0
    }
1249
1250
0
    g_clear_pointer(&blackbox_file_prefix, free);
1251
0
    g_clear_pointer(&crm_system_name, free);
1252
0
}
1253
1254
/* returns the old value */
1255
unsigned int
1256
set_crm_log_level(unsigned int level)
1257
0
{
1258
0
    unsigned int old = crm_log_level;
1259
1260
0
    if (level > LOG_TRACE) {
1261
0
        level = LOG_TRACE;
1262
0
    }
1263
0
    crm_log_level = level;
1264
0
    crm_update_callsites();
1265
0
    pcmk__trace("New log level: %d", level);
1266
0
    return old;
1267
0
}
1268
1269
void
1270
crm_enable_stderr(int enable)
1271
200
{
1272
200
    if (enable && qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0) != QB_LOG_STATE_ENABLED) {
1273
200
        qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
1274
200
        crm_update_callsites();
1275
1276
200
    } else if (enable == FALSE) {
1277
0
        qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_FALSE);
1278
0
    }
1279
200
}
1280
1281
/*!
1282
 * \brief Make logging more verbose
1283
 *
1284
 * If logging to stderr is not already enabled when this function is called,
1285
 * enable it. Otherwise, increase the log level by 1.
1286
 *
1287
 * \param[in] argc  Ignored
1288
 * \param[in] argv  Ignored
1289
 */
1290
void
1291
crm_bump_log_level(int argc, char **argv)
1292
0
{
1293
0
    if (qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_STATE_GET, 0)
1294
0
        != QB_LOG_STATE_ENABLED) {
1295
0
        crm_enable_stderr(TRUE);
1296
0
    } else {
1297
0
        set_crm_log_level(crm_log_level + 1);
1298
0
    }
1299
0
}
1300
1301
unsigned int
1302
get_crm_log_level(void)
1303
0
{
1304
0
    return crm_log_level;
1305
0
}
1306
1307
/*!
1308
 * \brief Log the command line (once)
1309
 *
1310
 * \param[in]  Number of values in \p argv
1311
 * \param[in]  Command-line arguments (including command name)
1312
 *
1313
 * \note This function will only log once, even if called with different
1314
 *       arguments.
1315
 */
1316
void
1317
crm_log_args(int argc, char **argv)
1318
0
{
1319
0
    static bool logged = false;
1320
0
    gchar *arg_string = NULL;
1321
1322
0
    if ((argc == 0) || (argv == NULL) || logged) {
1323
0
        return;
1324
0
    }
1325
0
    logged = true;
1326
0
    arg_string = g_strjoinv(" ", argv);
1327
0
    pcmk__notice("Invoked: %s", arg_string);
1328
0
    g_free(arg_string);
1329
0
}
1330
1331
void
1332
crm_log_output_fn(const char *file, const char *function, int line, int level, const char *prefix,
1333
                  const char *output)
1334
0
{
1335
0
    gchar **out_lines = NULL;
1336
1337
0
    if (level == PCMK__LOG_NEVER) {
1338
0
        return;
1339
0
    }
1340
1341
0
    if (output == NULL) {
1342
0
        if (level != PCMK__LOG_STDOUT) {
1343
0
            level = LOG_TRACE;
1344
0
        }
1345
0
        output = "-- empty --";
1346
0
    }
1347
1348
0
    out_lines = g_strsplit(output, "\n", 0);
1349
1350
0
    for (gchar **out_line = out_lines; *out_line != NULL; out_line++) {
1351
0
        do_crm_log_alias(level, file, function, line, "%s [ %s ]",
1352
0
                         prefix, *out_line);
1353
0
    }
1354
1355
0
    g_strfreev(out_lines);
1356
0
}
1357
1358
void
1359
pcmk__cli_init_logging(const char *name, unsigned int verbosity)
1360
0
{
1361
0
    crm_log_init(name, LOG_ERR, FALSE, FALSE, 0, NULL, TRUE);
1362
1363
0
    for (int i = 0; i < verbosity; i++) {
1364
        /* These arguments are ignored, so pass placeholders. */
1365
0
        crm_bump_log_level(0, NULL);
1366
0
    }
1367
0
}
1368
1369
/*!
1370
 * \brief Log XML line-by-line in a formatted fashion
1371
 *
1372
 * \param[in] file      File name to use for log filtering
1373
 * \param[in] function  Function name to use for log filtering
1374
 * \param[in] line      Line number to use for log filtering
1375
 * \param[in] tags      Logging tags to use for log filtering
1376
 * \param[in] level     Priority at which to log the messages
1377
 * \param[in] text      Prefix for each line
1378
 * \param[in] xml       XML to log
1379
 *
1380
 * \note This does nothing when \p level is \c PCMK__LOG_STDOUT.
1381
 * \note Do not call this function directly. It should be called only from the
1382
 *       \p do_crm_log_xml() macro.
1383
 */
1384
void
1385
pcmk_log_xml_as(const char *file, const char *function, uint32_t line,
1386
                uint32_t tags, uint8_t level, const char *text, const xmlNode *xml)
1387
0
{
1388
0
    if (xml == NULL) {
1389
0
        do_crm_log(level, "%s%sNo data to dump as XML",
1390
0
                   pcmk__s(text, ""), pcmk__str_empty(text)? "" : " ");
1391
1392
0
    } else {
1393
0
        if (logger_out == NULL) {
1394
0
            CRM_CHECK(pcmk__log_output_new(&logger_out) == pcmk_rc_ok, return);
1395
0
        }
1396
1397
0
        pcmk__output_set_log_level(logger_out, level);
1398
0
        pcmk__output_set_log_filter(logger_out, file, function, line, tags);
1399
0
        pcmk__xml_show(logger_out, text, xml, 1,
1400
0
                       pcmk__xml_fmt_pretty
1401
0
                       |pcmk__xml_fmt_open
1402
0
                       |pcmk__xml_fmt_children
1403
0
                       |pcmk__xml_fmt_close);
1404
0
        pcmk__output_set_log_filter(logger_out, NULL, NULL, 0U, 0U);
1405
0
    }
1406
0
}
1407
1408
/*!
1409
 * \internal
1410
 * \brief Log XML changes line-by-line in a formatted fashion
1411
 *
1412
 * \param[in] file      File name to use for log filtering
1413
 * \param[in] function  Function name to use for log filtering
1414
 * \param[in] line      Line number to use for log filtering
1415
 * \param[in] tags      Logging tags to use for log filtering
1416
 * \param[in] level     Priority at which to log the messages
1417
 * \param[in] xml       XML whose changes to log
1418
 *
1419
 * \note This does nothing when \p level is \c PCMK__LOG_STDOUT.
1420
 */
1421
void
1422
pcmk__log_xml_changes_as(const char *file, const char *function, uint32_t line,
1423
                         uint32_t tags, uint8_t level, const xmlNode *xml)
1424
0
{
1425
0
    if (xml == NULL) {
1426
0
        do_crm_log(level, "No XML to dump");
1427
0
        return;
1428
0
    }
1429
1430
0
    if (logger_out == NULL) {
1431
0
        CRM_CHECK(pcmk__log_output_new(&logger_out) == pcmk_rc_ok, return);
1432
0
    }
1433
0
    pcmk__output_set_log_level(logger_out, level);
1434
0
    pcmk__output_set_log_filter(logger_out, file, function, line, tags);
1435
0
    pcmk__xml_show_changes(logger_out, xml);
1436
0
    pcmk__output_set_log_filter(logger_out, NULL, NULL, 0U, 0U);
1437
0
}
1438
1439
/*!
1440
 * \internal
1441
 * \brief Log an XML patchset line-by-line in a formatted fashion
1442
 *
1443
 * \param[in] file      File name to use for log filtering
1444
 * \param[in] function  Function name to use for log filtering
1445
 * \param[in] line      Line number to use for log filtering
1446
 * \param[in] tags      Logging tags to use for log filtering
1447
 * \param[in] level     Priority at which to log the messages
1448
 * \param[in] patchset  XML patchset to log
1449
 *
1450
 * \note This does nothing when \p level is \c PCMK__LOG_STDOUT.
1451
 */
1452
void
1453
pcmk__log_xml_patchset_as(const char *file, const char *function, uint32_t line,
1454
                          uint32_t tags, uint8_t level, const xmlNode *patchset)
1455
0
{
1456
0
    if (patchset == NULL) {
1457
0
        do_crm_log(level, "No patchset to dump");
1458
0
        return;
1459
0
    }
1460
1461
0
    if (logger_out == NULL) {
1462
0
        CRM_CHECK(pcmk__log_output_new(&logger_out) == pcmk_rc_ok, return);
1463
0
    }
1464
0
    pcmk__output_set_log_level(logger_out, level);
1465
0
    pcmk__output_set_log_filter(logger_out, file, function, line, tags);
1466
0
    logger_out->message(logger_out, "xml-patchset", patchset);
1467
0
    pcmk__output_set_log_filter(logger_out, NULL, NULL, 0U, 0U);
1468
0
}
1469
1470
void pcmk__set_config_error_handler(pcmk__config_error_func error_handler, void *error_context)
1471
0
{
1472
0
    pcmk__config_error_handler = error_handler;
1473
0
    pcmk__config_error_context = error_context;    
1474
0
}
1475
1476
void pcmk__set_config_warning_handler(pcmk__config_warning_func warning_handler, void *warning_context)
1477
0
{
1478
0
    pcmk__config_warning_handler = warning_handler;
1479
0
    pcmk__config_warning_context = warning_context;   
1480
0
}