chiark / gitweb /
b3d05d9220d7f5fb1f6c18dd1a8bcbea8ede6f0b
[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 static int run_program(const char *filename, void *data)
36 {
37         pid_t pid;
38         int fd;
39         struct udevice *udev = data;
40
41         dbg("running %s", filename);
42
43         pid = fork();
44         switch (pid) {
45         case 0:
46                 /* child */
47                 fd = open("/dev/null", O_RDWR);
48                 if ( fd >= 0) {
49                         dup2(fd, STDOUT_FILENO);
50                         dup2(fd, STDIN_FILENO);
51                         dup2(fd, STDERR_FILENO);
52                 }
53                 close(fd);
54
55                 execl(filename, filename, udev->subsystem, NULL);
56                 dbg("exec of child failed");
57                 _exit(1);
58         case -1:
59                 dbg("fork of child failed");
60                 break;
61                 return -1;
62         default:
63                 waitpid(pid, NULL, 0);
64         }
65
66         return 0;
67 }
68
69 /* 
70  * runs files in these directories in order:
71  *      <node name given by udev>/
72  *      subsystem/
73  *      default/
74  */
75 void dev_d_execute(struct udevice *udev, const char *basedir, const char *suffix)
76 {
77         char dirname[PATH_MAX];
78         char devname[NAME_SIZE];
79         char *temp;
80
81         /* skip if UDEV_NO_DEVD is set */
82         if (udev_dev_d == 0)
83                 return;
84
85         strfieldcpy(devname, udev->name);
86
87         /* chop the device name up into pieces based on '/' */
88         temp = strchr(devname, '/');
89         while (temp != NULL) {
90                 temp[0] = '\0';
91                 snprintf(dirname, PATH_MAX, "%s/%s", basedir, devname);
92                 dirname[PATH_MAX-1] = '\0';
93                 call_foreach_file(run_program, dirname, suffix, udev);
94
95                 temp[0] = '/';
96                 ++temp;
97                 temp = strchr(temp, '/');
98         }
99
100         snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->name);
101         dirname[PATH_MAX-1] = '\0';
102         call_foreach_file(run_program, dirname, suffix, udev);
103
104         snprintf(dirname, PATH_MAX, "%s/%s", basedir, udev->subsystem);
105         dirname[PATH_MAX-1] = '\0';
106         call_foreach_file(run_program, dirname, suffix, udev);
107
108         snprintf(dirname, PATH_MAX, "%s/default", basedir);
109         dirname[PATH_MAX-1] = '\0';
110         call_foreach_file(run_program, dirname, suffix, udev);
111 }