Line data Source code
1 : /* SPDX-License-Identifier: LGPL-2.1+ */ 2 : 3 : #include <signal.h> 4 : #include <stdlib.h> 5 : #include <unistd.h> 6 : 7 : #include "log.h" 8 : #include "process-util.h" 9 : #include "spawn-ask-password-agent.h" 10 : #include "util.h" 11 : 12 : static pid_t agent_pid = 0; 13 : 14 0 : int ask_password_agent_open(void) { 15 : int r; 16 : 17 0 : if (agent_pid > 0) 18 0 : return 0; 19 : 20 : /* We check STDIN here, not STDOUT, since this is about input, 21 : * not output */ 22 0 : if (!isatty(STDIN_FILENO)) 23 0 : return 0; 24 : 25 0 : if (!is_main_thread()) 26 0 : return -EPERM; 27 : 28 0 : r = fork_agent("(sd-askpwagent)", 29 : NULL, 0, 30 : &agent_pid, 31 : SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, 32 : SYSTEMD_TTY_ASK_PASSWORD_AGENT_BINARY_PATH, "--watch", NULL); 33 0 : if (r < 0) 34 0 : return log_error_errno(r, "Failed to fork TTY ask password agent: %m"); 35 : 36 0 : return 1; 37 : } 38 : 39 281 : void ask_password_agent_close(void) { 40 : 41 281 : if (agent_pid <= 0) 42 281 : return; 43 : 44 : /* Inform agent that we are done */ 45 0 : (void) kill_and_sigcont(agent_pid, SIGTERM); 46 0 : (void) wait_for_terminate(agent_pid, NULL); 47 0 : agent_pid = 0; 48 : }