]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/getty-generator/getty-generator.c
Merge pull request #11827 from keszybz/pkgconfig-variables
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2a796654 2
2a796654 3#include <errno.h>
54340751 4#include <fcntl.h>
07630cea
LP
5#include <string.h>
6#include <unistd.h>
2a796654 7
b5efdb8a 8#include "alloc-util.h"
3ffd4af2 9#include "fd-util.h"
07630cea 10#include "fileio.h"
afe44c8f 11#include "generator.h"
2a796654 12#include "log.h"
49e942b2 13#include "mkdir.h"
1d97ff7d 14#include "path-util.h"
0b452006 15#include "process-util.h"
9d22f97b 16#include "strv.h"
288a74cc 17#include "terminal-util.h"
07630cea
LP
18#include "unit-name.h"
19#include "util.h"
20#include "virt.h"
2a796654 21
9d22f97b 22static const char *arg_dest = NULL;
2a796654
LP
23
24static int add_symlink(const char *fservice, const char *tservice) {
f5650614 25 char *from, *to;
2a796654
LP
26 int r;
27
4dc380d1
LP
28 assert(fservice);
29 assert(tservice);
30
63c372cb
LP
31 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", fservice);
32 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
2a796654 33
d2e54fae 34 mkdir_parents_label(to, 0755);
2a796654 35
a17b785b
LP
36 r = symlink(from, to);
37 if (r < 0) {
7410616c 38 /* In case console=hvc0 is passed this will very likely result in EEXIST */
a17b785b 39 if (errno == EEXIST)
f85fc845 40 return 0;
7410616c
LP
41
42 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
2a796654
LP
43 }
44
f85fc845 45 return 0;
2a796654
LP
46}
47
4dc380d1 48static int add_serial_getty(const char *tty) {
f85fc845 49 _cleanup_free_ char *n = NULL;
7410616c 50 int r;
4dc380d1
LP
51
52 assert(tty);
53
54 log_debug("Automatically adding serial getty for /dev/%s.", tty);
55
7410616c
LP
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");
4dc380d1 59
f85fc845 60 return add_symlink("serial-getty@.service", n);
4dc380d1
LP
61}
62
1d97ff7d
LP
63static int add_container_getty(const char *tty) {
64 _cleanup_free_ char *n = NULL;
7410616c 65 int r;
1d97ff7d
LP
66
67 assert(tty);
68
69 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
70
7410616c
LP
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");
1d97ff7d
LP
74
75 return add_symlink("container-getty@.service", n);
76}
77
54340751
LP
78static 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
63c372cb 87 p = strjoina("/dev/", name);
54340751
LP
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)
f5e5c28f 97 return errno > 0 ? -errno : -EIO;
54340751
LP
98
99 return 0;
100}
101
9d22f97b 102static int run(const char *dest, const char *dest_early, const char *dest_late) {
f85fc845 103 _cleanup_free_ char *active = NULL;
3c20189a 104 const char *j;
1d97ff7d 105 int r;
2a796654 106
9d22f97b 107 assert_se(arg_dest = dest);
07719a21 108
75f86906 109 if (detect_container() > 0) {
1d97ff7d
LP
110 _cleanup_free_ char *container_ttys = NULL;
111
6705c2df 112 log_debug("Automatically adding console shell.");
2a796654 113
9d22f97b
ZJS
114 r = add_symlink("console-getty.service", "console-getty.service");
115 if (r < 0)
116 return r;
2a796654 117
1d97ff7d
LP
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);
207d1d09 123 if (r > 0) {
a2a5291b 124 const char *word, *state;
1d97ff7d
LP
125 size_t l;
126
a2a5291b 127 FOREACH_WORD(word, l, container_ttys, state) {
1d97ff7d
LP
128 const char *t;
129 char tty[l + 1];
130
a2a5291b 131 memcpy(tty, word, l);
1d97ff7d
LP
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 */
3fa5dd6d 140 t = path_startswith(t, "pts/");
1d97ff7d
LP
141 if (!t)
142 continue;
143
9d22f97b
ZJS
144 r = add_container_getty(t);
145 if (r < 0)
146 return r;
1d97ff7d
LP
147 }
148 }
149
2a796654 150 /* Don't add any further magic if we are in a container */
9d22f97b 151 return 0;
2a796654
LP
152 }
153
154 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
a2a5291b 155 const char *word, *state;
39f0570d
MM
156 size_t l;
157
158 /* Automatically add in a serial getty on all active
159 * kernel consoles */
a2a5291b 160 FOREACH_WORD(word, l, active, state) {
f85fc845 161 _cleanup_free_ char *tty = NULL;
2a796654 162
a2a5291b 163 tty = strndup(word, l);
9d22f97b
ZJS
164 if (!tty)
165 return log_oom();
39f0570d 166
54194afb
AJ
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
f85fc845 171 if (isempty(tty) || tty_is_vc(tty))
39f0570d 172 continue;
39f0570d 173
54340751
LP
174 if (verify_tty(tty) < 0)
175 continue;
176
9d22f97b
ZJS
177 r = add_serial_getty(tty);
178 if (r < 0)
179 return r;
3c20189a 180 }
2a796654
LP
181 }
182
183 /* Automatically add in a serial getty on the first
184 * virtualizer console */
9d22f97b
ZJS
185 FOREACH_STRING(j,
186 "hvc0",
187 "xvc0",
188 "hvsi0",
189 "sclp_line0",
190 "ttysclp0",
191 "3270!tty1") {
192 const char *p;
980fc73d 193
63c372cb 194 p = strjoina("/sys/class/tty/", j);
f85fc845 195 if (access(p, F_OK) < 0)
3c20189a 196 continue;
980fc73d 197
9d22f97b
ZJS
198 r = add_serial_getty(j);
199 if (r < 0)
200 return r;
2a796654
LP
201 }
202
9d22f97b 203 return 0;
2a796654 204}
9d22f97b
ZJS
205
206DEFINE_MAIN_GENERATOR_FUNCTION(run);