]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/getty-generator/getty-generator.c
s390/getty-generator: initialize essential system terminals/consoles
[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
26 #include "log.h"
27 #include "util.h"
28 #include "mkdir.h"
29 #include "unit-name.h"
30 #include "virt.h"
31 #include "fileio.h"
32 #include "path-util.h"
33
34 static const char *arg_dest = "/tmp";
35
36 static int add_symlink(const char *fservice, const char *tservice) {
37 _cleanup_free_ char *from = NULL, *to = NULL;
38 int r;
39
40 assert(fservice);
41 assert(tservice);
42
43 from = strappend(SYSTEM_DATA_UNIT_PATH "/", fservice);
44 if (!from)
45 return log_oom();
46
47 to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL);
48 if (!to)
49 return log_oom();
50
51 mkdir_parents_label(to, 0755);
52
53 r = symlink(from, to);
54 if (r < 0) {
55 if (errno == EEXIST)
56 /* In case console=hvc0 is passed this will very likely result in EEXIST */
57 return 0;
58 else {
59 log_error("Failed to create symlink %s: %m", to);
60 return -errno;
61 }
62 }
63
64 return 0;
65 }
66
67 static int add_serial_getty(const char *tty) {
68 _cleanup_free_ char *n = NULL;
69
70 assert(tty);
71
72 log_debug("Automatically adding serial getty for /dev/%s.", tty);
73
74 n = unit_name_replace_instance("serial-getty@.service", tty);
75 if (!n)
76 return log_oom();
77
78 return add_symlink("serial-getty@.service", n);
79 }
80
81 static int add_container_getty(const char *tty) {
82 _cleanup_free_ char *n = NULL;
83
84 assert(tty);
85
86 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
87
88 n = unit_name_replace_instance("container-getty@.service", tty);
89 if (!n)
90 return log_oom();
91
92 return add_symlink("container-getty@.service", n);
93 }
94
95 int main(int argc, char *argv[]) {
96
97 static const char virtualization_consoles[] =
98 "hvc0\0"
99 "xvc0\0"
100 "hvsi0\0"
101 "sclp_line0\0"
102 "ttysclp0\0";
103
104 _cleanup_free_ char *active = NULL;
105 const char *j;
106 int r;
107
108 if (argc > 1 && argc != 4) {
109 log_error("This program takes three or no arguments.");
110 return EXIT_FAILURE;
111 }
112
113 if (argc > 1)
114 arg_dest = argv[1];
115
116 log_set_target(LOG_TARGET_SAFE);
117 log_parse_environment();
118 log_open();
119
120 umask(0022);
121
122 if (detect_container(NULL) > 0) {
123 _cleanup_free_ char *container_ttys = NULL;
124
125 log_debug("Automatically adding console shell.");
126
127 if (add_symlink("console-getty.service", "console-getty.service") < 0)
128 return EXIT_FAILURE;
129
130 /* When $container_ttys is set for PID 1, spawn
131 * gettys on all ptys named therein. Note that despite
132 * the variable name we only support ptys here. */
133
134 r = getenv_for_pid(1, "container_ttys", &container_ttys);
135 if (r > 0) {
136 char *w, *state;
137 size_t l;
138
139 FOREACH_WORD(w, l, container_ttys, state) {
140 const char *t;
141 char tty[l + 1];
142
143 memcpy(tty, w, l);
144 tty[l] = 0;
145
146 /* First strip off /dev/ if it is specified */
147 t = path_startswith(tty, "/dev/");
148 if (!t)
149 t = tty;
150
151 /* Then, make sure it's actually a pty */
152 t = path_startswith(t, "pts/");
153 if (!t)
154 continue;
155
156 if (add_container_getty(t) < 0)
157 return EXIT_FAILURE;
158 }
159 }
160
161 /* Don't add any further magic if we are in a container */
162 return EXIT_SUCCESS;
163 }
164
165 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
166 char *w, *state;
167 size_t l;
168
169 /* Automatically add in a serial getty on all active
170 * kernel consoles */
171 FOREACH_WORD(w, l, active, state) {
172 _cleanup_free_ char *tty = NULL;
173
174 tty = strndup(w, l);
175 if (!tty) {
176 log_oom();
177 return EXIT_FAILURE;
178 }
179
180 if (isempty(tty) || tty_is_vc(tty))
181 continue;
182
183 /* We assume that gettys on virtual terminals are
184 * started via manual configuration and do this magic
185 * only for non-VC terminals. */
186
187 if (add_serial_getty(tty) < 0)
188 return EXIT_FAILURE;
189 }
190 }
191
192 /* Automatically add in a serial getty on the first
193 * virtualizer console */
194 NULSTR_FOREACH(j, virtualization_consoles) {
195 _cleanup_free_ char *p = NULL;
196
197 p = strappend("/sys/class/tty/", j);
198 if (!p) {
199 log_oom();
200 return EXIT_FAILURE;
201 }
202
203 if (access(p, F_OK) < 0)
204 continue;
205
206 if (add_serial_getty(j) < 0)
207 return EXIT_FAILURE;
208 }
209
210 return EXIT_SUCCESS;
211 }