]>
git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
4 * When md arrays have user-space managed metadata, this is the program
5 * that does the managing.
7 * Given one argument: the name of the array (e.g. /dev/md0) that is
9 * We fork off a helper that runs high priority and mlocked. It responds to
10 * device failures and other events that might stop writeout, or that are
11 * trivial to deal with.
12 * The main thread then watches for new arrays being created in the container
13 * and starts monitoring them too ... along with a few other tasks.
15 * The main thread communicates with the priority thread by writing over
17 * Separate programs can communicate with the main thread via Unix-domain
19 * The two threads share address space and open file table.
29 #include <sys/types.h>
31 #include <sys/socket.h>
34 #include <sys/syscall.h>
48 struct active_array
*discard_this
;
49 struct active_array
*pending_discard
;
55 int run_child(void *v
)
57 struct supertype
*c
= v
;
63 int clone_monitor(struct supertype
*container
)
65 static char stack
[4096];
67 mon_tid
= clone(run_child
, stack
+4096-64,
68 CLONE_FS
|CLONE_FILES
|CLONE_VM
|CLONE_SIGHAND
|CLONE_THREAD
,
71 mgr_tid
= syscall(SYS_gettid
);
76 static struct superswitch
*find_metadata_methods(char *vers
)
78 if (strcmp(vers
, "ddf") == 0)
80 if (strcmp(vers
, "imsm") == 0)
86 int make_pidfile(char *devname
, int o_excl
)
96 sprintf(path
, "/var/run/mdadm/%s.pid", devname
);
98 fd
= open(path
, O_RDWR
|O_CREAT
|o_excl
, 0600);
101 sprintf(pid
, "%d\n", getpid());
102 n
= write(fd
, pid
, strlen(pid
));
109 int is_container_member(struct mdstat_ent
*mdstat
, char *container
)
111 if (mdstat
->metadata_version
== NULL
||
112 strncmp(mdstat
->metadata_version
, "external:", 9) != 0 ||
113 !is_subarray(mdstat
->metadata_version
+9) ||
114 strncmp(mdstat
->metadata_version
+10, container
, strlen(container
)) != 0 ||
115 mdstat
->metadata_version
[10+strlen(container
)] != '/')
121 void remove_pidfile(char *devname
);
122 static void try_kill_monitor(char *devname
)
127 struct mdstat_ent
*mdstat
;
129 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
130 fd
= open(buf
, O_RDONLY
);
134 if (read(fd
, buf
, sizeof(buf
)) < 0) {
140 pid
= strtoul(buf
, NULL
, 10);
142 /* kill this process if it is mdmon */
143 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
144 fd
= open(buf
, O_RDONLY
);
148 if (read(fd
, buf
, sizeof(buf
)) < 0) {
153 if (!strstr(buf
, "mdmon"))
158 mdstat
= mdstat_read(0, 0);
159 for ( ; mdstat
; mdstat
= mdstat
->next
)
160 if (is_container_member(mdstat
, devname
)) {
161 sprintf(buf
, "/dev/%s", mdstat
->dev
);
165 remove_pidfile(devname
);
168 void remove_pidfile(char *devname
)
175 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
177 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
181 int make_control_sock(char *devname
)
186 struct sockaddr_un addr
;
191 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
193 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
197 addr
.sun_family
= PF_LOCAL
;
198 strcpy(addr
.sun_path
, path
);
199 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
204 fl
= fcntl(sfd
, F_GETFL
, 0);
206 fcntl(sfd
, F_SETFL
, fl
);
210 int socket_hup_requested
;
211 static void hup(int sig
)
213 socket_hup_requested
= 1;
216 static void term(int sig
)
221 static void wake_me(int sig
)
226 /* if we are debugging and starting mdmon by hand then don't fork */
227 static int do_fork(void)
239 fprintf(stderr
, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
243 int main(int argc
, char *argv
[])
246 struct mdinfo
*mdi
, *di
;
247 struct supertype
*container
;
249 struct sigaction act
;
253 char *container_name
= NULL
;
254 char *switchroot
= NULL
;
258 container_name
= argv
[1];
261 if (strcmp(argv
[1], "--switch-root") != 0) {
262 fprintf(stderr
, "mdmon: unknown argument %s\n", argv
[1]);
265 switchroot
= argv
[2];
266 container_name
= argv
[3];
272 mdfd
= open(container_name
, O_RDWR
);
274 fprintf(stderr
, "mdmon: %s: %s\n", container_name
,
278 if (md_get_version(mdfd
) < 0) {
279 fprintf(stderr
, "mdmon: %s: Not an md device\n",
284 /* Fork, and have the child tell us when they are ready */
286 if (pipe(pfd
) != 0) {
287 fprintf(stderr
, "mdmon: failed to create pipe\n");
292 fprintf(stderr
, "mdmon: failed to fork: %s\n",
298 default: /* parent */
300 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
302 status
= WEXITSTATUS(status
);
307 pfd
[0] = pfd
[1] = -1;
309 container
= malloc(sizeof(*container
));
310 container
->devnum
= fd2devnum(mdfd
);
311 container
->devname
= devnum2devname(container
->devnum
);
312 container
->device_name
= container_name
;
313 container
->arrays
= NULL
;
315 if (!container
->devname
) {
316 fprintf(stderr
, "mdmon: failed to allocate container name string\n");
320 mdi
= sysfs_read(mdfd
, container
->devnum
,
321 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
324 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
328 if (mdi
->array
.level
!= UnSet
) {
329 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
333 if (mdi
->array
.major_version
!= -1 ||
334 mdi
->array
.minor_version
!= -2) {
335 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
340 container
->ss
= find_metadata_methods(mdi
->text_version
);
341 if (container
->ss
== NULL
) {
342 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
343 container_name
, mdi
->text_version
);
347 container
->devs
= NULL
;
348 for (di
= mdi
->devs
; di
; di
= di
->next
) {
349 struct mdinfo
*cd
= malloc(sizeof(*cd
));
351 cd
->next
= container
->devs
;
352 container
->devs
= cd
;
356 /* SIGUSR is sent between parent and child. So both block it
357 * and enable it only with pselect.
360 sigaddset(&set
, SIGUSR1
);
361 sigaddset(&set
, SIGHUP
);
362 sigaddset(&set
, SIGALRM
);
363 sigaddset(&set
, SIGTERM
);
364 sigprocmask(SIG_BLOCK
, &set
, NULL
);
365 act
.sa_handler
= wake_me
;
367 sigaction(SIGUSR1
, &act
, NULL
);
368 sigaction(SIGALRM
, &act
, NULL
);
369 act
.sa_handler
= hup
;
370 sigaction(SIGHUP
, &act
, NULL
);
371 act
.sa_handler
= term
;
372 sigaction(SIGTERM
, &act
, NULL
);
373 act
.sa_handler
= SIG_IGN
;
374 sigaction(SIGPIPE
, &act
, NULL
);
377 /* we assume we assume that /sys /proc /dev are available in
378 * the new root (see nash:setuproot)
380 * kill any monitors in the current namespace and change
383 try_kill_monitor(container
->devname
);
384 if (chroot(switchroot
) != 0) {
385 fprintf(stderr
, "mdmon: failed to chroot to '%s': %s\n",
386 switchroot
, strerror(errno
));
391 /* If this fails, we hope it already exists
392 * pid file lives in /var/run/mdadm/mdXX.pid
395 mkdir("/var/run", 0600);
396 mkdir("/var/run/mdadm", 0600);
398 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
399 if (ping_monitor(container
->devname
) == 0) {
400 fprintf(stderr
, "mdmon: %s already managed\n",
406 /* cleanup the old monitor, this one is taking over */
407 try_kill_monitor(container
->devname
);
408 err
= make_pidfile(container
->devname
, 0);
410 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
413 /* FIXME implement a mechanism to
414 * prevent duplicate monitor instances
417 "mdmon: continuing on read-only file system\n");
423 container
->sock
= make_control_sock(container
->devname
);
425 if (container
->ss
->load_super(container
, mdfd
, container_name
)) {
426 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
431 /* Ok, this is close enough. We can say goodbye to our parent now.
434 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
435 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
441 open("/dev/null", O_RDWR
);
449 mlockall(MCL_FUTURE
);
451 if (clone_monitor(container
) < 0) {
452 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
457 do_manager(container
);