+
+ /* switchroot is either a path name starting with '/', or a
+ * pid of the original mdmon (we have already done the chroot).
+ * In the latter case, stdin is a socket connected to the original
+ * mdmon.
+ */
+
+ /* try to spawn mdmon instances from the target file system */
+ if (switchroot && switchroot[0] == '/' &&
+ strcmp(switchroot, "/") != 0) {
+ pid_t pid;
+ char buf[20];
+
+ switch (fork()) {
+ case 0:
+ victim = mdmon_pid(devnum);
+ victim_sock = connect_monitor(devname);
+ if (chroot(switchroot) != 0) {
+ fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
+ switchroot, strerror(errno));
+ exit(4);
+ }
+ ignore = chdir("/");
+ sprintf(buf, "%d", victim);
+ if (victim_sock) {
+ close(0);
+ dup(victim_sock);
+ close(victim_sock);
+ }
+ execl("/sbin/mdmon", "mdmon", devname, buf, NULL);
+ exit(1);
+ case -1:
+ return 1;
+ default:
+ pid = wait(&status);
+ if (pid > -1 && WIFEXITED(status) &&
+ WEXITSTATUS(status) == 0)
+ return 0;
+ else
+ return 1;
+ }
+ }
+