1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
|
/***************************************************************************
*
* probe-xkb.c : Probe for keyboard device information
*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Licensed under the Academic Free License version 2.1
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <errno.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <sys/stropts.h>
#include <fcntl.h>
#include <unistd.h>
#include <priv.h>
#include <sys/kbd.h>
#include <sys/kbio.h>
#include <libhal.h>
#include <logger.h>
#define MAXLINELEN 256
#define COMMENTCHAR '#'
#define KBD_DEFAULT_DEVICE "/dev/kbd"
#define XKBTABLE_PATH "/usr/X11/lib/X11/xkb/xkbtable.map"
static int global_linenumber = 0;
static char line[MAXLINELEN + 1];
static void
drop_privileges()
{
priv_set_t *pPrivSet = NULL;
priv_set_t *lPrivSet = NULL;
/*
* Start with the 'basic' privilege set and then remove any
* of the 'basic' privileges that will not be needed.
*/
if ((pPrivSet = priv_str_to_set("basic", ",", NULL)) == NULL) {
HAL_INFO(("Error in setting the priv"));
return;
}
/* Clear privileges we will not need from the 'basic' set */
(void) priv_delset(pPrivSet, PRIV_FILE_LINK_ANY);
(void) priv_delset(pPrivSet, PRIV_PROC_INFO);
(void) priv_delset(pPrivSet, PRIV_PROC_SESSION);
(void) priv_delset(pPrivSet, PRIV_PROC_EXEC);
(void) priv_delset(pPrivSet, PRIV_PROC_FORK);
(void) priv_addset(pPrivSet, PRIV_SYS_DEVICES);
(void) priv_addset(pPrivSet, PRIV_FILE_DAC_READ);
/* Set the permitted privilege set. */
if (setppriv(PRIV_SET, PRIV_PERMITTED, pPrivSet) != 0) {
return;
}
/* Clear the limit set. */
if ((lPrivSet = priv_allocset()) == NULL) {
return;
}
priv_emptyset(lPrivSet);
if (setppriv(PRIV_SET, PRIV_LIMIT, lPrivSet) != 0) {
return;
}
priv_freeset(lPrivSet);
priv_freeset(pPrivSet);
}
static int
get_kbd_layout_type(char *device_file, int *kbd_type, int *kbd_layout)
{
int ret = 1;
int fd = -1;
if ((fd = open(device_file, O_RDONLY | O_NONBLOCK)) < 0) {
HAL_DEBUG(("Cannot open %s: %s", device_file, strerror(errno)));
goto out;
}
/*
* For usb keyboard devices, we need to first push "usbkbm" module upon
* the stream.
*/
if (strstr(device_file, "hid") != NULL) {
if (ioctl(fd, I_FIND, "usbkbm") == 0) {
(void) ioctl(fd, I_PUSH, "usbkbm");
HAL_DEBUG(("usbkbm module has been pushed %s", strerror(errno)));
}
}
if (ioctl(fd, KIOCTYPE, kbd_type) < 0) {
HAL_DEBUG(("get keyboard type failed %s: %s",
device_file, strerror(errno)));
goto out;
}
if (ioctl(fd, KIOCLAYOUT, kbd_layout) < 0) {
HAL_DEBUG(("get keyboard layout failed %s: %s",
device_file, strerror(errno)));
goto out;
}
ret = 0;
out: if (fd >= 0) {
close(fd);
}
return (ret);
}
/* Skips over the white space character in the string. */
static char *
skipwhite(char *ptr)
{
while ((*ptr == ' ') || (*ptr == '\t')) {
ptr++;
}
/* This should not occur. but .. */
if (*ptr == '\n') {
ptr = '\0';
}
return (ptr);
}
static char *
getaline(FILE *fp)
{
char *ptr;
char *tmp;
int index;
int c;
while (1) {
ptr = fgets(line, MAXLINELEN, fp);
if (!ptr) {
(void) fclose(fp);
return (NULL);
}
global_linenumber++;
/* Comment line */
if (ptr[0] == COMMENTCHAR) {
continue;
}
/* Blank line */
if (ptr[0] == '\n') {
continue;
}
if ((tmp = strchr(ptr, '#')) != NULL) {
*tmp = '\0';
}
if (ptr[strlen(ptr) - 1] == '\n') {
/* get rid of '\n' */
ptr[strlen(ptr) - 1] = '\0';
}
ptr = skipwhite(ptr);
if (*ptr) {
break;
}
}
return (ptr);
}
static int
sun_find_xkbnames(int kb_type, int kb_layout, char **xkb_keymap,
char **xkb_model, char **xkb_layout)
{
const char *type, *layout;
char *keymap, *defkeymap = NULL;
char *model, *defmodel = NULL;
char *xkblay, *defxkblay = NULL;
FILE *fp;
int found_error = 0, found_keytable = 0;
int ret = 1;
if ((fp = fopen(XKBTABLE_PATH, "r")) == NULL) {
return (ret);
}
global_linenumber = 0;
while (getaline(fp)) {
if ((type = strtok(line, " \t\n")) == NULL) {
found_error = 1;
}
if ((layout = strtok(NULL, " \t\n")) == NULL) {
found_error = 1;
}
if ((keymap = strtok(NULL, " \t\n")) == NULL) {
found_error = 1;
}
/* These two are optional entries */
model = strtok(NULL, " \t\n");
if ((model == NULL) || (*model == COMMENTCHAR)) {
model = xkblay = NULL;
} else {
xkblay = strtok(NULL, " \t\n");
if ((xkblay != NULL) && (*xkblay == COMMENTCHAR)) {
xkblay = NULL;
}
}
if (found_error) {
found_error = 0;
continue;
}
/* record default entry if/when found */
if (*type == '*') {
if (defkeymap == NULL) {
defkeymap = strdup(keymap);
defmodel = strdup(model);
defxkblay = strdup(xkblay);
}
} else if (atoi(type) == kb_type) {
if (*layout == '*') {
defkeymap = strdup(keymap);
defmodel = strdup(model);
defxkblay = strdup(xkblay);
} else if (atoi(layout) == kb_layout) {
found_keytable = 1;
break;
}
}
}
(void) fclose(fp);
if (!found_keytable) {
keymap = defkeymap;
model = defmodel;
xkblay = defxkblay;
}
if ((keymap != NULL) && (strcmp(keymap, "-") != 0)) {
*xkb_keymap = keymap;
}
if ((model != NULL) && (strcmp(model, "-") != 0)) {
*xkb_model = model;
}
if ((xkblay != NULL) && (strcmp(xkblay, "-") != 0)) {
*xkb_layout = xkblay;
}
return (0);
}
int
main(int argc, char *argv[])
{
int ret = 1;
char *udi;
char *device_file;
LibHalContext *ctx = NULL;
LibHalChangeSet *cs = NULL;
DBusError error;
int kbd_type, kbd_layout;
char *xkbkeymap = NULL, *xkbmodel = NULL, *xkblayout = NULL;
if ((udi = getenv("UDI")) == NULL) {
goto out;
}
if ((device_file = getenv("HAL_PROP_INPUT_DEVICE")) == NULL) {
goto out;
}
drop_privileges();
setup_logger();
dbus_error_init(&error);
if ((ctx = libhal_ctx_init_direct(&error)) == NULL) {
goto out;
}
if ((cs = libhal_device_new_changeset(udi)) == NULL) {
HAL_DEBUG(("Cannot allocate changeset"));
goto out;
}
HAL_DEBUG(("Doing probe-xkb for %s (udi=%s)", device_file, udi));
if (get_kbd_layout_type(device_file, &kbd_type, &kbd_layout)) {
goto out;
}
/*
* For some usb keyboard that is not self-identifying, get keyboard's
* layout and type from system default keyboard device--/dev/kbd.
*/
if ((kbd_layout == 0) && (strstr(device_file, "hid") != NULL)) {
if (get_kbd_layout_type(KBD_DEFAULT_DEVICE,
&kbd_type, &kbd_layout)) {
goto out;
}
}
if (sun_find_xkbnames(kbd_type, kbd_layout,
&xkbkeymap, &xkbmodel, &xkblayout)) {
goto out;
}
/*
* If doesn't find matching entry in xkbtable.map, using default
* values setting in 10-x11-input.fdi
*/
if ((xkbmodel != NULL) && (xkblayout != NULL)) {
libhal_changeset_set_property_string(cs,
"input.x11_options.XkbModel", xkbmodel);
libhal_changeset_set_property_string(cs,
"input.x11_options.XkbLayout", xkblayout);
libhal_device_commit_changeset(ctx, cs, &error);
}
ret = 0;
out:
if (cs != NULL) {
libhal_device_free_changeset(cs);
}
if (ctx != NULL) {
libhal_ctx_shutdown(ctx, &error);
libhal_ctx_free(ctx);
if (dbus_error_is_set(&error)) {
dbus_error_free(&error);
}
}
return (ret);
}
|