chiark / gitweb /
[PATCH] hm, somethings odd with DEVPATH, see if this fixes it...
[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                 udevdb_exit();  /* close udevdb */
51                 fd = open("/dev/null", O_RDWR);
52                 if ( fd >= 0) {
53                         dup2(fd, STDOUT_FILENO);
54                         dup2(fd, STDIN_FILENO);
55                         dup2(fd, STDERR_FILENO);
56                 }
57                 close(fd);
58
59                 argv[0] = name;
60                 argv[1] = main_argv[1];
61                 argv[2] = NULL;
62
63                 execv(name, argv);
64                 dbg("exec of child failed");
65                 exit(1);
66         case -1:
67                 dbg("fork of child failed");
68                 break;
69                 return -1;
70         default:
71                 wait(NULL);
72         }
73
74         return 0;
75 }
76
77 /* 
78  * runs files in these directories in order:
79  *      <node name given by udev>/
80  *      subsystem/
81  *      default/
82  */
83 void dev_d_send(struct udevice *dev, const char *subsystem, const char *devpath)
84 {
85         char dirname[256];
86         char env_devname[NAME_SIZE];
87         char *devname;
88         char *temp;
89
90         if (udev_dev_d == 0)
91                 return;
92
93         memset(env_devname, 0x00, sizeof(env_devname));
94         if (dev->type == 'b' || dev->type == 'c') {
95                 strfieldcpy(env_devname, udev_root);
96                 strfieldcat(env_devname, dev->name);
97         } else if (dev->type == 'n') {
98                 strfieldcpy(env_devname, dev->name);
99         }
100
101         setenv("DEVPATH", devpath, 1);
102         setenv("DEVNAME", env_devname, 1);
103         dbg("DEVNAME='%s'", env_devname);
104
105         devname = strdup(dev->name);
106         if (!devname) {
107                 dbg("out of memory");
108                 return;
109         }
110
111         /* Chop the device name up into pieces based on '/' */
112         temp = strchr(devname, '/');
113         while (temp != NULL) {
114                 *temp = 0x00;
115                 strcpy(dirname, DEVD_DIR);
116                 strfieldcat(dirname, devname);
117                 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
118
119                 *temp = '/';
120                 ++temp;
121                 temp = strchr(temp, '/');
122         }
123
124         strcpy(dirname, DEVD_DIR);
125         strfieldcat(dirname, dev->name);
126         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
127
128         strcpy(dirname, DEVD_DIR);
129         strfieldcat(dirname, subsystem);
130         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
131
132         strcpy(dirname, DEVD_DIR "default");
133         call_foreach_file(run_program, dirname, DEVD_SUFFIX);
134 }