]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-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
> 0 ? -errno
: -EIO
;
102 static int run(const char *dest
, const char *dest_early
, const char *dest_late
) {
103 _cleanup_free_
char *active
= NULL
;
107 assert_se(arg_dest
= dest
);
109 if (detect_container() > 0) {
110 _cleanup_free_
char *container_ttys
= NULL
;
112 log_debug("Automatically adding console shell.");
114 r
= add_symlink("console-getty.service", "console-getty.service");
118 /* When $container_ttys is set for PID 1, spawn
119 * gettys on all ptys named therein. Note that despite
120 * the variable name we only support ptys here. */
122 r
= getenv_for_pid(1, "container_ttys", &container_ttys
);
124 const char *word
, *state
;
127 FOREACH_WORD(word
, l
, container_ttys
, state
) {
131 memcpy(tty
, word
, l
);
134 /* First strip off /dev/ if it is specified */
135 t
= path_startswith(tty
, "/dev/");
139 /* Then, make sure it's actually a pty */
140 t
= path_startswith(t
, "pts/");
144 r
= add_container_getty(t
);
150 /* Don't add any further magic if we are in a container */
154 if (read_one_line_file("/sys/class/tty/console/active", &active
) >= 0) {
155 const char *word
, *state
;
158 /* Automatically add in a serial getty on all active
160 FOREACH_WORD(word
, l
, active
, state
) {
161 _cleanup_free_
char *tty
= NULL
;
163 tty
= strndup(word
, l
);
167 /* We assume that gettys on virtual terminals are
168 * started via manual configuration and do this magic
169 * only for non-VC terminals. */
171 if (isempty(tty
) || tty_is_vc(tty
))
174 if (verify_tty(tty
) < 0)
177 r
= add_serial_getty(tty
);
183 /* Automatically add in a serial getty on the first
184 * virtualizer console */
194 p
= strjoina("/sys/class/tty/", j
);
195 if (access(p
, F_OK
) < 0)
198 r
= add_serial_getty(j
);
206 DEFINE_MAIN_GENERATOR_FUNCTION(run
);