diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/fuzz/fuzz-udev-rules.c | 6 | ||||
-rw-r--r-- | src/test/test-udev.c | 6 | ||||
-rw-r--r-- | src/udev/udev-event.c | 30 | ||||
-rw-r--r-- | src/udev/udev-rules.c | 76 | ||||
-rw-r--r-- | src/udev/udev.h | 36 | ||||
-rw-r--r-- | src/udev/udevadm-test.c | 11 | ||||
-rw-r--r-- | src/udev/udevd.c | 14 |
7 files changed, 89 insertions, 90 deletions
diff --git a/src/fuzz/fuzz-udev-rules.c b/src/fuzz/fuzz-udev-rules.c index dbdcda329..e894fa8d2 100644 --- a/src/fuzz/fuzz-udev-rules.c +++ b/src/fuzz/fuzz-udev-rules.c @@ -74,7 +74,7 @@ static int cleanup_fake_filesystems(const char *runtime_dir) { } int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { - _cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; + _cleanup_(udev_rules_freep) UdevRules *rules = NULL; _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; FILE *f = NULL; @@ -99,9 +99,9 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { if (size != 0) assert_se(fwrite(data, size, 1, f) == 1); assert_se(fclose(f) == 0); - rules = udev_rules_new(RESOLVE_NAME_EARLY); - assert_se(cleanup_fake_filesystems(runtime_dir) >= 0); + assert_se(udev_rules_new(&rules, RESOLVE_NAME_EARLY) == 0); + assert_se(cleanup_fake_filesystems(runtime_dir) >= 0); return 0; } diff --git a/src/test/test-udev.c b/src/test/test-udev.c index 281dbb51f..7a4622b87 100644 --- a/src/test/test-udev.c +++ b/src/test/test-udev.c @@ -55,8 +55,8 @@ static int fake_filesystems(void) { } static int run(int argc, char *argv[]) { - _cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; - _cleanup_(udev_event_freep) struct udev_event *event = NULL; + _cleanup_(udev_rules_freep) UdevRules *rules = NULL; + _cleanup_(udev_event_freep) UdevEvent *event = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL; const char *devpath, *devname, *action; int r; @@ -87,7 +87,7 @@ static int run(int argc, char *argv[]) { action = argv[1]; devpath = argv[2]; - rules = udev_rules_new(RESOLVE_NAME_EARLY); + assert_se(udev_rules_new(&rules, RESOLVE_NAME_EARLY) == 0); const char *syspath = strjoina("/sys", devpath); r = device_new_from_synthetic_event(&dev, syspath, action); diff --git a/src/udev/udev-event.c b/src/udev/udev-event.c index bb3e1b9f2..bc0a32f39 100644 --- a/src/udev/udev-event.c +++ b/src/udev/udev-event.c @@ -45,16 +45,16 @@ typedef struct Spawn { size_t result_len; } Spawn; -struct udev_event *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl) { - struct udev_event *event; +UdevEvent *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl) { + UdevEvent *event; assert(dev); - event = new(struct udev_event, 1); + event = new(UdevEvent, 1); if (!event) return NULL; - *event = (struct udev_event) { + *event = (UdevEvent) { .dev = sd_device_ref(dev), .birth_usec = now(CLOCK_MONOTONIC), .exec_delay_usec = exec_delay_usec, @@ -64,7 +64,7 @@ struct udev_event *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_net return event; } -struct udev_event *udev_event_free(struct udev_event *event) { +UdevEvent *udev_event_free(UdevEvent *event) { if (!event) return NULL; @@ -125,7 +125,7 @@ static const struct subst_map_entry map[] = { { .name = "sys", .fmt = 'S', .type = SUBST_SYS }, }; -static ssize_t subst_format_var(struct udev_event *event, +static ssize_t subst_format_var(UdevEvent *event, const struct subst_map_entry *entry, char *attr, char *dest, size_t l) { sd_device *parent, *dev = event->dev; @@ -312,7 +312,7 @@ static ssize_t subst_format_var(struct udev_event *event, return s - dest; } -ssize_t udev_event_apply_format(struct udev_event *event, +ssize_t udev_event_apply_format(UdevEvent *event, const char *src, char *dest, size_t size, bool replace_whitespace) { const char *from; @@ -593,7 +593,7 @@ static int spawn_wait(Spawn *spawn) { return ret; } -int udev_event_spawn(struct udev_event *event, +int udev_event_spawn(UdevEvent *event, usec_t timeout_usec, bool accept_failure, const char *cmd, @@ -683,7 +683,7 @@ int udev_event_spawn(struct udev_event *event, return r; } -static int rename_netif(struct udev_event *event) { +static int rename_netif(UdevEvent *event) { sd_device *dev = event->dev; const char *action, *oldname; char name[IFNAMSIZ]; @@ -726,7 +726,7 @@ static int rename_netif(struct udev_event *event) { return 1; } -static int update_devnode(struct udev_event *event) { +static int update_devnode(UdevEvent *event) { sd_device *dev = event->dev; const char *action; bool apply; @@ -777,10 +777,10 @@ static int update_devnode(struct udev_event *event) { } static void event_execute_rules_on_remove( - struct udev_event *event, + UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list, - struct udev_rules *rules) { + UdevRules *rules) { sd_device *dev = event->dev; int r; @@ -806,10 +806,10 @@ static void event_execute_rules_on_remove( (void) udev_node_remove(dev); } -int udev_event_execute_rules(struct udev_event *event, +int udev_event_execute_rules(UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list, - struct udev_rules *rules) { + UdevRules *rules) { sd_device *dev = event->dev; const char *subsystem, *action; int r; @@ -876,7 +876,7 @@ int udev_event_execute_rules(struct udev_event *event, return 0; } -void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec) { +void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec) { const char *cmd; void *val; Iterator i; diff --git a/src/udev/udev-rules.c b/src/udev/udev-rules.c index a9fa04dbf..53c68d254 100644 --- a/src/udev/udev-rules.c +++ b/src/udev/udev-rules.c @@ -57,7 +57,7 @@ static const char* const rules_dirs[] = { NULL }; -struct udev_rules { +struct UdevRules { usec_t dirs_ts_usec; ResolveNameTiming resolve_name_timing; @@ -78,11 +78,11 @@ struct udev_rules { unsigned gids_max; }; -static char *rules_str(struct udev_rules *rules, unsigned off) { +static char *rules_str(UdevRules *rules, unsigned off) { return rules->strbuf->buf + off; } -static unsigned rules_add_string(struct udev_rules *rules, const char *s) { +static unsigned rules_add_string(UdevRules *rules, const char *s) { return strbuf_add_string(rules->strbuf, s, strlen(s)); } @@ -216,7 +216,7 @@ struct token { #define MAX_TK 64 struct rule_tmp { - struct udev_rules *rules; + UdevRules *rules; struct token rule; struct token token[MAX_TK]; unsigned token_cur; @@ -318,7 +318,7 @@ static const char *token_str(enum token_type type) { return token_strs[type]; } -static void dump_token(struct udev_rules *rules, struct token *token) { +static void dump_token(UdevRules *rules, struct token *token) { enum token_type type = token->type; enum operation_type op = token->key.op; enum string_glob_type glob = token->key.glob; @@ -429,7 +429,7 @@ static void dump_token(struct udev_rules *rules, struct token *token) { } } -static void dump_rules(struct udev_rules *rules) { +static void dump_rules(UdevRules *rules) { unsigned i; log_debug("Dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings", @@ -441,11 +441,11 @@ static void dump_rules(struct udev_rules *rules) { dump_token(rules, &rules->tokens[i]); } #else -static inline void dump_token(struct udev_rules *rules, struct token *token) {} -static inline void dump_rules(struct udev_rules *rules) {} +static inline void dump_token(UdevRules *rules, struct token *token) {} +static inline void dump_rules(UdevRules *rules) {} #endif /* ENABLE_DEBUG_UDEV */ -static int add_token(struct udev_rules *rules, struct token *token) { +static int add_token(UdevRules *rules, struct token *token) { /* grow buffer if needed */ if (rules->token_cur+1 >= rules->token_max) { struct token *tokens; @@ -474,7 +474,7 @@ static void log_unknown_owner(sd_device *dev, int error, const char *entity, con log_device_error_errno(dev, error, "Failed to resolve %s '%s': %m", entity, owner); } -static uid_t add_uid(struct udev_rules *rules, const char *owner) { +static uid_t add_uid(UdevRules *rules, const char *owner) { unsigned i; uid_t uid = 0; unsigned off; @@ -517,7 +517,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) { return uid; } -static gid_t add_gid(struct udev_rules *rules, const char *group) { +static gid_t add_gid(UdevRules *rules, const char *group) { unsigned i; gid_t gid = 0; unsigned off; @@ -640,7 +640,7 @@ static int import_file_into_properties(sd_device *dev, const char *filename) { return 0; } -static int import_program_into_properties(struct udev_event *event, +static int import_program_into_properties(UdevEvent *event, usec_t timeout_usec, const char *program) { char result[UTIL_LINE_SIZE]; @@ -970,7 +970,7 @@ static void rule_add_key(struct rule_tmp *rule_tmp, enum token_type type, rule_tmp->token_cur++; } -static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) { +static int sort_token(UdevRules *rules, struct rule_tmp *rule_tmp) { unsigned i; unsigned start = 0; unsigned end = rule_tmp->token_cur; @@ -1010,7 +1010,7 @@ static int sort_token(struct udev_rules *rules, struct rule_tmp *rule_tmp) { #define LOG_RULE_DEBUG(fmt, ...) LOG_RULE_FULL(LOG_DEBUG, fmt, ##__VA_ARGS__) #define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; } -static void add_rule(struct udev_rules *rules, char *line, +static void add_rule(UdevRules *rules, char *line, const char *filename, unsigned filename_off, unsigned lineno) { char *linepos; const char *attr; @@ -1429,7 +1429,7 @@ static void add_rule(struct udev_rules *rules, char *line, LOG_RULE_ERROR("Failed to add rule token"); } -static int parse_file(struct udev_rules *rules, const char *filename) { +static int parse_file(UdevRules *rules, const char *filename) { _cleanup_fclose_ FILE *f = NULL; unsigned first_token; unsigned filename_off; @@ -1512,39 +1512,37 @@ static int parse_file(struct udev_rules *rules, const char *filename) { return 0; } -struct udev_rules *udev_rules_new(ResolveNameTiming resolve_name_timing) { - struct udev_rules *rules; - struct token end_token; - char **files, **f; +int udev_rules_new(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing) { + _cleanup_(udev_rules_freep) UdevRules *rules = NULL; + _cleanup_strv_free_ char **files = NULL; + char **f; int r; assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX); - rules = new(struct udev_rules, 1); + rules = new(UdevRules, 1); if (!rules) - return NULL; + return -ENOMEM; - *rules = (struct udev_rules) { + *rules = (UdevRules) { .resolve_name_timing = resolve_name_timing, }; /* init token array and string buffer */ rules->tokens = malloc_multiply(PREALLOC_TOKEN, sizeof(struct token)); if (!rules->tokens) - return udev_rules_free(rules); + return -ENOMEM; rules->token_max = PREALLOC_TOKEN; rules->strbuf = strbuf_new(); if (!rules->strbuf) - return udev_rules_free(rules); + return -ENOMEM; udev_rules_check_timestamp(rules); r = conf_files_list_strv(&files, ".rules", NULL, 0, rules_dirs); - if (r < 0) { - log_error_errno(r, "Failed to enumerate rules files: %m"); - return udev_rules_free(rules); - } + if (r < 0) + return log_error_errno(r, "Failed to enumerate rules files: %m"); /* * The offset value in the rules strct is limited; add all @@ -1556,10 +1554,7 @@ struct udev_rules *udev_rules_new(ResolveNameTiming resolve_name_timing) { STRV_FOREACH(f, files) parse_file(rules, *f); - strv_free(files); - - memzero(&end_token, sizeof(struct token)); - end_token.type = TK_END; + struct token end_token = { .type = TK_END }; add_token(rules, &end_token); log_debug("Rules contain %zu bytes tokens (%u * %zu bytes), %zu bytes strings", rules->token_max * sizeof(struct token), rules->token_max, sizeof(struct token), rules->strbuf->len); @@ -1579,10 +1574,11 @@ struct udev_rules *udev_rules_new(ResolveNameTiming resolve_name_timing) { rules->gids_max = 0; dump_rules(rules); - return rules; + *ret_rules = TAKE_PTR(rules); + return 0; } -struct udev_rules *udev_rules_free(struct udev_rules *rules) { +UdevRules *udev_rules_free(UdevRules *rules) { if (!rules) return NULL; free(rules->tokens); @@ -1592,14 +1588,14 @@ struct udev_rules *udev_rules_free(struct udev_rules *rules) { return mfree(rules); } -bool udev_rules_check_timestamp(struct udev_rules *rules) { +bool udev_rules_check_timestamp(UdevRules *rules) { if (!rules) return false; return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true); } -static int match_key(struct udev_rules *rules, struct token *token, const char *val) { +static int match_key(UdevRules *rules, struct token *token, const char *val) { char *key_value = rules_str(rules, token->key.value_off); char *pos; bool match = false; @@ -1672,7 +1668,7 @@ static int match_key(struct udev_rules *rules, struct token *token, const char * return -1; } -static int match_attr(struct udev_rules *rules, sd_device *dev, struct udev_event *event, struct token *cur) { +static int match_attr(UdevRules *rules, sd_device *dev, UdevEvent *event, struct token *cur) { char nbuf[UTIL_NAME_SIZE], vbuf[UTIL_NAME_SIZE]; const char *name, *value; size_t len; @@ -1724,8 +1720,8 @@ enum escape_type { }; int udev_rules_apply_to_event( - struct udev_rules *rules, - struct udev_event *event, + UdevRules *rules, + UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list) { sd_device *dev = event->dev; @@ -2456,7 +2452,7 @@ int udev_rules_apply_to_event( return 0; } -int udev_rules_apply_static_dev_perms(struct udev_rules *rules) { +int udev_rules_apply_static_dev_perms(UdevRules *rules) { struct token *cur; struct token *rule; uid_t uid = 0; diff --git a/src/udev/udev.h b/src/udev/udev.h index 27b335fd4..3bc69ff6c 100644 --- a/src/udev/udev.h +++ b/src/udev/udev.h @@ -16,7 +16,7 @@ #define READ_END 0 #define WRITE_END 1 -struct udev_event { +typedef struct UdevEvent { sd_device *dev; sd_device *dev_parent; sd_device *dev_db_clone; @@ -43,38 +43,40 @@ struct udev_event { bool name_final; bool devlink_final; bool run_final; -}; +} UdevEvent; /* udev-rules.c */ -struct udev_rules; -struct udev_rules *udev_rules_new(ResolveNameTiming resolve_name_timing); -struct udev_rules *udev_rules_free(struct udev_rules *rules); -bool udev_rules_check_timestamp(struct udev_rules *rules); -int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, +typedef struct UdevRules UdevRules; + +int udev_rules_new(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing); +UdevRules *udev_rules_free(UdevRules *rules); + +bool udev_rules_check_timestamp(UdevRules *rules); +int udev_rules_apply_to_event(UdevRules *rules, UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list); -int udev_rules_apply_static_dev_perms(struct udev_rules *rules); +int udev_rules_apply_static_dev_perms(UdevRules *rules); static inline usec_t udev_warn_timeout(usec_t timeout_usec) { return DIV_ROUND_UP(timeout_usec, 3); } /* udev-event.c */ -struct udev_event *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl); -struct udev_event *udev_event_free(struct udev_event *event); -ssize_t udev_event_apply_format(struct udev_event *event, +UdevEvent *udev_event_new(sd_device *dev, usec_t exec_delay_usec, sd_netlink *rtnl); +UdevEvent *udev_event_free(UdevEvent *event); +ssize_t udev_event_apply_format(UdevEvent *event, const char *src, char *dest, size_t size, bool replace_whitespace); -int udev_event_spawn(struct udev_event *event, +int udev_event_spawn(UdevEvent *event, usec_t timeout_usec, bool accept_failure, const char *cmd, char *result, size_t ressize); -int udev_event_execute_rules(struct udev_event *event, +int udev_event_execute_rules(UdevEvent *event, usec_t timeout_usec, Hashmap *properties_list, - struct udev_rules *rules); -void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec); + UdevRules *rules); +void udev_event_execute_run(UdevEvent *event, usec_t timeout_usec); /* Cleanup functions */ -DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_free); -DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_free); +DEFINE_TRIVIAL_CLEANUP_FUNC(UdevEvent*, udev_event_free); +DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRules*, udev_rules_free); diff --git a/src/udev/udevadm-test.c b/src/udev/udevadm-test.c index 005553cbb..f08aee47c 100644 --- a/src/udev/udevadm-test.c +++ b/src/udev/udevadm-test.c @@ -86,8 +86,8 @@ static int parse_argv(int argc, char *argv[]) { } int test_main(int argc, char *argv[], void *userdata) { - _cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; - _cleanup_(udev_event_freep) struct udev_event *event = NULL; + _cleanup_(udev_rules_freep) UdevRules *rules = NULL; + _cleanup_(udev_event_freep) UdevEvent *event = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL; const char *cmd, *key, *value; sigset_t mask, sigmask_orig; @@ -110,10 +110,9 @@ int test_main(int argc, char *argv[], void *userdata) { udev_builtin_init(); - rules = udev_rules_new(arg_resolve_name_timing); - if (!rules) { - log_error("Failed to read udev rules."); - r = -ENOMEM; + r = udev_rules_new(&rules, arg_resolve_name_timing); + if (r < 0) { + log_error_errno(r, "Failed to read udev rules: %m"); goto out; } diff --git a/src/udev/udevd.c b/src/udev/udevd.c index 024fa97ad..fb8724ea8 100644 --- a/src/udev/udevd.c +++ b/src/udev/udevd.c @@ -81,7 +81,7 @@ typedef struct Manager { const char *cgroup; pid_t pid; /* the process that originally allocated the manager object */ - struct udev_rules *rules; + UdevRules *rules; Hashmap *properties; sd_netlink *rtnl; @@ -394,7 +394,7 @@ static int worker_lock_block_device(sd_device *dev, int *ret_fd) { } static int worker_process_device(Manager *manager, sd_device *dev) { - _cleanup_(udev_event_freep) struct udev_event *udev_event = NULL; + _cleanup_(udev_event_freep) UdevEvent *udev_event = NULL; _cleanup_close_ int fd_lock = -1; const char *seqnum; int r; @@ -890,9 +890,11 @@ static void event_queue_start(Manager *manager) { udev_builtin_init(); if (!manager->rules) { - manager->rules = udev_rules_new(arg_resolve_name_timing); - if (!manager->rules) + r = udev_rules_new(&manager->rules, arg_resolve_name_timing); + if (r < 0) { + log_warning_errno(r, "Failed to read udev rules: %m"); return; + } } LIST_FOREACH(event, event, manager->events) { @@ -1608,9 +1610,9 @@ static int manager_new(Manager **ret, int fd_ctrl, int fd_uevent, const char *cg udev_builtin_init(); - manager->rules = udev_rules_new(arg_resolve_name_timing); + r = udev_rules_new(&manager->rules, arg_resolve_name_timing); if (!manager->rules) - return log_error_errno(SYNTHETIC_ERRNO(ENOMEM), "Failed to read udev rules"); + return log_error_errno(r, "Failed to read udev rules: %m"); manager->ctrl = udev_ctrl_new_from_fd(fd_ctrl); if (!manager->ctrl) |