]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <string.h>
6 #include <unistd.h>
7
8 #include "alloc-util.h"
9 #include "fd-util.h"
10 #include "fileio.h"
11 #include "generator.h"
12 #include "log.h"
13 #include "mkdir.h"
14 #include "path-util.h"
15 #include "process-util.h"
16 #include "strv.h"
17 #include "terminal-util.h"
18 #include "unit-name.h"
19 #include "util.h"
20 #include "virt.h"
21
22 static const char *arg_dest = NULL;
23
24 static int add_symlink(const char *fservice, const char *tservice) {
25 char *from, *to;
26 int r;
27
28 assert(fservice);
29 assert(tservice);
30
31 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", fservice);
32 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
33
34 mkdir_parents_label(to, 0755);
35
36 r = symlink(from, to);
37 if (r < 0) {
38 /* In case console=hvc0 is passed this will very likely result in EEXIST */
39 if (errno == EEXIST)
40 return 0;
41
42 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
43 }
44
45 return 0;
46 }
47
48 static int add_serial_getty(const char *tty) {
49 _cleanup_free_ char *n = NULL;
50 int r;
51
52 assert(tty);
53
54 log_debug("Automatically adding serial getty for /dev/%s.", tty);
55
56 r = unit_name_from_path_instance("serial-getty", tty, ".service", &n);
57 if (r < 0)
58 return log_error_errno(r, "Failed to generate service name: %m");
59
60 return add_symlink("serial-getty@.service", n);
61 }
62
63 static int add_container_getty(const char *tty) {
64 _cleanup_free_ char *n = NULL;
65 int r;
66
67 assert(tty);
68
69 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
70
71 r = unit_name_from_path_instance("container-getty", tty, ".service", &n);
72 if (r < 0)
73 return log_error_errno(r, "Failed to generate service name: %m");
74
75 return add_symlink("container-getty@.service", n);
76 }
77
78 static int verify_tty(const char *name) {
79 _cleanup_close_ int fd = -1;
80 const char *p;
81
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
85 * isatty() on it. */
86
87 p = strjoina("/dev/", name);
88
89 /* O_NONBLOCK is essential here, to make sure we don't wait
90 * for DCD */
91 fd = open(p, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW);
92 if (fd < 0)
93 return -errno;
94
95 errno = 0;
96 if (isatty(fd) <= 0)
97 return errno > 0 ? -errno : -EIO;
98
99 return 0;
100 }
101
102 static int run(const char *dest, const char *dest_early, const char *dest_late) {
103 _cleanup_free_ char *active = NULL;
104 const char *j;
105 int r;
106
107 assert_se(arg_dest = dest);
108
109 if (detect_container() > 0) {
110 _cleanup_free_ char *container_ttys = NULL;
111
112 log_debug("Automatically adding console shell.");
113
114 r = add_symlink("console-getty.service", "console-getty.service");
115 if (r < 0)
116 return r;
117
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. */
121
122 r = getenv_for_pid(1, "container_ttys", &container_ttys);
123 if (r > 0) {
124 const char *word, *state;
125 size_t l;
126
127 FOREACH_WORD(word, l, container_ttys, state) {
128 const char *t;
129 char tty[l + 1];
130
131 memcpy(tty, word, l);
132 tty[l] = 0;
133
134 /* First strip off /dev/ if it is specified */
135 t = path_startswith(tty, "/dev/");
136 if (!t)
137 t = tty;
138
139 /* Then, make sure it's actually a pty */
140 t = path_startswith(t, "pts/");
141 if (!t)
142 continue;
143
144 r = add_container_getty(t);
145 if (r < 0)
146 return r;
147 }
148 }
149
150 /* Don't add any further magic if we are in a container */
151 return 0;
152 }
153
154 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
155 const char *word, *state;
156 size_t l;
157
158 /* Automatically add in a serial getty on all active
159 * kernel consoles */
160 FOREACH_WORD(word, l, active, state) {
161 _cleanup_free_ char *tty = NULL;
162
163 tty = strndup(word, l);
164 if (!tty)
165 return log_oom();
166
167 /* We assume that gettys on virtual terminals are
168 * started via manual configuration and do this magic
169 * only for non-VC terminals. */
170
171 if (isempty(tty) || tty_is_vc(tty))
172 continue;
173
174 if (verify_tty(tty) < 0)
175 continue;
176
177 r = add_serial_getty(tty);
178 if (r < 0)
179 return r;
180 }
181 }
182
183 /* Automatically add in a serial getty on the first
184 * virtualizer console */
185 FOREACH_STRING(j,
186 "hvc0",
187 "xvc0",
188 "hvsi0",
189 "sclp_line0",
190 "ttysclp0",
191 "3270!tty1") {
192 const char *p;
193
194 p = strjoina("/sys/class/tty/", j);
195 if (access(p, F_OK) < 0)
196 continue;
197
198 r = add_serial_getty(j);
199 if (r < 0)
200 return r;
201 }
202
203 return 0;
204 }
205
206 DEFINE_MAIN_GENERATOR_FUNCTION(run);