X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fmdadm.git;a=blobdiff_plain;f=mdmon.c;h=d20bb3e490c01bbde2067bffb70c31ae50dcab04;hp=877cf1a16762315b6378fa62061cb155f4aed3bb;hb=24f6f99b3630b1a89aaa57930c5c9de8a3df9ded;hpb=13047e4c0708ad06a6c0a07e79a080dade94df38 diff --git a/mdmon.c b/mdmon.c index 877cf1a1..d20bb3e4 100644 --- a/mdmon.c +++ b/mdmon.c @@ -1,3 +1,22 @@ +/* + * mdmon - monitor external metadata arrays + * + * Copyright (C) 2007-2009 Neil Brown + * Copyright (C) 2007-2009 Intel Corporation + * + * This program is free software; you can redistribute it and/or modify it + * under the terms and conditions of the GNU General Public License, + * version 2, as published by the Free Software Foundation. + * + * This program is distributed in the hope it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for + * more details. + * + * You should have received a copy of the GNU General Public License along with + * this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. + */ /* * md array manager. @@ -60,13 +79,25 @@ int run_child(void *v) return 0; } -int clone_monitor(struct supertype *container) +#ifdef __ia64__ +int __clone2(int (*fn)(void *), + void *child_stack_base, size_t stack_size, + int flags, void *arg, ... + /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ ); +#endif + int clone_monitor(struct supertype *container) { static char stack[4096]; +#ifdef __ia64__ + mon_tid = __clone2(run_child, stack, sizeof(stack), + CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, + container); +#else mon_tid = clone(run_child, stack+4096-64, CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, container); +#endif mgr_tid = syscall(SYS_gettid); @@ -82,7 +113,6 @@ static struct superswitch *find_metadata_methods(char *vers) return NULL; } - int make_pidfile(char *devname, int o_excl) { char path[100]; @@ -118,26 +148,16 @@ int is_container_member(struct mdstat_ent *mdstat, char *container) return 1; } -void remove_pidfile(char *devname); -static void try_kill_monitor(char *devname) +static void try_kill_monitor(pid_t pid, char *devname, int sock) { char buf[100]; int fd; - pid_t pid; - struct mdstat_ent *mdstat; - - sprintf(buf, "/var/run/mdadm/%s.pid", devname); - fd = open(buf, O_RDONLY); - if (fd < 0) - return; + int n; + long fl; - if (read(fd, buf, sizeof(buf)) < 0) { - close(fd); + /* first rule of survival... don't off yourself */ + if (pid == getpid()) return; - } - - close(fd); - pid = strtoul(buf, NULL, 10); /* kill this process if it is mdmon */ sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid); @@ -145,24 +165,21 @@ static void try_kill_monitor(char *devname) 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); - } - free_mdstat(mdstat); - remove_pidfile(devname); + /* 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) @@ -227,7 +244,7 @@ static void wake_me(int sig) static int do_fork(void) { #ifdef DEBUG - if (env_no_mdmon()) + if (check_env("MDADM_NO_MDMON")) return 0; #endif @@ -236,11 +253,79 @@ static int do_fork(void) void usage(void) { - fprintf(stderr, "Usage: mdmon [--switch-root dir] /device/name/for/container\n"); + fprintf(stderr, "Usage: mdmon /device/name/for/container [target_dir]\n"); exit(2); } +static int mdmon(char *devname, int devnum, int must_fork, char *switchroot); + int main(int argc, char *argv[]) +{ + char *container_name = NULL; + char *switchroot = NULL; + int devnum; + char *devname; + int status = 0; + + switch (argc) { + case 3: + switchroot = argv[2]; + case 2: + container_name = argv[1]; + break; + default: + usage(); + } + + if (strcmp(container_name, "/proc/mdstat") == 0) { + struct mdstat_ent *mdstat, *e; + + /* launch an mdmon instance for each container found */ + mdstat = mdstat_read(0, 0); + for (e = mdstat; e; e = e->next) { + if (strncmp(e->metadata_version, "external:", 9) == 0 && + !is_subarray(&e->metadata_version[9])) { + devname = devnum2devname(e->devnum); + /* update cmdline so this mdmon instance can be + * distinguished from others in a call to ps(1) + */ + if (strlen(devname) <= strlen(container_name)) { + memset(container_name, 0, strlen(container_name)); + sprintf(container_name, "%s", devname); + } + status |= mdmon(devname, e->devnum, 1, + switchroot); + } + } + free_mdstat(mdstat); + + return status; + } else if (strncmp(container_name, "md", 2) == 0) { + devnum = devname2devnum(container_name); + devname = devnum2devname(devnum); + if (strcmp(container_name, devname) != 0) + devname = NULL; + } else { + struct stat st; + + devnum = NoMdDev; + if (stat(container_name, &st) == 0) + devnum = stat2devnum(&st); + if (devnum == NoMdDev) + devname = NULL; + else + devname = devnum2devname(devnum); + } + + if (!devname) { + fprintf(stderr, "mdmon: %s is not a valid md device name\n", + container_name); + exit(1); + } + return mdmon(devname, devnum, do_fork(), switchroot); +} + +static int mdmon(char *devname, int devnum, int must_fork, char *switchroot) { int mdfd; struct mdinfo *mdi, *di; @@ -250,48 +335,77 @@ int main(int argc, char *argv[]) int pfd[2]; int status; int ignore; - char *container_name = NULL; - char *switchroot = NULL; + pid_t victim = -1; + int victim_sock = -1; - switch (argc) { - case 2: - container_name = argv[1]; - break; - case 4: - if (strcmp(argv[1], "--switch-root") != 0) { - fprintf(stderr, "mdmon: unknown argument %s\n", argv[1]); - usage(); + 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; } - switchroot = argv[2]; - container_name = argv[3]; - break; - default: - usage(); } - mdfd = open(container_name, O_RDWR); + mdfd = open_dev(devnum); if (mdfd < 0) { - fprintf(stderr, "mdmon: %s: %s\n", container_name, + fprintf(stderr, "mdmon: %s: %s\n", devname, strerror(errno)); - exit(1); + return 1; } if (md_get_version(mdfd) < 0) { fprintf(stderr, "mdmon: %s: Not an md device\n", - container_name); - exit(1); + devname); + return 1; } /* Fork, and have the child tell us when they are ready */ - if (do_fork()) { + if (must_fork) { if (pipe(pfd) != 0) { fprintf(stderr, "mdmon: failed to create pipe\n"); - exit(1); + return 1; } switch(fork()) { case -1: fprintf(stderr, "mdmon: failed to fork: %s\n", strerror(errno)); - exit(1); + return 1; case 0: /* child */ close(pfd[0]); break; @@ -301,16 +415,17 @@ int main(int argc, char *argv[]) wait(&status); status = WEXITSTATUS(status); } - exit(status); + return status; } } else pfd[0] = pfd[1] = -1; - container = malloc(sizeof(*container)); - container->devnum = fd2devnum(mdfd); - container->devname = devnum2devname(container->devnum); - container->device_name = container_name; + container = calloc(1, sizeof(*container)); + container->devnum = devnum; + container->devname = devname; container->arrays = NULL; + container->subarray[0] = 0; + container->sock = -1; if (!container->devname) { fprintf(stderr, "mdmon: failed to allocate container name string\n"); @@ -318,7 +433,7 @@ int main(int argc, char *argv[]) } mdi = sysfs_read(mdfd, container->devnum, - GET_VERSION|GET_LEVEL|GET_DEVS); + GET_VERSION|GET_LEVEL|GET_DEVS|SKIP_GONE_DEVS); if (!mdi) { fprintf(stderr, "mdmon: failed to load sysfs info for %s\n", @@ -327,20 +442,20 @@ int main(int argc, char *argv[]) } if (mdi->array.level != UnSet) { fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n", - container_name); + devname); exit(3); } if (mdi->array.major_version != -1 || mdi->array.minor_version != -2) { fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n", - container_name); + devname); exit(3); } container->ss = find_metadata_methods(mdi->text_version); if (container->ss == NULL) { fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n", - container_name, mdi->text_version); + devname, mdi->text_version); exit(3); } @@ -375,58 +490,33 @@ int main(int argc, char *argv[]) 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 + * the new root */ - 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 (switchroot[0] == '/') { + victim = mdmon_pid(container->devnum); + victim_sock = connect_monitor(container->devname); + } else { + victim = atoi(switchroot); + victim_sock = 0; } } - /* 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 (victim < 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); - } } + /* if there is a pid file, kill whoever is there just in case */ + victim = mdmon_pid(container->devnum); } - container->sock = make_control_sock(container->devname); - - if (container->ss->load_super(container, mdfd, container_name)) { + if (container->ss->load_super(container, mdfd, devname)) { fprintf(stderr, "mdmon: Cannot load metadata for %s\n", - container_name); + devname); exit(3); } + close(mdfd); /* Ok, this is close enough. We can say goodbye to our parent now. */ @@ -436,6 +526,19 @@ int main(int argc, char *argv[]) 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); @@ -446,14 +549,6 @@ int main(int argc, char *argv[]) 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); - } - do_manager(container); exit(0);