Having single function to read mdmon pid file.
[thirdparty/mdadm.git] / mdmon.c
CommitLineData
a54d5262
DW
1/*
2 * mdmon - monitor external metadata arrays
3 *
e736b623
N
4 * Copyright (C) 2007-2009 Neil Brown <neilb@suse.de>
5 * Copyright (C) 2007-2009 Intel Corporation
a54d5262
DW
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
67struct active_array *discard_this;
68struct active_array *pending_discard;
4d43913c
NB
69
70int mon_tid, mgr_tid;
549e9569 71
6144ed44
DW
72int sigterm;
73
549e9569
NB
74int run_child(void *v)
75{
76 struct supertype *c = v;
1ed3f387 77
549e9569
NB
78 do_monitor(c);
79 return 0;
80}
81
97f734fd
N
82#ifdef __ia64__
83int __clone2(int (*fn)(void *),
84 void *child_stack_base, size_t stack_size,
85 int flags, void *arg, ...
86 /* pid_t *pid, struct user_desc *tls, pid_t *ctid */ );
87#endif
88 int clone_monitor(struct supertype *container)
549e9569 89{
549e9569 90 static char stack[4096];
549e9569 91
97f734fd
N
92#ifdef __ia64__
93 mon_tid = __clone2(run_child, stack, sizeof(stack),
94 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
95 container);
96#else
2cc98f9e 97 mon_tid = clone(run_child, stack+4096-64,
549e9569
NB
98 CLONE_FS|CLONE_FILES|CLONE_VM|CLONE_SIGHAND|CLONE_THREAD,
99 container);
97f734fd 100#endif
3e70c845 101
4d43913c 102 mgr_tid = syscall(SYS_gettid);
2cc98f9e
DW
103
104 return mon_tid;
549e9569
NB
105}
106
ce744c97
DW
107static struct superswitch *find_metadata_methods(char *vers)
108{
109 if (strcmp(vers, "ddf") == 0)
110 return &super_ddf;
111 if (strcmp(vers, "imsm") == 0)
112 return &super_imsm;
113 return NULL;
114}
115
295646b3 116int make_pidfile(char *devname, int o_excl)
549e9569
NB
117{
118 char path[100];
119 char pid[10];
120 int fd;
3d2c4fc7
DW
121 int n;
122
6144ed44
DW
123 if (sigterm)
124 return -1;
125
549e9569
NB
126 sprintf(path, "/var/run/mdadm/%s.pid", devname);
127
b109d928 128 fd = open(path, O_RDWR|O_CREAT|o_excl, 0600);
549e9569 129 if (fd < 0)
295646b3 130 return -errno;
549e9569 131 sprintf(pid, "%d\n", getpid());
3d2c4fc7 132 n = write(fd, pid, strlen(pid));
549e9569 133 close(fd);
3d2c4fc7
DW
134 if (n < 0)
135 return -errno;
549e9569
NB
136 return 0;
137}
138
883a6142
DW
139int is_container_member(struct mdstat_ent *mdstat, char *container)
140{
141 if (mdstat->metadata_version == NULL ||
142 strncmp(mdstat->metadata_version, "external:", 9) != 0 ||
143 !is_subarray(mdstat->metadata_version+9) ||
144 strncmp(mdstat->metadata_version+10, container, strlen(container)) != 0 ||
145 mdstat->metadata_version[10+strlen(container)] != '/')
146 return 0;
147
148 return 1;
149}
150
9f1da824 151static void try_kill_monitor(pid_t pid, char *devname, int sock)
96a8270d
DW
152{
153 char buf[100];
154 int fd;
417a4b04 155 int n;
af7ca334 156 long fl;
b109d928 157
8aae4219
DW
158 /* first rule of survival... don't off yourself */
159 if (pid == getpid())
160 return;
161
b109d928
DW
162 /* kill this process if it is mdmon */
163 sprintf(buf, "/proc/%lu/cmdline", (unsigned long) pid);
164 fd = open(buf, O_RDONLY);
165 if (fd < 0)
166 return;
167
417a4b04
N
168 n = read(fd, buf, sizeof(buf)-1);
169 buf[sizeof(buf)-1] = 0;
170 close(fd);
b109d928 171
417a4b04 172 if (n < 0 || !strstr(buf, "mdmon"))
883a6142
DW
173 return;
174
175 kill(pid, SIGTERM);
176
af7ca334
N
177 /* Wait for monitor to exit by reading from the socket, after
178 * clearing the non-blocking flag */
179 fl = fcntl(sock, F_GETFL, 0);
180 fl &= ~O_NONBLOCK;
181 fcntl(sock, F_SETFL, fl);
182 read(sock, buf, 100);
b109d928
DW
183}
184
e0d6609f
NB
185void remove_pidfile(char *devname)
186{
187 char buf[100];
188
6144ed44
DW
189 if (sigterm)
190 return;
191
e0d6609f
NB
192 sprintf(buf, "/var/run/mdadm/%s.pid", devname);
193 unlink(buf);
57752795
N
194 sprintf(buf, "/var/run/mdadm/%s.sock", devname);
195 unlink(buf);
e0d6609f
NB
196}
197
295646b3 198int make_control_sock(char *devname)
549e9569
NB
199{
200 char path[100];
201 int sfd;
202 long fl;
203 struct sockaddr_un addr;
204
6144ed44
DW
205 if (sigterm)
206 return -1;
207
549e9569
NB
208 sprintf(path, "/var/run/mdadm/%s.sock", devname);
209 unlink(path);
210 sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
211 if (sfd < 0)
212 return -1;
213
214 addr.sun_family = PF_LOCAL;
215 strcpy(addr.sun_path, path);
216 if (bind(sfd, &addr, sizeof(addr)) < 0) {
217 close(sfd);
218 return -1;
219 }
220 listen(sfd, 10);
221 fl = fcntl(sfd, F_GETFL, 0);
222 fl |= O_NONBLOCK;
223 fcntl(sfd, F_SETFL, fl);
224 return sfd;
225}
226
295646b3
DW
227int socket_hup_requested;
228static void hup(int sig)
229{
230 socket_hup_requested = 1;
231}
232
6144ed44
DW
233static void term(int sig)
234{
235 sigterm = 1;
236}
237
4d43913c
NB
238static void wake_me(int sig)
239{
240
241}
242
16ddab0d
DW
243/* if we are debugging and starting mdmon by hand then don't fork */
244static int do_fork(void)
245{
246 #ifdef DEBUG
40ebbb9c 247 if (check_env("MDADM_NO_MDMON"))
16ddab0d
DW
248 return 0;
249 #endif
250
251 return 1;
252}
253
13047e4c
DW
254void usage(void)
255{
5746141e 256 fprintf(stderr, "Usage: mdmon /device/name/for/container [target_dir]\n");
13047e4c
DW
257 exit(2);
258}
16ddab0d 259
3e7312a9 260static int mdmon(char *devname, int devnum, int must_fork, char *switchroot);
1ffd2840 261
549e9569
NB
262int main(int argc, char *argv[])
263{
13047e4c
DW
264 char *container_name = NULL;
265 char *switchroot = NULL;
e8a70c89
N
266 int devnum;
267 char *devname;
1ffd2840 268 int status = 0;
13047e4c
DW
269
270 switch (argc) {
5746141e
DW
271 case 3:
272 switchroot = argv[2];
13047e4c
DW
273 case 2:
274 container_name = argv[1];
275 break;
13047e4c
DW
276 default:
277 usage();
549e9569 278 }
13047e4c 279
1ffd2840
DW
280 if (strcmp(container_name, "/proc/mdstat") == 0) {
281 struct mdstat_ent *mdstat, *e;
282
283 /* launch an mdmon instance for each container found */
1ffd2840
DW
284 mdstat = mdstat_read(0, 0);
285 for (e = mdstat; e; e = e->next) {
286 if (strncmp(e->metadata_version, "external:", 9) == 0 &&
287 !is_subarray(&e->metadata_version[9])) {
288 devname = devnum2devname(e->devnum);
1b34f519
DW
289 /* update cmdline so this mdmon instance can be
290 * distinguished from others in a call to ps(1)
291 */
292 if (strlen(devname) <= strlen(container_name)) {
293 memset(container_name, 0, strlen(container_name));
294 sprintf(container_name, "%s", devname);
295 }
3e7312a9 296 status |= mdmon(devname, e->devnum, 1,
1ffd2840
DW
297 switchroot);
298 }
299 }
300 free_mdstat(mdstat);
301
302 return status;
303 } else if (strncmp(container_name, "md", 2) == 0) {
6f4098a6
DW
304 devnum = devname2devnum(container_name);
305 devname = devnum2devname(devnum);
306 if (strcmp(container_name, devname) != 0)
307 devname = NULL;
308 } else {
309 struct stat st;
310
311 devnum = NoMdDev;
312 if (stat(container_name, &st) == 0)
313 devnum = stat2devnum(&st);
314 if (devnum == NoMdDev)
315 devname = NULL;
316 else
317 devname = devnum2devname(devnum);
318 }
319
320 if (!devname) {
e8a70c89
N
321 fprintf(stderr, "mdmon: %s is not a valid md device name\n",
322 container_name);
323 exit(1);
324 }
3e7312a9 325 return mdmon(devname, devnum, do_fork(), switchroot);
1ffd2840
DW
326}
327
3e7312a9 328static int mdmon(char *devname, int devnum, int must_fork, char *switchroot)
1ffd2840
DW
329{
330 int mdfd;
331 struct mdinfo *mdi, *di;
332 struct supertype *container;
333 sigset_t set;
334 struct sigaction act;
335 int pfd[2];
336 int status;
337 int ignore;
96a8270d 338 pid_t victim = -1;
9f1da824 339 int victim_sock = -1;
1ffd2840 340
5746141e
DW
341 dprintf("starting mdmon for %s in %s\n",
342 devname, switchroot ? : "/");
b928b5a0 343
e98ef225
N
344 /* switchroot is either a path name starting with '/', or a
345 * pid of the original mdmon (we have already done the chroot).
346 * In the latter case, stdin is a socket connected to the original
347 * mdmon.
348 */
349
b928b5a0 350 /* try to spawn mdmon instances from the target file system */
e98ef225
N
351 if (switchroot && switchroot[0] == '/' &&
352 strcmp(switchroot, "/") != 0) {
b928b5a0 353 pid_t pid;
e98ef225 354 char buf[20];
b928b5a0 355
b928b5a0
DW
356 switch (fork()) {
357 case 0:
24f6f99b 358 victim = mdmon_pid(devnum);
e98ef225
N
359 victim_sock = connect_monitor(devname);
360 if (chroot(switchroot) != 0) {
361 fprintf(stderr, "mdmon: failed to chroot to '%s': %s\n",
362 switchroot, strerror(errno));
363 exit(4);
364 }
365 ignore = chdir("/");
366 sprintf(buf, "%d", victim);
367 if (victim_sock) {
368 close(0);
369 dup(victim_sock);
370 close(victim_sock);
371 }
372 execl("/sbin/mdmon", "mdmon", devname, buf, NULL);
b928b5a0
DW
373 exit(1);
374 case -1:
375 return 1;
376 default:
377 pid = wait(&status);
378 if (pid > -1 && WIFEXITED(status) &&
379 WEXITSTATUS(status) == 0)
380 return 0;
381 else
382 return 1;
383 }
384 }
385
e8a70c89 386 mdfd = open_dev(devnum);
549e9569 387 if (mdfd < 0) {
1ffd2840 388 fprintf(stderr, "mdmon: %s: %s\n", devname,
549e9569 389 strerror(errno));
1ffd2840 390 return 1;
549e9569
NB
391 }
392 if (md_get_version(mdfd) < 0) {
13047e4c 393 fprintf(stderr, "mdmon: %s: Not an md device\n",
1ffd2840
DW
394 devname);
395 return 1;
549e9569
NB
396 }
397
9fe32043 398 /* Fork, and have the child tell us when they are ready */
3e7312a9 399 if (must_fork) {
3d2c4fc7
DW
400 if (pipe(pfd) != 0) {
401 fprintf(stderr, "mdmon: failed to create pipe\n");
1ffd2840 402 return 1;
3d2c4fc7 403 }
16ddab0d
DW
404 switch(fork()) {
405 case -1:
406 fprintf(stderr, "mdmon: failed to fork: %s\n",
407 strerror(errno));
1ffd2840 408 return 1;
16ddab0d
DW
409 case 0: /* child */
410 close(pfd[0]);
411 break;
412 default: /* parent */
413 close(pfd[1]);
414 if (read(pfd[0], &status, sizeof(status)) != sizeof(status)) {
415 wait(&status);
416 status = WEXITSTATUS(status);
417 }
1ffd2840 418 return status;
9fe32043 419 }
16ddab0d
DW
420 } else
421 pfd[0] = pfd[1] = -1;
549e9569 422
f5df5d69 423 container = calloc(1, sizeof(*container));
e8a70c89
N
424 container->devnum = devnum;
425 container->devname = devname;
13047e4c 426 container->arrays = NULL;
c1363b40 427 container->subarray[0] = 0;
96a8270d 428 container->sock = -1;
13047e4c
DW
429
430 if (!container->devname) {
431 fprintf(stderr, "mdmon: failed to allocate container name string\n");
432 exit(3);
433 }
434
435 mdi = sysfs_read(mdfd, container->devnum,
7da80e6f 436 GET_VERSION|GET_LEVEL|GET_DEVS|SKIP_GONE_DEVS);
13047e4c
DW
437
438 if (!mdi) {
439 fprintf(stderr, "mdmon: failed to load sysfs info for %s\n",
440 container->devname);
441 exit(3);
442 }
443 if (mdi->array.level != UnSet) {
444 fprintf(stderr, "mdmon: %s is not a container - cannot monitor\n",
1ffd2840 445 devname);
13047e4c
DW
446 exit(3);
447 }
448 if (mdi->array.major_version != -1 ||
449 mdi->array.minor_version != -2) {
450 fprintf(stderr, "mdmon: %s does not use external metadata - cannot monitor\n",
1ffd2840 451 devname);
13047e4c
DW
452 exit(3);
453 }
454
455 container->ss = find_metadata_methods(mdi->text_version);
456 if (container->ss == NULL) {
457 fprintf(stderr, "mdmon: %s uses unknown metadata: %s\n",
1ffd2840 458 devname, mdi->text_version);
13047e4c
DW
459 exit(3);
460 }
461
462 container->devs = NULL;
463 for (di = mdi->devs; di; di = di->next) {
464 struct mdinfo *cd = malloc(sizeof(*cd));
465 *cd = *di;
466 cd->next = container->devs;
467 container->devs = cd;
468 }
469 sysfs_free(mdi);
549e9569 470
883a6142
DW
471 /* SIGUSR is sent between parent and child. So both block it
472 * and enable it only with pselect.
473 */
474 sigemptyset(&set);
475 sigaddset(&set, SIGUSR1);
476 sigaddset(&set, SIGHUP);
477 sigaddset(&set, SIGALRM);
478 sigaddset(&set, SIGTERM);
479 sigprocmask(SIG_BLOCK, &set, NULL);
480 act.sa_handler = wake_me;
481 act.sa_flags = 0;
482 sigaction(SIGUSR1, &act, NULL);
483 sigaction(SIGALRM, &act, NULL);
484 act.sa_handler = hup;
485 sigaction(SIGHUP, &act, NULL);
486 act.sa_handler = term;
487 sigaction(SIGTERM, &act, NULL);
488 act.sa_handler = SIG_IGN;
489 sigaction(SIGPIPE, &act, NULL);
490
13047e4c
DW
491 if (switchroot) {
492 /* we assume we assume that /sys /proc /dev are available in
96a8270d 493 * the new root
13047e4c 494 */
e98ef225 495 if (switchroot[0] == '/') {
24f6f99b 496 victim = mdmon_pid(container->devnum);
e98ef225
N
497 victim_sock = connect_monitor(container->devname);
498 } else {
499 victim = atoi(switchroot);
500 victim_sock = 0;
13047e4c
DW
501 }
502 }
503
13047e4c 504 ignore = chdir("/");
24cfdbc5 505 if (victim < 0) {
b109d928
DW
506 if (ping_monitor(container->devname) == 0) {
507 fprintf(stderr, "mdmon: %s already managed\n",
508 container->devname);
509 exit(3);
24cfdbc5
N
510 }
511 /* if there is a pid file, kill whoever is there just in case */
24f6f99b 512 victim = mdmon_pid(container->devnum);
549e9569 513 }
1ffd2840 514 if (container->ss->load_super(container, mdfd, devname)) {
549e9569 515 fprintf(stderr, "mdmon: Cannot load metadata for %s\n",
1ffd2840 516 devname);
549e9569
NB
517 exit(3);
518 }
e8a70c89 519 close(mdfd);
549e9569 520
9fe32043
N
521 /* Ok, this is close enough. We can say goodbye to our parent now.
522 */
523 status = 0;
3d2c4fc7
DW
524 if (write(pfd[1], &status, sizeof(status)) < 0)
525 fprintf(stderr, "mdmon: failed to notify our parent: %d\n",
526 getppid());
9fe32043
N
527 close(pfd[1]);
528
1373b07d 529 mlockall(MCL_CURRENT | MCL_FUTURE);
549e9569 530
3e70c845 531 if (clone_monitor(container) < 0) {
295646b3 532 fprintf(stderr, "mdmon: failed to start monitor process: %s\n",
549e9569
NB
533 strerror(errno));
534 exit(2);
535 }
536
9f1da824
DW
537 if (victim > -1) {
538 try_kill_monitor(victim, container->devname, victim_sock);
539 close(victim_sock);
540 }
e98ef225
N
541
542 setsid();
543 close(0);
544 open("/dev/null", O_RDWR);
545 close(1);
546 ignore = dup(0);
547#ifndef DEBUG
548 close(2);
549 ignore = dup(0);
550#endif
551
549e9569
NB
552 do_manager(container);
553
554 exit(0);
555}