2020-11-09 05:23:58 +01:00
|
|
|
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
2012-04-11 18:50:16 +02:00
|
|
|
|
2015-10-06 16:27:24 +02:00
|
|
|
#include <signal.h>
|
2012-04-11 18:50:16 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "log.h"
|
2015-04-10 19:10:00 +02:00
|
|
|
#include "process-util.h"
|
2012-04-11 18:50:16 +02:00
|
|
|
#include "spawn-ask-password-agent.h"
|
2015-11-16 22:09:36 +01:00
|
|
|
#include "util.h"
|
2012-04-11 18:50:16 +02:00
|
|
|
|
|
|
|
static pid_t agent_pid = 0;
|
|
|
|
|
|
|
|
int ask_password_agent_open(void) {
|
|
|
|
int r;
|
|
|
|
|
|
|
|
if (agent_pid > 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* We check STDIN here, not STDOUT, since this is about input,
|
|
|
|
* not output */
|
|
|
|
if (!isatty(STDIN_FILENO))
|
|
|
|
return 0;
|
|
|
|
|
2017-12-29 18:13:38 +01:00
|
|
|
if (!is_main_thread())
|
|
|
|
return -EPERM;
|
|
|
|
|
2017-12-22 15:07:22 +01:00
|
|
|
r = fork_agent("(sd-askpwagent)",
|
2012-04-11 22:37:13 +02:00
|
|
|
NULL, 0,
|
2017-12-22 15:07:22 +01:00
|
|
|
&agent_pid,
|
2012-04-11 22:37:13 +02:00
|
|
|
SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH,
|
|
|
|
SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL);
|
2012-04-11 18:50:16 +02:00
|
|
|
if (r < 0)
|
2015-10-06 16:27:24 +02:00
|
|
|
return log_error_errno(r, "Failed to fork TTY ask password agent: %m");
|
2012-04-11 18:50:16 +02:00
|
|
|
|
2015-10-06 16:27:24 +02:00
|
|
|
return 1;
|
2012-04-11 18:50:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void ask_password_agent_close(void) {
|
|
|
|
|
|
|
|
if (agent_pid <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Inform agent that we are done */
|
2017-12-22 15:10:56 +01:00
|
|
|
(void) kill_and_sigcont(agent_pid, SIGTERM);
|
2014-11-09 15:51:04 +01:00
|
|
|
(void) wait_for_terminate(agent_pid, NULL);
|
2012-04-11 18:50:16 +02:00
|
|
|
agent_pid = 0;
|
|
|
|
}
|
2019-11-15 11:11:52 +01:00
|
|
|
|
|
|
|
int ask_password_agent_open_if_enabled(BusTransport transport, bool ask_password) {
|
|
|
|
|
|
|
|
/* Open the ask password agent as a child process if necessary */
|
|
|
|
|
|
|
|
if (transport != BUS_TRANSPORT_LOCAL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!ask_password)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return ask_password_agent_open();
|
|
|
|
}
|