Whamcloud - gitweb
LU-8141 tests: fix for acl test
[fs/lustre-release.git] / lustre / tests / opendevunlink.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef _GNU_SOURCE
36 #define _GNU_SOURCE
37 #endif
38
39 #include <stdio.h>
40 #include <errno.h>
41 #include <fcntl.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <dirent.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <stdlib.h>
48
49 int main(int argc, char **argv)
50 {
51         char *dname1, *dname2;
52         int fddev1, fddev2, rc;
53         //DIR *dp;
54         struct stat st1, st2;
55
56         if (argc < 2 || argc > 3) {
57                 fprintf(stderr, "usage: %s filename1 [filename2]\n", argv[0]);
58                 exit(1);
59         }
60
61         dname1 = argv[1];
62         if (argc == 3)
63                 dname2 = argv[2];
64         else
65                 dname2 = argv[1];
66
67         //create the special file (right now only test on pipe)
68         fprintf(stderr, "creating special file %s\n", dname1);
69         rc = mknod(dname1, 0777|S_IFIFO, 0);
70         if (rc == -1) {
71                 fprintf(stderr, "creating %s fails: %s\n",
72                         dname1, strerror(errno));
73                 exit(1);
74         }
75
76         // open the special file again
77         fprintf(stderr, "opening file\n");
78         fddev1 = open(dname1, O_RDONLY | O_NONBLOCK);
79         if (fddev1 == -1) {
80                 fprintf(stderr, "open %s fails: %s\n",
81                         dname1, strerror(errno));
82                 exit(1);
83         }
84
85         // doesn't matter if the two dirs are the same??
86         fddev2 = open(dname2, O_RDONLY | O_NONBLOCK);
87         if (fddev2 == -1) {
88                 fprintf(stderr, "open %s fails: %s\n",
89                         dname2, strerror(errno));
90                 exit(1);
91         }
92
93         // delete the special file
94         fprintf (stderr, "unlinking %s\n", dname1);
95         rc = unlink(dname1);
96         if (rc) {
97                 fprintf(stderr, "unlink %s error: %s\n",
98                         dname1, strerror(errno));
99                 exit(1);
100         }
101
102         if (access(dname2, F_OK) == 0) {
103                 fprintf(stderr, "%s still exists\n", dname2);
104                 exit(1);
105         }
106
107         if (access(dname1, F_OK) == 0) {
108                 fprintf(stderr, "%s still exists\n", dname1);
109                 exit(1);
110         }
111
112         // fchmod one special file
113         rc = fchmod (fddev1, 0777);
114         if (rc == -1) {
115                 fprintf(stderr, "fchmod unlinked special file %s fails: %s\n",
116                         dname1, strerror(errno));
117                 exit(1);
118         }
119
120         // fstat two files to check if they are the same
121         rc = fstat(fddev1, &st1);
122         if (rc == -1) {
123                 fprintf(stderr, "fstat unlinked special file %s fails: %s\n",
124                         dname1, strerror(errno));
125                 exit(1);
126         }
127
128         rc = fstat(fddev2, &st2);
129         if (rc == -1) {
130                 fprintf(stderr, "fstat file %s fails: %s\n",
131                         dname2, strerror(errno));
132                 exit(1);
133         }
134
135 #if 0
136         /* We cannot do this any longer, we do not store open special nodes
137          * on MDS after unlink */
138         if (st1.st_mode != st2.st_mode) {  // can we do this?
139                 fprintf(stderr, "fstat different value on %s and %s\n",                                 dname1, dname2);
140                 exit(1);
141         }
142 #endif
143
144         fprintf(stderr, "Ok, everything goes well.\n");
145         return 0;
146 }