]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
shared: add terminal-util.[ch]
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <string.h>
23 #include <errno.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26
27 #include "log.h"
28 #include "util.h"
29 #include "mkdir.h"
30 #include "unit-name.h"
31 #include "virt.h"
32 #include "fileio.h"
33 #include "path-util.h"
34 #include "process-util.h"
35 #include "terminal-util.h"
36
37 static const char *arg_dest = "/tmp";
38
39 static int add_symlink(const char *fservice, const char *tservice) {
40 char *from, *to;
41 int r;
42
43 assert(fservice);
44 assert(tservice);
45
46 from = strjoina(SYSTEM_DATA_UNIT_PATH "/", fservice);
47 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
48
49 mkdir_parents_label(to, 0755);
50
51 r = symlink(from, to);
52 if (r < 0) {
53 if (errno == EEXIST)
54 /* In case console=hvc0 is passed this will very likely result in EEXIST */
55 return 0;
56 else {
57 log_error_errno(errno, "Failed to create symlink %s: %m", to);
58 return -errno;
59 }
60 }
61
62 return 0;
63 }
64
65 static int add_serial_getty(const char *tty) {
66 _cleanup_free_ char *n = NULL;
67
68 assert(tty);
69
70 log_debug("Automatically adding serial getty for /dev/%s.", tty);
71
72 n = unit_name_from_path_instance("serial-getty", tty, ".service");
73 if (!n)
74 return log_oom();
75
76 return add_symlink("serial-getty@.service", n);
77 }
78
79 static int add_container_getty(const char *tty) {
80 _cleanup_free_ char *n = NULL;
81
82 assert(tty);
83
84 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
85
86 n = unit_name_from_path_instance("container-getty", tty, ".service");
87 if (!n)
88 return log_oom();
89
90 return add_symlink("container-getty@.service", n);
91 }
92
93 static int verify_tty(const char *name) {
94 _cleanup_close_ int fd = -1;
95 const char *p;
96
97 /* Some TTYs are weird and have been enumerated but don't work
98 * when you try to use them, such as classic ttyS0 and
99 * friends. Let's check that and open the device and run
100 * isatty() on it. */
101
102 p = strjoina("/dev/", name);
103
104 /* O_NONBLOCK is essential here, to make sure we don't wait
105 * for DCD */
106 fd = open(p, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW);
107 if (fd < 0)
108 return -errno;
109
110 errno = 0;
111 if (isatty(fd) <= 0)
112 return errno ? -errno : -EIO;
113
114 return 0;
115 }
116
117 int main(int argc, char *argv[]) {
118
119 static const char virtualization_consoles[] =
120 "hvc0\0"
121 "xvc0\0"
122 "hvsi0\0"
123 "sclp_line0\0"
124 "ttysclp0\0"
125 "3270!tty1\0";
126
127 _cleanup_free_ char *active = NULL;
128 const char *j;
129 int r;
130
131 if (argc > 1 && argc != 4) {
132 log_error("This program takes three or no arguments.");
133 return EXIT_FAILURE;
134 }
135
136 if (argc > 1)
137 arg_dest = argv[1];
138
139 log_set_target(LOG_TARGET_SAFE);
140 log_parse_environment();
141 log_open();
142
143 umask(0022);
144
145 if (detect_container(NULL) > 0) {
146 _cleanup_free_ char *container_ttys = NULL;
147
148 log_debug("Automatically adding console shell.");
149
150 if (add_symlink("console-getty.service", "console-getty.service") < 0)
151 return EXIT_FAILURE;
152
153 /* When $container_ttys is set for PID 1, spawn
154 * gettys on all ptys named therein. Note that despite
155 * the variable name we only support ptys here. */
156
157 r = getenv_for_pid(1, "container_ttys", &container_ttys);
158 if (r > 0) {
159 const char *word, *state;
160 size_t l;
161
162 FOREACH_WORD(word, l, container_ttys, state) {
163 const char *t;
164 char tty[l + 1];
165
166 memcpy(tty, word, l);
167 tty[l] = 0;
168
169 /* First strip off /dev/ if it is specified */
170 t = path_startswith(tty, "/dev/");
171 if (!t)
172 t = tty;
173
174 /* Then, make sure it's actually a pty */
175 t = path_startswith(t, "pts/");
176 if (!t)
177 continue;
178
179 if (add_container_getty(t) < 0)
180 return EXIT_FAILURE;
181 }
182 }
183
184 /* Don't add any further magic if we are in a container */
185 return EXIT_SUCCESS;
186 }
187
188 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
189 const char *word, *state;
190 size_t l;
191
192 /* Automatically add in a serial getty on all active
193 * kernel consoles */
194 FOREACH_WORD(word, l, active, state) {
195 _cleanup_free_ char *tty = NULL;
196
197 tty = strndup(word, l);
198 if (!tty) {
199 log_oom();
200 return EXIT_FAILURE;
201 }
202
203 if (isempty(tty) || tty_is_vc(tty))
204 continue;
205
206 if (verify_tty(tty) < 0)
207 continue;
208
209 /* We assume that gettys on virtual terminals are
210 * started via manual configuration and do this magic
211 * only for non-VC terminals. */
212
213 if (add_serial_getty(tty) < 0)
214 return EXIT_FAILURE;
215 }
216 }
217
218 /* Automatically add in a serial getty on the first
219 * virtualizer console */
220 NULSTR_FOREACH(j, virtualization_consoles) {
221 char *p;
222
223 p = strjoina("/sys/class/tty/", j);
224 if (access(p, F_OK) < 0)
225 continue;
226
227 if (add_serial_getty(j) < 0)
228 return EXIT_FAILURE;
229 }
230
231 return EXIT_SUCCESS;
232 }