]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
04dcacfd59a476b324fb0f8089e47b5c9c74f495
1 /* SPDX-License-Identifier: LGPL-2.1+ */
7 #include "alloc-util.h"
8 #include "errno-util.h"
11 #include "generator.h"
14 #include "path-util.h"
15 #include "process-util.h"
17 #include "terminal-util.h"
18 #include "unit-name.h"
22 static const char *arg_dest
= NULL
;
24 static int add_symlink(const char *fservice
, const char *tservice
) {
31 from
= strjoina(SYSTEM_DATA_UNIT_PATH
"/", fservice
);
32 to
= strjoina(arg_dest
, "/getty.target.wants/", tservice
);
34 mkdir_parents_label(to
, 0755);
36 r
= symlink(from
, to
);
38 /* In case console=hvc0 is passed this will very likely result in EEXIST */
42 return log_error_errno(errno
, "Failed to create symlink %s: %m", to
);
48 static int add_serial_getty(const char *tty
) {
49 _cleanup_free_
char *n
= NULL
;
54 log_debug("Automatically adding serial getty for /dev/%s.", tty
);
56 r
= unit_name_from_path_instance("serial-getty", tty
, ".service", &n
);
58 return log_error_errno(r
, "Failed to generate service name: %m");
60 return add_symlink("serial-getty@.service", n
);
63 static int add_container_getty(const char *tty
) {
64 _cleanup_free_
char *n
= NULL
;
69 log_debug("Automatically adding container getty for /dev/pts/%s.", tty
);
71 r
= unit_name_from_path_instance("container-getty", tty
, ".service", &n
);
73 return log_error_errno(r
, "Failed to generate service name: %m");
75 return add_symlink("container-getty@.service", n
);
78 static int verify_tty(const char *name
) {
79 _cleanup_close_
int fd
= -1;
82 /* Some TTYs are weird and have been enumerated but don't work
83 * when you try to use them, such as classic ttyS0 and
84 * friends. Let's check that and open the device and run
87 p
= strjoina("/dev/", name
);
89 /* O_NONBLOCK is essential here, to make sure we don't wait
91 fd
= open(p
, O_RDWR
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
|O_NOFOLLOW
);
97 return errno_or_else(EIO
);
102 static int run_container(void) {
103 _cleanup_free_
char *container_ttys
= NULL
;
106 log_debug("Automatically adding console shell.");
108 r
= add_symlink("console-getty.service", "console-getty.service");
112 /* When $container_ttys is set for PID 1, spawn gettys on all ptys named therein.
113 * Note that despite the variable name we only support ptys here. */
115 (void) getenv_for_pid(1, "container_ttys", &container_ttys
);
117 for (const char *p
= container_ttys
;;) {
118 _cleanup_free_
char *word
= NULL
;
120 r
= extract_first_word(&p
, &word
, NULL
, 0);
122 return log_error_errno(r
, "Failed to parse $container_ttys: %m");
126 const char *tty
= word
;
128 /* First strip off /dev/ if it is specified */
129 tty
= path_startswith(tty
, "/dev/") ?: tty
;
131 /* Then, make sure it's actually a pty */
132 tty
= path_startswith(tty
, "pts/");
136 r
= add_container_getty(tty
);
142 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
145 assert_se(arg_dest
= dest
);
147 if (detect_container() > 0)
148 /* Add console shell and look at $container_ttys, but don't do add any
149 * further magic if we are in a container. */
150 return run_container();
152 /* Automatically add in a serial getty on all active kernel consoles */
153 _cleanup_free_
char *active
= NULL
;
154 (void) read_one_line_file("/sys/class/tty/console/active", &active
);
155 for (const char *p
= active
;;) {
156 _cleanup_free_
char *tty
= NULL
;
158 r
= extract_first_word(&p
, &tty
, NULL
, 0);
160 return log_error_errno(r
, "Failed to parse /sys/class/tty/console/active: %m");
164 /* We assume that gettys on virtual terminals are started via manual configuration and do
165 * this magic only for non-VC terminals. */
167 if (isempty(tty
) || tty_is_vc(tty
))
170 if (verify_tty(tty
) < 0)
173 r
= add_serial_getty(tty
);
178 /* Automatically add in a serial getty on the first
179 * virtualizer console */
188 _cleanup_free_
char *p
= NULL
;
190 p
= path_join("/sys/class/tty", j
);
193 if (access(p
, F_OK
) < 0)
196 r
= add_serial_getty(j
);
204 DEFINE_MAIN_GENERATOR_FUNCTION(run
);