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) {
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;
}