summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/basic/process-util.c71
-rw-r--r--src/basic/process-util.h2
-rw-r--r--src/basic/util.c74
-rw-r--r--src/basic/util.h2
-rw-r--r--src/shared/spawn-ask-password-agent.c3
-rw-r--r--src/shared/spawn-polkit-agent.c9
6 files changed, 79 insertions, 82 deletions
diff --git a/src/basic/process-util.c b/src/basic/process-util.c
index 01c73cfa64..f91e2a49ce 100644
--- a/src/basic/process-util.c
+++ b/src/basic/process-util.c
@@ -1271,6 +1271,77 @@ int safe_fork_full(
return 0;
}
+int fork_agent(const char *name, const int except[], unsigned n_except, pid_t *ret_pid, const char *path, ...) {
+ bool stdout_is_tty, stderr_is_tty;
+ unsigned n, i;
+ va_list ap;
+ char **l;
+ int r;
+
+ assert(path);
+
+ /* Spawns a temporary TTY agent, making sure it goes away when we go away */
+
+ r = safe_fork_full(name, except, n_except, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, ret_pid);
+ if (r < 0)
+ return r;
+ if (r > 0)
+ return 0;
+
+ /* In the child: */
+
+ stdout_is_tty = isatty(STDOUT_FILENO);
+ stderr_is_tty = isatty(STDERR_FILENO);
+
+ if (!stdout_is_tty || !stderr_is_tty) {
+ int fd;
+
+ /* Detach from stdout/stderr. and reopen
+ * /dev/tty for them. This is important to
+ * ensure that when systemctl is started via
+ * popen() or a similar call that expects to
+ * read EOF we actually do generate EOF and
+ * not delay this indefinitely by because we
+ * keep an unused copy of stdin around. */
+ fd = open("/dev/tty", O_WRONLY);
+ if (fd < 0) {
+ log_error_errno(errno, "Failed to open /dev/tty: %m");
+ _exit(EXIT_FAILURE);
+ }
+
+ if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
+ log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+ _exit(EXIT_FAILURE);
+ }
+
+ if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
+ log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
+ _exit(EXIT_FAILURE);
+ }
+
+ if (fd > STDERR_FILENO)
+ close(fd);
+ }
+
+ /* Count arguments */
+ va_start(ap, path);
+ for (n = 0; va_arg(ap, char*); n++)
+ ;
+ va_end(ap);
+
+ /* Allocate strv */
+ l = alloca(sizeof(char *) * (n + 1));
+
+ /* Fill in arguments */
+ va_start(ap, path);
+ for (i = 0; i <= n; i++)
+ l[i] = va_arg(ap, char*);
+ va_end(ap);
+
+ execv(path, l);
+ _exit(EXIT_FAILURE);
+}
+
static const char *const ioprio_class_table[] = {
[IOPRIO_CLASS_NONE] = "none",
[IOPRIO_CLASS_RT] = "realtime",
diff --git a/src/basic/process-util.h b/src/basic/process-util.h
index 0ab1eecd90..1dd62c6d0a 100644
--- a/src/basic/process-util.h
+++ b/src/basic/process-util.h
@@ -156,3 +156,5 @@ int safe_fork_full(const char *name, const int except_fds[], size_t n_except_fds
static inline int safe_fork(const char *name, ForkFlags flags, pid_t *ret_pid) {
return safe_fork_full(name, NULL, 0, flags, ret_pid);
}
+
+int fork_agent(const char *name, const int except[], unsigned n_except, pid_t *pid, const char *path, ...);
diff --git a/src/basic/util.c b/src/basic/util.c
index 8e431877d0..53bfb9e0da 100644
--- a/src/basic/util.c
+++ b/src/basic/util.c
@@ -184,80 +184,6 @@ int prot_from_flags(int flags) {
}
}
-int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...) {
- bool stdout_is_tty, stderr_is_tty;
- pid_t agent_pid;
- unsigned n, i;
- va_list ap;
- char **l;
- int r;
-
- assert(pid);
- assert(path);
-
- /* Spawns a temporary TTY agent, making sure it goes away when
- * we go away */
-
- r = safe_fork_full("(sd-agent)", except, n_except, FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_CLOSE_ALL_FDS, &agent_pid);
- if (r < 0)
- return r;
- if (r > 0)
- return 0;
-
- /* In the child: */
-
- stdout_is_tty = isatty(STDOUT_FILENO);
- stderr_is_tty = isatty(STDERR_FILENO);
-
- if (!stdout_is_tty || !stderr_is_tty) {
- int fd;
-
- /* Detach from stdout/stderr. and reopen
- * /dev/tty for them. This is important to
- * ensure that when systemctl is started via
- * popen() or a similar call that expects to
- * read EOF we actually do generate EOF and
- * not delay this indefinitely by because we
- * keep an unused copy of stdin around. */
- fd = open("/dev/tty", O_WRONLY);
- if (fd < 0) {
- log_error_errno(errno, "Failed to open /dev/tty: %m");
- _exit(EXIT_FAILURE);
- }
-
- if (!stdout_is_tty && dup2(fd, STDOUT_FILENO) < 0) {
- log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
- _exit(EXIT_FAILURE);
- }
-
- if (!stderr_is_tty && dup2(fd, STDERR_FILENO) < 0) {
- log_error_errno(errno, "Failed to dup2 /dev/tty: %m");
- _exit(EXIT_FAILURE);
- }
-
- if (fd > STDERR_FILENO)
- close(fd);
- }
-
- /* Count arguments */
- va_start(ap, path);
- for (n = 0; va_arg(ap, char*); n++)
- ;
- va_end(ap);
-
- /* Allocate strv */
- l = alloca(sizeof(char *) * (n + 1));
-
- /* Fill in arguments */
- va_start(ap, path);
- for (i = 0; i <= n; i++)
- l[i] = va_arg(ap, char*);
- va_end(ap);
-
- execv(path, l);
- _exit(EXIT_FAILURE);
-}
-
bool in_initrd(void) {
struct statfs s;
diff --git a/src/basic/util.h b/src/basic/util.h
index a79907de3e..62bf1a6961 100644
--- a/src/basic/util.h
+++ b/src/basic/util.h
@@ -86,8 +86,6 @@ bool kexec_loaded(void);
int prot_from_flags(int flags) _const_;
-int fork_agent(pid_t *pid, const int except[], unsigned n_except, const char *path, ...);
-
bool in_initrd(void);
void in_initrd_force(bool value);
diff --git a/src/shared/spawn-ask-password-agent.c b/src/shared/spawn-ask-password-agent.c
index 9af5faa3dd..a99c467db3 100644
--- a/src/shared/spawn-ask-password-agent.c
+++ b/src/shared/spawn-ask-password-agent.c
@@ -40,8 +40,9 @@ int ask_password_agent_open(void) {
if (!isatty(STDIN_FILENO))
return 0;
- r = fork_agent(&agent_pid,
+ r = fork_agent("(sd-askpwagent)",
NULL, 0,
+ &agent_pid,
SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
if (r < 0)
diff --git a/src/shared/spawn-polkit-agent.c b/src/shared/spawn-polkit-agent.c
index 423069fb0e..c6bd65eb71 100644
--- a/src/shared/spawn-polkit-agent.c
+++ b/src/shared/spawn-polkit-agent.c
@@ -38,9 +38,8 @@
static pid_t agent_pid = 0;
int polkit_agent_open(void) {
- int r;
- int pipe_fd[2];
char notify_fd[DECIMAL_STR_MAX(int) + 1];
+ int pipe_fd[2], r;
if (agent_pid > 0)
return 0;
@@ -49,8 +48,7 @@ int polkit_agent_open(void) {
if (geteuid() == 0)
return 0;
- /* We check STDIN here, not STDOUT, since this is about input,
- * not output */
+ /* We check STDIN here, not STDOUT, since this is about input, not output */
if (!isatty(STDIN_FILENO))
return 0;
@@ -59,8 +57,9 @@ int polkit_agent_open(void) {
xsprintf(notify_fd, "%i", pipe_fd[1]);
- r = fork_agent(&agent_pid,
+ r = fork_agent("(polkit-agent)",
&pipe_fd[1], 1,
+ &agent_pid,
POLKIT_AGENT_BINARY_PATH,
POLKIT_AGENT_BINARY_PATH, "--notify-fd", notify_fd, "--fallback", NULL);