chiark / gitweb /
getty: automatically add getty on hvsi0 virtualizer console
[elogind.git] / src / 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 General Public License as published by
10   the Free Software Foundation; either version 2 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   General Public License for more details.
17
18   You should have received a copy of the GNU 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 "unit-name.h"
29
30 const char *arg_dest = "/tmp";
31
32 static int add_symlink(const char *fservice, const char *tservice) {
33         char *from = NULL, *to = NULL;
34         int r;
35
36         asprintf(&from, SYSTEM_DATA_UNIT_PATH "/%s", fservice);
37         asprintf(&to, "%s/getty.target.wants/%s", arg_dest, tservice);
38
39         if (!from || !to) {
40                 log_error("Out of memory");
41                 r = -ENOMEM;
42                 goto finish;
43         }
44
45         mkdir_parents(to, 0755);
46
47         r = symlink(from, to);
48         if (r < 0) {
49                 if (errno == EEXIST)
50                         /* In case console=hvc0 is passed this will very likely result in EEXIST */
51                         r = 0;
52                 else {
53                         log_error("Failed to create symlink from %s to %s: %m", from, to);
54                         r = -errno;
55                 }
56         }
57
58 finish:
59
60         free(from);
61         free(to);
62
63         return r;
64 }
65
66 int main(int argc, char *argv[]) {
67
68         static const char virtualization_consoles[] =
69                 "hvc0\0"
70                 "xvc0\0"
71                 "hvsi0\0";
72
73         int r = EXIT_SUCCESS;
74         char *active;
75         const char *j;
76
77         if (argc > 2) {
78                 log_error("This program takes one or no arguments.");
79                 return EXIT_FAILURE;
80         }
81
82         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
83         log_parse_environment();
84         log_open();
85
86         umask(0022);
87
88         if (argc > 1)
89             arg_dest = argv[1];
90
91         if (detect_container(NULL) > 0) {
92                 log_debug("Automatically adding console shell.");
93
94                 if (add_symlink("console-shell.service", "console-shell.service") < 0)
95                         r = EXIT_FAILURE;
96
97                 /* Don't add any further magic if we are in a container */
98                 goto finish;
99         }
100
101         if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) {
102                 const char *tty;
103
104                 tty = strrchr(active, ' ');
105                 if (tty)
106                         tty ++;
107                 else
108                         tty = active;
109
110                 /* Automatically add in a serial getty on the kernel
111                  * console */
112                 if (tty_is_vc(tty))
113                         free(active);
114                 else {
115                         char *n;
116                         int k;
117
118                         /* We assume that gettys on virtual terminals are
119                          * started via manual configuration and do this magic
120                          * only for non-VC terminals. */
121
122                         log_debug("Automatically adding serial getty for /dev/%s.", tty);
123
124                         n = unit_name_replace_instance("serial-getty@.service", tty);
125                         free(active);
126
127                         if (!n) {
128                                 log_error("Out of memory");
129                                 r = EXIT_FAILURE;
130                                 goto finish;
131                         }
132
133                         k = add_symlink("serial-getty@.service", n);
134                         free(n);
135
136                         if (k < 0) {
137                                 r = EXIT_FAILURE;
138                                 goto finish;
139                         }
140                 }
141         }
142
143         /* Automatically add in a serial getty on the first
144          * virtualizer console */
145         NULSTR_FOREACH(j, virtualization_consoles) {
146                 char *n, *p;
147                 int k;
148
149                 if (asprintf(&p, "/sys/class/tty/%s", j) < 0) {
150                         log_error("Out of memory");
151                         r = EXIT_FAILURE;
152                         goto finish;
153                 }
154
155                 k = access(p, F_OK);
156                 free(p);
157
158                 if (k < 0)
159                         continue;
160
161                 log_debug("Automatically adding serial getty for /dev/%s.", j);
162
163                 n = unit_name_replace_instance("serial-getty@.service", j);
164                 if (!n) {
165                         log_error("Out of memory");
166                         r = EXIT_FAILURE;
167                         goto finish;
168                 }
169
170                 k = add_symlink("serial-getty@.service", n);
171                 free(n);
172
173                 if (k < 0) {
174                         r = EXIT_FAILURE;
175                         goto finish;
176                 }
177         }
178
179 finish:
180         return r;
181 }