mdadm/mdmon.c

594 lines
13 KiB
C
Raw Normal View History

/*
* mdmon - monitor external metadata arrays
*
* Copyright (C) 2007-2009 Neil Brown <neilb@suse.de>
* 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.
*/
2008-05-15 08:48:37 +02:00
/*
* md array manager.
* When md arrays have user-space managed metadata, this is the program
* that does the managing.
*
* Given one argument: the name of the array (e.g. /dev/md0) that is
* the container.
* We fork off a helper that runs high priority and mlocked. It responds to
* device failures and other events that might stop writeout, or that are
* trivial to deal with.
* The main thread then watches for new arrays being created in the container
* and starts monitoring them too ... along with a few other tasks.
*
* The main thread communicates with the priority thread by writing over
* a pipe.
* Separate programs can communicate with the main thread via Unix-domain
* socket.
* The two threads share address space and open file table.
*
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
2008-05-15 08:48:37 +02:00
#include <sys/stat.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/mman.h>
#include <sys/syscall.h>
#include <sys/wait.h>
2008-05-15 08:48:37 +02:00
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#ifdef USE_PTHREADS
#include <pthread.h>
#else
2008-05-15 08:48:37 +02:00
#include <sched.h>
#endif
2008-05-15 08:48:37 +02:00
#include "mdadm.h"
#include "mdmon.h"
char const Name[] = "mdmon";
2008-05-15 08:48:37 +02:00
struct active_array *discard_this;
struct active_array *pending_discard;
int mon_tid, mgr_tid;
2008-05-15 08:48:37 +02:00
int sigterm;
#ifdef USE_PTHREADS
static void *run_child(void *v)
{
struct supertype *c = v;
mon_tid = syscall(SYS_gettid);
do_monitor(c);
return 0;
}
static int clone_monitor(struct supertype *container)
{
pthread_attr_t attr;
pthread_t thread;
int rc;
mon_tid = -1;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 4096);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
rc = pthread_create(&thread, &attr, run_child, container);
if (rc)
return rc;
while (mon_tid == -1)
usleep(10);
pthread_attr_destroy(&attr);
mgr_tid = syscall(SYS_gettid);
return mon_tid;
}
#else /* USE_PTHREADS */
static int run_child(void *v)
2008-05-15 08:48:37 +02:00
{
struct supertype *c = v;
2008-05-15 08:48:37 +02:00
do_monitor(c);
return 0;
}
#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
static int clone_monitor(struct supertype *container)
2008-05-15 08:48:37 +02:00
{
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,
2008-05-15 08:48:37 +02:00
CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
container);
#endif
mgr_tid = syscall(SYS_gettid);
return mon_tid;
2008-05-15 08:48:37 +02:00
}
#endif /* USE_PTHREADS */
2008-05-15 08:48:37 +02:00
static int make_pidfile(char *devname)
2008-05-15 08:48:37 +02:00
{
char path[100];
char pid[10];
int fd;
trivial warn_unused_result squashing Made the mistake of recompiling the F9 mdadm rpm which has a patch to remove -Werror and add "-Wp,-D_FORTIFY_SOURCE -O2" which turns on lots of errors: config.c:568: warning: ignoring return value of asprintf Assemble.c:411: warning: ignoring return value of asprintf Assemble.c:413: warning: ignoring return value of asprintf super0.c:549: warning: ignoring return value of posix_memalign super0.c:742: warning: ignoring return value of posix_memalign super0.c:812: warning: ignoring return value of posix_memalign super1.c:692: warning: ignoring return value of posix_memalign super1.c:1039: warning: ignoring return value of posix_memalign super1.c:1155: warning: ignoring return value of posix_memalign super-ddf.c:508: warning: ignoring return value of posix_memalign super-ddf.c:645: warning: ignoring return value of posix_memalign super-ddf.c:696: warning: ignoring return value of posix_memalign super-ddf.c:715: warning: ignoring return value of posix_memalign super-ddf.c:1476: warning: ignoring return value of posix_memalign super-ddf.c:1603: warning: ignoring return value of posix_memalign super-ddf.c:1614: warning: ignoring return value of posix_memalign super-ddf.c:1842: warning: ignoring return value of posix_memalign super-ddf.c:2013: warning: ignoring return value of posix_memalign super-ddf.c:2140: warning: ignoring return value of write super-ddf.c:2143: warning: ignoring return value of write super-ddf.c:2147: warning: ignoring return value of write super-ddf.c:2150: warning: ignoring return value of write super-ddf.c:2162: warning: ignoring return value of write super-ddf.c:2169: warning: ignoring return value of write super-ddf.c:2172: warning: ignoring return value of write super-ddf.c:2176: warning: ignoring return value of write super-ddf.c:2181: warning: ignoring return value of write super-ddf.c:2686: warning: ignoring return value of posix_memalign super-ddf.c:2690: warning: ignoring return value of write super-ddf.c:3070: warning: ignoring return value of posix_memalign super-ddf.c:3254: warning: ignoring return value of posix_memalign bitmap.c:128: warning: ignoring return value of posix_memalign mdmon.c:94: warning: ignoring return value of write mdmon.c:221: warning: ignoring return value of pipe mdmon.c:327: warning: ignoring return value of write mdmon.c:330: warning: ignoring return value of chdir mdmon.c:335: warning: ignoring return value of dup monitor.c:415: warning: rv may be used uninitialized in this function ...some of these like the write() ones are not so trivial so save those fixes for the next patch. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2008-09-28 21:12:07 +02:00
int n;
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
if (mkdir(MDMON_DIR, 0755) < 0 &&
errno != EEXIST)
return -errno;
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
sprintf(path, "%s/%s.pid", MDMON_DIR, devname);
2008-05-15 08:48:37 +02:00
fd = open(path, O_RDWR|O_CREAT|O_EXCL, 0600);
2008-05-15 08:48:37 +02:00
if (fd < 0)
return -errno;
2008-05-15 08:48:37 +02:00
sprintf(pid, "%d\n", getpid());
trivial warn_unused_result squashing Made the mistake of recompiling the F9 mdadm rpm which has a patch to remove -Werror and add "-Wp,-D_FORTIFY_SOURCE -O2" which turns on lots of errors: config.c:568: warning: ignoring return value of asprintf Assemble.c:411: warning: ignoring return value of asprintf Assemble.c:413: warning: ignoring return value of asprintf super0.c:549: warning: ignoring return value of posix_memalign super0.c:742: warning: ignoring return value of posix_memalign super0.c:812: warning: ignoring return value of posix_memalign super1.c:692: warning: ignoring return value of posix_memalign super1.c:1039: warning: ignoring return value of posix_memalign super1.c:1155: warning: ignoring return value of posix_memalign super-ddf.c:508: warning: ignoring return value of posix_memalign super-ddf.c:645: warning: ignoring return value of posix_memalign super-ddf.c:696: warning: ignoring return value of posix_memalign super-ddf.c:715: warning: ignoring return value of posix_memalign super-ddf.c:1476: warning: ignoring return value of posix_memalign super-ddf.c:1603: warning: ignoring return value of posix_memalign super-ddf.c:1614: warning: ignoring return value of posix_memalign super-ddf.c:1842: warning: ignoring return value of posix_memalign super-ddf.c:2013: warning: ignoring return value of posix_memalign super-ddf.c:2140: warning: ignoring return value of write super-ddf.c:2143: warning: ignoring return value of write super-ddf.c:2147: warning: ignoring return value of write super-ddf.c:2150: warning: ignoring return value of write super-ddf.c:2162: warning: ignoring return value of write super-ddf.c:2169: warning: ignoring return value of write super-ddf.c:2172: warning: ignoring return value of write super-ddf.c:2176: warning: ignoring return value of write super-ddf.c:2181: warning: ignoring return value of write super-ddf.c:2686: warning: ignoring return value of posix_memalign super-ddf.c:2690: warning: ignoring return value of write super-ddf.c:3070: warning: ignoring return value of posix_memalign super-ddf.c:3254: warning: ignoring return value of posix_memalign bitmap.c:128: warning: ignoring return value of posix_memalign mdmon.c:94: warning: ignoring return value of write mdmon.c:221: warning: ignoring return value of pipe mdmon.c:327: warning: ignoring return value of write mdmon.c:330: warning: ignoring return value of chdir mdmon.c:335: warning: ignoring return value of dup monitor.c:415: warning: rv may be used uninitialized in this function ...some of these like the write() ones are not so trivial so save those fixes for the next patch. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2008-09-28 21:12:07 +02:00
n = write(fd, pid, strlen(pid));
2008-05-15 08:48:37 +02:00
close(fd);
trivial warn_unused_result squashing Made the mistake of recompiling the F9 mdadm rpm which has a patch to remove -Werror and add "-Wp,-D_FORTIFY_SOURCE -O2" which turns on lots of errors: config.c:568: warning: ignoring return value of asprintf Assemble.c:411: warning: ignoring return value of asprintf Assemble.c:413: warning: ignoring return value of asprintf super0.c:549: warning: ignoring return value of posix_memalign super0.c:742: warning: ignoring return value of posix_memalign super0.c:812: warning: ignoring return value of posix_memalign super1.c:692: warning: ignoring return value of posix_memalign super1.c:1039: warning: ignoring return value of posix_memalign super1.c:1155: warning: ignoring return value of posix_memalign super-ddf.c:508: warning: ignoring return value of posix_memalign super-ddf.c:645: warning: ignoring return value of posix_memalign super-ddf.c:696: warning: ignoring return value of posix_memalign super-ddf.c:715: warning: ignoring return value of posix_memalign super-ddf.c:1476: warning: ignoring return value of posix_memalign super-ddf.c:1603: warning: ignoring return value of posix_memalign super-ddf.c:1614: warning: ignoring return value of posix_memalign super-ddf.c:1842: warning: ignoring return value of posix_memalign super-ddf.c:2013: warning: ignoring return value of posix_memalign super-ddf.c:2140: warning: ignoring return value of write super-ddf.c:2143: warning: ignoring return value of write super-ddf.c:2147: warning: ignoring return value of write super-ddf.c:2150: warning: ignoring return value of write super-ddf.c:2162: warning: ignoring return value of write super-ddf.c:2169: warning: ignoring return value of write super-ddf.c:2172: warning: ignoring return value of write super-ddf.c:2176: warning: ignoring return value of write super-ddf.c:2181: warning: ignoring return value of write super-ddf.c:2686: warning: ignoring return value of posix_memalign super-ddf.c:2690: warning: ignoring return value of write super-ddf.c:3070: warning: ignoring return value of posix_memalign super-ddf.c:3254: warning: ignoring return value of posix_memalign bitmap.c:128: warning: ignoring return value of posix_memalign mdmon.c:94: warning: ignoring return value of write mdmon.c:221: warning: ignoring return value of pipe mdmon.c:327: warning: ignoring return value of write mdmon.c:330: warning: ignoring return value of chdir mdmon.c:335: warning: ignoring return value of dup monitor.c:415: warning: rv may be used uninitialized in this function ...some of these like the write() ones are not so trivial so save those fixes for the next patch. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2008-09-28 21:12:07 +02:00
if (n < 0)
return -errno;
2008-05-15 08:48:37 +02:00
return 0;
}
static void try_kill_monitor(pid_t pid, char *devname, int sock)
{
char buf[100];
int fd;
int n;
long fl;
int rv;
/* first rule of survival... don't off yourself */
if (pid == getpid())
return;
/* kill this process if it is mdmon */
sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
fd = open(buf, O_RDONLY);
if (fd < 0)
return;
n = read(fd, buf, sizeof(buf)-1);
buf[sizeof(buf)-1] = 0;
close(fd);
if (n < 0 || !(strstr(buf, "mdmon") ||
strstr(buf, "@dmon")))
return;
kill(pid, SIGTERM);
if (sock < 0)
return;
/* 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);
n = read(sock, buf, 100);
/* If there is I/O going on it might took some time to get to
* clean state. Wait for monitor to exit fully to avoid races.
* Ping it with SIGUSR1 in case that it is sleeping */
for (n = 0; n < 25; n++) {
rv = kill(pid, SIGUSR1);
if (rv < 0)
break;
usleep(200000);
}
}
void remove_pidfile(char *devname)
{
char buf[100];
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
sprintf(buf, "%s/%s.pid", MDMON_DIR, devname);
unlink(buf);
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
sprintf(buf, "%s/%s.sock", MDMON_DIR, devname);
unlink(buf);
}
static int make_control_sock(char *devname)
2008-05-15 08:48:37 +02:00
{
char path[100];
int sfd;
long fl;
struct sockaddr_un addr;
if (sigterm)
return -1;
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
sprintf(path, "%s/%s.sock", MDMON_DIR, devname);
2008-05-15 08:48:37 +02:00
unlink(path);
sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (sfd < 0)
return -1;
addr.sun_family = PF_LOCAL;
strcpy(addr.sun_path, path);
umask(077); /* ensure no world write access */
if (bind(sfd, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
2008-05-15 08:48:37 +02:00
close(sfd);
return -1;
}
listen(sfd, 10);
fl = fcntl(sfd, F_GETFL, 0);
fl |= O_NONBLOCK;
fcntl(sfd, F_SETFL, fl);
return sfd;
}
static void term(int sig)
{
sigterm = 1;
}
static void wake_me(int sig)
{
}
2008-07-25 02:26:24 +02:00
/* if we are debugging and starting mdmon by hand then don't fork */
static int do_fork(void)
{
#ifdef DEBUG
if (check_env("MDADM_NO_MDMON"))
2008-07-25 02:26:24 +02:00
return 0;
#endif
return 1;
}
void usage(void)
{
fprintf(stderr,
"Usage: mdmon [options] CONTAINER\n"
"\n"
"Options are:\n"
" --help -h : This message\n"
" --all -a : All devices\n"
" --foreground -F : Run in foreground (do not fork)\n"
" --takeover -t : Takeover container\n"
);
exit(2);
}
2008-07-25 02:26:24 +02:00
static int mdmon(char *devnm, int must_fork, int takeover);
2008-05-15 08:48:37 +02:00
int main(int argc, char *argv[])
{
char *container_name = NULL;
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}
};
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':
takeover = 1;
break;
case 'F':
dofork = 0;
break;
case OffRootOpt:
argv[0][0] = '@';
break;
case 'h':
default:
usage();
break;
}
}
if (all == 0 && container_name == NULL) {
if (argv[optind])
container_name = argv[optind];
2008-05-15 08:48:37 +02:00
}
if (container_name == NULL)
usage();
if (argc - optind > 1)
usage();
if (strcmp(container_name, "/proc/mdstat") == 0)
all = 1;
if (all) {
struct mdstat_ent *mdstat, *e;
int container_len = strlen(container_name);
/* launch an mdmon instance for each container found */
mdstat = mdstat_read(0, 0);
for (e = mdstat; e; e = e->next) {
if (e->metadata_version &&
strncmp(e->metadata_version, "external:", 9) == 0 &&
!is_subarray(&e->metadata_version[9])) {
/* update cmdline so this mdmon instance can be
* distinguished from others in a call to ps(1)
*/
if (strlen(e->devnm) <= (unsigned)container_len) {
memset(container_name, 0, container_len);
sprintf(container_name, "%s", e->devnm);
}
status |= mdmon(e->devnm, 1, takeover);
}
}
free_mdstat(mdstat);
return status;
} else if (strncmp(container_name, "md", 2) == 0) {
int id = devnm2devid(container_name);
if (id)
devnm = container_name;
} else {
struct stat st;
if (stat(container_name, &st) == 0)
devnm = xstrdup(stat2devnm(&st));
}
if (!devnm) {
pr_err("%s is not a valid md device name\n",
container_name);
exit(1);
}
return mdmon(devnm, dofork && do_fork(), takeover);
}
static int mdmon(char *devnm, int must_fork, int takeover)
{
int mdfd;
struct mdinfo *mdi, *di;
struct supertype *container;
sigset_t set;
struct sigaction act;
int pfd[2];
int status;
int ignore;
pid_t victim = -1;
int victim_sock = -1;
dprintf("starting mdmon for %s\n", devnm);
mdfd = open_dev(devnm);
2008-05-15 08:48:37 +02:00
if (mdfd < 0) {
pr_err("%s: %s\n", devnm, strerror(errno));
return 1;
2008-05-15 08:48:37 +02:00
}
/* Fork, and have the child tell us when they are ready */
if (must_fork) {
trivial warn_unused_result squashing Made the mistake of recompiling the F9 mdadm rpm which has a patch to remove -Werror and add "-Wp,-D_FORTIFY_SOURCE -O2" which turns on lots of errors: config.c:568: warning: ignoring return value of asprintf Assemble.c:411: warning: ignoring return value of asprintf Assemble.c:413: warning: ignoring return value of asprintf super0.c:549: warning: ignoring return value of posix_memalign super0.c:742: warning: ignoring return value of posix_memalign super0.c:812: warning: ignoring return value of posix_memalign super1.c:692: warning: ignoring return value of posix_memalign super1.c:1039: warning: ignoring return value of posix_memalign super1.c:1155: warning: ignoring return value of posix_memalign super-ddf.c:508: warning: ignoring return value of posix_memalign super-ddf.c:645: warning: ignoring return value of posix_memalign super-ddf.c:696: warning: ignoring return value of posix_memalign super-ddf.c:715: warning: ignoring return value of posix_memalign super-ddf.c:1476: warning: ignoring return value of posix_memalign super-ddf.c:1603: warning: ignoring return value of posix_memalign super-ddf.c:1614: warning: ignoring return value of posix_memalign super-ddf.c:1842: warning: ignoring return value of posix_memalign super-ddf.c:2013: warning: ignoring return value of posix_memalign super-ddf.c:2140: warning: ignoring return value of write super-ddf.c:2143: warning: ignoring return value of write super-ddf.c:2147: warning: ignoring return value of write super-ddf.c:2150: warning: ignoring return value of write super-ddf.c:2162: warning: ignoring return value of write super-ddf.c:2169: warning: ignoring return value of write super-ddf.c:2172: warning: ignoring return value of write super-ddf.c:2176: warning: ignoring return value of write super-ddf.c:2181: warning: ignoring return value of write super-ddf.c:2686: warning: ignoring return value of posix_memalign super-ddf.c:2690: warning: ignoring return value of write super-ddf.c:3070: warning: ignoring return value of posix_memalign super-ddf.c:3254: warning: ignoring return value of posix_memalign bitmap.c:128: warning: ignoring return value of posix_memalign mdmon.c:94: warning: ignoring return value of write mdmon.c:221: warning: ignoring return value of pipe mdmon.c:327: warning: ignoring return value of write mdmon.c:330: warning: ignoring return value of chdir mdmon.c:335: warning: ignoring return value of dup monitor.c:415: warning: rv may be used uninitialized in this function ...some of these like the write() ones are not so trivial so save those fixes for the next patch. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2008-09-28 21:12:07 +02:00
if (pipe(pfd) != 0) {
pr_err("failed to create pipe\n");
return 1;
trivial warn_unused_result squashing Made the mistake of recompiling the F9 mdadm rpm which has a patch to remove -Werror and add "-Wp,-D_FORTIFY_SOURCE -O2" which turns on lots of errors: config.c:568: warning: ignoring return value of asprintf Assemble.c:411: warning: ignoring return value of asprintf Assemble.c:413: warning: ignoring return value of asprintf super0.c:549: warning: ignoring return value of posix_memalign super0.c:742: warning: ignoring return value of posix_memalign super0.c:812: warning: ignoring return value of posix_memalign super1.c:692: warning: ignoring return value of posix_memalign super1.c:1039: warning: ignoring return value of posix_memalign super1.c:1155: warning: ignoring return value of posix_memalign super-ddf.c:508: warning: ignoring return value of posix_memalign super-ddf.c:645: warning: ignoring return value of posix_memalign super-ddf.c:696: warning: ignoring return value of posix_memalign super-ddf.c:715: warning: ignoring return value of posix_memalign super-ddf.c:1476: warning: ignoring return value of posix_memalign super-ddf.c:1603: warning: ignoring return value of posix_memalign super-ddf.c:1614: warning: ignoring return value of posix_memalign super-ddf.c:1842: warning: ignoring return value of posix_memalign super-ddf.c:2013: warning: ignoring return value of posix_memalign super-ddf.c:2140: warning: ignoring return value of write super-ddf.c:2143: warning: ignoring return value of write super-ddf.c:2147: warning: ignoring return value of write super-ddf.c:2150: warning: ignoring return value of write super-ddf.c:2162: warning: ignoring return value of write super-ddf.c:2169: warning: ignoring return value of write super-ddf.c:2172: warning: ignoring return value of write super-ddf.c:2176: warning: ignoring return value of write super-ddf.c:2181: warning: ignoring return value of write super-ddf.c:2686: warning: ignoring return value of posix_memalign super-ddf.c:2690: warning: ignoring return value of write super-ddf.c:3070: warning: ignoring return value of posix_memalign super-ddf.c:3254: warning: ignoring return value of posix_memalign bitmap.c:128: warning: ignoring return value of posix_memalign mdmon.c:94: warning: ignoring return value of write mdmon.c:221: warning: ignoring return value of pipe mdmon.c:327: warning: ignoring return value of write mdmon.c:330: warning: ignoring return value of chdir mdmon.c:335: warning: ignoring return value of dup monitor.c:415: warning: rv may be used uninitialized in this function ...some of these like the write() ones are not so trivial so save those fixes for the next patch. Signed-off-by: Dan Williams <dan.j.williams@intel.com>
2008-09-28 21:12:07 +02:00
}
2008-07-25 02:26:24 +02:00
switch(fork()) {
case -1:
pr_err("failed to fork: %s\n", strerror(errno));
return 1;
2008-07-25 02:26:24 +02:00
case 0: /* child */
close(pfd[0]);
break;
default: /* parent */
close(pfd[1]);
if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) {
wait(&status);
status = WEXITSTATUS(status);
}
close(pfd[0]);
return status;
}
2008-07-25 02:26:24 +02:00
} else
pfd[0] = pfd[1] = -1;
2008-05-15 08:48:37 +02:00
container = xcalloc(1, sizeof(*container));
strcpy(container->devnm, devnm);
container->arrays = NULL;
container->sock = -1;
mdi = sysfs_read(mdfd, container->devnm, GET_VERSION|GET_LEVEL|GET_DEVS);
if (!mdi) {
pr_err("failed to load sysfs info for %s\n", container->devnm);
exit(3);
}
if (mdi->array.level != UnSet) {
pr_err("%s is not a container - cannot monitor\n", devnm);
exit(3);
}
if (mdi->array.major_version != -1 ||
mdi->array.minor_version != -2) {
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) {
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 = xmalloc(sizeof(*cd));
*cd = *di;
cd->next = container->devs;
container->devs = cd;
}
sysfs_free(mdi);
2008-05-15 08:48:37 +02:00
/* SIGUSR is sent between parent and child. So both block it
* and enable it only with pselect.
*/
sigemptyset(&set);
sigaddset(&set, SIGUSR1);
sigaddset(&set, SIGTERM);
sigprocmask(SIG_BLOCK, &set, NULL);
act.sa_handler = wake_me;
act.sa_flags = 0;
sigaction(SIGUSR1, &act, NULL);
act.sa_handler = term;
sigaction(SIGTERM, &act, NULL);
act.sa_handler = SIG_IGN;
sigaction(SIGPIPE, &act, NULL);
victim = mdmon_pid(container->devnm);
if (victim >= 0)
victim_sock = connect_monitor(container->devnm);
ignore = chdir("/");
if (!takeover && victim > 0 && victim_sock >= 0) {
if (fping_monitor(victim_sock) == 0) {
pr_err("%s already managed\n", container->devnm);
exit(3);
}
close(victim_sock);
victim_sock = -1;
2008-05-15 08:48:37 +02:00
}
if (container->ss->load_container(container, mdfd, devnm)) {
pr_err("Cannot load metadata for %s\n", devnm);
2008-05-15 08:48:37 +02:00
exit(3);
}
close(mdfd);
2008-05-15 08:48:37 +02:00
/* Ok, this is close enough. We can say goodbye to our parent now.
*/
if (victim > 0)
remove_pidfile(devnm);
if (make_pidfile(devnm) < 0) {
Fix all the confusion over directories once and for all. We now have 3 directory definitions: mdmon directory for its pid and sock files (compile time define, not changable at run time), mdmonitor directory which is for the mdadm monitor mode pid file (can only be passed in via command line at the time mdadm is invoked in monitor mode), and the directory for the mdadm incremental assembly map file (compile time define, not changable at run time). Only the mdadm map file still hunts multiple locations, and the number of locations has been reduced to /var/run and the compile time specified location. Re-use of similar sounding defines that actually didn't denote their actual usage at compile time made it more difficult for a person to know what affect changing the compile time defines would have on the resulting programs. This patch renames the various defines to clearly identify which item the define affects. It also reduces the number of various directories which will be searched for these files as this has lead to confusion in mdadm and mdmon in terms of which files should take precedence when files exist in multiple locations, etc. It's best if the person compiling the program intentionally and with planning selects the right directories to be used for the various purposes. Which directory is right depends on which items you are talking about and what boot loader your system uses and what initramfs generation program your system uses. Because of the inter-dependency of all these items it would typically be up to the distribution that mdadm is being integrated into to select the correct values for these defines. Signed-off-by: Doug Ledford <dledford@redhat.com>
2010-07-20 18:15:37 +02:00
exit(3);
}
container->sock = make_control_sock(devnm);
status = 0;
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);
2008-05-15 08:48:37 +02:00
if (clone_monitor(container) < 0) {
pr_err("failed to start monitor process: %s\n",
2008-05-15 08:48:37 +02:00
strerror(errno));
exit(2);
}
if (victim > 0) {
try_kill_monitor(victim, container->devnm, victim_sock);
if (victim_sock >= 0)
close(victim_sock);
}
setsid();
manage_fork_fds(0);
/* This silliness is to stop the compiler complaining
* that we ignore 'ignore'
*/
if (ignore)
ignore++;
2008-05-15 08:48:37 +02:00
do_manager(container);
exit(0);
}
/* Some stub functions so super-* can link with us */
int child_monitor(int afd, struct mdinfo *sra, struct reshape *reshape,
struct supertype *st, unsigned long blocks,
int *fds, unsigned long long *offsets,
int dests, int *destfd, unsigned long long *destoffsets)
{
return 0;
}
int restore_stripes(int *dest, unsigned long long *offsets,
int raid_disks, int chunk_size, int level, int layout,
int source, unsigned long long read_offset,
unsigned long long start, unsigned long long length,
char *src_buf)
{
return 1;
}
int save_stripes(int *source, unsigned long long *offsets,
int raid_disks, int chunk_size, int level, int layout,
int nwrites, int *dest,
unsigned long long start, unsigned long long length,
char *buf)
{
return 0;
}
struct superswitch super0 = {
.name = "0.90",
};
struct superswitch super1 = {
.name = "1.x",
};