]>
Commit | Line | Data |
---|---|---|
dd64e26b | 1 | /* |
4a539daf | 2 | * dev_d.c - dev.d/ multiplexer |
dd64e26b GKH |
3 | * |
4 | * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation version 2 of the License. | |
dd64e26b GKH |
9 | */ |
10 | ||
4a539daf | 11 | /* |
dd64e26b | 12 | * This essentially emulates the following shell script logic in C: |
4a539daf | 13 | * DIR="/etc/dev.d" |
4b06c852 | 14 | * export DEVNAME="whatever_dev_name_udev_just_gave" |
15 | * for I in "${DIR}/$DEVNAME/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do | |
4a539daf KS |
16 | * if [ -f $I ]; then $I $1 ; fi |
17 | * done | |
18 | * exit 1; | |
dd64e26b GKH |
19 | */ |
20 | ||
dd64e26b GKH |
21 | #include <stdio.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <sys/types.h> | |
25 | #include <sys/wait.h> | |
6e3e3c34 | 26 | #include <sys/stat.h> |
dd64e26b | 27 | #include <unistd.h> |
6e3e3c34 HH |
28 | #include <fcntl.h> |
29 | ||
dd64e26b GKH |
30 | #include "udev.h" |
31 | #include "udev_lib.h" | |
6e3e3c34 | 32 | #include "udevdb.h" |
dd64e26b GKH |
33 | #include "logging.h" |
34 | ||
4a539daf KS |
35 | #define DEVD_DIR "/etc/dev.d/" |
36 | #define DEVD_SUFFIX ".dev" | |
dd64e26b | 37 | |
4a539daf | 38 | static int run_program(char *name) |
dd64e26b GKH |
39 | { |
40 | pid_t pid; | |
6e3e3c34 | 41 | int fd; |
dd64e26b GKH |
42 | |
43 | dbg("running %s", name); | |
44 | ||
45 | pid = fork(); | |
4a539daf KS |
46 | switch (pid) { |
47 | case 0: | |
48 | /* child */ | |
6e3e3c34 HH |
49 | udevdb_exit(); /* close udevdb */ |
50 | fd = open("/dev/null", O_RDWR); | |
51 | if ( fd >= 0) { | |
52 | dup2(fd, STDOUT_FILENO); | |
53 | dup2(fd, STDIN_FILENO); | |
54 | dup2(fd, STDERR_FILENO); | |
55 | } | |
56 | close(fd); | |
4a539daf KS |
57 | execv(name, main_argv); |
58 | dbg("exec of child failed"); | |
59 | exit(1); | |
60 | case -1: | |
61 | dbg("fork of child failed"); | |
62 | break; | |
63 | return -1; | |
64 | default: | |
dd64e26b | 65 | wait(NULL); |
dd64e26b GKH |
66 | } |
67 | ||
4a539daf | 68 | return 0; |
dd64e26b GKH |
69 | } |
70 | ||
4a539daf KS |
71 | /* |
72 | * runs files in these directories in order: | |
73 | * <node name given by udev>/ | |
74 | * subsystem/ | |
75 | * default/ | |
dd64e26b | 76 | */ |
eb6c7cd0 | 77 | void dev_d_send(struct udevice *dev, const char *subsystem, const char *devpath) |
dd64e26b GKH |
78 | { |
79 | char dirname[256]; | |
a3e6c800 GKH |
80 | char env_devname[NAME_SIZE]; |
81 | char *devname; | |
82 | char *temp; | |
dd64e26b | 83 | |
bbbe503e KS |
84 | if (udev_dev_d == 0) |
85 | return; | |
86 | ||
470c0ef2 | 87 | memset(env_devname, 0x00, sizeof(env_devname)); |
bbbe503e | 88 | if (dev->type == 'b' || dev->type == 'c') { |
a3e6c800 GKH |
89 | strfieldcpy(env_devname, udev_root); |
90 | strfieldcat(env_devname, dev->name); | |
bbbe503e | 91 | } else if (dev->type == 'n') { |
a3e6c800 | 92 | strfieldcpy(env_devname, dev->name); |
9b28a52a | 93 | setenv("DEVPATH", devpath, 1); |
bbbe503e | 94 | } |
a3e6c800 GKH |
95 | setenv("DEVNAME", env_devname, 1); |
96 | dbg("DEVNAME='%s'", env_devname); | |
97 | ||
98 | devname = strdup(dev->name); | |
99 | if (!devname) { | |
100 | dbg("out of memory"); | |
101 | return; | |
102 | } | |
103 | ||
104 | /* Chop the device name up into pieces based on '/' */ | |
105 | temp = strchr(devname, '/'); | |
106 | while (temp != NULL) { | |
107 | *temp = 0x00; | |
108 | strcpy(dirname, DEVD_DIR); | |
109 | strfieldcat(dirname, devname); | |
110 | call_foreach_file(run_program, dirname, DEVD_SUFFIX); | |
111 | ||
112 | *temp = '/'; | |
113 | ++temp; | |
114 | temp = strchr(temp, '/'); | |
115 | } | |
dd64e26b | 116 | |
4a539daf KS |
117 | strcpy(dirname, DEVD_DIR); |
118 | strfieldcat(dirname, dev->name); | |
119 | call_foreach_file(run_program, dirname, DEVD_SUFFIX); | |
dd64e26b | 120 | |
4a539daf KS |
121 | strcpy(dirname, DEVD_DIR); |
122 | strfieldcat(dirname, subsystem); | |
123 | call_foreach_file(run_program, dirname, DEVD_SUFFIX); | |
dd64e26b | 124 | |
4a539daf KS |
125 | strcpy(dirname, DEVD_DIR "default"); |
126 | call_foreach_file(run_program, dirname, DEVD_SUFFIX); | |
dd64e26b | 127 | } |