mdev: do not follow symlinks in /sys (as was intended prior to rev 18811).
authorDenis Vlasenko <vda.linux@googlemail.com>
Sun, 6 Jul 2008 07:00:11 +0000 (07:00 -0000)
committerDenis Vlasenko <vda.linux@googlemail.com>
Sun, 6 Jul 2008 07:00:11 +0000 (07:00 -0000)
If this breaks things, please document why!
mdev,init: use shared code for fd sanitization

function                                             old     new   delta
bb_daemonize_or_rexec                                155     172     +17
mdev_main                                            500     505      +5
init_main                                            907     856     -51
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 22/-51)            Total: -29 bytes

init/init.c
libbb/vfork_daemon_rexec.c
util-linux/mdev.c

index 23289670971bc08270c0385b8e3da9c47d2074bc..4b2bd9c77f6b3ea86ce719ac1198fecfead388fb 100644 (file)
@@ -221,20 +221,7 @@ static void console_init(void)
        } else {
                /* Make sure fd 0,1,2 are not closed
                 * (so that they won't be used by future opens) */
-
-               /* bb_sanitize_stdio(); - WRONG.
-                * It fails if "/dev/null" doesnt exist, and for init
-                * this is a real possibility! Open code it instead. */
-
-               int fd = open(bb_dev_null, O_RDWR);
-               if (fd < 0) {
-                       /* Give me _ANY_ open descriptor! */
-                       fd = xopen("/", O_RDONLY); /* we don't believe this can fail */
-               }
-               while ((unsigned)fd < 2)
-                       fd = dup(fd);
-               if (fd > 2)
-                       close(fd);
+               bb_sanitize_stdio();
        }
 
        s = getenv("TERM");
index 37d4c274e9fa420a2225af5dbea2d44570e257d1..da0dc03e58b236f38c360a383f75e30aea185bb1 100644 (file)
@@ -265,7 +265,14 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
                close(2);
        }
 
-       fd = xopen(bb_dev_null, O_RDWR);
+       fd = open(bb_dev_null, O_RDWR);
+       if (fd < 0) {
+               /* NB: we can be called as bb_sanitize_stdio() from init
+                * or mdev, and there /dev/null may legitimately not (yet) exist!
+                * Do not use xopen above, but obtain _ANY_ open descriptor,
+                * even bogus one as below. */
+               fd = xopen("/", O_RDONLY); /* don't believe this can fail */
+       }
 
        while ((unsigned)fd < 2)
                fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
index c7109373dbd574a4096acc5caa7eaec5b42050d6..9c4938a706aa6f0eaa5933e4f6f9ac3c518146a0 100644 (file)
@@ -19,7 +19,8 @@ struct globals {
 #define root_major (G.root_major)
 #define root_minor (G.root_minor)
 
-#define MAX_SYSFS_DEPTH 3 /* prevent infinite loops in /sys symlinks */
+/* Prevent infinite loops in /sys symlinks */
+#define MAX_SYSFS_DEPTH 3
 
 /* We use additional 64+ bytes in make_device() */
 #define SCRATCH_SIZE 80
@@ -392,11 +393,17 @@ int mdev_main(int argc, char **argv)
        char *env_path;
        RESERVE_CONFIG_BUFFER(temp, PATH_MAX + SCRATCH_SIZE);
 
-#ifdef YOU_WANT_TO_DEBUG_HOTPLUG_EVENTS
+       /* We can be called as hotplug helper */
        /* Kernel cannot provide suitable stdio fds for us, do it ourself */
+#if 1
+       bb_sanitize_stdio();
+#else
+       /* Debug code */
        /* Replace LOGFILE by other file or device name if you need */
 #define LOGFILE "/dev/console"
-       xmove_fd(xopen("/dev/null", O_RDONLY), STDIN_FILENO);
+       /* Just making sure fd 0 is not closed,
+        * we don't really intend to read from it */
+       xmove_fd(xopen("/", O_RDONLY), STDIN_FILENO);
        xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDOUT_FILENO);
        xmove_fd(xopen(LOGFILE, O_WRONLY|O_APPEND), STDERR_FILENO);
 #endif
@@ -414,11 +421,11 @@ int mdev_main(int argc, char **argv)
                root_minor = minor(st.st_dev);
 
                recursive_action("/sys/block",
-                       ACTION_RECURSE | ACTION_FOLLOWLINKS,
+                       ACTION_RECURSE /* no ACTION_FOLLOWLINKS! */,
                        fileAction, dirAction, temp, 0);
 
                recursive_action("/sys/class",
-                       ACTION_RECURSE | ACTION_FOLLOWLINKS,
+                       ACTION_RECURSE /* no ACTION_FOLLOWLINKS! */,
                        fileAction, dirAction, temp, 0);
 
        } else {