chiark / gitweb /
general: unify error code we generate on timeout
[elogind.git] / src / vconsole-setup.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 Kay Sievers
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 <stdio.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <ctype.h>
29 #include <stdbool.h>
30 #include <stdarg.h>
31 #include <limits.h>
32 #include <locale.h>
33 #include <langinfo.h>
34 #include <sys/ioctl.h>
35 #include <sys/wait.h>
36 #include <linux/tiocl.h>
37 #include <linux/kd.h>
38
39 #include "util.h"
40 #include "log.h"
41 #include "macro.h"
42
43 static bool is_vconsole(int fd) {
44         unsigned char data[1];
45
46         data[0] = TIOCL_GETFGCONSOLE;
47         return ioctl(fd, TIOCLINUX, data) >= 0;
48 }
49
50 static bool is_locale_utf8(void) {
51         const char *set;
52
53         if (!setlocale(LC_ALL, ""))
54                 return true;
55
56         set = nl_langinfo(CODESET);
57         if (!set)
58                 return true;
59
60         return streq(set, "UTF-8");
61 }
62
63 static int disable_utf8(int fd) {
64         int r = 0, k;
65
66         if (ioctl(fd, KDSKBMODE, K_XLATE) < 0)
67                 r = -errno;
68
69         if (loop_write(fd, "\033%@", 3, false) < 0)
70                 r = -errno;
71
72         if ((k = write_one_line_file("/sys/module/vt/parameters/default_utf8", "0")) < 0)
73                 r = k;
74
75         if (r < 0)
76                 log_warning("Failed to disable UTF-8: %s", strerror(errno));
77
78         return r;
79 }
80
81 static int load_keymap(const char *vc, const char *map, const char *map_toggle, bool utf8, pid_t *_pid) {
82         const char *args[8];
83         int i = 0;
84         pid_t pid;
85
86         args[i++] = KBD_LOADKEYS;
87         args[i++] = "-q";
88         args[i++] = "-C";
89         args[i++] = vc;
90         if (utf8)
91                 args[i++] = "-u";
92         args[i++] = map;
93         if (map_toggle)
94                 args[i++] = map_toggle;
95         args[i++] = NULL;
96
97         if ((pid = fork()) < 0) {
98                 log_error("Failed to fork: %m");
99                 return -errno;
100         } else if (pid == 0) {
101                 execv(args[0], (char **) args);
102                 _exit(EXIT_FAILURE);
103         }
104
105         *_pid = pid;
106         return 0;
107 }
108
109 static int load_font(const char *vc, const char *font, const char *map, const char *unimap, pid_t *_pid) {
110         const char *args[9];
111         int i = 0;
112         pid_t pid;
113
114         args[i++] = KBD_SETFONT;
115         args[i++] = "-C";
116         args[i++] = vc;
117         args[i++] = font;
118         if (map) {
119                 args[i++] = "-m";
120                 args[i++] = map;
121         }
122         if (unimap) {
123                 args[i++] = "-u";
124                 args[i++] = unimap;
125         }
126         args[i++] = NULL;
127
128         if ((pid = fork()) < 0) {
129                 log_error("Failed to fork: %m");
130                 return -errno;
131         } else if (pid == 0) {
132                 execv(args[0], (char **) args);
133                 _exit(EXIT_FAILURE);
134         }
135
136         *_pid = pid;
137         return 0;
138 }
139
140 int main(int argc, char **argv) {
141         const char *vc;
142         char *vc_keymap = NULL;
143         char *vc_keymap_toggle = NULL;
144         char *vc_font = NULL;
145         char *vc_font_map = NULL;
146         char *vc_font_unimap = NULL;
147 #ifdef TARGET_GENTOO
148         char *vc_unicode = NULL;
149 #endif
150         int fd = -1;
151         bool utf8;
152         int r = EXIT_FAILURE;
153         pid_t font_pid = 0, keymap_pid = 0;
154
155         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
156         log_parse_environment();
157         log_open();
158
159         if (argv[1])
160                 vc = argv[1];
161         else
162                 vc = "/dev/tty0";
163
164         if ((fd = open(vc, O_RDWR|O_CLOEXEC)) < 0) {
165                 log_error("Failed to open %s: %m", vc);
166                 goto finish;
167         }
168
169         if (!is_vconsole(fd)) {
170                 log_error("Device %s is not a virtual console.", vc);
171                 goto finish;
172         }
173
174         utf8 = is_locale_utf8();
175
176         if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
177 #ifdef TARGET_FEDORA
178                                 "SYSFONT", &vc_font,
179                                 "KEYTABLE", &vc_keymap,
180 #endif
181                                 "vconsole.keymap", &vc_keymap,
182                                 "vconsole.keymap.toggle", &vc_keymap_toggle,
183                                 "vconsole.font", &vc_font,
184                                 "vconsole.font.map", &vc_font_map,
185                                 "vconsole.font.unimap", &vc_font_unimap,
186                                 NULL)) < 0) {
187
188                 if (r != -ENOENT)
189                         log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
190         }
191
192         /* Hmm, nothing set on the kernel cmd line? Then let's
193          * try /etc/vconsole.conf */
194         if (r <= 0 &&
195             (r = parse_env_file("/etc/vconsole.conf", NEWLINE,
196                                 "KEYMAP", &vc_keymap,
197                                 "KEYMAP_TOGGLE", &vc_keymap_toggle,
198                                 "FONT", &vc_font,
199                                 "FONT_MAP", &vc_font_map,
200                                 "FONT_UNIMAP", &vc_font_unimap,
201                                 NULL)) < 0) {
202
203                 if (r != -ENOENT)
204                         log_warning("Failed to read /etc/vconsole.conf: %s", strerror(-r));
205         }
206
207         if (r <= 0) {
208 #ifdef TARGET_FEDORA
209                 if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
210                                         "SYSFONT", &vc_font,
211                                         "SYSFONTACM", &vc_font_map,
212                                         "UNIMAP", &vc_font_unimap,
213                                         NULL)) < 0) {
214
215                         if (r != -ENOENT)
216                                 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
217                 }
218
219                 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
220                                         "KEYTABLE", &vc_keymap,
221                                         "KEYMAP", &vc_keymap,
222                                         NULL)) < 0) {
223
224                         if (r != -ENOENT)
225                                 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
226                 }
227
228                 if (access("/etc/sysconfig/console/default.kmap", F_OK) >= 0) {
229                         char *t;
230
231                         if (!(t = strdup("/etc/sysconfig/console/default.kmap"))) {
232                                 log_error("Out of memory.");
233                                 goto finish;
234                         }
235
236                         free(vc_keymap);
237                         vc_keymap = t;
238                 }
239
240 #elif defined(TARGET_SUSE)
241                 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
242                                         "KEYTABLE", &vc_keymap,
243                                         NULL)) < 0) {
244
245                         if (r != -ENOENT)
246                                 log_warning("Failed to read /etc/sysconfig/keyboard: %s", strerror(-r));
247                 }
248
249                 if ((r = parse_env_file("/etc/sysconfig/console", NEWLINE,
250                                         "CONSOLE_FONT", &vc_font,
251                                         "CONSOLE_SCREENMAP", &vc_font_map,
252                                         "CONSOLE_UNICODEMAP", &vc_font_unimap,
253                                         NULL)) < 0) {
254
255                         if (r != -ENOENT)
256                                 log_warning("Failed to read /etc/sysconfig/console: %s", strerror(-r));
257                 }
258
259 #elif defined(TARGET_ARCH)
260                 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
261                                         "KEYMAP", &vc_keymap,
262                                         "CONSOLEFONT", &vc_font,
263                                         "CONSOLEMAP", &vc_font_map,
264                                         NULL)) < 0) {
265
266                         if (r != -ENOENT)
267                                 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
268                 }
269
270 #elif defined(TARGET_FRUGALWARE)
271                 if ((r = parse_env_file("/etc/sysconfig/keymap", NEWLINE,
272                                         "keymap", &vc_keymap,
273                                         NULL)) < 0) {
274                         if (r != -ENOENT)
275                                 log_warning("Failed to read /etc/sysconfig/keymap: %s", strerror(-r));
276                 }
277                 if ((r = parse_env_file("/etc/sysconfig/font", NEWLINE,
278                                         "font", &vc_font,
279                                         NULL)) < 0) {
280                         if (r != -ENOENT)
281                                 log_warning("Failed to read /etc/sysconfig/font: %s", strerror(-r));
282                 }
283
284 #elif defined(TARGET_ALTLINUX)
285                 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
286                                         "KEYTABLE", &vc_keymap,
287                                         NULL)) < 0) {
288
289                         if (r != -ENOENT)
290                                 log_warning("Failed to read /etc/sysconfig/keyboard: %s", strerror(-r));
291                 }
292
293                 if ((r = parse_env_file("/etc/sysconfig/consolefont", NEWLINE,
294                                         "SYSFONT", &vc_font,
295                                         NULL)) < 0) {
296
297                         if (r != -ENOENT)
298                                 log_warning("Failed to read /etc/sysconfig/console: %s", strerror(-r));
299                 }
300
301 #elif defined(TARGET_GENTOO)
302                 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
303                                         "unicode", &vc_unicode,
304                                         NULL)) < 0) {
305                         if (r != -ENOENT)
306                                 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
307                 }
308
309                 if (vc_unicode) {
310                         int rc_unicode;
311
312                         if ((rc_unicode = parse_boolean(vc_unicode)) < 0)
313                                 log_error("Unknown value for /etc/rc.conf unicode=%s", vc_unicode);
314                         else {
315                                 if (rc_unicode && !utf8)
316                                         log_warning("/etc/rc.conf wants unicode, but current locale is not UTF-8 capable!");
317                                 else if (!rc_unicode && utf8) {
318                                         log_debug("/etc/rc.conf does not want unicode, leave it on in kernel but does not apply to vconsole.");
319                                         utf8 = false;
320                                 }
321                         }
322                 }
323
324                 /* /etc/conf.d/consolefont comments and gentoo
325                  * documentation mention uppercase, but the actual
326                  * contents are lowercase.  the existing
327                  * /etc/init.d/consolefont tries both
328                  */
329                 if ((r = parse_env_file("/etc/conf.d/consolefont", NEWLINE,
330                                         "CONSOLEFONT", &vc_font,
331                                         "consolefont", &vc_font,
332                                         "consoletranslation", &vc_font_map,
333                                         "CONSOLETRANSLATION", &vc_font_map,
334                                         "unicodemap", &vc_font_unimap,
335                                         "UNICODEMAP", &vc_font_unimap,
336                                         NULL)) < 0) {
337                         if (r != -ENOENT)
338                                 log_warning("Failed to read /etc/conf.d/consolefont: %s", strerror(-r));
339                 }
340
341                 if ((r = parse_env_file("/etc/conf.d/keymaps", NEWLINE,
342                                         "keymap", &vc_keymap,
343                                         "KEYMAP", &vc_keymap,
344                                         NULL)) < 0) {
345                         if (r != -ENOENT)
346                                 log_warning("Failed to read /etc/conf.d/keymaps: %s", strerror(-r));
347                 }
348 #endif
349         }
350
351         if (!vc_keymap)
352                 vc_keymap = strdup("us");
353         if (!vc_font)
354                 vc_font = strdup(DEFAULT_FONT);
355
356         if (!vc_keymap || !vc_font) {
357                 log_error("Failed to allocate strings.");
358                 goto finish;
359         }
360
361         if (!utf8)
362                 disable_utf8(fd);
363
364         if (load_keymap(vc, vc_keymap, vc_keymap_toggle, utf8, &keymap_pid) >= 0 &&
365             load_font(vc, vc_font, vc_font_map, vc_font_unimap, &font_pid) >= 0)
366                 r = EXIT_SUCCESS;
367
368 finish:
369         if (keymap_pid > 0)
370                 wait_for_terminate_and_warn(KBD_LOADKEYS, keymap_pid);
371
372         if (font_pid > 0)
373                 wait_for_terminate_and_warn(KBD_SETFONT, font_pid);
374
375         free(vc_keymap);
376         free(vc_font);
377         free(vc_font_map);
378         free(vc_font_unimap);
379
380         if (fd >= 0)
381                 close_nointr_nofail(fd);
382
383         return r;
384 }