#include "mdadm.h"
#include "mdmon.h"
+char const Name[] = "mdmon";
+
struct active_array *discard_this;
struct active_array *pending_discard;
buf[sizeof(buf)-1] = 0;
close(fd);
- if (n < 0 || !strstr(buf, "mdmon"))
+ if (n < 0 || !(strstr(buf, "mdmon") ||
+ strstr(buf, "@dmon")))
return;
kill(pid, SIGTERM);
fcntl(sock, F_SETFL, fl);
n = read(sock, buf, 100);
/* Ignore result, it is just the wait that
- * matters
+ * matters
*/
}
addr.sun_family = PF_LOCAL;
strcpy(addr.sun_path, path);
- if (bind(sfd, &addr, sizeof(addr)) < 0) {
+ umask(077); /* ensure no world write access */
+ if (bind(sfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
close(sfd);
return -1;
}
"\n"
"Options are:\n"
" --help -h : This message\n"
-" --all : All devices\n"
+" --all -a : All devices\n"
+" --foreground -F : Run in foreground (do not fork)\n"
" --takeover -t : Takeover container\n"
-" --offroot : Set first character of argv[0] to @ to indicate the\n"
-" application was launched from initrd/initramfs and\n"
-" should not be shutdown by systemd as part of the\n"
-" regular shutdown process.\n"
);
exit(2);
}
-static int mdmon(char *devname, int devnum, int must_fork, int takeover);
+static int mdmon(char *devnm, int must_fork, int takeover);
int main(int argc, char *argv[])
{
char *container_name = NULL;
- int devnum;
- char *devname;
+ char *devnm = NULL;
int status = 0;
int opt;
int all = 0;
int takeover = 0;
+ int dofork = 1;
static struct option options[] = {
{"all", 0, NULL, 'a'},
{"takeover", 0, NULL, 't'},
{"help", 0, NULL, 'h'},
{"offroot", 0, NULL, OffRootOpt},
+ {"foreground", 0, NULL, 'F'},
{NULL, 0, NULL, 0}
};
- while ((opt = getopt_long(argc, argv, "th", options, NULL)) != -1) {
+ if (in_initrd()) {
+ /*
+ * set first char of argv[0] to @. This is used by
+ * systemd to signal that the task was launched from
+ * initrd/initramfs and should be preserved during shutdown
+ */
+ argv[0][0] = '@';
+ }
+
+ while ((opt = getopt_long(argc, argv, "thaF", options, NULL)) != -1) {
switch (opt) {
case 'a':
container_name = argv[optind-1];
all = 1;
break;
case 't':
- container_name = optarg;
takeover = 1;
break;
+ case 'F':
+ dofork = 0;
+ break;
case OffRootOpt:
argv[0][0] = '@';
break;
if (e->metadata_version &&
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) <= (unsigned)container_len) {
+ if (strlen(e->devnm) <= (unsigned)container_len) {
memset(container_name, 0, container_len);
- sprintf(container_name, "%s", devname);
+ sprintf(container_name, "%s", e->devnm);
}
- status |= mdmon(devname, e->devnum, 1,
- takeover);
+ status |= mdmon(e->devnm, 1, takeover);
}
}
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;
+ int id = devnm2devid(container_name);
+ if (id)
+ devnm = container_name;
} else {
struct stat st;
- devnum = NoMdDev;
if (stat(container_name, &st) == 0)
- devnum = stat2devnum(&st);
- if (devnum == NoMdDev)
- devname = NULL;
- else
- devname = devnum2devname(devnum);
+ devnm = xstrdup(stat2devnm(&st));
}
- if (!devname) {
- fprintf(stderr, "mdmon: %s is not a valid md device name\n",
+ if (!devnm) {
+ pr_err("%s is not a valid md device name\n",
container_name);
exit(1);
}
- return mdmon(devname, devnum, do_fork(), takeover);
+ return mdmon(devnm, dofork && do_fork(), takeover);
}
-static int mdmon(char *devname, int devnum, int must_fork, int takeover)
+static int mdmon(char *devnm, int must_fork, int takeover)
{
int mdfd;
struct mdinfo *mdi, *di;
pid_t victim = -1;
int victim_sock = -1;
- dprintf("starting mdmon for %s\n", devname);
+ dprintf("starting mdmon for %s\n", devnm);
- mdfd = open_dev(devnum);
+ mdfd = open_dev(devnm);
if (mdfd < 0) {
- fprintf(stderr, "mdmon: %s: %s\n", devname,
- strerror(errno));
+ pr_err("%s: %s\n", devnm, strerror(errno));
return 1;
}
if (md_get_version(mdfd) < 0) {
- fprintf(stderr, "mdmon: %s: Not an md device\n",
- devname);
+ pr_err("%s: Not an md device\n", devnm);
return 1;
}
/* Fork, and have the child tell us when they are ready */
if (must_fork) {
if (pipe(pfd) != 0) {
- fprintf(stderr, "mdmon: failed to create pipe\n");
+ pr_err("failed to create pipe\n");
return 1;
}
switch(fork()) {
case -1:
- fprintf(stderr, "mdmon: failed to fork: %s\n",
- strerror(errno));
+ pr_err("failed to fork: %s\n", strerror(errno));
return 1;
case 0: /* child */
close(pfd[0]);
wait(&status);
status = WEXITSTATUS(status);
}
+ close(pfd[0]);
return status;
}
} else
pfd[0] = pfd[1] = -1;
- container = calloc(1, sizeof(*container));
- container->devnum = devnum;
- container->devname = devname;
+ container = xcalloc(1, sizeof(*container));
+ strcpy(container->devnm, devnm);
container->arrays = NULL;
container->sock = -1;
- if (!container->devname) {
- fprintf(stderr, "mdmon: failed to allocate container name string\n");
- exit(3);
- }
-
- mdi = sysfs_read(mdfd, container->devnum, GET_VERSION|GET_LEVEL|GET_DEVS);
+ mdi = sysfs_read(mdfd, container->devnm, GET_VERSION|GET_LEVEL|GET_DEVS);
if (!mdi) {
- fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
- container->devname);
+ pr_err("failed to load sysfs info for %s\n", container->devnm);
exit(3);
}
if (mdi->array.level != UnSet) {
- fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
- devname);
+ pr_err("%s is not a container - cannot monitor\n", devnm);
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",
- devname);
+ pr_err("%s does not use external metadata - cannot monitor\n",
+ devnm);
exit(3);
}
container->ss = version_to_superswitch(mdi->text_version);
if (container->ss == NULL) {
- fprintf(stderr, "mdmon: %s uses unsupported metadata: %s\n",
- devname, mdi->text_version);
+ pr_err("%s uses unsupported metadata: %s\n",
+ devnm, mdi->text_version);
exit(3);
}
container->devs = NULL;
for (di = mdi->devs; di; di = di->next) {
- struct mdinfo *cd = malloc(sizeof(*cd));
+ struct mdinfo *cd = xmalloc(sizeof(*cd));
*cd = *di;
cd->next = container->devs;
container->devs = cd;
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
- victim = mdmon_pid(container->devnum);
+ victim = mdmon_pid(container->devnm);
if (victim >= 0)
- victim_sock = connect_monitor(container->devname);
+ victim_sock = connect_monitor(container->devnm);
ignore = chdir("/");
if (!takeover && victim > 0 && victim_sock >= 0) {
if (fping_monitor(victim_sock) == 0) {
- fprintf(stderr, "mdmon: %s already managed\n",
- container->devname);
+ pr_err("%s already managed\n", container->devnm);
exit(3);
}
close(victim_sock);
victim_sock = -1;
}
- if (container->ss->load_container(container, mdfd, devname)) {
- fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
- devname);
+ if (container->ss->load_container(container, mdfd, devnm)) {
+ pr_err("Cannot load metadata for %s\n", devnm);
exit(3);
}
close(mdfd);
/* Ok, this is close enough. We can say goodbye to our parent now.
*/
if (victim > 0)
- remove_pidfile(devname);
- if (make_pidfile(devname) < 0) {
+ remove_pidfile(devnm);
+ if (make_pidfile(devnm) < 0) {
exit(3);
}
- container->sock = make_control_sock(devname);
+ container->sock = make_control_sock(devnm);
status = 0;
- if (write(pfd[1], &status, sizeof(status)) < 0)
- fprintf(stderr, "mdmon: failed to notify our parent: %d\n",
- getppid());
- close(pfd[1]);
+ if (pfd[1] >= 0) {
+ if (write(pfd[1], &status, sizeof(status)) < 0)
+ pr_err("failed to notify our parent: %d\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",
+ pr_err("failed to start monitor process: %s\n",
strerror(errno));
exit(2);
}
if (victim > 0) {
- try_kill_monitor(victim, container->devname, victim_sock);
+ try_kill_monitor(victim, container->devnm, victim_sock);
if (victim_sock >= 0)
close(victim_sock);
}
{
return 0;
}
+
+struct superswitch super0 = {
+ .name = "0.90",
+};
+struct superswitch super1 = {
+ .name = "1.x",
+};