Esempio n. 1
0
int main(int argc, char *argv[])
{
	puts("Mounting...");

	do_mounts();

	run_process("/virt/init_stage2");

	printf("Init failed: %s\n", strerror(errno));

	return 0;
}
Esempio n. 2
0
int main(int argc, char *argv[])
{
	puts("Mounting...");

	do_mounts();

	puts("Starting '/bin/sh'...");

	run_process("/bin/sh");

	printf("Init failed: %s\n", strerror(errno));

	return 0;
}
Esempio n. 3
0
int main(int argc, char *argv[])
{
	pid_t child;
	int status;

	puts("Mounting...");

	do_mounts();

	/* get session leader */
	setsid();

	/* set controlling terminal */
	ioctl(0, TIOCSCTTY, 1);

	child = fork();
	if (child < 0) {
		printf("Fatal: fork() failed with %d\n", child);
		return 0;
	} else if (child == 0) {
		if (access("/virt/sandbox.sh", R_OK) == 0)
			run_process_sandbox("/bin/sh");
		else
			run_process("/bin/sh");
	} else {
		pid_t corpse;

		do {
			corpse = waitpid(-1, &status, 0);
		} while (corpse != child);
	}

	reboot(LINUX_REBOOT_CMD_RESTART);

	printf("Init failed: %s\n", strerror(errno));

	return 0;
}
Esempio n. 4
0
int
parmount(char **mntlist, int count, char *fstype)
{
	int 		maxfd =	OPEN_MAX;
	struct 		rlimit rl;
	vfsent_t	**vl, *vp;

	/*
	 * Process scaling.  After running a series
	 * of tests based on the number of simultaneous processes and
	 * processors available, optimum performance was achieved near or
	 * at (PROCN * 2).
	 */
	if ((maxrun = sysconf(_SC_NPROCESSORS_ONLN)) == -1)
		maxrun = 4;
	else
		maxrun = maxrun * 2 + 1;

	if (getrlimit(RLIMIT_NOFILE, &rl) == 0) {
		rl.rlim_cur = rl.rlim_max;
		if (setrlimit(RLIMIT_NOFILE, &rl) == 0)
			maxfd = (int)rl.rlim_cur;
	}
	(void) enable_extended_FILE_stdio(-1, -1);

	/*
	 * The parent needs to maintain 3 of its own fd's, plus 2 for
	 * each child (the stdout and stderr pipes).
	 */
	maxfd = (maxfd / 2) - 6;	/* 6 takes care of temporary  */
					/* periods of open fds */
	if (maxfd < maxrun)
		maxrun = maxfd;
	if (maxrun < 4)
		maxrun = 4;		/* sanity check */

	if (count == 0)
		mntlist = NULL;		/* used as a flag later */
	else
		fstype = NULL;		/* mount points supplied: */
					/* ignore fstype */
	/*
	 * Read the whole vfstab into a linked list for quick processing.
	 * On average, this is the most efficient way to collect and
	 * manipulate the vfstab data.
	 */
	vfsll = getvfsall(fstype, mntlist == NULL);

	/*
	 * Make an array out of the vfs linked list for sorting purposes.
	 */
	if (vfsll == NULL ||
	    (vfsarray = make_vfsarray(mntlist, count)) == NULL) {
		if (mntlist == NULL)	/* not an error - just none found */
			return (0);

		fprintf(stderr, gettext("%s: No valid entries found in %s\n"),
		    myname, vfstab);
		return (1);
	}

	/*
	 * Sort the entries based on their resolved path names
	 *
	 * If an lofs is encountered, then the original order of the vfstab
	 * file needs to be maintained until we are done mounting lofs's.
	 */
	if (!lofscnt)
		qsort((void *)vfsarray, vfsarraysize, sizeof (vfsent_t *),
		    mlevelcmp);

	/*
	 * Shrink the vfsll linked list down to the new list.  This will
	 * speed up the pid search in cleanupkid() later.
	 */
	vfsll = vfsarray[0];
	for (vl = vfsarray; vp = *vl; )
		vp->next = *++vl;

	/*
	 * Try to handle interrupts in a reasonable way.
	 */
	sigset(SIGHUP, cleanup);
	sigset(SIGQUIT, cleanup);
	sigset(SIGINT, cleanup);

	do_mounts();		/* do the mounts */

	if (failcnt > 0 && failcnt == lofsfail)
		return (ALL_LOFS_FAILURES);

	return (exitcode);
}
Esempio n. 5
0
int main(int argc, char *argv[])
{
	char **cmdv, **args;
	char *cmdlines[3];
	int i;
	const char *errmsg;
	int ret = 0;
	int cmdc;
	int fd;
	struct timeval now;
	char *mount_argv[] = {"mount_part", "rootfs", "/root"};
	pid_t pid;
	int nandboot = 0;

	gettimeofday(&now, NULL);
	srand48(now.tv_usec ^ (now.tv_sec << 24));

	/* Default parameters for anything init-like we execute */
	init_argc = argc;
	init_argv = alloca((argc+1)*sizeof(char *));
	memcpy(init_argv, argv, (argc+1)*sizeof(char *));

	/*
	 * omit /dev/console when generating initramfs,
	 * so we create it dynamically
	 */
	if (access("/dev/console", O_RDWR)) {
		mknod("/dev/console", S_IFCHR|0644, makedev(5, 1));
	}

	if ((fd = open("/dev/console", O_RDWR)) != -1) {
		dup2(fd, STDIN_FILENO);
		dup2(fd, STDOUT_FILENO);
		dup2(fd, STDERR_FILENO);

		if (fd > STDERR_FILENO) {
			close(fd);
		}
	}

	mnt_procfs = mount_sys_fs("/proc/cmdline", "/proc", "proc") >= 0;
	if (!mnt_procfs) {
		ret = 1;
		goto bail;
	}

	mnt_sysfs = mount_sys_fs("/sys/bus", "/sys", "sysfs") >= 0;
	if (!mnt_sysfs) {
		ret = 1;
		goto bail;
	}

	/* Construct the effective kernel command line.  The
	   effective kernel command line consists of /arch.cmd, if
	   it exists, /proc/cmdline, plus any arguments after an --
	   argument on the proper command line, in that order. */

	ret = readfile("/arch.cmd", &cmdlines[0]);
	if (ret < 0)
		cmdlines[0] = "";

	ret = readfile("/proc/cmdline", &cmdlines[1]);
	if (ret < 0) {
		fprintf(stderr, "%s: cannot read /proc/cmdline\n", progname);
		ret = 1;
		goto bail;
	}

	cmdlines[2] = NULL;

	/* Find an -- argument, and if so append to the command line */
	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "--")) {
			i++;
			break;
		}
	}
	args = &argv[i];	/* Points either to first argument past -- or
				   to the final NULL */

	/* Count the number of arguments */
	cmdc = split_cmdline(INT_MAX, NULL, argv[0], cmdlines, args);

	/* Actually generate the cmdline array */
	cmdv = (char **)alloca((cmdc+1)*sizeof(char *));
	if (split_cmdline(cmdc, cmdv, argv[0], cmdlines, args) != cmdc) {
		ret = 1;
		goto bail;
	}

	/* Debugging... */
	dump_args(cmdc, cmdv);

	{
		const char * root_device_name = get_arg(cmdc, cmdv, "root=");
		if (strncmp(root_device_name, "/dev/mtdblock", strlen("/dev/mtdblock")) == 0) {
			nandboot = 1;
			printf("kinit: NAND mode, check online upgrade flag\n");
			do_rootfs_OU();
		} else {
			nandboot = 0;
			printf("kinit: None-NAND mode, ignore online upgrade flag\n");
		}
	}

	/* Resume from suspend-to-disk, if appropriate */
	/* If successful, does not return */
	do_resume(cmdc, cmdv);

	/* Initialize networking, if applicable */
	do_ipconfig(cmdc, cmdv);

	check_path("/root");

	if (nandboot) {
		int index = 0;
		while (1) {
			char name[128];
			snprintf(name, sizeof(name), "/sys/block/mtdblock%d", index);
			if (access(name, F_OK) == 0) {
				snprintf(name, sizeof(name), "/dev/mtdblock%d", index);
				create_dev(name, name_to_dev_t(name));
				index++;
			} else {
				break;
			}
		}
		if((pid=fork())<0)
			fprintf(stderr, "fork error.\n");
		else if(pid == 0)
		{
			if((ret = execve("/bin/mount_part", mount_argv, NULL)) <0)
				perror("excute mount_part error\n");
		}
		if(waitpid(pid, NULL, 0) < 0)
			fprintf(stderr, "wait mount_part error.\n");
	} else {
		do_mounts(cmdc, cmdv);
	}

	if (mnt_procfs) {
		umount2("/proc", 0);
		mnt_procfs = 0;
	}

	if (mnt_sysfs) {
		umount2("/sys", 0);
		mnt_sysfs = 0;
	}

	make_devices();

	init_path = find_init("/root", get_arg(cmdc, cmdv, "init="));
	if (!init_path) {
		fprintf(stderr, "%s: init not found!\n", progname);
		ret = 2;
		goto bail;
	}
	DEBUG(("kinit: init_path = %s, init=%s\n", init_path, get_arg(cmdc, cmdv, "init=")));

	init_argv[0] = strrchr(init_path, '/') + 1;

	errmsg = run_init("/root", "/dev/console", init_path, init_argv);

	/* If run_init returned, something went bad */
	fprintf(stderr, "%s: %s: %s\n", progname, errmsg, strerror(errno));
	ret = 2;
	goto bail;

bail:
	if (mnt_procfs)
		umount2("/proc", 0);

	if (mnt_sysfs)
		umount2("/sys", 0);

	/*
	 * If we get here, something bad probably happened, and the kernel
	 * will most likely panic.  Drain console output so the user can
	 * figure out what happened.
	 */
	tcdrain(2);
	tcdrain(1);

	return ret;
}