forked from illiliti/libudev-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.c
63 lines (55 loc) · 1.29 KB
/
helper.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
* this helper pretty similar to helper.sh, but it
* doesn't write unrelated variables to file (e.g PWD or PATH)
*
* build:
* cc helper.c -o helper
*
* usage:
* echo /full/path/to/helper > /proc/sys/kernel/hotplug
* echo "/full/path/to/helper UDEV_MONITOR_DIR" > /proc/sys/kernel/hotplug
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
int main(int argc, char **argv)
{
extern char **environ;
char path[PATH_MAX];
char *dir;
int fd, i;
switch (argc) {
case 1:
dir = "/tmp/.libudev-zero";
break;
case 2:
dir = argv[1];
break;
default:
fprintf(stderr, "usage: %s [dir]\n", argv[0]);
return 1;
}
snprintf(path, sizeof(path), "%s/uevent.XXXXXX", dir);
fd = mkstemp(path);
if (fd == -1) {
perror("mkstemp");
return 1;
}
for (i = 0; environ[i]; i++) {
if (strncmp(environ[i], "PATH", 4) == 0 ||
strncmp(environ[i], "HOME", 4) == 0) {
continue;
}
if (write(fd, environ[i], strlen(environ[i])) == -1 ||
write(fd, "\n", 1) == -1) {
perror("write");
close(fd);
unlink(path);
return 1;
}
}
close(fd);
return 0;
}