blob: bde906bc6e3c2a4c1fbb49df853fbd1d1be043d6 [file] [log] [blame]
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22
23#include <fcntl.h>
24#include <dirent.h>
25#include <unistd.h>
26#include <string.h>
27
28#include <sys/socket.h>
29#include <sys/un.h>
30#include <linux/netlink.h>
31#include <private/android_filesystem_config.h>
32#include <sys/time.h>
33#include <asm/page.h>
34
35#include "init.h"
36#include "devices.h"
37
38#define CMDLINE_PREFIX "/dev"
39#define SYSFS_PREFIX "/sys"
40#define FIRMWARE_DIR "/etc/firmware"
41#define MAX_QEMU_PERM 6
42
43struct uevent {
44 const char *action;
45 const char *path;
46 const char *subsystem;
47 const char *firmware;
48 int major;
49 int minor;
50};
51
52static int open_uevent_socket(void)
53{
54 struct sockaddr_nl addr;
55 int sz = 64*1024; // XXX larger? udev uses 16MB!
56 int s;
57
58 memset(&addr, 0, sizeof(addr));
59 addr.nl_family = AF_NETLINK;
60 addr.nl_pid = getpid();
61 addr.nl_groups = 0xffffffff;
62
63 s = socket(PF_NETLINK, SOCK_DGRAM, NETLINK_KOBJECT_UEVENT);
64 if(s < 0)
65 return -1;
66
67 setsockopt(s, SOL_SOCKET, SO_RCVBUFFORCE, &sz, sizeof(sz));
68
69 if(bind(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70 close(s);
71 return -1;
72 }
73
74 return s;
75}
76
77struct perms_ {
78 char *name;
79 mode_t perm;
80 unsigned int uid;
81 unsigned int gid;
82 unsigned short prefix;
83};
84static struct perms_ devperms[] = {
85 { "/dev/null", 0666, AID_ROOT, AID_ROOT, 0 },
86 { "/dev/zero", 0666, AID_ROOT, AID_ROOT, 0 },
87 { "/dev/full", 0666, AID_ROOT, AID_ROOT, 0 },
88 { "/dev/ptmx", 0666, AID_ROOT, AID_ROOT, 0 },
89 { "/dev/tty", 0666, AID_ROOT, AID_ROOT, 0 },
90 { "/dev/random", 0666, AID_ROOT, AID_ROOT, 0 },
91 { "/dev/urandom", 0666, AID_ROOT, AID_ROOT, 0 },
92 { "/dev/ashmem", 0666, AID_ROOT, AID_ROOT, 0 },
93 { "/dev/binder", 0666, AID_ROOT, AID_ROOT, 0 },
94
95 /* logger should be world writable (for logging) but not readable */
96 { "/dev/log/", 0662, AID_ROOT, AID_LOG, 1 },
97
Dima Zavin46e88632009-08-03 17:08:25 -070098 /* the msm hw3d client device node is world writable/readable. */
99 { "/dev/msm_hw3dc", 0666, AID_ROOT, AID_ROOT, 0 },
100
Dima Zavin37b210f2009-08-10 16:34:04 -0700101 /* gpu driver for adreno200 is globally accessible */
102 { "/dev/kgsl", 0666, AID_ROOT, AID_ROOT, 0 },
103
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700104 /* these should not be world writable */
Iliyan Malchev6ed11eb2009-08-05 09:50:12 -0700105 { "/dev/diag", 0660, AID_RADIO, AID_RADIO, 0 },
106 { "/dev/diag_arm9", 0660, AID_RADIO, AID_RADIO, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700107 { "/dev/android_adb", 0660, AID_ADB, AID_ADB, 0 },
108 { "/dev/android_adb_enable", 0660, AID_ADB, AID_ADB, 0 },
The Android Open Source Project35237d12008-12-17 18:08:08 -0800109 { "/dev/ttyMSM0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 },
110 { "/dev/ttyHS0", 0600, AID_BLUETOOTH, AID_BLUETOOTH, 0 },
Ken Schultzbe65da42009-07-15 17:01:47 -0500111 { "/dev/uinput", 0660, AID_SYSTEM, AID_BLUETOOTH, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700112 { "/dev/alarm", 0664, AID_SYSTEM, AID_RADIO, 0 },
The Android Open Source Project35237d12008-12-17 18:08:08 -0800113 { "/dev/tty0", 0660, AID_ROOT, AID_SYSTEM, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700114 { "/dev/graphics/", 0660, AID_ROOT, AID_GRAPHICS, 1 },
Dima Zavin46e88632009-08-03 17:08:25 -0700115 { "/dev/msm_hw3dm", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700116 { "/dev/input/", 0660, AID_ROOT, AID_INPUT, 1 },
117 { "/dev/eac", 0660, AID_ROOT, AID_AUDIO, 0 },
118 { "/dev/cam", 0660, AID_ROOT, AID_CAMERA, 0 },
119 { "/dev/pmem", 0660, AID_SYSTEM, AID_GRAPHICS, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700120 { "/dev/pmem_adsp", 0660, AID_SYSTEM, AID_AUDIO, 1 },
121 { "/dev/pmem_camera", 0660, AID_SYSTEM, AID_CAMERA, 1 },
122 { "/dev/oncrpc/", 0660, AID_ROOT, AID_SYSTEM, 1 },
123 { "/dev/adsp/", 0660, AID_SYSTEM, AID_AUDIO, 1 },
Sean McNeil102f6d22009-06-10 21:59:02 +0700124 { "/dev/snd/", 0660, AID_SYSTEM, AID_AUDIO, 1 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700125 { "/dev/mt9t013", 0660, AID_SYSTEM, AID_SYSTEM, 0 },
Iliyan Malchevfc0182e2009-05-01 10:25:05 -0700126 { "/dev/msm_camera/", 0660, AID_SYSTEM, AID_SYSTEM, 1 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700127 { "/dev/akm8976_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 },
128 { "/dev/akm8976_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 },
Iliyan Malchev9a52eda2009-06-25 16:47:31 -0700129 { "/dev/akm8973_daemon",0640, AID_COMPASS, AID_SYSTEM, 0 },
130 { "/dev/akm8973_aot", 0640, AID_COMPASS, AID_SYSTEM, 0 },
131 { "/dev/bma150", 0640, AID_COMPASS, AID_SYSTEM, 0 },
Iliyan Malchev3306e062009-07-07 19:42:46 -0700132 { "/dev/cm3602", 0640, AID_COMPASS, AID_SYSTEM, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700133 { "/dev/akm8976_pffd", 0640, AID_COMPASS, AID_SYSTEM, 0 },
Iliyan Malchev3bc1e9c2009-08-13 11:08:34 -0700134 { "/dev/lightsensor", 0640, AID_SYSTEM, AID_SYSTEM, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700135 { "/dev/msm_pcm_out", 0660, AID_SYSTEM, AID_AUDIO, 1 },
136 { "/dev/msm_pcm_in", 0660, AID_SYSTEM, AID_AUDIO, 1 },
137 { "/dev/msm_pcm_ctl", 0660, AID_SYSTEM, AID_AUDIO, 1 },
The Android Open Source Project5ae090e2009-01-09 17:51:25 -0800138 { "/dev/msm_snd", 0660, AID_SYSTEM, AID_AUDIO, 1 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700139 { "/dev/msm_mp3", 0660, AID_SYSTEM, AID_AUDIO, 1 },
Brian Swetlandc8222132009-09-01 17:23:07 -0700140 { "/dev/audience_a1026", 0660, AID_SYSTEM, AID_AUDIO, 1 },
Iliyan Malchev1340a092010-02-02 13:57:07 -0800141 { "/dev/tpa2018d1", 0660, AID_SYSTEM, AID_AUDIO, 1 },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800142 { "/dev/msm_audpre", 0660, AID_SYSTEM, AID_AUDIO, 0 },
Brian Swetlandc8222132009-09-01 17:23:07 -0700143 { "/dev/msm_audio_ctl", 0660, AID_SYSTEM, AID_AUDIO, 0 },
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -0800144 { "/dev/htc-acoustic", 0660, AID_SYSTEM, AID_AUDIO, 0 },
Dima Zavinc8401672009-08-31 13:40:23 -0700145 { "/dev/vdec", 0660, AID_SYSTEM, AID_AUDIO, 0 },
Dima Zavin8234aa32009-09-28 14:39:56 -0700146 { "/dev/q6venc", 0660, AID_SYSTEM, AID_AUDIO, 0 },
Masaki Sato1ccef182009-05-29 17:18:01 -0500147 { "/dev/snd/dsp", 0660, AID_SYSTEM, AID_AUDIO, 0 },
148 { "/dev/snd/dsp1", 0660, AID_SYSTEM, AID_AUDIO, 0 },
149 { "/dev/snd/mixer", 0660, AID_SYSTEM, AID_AUDIO, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700150 { "/dev/smd0", 0640, AID_RADIO, AID_RADIO, 0 },
Jack Veenstra4a762352009-05-05 11:48:17 -0700151 { "/dev/qemu_trace", 0666, AID_SYSTEM, AID_SYSTEM, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700152 { "/dev/qmi", 0640, AID_RADIO, AID_RADIO, 0 },
153 { "/dev/qmi0", 0640, AID_RADIO, AID_RADIO, 0 },
154 { "/dev/qmi1", 0640, AID_RADIO, AID_RADIO, 0 },
155 { "/dev/qmi2", 0640, AID_RADIO, AID_RADIO, 0 },
Erik Gillinge910ea52009-05-08 15:41:30 -0700156 /* CDMA radio interface MUX */
157 { "/dev/ts0710mux", 0640, AID_RADIO, AID_RADIO, 1 },
Chia-chi Yeh88dc6572009-06-19 14:59:08 +0800158 { "/dev/ppp", 0660, AID_RADIO, AID_VPN, 0 },
159 { "/dev/tun", 0640, AID_VPN, AID_VPN, 0 },
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700160 { NULL, 0, 0, 0, 0 },
161};
162
163/* devperms_partners list and perm_node are for hardware specific /dev entries */
164struct perm_node {
165 struct perms_ dp;
166 struct listnode plist;
167};
168list_declare(devperms_partners);
169
170/*
171 * Permission override when in emulator mode, must be parsed before
172 * system properties is initalized.
173 */
174static int qemu_perm_count;
175static struct perms_ qemu_perms[MAX_QEMU_PERM + 1];
176
177int add_devperms_partners(const char *name, mode_t perm, unsigned int uid,
178 unsigned int gid, unsigned short prefix) {
179 int size;
180 struct perm_node *node = malloc(sizeof (struct perm_node));
181 if (!node)
182 return -ENOMEM;
183
184 size = strlen(name) + 1;
185 if ((node->dp.name = malloc(size)) == NULL)
186 return -ENOMEM;
187
188 memcpy(node->dp.name, name, size);
189 node->dp.perm = perm;
190 node->dp.uid = uid;
191 node->dp.gid = gid;
192 node->dp.prefix = prefix;
193
194 list_add_tail(&devperms_partners, &node->plist);
195 return 0;
196}
197
198void qemu_init(void) {
199 qemu_perm_count = 0;
200 memset(&qemu_perms, 0, sizeof(qemu_perms));
201}
202
203static int qemu_perm(const char* name, mode_t perm, unsigned int uid,
204 unsigned int gid, unsigned short prefix)
205{
206 char *buf;
207 if (qemu_perm_count == MAX_QEMU_PERM)
208 return -ENOSPC;
209
210 buf = malloc(strlen(name) + 1);
211 if (!buf)
212 return -errno;
213
214 strlcpy(buf, name, strlen(name) + 1);
215 qemu_perms[qemu_perm_count].name = buf;
216 qemu_perms[qemu_perm_count].perm = perm;
217 qemu_perms[qemu_perm_count].uid = uid;
218 qemu_perms[qemu_perm_count].gid = gid;
219 qemu_perms[qemu_perm_count].prefix = prefix;
220
221 qemu_perm_count++;
222 return 0;
223}
224
225/* Permission overrides for emulator that are parsed from /proc/cmdline. */
226void qemu_cmdline(const char* name, const char *value)
227{
228 char *buf;
229 if (!strcmp(name, "android.ril")) {
230 /* cmd line params currently assume /dev/ prefix */
231 if (asprintf(&buf, CMDLINE_PREFIX"/%s", value) == -1) {
232 return;
233 }
234 INFO("nani- buf:: %s\n", buf);
235 qemu_perm(buf, 0660, AID_RADIO, AID_ROOT, 0);
236 }
237}
238
239static int get_device_perm_inner(struct perms_ *perms, const char *path,
240 unsigned *uid, unsigned *gid, mode_t *perm)
241{
242 int i;
243 for(i = 0; perms[i].name; i++) {
244
245 if(perms[i].prefix) {
246 if(strncmp(path, perms[i].name, strlen(perms[i].name)))
247 continue;
248 } else {
249 if(strcmp(path, perms[i].name))
250 continue;
251 }
252 *uid = perms[i].uid;
253 *gid = perms[i].gid;
254 *perm = perms[i].perm;
255 return 0;
256 }
257 return -1;
258}
259
260/* First checks for emulator specific permissions specified in /proc/cmdline. */
261static mode_t get_device_perm(const char *path, unsigned *uid, unsigned *gid)
262{
263 mode_t perm;
264
265 if (get_device_perm_inner(qemu_perms, path, uid, gid, &perm) == 0) {
266 return perm;
267 } else if (get_device_perm_inner(devperms, path, uid, gid, &perm) == 0) {
268 return perm;
269 } else {
270 struct listnode *node;
271 struct perm_node *perm_node;
272 struct perms_ *dp;
273
274 /* Check partners list. */
275 list_for_each(node, &devperms_partners) {
276 perm_node = node_to_item(node, struct perm_node, plist);
277 dp = &perm_node->dp;
278
279 if (dp->prefix) {
280 if (strncmp(path, dp->name, strlen(dp->name)))
281 continue;
282 } else {
283 if (strcmp(path, dp->name))
284 continue;
285 }
286 /* Found perm in partner list. */
287 *uid = dp->uid;
288 *gid = dp->gid;
289 return dp->perm;
290 }
291 /* Default if nothing found. */
292 *uid = 0;
293 *gid = 0;
294 return 0600;
295 }
296}
297
298static void make_device(const char *path, int block, int major, int minor)
299{
300 unsigned uid;
301 unsigned gid;
302 mode_t mode;
303 dev_t dev;
304
305 if(major > 255 || minor > 255)
306 return;
307
308 mode = get_device_perm(path, &uid, &gid) | (block ? S_IFBLK : S_IFCHR);
309 dev = (major << 8) | minor;
Nick Pelly6405c692010-01-21 18:13:39 -0800310 /* Temporarily change egid to avoid race condition setting the gid of the
311 * device node. Unforunately changing the euid would prevent creation of
312 * some device nodes, so the uid has to be set with chown() and is still
313 * racy. Fixing the gid race at least fixed the issue with system_server
314 * opening dynamic input devices under the AID_INPUT gid. */
315 setegid(gid);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700316 mknod(path, mode, dev);
Nick Pelly6405c692010-01-21 18:13:39 -0800317 chown(path, uid, -1);
318 setegid(AID_ROOT);
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700319}
320
Chuck Tuffli1e070842008-12-15 14:26:56 -0800321#if LOG_UEVENTS
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700322
323static inline suseconds_t get_usecs(void)
324{
325 struct timeval tv;
326 gettimeofday(&tv, 0);
327 return tv.tv_sec * (suseconds_t) 1000000 + tv.tv_usec;
328}
329
330#define log_event_print(x...) INFO(x)
331
332#else
333
334#define log_event_print(fmt, args...) do { } while (0)
335#define get_usecs() 0
336
337#endif
338
339static void parse_event(const char *msg, struct uevent *uevent)
340{
341 uevent->action = "";
342 uevent->path = "";
343 uevent->subsystem = "";
344 uevent->firmware = "";
345 uevent->major = -1;
346 uevent->minor = -1;
347
348 /* currently ignoring SEQNUM */
349 while(*msg) {
350 if(!strncmp(msg, "ACTION=", 7)) {
351 msg += 7;
352 uevent->action = msg;
353 } else if(!strncmp(msg, "DEVPATH=", 8)) {
354 msg += 8;
355 uevent->path = msg;
356 } else if(!strncmp(msg, "SUBSYSTEM=", 10)) {
357 msg += 10;
358 uevent->subsystem = msg;
359 } else if(!strncmp(msg, "FIRMWARE=", 9)) {
360 msg += 9;
361 uevent->firmware = msg;
362 } else if(!strncmp(msg, "MAJOR=", 6)) {
363 msg += 6;
364 uevent->major = atoi(msg);
365 } else if(!strncmp(msg, "MINOR=", 6)) {
366 msg += 6;
367 uevent->minor = atoi(msg);
368 }
369
370 /* advance to after the next \0 */
371 while(*msg++)
372 ;
373 }
374
375 log_event_print("event { '%s', '%s', '%s', '%s', %d, %d }\n",
376 uevent->action, uevent->path, uevent->subsystem,
377 uevent->firmware, uevent->major, uevent->minor);
378}
379
380static void handle_device_event(struct uevent *uevent)
381{
382 char devpath[96];
383 char *base, *name;
384 int block;
385
386 /* if it's not a /dev device, nothing to do */
387 if((uevent->major < 0) || (uevent->minor < 0))
388 return;
389
390 /* do we have a name? */
391 name = strrchr(uevent->path, '/');
392 if(!name)
393 return;
394 name++;
395
396 /* too-long names would overrun our buffer */
397 if(strlen(name) > 64)
398 return;
399
400 /* are we block or char? where should we live? */
The Android Open Source Project35237d12008-12-17 18:08:08 -0800401 if(!strncmp(uevent->subsystem, "block", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700402 block = 1;
403 base = "/dev/block/";
404 mkdir(base, 0755);
405 } else {
406 block = 0;
407 /* this should probably be configurable somehow */
The Android Open Source Project35237d12008-12-17 18:08:08 -0800408 if(!strncmp(uevent->subsystem, "graphics", 8)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700409 base = "/dev/graphics/";
410 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800411 } else if (!strncmp(uevent->subsystem, "oncrpc", 6)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700412 base = "/dev/oncrpc/";
413 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800414 } else if (!strncmp(uevent->subsystem, "adsp", 4)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700415 base = "/dev/adsp/";
416 mkdir(base, 0755);
Iliyan Malchevfc0182e2009-05-01 10:25:05 -0700417 } else if (!strncmp(uevent->subsystem, "msm_camera", 10)) {
418 base = "/dev/msm_camera/";
419 mkdir(base, 0755);
420 } else if(!strncmp(uevent->subsystem, "input", 5)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700421 base = "/dev/input/";
422 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800423 } else if(!strncmp(uevent->subsystem, "mtd", 3)) {
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700424 base = "/dev/mtd/";
425 mkdir(base, 0755);
Xiaopeng Yang5bb44c82008-11-25 16:20:07 -0800426 } else if(!strncmp(uevent->subsystem, "sound", 5)) {
427 base = "/dev/snd/";
428 mkdir(base, 0755);
The Android Open Source Project35237d12008-12-17 18:08:08 -0800429 } else if(!strncmp(uevent->subsystem, "misc", 4) &&
The Android Open Source Project4f6e8d72008-10-21 07:00:00 -0700430 !strncmp(name, "log_", 4)) {
431 base = "/dev/log/";
432 mkdir(base, 0755);
433 name += 4;
434 } else
435 base = "/dev/";
436 }
437
438 snprintf(devpath, sizeof(devpath), "%s%s", base, name);
439
440 if(!strcmp(uevent->action, "add")) {
441 make_device(devpath, block, uevent->major, uevent->minor);
442 return;
443 }
444
445 if(!strcmp(uevent->action, "remove")) {
446 unlink(devpath);
447 return;
448 }
449}
450
451static int load_firmware(int fw_fd, int loading_fd, int data_fd)
452{
453 struct stat st;
454 long len_to_copy;
455 int ret = 0;
456
457 if(fstat(fw_fd, &st) < 0)
458 return -1;
459 len_to_copy = st.st_size;
460
461 write(loading_fd, "1", 1); /* start transfer */
462
463 while (len_to_copy > 0) {
464 char buf[PAGE_SIZE];
465 ssize_t nr;
466
467 nr = read(fw_fd, buf, sizeof(buf));
468 if(!nr)
469 break;
470 if(nr < 0) {
471 ret = -1;
472 break;
473 }
474
475 len_to_copy -= nr;
476 while (nr > 0) {
477 ssize_t nw = 0;
478
479 nw = write(data_fd, buf + nw, nr);
480 if(nw <= 0) {
481 ret = -1;
482 goto out;
483 }
484 nr -= nw;
485 }
486 }
487
488out:
489 if(!ret)
490 write(loading_fd, "0", 1); /* successful end of transfer */
491 else
492 write(loading_fd, "-1", 2); /* abort transfer */
493
494 return ret;
495}
496
497static void process_firmware_event(struct uevent *uevent)
498{
499 char *root, *loading, *data, *file;
500 int l, loading_fd, data_fd, fw_fd;
501
502 log_event_print("firmware event { '%s', '%s' }\n",
503 uevent->path, uevent->firmware);
504
505 l = asprintf(&root, SYSFS_PREFIX"%s/", uevent->path);
506 if (l == -1)
507 return;
508
509 l = asprintf(&loading, "%sloading", root);
510 if (l == -1)
511 goto root_free_out;
512
513 l = asprintf(&data, "%sdata", root);
514 if (l == -1)
515 goto loading_free_out;
516
517 l = asprintf(&file, FIRMWARE_DIR"/%s", uevent->firmware);
518 if (l == -1)
519 goto data_free_out;
520
521 loading_fd = open(loading, O_WRONLY);
522 if(loading_fd < 0)
523 goto file_free_out;
524
525 data_fd = open(data, O_WRONLY);
526 if(data_fd < 0)
527 goto loading_close_out;
528
529 fw_fd = open(file, O_RDONLY);
530 if(fw_fd < 0)
531 goto data_close_out;
532
533 if(!load_firmware(fw_fd, loading_fd, data_fd))
534 log_event_print("firmware copy success { '%s', '%s' }\n", root, file);
535 else
536 log_event_print("firmware copy failure { '%s', '%s' }\n", root, file);
537
538 close(fw_fd);
539data_close_out:
540 close(data_fd);
541loading_close_out:
542 close(loading_fd);
543file_free_out:
544 free(file);
545data_free_out:
546 free(data);
547loading_free_out:
548 free(loading);
549root_free_out:
550 free(root);
551}
552
553static void handle_firmware_event(struct uevent *uevent)
554{
555 pid_t pid;
556
557 if(strcmp(uevent->subsystem, "firmware"))
558 return;
559
560 if(strcmp(uevent->action, "add"))
561 return;
562
563 /* we fork, to avoid making large memory allocations in init proper */
564 pid = fork();
565 if (!pid) {
566 process_firmware_event(uevent);
567 exit(EXIT_SUCCESS);
568 }
569}
570
571#define UEVENT_MSG_LEN 1024
572void handle_device_fd(int fd)
573{
574 char msg[UEVENT_MSG_LEN+2];
575 int n;
576
577 while((n = recv(fd, msg, UEVENT_MSG_LEN, 0)) > 0) {
578 struct uevent uevent;
579
580 if(n == UEVENT_MSG_LEN) /* overflow -- discard */
581 continue;
582
583 msg[n] = '\0';
584 msg[n+1] = '\0';
585
586 parse_event(msg, &uevent);
587
588 handle_device_event(&uevent);
589 handle_firmware_event(&uevent);
590 }
591}
592
593/* Coldboot walks parts of the /sys tree and pokes the uevent files
594** to cause the kernel to regenerate device add events that happened
595** before init's device manager was started
596**
597** We drain any pending events from the netlink socket every time
598** we poke another uevent file to make sure we don't overrun the
599** socket's buffer.
600*/
601
602static void do_coldboot(int event_fd, DIR *d)
603{
604 struct dirent *de;
605 int dfd, fd;
606
607 dfd = dirfd(d);
608
609 fd = openat(dfd, "uevent", O_WRONLY);
610 if(fd >= 0) {
611 write(fd, "add\n", 4);
612 close(fd);
613 handle_device_fd(event_fd);
614 }
615
616 while((de = readdir(d))) {
617 DIR *d2;
618
619 if(de->d_type != DT_DIR || de->d_name[0] == '.')
620 continue;
621
622 fd = openat(dfd, de->d_name, O_RDONLY | O_DIRECTORY);
623 if(fd < 0)
624 continue;
625
626 d2 = fdopendir(fd);
627 if(d2 == 0)
628 close(fd);
629 else {
630 do_coldboot(event_fd, d2);
631 closedir(d2);
632 }
633 }
634}
635
636static void coldboot(int event_fd, const char *path)
637{
638 DIR *d = opendir(path);
639 if(d) {
640 do_coldboot(event_fd, d);
641 closedir(d);
642 }
643}
644
645int device_init(void)
646{
647 suseconds_t t0, t1;
648 int fd;
649
650 fd = open_uevent_socket();
651 if(fd < 0)
652 return -1;
653
654 fcntl(fd, F_SETFD, FD_CLOEXEC);
655 fcntl(fd, F_SETFL, O_NONBLOCK);
656
657 t0 = get_usecs();
658 coldboot(fd, "/sys/class");
659 coldboot(fd, "/sys/block");
660 coldboot(fd, "/sys/devices");
661 t1 = get_usecs();
662
663 log_event_print("coldboot %ld uS\n", ((long) (t1 - t0)));
664
665 return fd;
666}