]>
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
);
96 int make_pidfile(char *devname
, int o_excl
)
106 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
108 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
111 sprintf(pid
, "%d\n", getpid());
112 n
= write(fd
, pid
, strlen(pid
));
119 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
121 if (mdstat
->metadata_version
== NULL
||
122 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
123 !is_subarray(mdstat
->metadata_version
+9) ||
124 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
125 mdstat
->metadata_version
[10+strlen(container
)] != '/')
131 void remove_pidfile(char *devname
);
132 static void try_kill_monitor(char *devname
)
137 struct mdstat_ent
*mdstat
;
139 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
140 fd
= open(buf
, O_RDONLY
);
144 if (read(fd
, buf
, sizeof(buf
)) < 0) {
150 pid
= strtoul(buf
, NULL
, 10);
152 /* first rule of survival... don't off yourself */
156 /* kill this process if it is mdmon */
157 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
158 fd
= open(buf
, O_RDONLY
);
162 if (read(fd
, buf
, sizeof(buf
)) < 0) {
167 if (!strstr(buf
, "mdmon"))
172 mdstat
= mdstat_read(0, 0);
173 for ( ; mdstat
; mdstat
= mdstat
->next
)
174 if (is_container_member(mdstat
, devname
)) {
175 sprintf(buf
, "/dev/%s", mdstat
->dev
);
179 remove_pidfile(devname
);
182 void remove_pidfile(char *devname
)
189 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
191 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
195 int make_control_sock(char *devname
)
200 struct sockaddr_un addr
;
205 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
207 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
211 addr
.sun_family
= PF_LOCAL
;
212 strcpy(addr
.sun_path
, path
);
213 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
218 fl
= fcntl(sfd
, F_GETFL
, 0);
220 fcntl(sfd
, F_SETFL
, fl
);
224 int socket_hup_requested
;
225 static void hup(int sig
)
227 socket_hup_requested
= 1;
230 static void term(int sig
)
235 static void wake_me(int sig
)
240 /* if we are debugging and starting mdmon by hand then don't fork */
241 static int do_fork(void)
253 fprintf(stderr
, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
257 int main(int argc
, char *argv
[])
260 struct mdinfo
*mdi
, *di
;
261 struct supertype
*container
;
263 struct sigaction act
;
267 char *container_name
= NULL
;
268 char *switchroot
= NULL
;
272 container_name
= argv
[1];
275 if (strcmp(argv
[1], "--switch-root") != 0) {
276 fprintf(stderr
, "mdmon: unknown argument %s\n", argv
[1]);
279 switchroot
= argv
[2];
280 container_name
= argv
[3];
286 mdfd
= open(container_name
, O_RDWR
);
288 fprintf(stderr
, "mdmon: %s: %s\n", container_name
,
292 if (md_get_version(mdfd
) < 0) {
293 fprintf(stderr
, "mdmon: %s: Not an md device\n",
298 /* Fork, and have the child tell us when they are ready */
300 if (pipe(pfd
) != 0) {
301 fprintf(stderr
, "mdmon: failed to create pipe\n");
306 fprintf(stderr
, "mdmon: failed to fork: %s\n",
312 default: /* parent */
314 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
316 status
= WEXITSTATUS(status
);
321 pfd
[0] = pfd
[1] = -1;
323 container
= malloc(sizeof(*container
));
324 container
->devnum
= fd2devnum(mdfd
);
325 container
->devname
= devnum2devname(container
->devnum
);
326 container
->device_name
= container_name
;
327 container
->arrays
= NULL
;
329 if (!container
->devname
) {
330 fprintf(stderr
, "mdmon: failed to allocate container name string\n");
334 mdi
= sysfs_read(mdfd
, container
->devnum
,
335 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
338 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
342 if (mdi
->array
.level
!= UnSet
) {
343 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
347 if (mdi
->array
.major_version
!= -1 ||
348 mdi
->array
.minor_version
!= -2) {
349 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
354 container
->ss
= find_metadata_methods(mdi
->text_version
);
355 if (container
->ss
== NULL
) {
356 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
357 container_name
, mdi
->text_version
);
361 container
->devs
= NULL
;
362 for (di
= mdi
->devs
; di
; di
= di
->next
) {
363 struct mdinfo
*cd
= malloc(sizeof(*cd
));
365 cd
->next
= container
->devs
;
366 container
->devs
= cd
;
370 /* SIGUSR is sent between parent and child. So both block it
371 * and enable it only with pselect.
374 sigaddset(&set
, SIGUSR1
);
375 sigaddset(&set
, SIGHUP
);
376 sigaddset(&set
, SIGALRM
);
377 sigaddset(&set
, SIGTERM
);
378 sigprocmask(SIG_BLOCK
, &set
, NULL
);
379 act
.sa_handler
= wake_me
;
381 sigaction(SIGUSR1
, &act
, NULL
);
382 sigaction(SIGALRM
, &act
, NULL
);
383 act
.sa_handler
= hup
;
384 sigaction(SIGHUP
, &act
, NULL
);
385 act
.sa_handler
= term
;
386 sigaction(SIGTERM
, &act
, NULL
);
387 act
.sa_handler
= SIG_IGN
;
388 sigaction(SIGPIPE
, &act
, NULL
);
391 /* we assume we assume that /sys /proc /dev are available in
392 * the new root (see nash:setuproot)
394 * kill any monitors in the current namespace and change
397 try_kill_monitor(container
->devname
);
398 if (chroot(switchroot
) != 0) {
399 fprintf(stderr
, "mdmon: failed to chroot to '%s': %s\n",
400 switchroot
, strerror(errno
));
405 /* If this fails, we hope it already exists
406 * pid file lives in /var/run/mdadm/mdXX.pid
409 mkdir("/var/run", 0600);
410 mkdir("/var/run/mdadm", 0600);
412 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
413 if (ping_monitor(container
->devname
) == 0) {
414 fprintf(stderr
, "mdmon: %s already managed\n",
420 /* cleanup the old monitor, this one is taking over */
421 try_kill_monitor(container
->devname
);
422 err
= make_pidfile(container
->devname
, 0);
424 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
427 /* FIXME implement a mechanism to
428 * prevent duplicate monitor instances
431 "mdmon: continuing on read-only file system\n");
437 container
->sock
= make_control_sock(container
->devname
);
439 if (container
->ss
->load_super(container
, mdfd
, container_name
)) {
440 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
445 /* Ok, this is close enough. We can say goodbye to our parent now.
448 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
449 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
455 open("/dev/null", O_RDWR
);
463 mlockall(MCL_FUTURE
);
465 if (clone_monitor(container
) < 0) {
466 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
471 do_manager(container
);