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