chiark / gitweb /
[PATCH] make the udev object available to more processing stages
[elogind.git] / dev_d.c
1 /*
2  * dev_d.c - dev.d/ 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_lib.h"
32 #include "udevdb.h"
33 #include "logging.h"
34
35 #define DEVD_DIR                        "/etc/dev.d/"
36 #define DEVD_SUFFIX                     ".dev"
37
38 static int run_program(const char *filename, void *data)
39 {
40         pid_t pid;
41         int fd;
42         struct udevice *udev = data;
43
44         dbg("running %s", filename);
45
46         pid = fork();
47         switch (pid) {
48         case 0:
49                 /* child */
50                 fd = open("/dev/null", O_RDWR);
51                 if ( fd >= 0) {
52                         dup2(fd, STDOUT_FILENO);
53                         dup2(fd, STDIN_FILENO);
54                         dup2(fd, STDERR_FILENO);
55                 }
56                 close(fd);
57
58                 execl(filename, filename, udev->subsystem, NULL);
59                 dbg("exec of child failed");
60                 _exit(1);
61         case -1:
62                 dbg("fork of child failed");
63                 break;
64                 return -1;
65         default:
66                 waitpid(pid, NULL, 0);
67         }
68
69         return 0;
70 }
71
72 /* 
73  * runs files in these directories in order:
74  *      <node name given by udev>/
75  *      subsystem/
76  *      default/
77  */
78 void dev_d_execute(struct udevice *udev)
79 {
80         char dirname[PATH_MAX];
81         char devname[NAME_SIZE];
82         char *temp;
83
84         /* skip if UDEV_NO_DEVD is set */
85         if (udev_dev_d == 0)
86                 return;
87
88         /* skip if udev did nothing, like unchanged netif or no "dev" file */
89         if (udev->devname[0] == '\0')
90                 return;
91
92         /* add the node name or the netif name to the environment */
93         setenv("DEVNAME", udev->devname, 1);
94         dbg("DEVNAME='%s'", udev->devname);
95
96         strfieldcpy(devname, udev->name);
97
98         /* Chop the device name up into pieces based on '/' */
99         temp = strchr(devname, '/');
100         while (temp != NULL) {
101                 temp[0] = '\0';
102                 strcpy(dirname, DEVD_DIR);
103                 strfieldcat(dirname, devname);
104                 call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
105
106                 temp[0] = '/';
107                 ++temp;
108                 temp = strchr(temp, '/');
109         }
110
111         strcpy(dirname, DEVD_DIR);
112         strfieldcat(dirname, udev->name);
113         call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
114
115         strcpy(dirname, DEVD_DIR);
116         strfieldcat(dirname, udev->subsystem);
117         call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
118
119         strcpy(dirname, DEVD_DIR "default");
120         call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev);
121 }