wok-tiny rev 128

busybox/mdev: restore /sys layout
author Pascal Bellard <pascal.bellard@slitaz.org>
date Thu Apr 13 19:09:32 2017 +0200 (2017-04-13)
parents 34a749493ee3
children 6c0be0a0a932
files busybox/receipt busybox/stuff/0001-mdev-create-devices-from-sys-dev.patch
line diff
     1.1 --- a/busybox/receipt	Mon Feb 13 00:04:27 2017 +0100
     1.2 +++ b/busybox/receipt	Thu Apr 13 19:09:32 2017 +0200
     1.3 @@ -39,6 +39,8 @@
     1.4  scriptreplay.u
     1.5  bug9471.u
     1.6  EOT
     1.7 +    [ $(. $WOK/linux/receipt; printf "%d%02d%02d" ${VERSION//./ }) -le 20626 ] &&
     1.8 +	patch -p1 -R < $stuff/0001-mdev-create-devices-from-sys-dev.patch
     1.9      cp $stuff/$PACKAGE-${VERSION%.*}.config .config
    1.10      var="CONFIG_CROSS_COMPILER_PREFIX"
    1.11      sed -i "s/.*$var.*/$var=\"uclibc-$TARGET-\"/" .config
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/busybox/stuff/0001-mdev-create-devices-from-sys-dev.patch	Thu Apr 13 19:09:32 2017 +0200
     2.3 @@ -0,0 +1,198 @@
     2.4 +From 20a3262cd756aadf8771969a4764cd3c571f0a3e Mon Sep 17 00:00:00 2001
     2.5 +From: Denys Vlasenko <vda.linux@googlemail.com>
     2.6 +Date: Wed, 7 Sep 2016 14:09:01 +0200
     2.7 +Subject: [PATCH] mdev: create devices from /sys/dev
     2.8 +
     2.9 +Currently some new devices that have a bus but no class will
    2.10 +be missed by mdev coldplug device creation after boot. This
    2.11 +happens because mdev recursively searches /sys/class which will
    2.12 +by definition only find class devices.
    2.13 +
    2.14 +Some important devices such as iio and gpiochip does not have
    2.15 +a class. But users will need them.
    2.16 +
    2.17 +This switches from using /sys/class as the place to look for
    2.18 +devices to create to using /sys/dev where all char and block
    2.19 +devices are listed.
    2.20 +
    2.21 +The subsystem lookup code that provide the G.subsystem
    2.22 +environment variable is changed from using the directory
    2.23 +name of the class device to instead dereference the
    2.24 +"subsystem" symlink for the device, and look at the last
    2.25 +element of the path of the symlink for the subsystem, which
    2.26 +will work with class devices and bus devices alike. (The new
    2.27 +bus-only devices only symlink to the /sys/bus/* hierarchy.)
    2.28 +
    2.29 +We delete the legacy kernel v2.6.2x /sys/block device path
    2.30 +code as part of this change. It's too old to be kept alive.
    2.31 +
    2.32 +Tested on kernel v4.6-rc2 with a bunch of devices, including
    2.33 +some IIO and gpiochip devices.
    2.34 +
    2.35 +With a print inserted before make_device() the log looks
    2.36 +like so:
    2.37 +
    2.38 +Create device from "/sys/dev/char/1:1", subsystem "mem"
    2.39 +Create device from "/sys/dev/char/1:2", subsystem "mem"
    2.40 +Create device from "/sys/dev/char/1:3", subsystem "mem"
    2.41 +Create device from "/sys/dev/char/1:5", subsystem "mem"
    2.42 +(...)
    2.43 +Create device from "/sys/dev/block/179:56", subsystem "block"
    2.44 +Create device from "/sys/dev/block/179:64", subsystem "block"
    2.45 +
    2.46 +function                                             old     new   delta
    2.47 +mdev_main                                           1388    1346     -42
    2.48 +dirAction                                            134      14    -120
    2.49 +------------------------------------------------------------------------------
    2.50 +(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-162)           Total: -162 bytes
    2.51 +
    2.52 +Cc: Isaac Dunham <ibid.ag@gmail.com>
    2.53 +Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
    2.54 +Cc: Jonathan Cameron <jic23@cam.ac.uk>
    2.55 +Signed-off-by: Linus Walleij <linus.walleij@linaro.org>
    2.56 +Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
    2.57 +---
    2.58 + util-linux/mdev.c | 88 ++++++++++++++++++++++++++-----------------------------
    2.59 + 1 file changed, 41 insertions(+), 47 deletions(-)
    2.60 +
    2.61 +diff --git a/util-linux/mdev.c b/util-linux/mdev.c
    2.62 +index 37514eb..a59115d 100644
    2.63 +--- a/util-linux/mdev.c
    2.64 ++++ b/util-linux/mdev.c
    2.65 +@@ -543,8 +543,7 @@ static char *build_alias(char *alias, const char *device_name)
    2.66 + 
    2.67 + /* mknod in /dev based on a path like "/sys/block/hda/hda1"
    2.68 +  * NB1: path parameter needs to have SCRATCH_SIZE scratch bytes
    2.69 +- * after NUL, but we promise to not mangle (IOW: to restore NUL if needed)
    2.70 +- * path string.
    2.71 ++ * after NUL, but we promise to not mangle it (IOW: to restore NUL if needed).
    2.72 +  * NB2: "mdev -s" may call us many times, do not leak memory/fds!
    2.73 +  *
    2.74 +  * device_name = $DEVNAME (may be NULL)
    2.75 +@@ -810,41 +809,39 @@ static void make_device(char *device_name, char *path, int operation)
    2.76 + 	} /* for (;;) */
    2.77 + }
    2.78 + 
    2.79 +-/* File callback for /sys/ traversal */
    2.80 ++/* File callback for /sys/ traversal.
    2.81 ++ * We act only on "/sys/.../dev" (pseudo)file
    2.82 ++ */
    2.83 + static int FAST_FUNC fileAction(const char *fileName,
    2.84 + 		struct stat *statbuf UNUSED_PARAM,
    2.85 + 		void *userData,
    2.86 + 		int depth UNUSED_PARAM)
    2.87 + {
    2.88 + 	size_t len = strlen(fileName) - 4; /* can't underflow */
    2.89 +-	char *scratch = userData;
    2.90 +-
    2.91 +-	/* len check is for paranoid reasons */
    2.92 +-	if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX)
    2.93 +-		return FALSE;
    2.94 +-
    2.95 +-	strcpy(scratch, fileName);
    2.96 +-	scratch[len] = '\0';
    2.97 +-	make_device(/*DEVNAME:*/ NULL, scratch, OP_add);
    2.98 +-
    2.99 +-	return TRUE;
   2.100 +-}
   2.101 +-
   2.102 +-/* Directory callback for /sys/ traversal */
   2.103 +-static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
   2.104 +-		struct stat *statbuf UNUSED_PARAM,
   2.105 +-		void *userData UNUSED_PARAM,
   2.106 +-		int depth)
   2.107 +-{
   2.108 +-	/* Extract device subsystem -- the name of the directory
   2.109 +-	 * under /sys/class/ */
   2.110 +-	if (1 == depth) {
   2.111 ++	char *path = userData;	/* char array[PATH_MAX + SCRATCH_SIZE] */
   2.112 ++	char subsys[PATH_MAX];
   2.113 ++	int res;
   2.114 ++
   2.115 ++	/* Is it a ".../dev" file? (len check is for paranoid reasons) */
   2.116 ++	if (strcmp(fileName + len, "/dev") != 0 || len >= PATH_MAX - 32)
   2.117 ++		return FALSE; /* not .../dev */
   2.118 ++
   2.119 ++	strcpy(path, fileName);
   2.120 ++	path[len] = '\0';
   2.121 ++
   2.122 ++	/* Read ".../subsystem" symlink in the same directory where ".../dev" is */
   2.123 ++	strcpy(subsys, path);
   2.124 ++	strcpy(subsys + len, "/subsystem");
   2.125 ++	res = readlink(subsys, subsys, sizeof(subsys)-1);
   2.126 ++	if (res > 0) {
   2.127 ++		subsys[res] = '\0';
   2.128 + 		free(G.subsystem);
   2.129 + 		if (G.subsys_env) {
   2.130 + 			bb_unsetenv_and_free(G.subsys_env);
   2.131 + 			G.subsys_env = NULL;
   2.132 + 		}
   2.133 +-		G.subsystem = strrchr(fileName, '/');
   2.134 ++		/* Set G.subsystem and $SUBSYSTEM from symlink's last component */
   2.135 ++		G.subsystem = strrchr(subsys, '/');
   2.136 + 		if (G.subsystem) {
   2.137 + 			G.subsystem = xstrdup(G.subsystem + 1);
   2.138 + 			G.subsys_env = xasprintf("%s=%s", "SUBSYSTEM", G.subsystem);
   2.139 +@@ -852,6 +849,17 @@ static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
   2.140 + 		}
   2.141 + 	}
   2.142 + 
   2.143 ++	make_device(/*DEVNAME:*/ NULL, path, OP_add);
   2.144 ++
   2.145 ++	return TRUE;
   2.146 ++}
   2.147 ++
   2.148 ++/* Directory callback for /sys/ traversal */
   2.149 ++static int FAST_FUNC dirAction(const char *fileName UNUSED_PARAM,
   2.150 ++		struct stat *statbuf UNUSED_PARAM,
   2.151 ++		void *userData UNUSED_PARAM,
   2.152 ++		int depth)
   2.153 ++{
   2.154 + 	return (depth >= MAX_SYSFS_DEPTH ? SKIP : TRUE);
   2.155 + }
   2.156 + 
   2.157 +@@ -872,8 +880,9 @@ static void load_firmware(const char *firmware, const char *sysfs_path)
   2.158 + 	int firmware_fd, loading_fd;
   2.159 + 
   2.160 + 	/* check for /lib/firmware/$FIRMWARE */
   2.161 +-	xchdir("/lib/firmware");
   2.162 +-	firmware_fd = open(firmware, O_RDONLY); /* can fail */
   2.163 ++	firmware_fd = -1;
   2.164 ++	if (chdir("/lib/firmware") == 0)
   2.165 ++		firmware_fd = open(firmware, O_RDONLY); /* can fail */
   2.166 + 
   2.167 + 	/* check for /sys/$DEVPATH/loading ... give 30 seconds to appear */
   2.168 + 	xchdir(sysfs_path);
   2.169 +@@ -1065,25 +1074,10 @@ int mdev_main(int argc UNUSED_PARAM, char **argv)
   2.170 + 
   2.171 + 		putenv((char*)"ACTION=add");
   2.172 + 
   2.173 +-		/* ACTION_FOLLOWLINKS is needed since in newer kernels
   2.174 +-		 * /sys/block/loop* (for example) are symlinks to dirs,
   2.175 +-		 * not real directories.
   2.176 +-		 * (kernel's CONFIG_SYSFS_DEPRECATED makes them real dirs,
   2.177 +-		 * but we can't enforce that on users)
   2.178 +-		 */
   2.179 +-		if (access("/sys/class/block", F_OK) != 0) {
   2.180 +-			/* Scan obsolete /sys/block only if /sys/class/block
   2.181 +-			 * doesn't exist. Otherwise we'll have dupes.
   2.182 +-			 * Also, do not complain if it doesn't exist.
   2.183 +-			 * Some people configure kernel to have no blockdevs.
   2.184 +-			 */
   2.185 +-			recursive_action("/sys/block",
   2.186 +-				ACTION_RECURSE | ACTION_FOLLOWLINKS | ACTION_QUIET,
   2.187 +-				fileAction, dirAction, temp, 0);
   2.188 +-		}
   2.189 +-		recursive_action("/sys/class",
   2.190 +-			ACTION_RECURSE | ACTION_FOLLOWLINKS,
   2.191 +-			fileAction, dirAction, temp, 0);
   2.192 ++		/* Create all devices from /sys/dev hierarchy */
   2.193 ++		recursive_action("/sys/dev",
   2.194 ++				 ACTION_RECURSE | ACTION_FOLLOWLINKS,
   2.195 ++				 fileAction, dirAction, temp, 0);
   2.196 + 	} else {
   2.197 + 		char *fw;
   2.198 + 		char *seq;
   2.199 +-- 
   2.200 +2.9.2
   2.201 +