]> git.ipfire.org Git - thirdparty/systemd.git/blame - dev_d.c
[PATCH] add cdsymlinks.sh support to gentoo rules file.
[thirdparty/systemd.git] / dev_d.c
CommitLineData
dd64e26b 1/*
4a539daf 2 * dev_d.c - dev.d/ multiplexer
dd64e26b
GKH
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.
dd64e26b
GKH
9 */
10
4a539daf 11/*
dd64e26b 12 * This essentially emulates the following shell script logic in C:
4a539daf 13 * DIR="/etc/dev.d"
4b06c852 14 * export DEVNAME="whatever_dev_name_udev_just_gave"
15 * for I in "${DIR}/$DEVNAME/"*.dev "${DIR}/$1/"*.dev "${DIR}/default/"*.dev ; do
4a539daf
KS
16 * if [ -f $I ]; then $I $1 ; fi
17 * done
18 * exit 1;
dd64e26b
GKH
19 */
20
dd64e26b
GKH
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <sys/types.h>
25#include <sys/wait.h>
6e3e3c34 26#include <sys/stat.h>
dd64e26b 27#include <unistd.h>
6e3e3c34
HH
28#include <fcntl.h>
29
dd64e26b
GKH
30#include "udev.h"
31#include "udev_lib.h"
6e3e3c34 32#include "udevdb.h"
dd64e26b
GKH
33#include "logging.h"
34
4a539daf
KS
35#define DEVD_DIR "/etc/dev.d/"
36#define DEVD_SUFFIX ".dev"
dd64e26b 37
4a539daf 38static int run_program(char *name)
dd64e26b
GKH
39{
40 pid_t pid;
6e3e3c34 41 int fd;
1059b07a 42 char *argv[3];
dd64e26b
GKH
43
44 dbg("running %s", name);
45
46 pid = fork();
4a539daf
KS
47 switch (pid) {
48 case 0:
49 /* child */
6e3e3c34
HH
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);
1059b07a
KS
58
59 argv[0] = name;
60 argv[1] = main_argv[1];
61 argv[2] = NULL;
62
63 execv(name, argv);
4a539daf 64 dbg("exec of child failed");
5d24c6ca 65 _exit(1);
4a539daf
KS
66 case -1:
67 dbg("fork of child failed");
68 break;
69 return -1;
70 default:
e920fed3 71 waitpid(pid, NULL, 0);
dd64e26b
GKH
72 }
73
4a539daf 74 return 0;
dd64e26b
GKH
75}
76
4a539daf
KS
77/*
78 * runs files in these directories in order:
79 * <node name given by udev>/
80 * subsystem/
81 * default/
dd64e26b 82 */
5d24c6ca 83void dev_d_execute(struct udevice *udev)
dd64e26b 84{
5d24c6ca
KS
85 char dirname[PATH_MAX];
86 char devname[NAME_SIZE];
a3e6c800 87 char *temp;
dd64e26b 88
5d24c6ca 89 /* skip if UDEV_NO_DEVD is set */
bbbe503e
KS
90 if (udev_dev_d == 0)
91 return;
92
5d24c6ca
KS
93 /* skip if udev did nothing, like unchanged netif or no "dev" file */
94 if (udev->devname[0] == '\0')
a3e6c800 95 return;
5d24c6ca
KS
96
97 /* add the node name or the netif name to the environment */
98 setenv("DEVNAME", udev->devname, 1);
99 dbg("DEVNAME='%s'", udev->devname);
100
101 strfieldcpy(devname, udev->name);
a3e6c800
GKH
102
103 /* Chop the device name up into pieces based on '/' */
104 temp = strchr(devname, '/');
105 while (temp != NULL) {
5d24c6ca 106 temp[0] = '\0';
a3e6c800
GKH
107 strcpy(dirname, DEVD_DIR);
108 strfieldcat(dirname, devname);
109 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
110
5d24c6ca 111 temp[0] = '/';
a3e6c800
GKH
112 ++temp;
113 temp = strchr(temp, '/');
114 }
dd64e26b 115
4a539daf 116 strcpy(dirname, DEVD_DIR);
7a947ce5 117 strfieldcat(dirname, udev->name);
4a539daf 118 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
dd64e26b 119
4a539daf 120 strcpy(dirname, DEVD_DIR);
7a947ce5 121 strfieldcat(dirname, udev->subsystem);
4a539daf 122 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
dd64e26b 123
4a539daf
KS
124 strcpy(dirname, DEVD_DIR "default");
125 call_foreach_file(run_program, dirname, DEVD_SUFFIX);
dd64e26b 126}