summaryrefslogtreecommitdiffstats
path: root/usr.sbin/diskcheckd/diskcheckd.c
blob: 8abdb9d983d076c1caf691341d9d187d9426e237 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/*-
 * Copyright (c) 2000, 2001 Ben Smithurst <ben@FreeBSD.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

static const char rcsid[] =
	"$FreeBSD$";

#include <sys/types.h>
#include <sys/sysctl.h>

#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#define DKTYPENAMES
#include <sys/disklabel.h>

#define	_PATH_CONF	"/etc/diskcheckd.conf"
#define	_PATH_SAVE	_PATH_VARDB"diskcheckd.offsets"

#define	MAXRATE	(128 << 10)
#define	MINRATE	512

struct disk {
	int fd;
	char *device;
	off_t size;
	int secsize;
	int days, rate, errors;
};

volatile sig_atomic_t got_sighup = 0, got_sigterm = 0;

char **getdisknames(void);
off_t dseek(struct disk *, off_t, int);
struct disk *readconf(const char *);
void getdisksize(struct disk *);
void logreaderror(struct disk *, int);
void readchunk(struct disk *, char *);
void readoffsets(struct disk *, const char *);
void setdiskrate(struct disk *);
void sighup(int);
void sigterm(int);
void updateproctitle(struct disk *);
void usage(void);
void writeoffsets(struct disk *, const char *);

int
main(int argc, char *argv[]) {
	char *buf;
	struct disk *disks, *dp;
	int ch, ok;
	struct sigaction sa;
	int counter, debug;
	const char *conf_file, *save_file;

	conf_file = _PATH_CONF;
	save_file = _PATH_SAVE;
	debug = 0;

	while ((ch = getopt(argc, argv, "df:o:")) != -1)
		switch (ch) {
		case 'd':
			debug = 1;
			break;
		case 'f':
			conf_file = optarg;
			break;
		case 'o':
			save_file = optarg;
			break;
		default:
			usage();
			/* NOTREACHED */
		}

	argv += optind;
	argc -= optind;

	if (argc != 0)
		usage();

	openlog("diskcheckd", LOG_CONS|LOG_PID, LOG_DAEMON);

	if (!debug && daemon(0, 0) < 0) {
		syslog(LOG_NOTICE, "daemon failure: %m");
		exit(EXIT_FAILURE);
	}

	sa.sa_handler = sigterm;
	sa.sa_flags = SA_RESTART;
	sigemptyset(&sa.sa_mask);
	sigaction(SIGTERM, &sa, NULL);
	sigaction(SIGINT, &sa, NULL);

	sa.sa_handler = sighup;
	sigaction(SIGHUP, &sa, NULL);

	/* Read the configuration file and the saved offsets */
	disks = readconf(conf_file);
	readoffsets(disks, save_file);

	if ((buf = malloc(MAXRATE)) == NULL) {
		syslog(LOG_NOTICE, "malloc failure: %m");
		exit(EXIT_FAILURE);
	}

	/* The main disk checking loop. */
	counter = 0;
	while (!got_sigterm) {
		ok = 0;
		for (dp = disks; dp->device != NULL; dp++)
			if (dp->fd != -1) {
				ok = 1;
				readchunk(dp, buf);
			}

		if (!ok) {
			syslog(LOG_EMERG, "all disks had read errors");
			exit(EXIT_FAILURE);
		}

		if (counter % 300 == 0) {
			updateproctitle(disks);
			writeoffsets(disks, save_file);
		}

		counter++;
		sleep(1);

		if (got_sighup) {
			/*
			 * Got a SIGHUP, so save the offsets, free the
			 * memory used for the disk structures, and then
			 * re-read the config file and the disk offsets.
			 */
			writeoffsets(disks, save_file);
			for (dp = disks; dp->device != NULL; dp++) {
				free(dp->device);
				close(dp->fd);
			}
			free(disks);
			disks = readconf(conf_file);
			readoffsets(disks, save_file);
			got_sighup = 0;
		}
	}

	writeoffsets(disks, save_file);
	return (EXIT_SUCCESS);
}

