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