xref: /freebsd/tools/test/stress2/misc/fdatasync2.sh (revision c1d255d3)
1#!/bin/sh
2
3#
4# Copyright (c) 2018 Dell EMC Isilon
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# fdatasync(2) fuzz. Variation of fdatasync.sh.
30# https://people.freebsd.org/~pho/stress/log/fdatasync2.txt
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34. ../default.cfg
35
36dir=$RUNDIR
37nfiles=10000
38[ `df -i $dir | tail -1 | awk '{print $7}'` -lt $nfiles ] && exit 0
39
40odir=`pwd`
41cd /tmp
42sed '1,/^EOF/d' < $odir/$0 > fdatasync2.c
43rm -f /tmp/fdatasync2
44mycc -o fdatasync2 -Wall -Wextra -O2 -g fdatasync2.c -lpthread || exit 1
45rm -f fdatasync2.c
46
47mkdir -p $dir && chmod 777 $dir
48
49cd $dir
50jot $nfiles | xargs touch
51jot $nfiles | xargs chmod 666
52cd $odir
53
54(cd /tmp; /tmp/fdatasync2 $dir)
55e=$?
56
57rm -rf $dir/* /tmp/fdatasync2
58exit $e
59EOF
60#include <sys/param.h>
61#include <sys/resource.h>
62
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <fts.h>
67#include <libutil.h>
68#include <pthread.h>
69#include <pwd.h>
70#include <stdint.h>
71#include <stdio.h>
72#include <stdlib.h>
73#include <string.h>
74#include <unistd.h>
75
76#define N (128 * 1024 / (int)sizeof(u_int32_t))
77#define RUNTIME 180
78#define THREADS 6
79
80static int fd[900];
81static u_int32_t r[N];
82static char *args[2];
83
84static unsigned long
85makearg(void)
86{
87	unsigned long val;
88
89	val = arc4random();
90#if defined(__LP64__)
91	val = (val << 32) | arc4random();
92	val = val & 0x00007fffffffffffUL;
93#endif
94
95	return(val);
96}
97
98static void *
99test(void *arg __unused)
100{
101	FTS *fts;
102	FTSENT *p;
103	int ftsoptions, i, n;
104
105	ftsoptions = FTS_PHYSICAL;
106
107	for (;;) {
108		for (i = 0; i < N; i++)
109			r[i] = arc4random();
110		if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
111			err(1, "fts_open");
112
113		i = n = 0;
114		while ((p = fts_read(fts)) != NULL) {
115			if (fd[i] > 0)
116				close(fd[i]);
117			if ((fd[i] = open(p->fts_path, O_RDWR)) == -1)
118				if ((fd[i] = open(p->fts_path, O_WRONLY)) ==
119				    -1)
120				continue;
121			if (ftruncate(fd[i], 0) != 0)
122				err(1, "ftruncate");
123			i++;
124			i = i % nitems(fd);
125		}
126
127		if (fts_close(fts) == -1)
128			err(1, "fts_close()");
129		sleep(1);
130	}
131	return(0);
132}
133
134static void *
135calls(void *arg __unused)
136{
137	off_t offset;
138	time_t start;
139	int fd2;
140
141	start = time(NULL);
142	while ((time(NULL) - start) < RUNTIME) {
143		fd2 = makearg() % nitems(fd) + 3;
144		offset = makearg();
145		if (lseek(fd2, offset - 1, SEEK_SET) != -1) {
146			if (write(fd2, "x", 1) != 1)
147				if (errno != EBADF && errno != ENOSPC &&
148				    errno != E2BIG && errno != ESTALE &&
149				    errno != EFBIG)
150					warn("write");
151		} else
152			if (errno != EBADF)
153				warn("lseek");
154		if (fdatasync(fd2) == -1)
155			if (errno != EBADF)
156				warn("x");
157
158	}
159
160	return (0);
161}
162
163int
164main(int argc, char **argv)
165{
166	struct passwd *pw;
167	pthread_t rp, cp[THREADS];
168	int e, i, threads;
169
170	if (argc != 2) {
171		fprintf(stderr, "Usage: %s <dir>\n", argv[0]);
172		exit(1);
173	}
174	args[0] = argv[1];
175	args[1] = 0;
176	threads = arc4random() % (THREADS -1 ) + 2; /* 2 - THREADS */
177
178	if ((pw = getpwnam("nobody")) == NULL)
179		err(1, "failed to resolve nobody");
180	if (setgroups(1, &pw->pw_gid) ||
181	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
182	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
183		err(1, "Can't drop privileges to \"nobody\"");
184	endpwent();
185
186	if ((e = pthread_create(&rp, NULL, test, NULL)) != 0)
187		errc(1, e, "pthread_create");
188	usleep(1000);
189	for (i = 0; i < threads; i++)
190		if ((e = pthread_create(&cp[i], NULL, calls, NULL)) != 0)
191			errc(1, e, "pthread_create");
192	for (i = 0; i < threads; i++)
193		pthread_join(cp[i], NULL);
194
195	return (0);
196}
197