Initial import of binutils 2.22 on the new vendor branch
[dragonfly.git] / contrib / lvm2 / dist / lib / netbsd / dev.c
1 /*      $NetBSD: dev.c,v 1.2 2009/10/16 21:00:41 joerg 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 <stdlib.h>
15 #include <unistd.h>
16 #include <ctype.h>
17 #include <fcntl.h>
18 #include <limits.h>
19
20 #include "netbsd.h"
21
22 #define LVM_FAILURE -1
23
24 /*
25  * Find major numbers for char/block parts of all block devices.
26  * In NetBSD every block device has it's char counter part.
27  * Return success only for device drivers with defined char/block
28  * major numbers.
29  */
30 int
31 nbsd_check_dev(int major, const char *path)
32 {
33
34         size_t val_len,i;
35
36         struct kinfo_drivers *kd;
37
38         /* XXX HACK */  
39         if (strcmp(path,"/dev/console") == 0)
40                 return LVM_FAILURE;
41         
42         /* get size kernel drivers array from kernel*/
43         if (sysctlbyname("kern.drivers",NULL,&val_len,NULL,0) < 0) {
44                 printf("sysctlbyname failed");
45                 return LVM_FAILURE;
46         }
47         
48         if ((kd = malloc (val_len)) == NULL){
49                 printf("malloc kd info error\n");
50                 return LVM_FAILURE;
51         }
52         
53         /* get array from kernel */
54         if (sysctlbyname("kern.drivers", kd, &val_len, NULL, 0) < 0) {
55                 printf("sysctlbyname failed kd");
56                 return LVM_FAILURE;
57         }
58
59         for (i = 0, val_len /= sizeof(*kd); i < val_len; i++)
60                 /* We select only devices with correct char/block major number. */
61                 if (kd[i].d_cmajor != -1 && kd[i].d_bmajor != -1) {
62                         
63                         if (kd[i].d_cmajor == major)
64                                 return 0;
65                 }
66         
67         return LVM_FAILURE;
68 }