From 70f7ef7be385f782e57106df523f1c5e16fbdc47 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Tue, 13 Dec 2005 08:21:33 +0000 Subject: Nothing to see here. Move along. Not buying it, eh? I know I said new features before 1.1, but, well... (I was weak!) The config file and hotplug modes aren't implemented yet. Might take a stab at those tomorrow. (I _should_ go back to focusing on the bug triage list.) --- util-linux/Config.in | 30 ++++++++++++ util-linux/Makefile.in | 2 + util-linux/mdev.c | 121 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) create mode 100644 util-linux/mdev.c (limited to 'util-linux') diff --git a/util-linux/Config.in b/util-linux/Config.in index fe71dac2c..5ab54e038 100644 --- a/util-linux/Config.in +++ b/util-linux/Config.in @@ -245,6 +245,36 @@ config CONFIG_LOSETUP file or block device, and to query the status of a loop device. This version does not currently support enabling data encryption. +config CONFIG_MDEV + bool "mdev" + default n + help + mdev is a mini-udev implementation: call it with -s to populate + /dev from /sys, then "echo /sbin/mdev > /sys/kernel/hotplug" to + have it handle hotplug events afterwards. Device names are taken + from sysfs. + +config CONFIG_FEATURE_MDEV_CONFIG + bool " Support /etc/mdev.conf" + default n + depends on CONFIG_MDEV + help + The mdev config file contains lines that look like: + + hd[a-z][0-9]* 0:3 660 + + That's device name (with regex match), uid:gid, and permissions. + + Optionally, that can be followed (on the same line) by an asterisk + and a command line to run after creating the corresponding device(s), + ala: + + hdc root:cdrom 660 *ln -s hdc cdrom + + Config file parsing stops on the first matching line. If no config + entry is matched, devices are created with default 0:0 660. (Make + the last line match .* to override this.) + config CONFIG_MKSWAP bool "mkswap" default n diff --git a/util-linux/Makefile.in b/util-linux/Makefile.in index f6fdba96a..782870cdb 100644 --- a/util-linux/Makefile.in +++ b/util-linux/Makefile.in @@ -24,12 +24,14 @@ UTILLINUX-$(CONFIG_HWCLOCK) +=hwclock.o UTILLINUX-$(CONFIG_IPCRM) +=ipcrm.o UTILLINUX-$(CONFIG_IPCS) +=ipcs.o UTILLINUX-$(CONFIG_LOSETUP) +=losetup.o +UTILLINUX-$(CONFIG_MDEV) +=mdev.o UTILLINUX-$(CONFIG_MKFS_MINIX) +=mkfs_minix.o UTILLINUX-$(CONFIG_MKSWAP) +=mkswap.o UTILLINUX-$(CONFIG_MORE) +=more.o UTILLINUX-$(CONFIG_MOUNT) +=mount.o UTILLINUX-$(CONFIG_FEATURE_MOUNT_NFS) +=nfsmount.o UTILLINUX-$(CONFIG_PIVOT_ROOT) +=pivot_root.o +UTILLINUX-$(CONFIG_SWITCH_ROOT) +=switch_root.o UTILLINUX-$(CONFIG_RDATE) +=rdate.o UTILLINUX-$(CONFIG_READPROFILE) +=readprofile.o UTILLINUX-$(CONFIG_SWAPONOFF) +=swaponoff.o diff --git a/util-linux/mdev.c b/util-linux/mdev.c new file mode 100644 index 000000000..49904d17a --- /dev/null +++ b/util-linux/mdev.c @@ -0,0 +1,121 @@ +/* vi:set ts=4: + * + * mdev - Mini udev for busybox + * + * Copyright 2005 Rob Landley + * Copyright 2005 Frank Sorenson + * + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEV_PATH "/dev" +#define DEV_MODE 0660 + +#include + +/* mknod in /dev based on a path like "/sys/block/hda/hda1" */ +void make_device(char *path) +{ + char *device_name, *s; + int major,minor,type,len,fd; + + RESERVE_CONFIG_BUFFER(temp,PATH_MAX); + + /* Try to read major/minor string */ + + snprintf(temp, PATH_MAX, "%s/dev", path); + fd = open(temp, O_RDONLY); + len = read(fd, temp, PATH_MAX-1); + if (len<1) goto end; + temp[len] = 0; + close(fd); + + /* Determine device name, type, major and minor */ + + device_name = strrchr(path, '/') + 1; + type = strncmp(path+5, "block/" ,6) ? S_IFCHR : S_IFBLK; + major = minor = 0; + for(s = temp; *s; s++) { + if(*s == ':') { + major = minor; + minor = 0; + } else { + minor *= 10; + minor += (*s) - '0'; + } + } + +/* Open config file here, look up permissions */ + + sprintf(temp, "%s/%s", DEV_PATH, device_name); + if(mknod(temp, DEV_MODE | type, makedev(major, minor)) && errno != EEXIST) + bb_perror_msg_and_die("mknod %s failed", temp); + +/* Perform shellout here */ + +end: + RELEASE_CONFIG_BUFFER(temp); +} + +/* Recursive search of /sys/block or /sys/class. path must be a writeable + * buffer of size PATH_MAX containing the directory string to start at. */ + +void find_dev(char *path) +{ + DIR *dir; + int len=strlen(path); + + if(!(dir = opendir(path))) + bb_perror_msg_and_die("No %s",path); + + for(;;) { + struct dirent *entry = readdir(dir); + + if(!entry) break; + + /* Skip "." and ".." (also skips hidden files, which is ok) */ + + if (entry->d_name[0]=='.') continue; + + if (entry->d_type == DT_DIR) { + snprintf(path+len, PATH_MAX-len, "/%s", entry->d_name); + find_dev(path); + path[len] = 0; + } + + /* If there's a dev entry, mknod it */ + + if (strcmp(entry->d_name, "dev")) make_device(path); + } + + closedir(dir); +} + +int mdev_main(int argc, char *argv[]) +{ + if (argc > 1) { + if(argc == 2 && !strcmp(argv[1],"-s")) { + RESERVE_CONFIG_BUFFER(temp,PATH_MAX); + strcpy(temp,"/sys/block"); + find_dev(temp); + strcpy(temp,"/sys/class"); + find_dev(temp); + if(ENABLE_FEATURE_CLEAN_UP) + RELEASE_CONFIG_BUFFER(temp); + return 0; + } else bb_show_usage(); + } + +/* hotplug support goes here */ + + return 0; +} -- cgit v1.2.3