1 From 6d5a094b85a1a148647744eb327593d6aef9a822 Mon Sep 17 00:00:00 2001
2 From: Platon Ryzhikov <ihummer63@yandex.ru>
3 Date: Mon, 18 May 2020 16:58:10 +0300
4 Subject: [PATCH] Scan all /sys to find uevents, they may contain info about
5 modules even if they don't describe devices
8 smdev.c | 127 ++++++++++++++++++++++++++++----------------------------
9 1 file changed, 64 insertions(+), 63 deletions(-)
11 diff --git a/smdev.c b/smdev.c
12 index b774908..ff35df2 100644
15 @@ -85,7 +85,7 @@ main(int argc, char *argv[])
19 - recurse("/sys/devices", populatedev);
20 + recurse("/sys", populatedev);
23 eprintf("Environment not set up correctly for hotplugging\n");
24 @@ -126,14 +126,14 @@ dohotplug(void)
25 action = getenv("ACTION");
26 devpath = getenv("DEVPATH");
27 devname = getenv("DEVNAME");
28 - if (!minor || !major || !action || !devpath || !devname)
29 + if (!action || !devpath)
32 - ev.minor = estrtol(minor, 10);
33 - ev.major = estrtol(major, 10);
34 + ev.minor = minor?estrtol(minor, 10):-1;
35 + ev.major = major?estrtol(major, 10):-1;
36 ev.action = mapaction(action);
38 - ev.devname = devname;
39 + ev.devname = devname?devname:".";
43 @@ -249,12 +249,14 @@ removedev(struct event *ev)
44 if (rule->path && rule->path[0] == '!')
47 - /* Delete device node */
49 - /* Delete symlink */
50 - if (rule->path && rule->path[0] == '>') {
51 - snprintf(buf, sizeof(buf), "/dev/%s", ev->devname);
53 + if (ev->major >= 0 && ev->minor >= 0 && ev->devname) {
54 + /* Delete device node */
56 + /* Delete symlink */
57 + if (rule->path && rule->path[0] == '>') {
58 + snprintf(buf, sizeof(buf), "/dev/%s", ev->devname);
64 @@ -276,58 +278,59 @@ createdev(struct event *ev)
65 if (rule->path && rule->path[0] == '!')
68 - snprintf(buf, sizeof(buf), "%d:%d", ev->major, ev->minor);
69 - if ((type = devtype(buf)) < 0)
72 - /* Parse path and create the directory tree */
73 - parsepath(rule, &rpath, ev->devname);
74 - if (!(dirc = strdup(rpath.path)))
76 - strlcpy(buf, dirname(dirc), sizeof(buf));
79 - if (mkpath(buf, 0755) < 0)
80 - eprintf("mkdir %s:", buf);
82 + if (ev->major >= 0 && ev->minor >= 0 && ev->devname) {
83 + snprintf(buf, sizeof(buf), "%d:%d", ev->major, ev->minor);
84 + if ((type = devtype(buf)) < 0)
87 - if (mknod(rpath.path, rule->mode | type,
88 - makedev(ev->major, ev->minor)) < 0 &&
90 - eprintf("mknod %s:", rpath.path);
93 - pw = getpwnam(rule->user);
96 - eprintf("getpwnam %s:", rule->user);
98 - eprintf("getpwnam %s: no such user\n",
101 + /* Parse path and create the directory tree */
102 + parsepath(rule, &rpath, ev->devname);
103 + if (!(dirc = strdup(rpath.path)))
104 + eprintf("strdup:");
105 + strlcpy(buf, dirname(dirc), sizeof(buf));
108 + if (mkpath(buf, 0755) < 0)
109 + eprintf("mkdir %s:", buf);
112 + if (mknod(rpath.path, rule->mode | type,
113 + makedev(ev->major, ev->minor)) < 0 &&
115 + eprintf("mknod %s:", rpath.path);
118 + pw = getpwnam(rule->user);
121 + eprintf("getpwnam %s:", rule->user);
123 + eprintf("getpwnam %s: no such user\n",
128 - gr = getgrnam(rule->group);
131 - eprintf("getgrnam %s:", rule->group);
133 - eprintf("getgrnam %s: no such group\n",
137 + gr = getgrnam(rule->group);
140 + eprintf("getgrnam %s:", rule->group);
142 + eprintf("getgrnam %s: no such group\n",
146 - if (chown(rpath.path, pw->pw_uid, gr->gr_gid) < 0)
147 - eprintf("chown %s:", rpath.path);
148 + if (chown(rpath.path, pw->pw_uid, gr->gr_gid) < 0)
149 + eprintf("chown %s:", rpath.path);
151 - if (chmod(rpath.path, rule->mode) < 0)
152 - eprintf("chmod %s:", rpath.path);
153 + if (chmod(rpath.path, rule->mode) < 0)
154 + eprintf("chmod %s:", rpath.path);
156 - if (rule->path && rule->path[0] == '>') {
157 - /* ev->devname is the original device name */
158 - snprintf(buf, sizeof(buf), "/dev/%s", ev->devname);
159 - symlink(rpath.path, buf);
160 + if (rule->path && rule->path[0] == '>') {
161 + /* ev->devname is the original device name */
162 + snprintf(buf, sizeof(buf), "/dev/%s", ev->devname);
163 + symlink(rpath.path, buf);
169 if (chdir("/dev") < 0)
170 @@ -389,12 +392,10 @@ populatedev(const char *path)
173 recurse(path, populatedev);
174 - if (strcmp(path, "dev") == 0) {