]> git.ipfire.org Git - thirdparty/mdadm.git/blob - mdmon.c
Preliminary -As support for container member arrays
[thirdparty/mdadm.git] / mdmon.c
1 /*
2 * mdmon - monitor external metadata arrays
3 *
4 * Copyright (C) 2007-2008 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2008 Intel Corporation
6 *
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.
10 *
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
14 * more details.
15 *
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.
19 */
20
21 /*
22 * md array manager.
23 * When md arrays have user-space managed metadata, this is the program
24 * that does the managing.
25 *
26 * Given one argument: the name of the array (e.g. /dev/md0) that is
27 * the container.
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.
33 *
34 * The main thread communicates with the priority thread by writing over
35 * a pipe.
36 * Separate programs can communicate with the main thread via Unix-domain
37 * socket.
38 * The two threads share address space and open file table.
39 *
40 */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45
46 #include <unistd.h>
47 #include <stdlib.h>
48 #include <sys/types.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <sys/un.h>
52 #include <sys/mman.h>
53 #include <sys/syscall.h>
54 #include <sys/wait.h>
55 #include <stdio.h>
56 #include <errno.h>
57 #include <string.h>
58 #include <fcntl.h>
59 #include <signal.h>
60 #include <dirent.h>
61
62 #include <sched.h>
63
64 #include "mdadm.h"
65 #include "mdmon.h"
66
67 struct active_array *discard_this;
68 struct active_array *pending_discard;
69
70 int mon_tid, mgr_tid;
71
72 int sigterm;
73
74 int run_child(void *v)
75 {
76 struct supertype *c = v;
77
78 do_monitor(c);
79 return 0;
80 }
81
82 int clone_monitor(struct supertype *container)
83 {
84 static char stack[4096];
85
86 mon_tid = clone(run_child, stack+4096-64,
87 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
88 container);
89
90 mgr_tid = syscall(SYS_gettid);
91
92 return mon_tid;
93 }
94
95
96 int make_pidfile(char *devname, int o_excl)
97 {
98 char path[100];
99 char pid[10];
100 int fd;
101 int n;
102
103 if (sigterm)
104 return -1;
105
106 sprintf(path, "/var/run/mdadm/%s.pid", devname);
107
108 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
109 if (fd < 0)
110 return -errno;
111 sprintf(pid, "%d\n", getpid());
112 n = write(fd, pid, strlen(pid));
113 close(fd);
114 if (n < 0)
115 return -errno;
116 return 0;
117 }
118
119 int is_container_member(struct mdstat_ent *mdstat, char *container)
120 {
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)] != '/')
126 return 0;
127
128 return 1;
129 }
130
131 void remove_pidfile(char *devname);
132 static void try_kill_monitor(char *devname)
133 {
134 char buf[100];
135 int fd;
136 pid_t pid;
137 struct mdstat_ent *mdstat;
138
139 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
140 fd = open(buf, O_RDONLY);
141 if (fd < 0)
142 return;
143
144 if (read(fd, buf, sizeof(buf)) < 0) {
145 close(fd);
146 return;
147 }
148
149 close(fd);
150 pid = strtoul(buf, NULL, 10);
151
152 /* first rule of survival... don't off yourself */
153 if (pid == getpid())
154 return;
155
156 /* kill this process if it is mdmon */
157 sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
158 fd = open(buf, O_RDONLY);
159 if (fd < 0)
160 return;
161
162 if (read(fd, buf, sizeof(buf)) < 0) {
163 close(fd);
164 return;
165 }
166
167 if (!strstr(buf, "mdmon"))
168 return;
169
170 kill(pid, SIGTERM);
171
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);
176 WaitClean(buf, 0);
177 }
178 free_mdstat(mdstat);
179 remove_pidfile(devname);
180 }
181
182 void remove_pidfile(char *devname)
183 {
184 char buf[100];
185
186 if (sigterm)
187 return;
188
189 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
190 unlink(buf);
191 sprintf(buf, "/var/run/mdadm/%s.sock", devname);
192 unlink(buf);
193 }
194
195 int make_control_sock(char *devname)
196 {
197 char path[100];
198 int sfd;
199 long fl;
200 struct sockaddr_un addr;
201
202 if (sigterm)
203 return -1;
204
205 sprintf(path, "/var/run/mdadm/%s.sock", devname);
206 unlink(path);
207 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
208 if (sfd < 0)
209 return -1;
210
211 addr.sun_family = PF_LOCAL;
212 strcpy(addr.sun_path, path);
213 if (bind(sfd, &addr, sizeof(addr)) < 0) {
214 close(sfd);
215 return -1;
216 }
217 listen(sfd, 10);
218 fl = fcntl(sfd, F_GETFL, 0);
219 fl |= O_NONBLOCK;
220 fcntl(sfd, F_SETFL, fl);
221 return sfd;
222 }
223
224 int socket_hup_requested;
225 static void hup(int sig)
226 {
227 socket_hup_requested = 1;
228 }
229
230 static void term(int sig)
231 {
232 sigterm = 1;
233 }
234
235 static void wake_me(int sig)
236 {
237
238 }
239
240 /* if we are debugging and starting mdmon by hand then don't fork */
241 static int do_fork(void)
242 {
243 #ifdef DEBUG
244 if (env_no_mdmon())
245 return 0;
246 #endif
247
248 return 1;
249 }
250
251 void usage(void)
252 {
253 fprintf(stderr, "Usage: mdmon [--switch-root dir] /device/name/for/container\n");
254 exit(2);
255 }
256
257 int main(int argc, char *argv[])
258 {
259 int mdfd;
260 struct mdinfo *mdi, *di;
261 struct supertype *container;
262 sigset_t set;
263 struct sigaction act;
264 int pfd[2];
265 int status;
266 int ignore;
267 char *container_name = NULL;
268 char *switchroot = NULL;
269
270 switch (argc) {
271 case 2:
272 container_name = argv[1];
273 break;
274 case 4:
275 if (strcmp(argv[1], "--switch-root") != 0) {
276 fprintf(stderr, "mdmon: unknown argument %s\n", argv[1]);
277 usage();
278 }
279 switchroot = argv[2];
280 container_name = argv[3];
281 break;
282 default:
283 usage();
284 }
285
286 mdfd = open(container_name, O_RDWR);
287 if (mdfd < 0) {
288 fprintf(stderr, "mdmon: %s: %s\n", container_name,
289 strerror(errno));
290 exit(1);
291 }
292 if (md_get_version(mdfd) < 0) {
293 fprintf(stderr, "mdmon: %s: Not an md device\n",
294 container_name);
295 exit(1);
296 }
297
298 /* Fork, and have the child tell us when they are ready */
299 if (do_fork()) {
300 if (pipe(pfd) != 0) {
301 fprintf(stderr, "mdmon: failed to create pipe\n");
302 exit(1);
303 }
304 switch(fork()) {
305 case -1:
306 fprintf(stderr, "mdmon: failed to fork: %s\n",
307 strerror(errno));
308 exit(1);
309 case 0: /* child */
310 close(pfd[0]);
311 break;
312 default: /* parent */
313 close(pfd[1]);
314 if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) {
315 wait(&status);
316 status = WEXITSTATUS(status);
317 }
318 exit(status);
319 }
320 } else
321 pfd[0] = pfd[1] = -1;
322
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;
328
329 if (!container->devname) {
330 fprintf(stderr, "mdmon: failed to allocate container name string\n");
331 exit(3);
332 }
333
334 mdi = sysfs_read(mdfd, container->devnum,
335 GET_VERSION|GET_LEVEL|GET_DEVS);
336
337 if (!mdi) {
338 fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
339 container->devname);
340 exit(3);
341 }
342 if (mdi->array.level != UnSet) {
343 fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
344 container_name);
345 exit(3);
346 }
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",
350 container_name);
351 exit(3);
352 }
353
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);
358 exit(3);
359 }
360
361 container->devs = NULL;
362 for (di = mdi->devs; di; di = di->next) {
363 struct mdinfo *cd = malloc(sizeof(*cd));
364 *cd = *di;
365 cd->next = container->devs;
366 container->devs = cd;
367 }
368 sysfs_free(mdi);
369
370 /* SIGUSR is sent between parent and child. So both block it
371 * and enable it only with pselect.
372 */
373 sigemptyset(&set);
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;
380 act.sa_flags = 0;
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);
389
390 if (switchroot) {
391 /* we assume we assume that /sys /proc /dev are available in
392 * the new root (see nash:setuproot)
393 *
394 * kill any monitors in the current namespace and change
395 * to the new one
396 */
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));
401 exit(4);
402 }
403 }
404
405 /* If this fails, we hope it already exists
406 * pid file lives in /var/run/mdadm/mdXX.pid
407 */
408 mkdir("/var", 0600);
409 mkdir("/var/run", 0600);
410 mkdir("/var/run/mdadm", 0600);
411 ignore = chdir("/");
412 if (make_pidfile(container->devname, O_EXCL) < 0) {
413 if (ping_monitor(container->devname) == 0) {
414 fprintf(stderr, "mdmon: %s already managed\n",
415 container->devname);
416 exit(3);
417 } else {
418 int err;
419
420 /* cleanup the old monitor, this one is taking over */
421 try_kill_monitor(container->devname);
422 err = make_pidfile(container->devname, 0);
423 if (err < 0) {
424 fprintf(stderr, "mdmon: %s Cannot create pidfile\n",
425 container->devname);
426 if (err == -EROFS) {
427 /* FIXME implement a mechanism to
428 * prevent duplicate monitor instances
429 */
430 fprintf(stderr,
431 "mdmon: continuing on read-only file system\n");
432 } else
433 exit(3);
434 }
435 }
436 }
437 container->sock = make_control_sock(container->devname);
438
439 if (container->ss->load_super(container, mdfd, container_name)) {
440 fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
441 container_name);
442 exit(3);
443 }
444
445 /* Ok, this is close enough. We can say goodbye to our parent now.
446 */
447 status = 0;
448 if (write(pfd[1], &status, sizeof(status)) < 0)
449 fprintf(stderr, "mdmon: failed to notify our parent: %d\n",
450 getppid());
451 close(pfd[1]);
452
453 setsid();
454 close(0);
455 open("/dev/null", O_RDWR);
456 close(1);
457 ignore = dup(0);
458 #ifndef DEBUG
459 close(2);
460 ignore = dup(0);
461 #endif
462
463 mlockall(MCL_FUTURE);
464
465 if (clone_monitor(container) < 0) {
466 fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
467 strerror(errno));
468 exit(2);
469 }
470
471 do_manager(container);
472
473 exit(0);
474 }