2 * Copyright (C) 2004-2006 Kay Sievers <kay@vrfy.org>
3 * Copyright (C) 2006 Hannes Reinecke <hare@suse.de>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 #include <sys/types.h>
39 void log_message(int priority
, const char *format
, ...)
43 va_start(args
, format
);
44 vsyslog(priority
, format
, args
);
49 #define err(format, arg...) \
51 log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
55 #define info(format, arg...) \
57 log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
62 #define dbg(format, arg...) \
64 log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
67 #define dbg(...) do {} while(0)
71 static void trigger_uevent(const char *devpath
)
73 char filename
[PATH_SIZE
];
76 strlcpy(filename
, "/sys", sizeof(filename
));
77 strlcat(filename
, devpath
, sizeof(filename
));
78 strlcat(filename
, "/uevent", sizeof(filename
));
81 printf("%s\n", devpath
);
86 fd
= open(filename
, O_WRONLY
);
88 dbg("error on opening %s: %s\n", filename
, strerror(errno
));
92 if (write(fd
, "add", 3) < 0)
93 info("error on triggering %s: %s\n", filename
, strerror(errno
));
98 static int sysfs_resolve_link(char *devpath
, size_t size
)
100 char link_path
[PATH_SIZE
];
101 char link_target
[PATH_SIZE
];
106 strlcpy(link_path
, "/sys", sizeof(link_path
));
107 strlcat(link_path
, devpath
, sizeof(link_path
));
108 len
= readlink(link_path
, link_target
, sizeof(link_target
));
111 link_target
[len
] = '\0';
112 dbg("path link '%s' points to '%s'", devpath
, link_target
);
114 for (back
= 0; strncmp(&link_target
[back
* 3], "../", 3) == 0; back
++)
116 dbg("base '%s', tail '%s', back %i", devpath
, &link_target
[back
* 3], back
);
117 for (i
= 0; i
<= back
; i
++) {
118 char *pos
= strrchr(devpath
, '/');
124 dbg("after moving back '%s'", devpath
);
125 strlcat(devpath
, "/", size
);
126 strlcat(devpath
, &link_target
[back
* 3], size
);
131 static int device_list_insert(const char *path
)
133 char filename
[PATH_SIZE
];
134 char devpath
[PATH_SIZE
];
137 dbg("add '%s'" , path
);
139 /* we only have a device, if we have an uevent file */
140 strlcpy(filename
, path
, sizeof(filename
));
141 strlcat(filename
, "/uevent", sizeof(filename
));
142 if (stat(filename
, &statbuf
) < 0)
144 if (!(statbuf
.st_mode
& S_IWUSR
))
147 strlcpy(devpath
, &path
[4], sizeof(devpath
));
149 /* resolve possible link to real target */
150 if (lstat(path
, &statbuf
) < 0)
152 if (S_ISLNK(statbuf
.st_mode
))
153 if (sysfs_resolve_link(devpath
, sizeof(devpath
)) != 0)
156 trigger_uevent(devpath
);
161 static void scan_subsystem(const char *subsys
)
163 char base
[PATH_SIZE
];
167 strlcpy(base
, "/sys/", sizeof(base
));
168 strlcat(base
, subsys
, sizeof(base
));
172 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
173 char dirname
[PATH_SIZE
];
175 struct dirent
*dent2
;
177 if (dent
->d_name
[0] == '.')
180 strlcpy(dirname
, base
, sizeof(dirname
));
181 strlcat(dirname
, "/", sizeof(dirname
));
182 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
183 strlcat(dirname
, "/devices", sizeof(dirname
));
185 /* look for devices */
186 dir2
= opendir(dirname
);
188 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
189 char dirname2
[PATH_SIZE
];
191 if (dent2
->d_name
[0] == '.')
194 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
195 strlcat(dirname2
, "/", sizeof(dirname2
));
196 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
197 device_list_insert(dirname2
);
206 static void scan_block(void)
208 char base
[PATH_SIZE
];
212 strlcpy(base
, "/sys/block", sizeof(base
));
216 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
217 char dirname
[PATH_SIZE
];
219 struct dirent
*dent2
;
221 if (dent
->d_name
[0] == '.')
224 strlcpy(dirname
, base
, sizeof(dirname
));
225 strlcat(dirname
, "/", sizeof(dirname
));
226 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
227 if (device_list_insert(dirname
) != 0)
230 /* look for partitions */
231 dir2
= opendir(dirname
);
233 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
234 char dirname2
[PATH_SIZE
];
236 if (dent2
->d_name
[0] == '.')
239 if (!strcmp(dent2
->d_name
,"device"))
242 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
243 strlcat(dirname2
, "/", sizeof(dirname2
));
244 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
245 device_list_insert(dirname2
);
254 static void scan_class(void)
256 char base
[PATH_SIZE
];
260 strlcpy(base
, "/sys/class", sizeof(base
));
264 for (dent
= readdir(dir
); dent
!= NULL
; dent
= readdir(dir
)) {
265 char dirname
[PATH_SIZE
];
267 struct dirent
*dent2
;
269 if (dent
->d_name
[0] == '.')
272 strlcpy(dirname
, base
, sizeof(dirname
));
273 strlcat(dirname
, "/", sizeof(dirname
));
274 strlcat(dirname
, dent
->d_name
, sizeof(dirname
));
275 dir2
= opendir(dirname
);
277 for (dent2
= readdir(dir2
); dent2
!= NULL
; dent2
= readdir(dir2
)) {
278 char dirname2
[PATH_SIZE
];
280 if (dent2
->d_name
[0] == '.')
283 if (!strcmp(dent2
->d_name
, "device"))
286 strlcpy(dirname2
, dirname
, sizeof(dirname2
));
287 strlcat(dirname2
, "/", sizeof(dirname2
));
288 strlcat(dirname2
, dent2
->d_name
, sizeof(dirname2
));
289 device_list_insert(dirname2
);
298 int main(int argc
, char *argv
[], char *envp
[])
300 char base
[PATH_SIZE
];
305 openlog("udevtrigger", LOG_PID
| LOG_CONS
, LOG_DAEMON
);
308 option
= getopt(argc
, argv
, "vnh");
320 printf("Usage: udevtrigger OPTIONS\n"
321 " -v print the list of devices while running\n"
322 " -n do not actually trigger the events\n"
323 " -h print this text\n"
332 /* if we have /sys/subsystem, forget all the old stuff */
333 scan_subsystem("bus");
336 /* scan "block" if it isn't a "class" */
337 strlcpy(base
, "/sys/class/block", sizeof(base
));
338 if (stat(base
, &statbuf
) != 0)
This page took 0.071953 seconds and 5 git commands to generate.