From: Yu Watanabe Date: Mon, 17 Mar 2025 01:36:33 +0000 (+0900) Subject: getty-generator: unify add_serial_getty() and add_container_getty() X-Git-Tag: v258-rc1~1057 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=910dcd7049de8b0f86b382e9598adce8bcf63540;p=thirdparty%2Fsystemd.git getty-generator: unify add_serial_getty() and add_container_getty() 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. --- diff --git a/src/getty-generator/getty-generator.c b/src/getty-generator/getty-generator.c index a10f2784417..10e50f20ad8 100644 --- a/src/getty-generator/getty-generator.c +++ b/src/getty-generator/getty-generator.c @@ -25,41 +25,38 @@ 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; }