xref: /netbsd-src/external/gpl2/lvm2/dist/lib/netbsd/dev.c (revision 2c52b012a9a3b1f2db27c68f05e98cb7ca80ad31)
1 /*      $NetBSD: dev.c,v 1.3 2011/05/15 01:16:29 christos Exp $        */
2 
3 /*
4  * NetBSD specific device routines are added to this file.
5  */
6 
7 #include <sys/param.h>
8 #include <sys/types.h>
9 
10 #include <sys/sysctl.h>
11 
12 #include <dirent.h>
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <ctype.h>
18 #include <fcntl.h>
19 #include <limits.h>
20 
21 #include "netbsd.h"
22 
23 #define LVM_FAILURE -1
24 
25 /*
26  * Find major numbers for char/block parts of all block devices.
27  * In NetBSD every block device has it's char counter part.
28  * Return success only for device drivers with defined char/block
29  * major numbers.
30  */
31 int
nbsd_check_dev(int major,const char * path)32 nbsd_check_dev(int major, const char *path)
33 {
34 
35 	size_t val_len,i;
36 
37 	struct kinfo_drivers *kd;
38 
39 	/* XXX HACK */
40 	if (strcmp(path,"/dev/console") == 0)
41 		return LVM_FAILURE;
42 
43 	/* get size kernel drivers array from kernel*/
44 	if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
45 		printf("sysctlbyname failed");
46 		return LVM_FAILURE;
47 	}
48 
49 	if ((kd = malloc (val_len)) == NULL){
50 		printf("malloc kd info error\n");
51 		return LVM_FAILURE;
52 	}
53 
54 	/* get array from kernel */
55 	if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
56 		printf("sysctlbyname failed kd");
57 		return LVM_FAILURE;
58 	}
59 
60 	for (i = 0, val_len /= sizeof(*kd); i < val_len; i++)
61 		/* We select only devices with correct char/block major number. */
62 		if (kd[i].d_cmajor != -1 && kd[i].d_bmajor != -1) {
63 
64 			if (kd[i].d_cmajor == major)
65 				return 0;
66 		}
67 
68 	return LVM_FAILURE;
69 }
70