From af4b05d4917fdfa55eff3d8d53a830464d8162a1 Mon Sep 17 00:00:00 2001 From: "kay.sievers@vrfy.org" Date: Fri, 12 Nov 2004 06:20:22 +0100 Subject: [PATCH] [PATCH] make the udev object available to more processing stages Remove the overwriting of main_argv[] hack and use the values from the udev object. Pass the udev object to call_foreach_file(). In the udevstart case, export SUBSYSTEM and UDEVSTART to the environment. --- dev_d.c | 20 ++++++++------------ namedev.c | 6 +++--- namedev_parse.c | 16 ++++++++-------- udev.c | 9 --------- udev.h | 2 -- udev_lib.c | 12 +++++++----- udev_lib.h | 5 ++++- udevinfo.c | 16 +++++----------- udevstart.c | 15 +++++++-------- udevtest.c | 7 ------- 10 files changed, 42 insertions(+), 66 deletions(-) diff --git a/dev_d.c b/dev_d.c index 6e35f55c..1108c8ce 100644 --- a/dev_d.c +++ b/dev_d.c @@ -35,13 +35,13 @@ #define DEVD_DIR "/etc/dev.d/" #define DEVD_SUFFIX ".dev" -static int run_program(char *name) +static int run_program(const char *filename, void *data) { pid_t pid; int fd; - char *argv[3]; + struct udevice *udev = data; - dbg("running %s", name); + dbg("running %s", filename); pid = fork(); switch (pid) { @@ -55,11 +55,7 @@ static int run_program(char *name) } close(fd); - argv[0] = name; - argv[1] = main_argv[1]; - argv[2] = NULL; - - execv(name, argv); + execl(filename, filename, udev->subsystem, NULL); dbg("exec of child failed"); _exit(1); case -1: @@ -105,7 +101,7 @@ void dev_d_execute(struct udevice *udev) temp[0] = '\0'; strcpy(dirname, DEVD_DIR); strfieldcat(dirname, devname); - call_foreach_file(run_program, dirname, DEVD_SUFFIX); + call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev); temp[0] = '/'; ++temp; @@ -114,12 +110,12 @@ void dev_d_execute(struct udevice *udev) strcpy(dirname, DEVD_DIR); strfieldcat(dirname, udev->name); - call_foreach_file(run_program, dirname, DEVD_SUFFIX); + call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev); strcpy(dirname, DEVD_DIR); strfieldcat(dirname, udev->subsystem); - call_foreach_file(run_program, dirname, DEVD_SUFFIX); + call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev); strcpy(dirname, DEVD_DIR "default"); - call_foreach_file(run_program, dirname, DEVD_SUFFIX); + call_foreach_file(run_program, dirname, DEVD_SUFFIX, udev); } diff --git a/namedev.c b/namedev.c index d63e36e1..268a3d5b 100644 --- a/namedev.c +++ b/namedev.c @@ -358,7 +358,7 @@ static void fix_kernel_name(struct udevice *udev) } } -static int execute_program(const char *path, char *value, int len) +static int execute_program(struct udevice *udev, const char *path, char *value, int len) { int retval; int count; @@ -391,7 +391,7 @@ static int execute_program(const char *path, char *value, int len) dbg("execute '%s' with parsed arguments", arg); } else { argv[0] = arg; - argv[1] = main_argv[1]; + argv[1] = udev->subsystem; argv[2] = NULL; dbg("execute '%s' with subsystem '%s' argument", arg, argv[1]); } @@ -655,7 +655,7 @@ static int match_rule(struct config_device *dev, struct sysfs_class_device *clas dbg("check " FIELD_PROGRAM); strfieldcpy(program, dev->program); apply_format(udev, program, sizeof(program), class_dev, sysfs_device); - if (execute_program(program, udev->program_result, NAME_SIZE) != 0) { + if (execute_program(udev, program, udev->program_result, NAME_SIZE) != 0) { dbg(FIELD_PROGRAM " returned nonzero"); goto try_parent; } else { diff --git a/namedev_parse.c b/namedev_parse.c index 22e3523f..73828a83 100644 --- a/namedev_parse.c +++ b/namedev_parse.c @@ -142,7 +142,7 @@ static char *get_key_attribute(char *str) return NULL; } -static int namedev_parse_rules(char *filename) +static int namedev_parse_rules(const char *filename, void *data) { char line[LINE_SIZE]; char *bufline; @@ -343,7 +343,7 @@ error: return retval; } -static int namedev_parse_permissions(char *filename) +static int namedev_parse_permissions(const char *filename, void *data) { char line[LINE_SIZE]; char *bufline; @@ -447,10 +447,10 @@ int namedev_init_rules(void) stat(udev_rules_filename, &stats); if ((stats.st_mode & S_IFMT) != S_IFDIR) - return namedev_parse_rules(udev_rules_filename); + return namedev_parse_rules(udev_rules_filename, NULL); else - return call_foreach_file(namedev_parse_rules, - udev_rules_filename, RULEFILE_SUFFIX); + return call_foreach_file(namedev_parse_rules, udev_rules_filename, + RULEFILE_SUFFIX, NULL); } int namedev_init_permissions(void) @@ -459,8 +459,8 @@ int namedev_init_permissions(void) stat(udev_permissions_filename, &stats); if ((stats.st_mode & S_IFMT) != S_IFDIR) - return namedev_parse_permissions(udev_permissions_filename); + return namedev_parse_permissions(udev_permissions_filename, NULL); else - return call_foreach_file(namedev_parse_permissions, - udev_permissions_filename, PERMFILE_SUFFIX); + return call_foreach_file(namedev_parse_permissions, udev_permissions_filename, + PERMFILE_SUFFIX, NULL); } diff --git a/udev.c b/udev.c index 480a1cdc..300e85ce 100644 --- a/udev.c +++ b/udev.c @@ -39,10 +39,6 @@ #include "udevdb.h" -/* global variables */ -char **main_argv; -char **main_envp; - #ifdef LOG unsigned char logname[LOGNAME_SIZE]; void log_message(int level, const char *format, ...) @@ -83,12 +79,7 @@ int main(int argc, char *argv[], char *envp[]) } act_type; dbg("version %s", UDEV_VERSION); - - main_argv = argv; - main_envp = envp; - logging_init("udev"); - udev_init_config(); if (strstr(argv[0], "udevstart") || (argv[1] != NULL && strstr(argv[1], "udevstart"))) { diff --git a/udev.h b/udev.h index 3f9f2c86..d031b969 100644 --- a/udev.h +++ b/udev.h @@ -73,8 +73,6 @@ extern int udev_start(void); extern int parse_get_pair(char **orig_string, char **left, char **right); extern void dev_d_execute(struct udevice *udev); -extern char **main_argv; -extern char **main_envp; extern char sysfs_path[SYSFS_PATH_MAX]; extern char udev_root[PATH_MAX]; extern char udev_db_path[PATH_MAX+NAME_MAX]; diff --git a/udev_lib.c b/udev_lib.c index 84daf9fd..012d60b0 100644 --- a/udev_lib.c +++ b/udev_lib.c @@ -178,12 +178,12 @@ static int file_list_insert(char *filename, struct list_head *file_list) } /* calls function for every file found in specified directory */ -int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix) +int call_foreach_file(file_fnct_t fnct, const char *dirname, + const char *suffix, void *data) { struct dirent *ent; DIR *dir; char *ext; - char file[NAME_SIZE]; struct files *loop_file; struct files *tmp_file; LIST_HEAD(file_list); @@ -217,10 +217,12 @@ int call_foreach_file(int fnct(char *f) , char *dirname, char *suffix) /* call function for every file in the list */ list_for_each_entry_safe(loop_file, tmp_file, &file_list, list) { - snprintf(file, NAME_SIZE-1, "%s/%s", dirname, loop_file->name); - file[NAME_SIZE-1] = '\0'; + char filename[NAME_SIZE]; - fnct(file); + snprintf(filename, NAME_SIZE-1, "%s/%s", dirname, loop_file->name); + filename[NAME_SIZE-1] = '\0'; + + fnct(filename, data); list_del(&loop_file->list); free(loop_file); diff --git a/udev_lib.h b/udev_lib.h index 3777a7ca..2bc9ba82 100644 --- a/udev_lib.h +++ b/udev_lib.h @@ -84,6 +84,9 @@ extern int file_map(const char *filename, char **buf, size_t *bufsize); extern void file_unmap(char *buf, size_t bufsize); extern size_t buf_get_line(char *buf, size_t buflen, size_t cur); extern void no_trailing_slash(char *path); -extern int call_foreach_file(int fnct(char *f) , char *filename, char *extension); + +typedef int (*file_fnct_t)(const char *filename, void *data); +extern int call_foreach_file(file_fnct_t fnct, const char *dirname, + const char *suffix, void *data); #endif diff --git a/udevinfo.c b/udevinfo.c index 290a09e8..11482727 100644 --- a/udevinfo.c +++ b/udevinfo.c @@ -36,10 +36,7 @@ #include "udevdb.h" -# define SYSFS_VALUE_MAX 200 - -char **main_argv; -int main_argc; +#define SYSFS_VALUE_SIZE 256 #ifdef LOG unsigned char logname[LOGNAME_SIZE]; @@ -58,7 +55,7 @@ static int print_all_attributes(const char *path) struct dlist *attributes; struct sysfs_attribute *attr; struct sysfs_directory *sysfs_dir; - char value[SYSFS_VALUE_MAX]; + char value[SYSFS_VALUE_SIZE]; int len; int retval = 0; @@ -257,7 +254,7 @@ static int print_sysfs_devices(void) return 0; } -static int process_options(void) +static int process_options(int argc, char *argv[]) { static const char short_options[] = "adn:p:q:rsVh"; int option; @@ -274,7 +271,7 @@ static int process_options(void) /* get command line options */ while (1) { - option = getopt(main_argc, main_argv, short_options); + option = getopt(argc, argv, short_options); if (option == -1) break; @@ -479,15 +476,12 @@ int main(int argc, char *argv[], char *envp[]) { int rc = 0; - main_argv = argv; - main_argc = argc; - logging_init("udevinfo"); /* initialize our configuration */ udev_init_config(); - rc = process_options(); + rc = process_options(argc, argv); logging_close(); exit(rc); diff --git a/udevstart.c b/udevstart.c index e05680aa..0433e568 100644 --- a/udevstart.c +++ b/udevstart.c @@ -92,16 +92,10 @@ static int add_device(char *devpath, char *subsystem) struct udevice udev; char path[SYSFS_PATH_MAX]; struct sysfs_class_device *class_dev; - char *argv[3]; - /* fake argument vector and environment for callouts and dev.d/ */ - argv[0] = "udev"; - argv[1] = subsystem; - argv[2] = NULL; - - main_argv = argv; + /* set environment for callouts and dev.d/ */ setenv("DEVPATH", devpath, 1); - setenv("ACTION", "add", 1); + setenv("SUBSYSTEM", subsystem, 1); snprintf(path, SYSFS_PATH_MAX-1, "%s%s", sysfs_path, devpath); class_dev = sysfs_open_class_device_path(path); @@ -291,7 +285,12 @@ static void udev_scan_class(void) int udev_start(void) { + /* set environment for callouts and dev.d/ */ + setenv("ACTION", "add", 1); + setenv("UDEVSTART", "1", 1); + udev_scan_class(); udev_scan_block(); + return 0; } diff --git a/udevtest.c b/udevtest.c index e67af0df..14c51158 100644 --- a/udevtest.c +++ b/udevtest.c @@ -34,10 +34,6 @@ #include "logging.h" #include "namedev.h" -/* global variables */ -char **main_argv; -char **main_envp; - #ifdef LOG unsigned char logname[LOGNAME_SIZE]; @@ -62,9 +58,6 @@ int main(int argc, char *argv[], char *envp[]) char *subsystem = ""; struct udevice udev; - main_argv = argv; - main_envp = envp; - info("version %s", UDEV_VERSION); if (argv[1] == NULL) { -- 2.39.5