general code cleanup

This commit is contained in:
illiliti
2021-01-20 08:58:19 +03:00
parent 9868e5e9ea
commit b90b0acc88
4 changed files with 151 additions and 178 deletions

View File

@@ -9,7 +9,7 @@
#include "udev.h"
#include "udev_list.h"
enum { BITS_MAX = 96 };
#define BITS_MAX 96
struct udev_device {
struct udev_list_entry properties;
@@ -136,23 +136,17 @@ struct udev_device *udev_device_get_parent_with_subsystem_devtype(struct udev_de
return NULL;
}
parent = udev_device_get_parent(udev_device);
while (parent) {
for (parent = udev_device_get_parent(udev_device); parent; parent = udev_device_get_parent(parent)) {
parent_subsystem = udev_device_get_subsystem(parent);
parent_devtype = udev_device_get_devtype(parent);
if (parent_subsystem && strcmp(parent_subsystem, subsystem) == 0) {
if (!devtype) {
return parent;
}
if (parent_devtype && strcmp(parent_devtype, devtype) == 0) {
return parent;
}
if (!parent_subsystem || strcmp(parent_subsystem, subsystem) != 0) {
continue;
}
parent = udev_device_get_parent(parent);
if (!devtype || (parent_devtype && strcmp(parent_devtype, devtype) == 0)) {
return parent;
}
}
return NULL;
@@ -205,7 +199,7 @@ const char *udev_device_get_property_value(struct udev_device *udev_device, cons
const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const char *sysattr)
{
struct udev_list_entry *list_entry;
char data[BUFSIZ], path[PATH_MAX];
char data[1024], path[PATH_MAX];
struct stat st;
size_t len;
FILE *file;
@@ -241,13 +235,11 @@ const char *udev_device_get_sysattr_value(struct udev_device *udev_device, const
}
if ((pos = memchr(data, '\n', len))) {
*pos = '\0';
}
else {
data[len] = '\0';
len = pos - data;
}
fclose(file);
data[len] = '\0';
list_entry = udev_list_entry_add(&udev_device->sysattrs, sysattr, data, 0);
return udev_list_entry_get_value(list_entry);
}
@@ -305,9 +297,9 @@ static char *udev_device_read_symlink(struct udev_device *udev_device, const cha
static void udev_device_set_properties_from_uevent(struct udev_device *udev_device)
{
char line[LINE_MAX], path[PATH_MAX];
char *pos, devnode[PATH_MAX];
char line[LINE_MAX], path[PATH_MAX], devnode[PATH_MAX];
FILE *file;
char *pos;
snprintf(path, sizeof(path), "%s/uevent", udev_device_get_syspath(udev_device));
file = fopen(path, "r");
@@ -317,7 +309,7 @@ static void udev_device_set_properties_from_uevent(struct udev_device *udev_devi
}
while (fgets(line, sizeof(line), file)) {
line[strcspn(line, "\n")] = '\0';
line[strlen(line) - 1] = '\0';
if (strncmp(line, "DEVNAME", 7) == 0) {
snprintf(devnode, sizeof(devnode), "/dev/%s", line + 8);
@@ -332,41 +324,34 @@ static void udev_device_set_properties_from_uevent(struct udev_device *udev_devi
fclose(file);
}
static int populate_bit(unsigned long *arr, const char *val)
static int populate_bit(unsigned long *arr, const char *str)
{
char *bit, *bits, *save;
char *beg, *end;
int i;
if (!val) {
if (!str) {
return 0;
}
bits = strdup(val);
// TODO drop strdup ?
beg = end = strdup(str);
if (!bits) {
if (!beg) {
return 0;
}
// TODO remove. strtoul is able to handle this
bit = strtok_r(bits, " ", &save);
for (i = 0; bit && i < BITS_MAX; i++) {
arr[i] = strtoul(bit, NULL, 16);
bit = strtok_r(NULL, " ", &save);
for (i = 0; end[0] != '\0' && i < BITS_MAX; i++) {
arr[i] = strtoul(end, &end, 16);
}
free(bits);
free(beg);
return i;
}
static int find_bit(unsigned long *arr, int cnt, int bit)
static int find_bit(unsigned long *arr, int cnt, unsigned long bit)
{
int i;
if (!cnt) {
return 0;
}
for (i = 0; i < cnt; i++) {
if (arr[i] & (1UL << (bit % LONG_BIT))) {
return 1;
@@ -561,10 +546,10 @@ struct udev_device *udev_device_new_from_devnum(struct udev *udev, char type, de
switch (type) {
case 'c':
snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", major(devnum), minor(devnum));
snprintf(path, sizeof(path), "/sys/dev/char/%u:%u", major(devnum), minor(devnum));
break;
case 'b':
snprintf(path, sizeof(path), "/sys/dev/block/%d:%d", major(devnum), minor(devnum));
snprintf(path, sizeof(path), "/sys/dev/block/%u:%u", major(devnum), minor(devnum));
break;
default:
return NULL;
@@ -598,26 +583,26 @@ struct udev_device *udev_device_new_from_file(struct udev *udev, const char *pat
{
char line[LINE_MAX], syspath[PATH_MAX], devnode[PATH_MAX];
struct udev_device *udev_device;
char *pos, *sysname;
char *sysname = NULL;
struct stat st;
FILE *file;
char *pos;
int i;
udev_device = calloc(1, sizeof(struct udev_device));
if (!udev_device) {
return NULL;
}
if (stat(path, &st) != 0 || st.st_size > 8192) {
free(udev_device);
return NULL;
}
file = fopen(path, "r");
if (!file) {
free(udev_device);
return NULL;
}
udev_device = calloc(1, sizeof(struct udev_device));
if (!udev_device) {
fclose(file);
return NULL;
}
@@ -629,7 +614,7 @@ struct udev_device *udev_device_new_from_file(struct udev *udev, const char *pat
udev_list_entry_init(&udev_device->sysattrs);
while (fgets(line, sizeof(line), file)) {
line[strcspn(line, "\n")] = '\0';
line[strlen(line) - 1] = '\0';
if (strncmp(line, "DEVPATH", 7) == 0) {
snprintf(syspath, sizeof(syspath), "/sys%s", line + 8);
@@ -658,7 +643,7 @@ struct udev_device *udev_device_new_from_file(struct udev *udev, const char *pat
fclose(file);
if (!udev_device_get_syspath(udev_device)) {
if (!sysname) {
udev_device_unref(udev_device);
return NULL;
}

View File

@@ -24,63 +24,38 @@ struct udev_enumerate {
struct udev_enumerate_thread {
struct udev_enumerate *udev_enumerate;
pthread_mutex_t *mutex;
char path[PATH_MAX];
pthread_t thread;
const char *name;
const char *path;
};
int udev_enumerate_add_match_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
{
if (!udev_enumerate || !subsystem) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->subsystem_match, subsystem, NULL, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->subsystem_match, subsystem, NULL, 0) - 1 : -1;
}
int udev_enumerate_add_nomatch_subsystem(struct udev_enumerate *udev_enumerate, const char *subsystem)
{
if (!udev_enumerate || !subsystem) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->subsystem_nomatch, subsystem, NULL, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->subsystem_nomatch, subsystem, NULL, 0) - 1 : -1;
}
int udev_enumerate_add_match_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
{
if (!udev_enumerate || !sysattr) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->sysattr_match, sysattr, value, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->sysattr_match, sysattr, value, 0) - 1 : -1;
}
int udev_enumerate_add_nomatch_sysattr(struct udev_enumerate *udev_enumerate, const char *sysattr, const char *value)
{
if (!udev_enumerate || !sysattr) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->sysattr_nomatch, sysattr, value, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->sysattr_nomatch, sysattr, value, 0) - 1 : -1;
}
int udev_enumerate_add_match_property(struct udev_enumerate *udev_enumerate, const char *property, const char *value)
{
if (!udev_enumerate || !property) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->property_match, property, value, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->property_match, property, value, 0) - 1 : -1;
}
int udev_enumerate_add_match_sysname(struct udev_enumerate *udev_enumerate, const char *sysname)
{
if (!udev_enumerate || !sysname) {
return -1;
}
return udev_list_entry_add(&udev_enumerate->sysname_match, sysname, NULL, 0) ? 0 : -1;
return udev_enumerate ? !!udev_list_entry_add(&udev_enumerate->sysname_match, sysname, NULL, 0) - 1 : -1;
}
/* XXX NOT IMPLEMENTED */ int udev_enumerate_add_match_tag(struct udev_enumerate *udev_enumerate, const char *tag)
@@ -249,94 +224,99 @@ static int udev_enumerate_filter_sysattr(struct udev_enumerate *udev_enumerate,
static void *udev_enumerate_add_device(void *ptr)
{
struct udev_enumerate_thread *data = ptr;
struct udev_enumerate_thread *thread = ptr;
struct udev_device *udev_device;
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", data->path, data->name);
udev_device = udev_device_new_from_syspath(data->udev_enumerate->udev, path);
udev_device = udev_device_new_from_syspath(thread->udev_enumerate->udev, thread->path);
if (!udev_device) {
return NULL;
}
if (!udev_enumerate_filter_subsystem(data->udev_enumerate, udev_device) ||
!udev_enumerate_filter_sysname(data->udev_enumerate, udev_device) ||
!udev_enumerate_filter_property(data->udev_enumerate, udev_device) ||
!udev_enumerate_filter_sysattr(data->udev_enumerate, udev_device)) {
if (!udev_enumerate_filter_subsystem(thread->udev_enumerate, udev_device) ||
!udev_enumerate_filter_sysname(thread->udev_enumerate, udev_device) ||
!udev_enumerate_filter_property(thread->udev_enumerate, udev_device) ||
!udev_enumerate_filter_sysattr(thread->udev_enumerate, udev_device)) {
udev_device_unref(udev_device);
return NULL;
}
pthread_mutex_lock(data->mutex);
udev_list_entry_add(&data->udev_enumerate->devices, udev_device_get_syspath(udev_device), NULL, 0);
pthread_mutex_unlock(data->mutex);
pthread_mutex_lock(thread->mutex);
udev_list_entry_add(&thread->udev_enumerate->devices, udev_device_get_syspath(udev_device), NULL, 0);
pthread_mutex_unlock(thread->mutex);
udev_device_unref(udev_device);
return NULL;
}
static int udev_enumerate_filter_dots(const struct dirent *de)
static int filter_dot(const struct dirent *de)
{
if (strcmp(de->d_name, ".") == 0 ||
strcmp(de->d_name, "..") == 0) {
return 0;
return de->d_name[0] != '.';
}
static int udev_enumerate_add_devices(struct udev_enumerate *udev_enumerate, const char *path)
{
struct udev_enumerate_thread *thread;
pthread_mutex_t mutex;
struct dirent **de;
int cnt, i;
cnt = scandir(path, &de, filter_dot, NULL);
if (cnt == -1) {
return -1;
}
return 1;
thread = calloc(cnt, sizeof(struct udev_enumerate_thread));
if (!thread) {
for (i = 0; i < cnt; i++) {
free(de[i]);
}
free(de);
return -1;
}
pthread_mutex_init(&mutex, NULL);
for (i = 0; i < cnt; i++) {
thread[i].mutex = &mutex;
thread[i].udev_enumerate = udev_enumerate;
snprintf(thread[i].path, sizeof(thread[i].path), "%s/%s", path, de[i]->d_name);
pthread_create(&thread[i].thread, NULL, udev_enumerate_add_device, &thread[i]);
}
for (i = 0; i < cnt; i++) {
pthread_join(thread[i].thread, NULL);
}
for (i = 0; i < cnt; i++) {
free(de[i]);
}
free(de);
free(thread);
pthread_mutex_destroy(&mutex);
return 0;
}
int udev_enumerate_scan_devices(struct udev_enumerate *udev_enumerate)
{
const char *path[] = { "/sys/dev/block", "/sys/dev/char", NULL };
struct udev_enumerate_thread *data;
pthread_mutex_t mutex;
struct dirent **de;
int cnt, i, u;
int i;
pthread_mutex_init(&mutex, NULL);
for (i = 0; path[i]; i++) {
cnt = scandir(path[i], &de, udev_enumerate_filter_dots, NULL);
if (cnt == -1) {
continue;
}
data = calloc(cnt, sizeof(struct udev_enumerate_thread));
if (!data) {
for (u = 0; u < cnt; u++) {
free(de[u]);
}
free(de);
continue;
}
// TODO do we really need structure for every thread ?
for (u = 0; u < cnt; u++) {
data[u].path = path[i];
data[u].name = de[u]->d_name;
data[u].mutex = &mutex;
data[u].udev_enumerate = udev_enumerate;
pthread_create(&data[u].thread, NULL, udev_enumerate_add_device, &data[u]);
}
for (u = 0; u < cnt; u++) {
pthread_join(data[u].thread, NULL);
}
for (u = 0; u < cnt; u++) {
free(de[u]);
}
free(de);
free(data);
if (!udev_enumerate) {
return -1;
}
for (i = 0; path[i]; i++) {
if (udev_enumerate_add_devices(udev_enumerate, path[i]) == -1) {
return -1;
}
}
pthread_mutex_destroy(&mutex);
return 0;
}

View File

@@ -20,48 +20,61 @@ void udev_list_entry_free(struct udev_list_entry *list_entry)
void udev_list_entry_free_all(struct udev_list_entry *list_entry)
{
struct udev_list_entry *tmp, *tmp2;
struct udev_list_entry *list_entry2 = list_entry->next;
tmp = list_entry->next;
while (tmp) {
tmp2 = tmp;
tmp = tmp->next;
udev_list_entry_free(tmp2);
while (list_entry2) {
list_entry = list_entry2;
list_entry2 = list_entry->next;
udev_list_entry_free(list_entry);
}
}
struct udev_list_entry *udev_list_entry_add(struct udev_list_entry *list_entry, const char *name, const char *value, int uniq)
{
struct udev_list_entry *new, *old;
struct udev_list_entry *list_entry2;
if (uniq) {
old = udev_list_entry_get_by_name(list_entry, name);
list_entry2 = udev_list_entry_get_by_name(list_entry, name);
if (old) {
if (old->value && strcmp(old->value, value) == 0) {
return old;
if (list_entry2 && value) {
if (list_entry2->value && strcmp(list_entry2->value, value) == 0) {
return list_entry2;
}
free(old->value);
old->value = value ? strdup(value) : NULL;
return old;
free(list_entry2->value);
list_entry2->value = strdup(value);
if (!list_entry2->value) {
return NULL;
}
return list_entry2;
}
}
new = calloc(1, sizeof(struct udev_list_entry));
list_entry2 = calloc(1, sizeof(struct udev_list_entry));
if (!new) {
if (!list_entry2) {
return NULL;
}
new->value = value ? strdup(value) : NULL;
new->name = strdup(name);
list_entry2->name = strdup(name);
new->next = list_entry->next;
list_entry->next = new;
if (!list_entry2->name) {
return NULL;
}
return new;
if (value) {
list_entry2->value = strdup(value);
if (!list_entry2->value) {
return NULL;
}
}
list_entry2->next = list_entry->next;
list_entry->next = list_entry2;
return list_entry2;
}
struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_entry)
@@ -71,21 +84,16 @@ struct udev_list_entry *udev_list_entry_get_next(struct udev_list_entry *list_en
struct udev_list_entry *udev_list_entry_get_by_name(struct udev_list_entry *list_entry, const char *name)
{
struct udev_list_entry *tmp;
if (!list_entry || !name) {
return NULL;
}
tmp = list_entry;
while (tmp) {
if (tmp->name && strcmp(tmp->name, name) == 0) {
return tmp;
do {
if (list_entry->name && strcmp(list_entry->name, name) == 0) {
return list_entry;
}
tmp = tmp->next;
}
while ((list_entry = list_entry->next));
return NULL;
}

View File

@@ -13,12 +13,12 @@
#include "udev.h"
#include "udev_list.h"
#define THREADS_MAX 5
#ifndef UDEV_MONITOR_DIR
#define UDEV_MONITOR_DIR "/tmp/.libudev-zero"
#endif
enum { THREADS_MAX = 5 };
struct udev_monitor {
struct udev_list_entry subsystem_match;
struct udev_list_entry devtype_match;
@@ -132,7 +132,7 @@ static void *udev_monitor_handle_event(void *ptr)
for (i = 0; i < len; i += sizeof(struct inotify_event) + event->len) {
event = (struct inotify_event *)&data[i];
// TODO user deleted directory, what should we do ?
// TODO directory is removed
if (event->mask & IN_IGNORED) {
break;
}