udev: use typedef for struct udev_rules

This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2018-12-14 10:24:40 +01:00
parent 1d79128121
commit 9a07157dd5
7 changed files with 39 additions and 38 deletions

View file

@ -74,7 +74,7 @@ static int cleanup_fake_filesystems(const char *runtime_dir) {
} }
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { 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; _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
FILE *f = NULL; FILE *f = NULL;

View file

@ -55,7 +55,7 @@ static int fake_filesystems(void) {
} }
static int run(int argc, char *argv[]) { static int run(int argc, char *argv[]) {
_cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
_cleanup_(udev_event_freep) struct udev_event *event = NULL; _cleanup_(udev_event_freep) struct udev_event *event = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *devpath, *devname, *action; const char *devpath, *devname, *action;

View file

@ -780,7 +780,7 @@ static void event_execute_rules_on_remove(
struct udev_event *event, struct udev_event *event,
usec_t timeout_usec, usec_t timeout_usec,
Hashmap *properties_list, Hashmap *properties_list,
struct udev_rules *rules) { UdevRules *rules) {
sd_device *dev = event->dev; sd_device *dev = event->dev;
int r; int r;
@ -809,7 +809,7 @@ static void event_execute_rules_on_remove(
int udev_event_execute_rules(struct udev_event *event, int udev_event_execute_rules(struct udev_event *event,
usec_t timeout_usec, usec_t timeout_usec,
Hashmap *properties_list, Hashmap *properties_list,
struct udev_rules *rules) { UdevRules *rules) {
sd_device *dev = event->dev; sd_device *dev = event->dev;
const char *subsystem, *action; const char *subsystem, *action;
int r; int r;

View file

@ -57,7 +57,7 @@ static const char* const rules_dirs[] = {
NULL NULL
}; };
struct udev_rules { struct UdevRules {
usec_t dirs_ts_usec; usec_t dirs_ts_usec;
ResolveNameTiming resolve_name_timing; ResolveNameTiming resolve_name_timing;
@ -78,11 +78,11 @@ struct udev_rules {
unsigned gids_max; 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; 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)); return strbuf_add_string(rules->strbuf, s, strlen(s));
} }
@ -216,7 +216,7 @@ struct token {
#define MAX_TK 64 #define MAX_TK 64
struct rule_tmp { struct rule_tmp {
struct udev_rules *rules; UdevRules *rules;
struct token rule; struct token rule;
struct token token[MAX_TK]; struct token token[MAX_TK];
unsigned token_cur; unsigned token_cur;
@ -318,7 +318,7 @@ static const char *token_str(enum token_type type) {
return token_strs[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 token_type type = token->type;
enum operation_type op = token->key.op; enum operation_type op = token->key.op;
enum string_glob_type glob = token->key.glob; 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; unsigned i;
log_debug("Dumping %u (%zu bytes) tokens, %zu (%zu bytes) strings", 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]); dump_token(rules, &rules->tokens[i]);
} }
#else #else
static inline void dump_token(struct udev_rules *rules, struct token *token) {} static inline void dump_token(UdevRules *rules, struct token *token) {}
static inline void dump_rules(struct udev_rules *rules) {} static inline void dump_rules(UdevRules *rules) {}
#endif /* ENABLE_DEBUG_UDEV */ #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 */ /* grow buffer if needed */
if (rules->token_cur+1 >= rules->token_max) { if (rules->token_cur+1 >= rules->token_max) {
struct token *tokens; 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); 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; unsigned i;
uid_t uid = 0; uid_t uid = 0;
unsigned off; unsigned off;
@ -517,7 +517,7 @@ static uid_t add_uid(struct udev_rules *rules, const char *owner) {
return uid; 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; unsigned i;
gid_t gid = 0; gid_t gid = 0;
unsigned off; unsigned off;
@ -970,7 +970,7 @@ static void rule_add_key(struct rule_tmp *rule_tmp, enum token_type type,
rule_tmp->token_cur++; 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 i;
unsigned start = 0; unsigned start = 0;
unsigned end = rule_tmp->token_cur; 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_RULE_DEBUG(fmt, ...) LOG_RULE_FULL(LOG_DEBUG, fmt, ##__VA_ARGS__)
#define LOG_AND_RETURN(fmt, ...) { LOG_RULE_ERROR(fmt, __VA_ARGS__); return; } #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) { const char *filename, unsigned filename_off, unsigned lineno) {
char *linepos; char *linepos;
const char *attr; 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"); 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; _cleanup_fclose_ FILE *f = NULL;
unsigned first_token; unsigned first_token;
unsigned filename_off; unsigned filename_off;
@ -1512,19 +1512,19 @@ static int parse_file(struct udev_rules *rules, const char *filename) {
return 0; return 0;
} }
int udev_rules_new(struct udev_rules **ret_rules, ResolveNameTiming resolve_name_timing) { int udev_rules_new(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing) {
_cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
_cleanup_strv_free_ char **files = NULL; _cleanup_strv_free_ char **files = NULL;
char **f; char **f;
int r; int r;
assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX); assert(resolve_name_timing >= 0 && resolve_name_timing < _RESOLVE_NAME_TIMING_MAX);
rules = new(struct udev_rules, 1); rules = new(UdevRules, 1);
if (!rules) if (!rules)
return -ENOMEM; return -ENOMEM;
*rules = (struct udev_rules) { *rules = (UdevRules) {
.resolve_name_timing = resolve_name_timing, .resolve_name_timing = resolve_name_timing,
}; };
@ -1578,7 +1578,7 @@ int udev_rules_new(struct udev_rules **ret_rules, ResolveNameTiming resolve_name
return 0; return 0;
} }
struct udev_rules *udev_rules_free(struct udev_rules *rules) { UdevRules *udev_rules_free(UdevRules *rules) {
if (!rules) if (!rules)
return NULL; return NULL;
free(rules->tokens); free(rules->tokens);
@ -1588,14 +1588,14 @@ struct udev_rules *udev_rules_free(struct udev_rules *rules) {
return mfree(rules); return mfree(rules);
} }
bool udev_rules_check_timestamp(struct udev_rules *rules) { bool udev_rules_check_timestamp(UdevRules *rules) {
if (!rules) if (!rules)
return false; return false;
return paths_check_timestamp(rules_dirs, &rules->dirs_ts_usec, true); 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 *key_value = rules_str(rules, token->key.value_off);
char *pos; char *pos;
bool match = false; bool match = false;
@ -1668,7 +1668,7 @@ static int match_key(struct udev_rules *rules, struct token *token, const char *
return -1; 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, struct udev_event *event, struct token *cur) {
char nbuf[UTIL_NAME_SIZE], vbuf[UTIL_NAME_SIZE]; char nbuf[UTIL_NAME_SIZE], vbuf[UTIL_NAME_SIZE];
const char *name, *value; const char *name, *value;
size_t len; size_t len;
@ -1720,7 +1720,7 @@ enum escape_type {
}; };
int udev_rules_apply_to_event( int udev_rules_apply_to_event(
struct udev_rules *rules, UdevRules *rules,
struct udev_event *event, struct udev_event *event,
usec_t timeout_usec, usec_t timeout_usec,
Hashmap *properties_list) { Hashmap *properties_list) {
@ -2452,7 +2452,7 @@ int udev_rules_apply_to_event(
return 0; 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 *cur;
struct token *rule; struct token *rule;
uid_t uid = 0; uid_t uid = 0;

View file

@ -46,15 +46,16 @@ struct udev_event {
}; };
/* udev-rules.c */ /* udev-rules.c */
struct udev_rules; typedef struct UdevRules UdevRules;
int udev_rules_new(struct udev_rules **ret_rules, 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_new(UdevRules **ret_rules, ResolveNameTiming resolve_name_timing);
int udev_rules_apply_to_event(struct udev_rules *rules, struct udev_event *event, UdevRules *udev_rules_free(UdevRules *rules);
bool udev_rules_check_timestamp(UdevRules *rules);
int udev_rules_apply_to_event(UdevRules *rules, struct udev_event *event,
usec_t timeout_usec, usec_t timeout_usec,
Hashmap *properties_list); 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) { static inline usec_t udev_warn_timeout(usec_t timeout_usec) {
return DIV_ROUND_UP(timeout_usec, 3); return DIV_ROUND_UP(timeout_usec, 3);
@ -73,9 +74,9 @@ int udev_event_spawn(struct udev_event *event,
int udev_event_execute_rules(struct udev_event *event, int udev_event_execute_rules(struct udev_event *event,
usec_t timeout_usec, usec_t timeout_usec,
Hashmap *properties_list, Hashmap *properties_list,
struct udev_rules *rules); UdevRules *rules);
void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec); void udev_event_execute_run(struct udev_event *event, usec_t timeout_usec);
/* Cleanup functions */ /* Cleanup functions */
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_free); DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_event*, udev_event_free);
DEFINE_TRIVIAL_CLEANUP_FUNC(struct udev_rules*, udev_rules_free); DEFINE_TRIVIAL_CLEANUP_FUNC(UdevRules*, udev_rules_free);

View file

@ -86,7 +86,7 @@ static int parse_argv(int argc, char *argv[]) {
} }
int test_main(int argc, char *argv[], void *userdata) { int test_main(int argc, char *argv[], void *userdata) {
_cleanup_(udev_rules_freep) struct udev_rules *rules = NULL; _cleanup_(udev_rules_freep) UdevRules *rules = NULL;
_cleanup_(udev_event_freep) struct udev_event *event = NULL; _cleanup_(udev_event_freep) struct udev_event *event = NULL;
_cleanup_(sd_device_unrefp) sd_device *dev = NULL; _cleanup_(sd_device_unrefp) sd_device *dev = NULL;
const char *cmd, *key, *value; const char *cmd, *key, *value;

View file

@ -81,7 +81,7 @@ typedef struct Manager {
const char *cgroup; const char *cgroup;
pid_t pid; /* the process that originally allocated the manager object */ pid_t pid; /* the process that originally allocated the manager object */
struct udev_rules *rules; UdevRules *rules;
Hashmap *properties; Hashmap *properties;
sd_netlink *rtnl; sd_netlink *rtnl;