]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
8 #include "errno-util.h"
11 #include "generator.h"
13 #include "mkdir-label.h"
14 #include "parse-util.h"
15 #include "path-util.h"
16 #include "process-util.h"
17 #include "proc-cmdline.h"
19 #include "terminal-util.h"
20 #include "unit-name.h"
23 static const char *arg_dest
= NULL
;
24 static bool arg_enabled
= true;
26 static int add_symlink(const char *fservice
, const char *tservice
) {
33 from
= strjoina(SYSTEM_DATA_UNIT_DIR
"/", fservice
);
34 to
= strjoina(arg_dest
, "/getty.target.wants/", tservice
);
36 (void) mkdir_parents_label(to
, 0755);
38 r
= symlink(from
, to
);
40 /* In case console=hvc0 is passed this will very likely result in EEXIST */
44 return log_error_errno(errno
, "Failed to create symlink %s: %m", to
);
50 static int add_serial_getty(const char *tty
) {
51 _cleanup_free_
char *n
= NULL
;
56 log_debug("Automatically adding serial getty for /dev/%s.", tty
);
58 r
= unit_name_from_path_instance("serial-getty", tty
, ".service", &n
);
60 return log_error_errno(r
, "Failed to generate service name: %m");
62 return add_symlink("serial-getty@.service", n
);
65 static int add_container_getty(const char *tty
) {
66 _cleanup_free_
char *n
= NULL
;
71 log_debug("Automatically adding container getty for /dev/pts/%s.", tty
);
73 r
= unit_name_from_path_instance("container-getty", tty
, ".service", &n
);
75 return log_error_errno(r
, "Failed to generate service name: %m");
77 return add_symlink("container-getty@.service", n
);
80 static int verify_tty(const char *name
) {
81 _cleanup_close_
int fd
= -EBADF
;
84 /* Some TTYs are weird and have been enumerated but don't work
85 * when you try to use them, such as classic ttyS0 and
86 * friends. Let's check that and open the device and run
89 p
= strjoina("/dev/", name
);
91 /* O_NONBLOCK is essential here, to make sure we don't wait
93 fd
= open(p
, O_RDWR
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
|O_NOFOLLOW
);
99 return errno_or_else(EIO
);
104 static int run_container(void) {
105 _cleanup_free_
char *container_ttys
= NULL
;
108 log_debug("Automatically adding console shell.");
110 r
= add_symlink("console-getty.service", "console-getty.service");
114 /* When $container_ttys is set for PID 1, spawn gettys on all ptys named therein.
115 * Note that despite the variable name we only support ptys here. */
117 (void) getenv_for_pid(1, "container_ttys", &container_ttys
);
119 for (const char *p
= container_ttys
;;) {
120 _cleanup_free_
char *word
= NULL
;
122 r
= extract_first_word(&p
, &word
, NULL
, 0);
124 return log_error_errno(r
, "Failed to parse $container_ttys: %m");
128 const char *tty
= word
;
130 /* First strip off /dev/ if it is specified */
131 tty
= path_startswith(tty
, "/dev/") ?: tty
;
133 /* Then, make sure it's actually a pty */
134 tty
= path_startswith(tty
, "pts/");
138 r
= add_container_getty(tty
);
144 static int parse_proc_cmdline_item(const char *key
, const char *value
, void *data
) {
149 if (proc_cmdline_key_streq(key
, "systemd.getty_auto")) {
150 r
= value
? parse_boolean(value
) : 1;
152 log_warning_errno(r
, "Failed to parse getty_auto switch \"%s\", ignoring: %m", value
);
160 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
161 _cleanup_free_
char *getty_auto
= NULL
;
164 assert_se(arg_dest
= dest
);
166 r
= proc_cmdline_parse(parse_proc_cmdline_item
, NULL
, 0);
168 log_warning_errno(r
, "Failed to parse kernel command line, ignoring: %m");
170 r
= getenv_for_pid(1, "SYSTEMD_GETTY_AUTO", &getty_auto
);
172 log_warning_errno(r
, "Failed to parse $SYSTEMD_GETTY_AUTO environment variable, ignoring: %m");
174 r
= parse_boolean(getty_auto
);
176 log_warning_errno(r
, "Failed to parse $SYSTEMD_GETTY_AUTO value \"%s\", ignoring: %m", getty_auto
);
182 log_debug("Disabled, exiting.");
186 if (detect_container() > 0)
187 /* Add console shell and look at $container_ttys, but don't do add any
188 * further magic if we are in a container. */
189 return run_container();
191 /* Automatically add in a serial getty on all active kernel consoles */
192 _cleanup_free_
char *active
= NULL
;
193 (void) read_one_line_file("/sys/class/tty/console/active", &active
);
194 for (const char *p
= active
;;) {
195 _cleanup_free_
char *tty
= NULL
;
197 r
= extract_first_word(&p
, &tty
, NULL
, 0);
199 return log_error_errno(r
, "Failed to parse /sys/class/tty/console/active: %m");
203 /* We assume that gettys on virtual terminals are started via manual configuration and do
204 * this magic only for non-VC terminals. */
206 if (isempty(tty
) || tty_is_vc(tty
))
209 if (verify_tty(tty
) < 0)
212 r
= add_serial_getty(tty
);
217 /* Automatically add in a serial getty on the first virtualizer console */
225 _cleanup_free_
char *p
= NULL
;
227 p
= path_join("/sys/class/tty", j
);
230 if (access(p
, F_OK
) < 0)
233 r
= add_serial_getty(j
);
241 DEFINE_MAIN_GENERATOR_FUNCTION(run
);