diff options
| author | Michael Forney <mforney@mforney.org> | 2020-06-23 12:02:38 -0700 |
|---|---|---|
| committer | Michael Forney <mforney@mforney.org> | 2020-06-23 12:24:16 -0700 |
| commit | 9aa9052cc198dcbd05b299f0d1b6dc7224c84dc9 (patch) | |
| tree | 4038a042cc27dce06031992be04027ff336811ab /src/devd-trigger.c | |
| parent | 53e09653d94bb395beb81742ed4dc1dc09b76829 (diff) | |
Move devd to separate repository
Diffstat (limited to 'src/devd-trigger.c')
| -rw-r--r-- | src/devd-trigger.c | 106 |
1 files changed, 0 insertions, 106 deletions
diff --git a/src/devd-trigger.c b/src/devd-trigger.c deleted file mode 100644 index e0a3373d..00000000 --- a/src/devd-trigger.c +++ /dev/null @@ -1,106 +0,0 @@ -#define _XOPEN_SOURCE 700 -#include <errno.h> -#include <fcntl.h> -#include <limits.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <stdnoreturn.h> -#include <string.h> - -#include <dirent.h> -#include <sys/stat.h> -#include <unistd.h> - -static char path[PATH_MAX] = "/sys/devices"; -static size_t pathlen; -static int ret; - -static void -error(const char *fmt, ...) -{ - va_list ap; - - va_start(ap, fmt); - vfprintf(stderr, fmt, ap); - va_end(ap); - if (fmt[0] && fmt[strlen(fmt) - 1] == ':') { - fputc(' ', stderr); - perror(NULL); - } else { - fputc('\n', stderr); - } - ret = 1; -} - -static void -trigger(int fd, const char *name) -{ - static const char action[] = "add"; - - fd = openat(fd, name, O_WRONLY); - if (fd == -1) { - error("open %s:", path); - } else { - if (write(fd, action, sizeof(action) - 1) != (ssize_t)(sizeof(action) - 1)) - error("write %s:", path); - close(fd); - } -} - -static void -search(int fd, const char *name) -{ - DIR *dir; - struct dirent *d; - char *nul; - size_t oldlen; - struct stat st; - - fd = openat(fd, name, O_RDONLY | O_DIRECTORY); - if (fd == -1) { - error("opendir %s:", path); - return; - } - dir = fdopendir(fd); - if (!dir) { - error("opendir %s:", path); - return; - } - oldlen = pathlen; - for (;; pathlen = oldlen) { - errno = 0; - d = readdir(dir); - if (!d) - break; - if (strcmp(d->d_name, ".") == 0 || strcmp(d->d_name, "..") == 0) - continue; - path[pathlen++] = '/'; - nul = memccpy(path + pathlen, d->d_name, '\0', sizeof(path) - pathlen); - if (!nul) { - error("path too large"); - continue; - } - pathlen = nul - path - 1; - if (fstatat(fd, d->d_name, &st, AT_SYMLINK_NOFOLLOW) == -1) { - error("stat %s:", path); - continue; - } - if (S_ISDIR(st.st_mode)) - search(fd, d->d_name); - else if (S_ISREG(st.st_mode) && strcmp(d->d_name, "uevent") == 0) - trigger(fd, d->d_name); - } - path[pathlen] = '\0'; - if (errno) - error("readdir %s", path); - closedir(dir); -} - -int -main(int argc, char *argv[]) -{ - pathlen = strlen(path); - search(AT_FDCWD, path); - return ret; -} |
