From eb6c7cd03635ffc28798734f0b87b9e21dae6f9e Mon Sep 17 00:00:00 2001 From: "trini@kernel.crashing.org" Date: Tue, 10 Aug 2004 00:50:21 -0700 Subject: [PATCH] [PATCH] Make udev/udevstart be one binary Hi, The following patch makes udev/udevstart be a common binary. First, doing this grows udev by a total of 1.8kB (ppc32, stripped) whereas udevstart by itself is 6.4kB. I know you mentioned being able to replace udevstart with a script, but at 1.8kB I don't think it'll be easy to beat this with size there. Next, the following are by-eye timings of before, after, and with devfs on a slow, but still usable embedded platform (config stripped down to more-or-less bare for ramdisk): -- Embedded Planet RPX LITE, 64Mhz MPC 823e -- devfs : 15.333s, 15.253s, 14.988s (15.191s avg) udev-pristine : 18.675s, 18.079s, 18.418s (18.390s avg) udev-multi : 14.587s, 14.747s, 14.868s (14.734s avg) The patch ends up being rather large to add this, as in doing so I ended up making all refs (that I hit..) to devpath/subsystem be marked as 'const'. Signed-off-by: Tom Rini Signed-off-by: Greg Kroah-Hartman --- Makefile | 14 ++++------- dev_d.c | 2 +- udev-add.c | 6 ++--- udev-remove.c | 2 +- udev.c | 66 ++++++++++++++++++++++++++++++++------------------- udev.h | 10 +++++--- udevstart.c | 47 +++++++----------------------------- 7 files changed, 67 insertions(+), 80 deletions(-) diff --git a/Makefile b/Makefile index f4f371d4..d4d3ac8b 100644 --- a/Makefile +++ b/Makefile @@ -172,7 +172,7 @@ endif CFLAGS += -I$(PWD)/libsysfs -all: $(ROOT) $(SENDER) $(DAEMON) $(INFO) $(TESTER) $(STARTER) +all: $(ROOT) $(SENDER) $(DAEMON) $(INFO) $(TESTER) @extras="$(EXTRAS)" ; for target in $$extras ; do \ echo $$target ; \ $(MAKE) prefix=$(prefix) \ @@ -261,8 +261,8 @@ $(DAEMON).o: $(GEN_HEADERS) $(SENDER).o: $(GEN_HEADERS) $(STARTER).o: $(GEN_HEADERS) -$(ROOT): $(ROOT).o $(OBJS) $(HEADERS) $(LIBC) $(GEN_MANPAGES) - $(LD) $(LDFLAGS) -o $@ $(CRT0) udev.o $(OBJS) $(LIB_OBJS) $(ARCH_LIB_OBJS) +$(ROOT): $(ROOT).o $(STARTER).o $(OBJS) $(HEADERS) $(LIBC) $(GEN_MANPAGES) + $(LD) $(LDFLAGS) -o $@ $(CRT0) udev.o udevstart.o $(OBJS) $(LIB_OBJS) $(ARCH_LIB_OBJS) $(STRIPCMD) $@ $(TESTER): $(TESTER).o $(OBJS) $(HEADERS) $(LIBC) @@ -281,10 +281,6 @@ $(SENDER): $(SENDER).o udevd.h $(LIBC) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevsend.o udev_lib.o $(LIB_OBJS) $(ARCH_LIB_OBJS) $(STRIPCMD) $@ -$(STARTER): $(STARTER).o $(HEADERS) $(LIBC) - $(LD) $(LDFLAGS) -o $@ $(CRT0) udevstart.o $(LIB_OBJS) $(ARCH_LIB_OBJS) - $(STRIPCMD) $@ - $(RULER): $(RULER).o $(OBJS) $(HEADERS) $(LIBC) $(LD) $(LDFLAGS) -o $@ $(CRT0) udevruler.o udev_lib.o udev_config.o udevdb.o $(SYSFS) $(TDB) $(LIB_OBJS) $(ARCH_LIB_OBJS) -lnewt $(STRIPCMD) $@ @@ -292,7 +288,7 @@ $(RULER): $(RULER).o $(OBJS) $(HEADERS) $(LIBC) clean: -find . \( -not -type d \) -and \( -name '*~' -o -name '*.[oas]' \) -type f -print \ | xargs rm -f - -rm -f core $(ROOT) $(GEN_HEADERS) $(GEN_CONFIGS) $(GEN_MANPAGES) $(INFO) $(DAEMON) $(SENDER) $(TESTER) $(STARTER) $(RULER) + -rm -f core $(ROOT) $(GEN_HEADERS) $(GEN_CONFIGS) $(GEN_MANPAGES) $(INFO) $(DAEMON) $(SENDER) $(TESTER) $(RULER) $(MAKE) -C klibc clean @extras="$(EXTRAS)" ; for target in $$extras ; do \ echo $$target ; \ @@ -392,7 +388,7 @@ install: install-initscript install-config install-man install-dev.d all $(INSTALL_PROGRAM) -D $(SENDER) $(DESTDIR)$(sbindir)/$(SENDER) $(INSTALL_PROGRAM) -D $(INFO) $(DESTDIR)$(usrbindir)/$(INFO) $(INSTALL_PROGRAM) -D $(TESTER) $(DESTDIR)$(usrbindir)/$(TESTER) - $(INSTALL_PROGRAM) -D $(STARTER) $(DESTDIR)$(sbindir)/$(STARTER) + ln -sf $(sbindir)/udev $(DESTDIR)$(sbindir)/$(STARTER) - ln -f -s $(sbindir)/$(SENDER) $(DESTDIR)$(hotplugdir)/$(ROOT).hotplug ifndef DESTDIR - killall udevd diff --git a/dev_d.c b/dev_d.c index 97c55264..1ad599ea 100644 --- a/dev_d.c +++ b/dev_d.c @@ -61,7 +61,7 @@ static int run_program(char *name) * subsystem/ * default/ */ -void dev_d_send(struct udevice *dev, char *subsystem, char *devpath) +void dev_d_send(struct udevice *dev, const char *subsystem, const char *devpath) { char dirname[256]; char env_devname[NAME_SIZE]; diff --git a/udev-add.c b/udev-add.c index 900e4795..1f693297 100644 --- a/udev-add.c +++ b/udev-add.c @@ -316,7 +316,7 @@ error: return -1; } -static struct sysfs_class_device *get_class_dev(char *device_name) +static struct sysfs_class_device *get_class_dev(const char *device_name) { char dev_path[SYSFS_PATH_MAX]; struct sysfs_class_device *class_dev = NULL; @@ -341,7 +341,7 @@ exit: * If it doesn't happen in about 10 seconds, give up. */ #define SECONDS_TO_WAIT_FOR_FILE 10 -static int sleep_for_file(char *path, char* file) +static int sleep_for_file(const char *path, char* file) { char filename[SYSFS_PATH_MAX + 6]; int loop = SECONDS_TO_WAIT_FOR_FILE; @@ -395,7 +395,7 @@ static int rename_net_if(struct udevice *dev, int fake) return retval; } -int udev_add_device(char *path, char *subsystem, int fake) +int udev_add_device(const char *path, const char *subsystem, int fake) { struct sysfs_class_device *class_dev; struct udevice dev; diff --git a/udev-remove.c b/udev-remove.c index 4f2cd479..7ad7c240 100644 --- a/udev-remove.c +++ b/udev-remove.c @@ -159,7 +159,7 @@ static int delete_node(struct udevice *dev) * something different from the kernel name. If we have, us it. If not, use * the default kernel name for lack of anything else to know to do. */ -int udev_remove_device(char *path, char *subsystem) +int udev_remove_device(const char *path, const char *subsystem) { struct udevice dev; char *temp; diff --git a/udev.c b/udev.c index cc6ce105..0ef4cf86 100644 --- a/udev.c +++ b/udev.c @@ -40,6 +40,9 @@ char **main_argv; char **main_envp; +/* local variables */ +static int is_udevstart; + #ifdef LOG unsigned char logname[LOGNAME_SIZE]; void log_message(int level, const char *format, ...) @@ -76,27 +79,13 @@ static char *subsystem_blacklist[] = { "" }; -static int udev_hotplug(void) +int __udev_hotplug(char *action, const char *devpath, const char *subsystem) { - char *action; - char *devpath; - char *subsystem; int retval = -EINVAL; int i; struct sigaction act; const int nofake = 0; - action = get_action(); - if (!action) { - dbg("no action?"); - goto exit; - } - - devpath = get_devpath(); - if (!devpath) { - dbg("no devpath?"); - goto exit; - } dbg("looking at '%s'", devpath); /* we only care about class devices and block stuff */ @@ -106,12 +95,6 @@ static int udev_hotplug(void) goto exit; } - /* skip blacklisted subsystems */ - subsystem = get_subsystem(main_argv[1]); - if (!subsystem) { - dbg("no subsystem?"); - goto exit; - } i = 0; while (subsystem_blacklist[i][0] != '\0') { if (strcmp(subsystem, subsystem_blacklist[i]) == 0) { @@ -136,7 +119,9 @@ static int udev_hotplug(void) sigaction(SIGTERM, &act, NULL); if (strcmp(action, "add") == 0) { - namedev_init(); + /* Already done. */ + if (!is_udevstart) + namedev_init(); retval = udev_add_device(devpath, subsystem, nofake); goto action_done; } @@ -156,17 +141,50 @@ exit: return retval; } +static int udev_hotplug(void) +{ + char *action; + char *devpath; + char *subsystem; + + action = get_action(); + if (!action) { + dbg("no action?"); + return -EINVAL; + } + + devpath = get_devpath(); + if (!devpath) { + dbg("no devpath?"); + return -EINVAL; + } + + /* skip blacklisted subsystems */ + subsystem = get_subsystem(main_argv[1]); + if (!subsystem) { + dbg("no subsystem?"); + return -EINVAL; + } + + return __udev_hotplug(action, devpath, subsystem); +} + int main(int argc, char *argv[], char *envp[]) { main_argv = argv; main_envp = envp; - init_logging("udev"); + if (strstr(argv[0], "udevstart")) + is_udevstart = 1; /* initialize our configuration */ udev_init_config(); dbg("version %s", UDEV_VERSION); - return udev_hotplug(); + if (is_udevstart) { + namedev_init(); + return udev_start(); + } else + return udev_hotplug(); } diff --git a/udev.h b/udev.h index da02e26b..310154f5 100644 --- a/udev.h +++ b/udev.h @@ -61,11 +61,15 @@ struct udevice { char kernel_name[NAME_SIZE]; }; -extern int udev_add_device(char *path, char *subsystem, int fake); -extern int udev_remove_device(char *path, char *subsystem); +extern int udev_add_device(const char *path, const char *subsystem, int fake); +extern int udev_remove_device(const char *path, const char *subsystem); extern void udev_init_config(void); +extern int udev_start(void); +extern int __udev_hotplug(char *action, const char *devpath, + const char *subsystem); extern int parse_get_pair(char **orig_string, char **left, char **right); -extern void dev_d_send(struct udevice *dev, char *subsystem, char *devpath); +extern void dev_d_send(struct udevice *dev, const char *subsystem, + const char *devpath); extern char **main_argv; extern char **main_envp; diff --git a/udevstart.c b/udevstart.c index 3ecfd4cb..d94c9501 100644 --- a/udevstart.c +++ b/udevstart.c @@ -36,25 +36,12 @@ #include "logging.h" #include "udev_lib.h" #include "list.h" - - -#ifdef LOG -unsigned char logname[LOGNAME_SIZE]; -void log_message(int level, const char *format, ...) -{ - va_list args; - - va_start(args, format); - vsyslog(level, format, args); - va_end(args); -} -#endif +#include "udev.h" #define MAX_PATHLEN 1024 #define SYSBLOCK "/sys/block" #define SYSCLASS "/sys/class" -#define UDEV_BIN "/sbin/udev" struct device { struct list_head list; @@ -88,28 +75,13 @@ static int device_list_insert(char *path, char *subsystem, struct list_head *dev static void udev_exec(const char *path, const char* subsystem) { - pid_t pid; - char action[] = "ACTION=add"; - char devpath[MAX_PATHLEN]; - char nosleep[] = "UDEV_NO_SLEEP=1"; - char *env[] = { action, devpath, nosleep, NULL }; - - strcpy(devpath, "DEVPATH="); - strfieldcat(devpath, path); - - pid = fork(); - switch (pid) { - case 0: - /* child */ - execle(UDEV_BIN, "udev", subsystem, NULL, env); - dbg("exec of child failed"); + /* Setup env variables. */ + setenv("UDEV_NO_SLEEP", "1", 1); + + /* Now call __udev_hotplug(). */ + if (__udev_hotplug("add", path, subsystem)) { + dbg("Calling of udev_hotplug failed"); exit(1); - break; - case -1: - dbg("fork of child failed"); - break; - default: - wait(NULL); } } @@ -267,12 +239,9 @@ static void udev_scan_class(void) exec_list(&device_list); } -int main(int argc, char *argv[], char *envp[]) +int udev_start(void) { - init_logging("udevstart"); - udev_scan_class(); udev_scan_block(); - return 0; } -- 2.39.5