Skip to content

Commit

Permalink
Update logging on behalf of elogind
Browse files Browse the repository at this point in the history
* log_debug_elogind(): Remove some indirections
* log_dispatch_internal(): Prefer syslog over kmsg
* basic/log.*: Mask unneeded functions and ensure that elogind
  prefers syslog over kmsg

Closes: #168
Closes: #179

Signed-off-by: Sven Eden <sven.eden@prydeworx.com>
(cherry picked from commit df1cd9f)
(cherry picked from commit c7cce50)
(cherry picked from commit 4518675)
  • Loading branch information
Yamakuzure committed Nov 29, 2020
1 parent 4a2e19b commit 90e4402
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
26 changes: 18 additions & 8 deletions src/basic/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,9 @@ static bool upgrade_syslog_to_journal = false;
#endif // 0
static bool always_reopen_console = false;
static bool open_when_needed = false;
#if 0 /// UNNEEDED by elogind
static bool prohibit_ipc = false;
#endif // 0

/* Akin to glibc's __abort_msg; which is private and we hence cannot
* use here. */
Expand Down Expand Up @@ -224,7 +226,6 @@ static int log_open_journal(void) {
log_close_journal();
return r;
}
#endif // 0

static bool stderr_is_journal(void) {
_cleanup_free_ char *w = NULL;
Expand All @@ -251,6 +252,7 @@ static bool stderr_is_journal(void) {

return st.st_dev == dev && st.st_ino == ino;
}
#endif // 0

int log_open(void) {
int r;
Expand All @@ -271,9 +273,9 @@ int log_open(void) {
return 0;
}

#if 0 /// elogind does not support logging to systemd-journald and is not PID 1
if (log_target != LOG_TARGET_AUTO || getpid_cached() == 1 || stderr_is_journal()) {

#if 0 /// elogind does not support logging to systemd-journald
if (!prohibit_ipc &&
IN_SET(log_target, LOG_TARGET_AUTO,
LOG_TARGET_JOURNAL_OR_KMSG,
Expand All @@ -285,16 +287,16 @@ int log_open(void) {
return r;
}
}
#else // 0
if (log_target != LOG_TARGET_CONSOLE) {
#endif // 0

if (!prohibit_ipc &&
#if 0 /// Add syslog to elogind LOG_TARGET_AUTO set
if (!prohibit_ipc &&
IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
LOG_TARGET_SYSLOG)) {
#else // 0
IN_SET(log_target, LOG_TARGET_AUTO,
LOG_TARGET_SYSLOG_OR_KMSG,
LOG_TARGET_SYSLOG)) {
if ( IN_SET(log_target, LOG_TARGET_AUTO, LOG_TARGET_SYSLOG_OR_KMSG, LOG_TARGET_SYSLOG)) {
#endif // 0
r = log_open_syslog();
if (r >= 0) {
Expand Down Expand Up @@ -668,7 +670,7 @@ int log_dispatch_internal(
if ((e = strpbrk(buffer, NEWLINE)))
*(e++) = 0;

#if 0 /// elogind does not support logging to systemd-journald
#if 0 /// elogind does not support logging to systemd-journald and prefers syslog over kmsg
if (IN_SET(log_target, LOG_TARGET_AUTO,
LOG_TARGET_JOURNAL_OR_KMSG,
LOG_TARGET_JOURNAL)) {
Expand All @@ -677,10 +679,14 @@ int log_dispatch_internal(
if (k < 0 && k != -EAGAIN)
log_close_journal();
}
#endif // 0

if (IN_SET(log_target, LOG_TARGET_SYSLOG_OR_KMSG,
LOG_TARGET_SYSLOG)) {
#else // 0
if (IN_SET(log_target, LOG_TARGET_AUTO,
LOG_TARGET_SYSLOG_OR_KMSG,
LOG_TARGET_SYSLOG)) {
#endif // 0

k = write_to_syslog(level, error, file, line, func, buffer);
if (k < 0 && k != -EAGAIN)
Expand Down Expand Up @@ -1434,9 +1440,11 @@ void log_set_open_when_needed(bool b) {
open_when_needed = b;
}

#if 0 /// UNNEEDED by elogind
void log_set_prohibit_ipc(bool b) {
prohibit_ipc = b;
}
#endif // 0

int log_emergency_level(void) {
/* Returns the log level to use for log_emergency() logging. We use LOG_EMERG only when we are PID 1, as only
Expand All @@ -1445,6 +1453,7 @@ int log_emergency_level(void) {
return getpid_cached() == 1 ? LOG_EMERG : LOG_ERR;
}

#if 0 /// UNNEEDED by elogind
int log_dup_console(void) {
int copy;

Expand All @@ -1461,6 +1470,7 @@ int log_dup_console(void) {
console_fd = copy;
return 0;
}
#endif // 0

void log_setup_service(void) {
/* Sets up logging the way it is most appropriate for running a program as a service. Note that using this
Expand Down
12 changes: 7 additions & 5 deletions src/basic/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,11 +269,11 @@ int log_emergency_level(void);
#endif

#if ENABLE_DEBUG_ELOGIND
# define log_debug_elogind_full(...) { \
log_set_max_level(LOG_DEBUG); \
log_full(LOG_DEBUG, __VA_ARGS__); \
}
# define log_debug_elogind(fmt, ...) log_debug_elogind_full("(DEBUG) " fmt, __VA_ARGS__)
# define log_debug_elogind_full(...) do { \
log_set_max_level_realm(LOG_REALM, LOG_DEBUG); \
log_full_errno_realm(LOG_REALM, LOG_DEBUG, 0, __VA_ARGS__); \
} while(0)
# define log_debug_elogind(fmt, ...) log_debug_elogind_full("%s (DEBUG) " fmt, program_invocation_short_name, __VA_ARGS__)
#else
# define log_debug_elogind(...) do {} while (0)
#endif // ENABLE_DEBUG_ELOGIND
Expand Down Expand Up @@ -320,11 +320,13 @@ void log_set_always_reopen_console(bool b);
* desired as we want to reuse our logging streams. It is useful however */
void log_set_open_when_needed(bool b);

#if 0 /// UNNEEDED by elogind
/* If turned on, then we'll never use IPC-based logging, i.e. never log to syslog or the journal. We'll only log to
* stderr, the console or kmsg */
void log_set_prohibit_ipc(bool b);

int log_dup_console(void);
#endif // 0

int log_syntax_internal(
const char *unit,
Expand Down
9 changes: 7 additions & 2 deletions src/login/logind.c
Original file line number Diff line number Diff line change
Expand Up @@ -1237,27 +1237,30 @@ static int run(int argc, char *argv[]) {
return r;

#if 1 /// perform extra checks for elogind startup
log_debug_elogind("%s", "Calling elogind startup");
r = elogind_startup();
if (r)
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;

/* elogind allows to daemonize itself via -D/--daemon argument */
if ( daemonize ) {
log_debug_elogind("%s", "Daemonizing elogind...");
r = elogind_daemonize();
if ( r < 0 )
return log_error_errno( r, "Failed to daemonoze: %m" );
return log_error_errno( r, "Failed to daemonize: %m" );
if ( r > 0 )
return EXIT_SUCCESS;
// Re-setup logging
log_debug_elogind("%s", "Setup logging again...");
log_set_facility(LOG_AUTH);
log_setup_service();
#if ENABLE_DEBUG_ELOGIND
log_set_max_level(LOG_DEBUG);
log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
#endif // ENABLE_DEBUG_ELOGIND
}

// Now the PID file can be written, whether we daemonized or not
log_debug_elogind("%s", "Writing PID file...");
write_pid_file();
#endif // 1

Expand Down Expand Up @@ -1298,6 +1301,7 @@ static int run(int argc, char *argv[]) {
assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGHUP, SIGTERM, SIGINT, SIGCHLD, SIGQUIT, -1) >= 0);
#endif // 0

log_debug_elogind("%s", "Creating manager...");
r = manager_new(&m);
if (r < 0)
return log_error_errno(r, "Failed to allocate manager object: %m");
Expand All @@ -1307,6 +1311,7 @@ static int run(int argc, char *argv[]) {
#if 1 /// elogind needs an Add-On for sleep configuration
elogind_manager_reset_config(m);
#endif // 1
log_debug_elogind("%s", "Starting manager...");
r = manager_startup(m);
if (r < 0)
return log_error_errno(r, "Failed to fully start up daemon: %m");
Expand Down
25 changes: 13 additions & 12 deletions src/sleep/sleep.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static int nvidia_sleep(Manager* m, char const* verb, unsigned* vtnr) {
r = sd_uid_get_sessions(m->scheduled_sleep_uid, 1, &sessions);
#if ENABLE_DEBUG_ELOGIND
char *t = strv_join(sessions, " ");
log_debug("sd_uid_get_sessions() returned %d, result is: %s", r, strnull(t));
log_debug_elogind("sd_uid_get_sessions() returned %d, result is: %s", r, strnull(t));
free(t);
#endif // elogind debug
if (r < 0)
Expand All @@ -82,7 +82,7 @@ static int nvidia_sleep(Manager* m, char const* verb, unsigned* vtnr) {
int k;
k = sd_session_get_vt(*session, &vt);
if (k >= 0) {
log_debug("Active session %s with VT %u found", *session, vt);
log_debug_elogind("Active session %s with VT %u found", *session, vt);
break;
}
}
Expand All @@ -92,7 +92,7 @@ static int nvidia_sleep(Manager* m, char const* verb, unsigned* vtnr) {
int k;
k = sd_session_get_type(*session, &type);
if ((k >= 0) && strcmp("tty", strnull(type))) {
log_debug("Session %s with Type %s counted as non-tty", *session, type);
log_debug_elogind("Session %s with Type %s counted as non-tty", *session, type);
++x11;
}
}
Expand All @@ -101,29 +101,29 @@ static int nvidia_sleep(Manager* m, char const* verb, unsigned* vtnr) {

// Get to a safe non-gui VT if we are on any GUI
if ( x11 > 0 ) {
log_debug("Storing VT %u and switching to VT 0", vt);
log_debug_elogind("Storing VT %u and switching to VT 0", vt);
*vtnr = vt;
(void) chvt(0);
}

// Okay, go to sleep.
if (streq(verb, "suspend")) {
log_debug("Writing 'suspend' into %s", drv_suspend);
log_debug_elogind("Writing 'suspend' into %s", drv_suspend);
r = write_string_file(drv_suspend, "suspend", WRITE_STRING_FILE_DISABLE_BUFFER);
} else {
log_debug("Writing 'hibernate' into %s", drv_suspend);
log_debug_elogind("Writing 'hibernate' into %s", drv_suspend);
r = write_string_file(drv_suspend, "hibernate", WRITE_STRING_FILE_DISABLE_BUFFER);
}

if (r)
return 0;
} else if (streq(verb, "resume")) {
// Wakeup the device
log_debug("Writing '%s' into %s", verb, drv_suspend);
log_debug_elogind("Writing '%s' into %s", verb, drv_suspend);
(void) write_string_file(drv_suspend, verb, WRITE_STRING_FILE_DISABLE_BUFFER);
// Then try to change back
if (*vtnr > 0) {
log_debug("Switching back to VT %u", *vtnr);
log_debug_elogind("Switching back to VT %u", *vtnr);
(void) chvt(*vtnr);
}
}
Expand Down Expand Up @@ -202,6 +202,7 @@ static int write_mode(char **modes) {
char **mode;
#if 1 /// Heeding elogind configuration for SuspendMode, we need to know where to write what
char const* mode_location = strcmp( arg_verb, "suspend") ? "/sys/power/disk" : "/sys/power/mem_sleep";
log_debug_elogind("mode_location is: '%s'", mode_location);
#endif // 1

STRV_FOREACH(mode, modes) {
Expand All @@ -210,7 +211,7 @@ static int write_mode(char **modes) {
#if 0 /// elogind uses an adapting target
k = write_string_file("/sys/power/disk", *mode, WRITE_STRING_FILE_DISABLE_BUFFER);
#else // 0
log_debug("Writing '%s' to '%s'...", *mode, mode_location);
log_debug_elogind("Writing '%s' to '%s'...", *mode, mode_location);
k = write_string_file(mode_location, *mode, WRITE_STRING_FILE_DISABLE_BUFFER);
#endif // 0
if (k >= 0)
Expand Down Expand Up @@ -409,15 +410,15 @@ static int execute(Manager* m, char const* verb, char **modes, char **states) {
/* Configure hibernation settings if we are supposed to hibernate */
if (!strv_isempty(modes)) {
if (strcmp( verb, "suspend")) {
log_debug("Trying to find a hibernation location...");
log_debug_elogind("%s", "Trying to find a hibernation location...");
r = find_hibernate_location(&hibernate_location);
if (r < 0)
return log_error_errno(r, "Failed to find location to hibernate to: %m");
if (r == 0) {
/* 0 means: no hibernation location was configured in the kernel so far, let's
* do it ourselves then. > 0 means: kernel already had a configured hibernation
* location which we shouldn't touch. */
log_debug("Writing new hibernation location...");
log_debug_elogind("%s", "Writing new hibernation location...");
r = write_hibernate_location_info(hibernate_location);
if (r < 0)
return log_error_errno(r, "Failed to prepare for hibernation: %m");
Expand Down Expand Up @@ -675,7 +676,7 @@ int do_sleep(Manager *m, const char *verb) {

arg_verb = (char*) verb;

log_debug_elogind("%s called for %s", __FUNCTION__, strnull(verb));
log_debug_elogind("%s called for %s", __func__, strnull(verb));

/* Re-load the sleep configuration, so users can change their options
* on-the-fly without having to reload elogind
Expand Down

0 comments on commit 90e4402

Please sign in to comment.