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>
38 #define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0')
42 #define strlcat(d,s,l) strncat(d,s,(l)-strlen(d)-1)
48 static void log_message(int priority, const char *format, ...)
52 va_start(args, format);
53 vsyslog(priority, format, args);
58 #define err(format, arg...) \
60 log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
64 #define info(format, arg...) \
66 log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
71 #define dbg(format, arg...) \
73 log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
76 #define dbg(...) do {} while(0)
80 static void trigger_uevent(const char *devpath)
82 char filename[PATH_SIZE];
85 strlcpy(filename, "/sys", sizeof(filename));
86 strlcat(filename, devpath, sizeof(filename));
87 strlcat(filename, "/uevent", sizeof(filename));
90 printf("%s\n", devpath);
95 fd = open(filename, O_WRONLY);
97 dbg("error on opening %s: %s\n", filename, strerror(errno));
101 if (write(fd, "add", 3) < 0)
102 info("error on triggering %s: %s\n", filename, strerror(errno));
107 static int sysfs_resolve_link(char *devpath, size_t size)
109 char link_path[PATH_SIZE];
110 char link_target[PATH_SIZE];
115 strlcpy(link_path, "/sys", sizeof(link_path));
116 strlcat(link_path, devpath, sizeof(link_path));
117 len = readlink(link_path, link_target, sizeof(link_target));
120 link_target[len] = '\0';
121 dbg("path link '%s' points to '%s'", devpath, link_target);
123 for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
125 dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
126 for (i = 0; i <= back; i++) {
127 char *pos = strrchr(devpath, '/');
133 dbg("after moving back '%s'", devpath);
134 strlcat(devpath, "/", size);
135 strlcat(devpath, &link_target[back * 3], size);
139 static bool device_has_attribute(const char *path, const char *attr,
142 char filename[PATH_SIZE];
145 strlcpy(filename, path, sizeof(filename));
146 strlcat(filename, attr, sizeof(filename));
148 if (stat(filename, &statbuf) < 0)
151 if (!(statbuf.st_mode & mode))
157 static int device_list_insert(const char *path)
159 char devpath[PATH_SIZE];
162 dbg("add '%s'" , path);
164 /* we only have a device, if we have a dev and an uevent file */
165 if (!device_has_attribute(path, "/dev", S_IRUSR) ||
166 !device_has_attribute(path, "/uevent", S_IWUSR))
169 strlcpy(devpath, &path[4], sizeof(devpath));
171 /* resolve possible link to real target */
172 if (lstat(path, &statbuf) < 0)
174 if (S_ISLNK(statbuf.st_mode))
175 if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
178 trigger_uevent(devpath);
183 static void scan_subsystem(const char *subsys)
185 char base[PATH_SIZE];
189 strlcpy(base, "/sys/", sizeof(base));
190 strlcat(base, subsys, sizeof(base));
194 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
195 char dirname[PATH_SIZE];
197 struct dirent *dent2;
199 if (dent->d_name[0] == '.')
202 strlcpy(dirname, base, sizeof(dirname));
203 strlcat(dirname, "/", sizeof(dirname));
204 strlcat(dirname, dent->d_name, sizeof(dirname));
205 strlcat(dirname, "/devices", sizeof(dirname));
207 /* look for devices */
208 dir2 = opendir(dirname);
210 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
211 char dirname2[PATH_SIZE];
213 if (dent2->d_name[0] == '.')
216 strlcpy(dirname2, dirname, sizeof(dirname2));
217 strlcat(dirname2, "/", sizeof(dirname2));
218 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
219 device_list_insert(dirname2);
228 static void scan_block(void)
230 char base[PATH_SIZE];
234 strlcpy(base, "/sys/block", sizeof(base));
238 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
239 char dirname[PATH_SIZE];
241 struct dirent *dent2;
243 if (dent->d_name[0] == '.')
246 strlcpy(dirname, base, sizeof(dirname));
247 strlcat(dirname, "/", sizeof(dirname));
248 strlcat(dirname, dent->d_name, sizeof(dirname));
249 if (device_list_insert(dirname) != 0)
252 /* look for partitions */
253 dir2 = opendir(dirname);
255 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
256 char dirname2[PATH_SIZE];
258 if (dent2->d_name[0] == '.')
261 if (!strcmp(dent2->d_name,"device"))
264 strlcpy(dirname2, dirname, sizeof(dirname2));
265 strlcat(dirname2, "/", sizeof(dirname2));
266 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
267 device_list_insert(dirname2);
276 static void scan_class(void)
278 char base[PATH_SIZE];
282 strlcpy(base, "/sys/class", sizeof(base));
286 for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
287 char dirname[PATH_SIZE];
289 struct dirent *dent2;
291 if (dent->d_name[0] == '.')
294 strlcpy(dirname, base, sizeof(dirname));
295 strlcat(dirname, "/", sizeof(dirname));
296 strlcat(dirname, dent->d_name, sizeof(dirname));
297 dir2 = opendir(dirname);
299 for (dent2 = readdir(dir2); dent2 != NULL; dent2 = readdir(dir2)) {
300 char dirname2[PATH_SIZE];
302 if (dent2->d_name[0] == '.')
305 if (!strcmp(dent2->d_name, "device"))
308 strlcpy(dirname2, dirname, sizeof(dirname2));
309 strlcat(dirname2, "/", sizeof(dirname2));
310 strlcat(dirname2, dent2->d_name, sizeof(dirname2));
311 device_list_insert(dirname2);
320 int main(int argc, char *argv[], char *envp[])
322 char base[PATH_SIZE];
326 openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON);
329 option = getopt(argc, argv, "vnh");
341 printf("Usage: udevtrigger OPTIONS\n"
342 " -v print the list of devices while running\n"
343 " -n do not actually trigger the events\n"
344 " -h print this text\n"
353 /* if we have /sys/subsystem, forget all the old stuff */
354 scan_subsystem("bus");
357 /* scan "block" if it isn't a "class" */
358 strlcpy(base, "/sys/class/block", sizeof(base));
359 if (stat(base, &statbuf) != 0)