Whamcloud - gitweb
LU-15210 tests: fix sanity-lnet to handle duplicate IP
[fs/lustre-release.git] / lustre / tests / openunlink.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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  */
29
30 #include <stdio.h>
31 #include <fcntl.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
36 #include <stdlib.h>
37 #include <unistd.h>
38
39 #define T1 "write data before unlink\n"
40 #define T2 "write data after unlink\n"
41 char buf[128];
42
43 int main(int argc, char **argv)
44 {
45         char *fname, *fname2;
46         struct stat st;
47         int fd, rc;
48
49         if (argc < 2 || argc > 3) {
50                 fprintf(stderr, "usage: %s filename [filename2]\n", argv[0]);
51                 exit(1);
52         }
53
54         fname = argv[1];
55         if (argc == 3)
56                 fname2 = argv[2];
57         else
58                 fname2 = argv[1];
59
60         fprintf(stderr, "opening\n");
61         fd = open(fname, O_RDWR | O_TRUNC | O_CREAT, 0644);
62         if (fd == -1) {
63                 fprintf(stderr, "open (normal) %s\n", strerror(errno));
64                 exit(1);
65         }
66
67         fprintf(stderr, "writing\n");
68         rc = write(fd, T1, strlen(T1) + 1);
69         if (rc != strlen(T1) + 1) {
70                 fprintf(stderr, "write (normal) %s (rc %d)\n",
71                         strerror(errno), rc);
72                 exit(1);
73         }
74
75         if (argc == 3) {
76                 fprintf(stderr, "unlinking %s\n", fname2);
77                 rc = unlink(fname2);
78                 if (rc) {
79                         fprintf(stderr, "unlink %s\n", strerror(errno));
80                         exit(1);
81                 }
82         } else {
83                 printf("unlink %s and press enter\n", fname);
84                 getc(stdin);
85         }
86
87         fprintf(stderr, "accessing (1)\n");
88         if (access(fname, F_OK) == 0) {
89                 fprintf(stderr, "%s still exists\n", fname);
90                 exit(1);
91         }
92
93         fprintf(stderr, "seeking (1)\n");
94         rc = lseek(fd, 0, SEEK_SET);
95         if (rc) {
96                 fprintf(stderr, "seek %s\n", strerror(errno));
97                 exit(1);
98         }
99
100         fprintf(stderr, "accessing (2)\n");
101         if (access(fname, F_OK) == 0) {
102                 fprintf(stderr, "%s still exists\n", fname);
103                 exit(1);
104         }
105
106         fprintf(stderr, "fstat...\n");
107         rc = fstat(fd, &st);
108         if (rc) {
109                 fprintf(stderr, "fstat (unlink) %s\n", strerror(errno));
110                 exit(1);
111         }
112         if (st.st_nlink != 0)
113                 fprintf(stderr, "st_nlink = %d\n", (int)st.st_nlink);
114
115         fprintf(stderr, "reading\n");
116         rc = read(fd, buf, strlen(T1) + 1);
117         if (rc != strlen(T1) + 1) {
118                 fprintf(stderr, "read (unlink) %s (rc %d)\n",
119                         strerror(errno), rc);
120                 exit(1);
121         }
122
123         fprintf(stderr, "comparing data\n");
124         if (memcmp(buf, T1, strlen(T1) + 1)) {
125                 fprintf(stderr, "FAILURE: read wrong data after unlink\n");
126                 exit(1);
127         }
128
129         fprintf(stderr, "truncating\n");
130         rc = ftruncate(fd, 0);
131         if (rc) {
132                 fprintf(stderr, "truncate (unlink) %s\n", strerror(errno));
133                 exit(1);
134         }
135
136         fprintf(stderr, "seeking (2)\n");
137         rc = lseek(fd, 0, SEEK_SET);
138         if (rc) {
139                 fprintf(stderr, "seek (after unlink trunc) %s\n",
140                         strerror(errno));
141                 exit(1);
142         }
143
144         fprintf(stderr, "writing again\n");
145         rc = write(fd, T2, strlen(T2) + 1);
146         if (rc != strlen(T2) + 1) {
147                 fprintf(stderr, "write (after unlink trunc) %s (rc %d)\n",
148                         strerror(errno), rc);
149                 exit(1);
150         }
151
152         fprintf(stderr, "seeking (3)\n");
153         rc = lseek(fd, 0, SEEK_SET);
154         if (rc) {
155                 fprintf(stderr, "seek (before unlink read) %s\n",
156                         strerror(errno));
157                 exit(1);
158         }
159
160         fprintf(stderr, "reading again\n");
161         rc = read(fd, buf, strlen(T2) + 1);
162         if (rc != strlen(T2) + 1) {
163                 fprintf(stderr, "read (after unlink rewrite) %s (rc %d)\n",
164                         strerror(errno), rc);
165                 exit(1);
166         }
167
168         fprintf(stderr, "comparing data again\n");
169         if (memcmp(buf, T2, strlen(T2) + 1)) {
170                 fprintf(stderr, "FAILURE: read wrong data after rewrite\n");
171                 exit(1);
172         }
173
174         fprintf(stderr, "closing\n");
175         rc = close(fd);
176         if (rc) {
177                 fprintf(stderr, "close (unlink) %s\n", strerror(errno));
178                 exit(1);
179         }
180
181         fprintf(stderr, "SUCCESS - goto beer\n");
182         return 0;
183 }