chiark / gitweb /
mount: don't pull in nofail mounts by default, but use them if they are around
[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, bool utf8, pid_t *_pid) {
82         const char *args[7];
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         args[i++] = NULL;
94
95         if ((pid = fork()) < 0) {
96                 log_error("Failed to fork: %m");
97                 return -errno;
98         } else if (pid == 0) {
99                 execv(args[0], (char **) args);
100                 _exit(EXIT_FAILURE);
101         }
102
103         *_pid = pid;
104         return 0;
105 }
106
107 static int load_font(const char *vc, const char *font, const char *map, const char *unimap, pid_t *_pid) {
108         const char *args[9];
109         int i = 0;
110         pid_t pid;
111
112         args[i++] = KBD_SETFONT;
113         args[i++] = "-C";
114         args[i++] = vc;
115         args[i++] = font;
116         if (map) {
117                 args[i++] = "-m";
118                 args[i++] = map;
119         }
120         if (unimap) {
121                 args[i++] = "-u";
122                 args[i++] = unimap;
123         }
124         args[i++] = NULL;
125
126         if ((pid = fork()) < 0) {
127                 log_error("Failed to fork: %m");
128                 return -errno;
129         } else if (pid == 0) {
130                 execv(args[0], (char **) args);
131                 _exit(EXIT_FAILURE);
132         }
133
134         *_pid = pid;
135         return 0;
136 }
137
138 int main(int argc, char **argv) {
139         const char *vc;
140         char *vc_keymap = NULL;
141         char *vc_font = NULL;
142         char *vc_font_map = NULL;
143         char *vc_font_unimap = NULL;
144 #ifdef TARGET_GENTOO
145         char *vc_unicode = NULL;
146 #endif
147         int fd = -1;
148         bool utf8;
149         int r = EXIT_FAILURE;
150         pid_t font_pid = 0, keymap_pid = 0;
151
152         log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
153         log_parse_environment();
154         log_open();
155
156         if (argv[1])
157                 vc = argv[1];
158         else
159                 vc = "/dev/tty0";
160
161         if ((fd = open(vc, O_RDWR|O_CLOEXEC)) < 0) {
162                 log_error("Failed to open %s: %m", vc);
163                 goto finish;
164         }
165
166         if (!is_vconsole(fd)) {
167                 log_error("Device %s is not a virtual console.", vc);
168                 goto finish;
169         }
170
171         utf8 = is_locale_utf8();
172
173         if ((r = parse_env_file("/proc/cmdline", WHITESPACE,
174 #ifdef TARGET_FEDORA
175                                 "SYSFONT", &vc_font,
176                                 "KEYTABLE", &vc_keymap,
177 #endif
178                                 "vconsole.keymap", &vc_keymap,
179                                 "vconsole.font", &vc_font,
180                                 "vconsole.font.map", &vc_font_map,
181                                 "vconsole.font.unimap", &vc_font_unimap,
182                                 NULL)) < 0) {
183
184                 if (r != -ENOENT)
185                         log_warning("Failed to read /proc/cmdline: %s", strerror(-r));
186         }
187
188         /* Hmm, nothing set on the kernel cmd line? Then let's
189          * try /etc/vconsole.conf */
190         if (r <= 0 &&
191             (r = parse_env_file("/etc/vconsole.conf", NEWLINE,
192                                 "KEYMAP", &vc_keymap,
193                                 "FONT", &vc_font,
194                                 "FONT_MAP", &vc_font_map,
195                                 "FONT_UNIMAP", &vc_font_unimap,
196                                 NULL)) < 0) {
197
198                 if (r != -ENOENT)
199                         log_warning("Failed to read /etc/vconsole.conf: %s", strerror(-r));
200         }
201
202         if (r <= 0) {
203 #ifdef TARGET_FEDORA
204                 if ((r = parse_env_file("/etc/sysconfig/i18n", NEWLINE,
205                                         "SYSFONT", &vc_font,
206                                         "SYSFONTACM", &vc_font_map,
207                                         "UNIMAP", &vc_font_unimap,
208                                         NULL)) < 0) {
209
210                         if (r != -ENOENT)
211                                 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
212                 }
213
214                 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
215                                         "KEYTABLE", &vc_keymap,
216                                         "KEYMAP", &vc_keymap,
217                                         NULL)) < 0) {
218
219                         if (r != -ENOENT)
220                                 log_warning("Failed to read /etc/sysconfig/i18n: %s", strerror(-r));
221                 }
222
223                 if (access("/etc/sysconfig/console/default.kmap", F_OK) >= 0) {
224                         char *t;
225
226                         if (!(t = strdup("/etc/sysconfig/console/default.kmap"))) {
227                                 log_error("Out of memory.");
228                                 goto finish;
229                         }
230
231                         free(vc_keymap);
232                         vc_keymap = t;
233                 }
234
235 #elif defined(TARGET_SUSE)
236                 if ((r = parse_env_file("/etc/sysconfig/keyboard", NEWLINE,
237                                         "KEYTABLE", &vc_keymap,
238                                         NULL)) < 0) {
239
240                         if (r != -ENOENT)
241                                 log_warning("Failed to read /etc/sysconfig/keyboard: %s", strerror(-r));
242                 }
243
244                 if ((r = parse_env_file("/etc/sysconfig/console", NEWLINE,
245                                         "CONSOLE_FONT", &vc_font,
246                                         "CONSOLE_SCREENMAP", &vc_font_map,
247                                         "CONSOLE_UNICODEMAP", &vc_font_unimap,
248                                         NULL)) < 0) {
249
250                         if (r != -ENOENT)
251                                 log_warning("Failed to read /etc/sysconfig/console: %s", strerror(-r));
252                 }
253
254 #elif defined(TARGET_ARCH)
255                 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
256                                         "KEYMAP", &vc_keymap,
257                                         "CONSOLEFONT", &vc_font,
258                                         "CONSOLEMAP", &vc_font_map,
259                                         NULL)) < 0) {
260
261                         if (r != -ENOENT)
262                                 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
263                 }
264
265 #elif defined(TARGET_GENTOO)
266                 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
267                                         "unicode", &vc_unicode,
268                                         NULL)) < 0) {
269                         if (r != -ENOENT)
270                                 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
271                 }
272
273                 if (vc_unicode) {
274                         int rc_unicode;
275
276                         if ((rc_unicode = parse_boolean(vc_unicode)) < 0)
277                                 log_error("Unknown value for /etc/rc.conf unicode=%s", vc_unicode);
278                         else {
279                                 if (rc_unicode && !utf8)
280                                         log_warning("/etc/rc.conf wants unicode, but current locale is not UTF-8 capable!");
281                                 else if (!rc_unicode && utf8) {
282                                         log_debug("/etc/rc.conf does not want unicode, leave it on in kernel but does not apply to vconsole.");
283                                         utf8 = false;
284                                 }
285                         }
286                 }
287
288                 /* /etc/conf.d/consolefont comments and gentoo
289                  * documentation mention uppercase, but the actual
290                  * contents are lowercase.  the existing
291                  * /etc/init.d/consolefont tries both
292                  */
293                 if ((r = parse_env_file("/etc/conf.d/consolefont", NEWLINE,
294                                         "CONSOLEFONT", &vc_font,
295                                         "consolefont", &vc_font,
296                                         "consoletranslation", &vc_font_map,
297                                         "CONSOLETRANSLATION", &vc_font_map,
298                                         "unicodemap", &vc_font_unimap,
299                                         "UNICODEMAP", &vc_font_unimap,
300                                         NULL)) < 0) {
301                         if (r != -ENOENT)
302                                 log_warning("Failed to read /etc/conf.d/consolefont: %s", strerror(-r));
303                 }
304
305                 if ((r = parse_env_file("/etc/conf.d/keymaps", NEWLINE,
306                                         "keymap", &vc_keymap,
307                                         "KEYMAP", &vc_keymap,
308                                         NULL)) < 0) {
309                         if (r != -ENOENT)
310                                 log_warning("Failed to read /etc/conf.d/keymaps: %s", strerror(-r));
311                 }
312 #endif
313         }
314
315         if (!vc_keymap)
316                 vc_keymap = strdup("us");
317         if (!vc_font)
318                 vc_font = strdup(DEFAULT_FONT);
319
320         if (!vc_keymap || !vc_font) {
321                 log_error("Failed to allocate strings.");
322                 goto finish;
323         }
324
325         if (!utf8)
326                 disable_utf8(fd);
327
328         if (load_keymap(vc, vc_keymap, utf8, &keymap_pid) >= 0 &&
329             load_font(vc, vc_font, vc_font_map, vc_font_unimap, &font_pid) >= 0)
330                 r = EXIT_SUCCESS;
331
332 finish:
333         if (keymap_pid > 0)
334                 wait_for_terminate_and_warn(KBD_LOADKEYS, keymap_pid);
335
336         if (font_pid > 0)
337                 wait_for_terminate_and_warn(KBD_SETFONT, font_pid);
338
339         free(vc_keymap);
340         free(vc_font);
341         free(vc_font_map);
342         free(vc_font_unimap);
343
344         if (fd >= 0)
345                 close_nointr_nofail(fd);
346
347         return r;
348 }