/*
 * Read the next chunk from the specified disk, retrying if necessary.
 */
void
readchunk(struct disk *dp, char *buf) {
	ssize_t n;
	int s;

	n = read(dp->fd, buf, dp->rate);
	if (n == 0) {
		eof:
		syslog(LOG_INFO, "reached end of %s with %d errors",
		    dp->device, dp->errors);
		dseek(dp, 0, SEEK_SET);
		dp->errors = 0;
		return;
	} else if (n > 0)
		return;

	/*
	 * Read error, retry in smaller chunks.
	 */
	logreaderror(dp, dp->rate);

	for (s = 0; s < dp->rate; s += 512) {
		n = read(dp->fd, buf, 512);
		if (n == 0)
			goto eof;
		else if (n < 0) {
			/* log the error and seek past it. */
			logreaderror(dp, 512);
			dseek(dp, 512, SEEK_CUR);
		}
	}
}

const char *
fstypename(u_int8_t type) {
	static char buf[32];

	if (type < FSMAXTYPES)
		return (fstypenames[type]);
	else {
		snprintf(buf, sizeof buf, "%u", type);
		return (buf);
	}
}

/*
 * Report a read error, logging how many bytes were trying to be read, which
 * sector they were being read from, and try to also find out what that sector
 * is used for.
 */
void
logreaderror(struct disk *dp, int nbytes) {
	quad_t secno;
	off_t saved_offset;
	int fd, slice, part;
	struct dos_partition *dos;
	struct disklabel label;
	char buf[512];
	char newdev[512];

	saved_offset = dseek(dp, 0, SEEK_CUR);
	secno = (quad_t)saved_offset / dp->secsize;
	dp->errors++;

	syslog(LOG_NOTICE, "error reading %d bytes from sector %qd on %s",
	    nbytes, secno, dp->device);

	/*
	 * First, find out which slice it's in.  To do this, we seek to the
	 * start of the disk, read the first sector, and go through the DOS
	 * slice table.
	 */
	if (dseek(dp, 0, SEEK_SET) == -1)
		exit(EXIT_FAILURE);

	if (read(dp->fd, buf, sizeof buf) != sizeof buf) {
		dseek(dp, saved_offset, SEEK_SET);
		return;
	}

	/* seek back to where we were */
	if (dseek(dp, saved_offset, SEEK_SET) == -1)
		exit(EXIT_FAILURE);

	dos = (struct dos_partition *)&buf[DOSPARTOFF];
	for (slice = 0; slice < NDOSPART; slice++)
		if (dos[slice].dp_start <= secno &&
		  secno < dos[slice].dp_start + dos[slice].dp_size)
			break;

	if (slice == NDOSPART) {
		syslog(LOG_NOTICE,
		  "sector %qd on %s doesn't appear "
		  "to be within any DOS slice", secno, dp->device);
		return;
	}

	/* Make secno relative to this slice */
	secno -= dos[slice].dp_start;

	snprintf(newdev, sizeof newdev, "%ss%d", dp->device, slice + 1);
	syslog(LOG_DEBUG, "bad sector seems to be within %s", newdev);

	/* Check the type of that partition. */
	if (dos[slice].dp_typ != DOSPTYP_386BSD) {
		/* If not a BSD slice, we can't do much more. */
		syslog(LOG_NOTICE, "last bad sector is sector %qd "
		    "on device %s, type %02x", secno, newdev,
		    dos[slice].dp_typ);
		return;
	}

	if ((fd = open(newdev, O_RDONLY)) < 0) {
		syslog(LOG_NOTICE, "open %s failure: %m", newdev);
		return;
	}

	/* Try to read the disklabel from that device. */
	if (ioctl(fd, DIOCGDINFO, &label) < 0) {
		syslog(LOG_NOTICE, "DIOCGDINFO on %s failed: %m",
		    newdev);
		return;
	}

	/* Check which partition this sector is in. */
	for (part = 0; part < MAXPARTITIONS; part++)
		if (part != 2 && /* skip 'c' partition */
		    label.d_partitions[part].p_offset <= secno &&
		    secno < label.d_partitions[part].p_offset +
		            label.d_partitions[part].p_size)
			break;

	if (part == MAXPARTITIONS) {
		syslog(LOG_NOTICE,
		  "sector %qd on %s doesn't appear "
		  "to be within any BSD partition", secno, newdev);
		return;
	}

	secno -= label.d_partitions[part].p_offset;
	snprintf(newdev, sizeof newdev, "%ss%d%c",
	    dp->device, slice + 1, 'a' + part);
	syslog(LOG_DEBUG, "bad sector seems to be within %s", newdev);
	if (label.d_partitions[part].p_fstype != FS_BSDFFS) {
		/* Again, if not a BSD partition, can't do much. */
		syslog(LOG_NOTICE, "last bad sector is sector %qd "
		    "on device %s, type %s", secno, newdev,
		    fstypename(label.d_partitions[part].p_fstype));
		return;
	}

	syslog(LOG_NOTICE, "last bad sector is sector %qd "
	  "on 4.2BSD filesystem %s", secno, newdev);

	/*
	 * XXX: todo: find out which file on the BSD filesystem uses this
	 * sector...
	 */
}

