]>
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 /* first rule of survival... don't off yourself */
146 /* kill this process if it is mdmon */
147 sprintf(buf
, "/proc/%lu/cmdline", (unsigned long) pid
);
148 fd
= open(buf
, O_RDONLY
);
152 if (read(fd
, buf
, sizeof(buf
)) < 0) {
157 if (!strstr(buf
, "mdmon"))
162 mdstat
= mdstat_read(0, 0);
163 for ( ; mdstat
; mdstat
= mdstat
->next
)
164 if (is_container_member(mdstat
, devname
)) {
165 sprintf(buf
, "/dev/%s", mdstat
->dev
);
169 remove_pidfile(devname
);
172 void remove_pidfile(char *devname
)
179 sprintf(buf
, "/var/run/mdadm/%s.pid", devname
);
181 sprintf(buf
, "/var/run/mdadm/%s.sock", devname
);
185 int make_control_sock(char *devname
)
190 struct sockaddr_un addr
;
195 sprintf(path
, "/var/run/mdadm/%s.sock", devname
);
197 sfd
= socket(PF_LOCAL
, SOCK_STREAM
, 0);
201 addr
.sun_family
= PF_LOCAL
;
202 strcpy(addr
.sun_path
, path
);
203 if (bind(sfd
, &addr
, sizeof(addr
)) < 0) {
208 fl
= fcntl(sfd
, F_GETFL
, 0);
210 fcntl(sfd
, F_SETFL
, fl
);
214 int socket_hup_requested
;
215 static void hup(int sig
)
217 socket_hup_requested
= 1;
220 static void term(int sig
)
225 static void wake_me(int sig
)
230 /* if we are debugging and starting mdmon by hand then don't fork */
231 static int do_fork(void)
243 fprintf(stderr
, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
247 int main(int argc
, char *argv
[])
250 struct mdinfo
*mdi
, *di
;
251 struct supertype
*container
;
253 struct sigaction act
;
257 char *container_name
= NULL
;
258 char *switchroot
= NULL
;
262 container_name
= argv
[1];
265 if (strcmp(argv
[1], "--switch-root") != 0) {
266 fprintf(stderr
, "mdmon: unknown argument %s\n", argv
[1]);
269 switchroot
= argv
[2];
270 container_name
= argv
[3];
276 mdfd
= open(container_name
, O_RDWR
);
278 fprintf(stderr
, "mdmon: %s: %s\n", container_name
,
282 if (md_get_version(mdfd
) < 0) {
283 fprintf(stderr
, "mdmon: %s: Not an md device\n",
288 /* Fork, and have the child tell us when they are ready */
290 if (pipe(pfd
) != 0) {
291 fprintf(stderr
, "mdmon: failed to create pipe\n");
296 fprintf(stderr
, "mdmon: failed to fork: %s\n",
302 default: /* parent */
304 if (read(pfd
[0], &status
, sizeof(status
)) != sizeof(status
)) {
306 status
= WEXITSTATUS(status
);
311 pfd
[0] = pfd
[1] = -1;
313 container
= malloc(sizeof(*container
));
314 container
->devnum
= fd2devnum(mdfd
);
315 container
->devname
= devnum2devname(container
->devnum
);
316 container
->device_name
= container_name
;
317 container
->arrays
= NULL
;
319 if (!container
->devname
) {
320 fprintf(stderr
, "mdmon: failed to allocate container name string\n");
324 mdi
= sysfs_read(mdfd
, container
->devnum
,
325 GET_VERSION
|GET_LEVEL
|GET_DEVS
);
328 fprintf(stderr
, "mdmon: failed to load sysfs info for %s\n",
332 if (mdi
->array
.level
!= UnSet
) {
333 fprintf(stderr
, "mdmon: %s is not a container - cannot monitor\n",
337 if (mdi
->array
.major_version
!= -1 ||
338 mdi
->array
.minor_version
!= -2) {
339 fprintf(stderr
, "mdmon: %s does not use external metadata - cannot monitor\n",
344 container
->ss
= find_metadata_methods(mdi
->text_version
);
345 if (container
->ss
== NULL
) {
346 fprintf(stderr
, "mdmon: %s uses unknown metadata: %s\n",
347 container_name
, mdi
->text_version
);
351 container
->devs
= NULL
;
352 for (di
= mdi
->devs
; di
; di
= di
->next
) {
353 struct mdinfo
*cd
= malloc(sizeof(*cd
));
355 cd
->next
= container
->devs
;
356 container
->devs
= cd
;
360 /* SIGUSR is sent between parent and child. So both block it
361 * and enable it only with pselect.
364 sigaddset(&set
, SIGUSR1
);
365 sigaddset(&set
, SIGHUP
);
366 sigaddset(&set
, SIGALRM
);
367 sigaddset(&set
, SIGTERM
);
368 sigprocmask(SIG_BLOCK
, &set
, NULL
);
369 act
.sa_handler
= wake_me
;
371 sigaction(SIGUSR1
, &act
, NULL
);
372 sigaction(SIGALRM
, &act
, NULL
);
373 act
.sa_handler
= hup
;
374 sigaction(SIGHUP
, &act
, NULL
);
375 act
.sa_handler
= term
;
376 sigaction(SIGTERM
, &act
, NULL
);
377 act
.sa_handler
= SIG_IGN
;
378 sigaction(SIGPIPE
, &act
, NULL
);
381 /* we assume we assume that /sys /proc /dev are available in
382 * the new root (see nash:setuproot)
384 * kill any monitors in the current namespace and change
387 try_kill_monitor(container
->devname
);
388 if (chroot(switchroot
) != 0) {
389 fprintf(stderr
, "mdmon: failed to chroot to '%s': %s\n",
390 switchroot
, strerror(errno
));
395 /* If this fails, we hope it already exists
396 * pid file lives in /var/run/mdadm/mdXX.pid
399 mkdir("/var/run", 0600);
400 mkdir("/var/run/mdadm", 0600);
402 if (make_pidfile(container
->devname
, O_EXCL
) < 0) {
403 if (ping_monitor(container
->devname
) == 0) {
404 fprintf(stderr
, "mdmon: %s already managed\n",
410 /* cleanup the old monitor, this one is taking over */
411 try_kill_monitor(container
->devname
);
412 err
= make_pidfile(container
->devname
, 0);
414 fprintf(stderr
, "mdmon: %s Cannot create pidfile\n",
417 /* FIXME implement a mechanism to
418 * prevent duplicate monitor instances
421 "mdmon: continuing on read-only file system\n");
427 container
->sock
= make_control_sock(container
->devname
);
429 if (container
->ss
->load_super(container
, mdfd
, container_name
)) {
430 fprintf(stderr
, "mdmon: Cannot load metadata for %s\n",
435 /* Ok, this is close enough. We can say goodbye to our parent now.
438 if (write(pfd
[1], &status
, sizeof(status
)) < 0)
439 fprintf(stderr
, "mdmon: failed to notify our parent: %d\n",
445 open("/dev/null", O_RDWR
);
453 mlockall(MCL_FUTURE
);
455 if (clone_monitor(container
) < 0) {
456 fprintf(stderr
, "mdmon: failed to start monitor process: %s\n",
461 do_manager(container
);