xref: /freebsd/tools/test/stress2/misc/dup.sh (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2019 Dell EMC Isilon
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Test scenario for "D20947: Check and avoid overflow when incrementing
31# fp->f_count in fget_unlocked() and fhold()".
32
33# OOM killing seen. Cap files and procs for now.
34
35. ../default.cfg
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40rm -f /tmp/dup /tmp/dup.c || exit 1
41sed '1,/^EOF/d' < $odir/$0 > $dir/dup.c
42mycc -o dup  -Wall -Wextra dup.c || exit 1
43rm -f dup.c
44cd $odir
45
46/tmp/dup; s=$?
47
48rm -f /tmp/dup
49exit $s
50
51EOF
52#include <sys/param.h>
53#include <sys/mman.h>
54#include <sys/stat.h>
55#include <sys/wait.h>
56
57#include <fcntl.h>
58#include <err.h>
59#include <errno.h>
60#include <stdlib.h>
61#include <stdio.h>
62#include <unistd.h>
63
64static volatile u_int *share;
65#define N 10240
66#define CAP_FILES 50000
67#define CAP_PROCS 1000
68#define SYNC 0
69
70int
71main(void)
72{
73	struct stat st;
74	pid_t pid[N];
75	size_t len;
76	int fd, fd2, i, j, last;
77
78	len = PAGE_SIZE;
79	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
80	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
81		err(1, "mmap");
82	if ((fd = open("/dev/zero", O_RDONLY)) == -1)
83		err(1, "open(/dev/zero)");
84	i = 0;
85	for (;;) {
86		if ((fd2 = dup(fd)) == -1) {
87			if (errno == EMFILE)
88				break;
89			err(1, "dup()");
90		}
91		last = fd2;
92		if (++i == CAP_FILES)
93			break;
94	}
95#if defined(DEBUG)
96	fprintf(stderr, "i = %d\n", i);
97#endif
98
99	for (i = 0; i < N; i++) {
100		if ((pid[i] = fork()) == 0) {
101			if (fstat(last, &st) == -1)
102				err(1, "stat(%s)", "/dev/zero");
103			while(share[SYNC] == 0)
104				usleep(100000);
105			_exit(0);
106		}
107		if (pid[i] == -1) {
108			warn("fork()");
109			i--;
110			break;
111		}
112		if (i + 1 == CAP_PROCS)
113			break;
114	}
115	share[SYNC] = 1;
116	for (j = 0; j < i; j++) {
117		if (waitpid(pid[j], NULL, 0) != pid[j])
118			err(1, "waitpid(%d), index %d", pid[j], j);
119	}
120
121	return (0);
122}
123