pam_systemd: fix OSC write failure message appearing in error logs (#39791)

This commit is contained in:
Yu Watanabe
2025-11-20 23:07:00 +09:00
committed by GitHub
3 changed files with 16 additions and 9 deletions

View File

@@ -1599,7 +1599,7 @@ static int setup_environment(
return setup_runtime_directory(handle, ur, runtime_directory, area);
}
static int open_osc_context(pam_handle_t *handle, const char *session_type, UserRecord *ur) {
static int open_osc_context(pam_handle_t *handle, const char *session_type, UserRecord *ur, bool debug) {
int r;
assert(handle);
@@ -1628,7 +1628,7 @@ static int open_osc_context(pam_handle_t *handle, const char *session_type, User
* so that we don't delay tty hang-up. */
_cleanup_close_ int tty_opath_fd = fd_reopen(STDOUT_FILENO, O_PATH|O_CLOEXEC);
if (tty_opath_fd < 0)
pam_syslog_errno(handle, LOG_DEBUG, tty_opath_fd, "Failed to pin TTY, ignoring: %m");
pam_debug_syslog_errno(handle, debug, tty_opath_fd, "Failed to pin TTY, ignoring: %m");
else
tty_opath_fd = fd_move_above_stdio(tty_opath_fd);
@@ -1670,7 +1670,7 @@ static int open_osc_context(pam_handle_t *handle, const char *session_type, User
return PAM_SUCCESS;
}
static int close_osc_context(pam_handle_t *handle) {
static int close_osc_context(pam_handle_t *handle, bool debug) {
int r;
assert(handle);
@@ -1695,7 +1695,7 @@ static int close_osc_context(pam_handle_t *handle) {
/* Now open the original TTY again, so that we can write on it */
_cleanup_close_ int fd = fd_reopen(tty_opath_fd, O_WRONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
if (fd < 0) {
pam_syslog_errno(handle, LOG_DEBUG, fd, "Failed to reopen TTY, ignoring: %m");
pam_debug_syslog_errno(handle, debug, fd, "Failed to reopen TTY, ignoring: %m");
return PAM_SUCCESS;
}
@@ -1715,7 +1715,7 @@ static int close_osc_context(pam_handle_t *handle) {
/* When we are closing things, the TTY might not take our writes anymore. Accept that gracefully. */
r = loop_write(fd, osc, SIZE_MAX);
if (r < 0)
pam_syslog_errno(handle, LOG_DEBUG, r, "Failed to write OSC sequence to TTY, ignoring: %m");
pam_debug_syslog_errno(handle, debug, r, "Failed to write OSC sequence to TTY, ignoring: %m");
return PAM_SUCCESS;
}
@@ -1807,7 +1807,7 @@ _public_ PAM_EXTERN int pam_sm_open_session(
if (r != PAM_SUCCESS)
return r;
return open_osc_context(handle, c.type, ur);
return open_osc_context(handle, c.type, ur, debug);
}
_public_ PAM_EXTERN int pam_sm_close_session(
@@ -1844,7 +1844,7 @@ _public_ PAM_EXTERN int pam_sm_close_session(
return pam_syslog_pam_error(handle, LOG_ERR, r,
"Failed to get PAM systemd.existing data: @PAMERR@");
(void) close_osc_context(handle);
(void) close_osc_context(handle, debug);
id = pam_getenv(handle, "XDG_SESSION_ID");
if (id && !existing) {

View File

@@ -75,7 +75,7 @@ int pam_syslog_errno(pam_handle_t *handle, int level, int error, const char *for
LOCAL_ERRNO(error);
va_start(ap, format);
sym_pam_vsyslog(handle, LOG_ERR, format, ap);
sym_pam_vsyslog(handle, level, format, ap);
va_end(ap);
return error == -ENOMEM ? PAM_BUF_ERR : PAM_SERVICE_ERR;

View File

@@ -35,13 +35,20 @@ int pam_syslog_errno(pam_handle_t *handle, int level, int error, const char *for
int pam_syslog_pam_error(pam_handle_t *handle, int level, int error, const char *format, ...) _printf_(4,5);
/* Call pam_vsyslog if debug is enabled */
/* Call sym_pam_syslog if debug is enabled */
#define pam_debug_syslog(handle, debug, fmt, ...) \
({ \
if (debug) \
sym_pam_syslog(handle, LOG_DEBUG, fmt, ## __VA_ARGS__); \
})
/* Call pam_syslog_errno if debug is enabled */
#define pam_debug_syslog_errno(handle, debug, error, fmt, ...) \
({ \
if (debug) \
pam_syslog_errno(handle, LOG_DEBUG, error, fmt, ## __VA_ARGS__); \
})
static inline int pam_log_oom(pam_handle_t *handle) {
/* This is like log_oom(), but uses PAM logging */
return pam_syslog_errno(handle, LOG_ERR, ENOMEM, "Out of memory.");