chiark / gitweb /
[PATCH] remove dbus code from core udev code as it's no longer needed to be there.
[elogind.git] / dev_d.c
1 /*
2  * dev.d multipleer
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  * Based on the klibc version of hotplug written by:
11  *      Author(s) Christian Borntraeger <cborntra@de.ibm.com>
12  * which was based on the shell script written by:
13  *      Greg Kroah-Hartman <greg@kroah.com>
14  *
15  */
16
17 /* 
18  * This essentially emulates the following shell script logic in C:
19         DIR="/etc/dev.d"
20         export DEVNODE="whatever_dev_name_udev_just_gave"
21         for I in "${DIR}/$DEVNODE/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do
22                 if [ -f $I ]; then $I $1 ; fi
23         done
24         exit 1;
25  */
26
27 #include <dirent.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <unistd.h>
34 #include "udev.h"
35 #include "udev_lib.h"
36 #include "logging.h"
37
38 #define HOTPLUGDIR      "/etc/dev.d"
39 #define COMMENT_PREFIX  '#'
40
41 static void run_program(char *name)
42 {
43         pid_t pid;
44
45         dbg("running %s", name);
46
47         pid = fork();
48
49         if (pid < 0) {
50                 perror("fork");
51                 return;
52         } 
53         
54         if (pid > 0) {
55                 wait(NULL);
56                 return;
57         }
58
59         execv(name, main_argv);
60         exit(1);
61 }
62
63 static void execute_dir (char *dirname)
64 {
65         DIR *directory;
66         struct dirent *entry;
67         char filename[256];
68
69         dbg("opening %s", dirname);
70         directory = opendir(dirname);
71         if (!directory)
72                 return;
73
74         while ((entry = readdir(directory))) {
75                 if (entry->d_name[0] == '\0')
76                         break;
77                 /* Don't run the files '.', '..', or hidden files, 
78                  * or files that start with a '#' */
79                 if ((entry->d_name[0] == '.') ||
80                     (entry->d_name[0] == COMMENT_PREFIX))
81                         continue;
82
83                 /* FIXME - need to use file_list_insert() here to run these in sorted order... */
84                 snprintf(filename, sizeof(filename), "%s%s", dirname, entry->d_name);
85                 filename[sizeof(filename)-1] = '\0';
86                 run_program(filename);
87         }
88
89         closedir(directory);
90 }
91
92 /* runs files in these directories in order:
93  *      name given by udev
94  *      subsystem
95  *      default
96  */
97 void dev_d_send(struct udevice *dev, char *subsystem)
98 {
99         char dirname[256];
100         char devnode[NAME_SIZE];
101
102         strfieldcpy(devnode, udev_root);
103         strfieldcat(devnode, dev->name);
104         setenv("DEVNODE", devnode, 1);
105
106         snprintf(dirname, sizeof(dirname), HOTPLUGDIR "/%s/", dev->name);
107         dirname[sizeof(dirname)-1] = '\0';
108         execute_dir(dirname);
109
110         snprintf(dirname, sizeof(dirname), HOTPLUGDIR "/%s/", subsystem);
111         dirname[sizeof(dirname)-1] = '\0';
112         execute_dir(dirname);
113
114         strcpy(dirname, HOTPLUGDIR "/default/");
115         execute_dir(dirname);
116 }
117