mdev: set $MDEV correctly for renamed nodes
[oweals/busybox.git] / util-linux / mdev.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * mdev - Mini udev for busybox
4  *
5  * Copyright 2005 Rob Landley <rob@landley.net>
6  * Copyright 2005 Frank Sorenson <frank@tuxrocks.com>
7  *
8  * Licensed under GPL version 2, see file LICENSE in this tarball for details.
9  */
10 #include "libbb.h"
11 #include "xregex.h"
12
13 /* "mdev -s" scans /sys/class/xxx, looking for directories which have dev
14  * file (it is of the form "M:m\n"). Example: /sys/class/tty/tty0/dev
15  * contains "4:0\n". Directory name is taken as device name, path component
16  * directly after /sys/class/ as subsystem. In this example, "tty0" and "tty".
17  * Then mdev creates the /dev/device_name node.
18  * If /sys/class/.../dev file does not exist, mdev still may act
19  * on this device: see "@|$|*command args..." parameter in config file.
20  *
21  * mdev w/o parameters is called as hotplug helper. It takes device
22  * and subsystem names from $DEVPATH and $SUBSYSTEM, extracts
23  * maj,min from "/sys/$DEVPATH/dev" and also examines
24  * $ACTION ("add"/"delete") and $FIRMWARE.
25  *
26  * If action is "add", mdev creates /dev/device_name similarly to mdev -s.
27  * (todo: explain "delete" and $FIRMWARE)
28  *
29  * If /etc/mdev.conf exists, it may modify /dev/device_name's properties.
30  * /etc/mdev.conf file format:
31  *
32  * [-][subsystem/]device  user:grp  mode  [>|=path] [@|$|*command args...]
33  * [-]@maj,min[-min2]     user:grp  mode  [>|=path] [@|$|*command args...]
34  * [-]$envvar=val         user:grp  mode  [>|=path] [@|$|*command args...]
35  *
36  * Leading minus in 1st field means "don't stop on this line", otherwise
37  * search is stopped after the matching line is encountered.
38  *
39  * The device name or "subsystem/device" combo is matched against 1st field
40  * (which is a regex), or maj,min is matched against 1st field,
41  * or specified environment variable (as regex) is matched against 1st field.
42  *
43  * $envvar=val format is useful for loading modules for hot-plugged devices
44  * which do not have driver loaded yet. In this case /sys/class/.../dev
45  * does not exist, but $MODALIAS is set to needed module's name
46  * (actually, an alias to it) by kernel. This rule instructs mdev
47  * to load the module and exit:
48  *    $MODALIAS=.* 0:0 660 @modprobe "$MODALIAS"
49  * The kernel will generate another hotplug event when /sys/class/.../dev
50  * file appears.
51  *
52  * When line matches, the device node is created, chmod'ed and chown'ed,
53  * moved to path, and if >path, a symlink to moved node is created,
54  * all this if /sys/class/.../dev exists.
55  *    Examples:
56  *    =loop/      - moves to /dev/loop
57  *    >disk/sda%1 - moves to /dev/disk/sdaN, makes /dev/sdaN a symlink
58  *
59  * Then "command args..." is executed (via sh -c 'command args...').
60  * @:execute on creation, $:on deletion, *:on both.
61  * This happens regardless of /sys/class/.../dev existence.
62  */
63
64 struct globals {
65         int root_major, root_minor;
66         char *subsystem;
67 };
68 #define G (*(struct globals*)&bb_common_bufsiz1)
69 #define root_major (G.root_major)
70 #define root_minor (G.root_minor)
71 #define subsystem  (G.subsystem )
72
73 /* Prevent infinite loops in /sys symlinks */
74 #define MAX_SYSFS_DEPTH 3
75
76 /* We use additional 64+ bytes in make_device() */
77 #define SCRATCH_SIZE 80
78
79 /* Builds an alias path.
80  * This function potentionally reallocates the alias parameter.
81  * Only used for ENABLE_FEATURE_MDEV_RENAME
82  */
83 static char *build_alias(char *alias, const char *device_name)
84 {
85         char *dest;
86
87         /* ">bar/": rename to bar/device_name */
88         /* ">bar[/]baz": rename to bar[/]baz */
89         dest = strrchr(alias, '/');
90         if (dest) { /* ">bar/[baz]" ? */
91                 *dest = '\0'; /* mkdir bar */
92                 bb_make_directory(alias, 0755, FILEUTILS_RECUR);
93                 *dest = '/';
94                 if (dest[1] == '\0') { /* ">bar/" => ">bar/device_name" */
95                         dest = alias;
96                         alias = concat_path_file(alias, device_name);
97                         free(dest);
98                 }
99         }
100
101         return alias;
102 }
103
104 /* mknod in /dev based on a path like "/sys/block/hda/hda1"
105  * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
106  * after NUL, but we promise to not mangle (IOW: to restore if needed)
107  * path string.
108  * NB2: "mdev -s" may call us many times, do not leak memory/fds!
109  */
110 static void make_device(char *path, int delete)
111 {
112         char *device_name;
113         int major, minor, type, len;
114         int mode;
115         parser_t *parser;
116
117         /* Try to read major/minor string.  Note that the kernel puts \n after
118          * the data, so we don't need to worry about null terminating the string
119          * because sscanf() will stop at the first nondigit, which \n is.
120          * We also depend on path having writeable space after it.
121          */
122         major = -1;
123         if (!delete) {
124                 char *dev_maj_min = path + strlen(path);
125
126                 strcpy(dev_maj_min, "/dev");
127                 len = open_read_close(path, dev_maj_min + 1, 64);
128                 *dev_maj_min = '\0';
129                 if (len < 1) {
130                         if (!ENABLE_FEATURE_MDEV_EXEC)
131                                 return;
132                         /* no "dev" file, but we can still run scripts
133                          * based on device name */
134                 } else if (sscanf(++dev_maj_min, "%u:%u", &major, &minor) != 2) {
135                         major = -1;
136                 }
137         }
138
139         /* Determine device name, type, major and minor */
140         device_name = (char*) bb_basename(path);
141         /* http://kernel.org/doc/pending/hotplug.txt says that only
142          * "/sys/block/..." is for block devices. "/sys/bus" etc is not.
143          * But since 2.6.25 block devices are also in /sys/class/block,
144          * we use strstr("/block/") to forestall future surprises. */
145         type = S_IFCHR;
146         if (strstr(path, "/block/"))
147                 type = S_IFBLK;
148
149         /* Make path point to "subsystem/device_name" */
150         if (path[5] == 'b') /* legacy /sys/block? */
151                 path += sizeof("/sys/") - 1;
152         else
153                 path += sizeof("/sys/class/") - 1;
154
155         /* If we have config file, look up user settings */
156         if (ENABLE_FEATURE_MDEV_CONF)
157                 parser = config_open2("/etc/mdev.conf", fopen_for_read);
158
159         do {
160                 int keep_matching;
161                 struct bb_uidgid_t ugid;
162                 char *tokens[4];
163                 char *command = NULL;
164                 char *alias = NULL;
165                 char aliaslink = aliaslink; /* for compiler */
166
167                 /* Defaults in case we won't match any line */
168                 ugid.uid = ugid.gid = 0;
169                 keep_matching = 0;
170                 mode = 0660;
171
172                 if (ENABLE_FEATURE_MDEV_CONF
173                  && config_read(parser, tokens, 4, 3, "# \t", PARSE_NORMAL)
174                 ) {
175                         char *val;
176                         char *str_to_match;
177                         regmatch_t off[1 + 9 * ENABLE_FEATURE_MDEV_RENAME_REGEXP];
178
179                         val = tokens[0];
180                         keep_matching = ('-' == val[0]);
181                         val += keep_matching; /* swallow leading dash */
182
183                         /* Match against either "subsystem/device_name"
184                          * or "device_name" alone */
185                         str_to_match = strchr(val, '/') ? path : device_name;
186
187                         /* Fields: regex uid:gid mode [alias] [cmd] */
188
189                         if (val[0] == '@') {
190                                 /* @major,minor[-minor2] */
191                                 /* (useful when name is ambiguous:
192                                  * "/sys/class/usb/lp0" and
193                                  * "/sys/class/printer/lp0") */
194                                 int cmaj, cmin0, cmin1, sc;
195                                 if (major < 0)
196                                         continue; /* no dev, no match */
197                                 sc = sscanf(val, "@%u,%u-%u", &cmaj, &cmin0, &cmin1);
198                                 if (sc < 1 || major != cmaj
199                                  || (sc == 2 && minor != cmin0)
200                                  || (sc == 3 && (minor < cmin0 || minor > cmin1))
201                                 ) {
202                                         continue; /* this line doesn't match */
203                                 }
204                                 goto line_matches;
205                         }
206                         if (val[0] == '$') {
207                                 /* regex to match an environment variable */
208                                 char *eq = strchr(++val, '=');
209                                 if (!eq)
210                                         continue;
211                                 *eq = '\0';
212                                 str_to_match = getenv(val);
213                                 if (!str_to_match)
214                                         continue;
215                                 str_to_match -= strlen(val) + 1;
216                                 *eq = '=';
217                         }
218                         /* else: regex to match [subsystem/]device_name */
219
220                         {
221                                 regex_t match;
222                                 int result;
223
224                                 xregcomp(&match, val, REG_EXTENDED);
225                                 result = regexec(&match, str_to_match, ARRAY_SIZE(off), off, 0);
226                                 regfree(&match);
227                                 //bb_error_msg("matches:");
228                                 //for (int i = 0; i < ARRAY_SIZE(off); i++) {
229                                 //      if (off[i].rm_so < 0) continue;
230                                 //      bb_error_msg("match %d: '%.*s'\n", i,
231                                 //              (int)(off[i].rm_eo - off[i].rm_so),
232                                 //              device_name + off[i].rm_so);
233                                 //}
234
235                                 /* If no match, skip rest of line */
236                                 /* (regexec returns whole pattern as "range" 0) */
237                                 if (result || off[0].rm_so
238                                  || ((int)off[0].rm_eo != (int)strlen(str_to_match))
239                                 ) {
240                                         continue; /* this line doesn't match */
241                                 }
242                         }
243  line_matches:
244                         /* This line matches. Stop parsing after parsing
245                          * the rest the line unless keep_matching == 1 */
246
247                         /* 2nd field: uid:gid - device ownership */
248                         if (get_uidgid(&ugid, tokens[1], 1) == 0)
249                                 bb_error_msg("unknown user/group %s", tokens[1]);
250
251                         /* 3rd field: mode - device permissions */
252                         mode = strtoul(tokens[2], NULL, 8);
253
254                         val = tokens[3];
255                         /* 4th field (opt): >|=alias */
256
257                         if (ENABLE_FEATURE_MDEV_RENAME && val) {
258                                 aliaslink = val[0];
259                                 if (aliaslink == '>' || aliaslink == '=') {
260                                         char *a, *s, *st;
261                                         char *p;
262                                         unsigned i, n;
263
264                                         a = val;
265                                         s = strchrnul(val, ' ');
266                                         st = strchrnul(val, '\t');
267                                         if (st < s)
268                                                 s = st;
269                                         val = (s[0] && s[1]) ? s+1 : NULL;
270                                         s[0] = '\0';
271
272                                         if (ENABLE_FEATURE_MDEV_RENAME_REGEXP) {
273                                                 /* substitute %1..9 with off[1..9], if any */
274                                                 n = 0;
275                                                 s = a;
276                                                 while (*s)
277                                                         if (*s++ == '%')
278                                                                 n++;
279
280                                                 p = alias = xzalloc(strlen(a) + n * strlen(str_to_match));
281                                                 s = a + 1;
282                                                 while (*s) {
283                                                         *p = *s;
284                                                         if ('%' == *s) {
285                                                                 i = (s[1] - '0');
286                                                                 if (i <= 9 && off[i].rm_so >= 0) {
287                                                                         n = off[i].rm_eo - off[i].rm_so;
288                                                                         strncpy(p, str_to_match + off[i].rm_so, n);
289                                                                         p += n - 1;
290                                                                         s++;
291                                                                 }
292                                                         }
293                                                         p++;
294                                                         s++;
295                                                 }
296                                         } else {
297                                                 alias = xstrdup(a + 1);
298                                         }
299                                 }
300                         }
301
302                         if (ENABLE_FEATURE_MDEV_EXEC && val) {
303                                 const char *s = "$@*";
304                                 const char *s2 = strchr(s, val[0]);
305
306                                 if (!s2)
307                                         bb_error_msg_and_die("bad line %u", parser->lineno);
308
309                                 /* Are we running this command now?
310                                  * Run $cmd on delete, @cmd on create, *cmd on both
311                                  */
312                                 if (s2-s != delete)
313                                         command = xstrdup(val + 1);
314                         }
315                 }
316
317                 /* End of field parsing */
318
319                 /* "Execute" the line we found */
320                 {
321                         const char *node_name;
322
323                         node_name = device_name;
324                         if (ENABLE_FEATURE_MDEV_RENAME && alias)
325                                 node_name = alias = build_alias(alias, device_name);
326
327                         if (!delete && major >= 0) {
328                                 if (mknod(node_name, mode | type, makedev(major, minor)) && errno != EEXIST)
329                                         bb_perror_msg_and_die("mknod %s", node_name);
330                                 if (major == root_major && minor == root_minor)
331                                         symlink(node_name, "root");
332                                 if (ENABLE_FEATURE_MDEV_CONF) {
333                                         chmod(node_name, mode);
334                                         chown(node_name, ugid.uid, ugid.gid);
335                                 }
336                                 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
337                                         if (aliaslink == '>')
338                                                 symlink(node_name, device_name);
339                                 }
340                         }
341
342                         if (ENABLE_FEATURE_MDEV_EXEC && command) {
343                                 /* setenv will leak memory, use putenv/unsetenv/free */
344                                 char *s = xasprintf("%s=%s", "MDEV", node_name);
345                                 char *s1 = xasprintf("%s=%s", "SUBSYSTEM", subsystem);
346                                 putenv(s);
347                                 putenv(s1);
348                                 if (system(command) == -1)
349                                         bb_perror_msg_and_die("can't run '%s'", command);
350                                 unsetenv("SUBSYSTEM");
351                                 free(s1);
352                                 unsetenv("MDEV");
353                                 free(s);
354                                 free(command);
355                         }
356
357                         if (delete) {
358                                 if (ENABLE_FEATURE_MDEV_RENAME && alias) {
359                                         if (aliaslink == '>')
360                                                 unlink(device_name);
361                                 }
362                                 unlink(node_name);
363                         }
364
365                         if (ENABLE_FEATURE_MDEV_RENAME)
366                                 free(alias);
367                 }
368
369                 /* We found matching line.
370                  * Stop unless it was prefixed with '-' */
371                 if (ENABLE_FEATURE_MDEV_CONF && !keep_matching)
372                         break;
373
374         /* end of "while line is read from /etc/mdev.conf" */
375         } while (ENABLE_FEATURE_MDEV_CONF);
376
377         if (ENABLE_FEATURE_MDEV_CONF)
378                 config_close(parser);
379 }
380
381 /* File callback for /sys/ traversal */
382 static int FAST_FUNC fileAction(const char *fileName,
383                 struct stat *statbuf UNUSED_PARAM,
384                 void *userData,
385                 int depth UNUSED_PARAM)
386 {
387         size_t len = strlen(fileName) - 4; /* can't underflow */
388         char *scratch = userData;
389
390         /* len check is for paranoid reasons */
391         if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
392                 return FALSE;
393
394         strcpy(scratch, fileName);
395         scratch[len] = '\0';
396         make_device(scratch, 0);
397
398         return TRUE;
399 }
400
401 /* Directory callback for /sys/ traversal */
402 static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
403                 struct stat *statbuf UNUSED_PARAM,
404                 void *userData UNUSED_PARAM,
405                 int depth)
406 {
407         /* Extract device subsystem -- the name of the directory
408          * under /sys/class/ */
409         if (1 == depth) {
410                 free(subsystem);
411                 subsystem = strrchr(fileName, '/');
412                 if (subsystem)
413                         subsystem = xstrdup(subsystem + 1);
414         }
415
416         return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
417 }
418
419 /* For the full gory details, see linux/Documentation/firmware_class/README
420  *
421  * Firmware loading works like this:
422  * - kernel sets FIRMWARE env var
423  * - userspace checks /lib/firmware/$FIRMWARE
424  * - userspace waits for /sys/$DEVPATH/loading to appear
425  * - userspace writes "1" to /sys/$DEVPATH/loading
426  * - userspace copies /lib/firmware/$FIRMWARE into /sys/$DEVPATH/data
427  * - userspace writes "0" (worked) or "-1" (failed) to /sys/$DEVPATH/loading
428  * - kernel loads firmware into device
429  */
430 static void load_firmware(const char *firmware, const char *sysfs_path)
431 {
432         int cnt;
433         int firmware_fd, loading_fd, data_fd;
434
435         /* check for /lib/firmware/$FIRMWARE */
436         xchdir("/lib/firmware");
437         firmware_fd = xopen(firmware, O_RDONLY);
438
439         /* in case we goto out ... */
440         data_fd = -1;
441
442         /* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
443         xchdir(sysfs_path);
444         for (cnt = 0; cnt < 30; ++cnt) {
445                 loading_fd = open("loading", O_WRONLY);
446                 if (loading_fd != -1)
447                         goto loading;
448                 sleep(1);
449         }
450         goto out;
451
452  loading:
453         /* tell kernel we're loading by "echo 1 > /sys/$DEVPATH/loading" */
454         if (full_write(loading_fd, "1", 1) != 1)
455                 goto out;
456
457         /* load firmware into /sys/$DEVPATH/data */
458         data_fd = open("data", O_WRONLY);
459         if (data_fd == -1)
460                 goto out;
461         cnt = bb_copyfd_eof(firmware_fd, data_fd);
462
463         /* tell kernel result by "echo [0|-1] > /sys/$DEVPATH/loading" */
464         if (cnt > 0)
465                 full_write(loading_fd, "0", 1);
466         else
467                 full_write(loading_fd, "-1", 2);
468
469  out:
470         if (ENABLE_FEATURE_CLEAN_UP) {
471                 close(firmware_fd);
472                 close(loading_fd);
473                 close(data_fd);
474         }
475 }
476
477 int mdev_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
478 int mdev_main(int argc UNUSED_PARAM, char **argv)
479 {
480         RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
481
482         /* We can be called as hotplug helper */
483         /* Kernel cannot provide suitable stdio fds for us, do it ourself */
484         bb_sanitize_stdio();
485
486         /* Force the configuration file settings exactly */
487         umask(0);
488
489         xchdir("/dev");
490
491         if (argv[1] && strcmp(argv[1], "-s") == 0) {
492                 /* Scan:
493                  * mdev -s
494                  */
495                 struct stat st;
496
497                 xstat("/", &st);
498                 root_major = major(st.st_dev);
499                 root_minor = minor(st.st_dev);
500
501                 /* ACTION_FOLLOWLINKS is needed since in newer kernels
502                  * /sys/block/loop* (for example) are symlinks to dirs,
503                  * not real directories.
504                  * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
505                  * but we can't enforce that on users)
506                  */
507                 if (access("/sys/class/block", F_OK) != 0) {
508                         /* Scan obsolete /sys/block only if /sys/class/block
509                          * doesn't exist. Otherwise we'll have dupes.
510                          * Also, do not complain if it doesn't exist.
511                          * Some people configure kernel to have no blockdevs.
512                          */
513                         recursive_action("/sys/block",
514                                 ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
515                                 fileAction, dirAction, temp, 0);
516                 }
517                 recursive_action("/sys/class",
518                         ACTION_RECURSE | ACTION_FOLLOWLINKS,
519                         fileAction, dirAction, temp, 0);
520         } else {
521                 char *fw;
522                 char *seq;
523                 char *action;
524                 char *env_path;
525
526                 /* Hotplug:
527                  * env ACTION=... DEVPATH=... SUBSYSTEM=... [SEQNUM=...] mdev
528                  * ACTION can be "add" or "remove"
529                  * DEVPATH is like "/block/sda" or "/class/input/mice"
530                  */
531                 action = getenv("ACTION");
532                 env_path = getenv("DEVPATH");
533                 subsystem = getenv("SUBSYSTEM");
534                 if (!action || !env_path /*|| !subsystem*/)
535                         bb_show_usage();
536                 fw = getenv("FIRMWARE");
537
538                 /* If it exists, does /dev/mdev.seq match $SEQNUM?
539                  * If it does not match, earlier mdev is running
540                  * in parallel, and we need to wait */
541                 seq = getenv("SEQNUM");
542                 if (seq) {
543                         int timeout = 2000 / 32; /* 2000 msec */
544                         do {
545                                 int seqlen;
546                                 char seqbuf[sizeof(int)*3 + 2];
547
548                                 seqlen = open_read_close("mdev.seq", seqbuf, sizeof(seqbuf-1));
549                                 if (seqlen < 0) {
550                                         seq = NULL;
551                                         break;
552                                 }
553                                 seqbuf[seqlen] = '\0';
554                                 if (seqbuf[0] == '\n' /* seed file? */
555                                  || strcmp(seq, seqbuf) == 0 /* correct idx? */
556                                 ) {
557                                         break;
558                                 }
559                                 usleep(32*1000);
560                         } while (--timeout);
561                 }
562
563                 snprintf(temp, PATH_MAX, "/sys%s", env_path);
564                 if (strcmp(action, "remove") == 0) {
565                         /* Ignoring "remove firmware". It was reported
566                          * to happen and to cause erroneous deletion
567                          * of device nodes. */
568                         if (!fw)
569                                 make_device(temp, 1);
570                 }
571                 else if (strcmp(action, "add") == 0) {
572                         make_device(temp, 0);
573                         if (ENABLE_FEATURE_MDEV_LOAD_FIRMWARE) {
574                                 if (fw)
575                                         load_firmware(fw, temp);
576                         }
577                 }
578
579                 if (seq) {
580                         xopen_xwrite_close("mdev.seq", utoa(xatou(seq) + 1));
581                 }
582         }
583
584         if (ENABLE_FEATURE_CLEAN_UP)
585                 RELEASE_CONFIG_BUFFER(temp);
586
587         return EXIT_SUCCESS;
588 }