xref: /freebsd/tools/test/stress2/testcases/openat/openat.c (revision d4eeb02986980bf33dd56c41ceb9fc5f180c0d47)
1 /*-
2  * Copyright (c) 2008 Peter Holm <pho@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  */
27 
28 #include <sys/param.h>
29 #include <sys/stat.h>
30 #include <err.h>
31 #include <fcntl.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include "stress.h"
38 
39 static char path1[128];
40 static char path2[] = "tmp";
41 
42 static char rpath[128];
43 static char apath[128];
44 
45 static int fd;
46 static int freespace;
47 
48 int
49 setup(int nb)
50 {
51 	int64_t bl;
52 	int64_t in;
53 	int64_t reserve_bl;
54 	int64_t reserve_in;
55 
56 	umask(0);
57 
58 	if (nb == 0) {
59 		getdf(&bl, &in);
60 
61 		/* Resource requirements: */
62 		reserve_in =    4 * op->incarnations;
63 		reserve_bl = 8192 * op->incarnations;
64 		freespace = (reserve_bl <= bl && reserve_in <= in);
65 		if (!freespace)
66 			reserve_bl = reserve_in = 0;
67 
68 		if (op->verbose > 1)
69 			printf("openat(incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
70 			    op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
71 		reservedf(reserve_bl, reserve_in);
72 		putval(freespace);
73 	} else {
74 		freespace = getval();
75 	}
76 	if (!freespace)
77 		exit(0);
78 
79 	sprintf(path1,"%s.%05d", getprogname(), getpid());
80 	if (mkdir(path1, 0770) < 0)
81 		err(1, "mkdir(%s), %s:%d", path1, __FILE__, __LINE__);
82 	if (chdir(path1) == -1)
83 		err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);
84 
85 	if (mkdir(path2, 0770) < 0)
86 		err(1, "mkdir(%s), %s:%d", path2, __FILE__, __LINE__);
87 	if (chdir(path2) == -1)
88 		err(1, "chdir(%s), %s:%d", path2, __FILE__, __LINE__);
89 	if (getcwd(apath, sizeof(apath)) == NULL)
90 		err(1, "getcwd(%s), %s:%d", path2, __FILE__, __LINE__);
91 
92 	if (chdir("..") == -1)
93 		err(1, "chdir(%s), %s:%d", path1, __FILE__, __LINE__);
94 
95 	if ((fd = open(path2, O_RDONLY)) == -1)
96 		err(1, "open(%s), %s:%d", path2, __FILE__, __LINE__);
97 
98 	strcpy(rpath, "tmp");
99 	return (0);
100 }
101 
102 void
103 cleanup(void)
104 {
105 	if (rmdir(path2) == -1)
106 		warn("rmdir(%s), %s:%d", path2, __FILE__, __LINE__);
107 	(void)chdir("..");
108 	if (rmdir(path1) == -1)
109 		warn("rmdir(%s), %s:%d", path1, __FILE__, __LINE__);
110 }
111 
112 static void
113 test_openat(void)
114 {
115 	pid_t pid;
116 	int i;
117 	int tfd;
118 	char file[128];
119 	char p[128];
120 
121 	pid = getpid();
122 	for (i = 0; i < 100 && done_testing == 0; i++) {
123 		sprintf(file,"p%05d.%05d", pid, i);
124 		if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
125 			err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
126 		close(tfd);
127 		strcpy(p, "tmp/");
128 		strcat(p, file);
129 		if (unlink(p) == -1)
130 			err(1, "unlink(%s), %s:%d", p, __FILE__, __LINE__);
131 	}
132 }
133 
134 static void
135 test_renameat(void)
136 {
137 	pid_t pid;
138 	int i;
139 	int tfd;
140 	char file[128];
141 	char file2[128];
142 
143 	pid = getpid();
144 	for (i = 0; i < 100 && done_testing == 0; i++) {
145 		sprintf(file,"p%05d.%05d", pid, i);
146 		if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
147 			err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
148 		close(tfd);
149 
150 		sprintf(file2,"p%05d.%05d.togo", pid, i);
151 		if (renameat(fd, file, fd, file2) == -1)
152 			err(1, "renameat(%s)", file2);
153 
154 		sprintf(file2,"tmp/p%05d.%05d.togo", pid, i);
155 		if (unlink(file2) == -1)
156 			err(1, "unlink(%s), %s:%d", file2, __FILE__, __LINE__);
157 	}
158 }
159 
160 static void
161 test_unlinkat(void)
162 {
163 	pid_t pid;
164 	int i;
165 	int tfd;
166 	char file[128];
167 
168 	pid = getpid();
169 	for (i = 0; i < 100 && done_testing == 0; i++) {
170 		sprintf(file,"p%05d.%05d", pid, i);
171 		if ((tfd = openat(fd, file, O_RDONLY|O_CREAT, 0660)) == -1)
172 			err(1, "openat(%s), %s:%d", file, __FILE__, __LINE__);
173 		close(tfd);
174 		if (unlinkat(fd, file, 0) == -1)
175 			err(1, "unlinkat(%s), %s:%d", file, __FILE__, __LINE__);
176 	}
177 }
178 
179 int
180 test(void)
181 {
182 	test_openat();
183 	test_renameat();
184 	test_unlinkat();
185 
186 	return (0);
187 }
188