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