/*
 * Read the offsets written by writeoffsets().
 */
void
readoffsets(struct disk *disks, const char *save_file) {
	FILE *fp;
	struct disk *dp;
	char *space, buf[1024];

	if ((fp = fopen(save_file, "r")) == NULL) {
		if (errno != ENOENT)
			syslog(LOG_NOTICE, "open %s failed: %m", save_file);
		return;
	}

	while (fgets(buf, sizeof buf, fp) != NULL) {
		if ((space = strchr(buf, ' ')) == NULL)
			continue;
		*space = '\0';

		for (dp = disks;
		    dp->device != NULL && strcmp(dp->device, buf) != 0; dp++)
			; /* nothing */

		if (dp->device != NULL)
			dseek(dp, (off_t)strtoq(space + 1, NULL, 0), SEEK_SET);
	}
	fclose(fp);
}

/*
 * Save the offsets we've reached for each disk to a file, so we can start
 * at that position next time the daemon is started.
 */
void
writeoffsets(struct disk *disks, const char *save_file) {
	FILE *fp;
	struct disk *dp;

	if ((fp = fopen(save_file, "w")) == NULL) {
		syslog(LOG_NOTICE, "open %s failed: %m", save_file);
		return;
	}

	for (dp = disks; dp->device != NULL; dp++)
		if (strcmp(dp->device, "*") != 0)
			fprintf(fp, "%s %qd\n", dp->device,
			    (quad_t)dseek(dp, 0, SEEK_CUR));
	fclose(fp);
}

/*
 * Set the process title so it's easy to see using ps(1) how much has been
 * done.
 */
void
updateproctitle(struct disk *disks) {
	struct disk *dp;
	char *bp, *p;
	static char *buf;
	static size_t bufsize;
	size_t size;
	int inc, ret;
	double percent;

	bp = buf;
	size = bufsize;
	for (dp = disks; dp->device != NULL; dp++) {
		p = dp->device;
		if (strcmp(p, "*") == 0)
			continue;
		if (strncmp(p, _PATH_DEV, sizeof _PATH_DEV - 1) == 0)
			p += sizeof _PATH_DEV - 1;

		percent = 100 * (double)dseek(dp, 0, SEEK_CUR) / dp->size;
		if ((size_t)(ret = snprintf(bp, size,
		    "%s %.2f%%, ", p, percent)) >= size) {
			inc = ((ret + 1023) >> 10) << 10;
			size += inc;
			bufsize += inc;
			if ((buf = reallocf(buf, bufsize)) == NULL) {
				/* Not fatal. */
				syslog(LOG_NOTICE, "reallocf failure: %m");
				bufsize = 0;
				return;
			}
			bp = buf + bufsize - size;
			ret = snprintf(bp, size, "%s %.2f%%, ", p, percent);
		}
		bp += ret;
		size -= ret;
	}

	if (buf != NULL) {
		/* Remove the trailing comma. */
		if (&bp[-2] >= buf)
			bp[-2] = '\0';
		setproctitle("%s", buf);
	}
}

