]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 This file is part of systemd.
4
5 Copyright 2010 Lennart Poettering
6
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.
11
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.
16
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/>.
19 ***/
20
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "fd-util.h"
28 #include "fileio.h"
29 #include "log.h"
30 #include "mkdir.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"
36 #include "util.h"
37 #include "virt.h"
38
39 static const char *arg_dest = "/tmp";
40
41 static int add_symlink(const char *fservice, const char *tservice) {
42 char *from, *to;
43 int r;
44
45 assert(fservice);
46 assert(tservice);
47
48 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", fservice);
49 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
50
51 mkdir_parents_label(to, 0755);
52
53 r = symlink(from, to);
54 if (r < 0) {
55 /* In case console=hvc0 is passed this will very likely result in EEXIST */
56 if (errno == EEXIST)
57 return 0;
58
59 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
60 }
61
62 return 0;
63 }
64
65 static int add_serial_getty(const char *tty) {
66 _cleanup_free_ char *n = NULL;
67 int r;
68
69 assert(tty);
70
71 log_debug("Automatically adding serial getty for /dev/%s.", tty);
72
73 r = unit_name_from_path_instance("serial-getty", tty, ".service", &n);
74 if (r < 0)
75 return log_error_errno(r, "Failed to generate service name: %m");
76
77 return add_symlink("serial-getty@.service", n);
78 }
79
80 static int add_container_getty(const char *tty) {
81 _cleanup_free_ char *n = NULL;
82 int r;
83
84 assert(tty);
85
86 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
87
88 r = unit_name_from_path_instance("container-getty", tty, ".service", &n);
89 if (r < 0)
90 return log_error_errno(r, "Failed to generate service name: %m");
91
92 return add_symlink("container-getty@.service", n);
93 }
94
95 static int verify_tty(const char *name) {
96 _cleanup_close_ int fd = -1;
97 const char *p;
98
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
102 * isatty() on it. */
103
104 p = strjoina("/dev/", name);
105
106 /* O_NONBLOCK is essential here, to make sure we don't wait
107 * for DCD */
108 fd = open(p, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW);
109 if (fd < 0)
110 return -errno;
111
112 errno = 0;
113 if (isatty(fd) <= 0)
114 return errno > 0 ? -errno : -EIO;
115
116 return 0;
117 }
118
119 int main(int argc, char *argv[]) {
120
121 static const char virtualization_consoles[] =
122 "hvc0\0"
123 "xvc0\0"
124 "hvsi0\0"
125 "sclp_line0\0"
126 "ttysclp0\0"
127 "3270!tty1\0";
128
129 _cleanup_free_ char *active = NULL;
130 const char *j;
131 int r;
132
133 if (argc > 1 && argc != 4) {
134 log_error("This program takes three or no arguments.");
135 return EXIT_FAILURE;
136 }
137
138 if (argc > 1)
139 arg_dest = argv[1];
140
141 log_set_target(LOG_TARGET_SAFE);
142 log_parse_environment();
143 log_open();
144
145 umask(0022);
146
147 if (detect_container() > 0) {
148 _cleanup_free_ char *container_ttys = NULL;
149
150 log_debug("Automatically adding console shell.");
151
152 if (add_symlink("console-getty.service", "console-getty.service") < 0)
153 return EXIT_FAILURE;
154
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. */
158
159 r = getenv_for_pid(1, "container_ttys", &container_ttys);
160 if (r > 0) {
161 const char *word, *state;
162 size_t l;
163
164 FOREACH_WORD(word, l, container_ttys, state) {
165 const char *t;
166 char tty[l + 1];
167
168 memcpy(tty, word, l);
169 tty[l] = 0;
170
171 /* First strip off /dev/ if it is specified */
172 t = path_startswith(tty, "/dev/");
173 if (!t)
174 t = tty;
175
176 /* Then, make sure it's actually a pty */
177 t = path_startswith(t, "pts/");
178 if (!t)
179 continue;
180
181 if (add_container_getty(t) < 0)
182 return EXIT_FAILURE;
183 }
184 }
185
186 /* Don't add any further magic if we are in a container */
187 return EXIT_SUCCESS;
188 }
189
190 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
191 const char *word, *state;
192 size_t l;
193
194 /* Automatically add in a serial getty on all active
195 * kernel consoles */
196 FOREACH_WORD(word, l, active, state) {
197 _cleanup_free_ char *tty = NULL;
198
199 tty = strndup(word, l);
200 if (!tty) {
201 log_oom();
202 return EXIT_FAILURE;
203 }
204
205 /* We assume that gettys on virtual terminals are
206 * started via manual configuration and do this magic
207 * only for non-VC terminals. */
208
209 if (isempty(tty) || tty_is_vc(tty))
210 continue;
211
212 if (verify_tty(tty) < 0)
213 continue;
214
215 if (add_serial_getty(tty) < 0)
216 return EXIT_FAILURE;
217 }
218 }
219
220 /* Automatically add in a serial getty on the first
221 * virtualizer console */
222 NULSTR_FOREACH(j, virtualization_consoles) {
223 char *p;
224
225 p = strjoina("/sys/class/tty/", j);
226 if (access(p, F_OK) < 0)
227 continue;
228
229 if (add_serial_getty(j) < 0)
230 return EXIT_FAILURE;
231 }
232
233 return EXIT_SUCCESS;
234 }