sd-varlink: replace manual move_fd() with FORK_PACK_FDS + FORK_CLOEXEC_OFF

Note that this actually matters: we might otherwise clobber
the logging fds reopened when rearranging fd '3'.

While at it, avoid logging from library functions.
This commit is contained in:
Mike Yuan
2026-02-16 09:44:56 +01:00
parent 122bde2c45
commit 9257690859
+4 -12
View File
@@ -247,28 +247,20 @@ _public_ int sd_varlink_connect_exec(sd_varlink **ret, const char *_command, cha
/* stdio_fds= */ NULL,
/* except_fds= */ (int[]) { pair[1] },
/* n_except_fds= */ 1,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_PACK_FDS|FORK_CLOEXEC_OFF|FORK_REOPEN_LOG|FORK_DEATHSIG_SIGTERM|FORK_RLIMIT_NOFILE_SAFE,
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");
if (r == 0) {
char spid[DECIMAL_STR_MAX(pid_t)+1];
const char *setenv_list[] = {
"LISTEN_FDS", "1",
"LISTEN_PID", spid,
"LISTEN_FDS", "1",
"LISTEN_FDNAMES", "varlink",
NULL, NULL,
};
/* Child */
pair[0] = -EBADF;
r = move_fd(pair[1], 3, /* cloexec= */ false);
if (r < 0) {
log_debug_errno(r, "Failed to move file descriptor to 3: %m");
_exit(EXIT_FAILURE);
}
xsprintf(spid, PID_FMT, pidref.pid);
uint64_t pidfdid;
@@ -364,7 +356,7 @@ static int varlink_connect_ssh_unix(sd_varlink **ret, const char *where) {
/* stdio_fds= */ (int[]) { pair[1], pair[1], STDERR_FILENO },
/* except_fds= */ NULL,
/* n_except_fds= */ 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");
@@ -448,7 +440,7 @@ static int varlink_connect_ssh_exec(sd_varlink **ret, const char *where) {
/* stdio_fds= */ (int[]) { input_pipe[0], output_pipe[1], STDERR_FILENO },
/* except_fds= */ NULL,
/* n_except_fds= */ 0,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_RLIMIT_NOFILE_SAFE|FORK_REARRANGE_STDIO,
&pidref);
if (r < 0)
return log_debug_errno(r, "Failed to spawn process: %m");