util: readuevent: read uevent file and set environment variables - smdev - suck… | |
git clone git://git.suckless.org/smdev | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 87a607f4331ce9994047eac5b640a1216de892c5 | |
parent 3432f0ce1619b8c0a8d81d87570c5873e3ef01b3 | |
Author: Hiltjo Posthuma <[email protected]> | |
Date: Mon, 7 Apr 2014 17:31:16 +0200 | |
util: readuevent: read uevent file and set environment variables | |
remove devtomajmin() | |
Signed-off-by: Hiltjo Posthuma <[email protected]> | |
Diffstat: | |
M smdev.c | 24 ++++++++++++------------ | |
M util.h | 2 +- | |
M util/dev.c | 43 +++++++++++++++++++----------… | |
3 files changed, 39 insertions(+), 30 deletions(-) | |
--- | |
diff --git a/smdev.c b/smdev.c | |
@@ -53,8 +53,7 @@ static void parsepath(struct rule *rule, struct rulepath *rpa… | |
static int removedev(struct event *ev); | |
static int createdev(struct event *ev); | |
static int doevent(struct event *ev); | |
-static int craftev(struct event *ev, enum action action, | |
- char *sysfspath); | |
+static int craftev(char *sysfspath); | |
static void populatedev(const char *path); | |
static void | |
@@ -352,16 +351,18 @@ doevent(struct event *ev) | |
/* Craft a fake event so the rest of the code can cope */ | |
static int | |
-craftev(struct event *ev, enum action action, char *sysfspath) | |
+craftev(char *sysfspath) | |
{ | |
char path[PATH_MAX]; | |
+ char *devpath; | |
+ | |
+ devpath = sysfspath + strlen("/sys"); | |
+ snprintf(path, sizeof(path), "/sys%s/uevent", devpath); | |
- ev->action = action; | |
- ev->devpath = sysfspath + strlen("/sys"); | |
- ev->devname = basename(sysfspath); | |
- snprintf(path, sizeof(path), "/sys%s/dev", | |
- ev->devpath); | |
- if (devtomajmin(path, &ev->major, &ev->minor) < 0) | |
+ clearenv(); | |
+ setenv("ACTION", "add", 1); | |
+ setenv("DEVPATH", devpath, 1); | |
+ if(readuevent(path) < 0) | |
return -1; | |
return 0; | |
} | |
@@ -370,13 +371,12 @@ static void | |
populatedev(const char *path) | |
{ | |
char *cwd; | |
- struct event ev; | |
recurse(path, populatedev); | |
if (strcmp(path, "dev") == 0) { | |
cwd = agetcwd(); | |
- if (!craftev(&ev, ADD_ACTION, cwd)) | |
- doevent(&ev); | |
+ if (!craftev(cwd)) | |
+ dohotplug(); | |
free(cwd); | |
} | |
} | |
diff --git a/util.h b/util.h | |
@@ -7,7 +7,7 @@ extern char *argv0; | |
char *agetcwd(void); | |
void apathmax(char **, long *); | |
-int devtomajmin(const char *path, int *maj, int *min); | |
+int readuevent(const char *); | |
int devtype(const char *majmin); | |
void enprintf(int, const char *, ...); | |
void eprintf(const char *, ...); | |
diff --git a/util/dev.c b/util/dev.c | |
@@ -3,31 +3,40 @@ | |
#include <fcntl.h> | |
#include <unistd.h> | |
#include <limits.h> | |
+#include <stdlib.h> | |
#include <stdio.h> | |
+#include <string.h> | |
#include "../util.h" | |
-/* Example `path' is /sys/devices/virtual/tty/tty0/dev */ | |
+/* read uevent file and set environment variables */ | |
int | |
-devtomajmin(const char *path, int *maj, int *min) | |
+readuevent(const char *file) | |
{ | |
+ FILE *fp; | |
+ int status = 0; | |
char buf[BUFSIZ]; | |
- int fd; | |
- ssize_t n; | |
+ char *p, *name, *value; | |
- fd = open(path, O_RDONLY); | |
- if (fd < 0) | |
- eprintf("open %s:", path); | |
- n = read(fd, buf, sizeof(buf) - 1); | |
- close(fd); | |
- if (n < 0) | |
- eprintf("%s: read error:", path); | |
- if (!n) | |
+ if(!(fp = fopen(file, "r"))) | |
return -1; | |
- if (buf[n - 1] == '\n') | |
- buf[n - 1] = '\0'; | |
- buf[n] = '\0'; | |
- sscanf(buf, "%d:%d", maj, min); | |
- return 0; | |
+ while(!feof(fp)) { | |
+ fgets(buf, sizeof(buf) - 1, fp); | |
+ if(ferror(fp)) { | |
+ status = -2; | |
+ break; | |
+ } | |
+ if((p = strchr(buf, '\n'))) | |
+ *p = '\0'; | |
+ if(!(p = strchr(buf, '='))) | |
+ continue; | |
+ *p = '\0'; | |
+ p++; | |
+ name = buf; | |
+ value = p; | |
+ setenv(name, value, 1); | |
+ } | |
+ fclose(fp); | |
+ return status; | |
} | |
/* `majmin' format is maj:min */ |