]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2010 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include "alloc-util.h"
31 #include "path-util.h"
32 #include "process-util.h"
33 #include "string-util.h"
34 #include "terminal-util.h"
35 #include "unit-name.h"
39 static const char *arg_dest
= "/tmp";
41 static int add_symlink(const char *fservice
, const char *tservice
) {
48 from
= strjoina(SYSTEM_DATA_UNIT_PATH
"/", fservice
);
49 to
= strjoina(arg_dest
, "/getty.target.wants/", tservice
);
51 mkdir_parents_label(to
, 0755);
53 r
= symlink(from
, to
);
55 /* In case console=hvc0 is passed this will very likely result in EEXIST */
59 return log_error_errno(errno
, "Failed to create symlink %s: %m", to
);
65 static int add_serial_getty(const char *tty
) {
66 _cleanup_free_
char *n
= NULL
;
71 log_debug("Automatically adding serial getty for /dev/%s.", tty
);
73 r
= unit_name_from_path_instance("serial-getty", tty
, ".service", &n
);
75 return log_error_errno(r
, "Failed to generate service name: %m");
77 return add_symlink("serial-getty@.service", n
);
80 static int add_container_getty(const char *tty
) {
81 _cleanup_free_
char *n
= NULL
;
86 log_debug("Automatically adding container getty for /dev/pts/%s.", tty
);
88 r
= unit_name_from_path_instance("container-getty", tty
, ".service", &n
);
90 return log_error_errno(r
, "Failed to generate service name: %m");
92 return add_symlink("container-getty@.service", n
);
95 static int verify_tty(const char *name
) {
96 _cleanup_close_
int fd
= -1;
99 /* Some TTYs are weird and have been enumerated but don't work
100 * when you try to use them, such as classic ttyS0 and
101 * friends. Let's check that and open the device and run
104 p
= strjoina("/dev/", name
);
106 /* O_NONBLOCK is essential here, to make sure we don't wait
108 fd
= open(p
, O_RDWR
|O_NONBLOCK
|O_NOCTTY
|O_CLOEXEC
|O_NOFOLLOW
);
114 return errno
> 0 ? -errno
: -EIO
;
119 int main(int argc
, char *argv
[]) {
121 static const char virtualization_consoles
[] =
129 _cleanup_free_
char *active
= NULL
;
133 if (argc
> 1 && argc
!= 4) {
134 log_error("This program takes three or no arguments.");
141 log_set_target(LOG_TARGET_SAFE
);
142 log_parse_environment();
147 if (detect_container() > 0) {
148 _cleanup_free_
char *container_ttys
= NULL
;
150 log_debug("Automatically adding console shell.");
152 if (add_symlink("console-getty.service", "console-getty.service") < 0)
155 /* When $container_ttys is set for PID 1, spawn
156 * gettys on all ptys named therein. Note that despite
157 * the variable name we only support ptys here. */
159 r
= getenv_for_pid(1, "container_ttys", &container_ttys
);
161 const char *word
, *state
;
164 FOREACH_WORD(word
, l
, container_ttys
, state
) {
168 memcpy(tty
, word
, l
);
171 /* First strip off /dev/ if it is specified */
172 t
= path_startswith(tty
, "/dev/");
176 /* Then, make sure it's actually a pty */
177 t
= path_startswith(t
, "pts/");
181 if (add_container_getty(t
) < 0)
186 /* Don't add any further magic if we are in a container */
190 if (read_one_line_file("/sys/class/tty/console/active", &active
) >= 0) {
191 const char *word
, *state
;
194 /* Automatically add in a serial getty on all active
196 FOREACH_WORD(word
, l
, active
, state
) {
197 _cleanup_free_
char *tty
= NULL
;
199 tty
= strndup(word
, l
);
205 /* We assume that gettys on virtual terminals are
206 * started via manual configuration and do this magic
207 * only for non-VC terminals. */
209 if (isempty(tty
) || tty_is_vc(tty
))
212 if (verify_tty(tty
) < 0)
215 if (add_serial_getty(tty
) < 0)
220 /* Automatically add in a serial getty on the first
221 * virtualizer console */
222 NULSTR_FOREACH(j
, virtualization_consoles
) {
225 p
= strjoina("/sys/class/tty/", j
);
226 if (access(p
, F_OK
) < 0)
229 if (add_serial_getty(j
) < 0)