getty-generator: unify add_serial_getty() and add_container_getty()

This also makes the generator not trigger an assertion added by
1cd3c49d09. 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.
This commit is contained in:
Yu Watanabe
2025-03-18 18:25:10 +09:00
parent 0364329a03
commit 910dcd7049
+25 -31
View File
@@ -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);
assert(type);
assert(unit_path);
tty = skip_dev_prefix(tty);
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 log_error_errno(r, "Failed to escape %s tty path %s: %m", type, tty);
return generator_add_symlink_full(arg_dest,
"getty.target", "wants",
SYSTEM_DATA_UNIT_DIR "/serial-getty@.service", instance);
log_debug("Automatically adding %s getty for %s.", type, tty);
return generator_add_symlink_full(arg_dest, "getty.target", "wants", unit_path, instance);
}
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");
}
static int add_container_getty(const char *tty) {
_cleanup_free_ char *instance = NULL;
int r;
if (is_path(tty))
/* Check if it is actually a pty. */
tty = path_startswith(skip_dev_prefix(tty), "pts/");
assert(tty);
assert(!path_startswith(tty, "/dev/"));
log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
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 "/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;
}