chiark / gitweb /
[PATCH] udev-test.pl: add test for DEVNAME export to RUN environment
[elogind.git] / udev_multiplex.c
1 /*
2  * udev_multiplex.c directory multiplexer
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.
9  */
10
11 /*
12  * This essentially emulates the following shell script logic in C:
13  *      DIR="/etc/dev.d"
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
17  *      done
18  *      exit 1;
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/types.h>
25 #include <sys/wait.h>
26 #include <sys/stat.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29
30 #include "udev.h"
31 #include "udev_libc_wrapper.h"
32 #include "udev_utils.h"
33 #include "logging.h"
34
35
36 /* 
37  * runs files in these directories in order:
38  *      <node name given by udev>/
39  *      subsystem/
40  *      default/
41  */
42 void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix)
43 {
44         char dirname[PATH_SIZE];
45         struct name_entry *name_loop, *name_tmp;
46         LIST_HEAD(name_list);
47
48         /* chop the device name up into pieces based on '/' */
49         if (udev->name[0] != '\0') {
50                 char devname[PATH_SIZE];
51                 char *temp;
52
53                 strlcpy(devname, udev->name, sizeof(devname));
54                 temp = strchr(devname, '/');
55                 while (temp != NULL) {
56                         temp[0] = '\0';
57
58                         /* don't call the subsystem directory here */
59                         if (strcmp(devname, udev->subsystem) != 0) {
60                                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, devname);
61                                 dirname[sizeof(dirname)-1] = '\0';
62                                 add_matching_files(&name_list, dirname, suffix);
63                         }
64
65                         temp[0] = '/';
66                         ++temp;
67                         temp = strchr(temp, '/');
68                 }
69         }
70
71         if (udev->name[0] != '\0') {
72                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->name);
73                 dirname[sizeof(dirname)-1] = '\0';
74                 add_matching_files(&name_list, dirname, suffix);
75         }
76
77         if (udev->subsystem[0] != '\0') {
78                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->subsystem);
79                 dirname[sizeof(dirname)-1] = '\0';
80                 add_matching_files(&name_list, dirname, suffix);
81         }
82
83         snprintf(dirname, sizeof(dirname), "%s/default", basedir);
84         dirname[sizeof(dirname)-1] = '\0';
85         add_matching_files(&name_list, dirname, suffix);
86
87         list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
88                 execute_command(name_loop->name, udev->subsystem);
89                 list_del(&name_loop->node);
90         }
91
92 }