]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tty-ask-password-agent: do not needlessly spawn subprocesses if there's only one...
authorMike Yuan <me@yhndnzj.com>
Sat, 25 Jan 2025 19:30:10 +0000 (20:30 +0100)
committerMike Yuan <me@yhndnzj.com>
Sun, 26 Jan 2025 03:29:56 +0000 (04:29 +0100)
src/tty-ask-password-agent/tty-ask-password-agent.c

index 5ff892c25df6d5ca1afd51b217cbb869e35b4bc7..1c1b95bef3cf813721036ac19e9a2353e05a7593 100644 (file)
@@ -40,6 +40,7 @@
 #include "set.h"
 #include "signal-util.h"
 #include "socket-util.h"
+#include "static-destruct.h"
 #include "string-util.h"
 #include "strv.h"
 #include "terminal-util.h"
@@ -54,7 +55,9 @@ static enum {
 
 static bool arg_plymouth = false;
 static bool arg_console = false;
-static const char *arg_device = NULL;
+static char *arg_device = NULL;
+
+STATIC_DESTRUCTOR_REGISTER(arg_device, freep);
 
 static int send_passwords(const char *socket_name, char **passwords) {
         int r;
@@ -530,12 +533,13 @@ static int parse_argv(int argc, char *argv[]) {
                 case ARG_CONSOLE:
                         arg_console = true;
                         if (optarg) {
-
                                 if (isempty(optarg))
                                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
                                                                "Empty console device path is not allowed.");
 
-                                arg_device = optarg;
+                                arg_device = strdup(optarg);
+                                if (!arg_device)
+                                        return log_oom();
                         }
                         break;
 
@@ -667,6 +671,11 @@ static int ask_on_consoles(char *argv[]) {
         r = get_kernel_consoles(&consoles);
         if (r < 0)
                 return log_error_errno(r, "Failed to determine devices of /dev/console: %m");
+        if (r <= 1) {
+                /* No need to spawn subprocesses, there's only one console or using /dev/console as fallback */
+                arg_device = TAKE_PTR(consoles[0]);
+                return 0;
+        }
 
         pids = set_new(NULL);
         if (!pids)
@@ -714,7 +723,7 @@ static int ask_on_consoles(char *argv[]) {
         }
 
         terminate_agents(pids);
-        return 0;
+        return 1;
 }
 
 static int run(int argc, char *argv[]) {
@@ -728,11 +737,14 @@ static int run(int argc, char *argv[]) {
         if (r <= 0)
                 return r;
 
-        if (arg_console && !arg_device)
-                /*
-                 * Spawn a separate process for each console device.
-                 */
-                return ask_on_consoles(argv);
+        /* Spawn a separate process for each console device if there're multiple. */
+        if (arg_console && !arg_device) {
+                r = ask_on_consoles(argv);
+                if (r != 0)
+                        return r;
+
+                assert(arg_device);
+        }
 
         if (arg_device)
                 /* Later on, a controlling terminal will be acquired, therefore the current process has to