1
0
mirror of https://github.com/systemd/systemd.git synced 2025-03-09 12:58:26 +03:00

exec-util: split out common checks before fork_agent() to can_fork_agent()

No functional change, just refactoring.
This commit is contained in:
Yu Watanabe 2024-12-03 18:40:06 +09:00
parent 388d6c5f37
commit fc3691a70a
4 changed files with 22 additions and 20 deletions

View File

@ -8,7 +8,6 @@
#include "exec-util.h"
#include "log.h"
#include "process-util.h"
#include "terminal-util.h"
static pid_t agent_pid = 0;
@ -18,17 +17,10 @@ int ask_password_agent_open(void) {
if (agent_pid > 0)
return 0;
/* Check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked
* interactively on a terminal, hence fail. */
r = get_ctty_devnr(0, NULL);
if (r == -ENXIO)
return 0;
if (r < 0)
r = shall_fork_agent();
if (r <= 0)
return r;
if (!is_main_thread())
return -EPERM;
r = fork_agent("(sd-askpwagent)",
NULL, 0,
&agent_pid,

View File

@ -543,6 +543,23 @@ int fexecve_or_execve(int executable_fd, const char *executable, char *const arg
return -errno;
}
int shall_fork_agent(void) {
int r;
/* Check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked
* interactively on a terminal, hence fail. */
r = get_ctty_devnr(0, NULL);
if (r == -ENXIO)
return false;
if (r < 0)
return r;
if (!is_main_thread())
return -EPERM;
return true;
}
int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) {
size_t n, i;
va_list ap;

View File

@ -61,5 +61,6 @@ ExecCommandFlags exec_command_flags_from_string(const char *s);
int fexecve_or_execve(int executable_fd, const char *executable, char *const argv[], char *const envp[]);
int shall_fork_agent(void);
int _fork_agent(const char *name, const int except[], size_t n_except, pid_t *ret_pid, const char *path, ...) _sentinel_;
#define fork_agent(name, ...) _fork_agent(name, __VA_ARGS__, NULL)

View File

@ -14,7 +14,6 @@
#include "polkit-agent.h"
#include "process-util.h"
#include "stdio-util.h"
#include "terminal-util.h"
#include "time-util.h"
#if ENABLE_POLKIT
@ -32,17 +31,10 @@ int polkit_agent_open(void) {
if (geteuid() == 0)
return 0;
/* Check if we have a controlling terminal. If not (ENXIO here), we aren't actually invoked
* interactively on a terminal, hence fail. */
r = get_ctty_devnr(0, NULL);
if (r == -ENXIO)
return 0;
if (r < 0)
r = shall_fork_agent();
if (r <= 0)
return r;
if (!is_main_thread())
return -EPERM;
if (pipe2(pipe_fd, 0) < 0)
return -errno;