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