/* used to keep track of which fields have been specified */
#define FL_SIZE_SPEC 1
#define FL_DAYS_SPEC 2
#define FL_RATE_SPEC 4

/*
 * Read the configuration file, set the rate appropriately for each disk,
 * and get a file descriptor for each disk.
 */
struct disk *
readconf(const char *conf_file) {
	FILE *fp;
	char buf[1024], *line, *field, *ep, **np, **np0;
	int fields, flags;
	struct disk *disks, *dp, *odisks;
	int numdisks, onumdisks;
	double dval;
	long lval;
	int linenum;

	if ((fp = fopen(conf_file, "r")) == NULL) {
		syslog(LOG_NOTICE, "open %s failure: %m", conf_file);
		exit(EXIT_FAILURE);
	}

	numdisks = 0;
	disks = NULL;
	linenum = 0;

	/* Step 1: read and parse the configuration file. */
	while (fgets(buf, sizeof buf, fp) != NULL) {
		line = buf;
		linenum++;
		while (isspace(*line))
			line++;
		if (*line == '#' || *line == '\n' || *line == '\0')
			continue;
		fields = flags = 0;
		while ((field = strsep(&line, " \t\n")) != NULL) {
			if (*field == '\0')
				continue;

			/*
			 * If this is the first field on a line, allocate
			 * space for one more disk structure.
			 */
			if (fields == 0 && (disks = reallocf(disks,
			    (numdisks + 1) * sizeof (*disks))) == NULL) {
				syslog(LOG_NOTICE, "reallocf failure: %m");
				exit(EXIT_FAILURE);
			}

			dp = &disks[numdisks];
			switch (fields++) {
			case 0:
				/* device name */
				if ((dp->device = strdup(field)) == NULL) {
					syslog(LOG_NOTICE,
					    "strdup failure: %m");
					exit(EXIT_FAILURE);
				}
				dp->fd = -1;
				dp->rate = -1;
				dp->size = -1;
				break;
			case 1:
				/* size */
				if (strcmp(field, "*") == 0)
					break;
				flags |= FL_SIZE_SPEC;
				dval = strtod(field, &ep);
				if (dval < 0) {
					syslog(LOG_NOTICE,
					    "%s:%d: size cannot be negative",
					    conf_file, linenum);
					exit(EXIT_FAILURE);
				}
				if (strcasecmp(ep, "M") == 0)
					dp->size = dval * 1024 * 1024;
				else if (strcasecmp(ep, "G") == 0)
					dp->size = dval * 1024 * 1024 * 1024;
				else if (*ep == '\0')
					dp->size = dval;
				else {
					syslog(LOG_NOTICE,
					    "%s:%d: bad suffix \"%s\" on "
					    "size \"%s\"", conf_file,
					    linenum, ep, field);
					exit(EXIT_FAILURE);
				}
				break;
			case 2:
				/* days */
				if (strcmp(field, "*") == 0)
					break;
				flags |= FL_DAYS_SPEC;
				lval = strtol(field, &ep, 0);
				if (ep == field || *ep != '\0' ||
				    lval <= 0) {
					syslog(LOG_NOTICE,
					    "%s:%d: bad number of days",
					    conf_file, linenum);
					exit(EXIT_FAILURE);
				}
				dp->days = lval;
				break;
			case 3:
				/* rate */
				if (strcmp(field, "*") == 0)
					break;
				flags |= FL_RATE_SPEC;
				lval = strtol(field, &ep, 0);
				if (ep == field || *ep != '\0' ||
				    lval <= 0) {
					syslog(LOG_NOTICE, "%s:%d: bad rate",
					    conf_file, linenum);
					exit(EXIT_FAILURE);
				}
				dp->rate = lval * 1024;
				break;
			default:
				/* Report error at end of line. */
				break;
			}
		}
		if (fields != 4) {
			syslog(LOG_NOTICE,
			    "%s:%d: %d fields, should be 4",
			    conf_file, linenum, fields);
			exit(EXIT_FAILURE);
		}
		if (flags != (FL_SIZE_SPEC|FL_DAYS_SPEC) &&
		    flags != FL_DAYS_SPEC &&
		    flags != FL_RATE_SPEC) {
			syslog(LOG_NOTICE,
			    "%s:%d: should specify frequency "
			    "or rate, not both", conf_file,
			    linenum);
			exit(EXIT_FAILURE);
		}
		numdisks++;
	}
	fclose(fp);

	if (numdisks == 0) {
		syslog(LOG_NOTICE, "no disks specified");
		exit(EXIT_FAILURE);
	}

	/*
	 * Step 2: expand any dp->device == "*" entries.  onumdisks is used
	 * so that we don't keep checking dp->device for the new disks as
	 * they're added, since that's pointless.
	 */
	onumdisks = numdisks;
	for (dp = disks; dp < disks + onumdisks; dp++) {
		if (strcmp(dp->device, "*") == 0) {
			for (np = np0 = getdisknames(); *np != NULL; np++) {
				odisks = disks;
				if ((disks = reallocf(disks,
				    (numdisks + 1) * sizeof (*disks))) == NULL) {
					syslog(LOG_NOTICE,
					  "reallocf failure: %m");
					exit(EXIT_FAILURE);
				}

				/* "disks" pointer may have changed. */
				dp = disks + (dp - odisks);

				disks[numdisks].rate = dp->rate;
				disks[numdisks].size = dp->size;
				disks[numdisks].days = dp->days;
				disks[numdisks].device = *np;
				numdisks++;
			}

			/* Don't free the individual pointers, since they're
			 * used as new dp->device entries.
			 */
			free(np0);
		}
	}

	/* Step 3: open all the disks and set the rate to check at
	 * appropriately.  Use "onumdisks" because numdisks changes within
	 * the loop.
	 */
	onumdisks = numdisks;
	for (dp = disks; dp < disks + onumdisks; dp++) {
		if (strcmp(dp->device, "*") == 0) {
			numdisks--;
			continue;
		}

		if ((dp->fd = open(dp->device, O_RDONLY)) < 0) {
			syslog(LOG_NOTICE, "open %s failed: %m", dp->device);
			numdisks--;
			continue;
		}

		dp->errors = 0;

		/*
		 * Set the rate appropriately.  We read in blocks of this
		 * size, so make it a power of 2 as close as possible to the
		 * specified/calculated rate, and make it in the range
		 * MINRATE..MAXRATE.
		 */
		if (dp->size < 0)
			getdisksize(dp);
		if (dp->rate < 0)
			dp->rate = dp->size / (dp->days * 86400);
		setdiskrate(dp);
	}

	if (numdisks == 0) {
		syslog(LOG_NOTICE, "no disks usable");
		exit(EXIT_FAILURE);
	}

	/* Add a final entry with dp->device == NULL to end the array. */
	if ((disks = reallocf(disks,
	    (onumdisks + 1) * sizeof (*disks))) == NULL) {
		syslog(LOG_NOTICE, "reallocf failure: %m");
		exit(EXIT_FAILURE);
	}
	disks[onumdisks].device = NULL;

	return (disks);
}

