chiark / gitweb /
[PATCH] change call_foreach_file to return a list
[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 static int run_program(struct udevice *udev, const char *filename)
36 {
37         pid_t pid;
38         int fd;
39
40         dbg("running %s", filename);
41
42         pid = fork();
43         switch (pid) {
44         case 0:
45                 /* child */
46                 fd = open("/dev/null", O_RDWR);
47                 if ( fd >= 0) {
48                         dup2(fd, STDOUT_FILENO);
49                         dup2(fd, STDIN_FILENO);
50                         dup2(fd, STDERR_FILENO);
51                 }
52                 close(fd);
53
54                 execl(filename, filename, udev->subsystem, NULL);
55                 dbg("exec of child failed");
56                 _exit(1);
57         case -1:
58                 dbg("fork of child failed");
59                 break;
60                 return -1;
61         default:
62                 waitpid(pid, NULL, 0);
63         }
64
65         return 0;
66 }
67
68 /* 
69  * runs files in these directories in order:
70  *      <node name given by udev>/
71  *      subsystem/
72  *      default/
73  */
74 void udev_multiplex_directory(struct udevice *udev, const char *basedir, const char *suffix)
75 {
76         char dirname[PATH_SIZE];
77         struct name_entry *name_loop, *name_tmp;
78         LIST_HEAD(name_list);
79
80         /* chop the device name up into pieces based on '/' */
81         if (udev->name[0] != '\0') {
82                 char devname[PATH_SIZE];
83                 char *temp;
84
85                 strlcpy(devname, udev->name, sizeof(devname));
86                 temp = strchr(devname, '/');
87                 while (temp != NULL) {
88                         temp[0] = '\0';
89
90                         /* don't call the subsystem directory here */
91                         if (strcmp(devname, udev->subsystem) != 0) {
92                                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, devname);
93                                 dirname[sizeof(dirname)-1] = '\0';
94                                 add_matching_files(&name_list, dirname, suffix);
95                         }
96
97                         temp[0] = '/';
98                         ++temp;
99                         temp = strchr(temp, '/');
100                 }
101         }
102
103         if (udev->name[0] != '\0') {
104                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->name);
105                 dirname[sizeof(dirname)-1] = '\0';
106                 add_matching_files(&name_list, dirname, suffix);
107         }
108
109         if (udev->subsystem[0] != '\0') {
110                 snprintf(dirname, sizeof(dirname), "%s/%s", basedir, udev->subsystem);
111                 dirname[sizeof(dirname)-1] = '\0';
112                 add_matching_files(&name_list, dirname, suffix);
113         }
114
115         snprintf(dirname, sizeof(dirname), "%s/default", basedir);
116         dirname[sizeof(dirname)-1] = '\0';
117         add_matching_files(&name_list, dirname, suffix);
118
119         list_for_each_entry_safe(name_loop, name_tmp, &name_list, node) {
120                 run_program(udev, name_loop->name);
121                 list_del(&name_loop->node);
122         }
123
124 }