]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/getty-generator/getty-generator.c
getty-generator: Enable getty on all active serial consoles.
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
CommitLineData
2a796654
LP
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
5430f7f2
LP
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
2a796654
LP
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
5430f7f2 16 Lesser General Public License for more details.
2a796654 17
5430f7f2 18 You should have received a copy of the GNU Lesser General Public License
2a796654
LP
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"
49e942b2 28#include "mkdir.h"
2a796654 29#include "unit-name.h"
5dc4c17f 30#include "virt.h"
a5c32cff 31#include "fileio.h"
2a796654 32
6a39419f 33static const char *arg_dest = "/tmp";
2a796654
LP
34
35static int add_symlink(const char *fservice, const char *tservice) {
36 char *from = NULL, *to = NULL;
37 int r;
38
4dc380d1
LP
39 assert(fservice);
40 assert(tservice);
41
07719a21 42 from = strappend(SYSTEM_DATA_UNIT_PATH "/", fservice);
b7def684 43 to = strjoin(arg_dest,"/getty.target.wants/", tservice, NULL);
2a796654
LP
44
45 if (!from || !to) {
0d0f0c50 46 r = log_oom();
2a796654
LP
47 goto finish;
48 }
49
d2e54fae 50 mkdir_parents_label(to, 0755);
2a796654 51
a17b785b
LP
52 r = symlink(from, to);
53 if (r < 0) {
54 if (errno == EEXIST)
3c20189a 55 /* In case console=hvc0 is passed this will very likely result in EEXIST */
a17b785b
LP
56 r = 0;
57 else {
c79bb9e4 58 log_error("Failed to create symlink %s: %m", to);
a17b785b
LP
59 r = -errno;
60 }
2a796654
LP
61 }
62
63finish:
64
65 free(from);
66 free(to);
67
68 return r;
69}
70
4dc380d1
LP
71static int add_serial_getty(const char *tty) {
72 char *n;
73 int r;
74
75 assert(tty);
76
77 log_debug("Automatically adding serial getty for /dev/%s.", tty);
78
79 n = unit_name_replace_instance("serial-getty@.service", tty);
0d0f0c50
SL
80 if (!n)
81 return log_oom();
4dc380d1
LP
82
83 r = add_symlink("serial-getty@.service", n);
84 free(n);
85
86 return r;
87}
88
2a796654 89int main(int argc, char *argv[]) {
3c20189a
LP
90
91 static const char virtualization_consoles[] =
92 "hvc0\0"
93 "xvc0\0"
94 "hvsi0\0";
95
2a796654
LP
96 int r = EXIT_SUCCESS;
97 char *active;
3c20189a 98 const char *j;
2a796654 99
07719a21
LP
100 if (argc > 1 && argc != 4) {
101 log_error("This program takes three or no arguments.");
2a796654
LP
102 return EXIT_FAILURE;
103 }
104
07719a21
LP
105 if (argc > 1)
106 arg_dest = argv[1];
107
a6903061 108 log_set_target(LOG_TARGET_SAFE);
2a796654
LP
109 log_parse_environment();
110 log_open();
111
4c12626c
LP
112 umask(0022);
113
2a796654 114 if (detect_container(NULL) > 0) {
6705c2df 115 log_debug("Automatically adding console shell.");
2a796654 116
337eebb9 117 if (add_symlink("console-getty.service", "console-getty.service") < 0)
2a796654
LP
118 r = EXIT_FAILURE;
119
120 /* Don't add any further magic if we are in a container */
121 goto finish;
122 }
123
124 if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
39f0570d
MM
125 char *w, *state;
126 size_t l;
127
128 /* Automatically add in a serial getty on all active
129 * kernel consoles */
130 FOREACH_WORD(w, l, active, state) {
131 char *tty;
3c20189a 132 int k;
2a796654 133
39f0570d
MM
134 tty = strndup(w, l);
135 if (!tty) {
136 log_oom();
137 free(active);
138 r = EXIT_FAILURE;
139 goto finish;
140 }
141
142 if (isempty(tty) || tty_is_vc(tty)) {
143 free(tty);
144 continue;
145 }
146
2a796654
LP
147 /* We assume that gettys on virtual terminals are
148 * started via manual configuration and do this magic
149 * only for non-VC terminals. */
150
4dc380d1 151 k = add_serial_getty(tty);
3c20189a 152
3c20189a 153 if (k < 0) {
39f0570d
MM
154 free(tty);
155 free(active);
3c20189a
LP
156 r = EXIT_FAILURE;
157 goto finish;
158 }
159 }
39f0570d 160 free(active);
2a796654
LP
161 }
162
163 /* Automatically add in a serial getty on the first
164 * virtualizer console */
3c20189a 165 NULSTR_FOREACH(j, virtualization_consoles) {
4dc380d1 166 char *p;
3c20189a 167 int k;
2a796654 168
3c20189a 169 if (asprintf(&p, "/sys/class/tty/%s", j) < 0) {
0d0f0c50 170 log_oom();
2a796654 171 r = EXIT_FAILURE;
3c20189a
LP
172 goto finish;
173 }
980fc73d 174
3c20189a
LP
175 k = access(p, F_OK);
176 free(p);
177
178 if (k < 0)
179 continue;
980fc73d 180
4dc380d1 181 k = add_serial_getty(j);
3c20189a
LP
182 if (k < 0) {
183 r = EXIT_FAILURE;
184 goto finish;
185 }
2a796654
LP
186 }
187
188finish:
189 return r;
190}