/*
 * Read in the specified disk's size from the disklabel.
 */
void
getdisksize(struct disk *dp) {
	struct disklabel label;

	if (ioctl(dp->fd, DIOCGDINFO, &label) < 0) {
		syslog(LOG_NOTICE, "DIOCGDINFO on %s failed: %m",
		    dp->device);
		exit(EXIT_FAILURE);
	}

	dp->secsize = label.d_secsize;
	dp->size = (off_t)label.d_secperunit * label.d_secsize;

	if (label.d_secsize != 512)
		syslog(LOG_NOTICE,
		    "%s has %d byte sectors, may cause minor problems",
		    dp->device, label.d_secsize);
}

/*
 * Find the nearest power of 2 to the calculated or specified rate, limited
 * to somewhere between MINRATE and MAXRATE.
 */
void
setdiskrate(struct disk *dp) {
	int s, r;
	int above, below;

	if (dp->rate <= MINRATE)
		dp->rate = MINRATE;
	else if (dp->rate >= MAXRATE)
		dp->rate = MAXRATE;
	else {
		for (r = dp->rate, s = 0; r != 0; s++)
			r >>= 1;

		/*
		 * "above" and "below" are the closest power-of-2 numbers to the
		 * calculated rate.
		 */
		above = 1 << s;
		below = 1 << (s - 1);
		if (above - dp->rate < dp->rate - below)
			dp->rate = above;
		else
			dp->rate = below;
	}
}

