]>
Commit | Line | Data |
---|---|---|
a54d5262 DW |
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 | */ | |
549e9569 NB |
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> | |
4d43913c | 48 | #include <sys/types.h> |
549e9569 NB |
49 | #include <sys/stat.h> |
50 | #include <sys/socket.h> | |
51 | #include <sys/un.h> | |
52 | #include <sys/mman.h> | |
4d43913c | 53 | #include <sys/syscall.h> |
9fe32043 | 54 | #include <sys/wait.h> |
549e9569 NB |
55 | #include <stdio.h> |
56 | #include <errno.h> | |
57 | #include <string.h> | |
58 | #include <fcntl.h> | |
b109d928 | 59 | #include <signal.h> |
13047e4c | 60 | #include <dirent.h> |
549e9569 NB |
61 | |
62 | #include <sched.h> | |
63 | ||
64 | #include "mdadm.h" | |
65 | #include "mdmon.h" | |
66 | ||
549e9569 NB |
67 | struct active_array *discard_this; |
68 | struct active_array *pending_discard; | |
4d43913c NB |
69 | |
70 | int mon_tid, mgr_tid; | |
549e9569 | 71 | |
6144ed44 DW |
72 | int sigterm; |
73 | ||
549e9569 NB |
74 | int run_child(void *v) |
75 | { | |
76 | struct supertype *c = v; | |
1ed3f387 | 77 | |
549e9569 NB |
78 | do_monitor(c); |
79 | return 0; | |
80 | } | |
81 | ||
82 | int clone_monitor(struct supertype *container) | |
83 | { | |
549e9569 | 84 | static char stack[4096]; |
549e9569 | 85 | |
2cc98f9e | 86 | mon_tid = clone(run_child, stack+4096-64, |
549e9569 NB |
87 | CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD, |
88 | container); | |
3e70c845 | 89 | |
4d43913c | 90 | mgr_tid = syscall(SYS_gettid); |
2cc98f9e DW |
91 | |
92 | return mon_tid; | |
549e9569 NB |
93 | } |
94 | ||
549e9569 | 95 | |
295646b3 | 96 | int make_pidfile(char *devname, int o_excl) |
549e9569 NB |
97 | { |
98 | char path[100]; | |
99 | char pid[10]; | |
100 | int fd; | |
3d2c4fc7 DW |
101 | int n; |
102 | ||
6144ed44 DW |
103 | if (sigterm) |
104 | return -1; | |
105 | ||
549e9569 NB |
106 | sprintf(path, "/var/run/mdadm/%s.pid", devname); |
107 | ||
b109d928 | 108 | fd = open(path, O_RDWR|O_CREAT|o_excl, 0600); |
549e9569 | 109 | if (fd < 0) |
295646b3 | 110 | return -errno; |
549e9569 | 111 | sprintf(pid, "%d\n", getpid()); |
3d2c4fc7 | 112 | n = write(fd, pid, strlen(pid)); |
549e9569 | 113 | close(fd); |
3d2c4fc7 DW |
114 | if (n < 0) |
115 | return -errno; | |
549e9569 NB |
116 | return 0; |
117 | } | |
118 | ||
883a6142 DW |
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); | |
b109d928 DW |
132 | static void try_kill_monitor(char *devname) |
133 | { | |
134 | char buf[100]; | |
135 | int fd; | |
136 | pid_t pid; | |
883a6142 | 137 | struct mdstat_ent *mdstat; |
b109d928 DW |
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 | ||
8aae4219 DW |
152 | /* first rule of survival... don't off yourself */ |
153 | if (pid == getpid()) | |
154 | return; | |
155 | ||
b109d928 DW |
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 | ||
883a6142 DW |
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); | |
27dec8fa | 176 | WaitClean(buf, 0); |
883a6142 DW |
177 | } |
178 | free_mdstat(mdstat); | |
179 | remove_pidfile(devname); | |
b109d928 DW |
180 | } |
181 | ||
e0d6609f NB |
182 | void remove_pidfile(char *devname) |
183 | { | |
184 | char buf[100]; | |
185 | ||
6144ed44 DW |
186 | if (sigterm) |
187 | return; | |
188 | ||
e0d6609f NB |
189 | sprintf(buf, "/var/run/mdadm/%s.pid", devname); |
190 | unlink(buf); | |
57752795 N |
191 | sprintf(buf, "/var/run/mdadm/%s.sock", devname); |
192 | unlink(buf); | |
e0d6609f NB |
193 | } |
194 | ||
295646b3 | 195 | int make_control_sock(char *devname) |
549e9569 NB |
196 | { |
197 | char path[100]; | |
198 | int sfd; | |
199 | long fl; | |
200 | struct sockaddr_un addr; | |
201 | ||
6144ed44 DW |
202 | if (sigterm) |
203 | return -1; | |
204 | ||
549e9569 NB |
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 | ||
295646b3 DW |
224 | int socket_hup_requested; |
225 | static void hup(int sig) | |
226 | { | |
227 | socket_hup_requested = 1; | |
228 | } | |
229 | ||
6144ed44 DW |
230 | static void term(int sig) |
231 | { | |
232 | sigterm = 1; | |
233 | } | |
234 | ||
4d43913c NB |
235 | static void wake_me(int sig) |
236 | { | |
237 | ||
238 | } | |
239 | ||
16ddab0d DW |
240 | /* if we are debugging and starting mdmon by hand then don't fork */ |
241 | static int do_fork(void) | |
242 | { | |
243 | #ifdef DEBUG | |
40ebbb9c | 244 | if (check_env("MDADM_NO_MDMON")) |
16ddab0d DW |
245 | return 0; |
246 | #endif | |
247 | ||
248 | return 1; | |
249 | } | |
250 | ||
13047e4c DW |
251 | void usage(void) |
252 | { | |
253 | fprintf(stderr, "Usage: mdmon [--switch-root dir] /device/name/for/container\n"); | |
254 | exit(2); | |
255 | } | |
16ddab0d | 256 | |
549e9569 NB |
257 | int main(int argc, char *argv[]) |
258 | { | |
259 | int mdfd; | |
549e9569 NB |
260 | struct mdinfo *mdi, *di; |
261 | struct supertype *container; | |
4d43913c | 262 | sigset_t set; |
bfa44e2e | 263 | struct sigaction act; |
9fe32043 N |
264 | int pfd[2]; |
265 | int status; | |
3d2c4fc7 | 266 | int ignore; |
13047e4c DW |
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(); | |
549e9569 | 284 | } |
13047e4c DW |
285 | |
286 | mdfd = open(container_name, O_RDWR); | |
549e9569 | 287 | if (mdfd < 0) { |
13047e4c | 288 | fprintf(stderr, "mdmon: %s: %s\n", container_name, |
549e9569 NB |
289 | strerror(errno)); |
290 | exit(1); | |
291 | } | |
292 | if (md_get_version(mdfd) < 0) { | |
13047e4c DW |
293 | fprintf(stderr, "mdmon: %s: Not an md device\n", |
294 | container_name); | |
549e9569 NB |
295 | exit(1); |
296 | } | |
297 | ||
9fe32043 | 298 | /* Fork, and have the child tell us when they are ready */ |
16ddab0d | 299 | if (do_fork()) { |
3d2c4fc7 DW |
300 | if (pipe(pfd) != 0) { |
301 | fprintf(stderr, "mdmon: failed to create pipe\n"); | |
302 | exit(1); | |
303 | } | |
16ddab0d DW |
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); | |
9fe32043 | 319 | } |
16ddab0d DW |
320 | } else |
321 | pfd[0] = pfd[1] = -1; | |
549e9569 NB |
322 | |
323 | container = malloc(sizeof(*container)); | |
549e9569 NB |
324 | container->devnum = fd2devnum(mdfd); |
325 | container->devname = devnum2devname(container->devnum); | |
13047e4c DW |
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); | |
549e9569 | 369 | |
883a6142 DW |
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 | ||
13047e4c DW |
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); | |
549e9569 | 410 | mkdir("/var/run/mdadm", 0600); |
13047e4c | 411 | ignore = chdir("/"); |
b109d928 DW |
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 { | |
295646b3 DW |
418 | int err; |
419 | ||
b109d928 DW |
420 | /* cleanup the old monitor, this one is taking over */ |
421 | try_kill_monitor(container->devname); | |
295646b3 DW |
422 | err = make_pidfile(container->devname, 0); |
423 | if (err < 0) { | |
b109d928 DW |
424 | fprintf(stderr, "mdmon: %s Cannot create pidfile\n", |
425 | container->devname); | |
295646b3 DW |
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); | |
b109d928 DW |
434 | } |
435 | } | |
549e9569 | 436 | } |
549e9569 | 437 | container->sock = make_control_sock(container->devname); |
549e9569 | 438 | |
13047e4c | 439 | if (container->ss->load_super(container, mdfd, container_name)) { |
549e9569 | 440 | fprintf(stderr, "mdmon: Cannot load metadata for %s\n", |
13047e4c | 441 | container_name); |
549e9569 NB |
442 | exit(3); |
443 | } | |
549e9569 | 444 | |
9fe32043 N |
445 | /* Ok, this is close enough. We can say goodbye to our parent now. |
446 | */ | |
447 | status = 0; | |
3d2c4fc7 DW |
448 | if (write(pfd[1], &status, sizeof(status)) < 0) |
449 | fprintf(stderr, "mdmon: failed to notify our parent: %d\n", | |
450 | getppid()); | |
9fe32043 N |
451 | close(pfd[1]); |
452 | ||
9fe32043 N |
453 | setsid(); |
454 | close(0); | |
455 | open("/dev/null", O_RDWR); | |
456 | close(1); | |
3d2c4fc7 | 457 | ignore = dup(0); |
9fe32043 N |
458 | #ifndef DEBUG |
459 | close(2); | |
3d2c4fc7 | 460 | ignore = dup(0); |
9fe32043 N |
461 | #endif |
462 | ||
549e9569 NB |
463 | mlockall(MCL_FUTURE); |
464 | ||
3e70c845 | 465 | if (clone_monitor(container) < 0) { |
295646b3 | 466 | fprintf(stderr, "mdmon: failed to start monitor process: %s\n", |
549e9569 NB |
467 | strerror(errno)); |
468 | exit(2); | |
469 | } | |
470 | ||
471 | do_manager(container); | |
472 | ||
473 | exit(0); | |
474 | } |