return NULL;
}
-static int test_pidfile(char *devname)
-{
- char path[100];
- struct stat st;
-
- sprintf(path, "/var/run/mdadm/%s.pid", devname);
- return stat(path, &st);
-}
-
int make_pidfile(char *devname, int o_excl)
{
char path[100];
return 1;
}
-pid_t devname2mdmon(char *devname)
-{
- char buf[100];
- pid_t pid = -1;
- int fd;
-
- sprintf(buf, "/var/run/mdadm/%s.pid", devname);
- fd = open(buf, O_RDONLY|O_NOATIME);
- if (fd < 0)
- return -1;
-
- if (read(fd, buf, sizeof(buf)) > 0)
- sscanf(buf, "%d\n", &pid);
- close(fd);
-
- return pid;
-}
-
-static void try_kill_monitor(pid_t pid, char *devname)
+static void try_kill_monitor(pid_t pid, char *devname, int sock)
{
char buf[100];
int fd;
- struct mdstat_ent *mdstat;
+ int n;
+ long fl;
/* first rule of survival... don't off yourself */
if (pid == getpid())
if (fd < 0)
return;
- if (read(fd, buf, sizeof(buf)) < 0) {
- close(fd);
- return;
- }
+ n = read(fd, buf, sizeof(buf)-1);
+ buf[sizeof(buf)-1] = 0;
+ close(fd);
- if (!strstr(buf, "mdmon"))
+ if (n < 0 || !strstr(buf, "mdmon"))
return;
kill(pid, SIGTERM);
- mdstat = mdstat_read(0, 0);
- for ( ; mdstat; mdstat = mdstat->next)
- if (is_container_member(mdstat, devname)) {
- sprintf(buf, "/dev/%s", mdstat->dev);
- WaitClean(buf, 0);
- }
- free_mdstat(mdstat);
+ /* Wait for monitor to exit by reading from the socket, after
+ * clearing the non-blocking flag */
+ fl = fcntl(sock, F_GETFL, 0);
+ fl &= ~O_NONBLOCK;
+ fcntl(sock, F_SETFL, fl);
+ read(sock, buf, 100);
}
void remove_pidfile(char *devname)
exit(2);
}
-int mdmon(char *devname, int devnum, int scan, char *switchroot);
+static int mdmon(char *devname, int devnum, int must_fork, char *switchroot);
int main(int argc, char *argv[])
{
char *switchroot = NULL;
int devnum;
char *devname;
- int scan = 0;
int status = 0;
switch (argc) {
struct mdstat_ent *mdstat, *e;
/* launch an mdmon instance for each container found */
- scan = 1;
mdstat = mdstat_read(0, 0);
for (e = mdstat; e; e = e->next) {
if (strncmp(e->metadata_version, "external:", 9) == 0 &&
memset(container_name, 0, strlen(container_name));
sprintf(container_name, "%s", devname);
}
- status |= mdmon(devname, e->devnum, scan,
+ status |= mdmon(devname, e->devnum, 1,
switchroot);
}
}
container_name);
exit(1);
}
- return mdmon(devname, devnum, scan, switchroot);
+ return mdmon(devname, devnum, do_fork(), switchroot);
}
-int mdmon(char *devname, int devnum, int scan, char *switchroot)
+static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
{
int mdfd;
struct mdinfo *mdi, *di;
int status;
int ignore;
pid_t victim = -1;
+ int victim_sock = -1;
dprintf("starting mdmon for %s in %s\n",
devname, switchroot ? : "/");
+
+ /* 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;
+ }
+ }
+
mdfd = open_dev(devnum);
if (mdfd < 0) {
fprintf(stderr, "mdmon: %s: %s\n", devname,
}
/* Fork, and have the child tell us when they are ready */
- if (do_fork() || scan) {
+ if (must_fork) {
if (pipe(pfd) != 0) {
fprintf(stderr, "mdmon: failed to create pipe\n");
return 1;
/* we assume we assume that /sys /proc /dev are available in
* the new root
*/
- victim = devname2mdmon(container->devname);
- if (chroot(switchroot) != 0) {
- fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
- switchroot, strerror(errno));
- exit(4);
+ if (switchroot[0] == '/') {
+ victim = mdmon_pid(container->devnum);
+ victim_sock = connect_monitor(container->devname);
+ } else {
+ victim = atoi(switchroot);
+ victim_sock = 0;
}
}
ignore = chdir("/");
- if (victim < 0 && test_pidfile(container->devname) == 0) {
+ if (victim < 0) {
if (ping_monitor(container->devname) == 0) {
fprintf(stderr, "mdmon: %s already managed\n",
container->devname);
exit(3);
- } else if (victim < 0)
- victim = devname2mdmon(container->devname);
+ }
+ /* if there is a pid file, kill whoever is there just in case */
+ victim = mdmon_pid(container->devnum);
}
if (container->ss->load_super(container, mdfd, devname)) {
fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
getppid());
close(pfd[1]);
+ mlockall(MCL_CURRENT | MCL_FUTURE);
+
+ if (clone_monitor(container) < 0) {
+ fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
+ strerror(errno));
+ exit(2);
+ }
+
+ if (victim > -1) {
+ try_kill_monitor(victim, container->devname, victim_sock);
+ close(victim_sock);
+ }
+
setsid();
close(0);
open("/dev/null", O_RDWR);
ignore = dup(0);
#endif
- mlockall(MCL_FUTURE);
-
- if (clone_monitor(container) < 0) {
- fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
- strerror(errno));
- exit(2);
- }
-
- if (victim > -1)
- try_kill_monitor(victim, container->devname);
do_manager(container);
exit(0);