2 * udev_multiplex.c directory multiplexer
4 * Copyright (C) 2004 Greg Kroah-Hartman <greg@kroah.com>
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.
12 * This essentially emulates the following shell script logic in C:
14 * export DEVNAME="whatever_dev_name_udev_just_gave"
15 * for I in "${DIR}/$DEVNAME/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do
16 * if [ -f $I ]; then $I $1 ; fi
24 #include <sys/types.h>
31 #include "udev_utils.h"
34 static int run_program(const char *filename, void *data)
38 struct udevice *udev = data;
40 dbg("running %s", filename);
46 fd = open("/dev/null", O_RDWR);
48 dup2(fd, STDOUT_FILENO);
49 dup2(fd, STDIN_FILENO);
50 dup2(fd, STDERR_FILENO);
54 execl(filename, filename, udev->subsystem, NULL);
55 dbg("exec of child failed");
58 dbg("fork of child failed");
62 waitpid(pid, NULL, 0);
69 * runs files in these directories in order:
70 * <node name given by udev>/
74 void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix)
76 char dirname[PATH_MAX];
78 /* chop the device name up into pieces based on '/' */
79 if (udev->name[0] != '\0') {
80 char devname[NAME_SIZE];
83 strfieldcpy(devname, udev->name);
84 temp = strchr(devname, '/');
85 while (temp != NULL) {
88 /* don't call the subsystem directory here */
89 if (strcmp(devname, udev->subsystem) != 0) {
90 snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname);
91 dirname[PATH_MAX-1] = '\0';
92 call_foreach_file(run_program, dirname, suffix, udev);
97 temp = strchr(temp, '/');
101 if (udev->name[0] != '\0') {
102 snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name);
103 dirname[PATH_MAX-1] = '\0';
104 call_foreach_file(run_program, dirname, suffix, udev);
107 if (udev->subsystem[0] != '\0') {
108 snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem);
109 dirname[PATH_MAX-1] = '\0';
110 call_foreach_file(run_program, dirname, suffix, udev);
113 snprintf(dirname, PATH_MAX, "%s/default", basedir);
114 dirname[PATH_MAX-1] = '\0';
115 call_foreach_file(run_program, dirname, suffix, udev);