sv,svc: fix NOEXEC fallout
[oweals/busybox.git] / console-tools / dumpkmap.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini dumpkmap implementation for busybox
4  *
5  * Copyright (C) Arne Bernin <arne@matrix.loopback.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
8  *
9  */
10 //config:config DUMPKMAP
11 //config:       bool "dumpkmap (1.3 kb)"
12 //config:       default y
13 //config:       select PLATFORM_LINUX
14 //config:       help
15 //config:       This program dumps the kernel's keyboard translation table to
16 //config:       stdout, in binary format. You can then use loadkmap to load it.
17
18 //applet:IF_DUMPKMAP(APPLET_NOEXEC(dumpkmap, dumpkmap, BB_DIR_BIN, BB_SUID_DROP, dumpkmap))
19 /* bb_common_bufsiz1 usage here is safe wrt NOEXEC: not expecting it to be zeroed. */
20
21 //kbuild:lib-$(CONFIG_DUMPKMAP) += dumpkmap.o
22
23 //usage:#define dumpkmap_trivial_usage
24 //usage:       "> keymap"
25 //usage:#define dumpkmap_full_usage "\n\n"
26 //usage:       "Print a binary keyboard translation table to stdout"
27 //usage:
28 //usage:#define dumpkmap_example_usage
29 //usage:       "$ dumpkmap > keymap\n"
30
31 #include "libbb.h"
32 #include "common_bufsiz.h"
33
34 /* From <linux/kd.h> */
35 struct kbentry {
36         unsigned char kb_table;
37         unsigned char kb_index;
38         unsigned short kb_value;
39 };
40 #define KDGKBENT 0x4B46  /* gets one entry in translation table */
41
42 /* From <linux/keyboard.h> */
43 #define NR_KEYS 128
44 #define MAX_NR_KEYMAPS 256
45
46 int dumpkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
47 int dumpkmap_main(int argc UNUSED_PARAM, char **argv)
48 {
49         struct kbentry ke;
50         int i, j, fd;
51
52         /* When user accidentally runs "dumpkmap FILE"
53          * instead of "dumpkmap >FILE", we'd dump binary stuff to tty.
54          * Let's prevent it:
55          */
56         if (argv[1])
57                 bb_show_usage();
58 /*      bb_warn_ignoring_args(argv[1]);*/
59
60         fd = get_console_fd_or_die();
61
62 #define flags bb_common_bufsiz1
63         setup_common_bufsiz();
64         /*                     0 1 2 3 4 5 6 7 8 9 a b c=12 */
65         memcpy(flags, "bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1",
66         /* Can use sizeof, or sizeof-1. sizeof is even, using that */
67         /****/ sizeof("bkeymap\1\1\1\0\1\1\1\0\1\1\1\0\1")
68         );
69         write(STDOUT_FILENO, flags, 7 + MAX_NR_KEYMAPS);
70 #define flags7 (flags + 7)
71
72         for (i = 0; i < 13; i++) {
73                 if (flags7[i]) {
74                         for (j = 0; j < NR_KEYS; j++) {
75                                 ke.kb_index = j;
76                                 ke.kb_table = i;
77                                 if (!ioctl_or_perror(fd, KDGKBENT, &ke,
78                                                 "ioctl(KDGKBENT{%d,%d}) failed",
79                                                 j, i)
80                                 ) {
81                                         write(STDOUT_FILENO, &ke.kb_value, 2);
82                                 }
83                         }
84                 }
85         }
86         if (ENABLE_FEATURE_CLEAN_UP) {
87                 close(fd);
88         }
89         return EXIT_SUCCESS;
90 }