xref: /freebsd/tools/test/stress2/testcases/symlink/symlink.c (revision 5ca8e32633c4ffbbcd6762e5888b6a4ba0708c6c)
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/mount.h>
30 #include <sys/stat.h>
31 #include <err.h>
32 #include <errno.h>
33 #include <fcntl.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <unistd.h>
37 
38 #include "stress.h"
39 
40 static char path[128];
41 static unsigned long size;
42 
43 int
44 setup(int nb)
45 {
46 	int64_t in;
47 	int64_t bl;
48 	int64_t reserve_in;
49 	int64_t reserve_bl;
50 	int pct;
51 
52 	umask(0);
53 	if (nb == 0) {
54 		getdf(&bl, &in);
55 		size = in / op->incarnations;
56 
57 		pct = 90;
58 		if (op->hog == 0)
59 			pct = random_int(1, 90);
60 		size = size / 100 * pct + 1;
61 
62 		if (size > 16000)
63 			size = 16000;	/* arbitrary limit number of files pr. dir */
64 
65 		/* Resource requirements: */
66 		while (size > 0) {
67 			reserve_in =  1 * size * op->incarnations + op->incarnations;
68 			reserve_bl = 26 * size * op->incarnations;
69 			if (reserve_bl <= bl && reserve_in <= in)
70 				break;
71 			size = size / 2;
72 		}
73 		if (size == 0)
74 			reserve_bl = reserve_in = 0;
75 
76 		if (op->verbose > 1)
77 			printf("symlink(size=%lu, incarnations=%d). Free(%jdk, %jd), reserve(%jdk, %jd)\n",
78 				size, op->incarnations, bl/1024, in, reserve_bl/1024, reserve_in);
79 		reservedf(reserve_bl, reserve_in);
80 		putval(size);
81 	} else {
82 		size = getval();
83 	}
84 	if (size == 0)
85 		exit(0);
86 
87 	sprintf(path,"%s.%05d", getprogname(), getpid());
88 	if (mkdir(path, 0770) < 0)
89 		err(1, "mkdir(%s), %s:%d", path, __FILE__, __LINE__);
90 
91 	if (chdir(path) == -1)
92 		err(1, "chdir(%s), %s:%d", path, __FILE__, __LINE__);
93 
94 	return (0);
95 }
96 
97 void
98 cleanup(void)
99 {
100 	if (path[0] != 0) {
101 		(void)chdir("..");
102 		if (rmdir(path) == -1) {
103 			warn("rmdir(%s), %s:%d", path, __FILE__, __LINE__);
104 		}
105 	}
106 }
107 
108 int
109 test(void)
110 {
111 	pid_t pid;
112 	int i, j, error = 0;
113 	char file[128];
114 
115 	pid = getpid();
116 	for (j = 0; j < (int)size && done_testing == 0; j++) {
117 		sprintf(file,"p%05d.%05d", pid, j);
118 		if (symlink("/tmp/not/there", file) == -1) {
119 			if (errno != EINTR) {
120 				warn("symlink(%s). %s.%d", file, __FILE__, __LINE__);
121 				error = 1;
122 				exit(1);
123 				break;
124 			}
125 		}
126 	}
127 
128 	for (i = --j; i >= 0; i--) {
129 		sprintf(file,"p%05d.%05d", pid, i);
130 		if (unlink(file) == -1)
131 			err(3, "unlink(%s)", file);
132 	}
133 
134 	if (error != 0)
135 		exit(1);
136 
137 	return (0);
138 }
139