]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
getty-generator: unify add_serial_getty() and add_container_getty()
authorYu Watanabe <watanabe.yu+github@gmail.com>
Mon, 17 Mar 2025 01:36:33 +0000 (10:36 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 18 Mar 2025 09:25:10 +0000 (18:25 +0900)
This also makes the generator not trigger an assertion added by
1cd3c49d09bf78a2a2e4cf25cb3d388e1f08a709. If getty.ttys.container
credential contains a line prefixed with '/dev/', then the assertion
    assert(!path_startswith(tty, "/dev/"));
was triggered. This drops the offending assertion, and such lines
are handled gracefully now.

Also, an empty string, "/dev/", and "/dev/pts/" (that is, a directory
without tty name) are gracefully skipped now.

src/getty-generator/getty-generator.c

index a10f27844171920dc81b95d47298bf4cb8461192..10e50f20ad8cc30ea44d4e596203756beceda767 100644 (file)
 static const char *arg_dest = NULL;
 static bool arg_enabled = true;
 
-static int add_serial_getty(const char *tty) {
-        _cleanup_free_ char *instance = NULL;
+static int add_getty_impl(const char *tty, const char *type, const char *unit_path) {
         int r;
 
-        assert(tty);
-
-        tty = skip_dev_prefix(tty);
+        assert(type);
+        assert(unit_path);
 
-        log_debug("Automatically adding serial getty for /dev/%s.", tty);
+        if (!filename_is_valid(tty)) {
+                log_debug("Invalid %s tty device specified, ignoring: %s", type, tty);
+                return 0;
+        }
 
+        _cleanup_free_ char *instance = NULL;
         r = unit_name_path_escape(tty, &instance);
         if (r < 0)
-                return log_error_errno(r, "Failed to escape tty path: %m");
-
-        return generator_add_symlink_full(arg_dest,
-                                          "getty.target", "wants",
-                                          SYSTEM_DATA_UNIT_DIR "/serial-getty@.service", instance);
-}
+                return log_error_errno(r, "Failed to escape %s tty path %s: %m", type, tty);
 
-static int add_container_getty(const char *tty) {
-        _cleanup_free_ char *instance = NULL;
-        int r;
+        log_debug("Automatically adding %s getty for %s.", type, tty);
 
-        assert(tty);
-        assert(!path_startswith(tty, "/dev/"));
+        return generator_add_symlink_full(arg_dest, "getty.target", "wants", unit_path, instance);
+}
 
-        log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
+static int add_serial_getty(const char *tty) {
+        tty = skip_dev_prefix(ASSERT_PTR(tty));
+        return add_getty_impl(tty, "serial", SYSTEM_DATA_UNIT_DIR "/serial-getty@.service");
+}
 
-        r = unit_name_path_escape(tty, &instance);
-        if (r < 0)
-                return log_error_errno(r, "Failed to escape tty path: %m");
+static int add_container_getty(const char *tty) {
+        if (is_path(tty))
+                /* Check if it is actually a pty. */
+                tty = path_startswith(skip_dev_prefix(tty), "pts/");
 
-        return generator_add_symlink_full(arg_dest,
-                                          "getty.target", "wants",
-                                          SYSTEM_DATA_UNIT_DIR "/container-getty@.service", instance);
+        return add_getty_impl(tty, "container", SYSTEM_DATA_UNIT_DIR "/container-getty@.service");
 }
 
 static int verify_tty(const char *path) {
@@ -104,15 +101,12 @@ static int run_container(void) {
                 if (r == 0)
                         return 0;
 
-                /* First strip off /dev/ if it is specified */
-                const char *tty = skip_dev_prefix(word);
-
-                /* Then, make sure it's actually a pty */
-                tty = path_startswith(tty, "pts/");
-                if (!tty)
+                /* add_container_getty() also accepts a filename, but here we request that the string
+                 * contains "pts/". */
+                if (!is_path(word))
                         continue;
 
-                r = add_container_getty(tty);
+                r = add_container_getty(word);
                 if (r < 0)
                         return r;
         }