Makefile, init.c, init.h: multiple changes in code and algorithms.
- New udev control logic imported from make-initrd scripts; - Udev-trigger replaced to udev-settle() in hardware wait loops; - All udev control code moved from init.c to new udev.c module; - Lazy initialization mode support added for work in cooperate with make-initrd: this is experimental future, turned off by default, and also required patched make-initrd-propagator; - Small fixes for suppress compiler warnings;
This commit is contained in:
parent
416b43d012
commit
70bd62ff84
2
Makefile
2
Makefile
@ -80,7 +80,7 @@ STAGE1_NETWORK_LIBS = $($(L)_STAGE1_NETWORK_LIBS)
|
||||
|
||||
STAGE1SRC = stage1.c log.c tools.c modules.c probing.c \
|
||||
mount.c lomount.c automatic.c frontend-common.c \
|
||||
cdrom.c disk.c common.c \
|
||||
cdrom.c disk.c common.c udev.c \
|
||||
network.c dhcp.c url.c dns.c adsl.c \
|
||||
sha256.c
|
||||
|
||||
|
183
init.c
183
init.c
@ -38,6 +38,7 @@
|
||||
#include "lomount.h"
|
||||
#include "tools.h"
|
||||
#include "common.h"
|
||||
#include "udev.h"
|
||||
|
||||
#define RAMFS_MAGIC 0x858458f6
|
||||
#define TMPFS_MAGIC 0x01021994
|
||||
@ -46,7 +47,13 @@
|
||||
#define MNT_DETACH 2
|
||||
#endif
|
||||
|
||||
char * env[] = {
|
||||
/*
|
||||
* This need to safe symbols export
|
||||
*/
|
||||
#define fatal(m) init_fatal(m)
|
||||
#define warn(m) init_warn(m)
|
||||
|
||||
char * const env[] = {
|
||||
"PATH=/usr/bin:/bin:/sbin:/usr/sbin",
|
||||
"HOME=/",
|
||||
"RUN_INITRD=1",
|
||||
@ -57,17 +64,25 @@ char * env[] = {
|
||||
|
||||
char ** myenv = NULL;
|
||||
|
||||
char *udevd[] = {"/sbin/udevd", "udevd", "--resolve-names=never", NULL};
|
||||
char *udevtrigger[] = {"/sbin/udevadm", "udevadm", "trigger", NULL};
|
||||
char *udevtrigger_add[] = {"/sbin/udevadm", "udevadm", "trigger", "--action=add", NULL};
|
||||
char *udevsettle[] = {"/sbin/udevadm", "udevadm", "settle", NULL};
|
||||
/*
|
||||
* Need to cooperate with make-inird scripts?
|
||||
*
|
||||
* 0=no, -1=yes.
|
||||
*
|
||||
* It is also turn on lazy initialization mode (after
|
||||
* /dev, /sys and /proc mounts, after udevd started).
|
||||
* This mode require make-initrd generated initramfs
|
||||
* image and patched make-initrd-propagator with hook
|
||||
* /scripts/pre/prepare/060-propagator. By default,
|
||||
* propagator is master to udev control.
|
||||
*/
|
||||
int cooperate_mode = 0;
|
||||
|
||||
char *init_top[] = {"/sbin/init-top", "init-top", NULL};
|
||||
char *init_premount[] = {"/sbin/init-premount", "init-premount", NULL};
|
||||
char *init_bottom[] = {"/sbin/init-bottom", "init-bottom", NULL};
|
||||
|
||||
extern void stage1();
|
||||
static void fatal(const char *) __attribute__((noreturn));
|
||||
|
||||
/*
|
||||
* this needs to handle the following cases:
|
||||
@ -78,14 +93,14 @@ static void fatal(const char *) __attribute__((noreturn));
|
||||
* 4) run from a floppy that's been loaded into a ramdisk
|
||||
*/
|
||||
|
||||
static void fatal(const char *msg)
|
||||
void init_fatal(const char *msg)
|
||||
{
|
||||
printf("FATAL ERROR IN INIT: %s\nI can't recover from this, "
|
||||
"please reboot manually and send bugreport.\n", msg);
|
||||
while (1);
|
||||
for(;;) sleep(600);
|
||||
}
|
||||
|
||||
static void warn(char *msg)
|
||||
void init_warn(const char *msg)
|
||||
{
|
||||
printf("W: %s\n", msg);
|
||||
}
|
||||
@ -107,7 +122,7 @@ static int _mknod(const char *pathname, mode_t mode, dev_t dev)
|
||||
*/
|
||||
static pid_t doklog()
|
||||
{
|
||||
int in, out, i, ii;
|
||||
int in, out, i;
|
||||
pid_t pid;
|
||||
char buf[1024];
|
||||
|
||||
@ -134,14 +149,16 @@ static pid_t doklog()
|
||||
/* disable on-console syslog output */
|
||||
klogctl(8, NULL, 1);
|
||||
|
||||
while (1)
|
||||
for (;;) {
|
||||
if ((i = read(in, buf, sizeof(buf))) > 0)
|
||||
ii = write(out, buf, i);
|
||||
i = write(out, buf, i);
|
||||
}
|
||||
}
|
||||
|
||||
pid_t spawn(char *av[])
|
||||
pid_t spawn(char * const av[])
|
||||
{
|
||||
pid_t pid;
|
||||
|
||||
if ((pid = fork())) {
|
||||
if (pid < 0) fatal(av[0]);
|
||||
return pid;
|
||||
@ -170,16 +187,16 @@ static void take_env(int fd)
|
||||
|
||||
if ((i = read(fd, buf, sizeof(buf))) < 0) {
|
||||
warn("failed to read env from pipe");
|
||||
myenv = env;
|
||||
myenv = (char **) env;
|
||||
return;
|
||||
}
|
||||
|
||||
close(fd);
|
||||
buf[i] = '\0';
|
||||
|
||||
if ((ep = myenv = malloc(sizeof(char *) * 32)) == NULL) {
|
||||
if ((ep = myenv = (char **) malloc(sizeof(char *) * 32)) == NULL) {
|
||||
warn("can't malloc env");
|
||||
myenv = env;
|
||||
myenv = (char **) env;
|
||||
return;
|
||||
}
|
||||
|
||||
@ -199,11 +216,14 @@ static int nuke_dirent(int len, const char *dir, const char *name, dev_t me)
|
||||
{
|
||||
int bytes = len+strlen(name)+2;
|
||||
char path[bytes];
|
||||
int xlen;
|
||||
struct stat st;
|
||||
|
||||
xlen = snprintf(path, bytes, "%s/%s", dir, name);
|
||||
/* assert(xlen < bytes); */
|
||||
#if 0 /* This is a way to suppress warning message about unused xlen */
|
||||
int xlen = snprintf(path, bytes, "%s/%s", dir, name);
|
||||
assert(xlen < bytes);
|
||||
#else
|
||||
snprintf(path, bytes, "%s/%s", dir, name);
|
||||
#endif
|
||||
|
||||
if ( lstat(path, &st) )
|
||||
return ENOENT; /* Return 0 since already gone? */
|
||||
@ -215,7 +235,7 @@ static int nuke_dirent(int len, const char *dir, const char *name, dev_t me)
|
||||
}
|
||||
|
||||
/* Wipe the contents of a directory, but not the directory itself */
|
||||
static int nuke_dir(const char *what)
|
||||
int nuke_dir(const char *what)
|
||||
{
|
||||
int len = strlen(what);
|
||||
DIR *dir;
|
||||
@ -241,7 +261,7 @@ static int nuke_dir(const char *what)
|
||||
(d->d_name[1] == '\0' ||
|
||||
(d->d_name[1] == '.' && d->d_name[2] == '\0')) )
|
||||
continue;
|
||||
|
||||
|
||||
err = nuke_dirent(len, what, d->d_name, st.st_dev);
|
||||
if ( err ) {
|
||||
closedir(dir);
|
||||
@ -337,7 +357,7 @@ static void unmount_filesystems(void)
|
||||
if (strncmp(fs[i].dev + sizeof("/dev/") - 1, "loop",
|
||||
sizeof("loop") - 1) == 0)
|
||||
del_loop(fs[i].dev);
|
||||
|
||||
|
||||
printf("\t%s\n", fs[i].name);
|
||||
fs[i].mounted = 0;
|
||||
nb++;
|
||||
@ -358,31 +378,40 @@ static void unmount_filesystems(void)
|
||||
|
||||
int main(int argc, char **argv)
|
||||
{
|
||||
static const char localhost[] = "localhost.localdomain";
|
||||
struct stat rst, cst, ist, pst;
|
||||
struct statfs sfs;
|
||||
pid_t pid, klogpid, udevpid;
|
||||
pid_t pid, klogpid;
|
||||
sigset_t sig;
|
||||
int wait_status;
|
||||
int fd = -1;
|
||||
int fds[2];
|
||||
int fd, fds[2];
|
||||
char *init = NULL;
|
||||
|
||||
if (mount("/proc", "/proc", "proc", 0, NULL))
|
||||
fatal("failed to mount proc filesystem");
|
||||
if (mount("sysfs", "/sys", "sysfs", 0, NULL))
|
||||
fatal("failed to mount sysfs filesystem");
|
||||
/* auto-detect lazy initialization mode */
|
||||
fd = open("/proc/mounts", O_RDONLY, 0);
|
||||
if (fd >= 0) {
|
||||
close(fd);
|
||||
cooperate_mode = -1;
|
||||
}
|
||||
fd = -1;
|
||||
|
||||
if (mount("udevfs", "/dev", "devtmpfs", 0, "size=8M,mode=0755")) {
|
||||
switch (errno) {
|
||||
case ENODEV:
|
||||
/* There is no devtmpfs for current kernel, try mount tmpfs */
|
||||
if (mount("udev", "/dev", "tmpfs", 0, "size=8M,mode=0755"))
|
||||
fatal("failed to mount tmpfs filesystem");
|
||||
case EBUSY:
|
||||
/* Don't mount /dev if it is already mounted */
|
||||
break;
|
||||
default:
|
||||
fatal("failed to mount devtmpfs filesystem");
|
||||
if (!cooperate_mode) {
|
||||
if (mount("/proc", "/proc", "proc", 0, NULL))
|
||||
fatal("failed to mount proc filesystem");
|
||||
if (mount("sysfs", "/sys", "sysfs", 0, NULL))
|
||||
fatal("failed to mount sysfs filesystem");
|
||||
if (mount("udevfs", "/dev", "devtmpfs", 0, "size=8M,mode=0755")) {
|
||||
switch (errno) {
|
||||
case ENODEV:
|
||||
/* There is no devtmpfs for current kernel, try mount tmpfs */
|
||||
if (mount("udev", "/dev", "tmpfs", 0, "size=8M,mode=0755"))
|
||||
fatal("failed to mount tmpfs filesystem");
|
||||
case EBUSY:
|
||||
/* Don't mount /dev if it is already mounted */
|
||||
break;
|
||||
default:
|
||||
fatal("failed to mount devtmpfs filesystem");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -392,19 +421,22 @@ int main(int argc, char **argv)
|
||||
sigaddset(&sig, SIGTSTP);
|
||||
sigprocmask(SIG_BLOCK, &sig, NULL);
|
||||
|
||||
if (_mknod("/dev/console", S_IFCHR, MKDEV(5, 1)) < 0 ||
|
||||
(fd = open("/dev/console", O_RDWR, 0)) < 0) {
|
||||
fatal("failed to open /dev/console");
|
||||
if (!cooperate_mode) {
|
||||
if (_mknod("/dev/console", S_IFCHR, MKDEV(5, 1)) < 0 ||
|
||||
(fd = open("/dev/console", O_RDWR, 0)) < 0)
|
||||
{
|
||||
fatal("failed to open /dev/console");
|
||||
}
|
||||
|
||||
dup2(fd, 0);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
close(fd);
|
||||
|
||||
if (_mknod("/dev/null", S_IFCHR, MKDEV(1, 3)) < 0)
|
||||
fatal("failed to create /dev/null");
|
||||
}
|
||||
|
||||
dup2(fd, 0);
|
||||
dup2(fd, 1);
|
||||
dup2(fd, 2);
|
||||
close(fd);
|
||||
|
||||
if (_mknod("/dev/null", S_IFCHR, MKDEV(1, 3)) < 0)
|
||||
fatal("failed to create /dev/null");
|
||||
|
||||
/* I set me up as session leader (probably not necessary?) */
|
||||
setsid();
|
||||
if (ioctl(0, TIOCSCTTY, NULL)) {
|
||||
@ -412,7 +444,7 @@ int main(int argc, char **argv)
|
||||
warn("could not set new controlling tty");
|
||||
}
|
||||
|
||||
if (sethostname("localhost.localdomain", sizeof("localhost.localdomain")) < 0)
|
||||
if (sethostname(localhost, sizeof(localhost)) < 0)
|
||||
warn("could not set hostname");
|
||||
|
||||
/* the default domainname (as of 2.0.35) is "(none)", which confuses glibc */
|
||||
@ -424,29 +456,7 @@ int main(int argc, char **argv)
|
||||
|
||||
klogpid = doklog();
|
||||
spawn_hook(init_top);
|
||||
|
||||
if (mkdirs_dev(".udev", ".udev/db", NULL) < 0)
|
||||
fatal("/dev/.udev/db");
|
||||
|
||||
printf("Spawning udevd...");
|
||||
|
||||
udevpid = spawn(udevd);
|
||||
|
||||
usleep(500);
|
||||
|
||||
if (mkdir_dev(".udev/queue") < 0)
|
||||
fatal("cannot create /dev/.udev/queue");
|
||||
|
||||
if (waitpid(spawn(udevtrigger), &wait_status, 0) < 0 ||
|
||||
!(WIFEXITED(wait_status)))
|
||||
warn("udevtrigger");
|
||||
|
||||
if (waitpid(spawn(udevsettle), &wait_status, 0) < 0 ||
|
||||
!(WIFEXITED(wait_status)))
|
||||
warn("udevsettle");
|
||||
|
||||
printf("done\n");
|
||||
|
||||
udev_start();
|
||||
spawn_hook(init_premount);
|
||||
|
||||
/* Go into normal init mode - keep going, and then do a orderly shutdown
|
||||
@ -496,28 +506,11 @@ int main(int argc, char **argv)
|
||||
unmount_filesystems();
|
||||
|
||||
printf("you may safely reboot your system\n");
|
||||
while (1);
|
||||
for (;;) sleep(600);
|
||||
}
|
||||
|
||||
take_env(fds[0]);
|
||||
|
||||
if (waitpid(spawn(udevsettle), &wait_status, 0) < 0 ||
|
||||
!(WIFEXITED(wait_status)))
|
||||
warn("udevsettle");
|
||||
|
||||
kill(udevpid, 9);
|
||||
waitpid(udevpid, &wait_status, 0);
|
||||
nuke_dir("/dev/.udev/queue");
|
||||
|
||||
if ((fd = open("/.udev_version", O_RDONLY, 0)) >0) {
|
||||
char buf[32];
|
||||
int n = read(fd, buf, sizeof(buf));
|
||||
close(fd);
|
||||
if ((fd = creat("/dev/.initramfs/udev_version", 0)) >0) {
|
||||
n = write(fd, buf, n);
|
||||
close(fd);
|
||||
} else fatal("creat()");
|
||||
}
|
||||
udev_stop();
|
||||
|
||||
spawn_hook(init_bottom);
|
||||
|
||||
@ -596,7 +589,7 @@ int main(int argc, char **argv)
|
||||
fatal("chroot");
|
||||
|
||||
/* Check for given init */
|
||||
init = get_from_env("INIT", myenv);
|
||||
init = get_from_env("INIT", (const char* const*) myenv);
|
||||
if (init == NULL) init = STAGE2_BINNAME;
|
||||
|
||||
if (stat(init, &ist) || !S_ISREG(ist.st_mode) || !(ist.st_mode & S_IXUSR))
|
||||
|
Loading…
x
Reference in New Issue
Block a user