xref: /freebsd/tools/test/stress2/misc/procfs4.sh (revision 9768746b)
1#!/bin/sh
2
3#
4# Copyright (c) 2012 Peter Holm <pho@FreeBSD.org>
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# Test scenario idea by kib@
30
31# "panic: double fault" seen due to recursion
32
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35. ../default.cfg
36
37mount | grep -q procfs || mount -t procfs procfs /proc
38here=`pwd`
39cd /tmp
40sed '1,/^EOF/d' < $here/$0 > procfs4.c
41mycc -o procfs4 -Wall -Wextra -O2 procfs4.c || exit 1
42rm -f procfs4.c
43cd $here
44
45su $testuser -c /tmp/procfs4
46e=$?
47
48rm -f /tmp/procfs4
49exit $e
50EOF
51#include <sys/param.h>
52#include <sys/ioctl.h>
53#include <sys/param.h>
54#include <sys/types.h>
55#include <sys/wait.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <signal.h>
61#include <stdio.h>
62#include <stdlib.h>
63#include <string.h>
64#include <time.h>
65#include <unistd.h>
66
67#define LOOPS 1000
68#define MAXRUN 1200
69#define PARALLEL 10
70
71char *files[] = {
72	"cmdline",
73	"ctl",
74	"dbregs",
75	"etype",
76	"file",
77	"fpregs",
78	"map",
79	"mem",
80	"note",
81	"notepg",
82	"osrel",
83	"regs",
84	"rlimit",
85	"status"
86};
87
88void
89test(void)
90{
91	pid_t p;
92	int fd, i, j, n, opens;
93	char path[128];
94
95	for (i = 0; i < 64; i++) {
96		if ((p = fork()) == 0) {
97			setproctitle("Sleeper");
98			usleep(20000);
99			usleep(arc4random() % 200);
100			for (j = 0; j < 10000; j++)
101				getpid();
102			_exit(0);
103		}
104		opens = 0;
105		setproctitle("load");
106		for (j = 0; j < 14; j++) {
107			snprintf(path, sizeof(path), "/proc/%d/%s", p, files[j]);
108			if ((fd = open(path, O_RDWR)) == -1)
109				if ((fd = open(path, O_RDONLY)) == -1)
110					continue;
111
112			ioctl(fd, FIONREAD, &n);
113			if (ioctl(fd, FIONBIO, &n) != -1)
114				opens++;
115
116			close(fd);
117		}
118		kill(p, SIGHUP);
119#if 0
120		if (opens < 1)
121			fprintf(stderr, "Warn %d open(s) for pid %d\n", opens, getpid());
122#endif
123	}
124
125	for (i = 0; i < 64; i++)
126		wait(NULL);
127
128	_exit(0);
129}
130
131int
132main(void)
133{
134	time_t start;
135	int e, i, j;
136
137	e = 0;
138	start = time(NULL);
139	for (i = 0; i < LOOPS; i++) {
140		for (j = 0; j < PARALLEL; j++) {
141			if (fork() == 0)
142				test();
143		}
144
145		for (j = 0; j < PARALLEL; j++)
146			wait(NULL);
147		usleep(10000);
148		if (time(NULL) - start > MAXRUN) {
149			fprintf(stderr, "FAIL Timeout\n");
150			e = 1;
151			break;
152		}
153	}
154
155	return (e);
156}
157