]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
2 * mdmon - monitor external metadata arrays
4 * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2008 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2, as published by the Free Software Foundation.
11 * This program is distributed in the hope it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 * You should have received a copy of the GNU General Public License along with
17 * this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23 * When md arrays have user-space managed metadata, this is the program
24 * that does the managing.
26 * Given one argument: the name of the array (e.g. /dev/md0) that is
28 * We fork off a helper that runs high priority and mlocked. It responds to
29 * device failures and other events that might stop writeout, or that are
30 * trivial to deal with.
31 * The main thread then watches for new arrays being created in the container
32 * and starts monitoring them too ... along with a few other tasks.
34 * The main thread communicates with the priority thread by writing over
36 * Separate programs can communicate with the main thread via Unix-domain
38 * The two threads share address space and open file table.
48 #include <sys/types.h>
50 #include <sys/socket.h>
53 #include <sys/syscall.h>
67 struct active_array
*discard_this
;
68 struct active_array
*pending_discard
;
74 int run_child(void *v
)
76 struct supertype
*c
= v
;
82 int clone_monitor(struct supertype
*container
)
84 static char stack
[4096];
86 mon_tid
= clone(run_child
, stack
+4096-64,
87 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
90 mgr_tid
= syscall(SYS_gettid
);
95 static struct superswitch
*find_metadata_methods(char *vers
)
97 if (strcmp(vers
, "ddf") == 0)
99 if (strcmp(vers
, "imsm") == 0)
105 int make_pidfile(char *devname
, int o_excl
)
115 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
117 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
120 sprintf(pid
, "%d\n", getpid());
121 n
= write(fd
, pid
, strlen(pid
));
128 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
130 if (mdstat
->metadata_version
== NULL
||
131 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
132 !is_subarray(mdstat
->metadata_version
+9) ||
133 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
134 mdstat
->metadata_version
[10+strlen(container
)] != '/')
140 void remove_pidfile(char *devname
);
141 static void try_kill_monitor(char *devname
)
146 struct mdstat_ent
*mdstat
;
148 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
149 fd
= open(buf
, O_RDONLY
);
153 if (read(fd
, buf
, sizeof(buf
)) < 0) {
159 pid
= strtoul(buf
, NULL
, 10);
161 /* first rule of survival... don't off yourself */
165 /* kill this process if it is mdmon */
166 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
167 fd
= open(buf
, O_RDONLY
);
171 if (read(fd
, buf
, sizeof(buf
)) < 0) {
176 if (!strstr(buf
, "mdmon"))
181 mdstat
= mdstat_read(0, 0);
182 for ( ; mdstat
; mdstat
= mdstat
->next
)
183 if (is_container_member(mdstat
, devname
)) {
184 sprintf(buf
, "/dev/%s", mdstat
->dev
);
188 remove_pidfile(devname
);
191 void remove_pidfile(char *devname
)
198 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
200 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
204 int make_control_sock(char *devname
)
209 struct sockaddr_un addr
;
214 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
216 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
220 addr
.sun_family
= PF_LOCAL
;
221 strcpy(addr
.sun_path
, path
);
222 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
227 fl
= fcntl(sfd
, F_GETFL
, 0);
229 fcntl(sfd
, F_SETFL
, fl
);
233 int socket_hup_requested
;
234 static void hup(int sig
)
236 socket_hup_requested
= 1;
239 static void term(int sig
)
244 static void wake_me(int sig
)
249 /* if we are debugging and starting mdmon by hand then don't fork */
250 static int do_fork(void)
253 if (check_env("MDADM_NO_MDMON"))
262 fprintf(stderr
, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
266 int main(int argc
, char *argv
[])
269 struct mdinfo
*mdi
, *di
;
270 struct supertype
*container
;
272 struct sigaction act
;
276 char *container_name
= NULL
;
277 char *switchroot
= NULL
;
281 container_name
= argv
[1];
284 if (strcmp(argv
[1], "--switch-root") != 0) {
285 fprintf(stderr
, "mdmon: unknown argument %s\n", argv
[1]);
288 switchroot
= argv
[2];
289 container_name
= argv
[3];
295 mdfd
= open(container_name
, O_RDWR
);
297 fprintf(stderr
, "mdmon: %s: %s\n", container_name
,
301 if (md_get_version(mdfd
) < 0) {
302 fprintf(stderr
, "mdmon: %s: Not an md device\n",
307 /* Fork, and have the child tell us when they are ready */
309 if (pipe(pfd
) != 0) {
310 fprintf(stderr
, "mdmon: failed to create pipe\n");
315 fprintf(stderr
, "mdmon: failed to fork: %s\n",
321 default: /* parent */
323 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
325 status
= WEXITSTATUS(status
);
330 pfd
[0] = pfd
[1] = -1;
332 container
= malloc(sizeof(*container
));
333 container
->devnum
= fd2devnum(mdfd
);
334 container
->devname
= devnum2devname(container
->devnum
);
335 container
->device_name
= container_name
;
336 container
->arrays
= NULL
;
338 if (!container
->devname
) {
339 fprintf(stderr
, "mdmon: failed to allocate container name string\n");
343 mdi
= sysfs_read(mdfd
, container
->devnum
,
344 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
347 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
351 if (mdi
->array
.level
!= UnSet
) {
352 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
356 if (mdi
->array
.major_version
!= -1 ||
357 mdi
->array
.minor_version
!= -2) {
358 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
363 container
->ss
= find_metadata_methods(mdi
->text_version
);
364 if (container
->ss
== NULL
) {
365 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
366 container_name
, mdi
->text_version
);
370 container
->devs
= NULL
;
371 for (di
= mdi
->devs
; di
; di
= di
->next
) {
372 struct mdinfo
*cd
= malloc(sizeof(*cd
));
374 cd
->next
= container
->devs
;
375 container
->devs
= cd
;
379 /* SIGUSR is sent between parent and child. So both block it
380 * and enable it only with pselect.
383 sigaddset(&set
, SIGUSR1
);
384 sigaddset(&set
, SIGHUP
);
385 sigaddset(&set
, SIGALRM
);
386 sigaddset(&set
, SIGTERM
);
387 sigprocmask(SIG_BLOCK
, &set
, NULL
);
388 act
.sa_handler
= wake_me
;
390 sigaction(SIGUSR1
, &act
, NULL
);
391 sigaction(SIGALRM
, &act
, NULL
);
392 act
.sa_handler
= hup
;
393 sigaction(SIGHUP
, &act
, NULL
);
394 act
.sa_handler
= term
;
395 sigaction(SIGTERM
, &act
, NULL
);
396 act
.sa_handler
= SIG_IGN
;
397 sigaction(SIGPIPE
, &act
, NULL
);
400 /* we assume we assume that /sys /proc /dev are available in
401 * the new root (see nash:setuproot)
403 * kill any monitors in the current namespace and change
406 try_kill_monitor(container
->devname
);
407 if (chroot(switchroot
) != 0) {
408 fprintf(stderr
, "mdmon: failed to chroot to '%s': %s\n",
409 switchroot
, strerror(errno
));
414 /* If this fails, we hope it already exists
415 * pid file lives in /var/run/mdadm/mdXX.pid
418 mkdir("/var/run", 0600);
419 mkdir("/var/run/mdadm", 0600);
421 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
422 if (ping_monitor(container
->devname
) == 0) {
423 fprintf(stderr
, "mdmon: %s already managed\n",
429 /* cleanup the old monitor, this one is taking over */
430 try_kill_monitor(container
->devname
);
431 err
= make_pidfile(container
->devname
, 0);
433 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
436 /* FIXME implement a mechanism to
437 * prevent duplicate monitor instances
440 "mdmon: continuing on read-only file system\n");
446 container
->sock
= make_control_sock(container
->devname
);
448 if (container
->ss
->load_super(container
, mdfd
, container_name
)) {
449 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
454 /* Ok, this is close enough. We can say goodbye to our parent now.
457 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
458 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
464 open("/dev/null", O_RDWR
);
472 mlockall(MCL_FUTURE
);
474 if (clone_monitor(container
) < 0) {
475 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
480 do_manager(container
);