add helper.c

This commit is contained in:
illiliti
2020-08-18 12:28:22 +03:00
parent b17173163f
commit b53bb9eb82
2 changed files with 64 additions and 1 deletions

63
contrib/helper.c Normal file
View File

@@ -0,0 +1,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;
}

View File

@@ -266,7 +266,7 @@ struct udev_monitor *udev_monitor_new_from_netlink(struct udev *udev, const char
return NULL;
}
if (inotify_add_watch(udev_monitor->ifd, UDEV_MONITOR_DIR, IN_CLOSE_WRITE) == -1) {
if (inotify_add_watch(udev_monitor->ifd, UDEV_MONITOR_DIR, IN_CLOSE_WRITE | IN_EXCL_UNLINK) == -1) {
close(udev_monitor->ifd);
close(udev_monitor->efd);
free(udev_monitor);