chiark / gitweb /
[PATCH] replace tdb database by simple lockless file database
[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(char *name)
39 {
40         pid_t pid;
41         int fd;
42         char *argv[3];
43
44         dbg("running %s", name);
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                 argv[0] = name;
59                 argv[1] = main_argv[1];
60                 argv[2] = NULL;
61
62                 execv(name, argv);
63                 dbg("exec of child failed");
64                 _exit(1);
65         case -1:
66                 dbg("fork of child failed");
67                 break;
68                 return -1;
69         default:
70                 waitpid(pid, NULL, 0);
71         }
72
73         return 0;
74 }
75
76 /* 
77  * runs files in these directories in order:
78  *      <node name given by udev>/
79  *      subsystem/
80  *      default/
81  */
82 void dev_d_execute(struct udevice *udev)
83 {
84         char dirname[PATH_MAX];
85         char devname[NAME_SIZE];
86         char *temp;
87
88         /* skip if UDEV_NO_DEVD is set */
89         if (udev_dev_d == 0)
90                 return;
91
92         /* skip if udev did nothing, like unchanged netif or no "dev" file */
93         if (udev->devname[0] == '\0')
94                 return;
95
96         /* add the node name or the netif name to the environment */
97         setenv("DEVNAME", udev->devname, 1);
98         dbg("DEVNAME='%s'", udev->devname);
99
100         strfieldcpy(devname, udev->name);
101
102         /* Chop the device name up into pieces based on '/' */
103         temp = strchr(devname, '/');
104         while (temp != NULL) {
105                 temp[0] = '\0';
106                 strcpy(dirname, DEVD_DIR);
107                 strfieldcat(dirname, devname);
108                 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
109
110                 temp[0] = '/';
111                 ++temp;
112                 temp = strchr(temp, '/');
113         }
114
115         strcpy(dirname, DEVD_DIR);
116         strfieldcat(dirname, udev->name);
117         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
118
119         strcpy(dirname, DEVD_DIR);
120         strfieldcat(dirname, udev->subsystem);
121         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
122
123         strcpy(dirname, DEVD_DIR "default");
124         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
125 }