Whamcloud - gitweb
b=15967
[fs/lustre-release.git] / lustre / tests / opendevunlink.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4
5 #ifndef _GNU_SOURCE
6 #define _GNU_SOURCE
7 #endif
8
9 #include <stdio.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <dirent.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <stdlib.h>
18
19 int main(int argc, char **argv)
20 {
21         char *dname1, *dname2;
22         int fddev1, fddev2, rc;
23         //DIR *dp;
24         struct stat st1, st2;
25
26         if (argc < 2 || argc > 3) {
27                 fprintf(stderr, "usage: %s filename1 [filename2]\n", argv[0]);
28                 exit(1);
29         }
30
31         dname1 = argv[1];
32         if (argc == 3)
33                 dname2 = argv[2];
34         else
35                 dname2 = argv[1];
36
37         //create the special file (right now only test on pipe)
38         fprintf(stderr, "creating special file %s\n", dname1);
39         rc = mknod(dname1, 0777|S_IFIFO, 0);
40         if (rc == -1) {
41                 fprintf(stderr, "creating %s fails: %s\n",
42                         dname1, strerror(errno));
43                 exit(1);
44         }
45
46         // open the special file again
47         fprintf(stderr, "opening file\n");
48         fddev1 = open(dname1, O_RDONLY | O_NONBLOCK);
49         if (fddev1 == -1) {
50                 fprintf(stderr, "open %s fails: %s\n",
51                         dname1, strerror(errno));
52                 exit(1);
53         }
54
55         // doesn't matter if the two dirs are the same??
56         fddev2 = open(dname2, O_RDONLY | O_NONBLOCK);
57         if (fddev2 == -1) {
58                 fprintf(stderr, "open %s fails: %s\n",
59                         dname2, strerror(errno));
60                 exit(1);
61         }
62
63         // delete the special file
64         fprintf (stderr, "unlinking %s\n", dname1);
65         rc = unlink(dname1);
66         if (rc) {
67                 fprintf(stderr, "unlink %s error: %s\n",
68                         dname1, strerror(errno));
69                 exit(1);
70         }
71
72         if (access(dname2, F_OK) == 0) {
73                 fprintf(stderr, "%s still exists\n", dname2);
74                 exit(1);
75         }
76
77         if (access(dname1, F_OK) == 0) {
78                 fprintf(stderr, "%s still exists\n", dname1);
79                 exit(1);
80         }
81
82         // fchmod one special file
83         rc = fchmod (fddev1, 0777);
84         if (rc == -1) {
85                 fprintf(stderr, "fchmod unlinked special file %s fails: %s\n",
86                         dname1, strerror(errno));
87                 exit(1);
88         }
89
90         // fstat two files to check if they are the same
91         rc = fstat(fddev1, &st1);
92         if (rc == -1) {
93                 fprintf(stderr, "fstat unlinked special file %s fails: %s\n",
94                         dname1, strerror(errno));
95                 exit(1);
96         }
97
98         rc = fstat(fddev2, &st2);
99         if (rc == -1) {
100                 fprintf(stderr, "fstat file %s fails: %s\n",
101                         dname2, strerror(errno));
102                 exit(1);
103         }
104
105 #if 0
106         /* We cannot do this any longer, we do not store open special nodes
107          * on MDS after unlink */
108         if (st1.st_mode != st2.st_mode) {  // can we do this?
109                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
110                 exit(1);
111         }
112 #endif
113
114         fprintf(stderr, "Ok, everything goes well.\n");
115         return 0;
116 }
117