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