+ /* SIGUSR is sent between parent and child. So both block it
+ * and enable it only with pselect.
+ */
+ sigemptyset(&set);
+ sigaddset(&set, SIGUSR1);
+ sigaddset(&set, SIGHUP);
+ sigaddset(&set, SIGALRM);
+ sigaddset(&set, SIGTERM);
+ sigprocmask(SIG_BLOCK, &set, NULL);
+ act.sa_handler = wake_me;
+ act.sa_flags = 0;
+ sigaction(SIGUSR1, &act, NULL);
+ sigaction(SIGALRM, &act, NULL);
+ act.sa_handler = hup;
+ sigaction(SIGHUP, &act, NULL);
+ act.sa_handler = term;
+ sigaction(SIGTERM, &act, NULL);
+ act.sa_handler = SIG_IGN;
+ sigaction(SIGPIPE, &act, NULL);
+
+ 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);