]> git.ipfire.org Git - thirdparty/mdadm.git/blame - Monitor.c
Fix parsing of "-a" in various contexts.
[thirdparty/mdadm.git] / Monitor.c
CommitLineData
52826846 1/*
9a9dab36 2 * mdadm - manage Linux "md" devices aka RAID arrays.
52826846 3 *
4f589ad0 4 * Copyright (C) 2001-2006 Neil Brown <neilb@suse.de>
52826846
NB
5 *
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author: Neil Brown
22 * Email: <neilb@cse.unsw.edu.au>
23 * Paper: Neil Brown
24 * School of Computer Science and Engineering
25 * The University of New South Wales
26 * Sydney, 2052
27 * Australia
28 */
29
9a9dab36 30#include "mdadm.h"
52826846
NB
31#include "md_p.h"
32#include "md_u.h"
e0d19036 33#include <sys/wait.h>
52826846 34#include <sys/signal.h>
98c6faba 35#include <values.h>
773135f5 36#include <syslog.h>
52826846 37
4948b8f7
NB
38static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom,
39 char *cmd, int dosyslog);
52826846 40
e0d19036
NB
41static char *percentalerts[] = {
42 "RebuildStarted",
43 "Rebuild20",
44 "Rebuild40",
45 "Rebuild60",
46 "Rebuild80",
47};
48
e27d562b
NB
49/* The largest number of disks current arrays can manage is 384
50 * This really should be dynamically, but that will have to wait
51 * At least it isn't MD_SB_DISKS.
52 */
53#define MaxDisks 384
cd29a5c8 54int Monitor(mddev_dev_t devlist,
52826846 55 char *mailaddr, char *alert_cmd,
aa88f531 56 int period, int daemonise, int scan, int oneshot,
8aec876d 57 int dosyslog, int test, char* pidfile)
52826846
NB
58{
59 /*
60 * Every few seconds, scan every md device looking for changes
61 * When a change is found, log it, possibly run the alert command,
62 * and possibly send Email
63 *
64 * For each array, we record:
65 * Update time
66 * active/working/failed/spare drives
67 * State of each device.
e0d19036 68 * %rebuilt if rebuilding
52826846
NB
69 *
70 * If the update time changes, check out all the data again
71 * It is possible that we cannot get the state of each device
72 * due to bugs in the md kernel module.
e0d19036
NB
73 * We also read /proc/mdstat to get rebuild percent,
74 * and to get state on all active devices incase of kernel bug.
52826846 75 *
e0d19036
NB
76 * Events are:
77 * Fail
78 * An active device had Faulty set or Active/Sync removed
79 * FailSpare
80 * A spare device had Faulty set
81 * SpareActive
82 * An active device had a reverse transition
83 * RebuildStarted
84 * percent went from -1 to +ve
85 * Rebuild20 Rebuild40 Rebuild60 Rebuild80
86 * percent went from below to not-below that number
87 * DeviceDisappeared
88 * Couldn't access a device which was previously visible
52826846
NB
89 *
90 * if we detect an array with active<raid and spare==0
91 * we look at other arrays that have same spare-group
92 * If we find one with active==raid and spare>0,
93 * and if we can get_disk_info and find a name
94 * Then we hot-remove and hot-add to the other array
95 *
e0d19036
NB
96 * If devlist is NULL, then we can monitor everything because --scan
97 * was given. We get an initial list from config file and add anything
98 * that appears in /proc/mdstat
52826846
NB
99 */
100
101 struct state {
102 char *devname;
e0d19036 103 int devnum; /* to sync with mdstat info */
52826846
NB
104 long utime;
105 int err;
e0d19036
NB
106 char *spare_group;
107 int active, working, failed, spare, raid;
feb716e9 108 int expected_spares;
e27d562b
NB
109 int devstate[MaxDisks];
110 int devid[MaxDisks];
e0d19036 111 int percent;
52826846
NB
112 struct state *next;
113 } *statelist = NULL;
114 int finished = 0;
e0d19036 115 struct mdstat_ent *mdstat = NULL;
4948b8f7 116 char *mailfrom = NULL;
e0d19036 117
d013a55e 118 if (!mailaddr) {
8aec876d 119 mailaddr = conf_get_mailaddr();
d013a55e 120 if (mailaddr && ! scan)
56eedc1a 121 fprintf(stderr, Name ": Monitor using email address \"%s\" from config file\n",
d013a55e
NB
122 mailaddr);
123 }
8aec876d 124 mailfrom = conf_get_mailfrom();
4948b8f7 125
d013a55e 126 if (!alert_cmd) {
8aec876d 127 alert_cmd = conf_get_program();
d013a55e 128 if (alert_cmd && ! scan)
56eedc1a 129 fprintf(stderr, Name ": Monitor using program \"%s\" from config file\n",
d013a55e
NB
130 alert_cmd);
131 }
56eedc1a
NB
132 if (scan && !mailaddr && !alert_cmd) {
133 fprintf(stderr, Name ": No mail address or alert command - not monitoring.\n");
d013a55e 134 return 1;
56eedc1a 135 }
d013a55e
NB
136
137 if (daemonise) {
138 int pid = fork();
139 if (pid > 0) {
b5e64645
NB
140 if (!pidfile)
141 printf("%d\n", pid);
142 else {
143 FILE *pid_file;
144 pid_file=fopen(pidfile, "w");
145 if (!pid_file)
146 perror("cannot create pid file");
147 else {
148 fprintf(pid_file,"%d\n", pid);
149 fclose(pid_file);
150 }
151 }
d013a55e
NB
152 return 0;
153 }
154 if (pid < 0) {
155 perror("daemonise");
156 return 1;
157 }
158 close(0);
159 open("/dev/null", 3);
160 dup2(0,1);
161 dup2(0,2);
162 setsid();
163 }
e0d19036
NB
164
165 if (devlist == NULL) {
8aec876d 166 mddev_ident_t mdlist = conf_get_ident(NULL);
e0d19036
NB
167 for (; mdlist; mdlist=mdlist->next) {
168 struct state *st = malloc(sizeof *st);
169 if (st == NULL)
170 continue;
171 st->devname = strdup(mdlist->devname);
172 st->utime = 0;
173 st->next = statelist;
d013a55e 174 st->err = 0;
98c6faba 175 st->devnum = MAXINT;
e0d19036 176 st->percent = -2;
feb716e9 177 st->expected_spares = mdlist->spare_disks;
e0d19036
NB
178 if (mdlist->spare_group)
179 st->spare_group = strdup(mdlist->spare_group);
180 else
181 st->spare_group = NULL;
182 statelist = st;
183 }
184 } else {
cd29a5c8 185 mddev_dev_t dv;
e0d19036 186 for (dv=devlist ; dv; dv=dv->next) {
8aec876d 187 mddev_ident_t mdlist = conf_get_ident(dv->devname);
e0d19036
NB
188 struct state *st = malloc(sizeof *st);
189 if (st == NULL)
190 continue;
191 st->devname = strdup(dv->devname);
192 st->utime = 0;
193 st->next = statelist;
d013a55e 194 st->err = 0;
98c6faba 195 st->devnum = MAXINT;
e0d19036 196 st->percent = -2;
feb716e9 197 st->expected_spares = -1;
e0d19036 198 st->spare_group = NULL;
e5329c37
NB
199 if (mdlist) {
200 st->expected_spares = mdlist->spare_disks;
201 if (mdlist->spare_group)
202 st->spare_group = strdup(mdlist->spare_group);
203 }
e0d19036
NB
204 statelist = st;
205 }
206 }
207
208
209 while (! finished) {
aa88f531 210 int new_found = 0;
e0d19036
NB
211 struct state *st;
212
213 if (mdstat)
214 free_mdstat(mdstat);
22a88995 215 mdstat = mdstat_read(oneshot?0:1, 0);
e0d19036
NB
216
217 for (st=statelist; st; st=st->next) {
ab5303d6 218 struct { int state, major, minor; } info[MaxDisks];
52826846 219 mdu_array_info_t array;
dd0781e5 220 struct mdstat_ent *mse = NULL, *mse2;
e0d19036 221 char *dev = st->devname;
52826846 222 int fd;
98c6faba 223 unsigned int i;
e0d19036 224
98c6faba 225 if (test)
4948b8f7 226 alert("TestMessage", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
52826846
NB
227 fd = open(dev, O_RDONLY);
228 if (fd < 0) {
229 if (!st->err)
e0d19036 230 alert("DeviceDisappeared", dev, NULL,
4948b8f7 231 mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 232/* fprintf(stderr, Name ": cannot open %s: %s\n",
52826846 233 dev, strerror(errno));
e0d19036 234*/ st->err=1;
52826846
NB
235 continue;
236 }
e4dc5106 237 fcntl(fd, F_SETFD, FD_CLOEXEC);
52826846
NB
238 if (ioctl(fd, GET_ARRAY_INFO, &array)<0) {
239 if (!st->err)
e0d19036 240 alert("DeviceDisappeared", dev, NULL,
4948b8f7 241 mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 242/* fprintf(stderr, Name ": cannot get array info for %s: %s\n",
52826846 243 dev, strerror(errno));
e0d19036 244*/ st->err=1;
52826846
NB
245 close(fd);
246 continue;
247 }
e5329c37
NB
248 if (array.level != 1 && array.level != 5 && array.level != -4 &&
249 array.level != 6 && array.level != 10) {
5787fa49
NB
250 if (!st->err)
251 alert("DeviceDisappeared", dev, "Wrong-Level",
4948b8f7 252 mailaddr, mailfrom, alert_cmd, dosyslog);
5787fa49
NB
253 st->err = 1;
254 close(fd);
255 continue;
256 }
98c6faba 257 if (st->devnum == MAXINT) {
e0d19036
NB
258 struct stat stb;
259 if (fstat(fd, &stb) == 0 &&
98c6faba 260 (S_IFMT&stb.st_mode)==S_IFBLK) {
0df46c2a
NB
261 if (major(stb.st_rdev) == MD_MAJOR)
262 st->devnum = minor(stb.st_rdev);
98c6faba 263 else
0df46c2a 264 st->devnum = -1- (minor(stb.st_rdev)>>6);
98c6faba 265 }
e0d19036
NB
266 }
267
dd0781e5
NB
268 for (mse2 = mdstat ; mse2 ; mse2=mse2->next)
269 if (mse2->devnum == st->devnum) {
270 mse2->devnum = MAXINT; /* flag it as "used" */
271 mse = mse2;
272 }
e0d19036 273
52826846 274 if (st->utime == array.utime &&
e0d19036
NB
275 st->failed == array.failed_disks &&
276 st->working == array.working_disks &&
277 st->spare == array.spare_disks &&
278 (mse == NULL || (
279 mse->percent == st->percent
280 ))) {
52826846 281 close(fd);
e0d19036 282 st->err = 0;
52826846
NB
283 continue;
284 }
aa88f531
NB
285 if (st->utime == 0 && /* new array */
286 mse && /* is in /proc/mdstat */
287 mse->pattern && strchr(mse->pattern, '_') /* degraded */
288 )
4948b8f7 289 alert("DegradedArray", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 290
feb716e9
NB
291 if (st->utime == 0 && /* new array */
292 st->expected_spares > 0 &&
293 array.spare_disks < st->expected_spares)
4948b8f7 294 alert("SparesMissing", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
295 if (mse &&
296 st->percent == -1 &&
297 mse->percent >= 0)
4948b8f7 298 alert("RebuildStarted", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
299 if (mse &&
300 st->percent >= 0 &&
301 mse->percent >= 0 &&
302 (mse->percent / 20) > (st->percent / 20))
303 alert(percentalerts[mse->percent/20],
4948b8f7 304 dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036 305
98c6faba
NB
306 if (mse &&
307 mse->percent == -1 &&
37dfc3d6
NB
308 st->percent >= 0) {
309 /* Rebuild/sync/whatever just finished.
310 * If there is a number in /mismatch_cnt,
311 * we should report that.
312 */
313 struct sysarray *sra =
314 sysfs_read(-1, st->devnum, GET_MISMATCH);
315 if (sra && sra->mismatch_cnt > 0) {
316 char cnt[40];
317 sprintf(cnt, " mismatches found: %d", sra->mismatch_cnt);
318 alert("RebuildFinished", dev, cnt, mailaddr, mailfrom, alert_cmd, dosyslog);
319 } else
320 alert("RebuildFinished", dev, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
321 if (sra)
322 free(sra);
323 }
98c6faba 324
e0d19036
NB
325 if (mse)
326 st->percent = mse->percent;
e27d562b 327
ab5303d6
NB
328
329 for (i=0; i<MaxDisks && i <= array.raid_disks + array.nr_disks;
330 i++) {
331 mdu_disk_info_t disc;
01d9299c 332 disc.number = i;
ab5303d6
NB
333 if (ioctl(fd, GET_DISK_INFO, &disc) >= 0) {
334 info[i].state = disc.state;
335 info[i].major = disc.major;
336 info[i].minor = disc.minor;
337 } else
338 info[i].major = info[i].minor = 0;
339 }
340 close(fd);
341
e27d562b 342 for (i=0; i<MaxDisks; i++) {
f8409e54 343 mdu_disk_info_t disc = {0};
e0d19036
NB
344 int newstate=0;
345 int change;
346 char *dv = NULL;
52826846 347 disc.number = i;
e27d562b
NB
348 if (i > array.raid_disks + array.nr_disks) {
349 newstate = 0;
350 disc.major = disc.minor = 0;
ab5303d6
NB
351 } else if (info[i].major || info[i].minor) {
352 newstate = info[i].state;
353 dv = map_dev(info[i].major, info[i].minor, 1);
354 disc.state = newstate;
355 disc.major = info[i].major;
356 disc.minor = info[i].minor;
e27d562b 357 } else if (mse && mse->pattern && i < strlen(mse->pattern)) {
e0d19036
NB
358 switch(mse->pattern[i]) {
359 case 'U': newstate = 6 /* ACTIVE/SYNC */; break;
360 case '_': newstate = 0; break;
52826846 361 }
e27d562b
NB
362 disc.major = disc.minor = 0;
363 }
fe394e5e
NB
364 if (dv == NULL && st->devid[i])
365 dv = map_dev(major(st->devid[i]),
16c6fa80 366 minor(st->devid[i]), 1);
e0d19036
NB
367 change = newstate ^ st->devstate[i];
368 if (st->utime && change && !st->err) {
98c6faba 369 if (i < (unsigned)array.raid_disks &&
e0d19036
NB
370 (((newstate&change)&(1<<MD_DISK_FAULTY)) ||
371 ((st->devstate[i]&change)&(1<<MD_DISK_ACTIVE)) ||
372 ((st->devstate[i]&change)&(1<<MD_DISK_SYNC)))
373 )
4948b8f7 374 alert("Fail", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 375 else if (i >= (unsigned)array.raid_disks &&
e0d19036 376 (disc.major || disc.minor) &&
0df46c2a 377 st->devid[i] == makedev(disc.major, disc.minor) &&
e0d19036
NB
378 ((newstate&change)&(1<<MD_DISK_FAULTY))
379 )
4948b8f7 380 alert("FailSpare", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
98c6faba 381 else if (i < (unsigned)array.raid_disks &&
e0d19036
NB
382 (((st->devstate[i]&change)&(1<<MD_DISK_FAULTY)) ||
383 ((newstate&change)&(1<<MD_DISK_ACTIVE)) ||
384 ((newstate&change)&(1<<MD_DISK_SYNC)))
385 )
4948b8f7 386 alert("SpareActive", dev, dv, mailaddr, mailfrom, alert_cmd, dosyslog);
52826846 387 }
3e6944b2 388 st->devstate[i] = newstate;
0df46c2a 389 st->devid[i] = makedev(disc.major, disc.minor);
52826846 390 }
52826846
NB
391 st->active = array.active_disks;
392 st->working = array.working_disks;
393 st->spare = array.spare_disks;
394 st->failed = array.failed_disks;
395 st->utime = array.utime;
e0d19036
NB
396 st->raid = array.raid_disks;
397 st->err = 0;
52826846 398 }
e0d19036
NB
399 /* now check if there are any new devices found in mdstat */
400 if (scan) {
401 struct mdstat_ent *mse;
402 for (mse=mdstat; mse; mse=mse->next)
98c6faba 403 if (mse->devnum != MAXINT &&
5787fa49
NB
404 (strcmp(mse->level, "raid1")==0 ||
405 strcmp(mse->level, "raid5")==0 ||
406 strcmp(mse->level, "multipath")==0)
407 ) {
e0d19036 408 struct state *st = malloc(sizeof *st);
98c6faba
NB
409 mdu_array_info_t array;
410 int fd;
e0d19036
NB
411 if (st == NULL)
412 continue;
413 st->devname = strdup(get_md_name(mse->devnum));
98c6faba
NB
414 if ((fd = open(st->devname, O_RDONLY)) < 0 ||
415 ioctl(fd, GET_ARRAY_INFO, &array)< 0) {
416 /* no such array */
417 if (fd >=0) close(fd);
8bd2e0c3 418 put_md_name(st->devname);
98c6faba
NB
419 free(st->devname);
420 free(st);
421 continue;
422 }
dd0781e5 423 close(fd);
e0d19036
NB
424 st->utime = 0;
425 st->next = statelist;
426 st->err = 1;
427 st->devnum = mse->devnum;
428 st->percent = -2;
429 st->spare_group = NULL;
98c6faba 430 st->expected_spares = -1;
e0d19036 431 statelist = st;
4948b8f7 432 alert("NewArray", st->devname, NULL, mailaddr, mailfrom, alert_cmd, dosyslog);
aa88f531 433 new_found = 1;
e0d19036
NB
434 }
435 }
436 /* If an array has active < raid && spare == 0 && spare_group != NULL
437 * Look for another array with spare > 0 and active == raid and same spare_group
438 * if found, choose a device and hotremove/hotadd
439 */
440 for (st = statelist; st; st=st->next)
441 if (st->active < st->raid &&
442 st->spare == 0 &&
443 st->spare_group != NULL) {
444 struct state *st2;
445 for (st2=statelist ; st2 ; st2=st2->next)
446 if (st2 != st &&
447 st2->spare > 0 &&
448 st2->active == st2->raid &&
449 st2->spare_group != NULL &&
450 strcmp(st->spare_group, st2->spare_group) == 0) {
451 /* try to remove and add */
452 int fd1 = open(st->devname, O_RDONLY);
453 int fd2 = open(st2->devname, O_RDONLY);
454 int dev = -1;
455 int d;
456 if (fd1 < 0 || fd2 < 0) {
457 if (fd1>=0) close(fd1);
458 if (fd2>=0) close(fd2);
459 continue;
460 }
e27d562b 461 for (d=st2->raid; d < MaxDisks; d++) {
e0d19036
NB
462 if (st2->devid[d] > 0 &&
463 st2->devstate[d] == 0) {
464 dev = st2->devid[d];
465 break;
466 }
467 }
468 if (dev > 0) {
469 if (ioctl(fd2, HOT_REMOVE_DISK,
470 (unsigned long)dev) == 0) {
471 if (ioctl(fd1, HOT_ADD_DISK,
472 (unsigned long)dev) == 0) {
4948b8f7 473 alert("MoveSpare", st->devname, st2->devname, mailaddr, mailfrom, alert_cmd, dosyslog);
e0d19036
NB
474 close(fd1);
475 close(fd2);
476 break;
477 }
478 else ioctl(fd2, HOT_ADD_DISK, (unsigned long) dev);
479 }
480 }
481 close(fd1);
482 close(fd2);
483 }
484 }
aa88f531
NB
485 if (!new_found) {
486 if (oneshot)
487 break;
488 else
dd0781e5 489 mdstat_wait(period);
aa88f531 490 }
98c6faba 491 test = 0;
52826846 492 }
b5e64645
NB
493 if (pidfile)
494 unlink(pidfile);
52826846
NB
495 return 0;
496}
497
498
4948b8f7 499static void alert(char *event, char *dev, char *disc, char *mailaddr, char *mailfrom, char *cmd,
773135f5 500 int dosyslog)
52826846 501{
773135f5
NB
502 int priority;
503
cd29a5c8
NB
504 if (!cmd && !mailaddr) {
505 time_t now = time(0);
506
e0d19036 507 printf("%1.15s: %s on %s %s\n", ctime(&now)+4, event, dev, disc?disc:"unknown device");
cd29a5c8 508 }
52826846
NB
509 if (cmd) {
510 int pid = fork();
511 switch(pid) {
512 default:
513 waitpid(pid, NULL, 0);
514 break;
515 case -1:
516 break;
517 case 0:
518 execl(cmd, cmd, event, dev, disc, NULL);
519 exit(2);
520 }
521 }
aa88f531
NB
522 if (mailaddr &&
523 (strncmp(event, "Fail", 4)==0 ||
98c6faba 524 strncmp(event, "Test", 4)==0 ||
d1732eeb 525 strncmp(event, "Spares", 6)==0 ||
aa88f531 526 strncmp(event, "Degrade", 7)==0)) {
52826846
NB
527 FILE *mp = popen(Sendmail, "w");
528 if (mp) {
008e1100 529 FILE *mdstat;
52826846
NB
530 char hname[256];
531 gethostname(hname, sizeof(hname));
532 signal(SIGPIPE, SIG_IGN);
4948b8f7
NB
533 if (mailfrom)
534 fprintf(mp, "From: %s\n", mailfrom);
535 else
536 fprintf(mp, "From: " Name " monitoring <root>\n");
52826846
NB
537 fprintf(mp, "To: %s\n", mailaddr);
538 fprintf(mp, "Subject: %s event on %s:%s\n\n", event, dev, hname);
539
540 fprintf(mp, "This is an automatically generated mail message from " Name "\n");
541 fprintf(mp, "running on %s\n\n", hname);
542
543 fprintf(mp, "A %s event had been detected on md device %s.\n\n", event, dev);
544
37dfc3d6 545 if (disc && disc[0] != ' ')
56eedc1a 546 fprintf(mp, "It could be related to component device %s.\n\n", disc);
37dfc3d6
NB
547 if (disc && disc[0] == ' ')
548 fprintf(mp, "Extra information:%s.\n\n", disc);
52826846
NB
549
550 fprintf(mp, "Faithfully yours, etc.\n");
008e1100
NB
551
552 mdstat = fopen("/proc/mdstat", "r");
553 if (mdstat) {
554 char buf[8192];
555 int n;
a524a7ee 556 fprintf(mp, "\nP.S. The /proc/mdstat file currently contains the following:\n\n");
008e1100 557 while ( (n=fread(buf, 1, sizeof(buf), mdstat)) > 0)
9fca7d62 558 n=fwrite(buf, 1, n, mp); /* yes, i don't care about the result */
008e1100
NB
559 fclose(mdstat);
560 }
52826846
NB
561 fclose(mp);
562 }
563
564 }
773135f5
NB
565
566 /* log the event to syslog maybe */
567 if (dosyslog) {
568 /* Log at a different severity depending on the event.
569 *
570 * These are the critical events: */
571 if (strncmp(event, "Fail", 4)==0 ||
572 strncmp(event, "Degrade", 7)==0 ||
573 strncmp(event, "DeviceDisappeared", 17)==0)
574 priority = LOG_CRIT;
575 /* Good to know about, but are not failures: */
576 else if (strncmp(event, "Rebuild", 7)==0 ||
d1732eeb
NB
577 strncmp(event, "MoveSpare", 9)==0 ||
578 strncmp(event, "Spares", 6) != 0)
773135f5
NB
579 priority = LOG_WARNING;
580 /* Everything else: */
581 else
582 priority = LOG_INFO;
583
584 if (disc)
585 syslog(priority, "%s event detected on md device %s, component device %s", event, dev, disc);
586 else
587 syslog(priority, "%s event detected on md device %s", event, dev);
588 }
52826846 589}
b90c0e9a
NB
590
591/* Not really Monitor but ... */
592int Wait(char *dev)
593{
594 struct stat stb;
595 int devnum;
596 int rv = 1;
597
598 if (stat(dev, &stb) != 0) {
599 fprintf(stderr, Name ": Cannot find %s: %s\n", dev,
600 strerror(errno));
601 return 2;
602 }
603 if (major(stb.st_rdev) == MD_MAJOR)
604 devnum = minor(stb.st_rdev);
605 else
8382f19b 606 devnum = -1-(minor(stb.st_rdev)/64);
b90c0e9a
NB
607
608 while(1) {
609 struct mdstat_ent *ms = mdstat_read(1, 0);
610 struct mdstat_ent *e;
611
612 for (e=ms ; e; e=e->next)
613 if (e->devnum == devnum)
614 break;
615
616 if (!e || e->percent < 0) {
617 free_mdstat(ms);
618 return rv;
619 }
620 free(ms);
621 rv = 0;
622 mdstat_wait(5);
623 }
624}