+ if (switchroot) {
+ /* we assume we assume that /sys /proc /dev are available in
+ * the new root (see nash:setuproot)
+ *
+ * kill any monitors in the current namespace and change
+ * to the new one
+ */
+ try_kill_monitor(container->devname);
+ if (chroot(switchroot) != 0) {
+ fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
+ switchroot, strerror(errno));
+ exit(4);
+ }
+ }
+
+ /* If this fails, we hope it already exists
+ * pid file lives in /var/run/mdadm/mdXX.pid
+ */
+ mkdir("/var", 0600);
+ mkdir("/var/run", 0600);
+ mkdir("/var/run/mdadm", 0600);
+ ignore = chdir("/");
+ if (make_pidfile(container->devname, O_EXCL) < 0) {
+ if (ping_monitor(container->devname) == 0) {
+ fprintf(stderr, "mdmon: %s already managed\n",
+ container->devname);
+ exit(3);
+ } else {
+ int err;
+
+ /* cleanup the old monitor, this one is taking over */
+ try_kill_monitor(container->devname);
+ err = make_pidfile(container->devname, 0);
+ if (err < 0) {
+ fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
+ container->devname);
+ if (err == -EROFS) {
+ /* FIXME implement a mechanism to
+ * prevent duplicate monitor instances
+ */
+ fprintf(stderr,
+ "mdmon: continuing on read-only file system\n");
+ } else
+ exit(3);
+ }
+ }
+ }
+ container->sock = make_control_sock(container->devname);
+
+ if (container->ss->load_super(container, mdfd, container_name)) {
+ fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
+ container_name);
+ exit(3);
+ }
+
+ /* Ok, this is close enough. We can say goodbye to our parent now.
+ */
+ status = 0;
+ if (write(pfd[1], &status, sizeof(status)) < 0)
+ fprintf(stderr, "mdmon: failed to notify our parent: %d\n",
+ getppid());
+ close(pfd[1]);
+
+ setsid();
+ close(0);
+ open("/dev/null", O_RDWR);
+ close(1);
+ ignore = dup(0);
+#ifndef DEBUG
+ close(2);
+ ignore = dup(0);
+#endif
+
+ mlockall(MCL_FUTURE);
+