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