From d7ddce186c8a0171eb98862b6f8ff72d64261b33 Mon Sep 17 00:00:00 2001 From: Kay Sievers Date: Sat, 18 Oct 2008 15:02:01 +0200 Subject: [PATCH] make struct udev_rules opaque --- udev/test-udev.c | 8 ++++---- udev/udev-rules.c | 37 +++++++++++++++++++++++-------------- udev/udev.h | 10 ++-------- udev/udevadm-test.c | 13 +++++++++---- udev/udevd.c | 21 +++++++++++++++------ 5 files changed, 53 insertions(+), 36 deletions(-) diff --git a/udev/test-udev.c b/udev/test-udev.c index 1caccce3..4f8a7e4f 100644 --- a/udev/test-udev.c +++ b/udev/test-udev.c @@ -46,7 +46,7 @@ int main(int argc, char *argv[]) struct udev *udev; struct udev_event *event; struct udev_device *dev; - struct udev_rules rules; + struct udev_rules *rules; char syspath[UTIL_PATH_SIZE]; const char *devpath; const char *action; @@ -81,7 +81,7 @@ int main(int argc, char *argv[]) goto exit; } - udev_rules_init(udev, &rules, 0); + rules = udev_rules_new(udev, 0); util_strlcpy(syspath, udev_get_sys_path(udev), sizeof(syspath)); util_strlcat(syspath, devpath, sizeof(syspath)); @@ -97,7 +97,7 @@ int main(int argc, char *argv[]) udev_device_set_action(dev, action); event = udev_event_new(dev); - err = udev_event_run(event, &rules); + err = udev_event_run(event, rules); /* rules may change/disable the timeout */ if (udev_device_get_event_timeout(dev) >= 0) @@ -109,7 +109,7 @@ int main(int argc, char *argv[]) udev_event_unref(event); udev_device_unref(dev); fail: - udev_rules_cleanup(&rules); + udev_rules_unref(rules); exit: selinux_exit(udev); udev_unref(udev); diff --git a/udev/udev-rules.c b/udev/udev-rules.c index 445eec42..6d7e3f4f 100644 --- a/udev/udev-rules.c +++ b/udev/udev-rules.c @@ -34,7 +34,17 @@ #include "udev.h" -#define PAIRS_MAX 5 +struct udev_rules { + struct udev *udev; + char *buf; + size_t bufsize; + int resolve_names; +}; + +struct udev_rules_iter { + struct udev_rules *rules; + size_t current; +}; enum key_operation { KEY_OP_UNSET, @@ -55,6 +65,7 @@ struct key_pair { size_t key_name_off; }; +#define PAIRS_MAX 5 struct key_pairs { int count; struct key_pair keys[PAIRS_MAX]; @@ -119,11 +130,6 @@ struct udev_rule { char buf[]; }; -struct udev_rules_iter { - struct udev_rules *rules; - size_t current; -}; - static void udev_rules_iter_init(struct udev_rules_iter *iter, struct udev_rules *rules) { dbg(rules->udev, "bufsize=%zi\n", rules->bufsize); @@ -1860,7 +1866,6 @@ static int get_key(struct udev_rules *rules, char **line, char **key, enum key_o /* move line to next key */ *line = temp; - return 0; } @@ -1932,7 +1937,6 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena char *attr; size_t padding; int physdev = 0; - int retval; memset(buf, 0x00, sizeof(buf)); rule = (struct udev_rule *) buf; @@ -1946,8 +1950,7 @@ static int add_to_rules(struct udev_rules *rules, char *line, const char *filena char *value; enum key_operation operation = KEY_OP_UNSET; - retval = get_key(rules, &linepos, &key, &operation, &value); - if (retval) + if (get_key(rules, &linepos, &key, &operation, &value) != 0) break; if (strcasecmp(key, "ACTION") == 0) { @@ -2462,14 +2465,17 @@ static int add_matching_files(struct udev *udev, struct udev_list_node *file_lis return 0; } -int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_names) +struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names) { + struct udev_rules *rules; struct stat statbuf; char filename[PATH_MAX]; struct udev_list_node file_list; struct udev_list_entry *file_loop, *file_tmp; - int retval = 0; + rules = malloc(sizeof(struct udev_rules)); + if (rules == NULL) + return rules; memset(rules, 0x00, sizeof(struct udev_rules)); rules->udev = udev; rules->resolve_names = resolve_names; @@ -2539,13 +2545,16 @@ int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_nam info(udev, "can not read '%s'\n", file_name); udev_list_entry_remove(file_loop); } - return retval; + return rules; } -void udev_rules_cleanup(struct udev_rules *rules) +void udev_rules_unref(struct udev_rules *rules) { + if (rules == NULL) + return; if (rules->buf) { free(rules->buf); rules->buf = NULL; } + free(rules); } diff --git a/udev/udev.h b/udev/udev.h index 0e8a8697..eb020cd3 100644 --- a/udev/udev.h +++ b/udev/udev.h @@ -92,14 +92,8 @@ extern void udev_event_unref(struct udev_event *event); extern int udev_event_run(struct udev_event *event, struct udev_rules *rules); /* udev-rules.c */ -struct udev_rules { - struct udev *udev; - char *buf; - size_t bufsize; - int resolve_names; -}; -extern int udev_rules_init(struct udev *udev, struct udev_rules *rules, int resolve_names); -extern void udev_rules_cleanup(struct udev_rules *rules); +extern struct udev_rules *udev_rules_new(struct udev *udev, int resolve_names); +extern void udev_rules_unref(struct udev_rules *rules); extern int udev_rules_get_name(struct udev_rules *rules, struct udev_event *event); extern int udev_rules_get_run(struct udev_rules *rules, struct udev_event *event); extern int udev_rules_run(struct udev_event *event); diff --git a/udev/udevadm-test.c b/udev/udevadm-test.c index 99f6c36d..c64e0456 100644 --- a/udev/udevadm-test.c +++ b/udev/udevadm-test.c @@ -38,7 +38,7 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) const char *syspath = NULL; struct udev_event *event; struct udev_device *dev; - struct udev_rules rules = {}; + struct udev_rules *rules = NULL; int err; int rc = 0; @@ -89,7 +89,12 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) "some values may be different, or not available at a simulation run.\n" "\n"); - udev_rules_init(udev, &rules, 0); + rules = udev_rules_new(udev, 0); + if (rules == NULL) { + fprintf(stderr, "error reading rules\n"); + rc = 1; + goto exit; + } /* add /sys if needed */ if (strncmp(syspath, udev_get_sys_path(udev), strlen(udev_get_sys_path(udev))) != 0) { @@ -116,7 +121,7 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) if (!force) event->test = 1; - err = udev_event_run(event, &rules); + err = udev_event_run(event, rules); if (udev_device_get_event_timeout(dev) >= 0) info(udev, "custom event timeout: %i\n", udev_device_get_event_timeout(dev)); @@ -135,6 +140,6 @@ int udevadm_test(struct udev *udev, int argc, char *argv[]) udev_event_unref(event); udev_device_unref(dev); exit: - udev_rules_cleanup(&rules); + udev_rules_unref(rules); return rc; } diff --git a/udev/udevd.c b/udev/udevd.c index 5388961c..918825ae 100644 --- a/udev/udevd.c +++ b/udev/udevd.c @@ -58,7 +58,7 @@ static void log_fn(struct udev *udev, int priority, } static int debug_trace; -static struct udev_rules rules; +static struct udev_rules *rules; static struct udev_ctrl *udev_ctrl; static struct udev_monitor *kernel_monitor; static int inotify_fd = -1; @@ -208,7 +208,7 @@ static void event_fork(struct udev_event *event) alarm(UDEV_EVENT_TIMEOUT); /* apply rules, create node, symlinks */ - err = udev_event_run(event, &rules); + err = udev_event_run(event, rules); /* rules may change/disable the timeout */ if (udev_device_get_event_timeout(event->dev) >= 0) @@ -748,9 +748,13 @@ int main(int argc, char *argv[]) goto exit; } + rules = udev_rules_new(udev, 1); + if (rules == NULL) { + err(udev, "error reading rules\n"); + goto exit; + } udev_list_init(&running_list); udev_list_init(&exec_list); - udev_rules_init(udev, &rules, 1); export_initial_seqnum(udev); if (daemonize) { @@ -934,9 +938,14 @@ int main(int argc, char *argv[]) /* rules changed, set by inotify or a HUP signal */ if (reload_config) { + struct udev_rules *rules_new; + reload_config = 0; - udev_rules_cleanup(&rules); - udev_rules_init(udev, &rules, 1); + rules_new = udev_rules_new(udev, 1); + if (rules_new != NULL) { + udev_rules_unref(rules); + rules = rules_new; + } } if (sigchilds_waiting) { @@ -953,7 +962,7 @@ int main(int argc, char *argv[]) rc = 0; exit: - udev_rules_cleanup(&rules); + udev_rules_unref(rules); if (signal_pipe[READ_END] >= 0) close(signal_pipe[READ_END]); -- 2.39.5