123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- #include <stdlib.h>
- #include <stddef.h>
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdbool.h>
- #include <unistd.h>
- #include <getopt.h>
- #include <errno.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <syslog.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <string.h>
- #define PATH_SIZE 512
- #ifndef strlcpy
- #define strlcpy(d,s,l) (strncpy(d,s,l), (d)[(l)-1] = '\0')
- #endif
- #ifndef strlcat
- #define strlcat(d,s,l) strncat(d,s,(l)-strlen(d)-1)
- #endif
- static int verbose;
- static int dry_run;
- static void log_message(int priority, const char *format, ...)
- {
- va_list args;
- va_start(args, format);
- vsyslog(priority, format, args);
- va_end(args);
- }
- #undef err
- #define err(format, arg...) \
- do { \
- log_message(LOG_ERR ,"%s: " format ,__FUNCTION__ ,## arg); \
- } while (0)
- #undef info
- #define info(format, arg...) \
- do { \
- log_message(LOG_INFO ,"%s: " format ,__FUNCTION__ ,## arg); \
- } while (0)
- #ifdef UDEV_DEBUG
- #undef dbg
- #define dbg(format, arg...) \
- do { \
- log_message(LOG_DEBUG ,"%s: " format ,__FUNCTION__ ,## arg); \
- } while (0)
- #else
- #define dbg(...) do {} while(0)
- #endif
- static void trigger_uevent(const char *devpath)
- {
- char filename[PATH_SIZE];
- int fd;
- strlcpy(filename, "/sys", sizeof(filename));
- strlcat(filename, devpath, sizeof(filename));
- strlcat(filename, "/uevent", sizeof(filename));
- if (verbose)
- printf("%s\n", devpath);
- if (dry_run)
- return;
- fd = open(filename, O_WRONLY);
- if (fd < 0) {
- dbg("error on opening %s: %m\n", filename);
- return;
- }
- if (write(fd, "add", 3) < 0)
- info("error on triggering %s: %m\n", filename);
- close(fd);
- }
- static int sysfs_resolve_link(char *devpath, size_t size)
- {
- char link_path[PATH_SIZE];
- char link_target[PATH_SIZE];
- int len;
- int i;
- int back;
- strlcpy(link_path, "/sys", sizeof(link_path));
- strlcat(link_path, devpath, sizeof(link_path));
- len = readlink(link_path, link_target, sizeof(link_target) - 1);
- if (len <= 0)
- return -1;
- link_target[len] = '\0';
- dbg("path link '%s' points to '%s'", devpath, link_target);
- for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
- ;
- dbg("base '%s', tail '%s', back %i", devpath, &link_target[back * 3], back);
- for (i = 0; i <= back; i++) {
- char *pos = strrchr(devpath, '/');
- if (pos == NULL)
- return -1;
- pos[0] = '\0';
- }
- dbg("after moving back '%s'", devpath);
- strlcat(devpath, "/", size);
- strlcat(devpath, &link_target[back * 3], size);
- return 0;
- }
- static bool device_has_attribute(const char *path, const char *attr,
- mode_t mode)
- {
- char filename[PATH_SIZE];
- struct stat statbuf;
- strlcpy(filename, path, sizeof(filename));
- strlcat(filename, attr, sizeof(filename));
- if (stat(filename, &statbuf) < 0)
- return false;
- if (!(statbuf.st_mode & mode))
- return false;
- return true;
- }
- static int device_list_insert(const char *path)
- {
- char devpath[PATH_SIZE];
- struct stat statbuf;
- dbg("add '%s'" , path);
-
- if (!device_has_attribute(path, "/dev", S_IRUSR) ||
- !device_has_attribute(path, "/uevent", S_IWUSR))
- return -1;
- strlcpy(devpath, &path[4], sizeof(devpath));
-
- if (lstat(path, &statbuf) < 0)
- return -1;
- if (S_ISLNK(statbuf.st_mode))
- if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
- return -1;
- trigger_uevent(devpath);
- return 0;
- }
- static void scan_subdir(const char *base, const char *subdir,
- bool insert, int depth)
- {
- DIR *dir;
- struct dirent *dent;
- dir = opendir(base);
- if (dir == NULL)
- return;
- for (dent = readdir(dir); dent != NULL; dent = readdir(dir)) {
- char dirname[PATH_SIZE];
- if (dent->d_name[0] == '.')
- continue;
- strlcpy(dirname, base, sizeof(dirname));
- strlcat(dirname, "/", sizeof(dirname));
- strlcat(dirname, dent->d_name, sizeof(dirname));
- if (insert) {
- int err;
- err = device_list_insert(dirname);
- if (err)
- continue;
- }
- if (subdir)
- strlcat(dirname, subdir, sizeof(base));
- if (depth)
- scan_subdir(dirname, NULL, true, depth - 1);
- }
- closedir(dir);
- }
- int main(int argc, char *argv[], char *envp[])
- {
- struct stat statbuf;
- int option;
- openlog("udevtrigger", LOG_PID | LOG_CONS, LOG_DAEMON);
- while (1) {
- option = getopt(argc, argv, "vnh");
- if (option == -1)
- break;
- switch (option) {
- case 'v':
- verbose = 1;
- break;
- case 'n':
- dry_run = 1;
- break;
- case 'h':
- printf("Usage: udevtrigger OPTIONS\n"
- " -v print the list of devices while running\n"
- " -n do not actually trigger the events\n"
- " -h print this text\n"
- "\n");
- goto exit;
- default:
- goto exit;
- }
- }
-
- scan_subdir("/sys/bus", "/devices", false, 1);
- scan_subdir("/sys/class", NULL, false, 1);
-
- if (stat("/sys/class/block", &statbuf) != 0)
- scan_subdir("/sys/block", NULL, true, 1);
- exit:
- closelog();
- return 0;
- }
|