off_t
dseek(struct disk *dp, off_t offset, int whence) {
	off_t n;

	if ((n = lseek(dp->fd, offset, whence)) == -1) {
		syslog(LOG_NOTICE, "seek failure on %s: %m", dp->device);
		close(dp->fd);
		dp->fd = -1;
	}
	return (n);
}

/*
 * Attempt to get a list of all disk names using the `kern.disks' sysctl
 * variable.  An NULL terminated list of pointers to these disks' pathnames
 * is returned.
 */
char **
getdisknames(void) {
	char *string, *field;
	size_t size, numdisks;
	char **disks;

	if (sysctlbyname("kern.disks", NULL, &size, NULL, 0) != 0 &&
	    errno != ENOMEM) {
		syslog(LOG_NOTICE, "sysctl kern.disks failure: %m");
		exit(EXIT_FAILURE);
	}
	if ((string = malloc(size)) == NULL) {
		syslog(LOG_NOTICE, "malloc failure: %m");
		exit(EXIT_FAILURE);
	}
	if (sysctlbyname("kern.disks", string, &size, NULL, 0) != 0) {
		syslog(LOG_NOTICE, "sysctl kern.disks failure: %m");
		exit(EXIT_FAILURE);
	}

	disks = NULL;
	numdisks = 0;
	while ((field = strsep(&string, " ")) != NULL) {
		if ((disks = reallocf(disks,
		  (numdisks + 1) * sizeof (*disks))) == NULL) {
			syslog(LOG_NOTICE, "reallocf failure: %m");
			exit(EXIT_FAILURE);
		}
		if (asprintf(&disks[numdisks],
		  "%s%s", _PATH_DEV, field) < 0) {
			syslog(LOG_NOTICE, "asprintf failure: %m");
			exit(EXIT_FAILURE);
		}
		numdisks++;
	}

	if ((disks = reallocf(disks,
	  (numdisks + 1) * sizeof (*disks))) == NULL) {
		syslog(LOG_NOTICE, "strdup failure: %m");
		exit(EXIT_FAILURE);
	}
	disks[numdisks] = NULL;

	free(string);
	return (disks);
}

void
sighup(int sig) {

	sig = sig;
	got_sighup = 1;
}

void
sigterm(int sig) {

	sig = sig;
	got_sigterm = 1;
}

void
usage(void) {

	fprintf(stderr,
	    "usage: diskcheckd [-d] [-f conf_file] [-o save_file]\n");
	exit(EXIT_FAILURE);
}
OpenPOWER on IntegriCloud