Skip to content

Commit

Permalink
Allow invoking helper script as a replacement for udev rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Ella-0 committed Jul 11, 2024
1 parent bbeb7ad commit 5ec5355
Showing 1 changed file with 94 additions and 11 deletions.
105 changes: 94 additions & 11 deletions udev_device.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <spawn.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <linux/input.h>

#include "udev.h"
Expand Down Expand Up @@ -52,6 +54,10 @@
#define INPUT_PROP_CNT 0x20
#endif

#ifndef RULES_HELPER_NAME
#define RULES_HELPER_NAME "libudev-zero-rules-helper"
#endif

struct udev_device {
struct udev_list_entry properties;
struct udev_list_entry sysattrs;
Expand Down Expand Up @@ -349,19 +355,10 @@ static char *read_symlink(const char *syspath, const char *name)
return strdup(strrchr(link, '/') + 1);
}

static int set_properties_from_uevent(struct udev_device *udev_device, const char *syspath)
static void set_properties_from_file(struct udev_device *udev_device, FILE *file)
{
char line[LINE_MAX], path[PATH_MAX + sizeof("/uevent")], devnode[PATH_MAX];
FILE *file;
char line[LINE_MAX], devnode[PATH_MAX];
char *pos;

snprintf(path, sizeof(path), "%s/uevent", syspath);
file = fopen(path, "r");

if (!file) {
return -1;
}

while (fgets(line, sizeof(line), file)) {
line[strlen(line) - 1] = '\0';

Expand All @@ -374,7 +371,21 @@ static int set_properties_from_uevent(struct udev_device *udev_device, const cha
udev_list_entry_add(&udev_device->properties, line, pos + 1, 0);
}
}
}

static int set_properties_from_uevent(struct udev_device *udev_device, const char *syspath)
{
char path[PATH_MAX + sizeof("/uevent")];
FILE *file;

snprintf(path, sizeof(path), "%s/uevent", syspath);
file = fopen(path, "r");

if (!file) {
return -1;
}

set_properties_from_file(udev_device, file);
fclose(file);
return 0;
}
Expand Down Expand Up @@ -540,6 +551,76 @@ static void set_properties_from_props(struct udev_device *udev_device)
udev_list_entry_add(&udev_device->properties, "ID_PATH", id, 0);
}

static int set_properties_from_helper(struct udev_device *udev_device)
{
size_t size = 0;
size_t count = 0;
struct udev_list_entry *entry;
for (entry = udev_device_get_properties_list_entry(udev_device);
entry;
entry = udev_list_entry_get_next(entry)) {

const char *name = entry->name;
const char *value = entry->value?entry->value:"";
// '{name}={value}\0'
size += strlen(name) + 1 + strlen(value) + 1;
count += 1;
}

char **envp = malloc((count + 1) * sizeof(char *));
char *env = malloc(size);
char *ptr = env;
unsigned i = 0;
for (entry = udev_device_get_properties_list_entry(udev_device);
entry;
entry = udev_list_entry_get_next(entry)) {
const char *name = entry->name;
const char *value = entry->value?entry->value:"";
// '{name}={value}\0'
size_t max_size = strlen(name) + 1 + strlen(value) + 1;
snprintf(ptr, max_size, "%s=%s", name, value);
envp[i] = ptr;
ptr += max_size;
i++;
}
envp[count] = NULL;

int out_pipe[2];
posix_spawn_file_actions_t actions;
pipe(out_pipe);

posix_spawn_file_actions_init(&actions);
posix_spawn_file_actions_addclose(&actions, out_pipe[0]);
posix_spawn_file_actions_adddup2(&actions, out_pipe[1], 1);
posix_spawn_file_actions_addclose(&actions, out_pipe[1]);

pid_t pid;

const char *argv[] = { RULES_HELPER_NAME, NULL };

if (posix_spawnp(&pid, argv[0], &actions, NULL, argv, envp)) {
perror("Could not launch properties helper");
return -1;
}

free(envp);
free(env);
close(out_pipe[1]);

FILE *file = fdopen(out_pipe[0], "r");

if (!file) {
return -1;
}

set_properties_from_file(udev_device, file);

int status;
waitpid(pid, &status, 0);
close(out_pipe[0]);
return 0;
}

struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *syspath)
{
char *subsystem, *driver, *sysname;
Expand Down Expand Up @@ -593,6 +674,7 @@ struct udev_device *udev_device_new_from_syspath(struct udev *udev, const char *

set_properties_from_evdev(udev_device);
set_properties_from_props(udev_device);
set_properties_from_helper(udev_device);

free(driver);
free(subsystem);
Expand Down Expand Up @@ -712,6 +794,7 @@ struct udev_device *udev_device_new_from_uevent(struct udev *udev, char *buf, si

set_properties_from_props(udev_device);
set_properties_from_evdev(udev_device);
set_properties_from_helper(udev_device);
return udev_device;
}

Expand Down

0 comments on commit 5ec5355

Please sign in to comment.