Whamcloud - gitweb
9faf8e2961dd1875f5df479305cf9c6e560d624d
[fs/lustre-release.git] / lustre / tests / cmknod.c
1 /* Simple test to check that device nodes are correctly created and visible */
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <unistd.h>
9 #include <errno.h>
10 #define TEST_MINOR 120
11 #define TEST_MAJOR 25
12
13 void usage(char *prog)
14 {
15         fprintf(stderr, "usage: %s <filename>\n", prog);
16         exit(1);
17 }
18
19 int main( int argc, char **argv)
20 {
21         char *prog = argv[0];
22         char *filename = argv[1];
23         int rc;
24         struct stat st;
25         dev_t device = makedev(TEST_MAJOR, TEST_MINOR);
26
27         if (argc != 2) 
28                 usage(prog);
29
30         unlink(filename);
31         /* First try block devices */
32         rc = mknod(filename, 0700 | S_IFBLK, device);
33         if ( rc < 0 ) {
34                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
35                         prog, filename, errno, strerror(errno));
36                 return 2;
37         }
38
39         rc = stat(filename, &st);
40         if ( rc < 0 ) {
41                 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
42                         prog, filename, errno, strerror(errno));
43                 return 3;
44         }
45         if ( st.st_rdev != device) {
46                 fprintf(stderr, "%s: created device other than requested: (%d,%d) instead of (%d,%d)\n", prog, major(st.st_rdev),minor(st.st_rdev),major(device),minor(device));
47                 return 4;
48         }
49         if (!S_ISCHR(st.st_mode)) {
50                 fprintf(stderr, "%s: created device of different type. Requested block device, got mode %o\n", prog, st.st_mode);
51                 return 5;
52         }
53
54         rc = unlink(filename);
55         if ( rc < 0 ) {
56                 fprintf(stderr, "%s: Cannot unlink created device %s, rc %d: %s\n",
57                         prog, filename, errno, strerror(errno));
58                 return 6;
59         }
60
61         /* Second try char devices */
62         rc = mknod(filename, 0700 | S_IFCHR, device);
63         if ( rc < 0 ) {
64                 fprintf(stderr, "%s: mknod(%s) failed: rc %d: %s\n",
65                         prog, filename, errno, strerror(errno));
66                 return 7;
67         }
68
69         rc = stat(filename, &st);
70         if ( rc < 0 ) {
71                 fprintf(stderr, "%s: stat(%s) failed: rc %d: %s\n",
72                         prog, filename, errno, strerror(errno));
73                 return 8;
74         }
75         if ( st.st_rdev != device) {
76                 fprintf(stderr, "%s: created device other than requested: (%d,%d) instead of (%d,%d)\n", prog, major(st.st_rdev),minor(st.st_rdev),major(device),minor(device));
77                 return 9;
78         }
79         if (!S_ISCHR(st.st_mode)) {
80                 fprintf(stderr, "%s: created device of different type. Requested char device, got mode %o\n", prog, st.st_mode);
81                 return 10;
82         }
83
84         rc = unlink(filename);
85         if ( rc < 0 ) {
86                 fprintf(stderr, "%s: Cannot unlink created device %s, rc %d: %s\n",
87                         prog, filename, errno, strerror(errno));
88                 return 11;
89         }
90
91         printf("%s: device nodes created correctly\n", prog);
92
93         return 0;
94 }