From a390e6f764087b74b1567e1daf1f1c41eae42c74 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Thu, 16 Oct 2008 20:23:56 +0200 Subject: [PATCH] delete name_list, move common file functions --- udev/udev-util-file.c | 95 ------------------------ udev/udev-util.c | 163 +++++++++++++++++++++--------------------- udev/udev.h | 15 +--- 3 files changed, 85 insertions(+), 188 deletions(-) diff --git a/udev/udev-util-file.c b/udev/udev-util-file.c index 84ff09e0..29c0d2c1 100644 --- a/udev/udev-util-file.c +++ b/udev/udev-util-file.c @@ -29,101 +29,6 @@ #include "udev.h" -int create_path(struct udev *udev, const char *path) -{ - char p[UTIL_PATH_SIZE]; - char *pos; - struct stat stats; - int ret; - - util_strlcpy(p, path, sizeof(p)); - pos = strrchr(p, '/'); - if (pos == p || pos == NULL) - return 0; - - while (pos[-1] == '/') - pos--; - pos[0] = '\0'; - - dbg(udev, "stat '%s'\n", p); - if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) - return 0; - - if (create_path(udev, p) != 0) - return -1; - - dbg(udev, "mkdir '%s'\n", p); - udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755); - ret = mkdir(p, 0755); - udev_selinux_resetfscreatecon(udev); - if (ret == 0) - return 0; - - if (errno == EEXIST) - if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) - return 0; - return -1; -} - -int delete_path(struct udev *udev, const char *path) -{ - char p[UTIL_PATH_SIZE]; - char *pos; - int retval; - - strcpy (p, path); - pos = strrchr(p, '/'); - if (pos == p || pos == NULL) - return 0; - - while (1) { - *pos = '\0'; - pos = strrchr(p, '/'); - - /* don't remove the last one */ - if ((pos == p) || (pos == NULL)) - break; - - /* remove if empty */ - retval = rmdir(p); - if (errno == ENOENT) - retval = 0; - if (retval) { - if (errno == ENOTEMPTY) - return 0; - err(udev, "rmdir(%s) failed: %m\n", p); - break; - } - dbg(udev, "removed '%s'\n", p); - } - return 0; -} - -/* Reset permissions on the device node, before unlinking it to make sure, - * that permisions of possible hard links will be removed too. - */ -int unlink_secure(struct udev *udev, const char *filename) -{ - int retval; - - retval = chown(filename, 0, 0); - if (retval) - err(udev, "chown(%s, 0, 0) failed: %m\n", filename); - - retval = chmod(filename, 0000); - if (retval) - err(udev, "chmod(%s, 0000) failed: %m\n", filename); - - retval = unlink(filename); - if (errno == ENOENT) - retval = 0; - - if (retval) - err(udev, "unlink(%s) failed: %m\n", filename); - - return retval; -} - int file_map(const char *filename, char **buf, size_t *bufsize) { struct stat stats; diff --git a/udev/udev-util.c b/udev/udev-util.c index e22e9b28..59c4d194 100644 --- a/udev/udev-util.c +++ b/udev/udev-util.c @@ -23,103 +23,104 @@ #include #include #include -#include -#include #include #include -#include -#include #include "udev.h" -struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort) +int create_path(struct udev *udev, const char *path) { - struct name_entry *name_loop; - struct name_entry *name_new; - - /* avoid duplicate entries */ - list_for_each_entry(name_loop, name_list, node) { - if (strcmp(name_loop->name, name) == 0) { - dbg(udev, "'%s' is already in the list\n", name); - return name_loop; - } - } + char p[UTIL_PATH_SIZE]; + char *pos; + struct stat stats; + int ret; + + util_strlcpy(p, path, sizeof(p)); + pos = strrchr(p, '/'); + if (pos == p || pos == NULL) + return 0; + + while (pos[-1] == '/') + pos--; + pos[0] = '\0'; + + dbg(udev, "stat '%s'\n", p); + if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) + return 0; + + if (create_path(udev, p) != 0) + return -1; + + dbg(udev, "mkdir '%s'\n", p); + udev_selinux_setfscreatecon(udev, p, S_IFDIR|0755); + ret = mkdir(p, 0755); + udev_selinux_resetfscreatecon(udev); + if (ret == 0) + return 0; + + if (errno == EEXIST) + if (stat(p, &stats) == 0 && (stats.st_mode & S_IFMT) == S_IFDIR) + return 0; + return -1; +} - if (sort) - list_for_each_entry(name_loop, name_list, node) { - if (strcmp(name_loop->name, name) > 0) - break; +int delete_path(struct udev *udev, const char *path) +{ + char p[UTIL_PATH_SIZE]; + char *pos; + int retval; + + strcpy (p, path); + pos = strrchr(p, '/'); + if (pos == p || pos == NULL) + return 0; + + while (1) { + *pos = '\0'; + pos = strrchr(p, '/'); + + /* don't remove the last one */ + if ((pos == p) || (pos == NULL)) + break; + + /* remove if empty */ + retval = rmdir(p); + if (errno == ENOENT) + retval = 0; + if (retval) { + if (errno == ENOTEMPTY) + return 0; + err(udev, "rmdir(%s) failed: %m\n", p); + break; } - - name_new = malloc(sizeof(struct name_entry)); - if (name_new == NULL) - return NULL; - memset(name_new, 0x00, sizeof(struct name_entry)); - util_strlcpy(name_new->name, name, sizeof(name_new->name)); - dbg(udev, "adding '%s'\n", name_new->name); - list_add_tail(&name_new->node, &name_loop->node); - - return name_new; + dbg(udev, "removed '%s'\n", p); + } + return 0; } -struct name_entry *name_list_key_add(struct udev *udev, struct list_head *name_list, const char *key, const char *value) +/* Reset permissions on the device node, before unlinking it to make sure, + * that permisions of possible hard links will be removed too. + */ +int unlink_secure(struct udev *udev, const char *filename) { - struct name_entry *name_loop; - struct name_entry *name_new; - size_t keylen = strlen(key); - - list_for_each_entry(name_loop, name_list, node) { - if (strncmp(name_loop->name, key, keylen) != 0) - continue; - if (name_loop->name[keylen] != '=') - continue; - dbg(udev, "key already present '%s', replace it\n", name_loop->name); - snprintf(name_loop->name, sizeof(name_loop->name), "%s=%s", key, value); - name_loop->name[sizeof(name_loop->name)-1] = '\0'; - return name_loop; - } + int retval; - name_new = malloc(sizeof(struct name_entry)); - if (name_new == NULL) - return NULL; - memset(name_new, 0x00, sizeof(struct name_entry)); - snprintf(name_new->name, sizeof(name_new->name), "%s=%s", key, value); - name_new->name[sizeof(name_new->name)-1] = '\0'; - dbg(udev, "adding '%s'\n", name_new->name); - list_add_tail(&name_new->node, &name_loop->node); + retval = chown(filename, 0, 0); + if (retval) + err(udev, "chown(%s, 0, 0) failed: %m\n", filename); - return name_new; -} + retval = chmod(filename, 0000); + if (retval) + err(udev, "chmod(%s, 0000) failed: %m\n", filename); -int name_list_key_remove(struct udev *udev, struct list_head *name_list, const char *key) -{ - struct name_entry *name_loop; - struct name_entry *name_tmp; - size_t keylen = strlen(key); - int retval = 0; - - list_for_each_entry_safe(name_loop, name_tmp, name_list, node) { - if (strncmp(name_loop->name, key, keylen) != 0) - continue; - if (name_loop->name[keylen] != '=') - continue; - list_del(&name_loop->node); - free(name_loop); - retval = 1; - break; - } - return retval; -} + retval = unlink(filename); + if (errno == ENOENT) + retval = 0; -void name_list_cleanup(struct udev *udev, struct list_head *name_list) -{ - struct name_entry *name_loop; - struct name_entry *name_tmp; + if (retval) + err(udev, "unlink(%s) failed: %m\n", filename); - list_for_each_entry_safe(name_loop, name_tmp, name_list, node) { - list_del(&name_loop->node); - free(name_loop); - } + return retval; } uid_t lookup_user(struct udev *udev, const char *user) diff --git a/udev/udev.h b/udev/udev.h index 9a808c39..e5c7cd19 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -102,22 +102,13 @@ extern int udev_node_remove(struct udev_device *dev, int test); extern void udev_node_update_old_links(struct udev_device *dev, struct udev_device *dev_old, int test); /* udev-util.c */ -struct name_entry { - struct list_head node; - char name[UTIL_PATH_SIZE]; - unsigned int ignore_error:1; -}; -extern struct name_entry *name_list_add(struct udev *udev, struct list_head *name_list, const char *name, int sort); -extern struct name_entry *name_list_key_add(struct udev *udev, struct list_head *name_list, const char *key, const char *value); -extern int name_list_key_remove(struct udev *udev, struct list_head *name_list, const char *key); -extern void name_list_cleanup(struct udev *udev, struct list_head *name_list); +extern int create_path(struct udev *udev, const char *path); +extern int delete_path(struct udev *udev, const char *path); +extern int unlink_secure(struct udev *udev, const char *filename); extern uid_t lookup_user(struct udev *udev, const char *user); extern gid_t lookup_group(struct udev *udev, const char *group); /* udev_utils_file.c */ -extern int create_path(struct udev *udev, const char *path); -extern int delete_path(struct udev *udev, const char *path); -extern int unlink_secure(struct udev *udev, const char *filename); extern int file_map(const char *filename, char **buf, size_t *bufsize); extern void file_unmap(void *buf, size_t bufsize); extern size_t buf_get_line(const char *buf, size_t buflen, size_t cur); -- 2.39.5