chiark / gitweb /
udev: fix NULL-ptr deref
[elogind.git] / src / udev / udev-builtin-input_id.c
1 /*
2  * compose persistent device path
3  *
4  * Copyright (C) 2009 Martin Pitt <martin.pitt@ubuntu.com>
5  * Portions Copyright (C) 2004 David Zeuthen, <david@fubar.dk>
6  * Copyright (C) 2011 Kay Sievers <kay@vrfy.org>
7  * Copyright (C) 2014 David Herrmann <dh.herrmann@gmail.com>
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <linux/limits.h>
30 #include <linux/input.h>
31
32 #include "udev.h"
33
34 /* we must use this kernel-compatible implementation */
35 #define BITS_PER_LONG (sizeof(unsigned long) * 8)
36 #define NBITS(x) ((((x)-1)/BITS_PER_LONG)+1)
37 #define OFF(x)  ((x)%BITS_PER_LONG)
38 #define BIT(x)  (1UL<<OFF(x))
39 #define LONG(x) ((x)/BITS_PER_LONG)
40 #define test_bit(bit, array)    ((array[LONG(bit)] >> OFF(bit)) & 1)
41
42 /*
43  * Read a capability attribute and return bitmask.
44  * @param dev udev_device
45  * @param attr sysfs attribute name (e. g. "capabilities/key")
46  * @param bitmask: Output array which has a sizeof of bitmask_size
47  */
48 static void get_cap_mask(struct udev_device *dev,
49                          struct udev_device *pdev, const char* attr,
50                          unsigned long *bitmask, size_t bitmask_size,
51                          bool test) {
52         const char *v;
53         char text[4096];
54         unsigned i;
55         char* word;
56         unsigned long val;
57
58         v = udev_device_get_sysattr_value(pdev, attr);
59         if (!v)
60                 v = "";
61
62         snprintf(text, sizeof(text), "%s", v);
63         log_debug("%s raw kernel attribute: %s", attr, text);
64
65         memzero(bitmask, bitmask_size);
66         i = 0;
67         while ((word = strrchr(text, ' ')) != NULL) {
68                 val = strtoul (word+1, NULL, 16);
69                 if (i < bitmask_size/sizeof(unsigned long))
70                         bitmask[i] = val;
71                 else
72                         log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
73                 *word = '\0';
74                 ++i;
75         }
76         val = strtoul (text, NULL, 16);
77         if (i < bitmask_size / sizeof(unsigned long))
78                 bitmask[i] = val;
79         else
80                 log_debug("ignoring %s block %lX which is larger than maximum size", attr, val);
81
82         if (test) {
83                 /* printf pattern with the right unsigned long number of hex chars */
84                 snprintf(text, sizeof(text), "  bit %%4u: %%0%zilX\n", 2 * sizeof(unsigned long));
85                 log_debug("%s decoded bit map:", attr);
86                 val = bitmask_size / sizeof (unsigned long);
87                 /* skip over leading zeros */
88                 while (bitmask[val-1] == 0 && val > 0)
89                         --val;
90                 for (i = 0; i < val; ++i) {
91                         DISABLE_WARNING_FORMAT_NONLITERAL;
92                         log_debug(text, i * BITS_PER_LONG, bitmask[i]);
93                         REENABLE_WARNING;
94                 }
95         }
96 }
97
98 /* pointer devices */
99 static void test_pointers (struct udev_device *dev,
100                            const unsigned long* bitmask_ev,
101                            const unsigned long* bitmask_abs,
102                            const unsigned long* bitmask_key,
103                            const unsigned long* bitmask_rel,
104                            bool test) {
105         int is_mouse = 0;
106         int is_touchpad = 0;
107
108         if (!test_bit (EV_KEY, bitmask_ev)) {
109                 if (test_bit (EV_ABS, bitmask_ev) &&
110                     test_bit (ABS_X, bitmask_abs) &&
111                     test_bit (ABS_Y, bitmask_abs) &&
112                     test_bit (ABS_Z, bitmask_abs))
113                         udev_builtin_add_property(dev, test, "ID_INPUT_ACCELEROMETER", "1");
114                 return;
115         }
116
117         if (test_bit (EV_ABS, bitmask_ev) &&
118             test_bit (ABS_X, bitmask_abs) && test_bit (ABS_Y, bitmask_abs)) {
119                 if (test_bit (BTN_STYLUS, bitmask_key) || test_bit (BTN_TOOL_PEN, bitmask_key))
120                         udev_builtin_add_property(dev, test, "ID_INPUT_TABLET", "1");
121                 else if (test_bit (BTN_TOOL_FINGER, bitmask_key) && !test_bit (BTN_TOOL_PEN, bitmask_key))
122                         is_touchpad = 1;
123                 else if (test_bit (BTN_MOUSE, bitmask_key))
124                         /* This path is taken by VMware's USB mouse, which has
125                          * absolute axes, but no touch/pressure button. */
126                         is_mouse = 1;
127                 else if (test_bit (BTN_TOUCH, bitmask_key))
128                         udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHSCREEN", "1");
129                 /* joysticks don't necessarily have to have buttons; e. g.
130                  * rudders/pedals are joystick-like, but buttonless; they have
131                  * other fancy axes */
132                 else if (test_bit (BTN_TRIGGER, bitmask_key) ||
133                          test_bit (BTN_A, bitmask_key) ||
134                          test_bit (BTN_1, bitmask_key) ||
135                          test_bit (ABS_RX, bitmask_abs) ||
136                          test_bit (ABS_RY, bitmask_abs) ||
137                          test_bit (ABS_RZ, bitmask_abs) ||
138                          test_bit (ABS_THROTTLE, bitmask_abs) ||
139                          test_bit (ABS_RUDDER, bitmask_abs) ||
140                          test_bit (ABS_WHEEL, bitmask_abs) ||
141                          test_bit (ABS_GAS, bitmask_abs) ||
142                          test_bit (ABS_BRAKE, bitmask_abs))
143                         udev_builtin_add_property(dev, test, "ID_INPUT_JOYSTICK", "1");
144         }
145
146         if (test_bit (EV_REL, bitmask_ev) &&
147             test_bit (REL_X, bitmask_rel) && test_bit (REL_Y, bitmask_rel) &&
148             test_bit (BTN_MOUSE, bitmask_key))
149                 is_mouse = 1;
150
151         if (is_mouse)
152                 udev_builtin_add_property(dev, test, "ID_INPUT_MOUSE", "1");
153         if (is_touchpad)
154                 udev_builtin_add_property(dev, test, "ID_INPUT_TOUCHPAD", "1");
155 }
156
157 /* key like devices */
158 static void test_key (struct udev_device *dev,
159                       const unsigned long* bitmask_ev,
160                       const unsigned long* bitmask_key,
161                       bool test) {
162         unsigned i;
163         unsigned long found;
164         unsigned long mask;
165
166         /* do we have any KEY_* capability? */
167         if (!test_bit (EV_KEY, bitmask_ev)) {
168                 log_debug("test_key: no EV_KEY capability");
169                 return;
170         }
171
172         /* only consider KEY_* here, not BTN_* */
173         found = 0;
174         for (i = 0; i < BTN_MISC/BITS_PER_LONG; ++i) {
175                 found |= bitmask_key[i];
176                 log_debug("test_key: checking bit block %lu for any keys; found=%i", (unsigned long)i*BITS_PER_LONG, found > 0);
177         }
178         /* If there are no keys in the lower block, check the higher block */
179         if (!found) {
180                 for (i = KEY_OK; i < BTN_TRIGGER_HAPPY; ++i) {
181                         if (test_bit (i, bitmask_key)) {
182                                 log_debug("test_key: Found key %x in high block", i);
183                                 found = 1;
184                                 break;
185                         }
186                 }
187         }
188
189         if (found > 0)
190                 udev_builtin_add_property(dev, test, "ID_INPUT_KEY", "1");
191
192         /* the first 32 bits are ESC, numbers, and Q to D; if we have all of
193          * those, consider it a full keyboard; do not test KEY_RESERVED, though */
194         mask = 0xFFFFFFFE;
195         if ((bitmask_key[0] & mask) == mask)
196                 udev_builtin_add_property(dev, test, "ID_INPUT_KEYBOARD", "1");
197 }
198
199 static int builtin_input_id(struct udev_device *dev, int argc, char *argv[], bool test) {
200         struct udev_device *pdev;
201         unsigned long bitmask_ev[NBITS(EV_MAX)];
202         unsigned long bitmask_abs[NBITS(ABS_MAX)];
203         unsigned long bitmask_key[NBITS(KEY_MAX)];
204         unsigned long bitmask_rel[NBITS(REL_MAX)];
205
206         /* walk up the parental chain until we find the real input device; the
207          * argument is very likely a subdevice of this, like eventN */
208         pdev = dev;
209         while (pdev != NULL && udev_device_get_sysattr_value(pdev, "capabilities/ev") == NULL)
210                 pdev = udev_device_get_parent_with_subsystem_devtype(pdev, "input", NULL);
211
212         /* not an "input" class device */
213         if (pdev == NULL)
214                 return EXIT_SUCCESS;
215
216         /* Use this as a flag that input devices were detected, so that this
217          * program doesn't need to be called more than once per device */
218         udev_builtin_add_property(dev, test, "ID_INPUT", "1");
219         get_cap_mask(dev, pdev, "capabilities/ev", bitmask_ev, sizeof(bitmask_ev), test);
220         get_cap_mask(dev, pdev, "capabilities/abs", bitmask_abs, sizeof(bitmask_abs), test);
221         get_cap_mask(dev, pdev, "capabilities/rel", bitmask_rel, sizeof(bitmask_rel), test);
222         get_cap_mask(dev, pdev, "capabilities/key", bitmask_key, sizeof(bitmask_key), test);
223         test_pointers(dev, bitmask_ev, bitmask_abs, bitmask_key, bitmask_rel, test);
224         test_key(dev, bitmask_ev, bitmask_key, test);
225         return EXIT_SUCCESS;
226 }
227
228 const struct udev_builtin udev_builtin_input_id = {
229         .name = "input_id",
230         .cmd = builtin_input_id,
231         .help = "Input device properties",
232 };