xref: /freebsd/tools/test/stress2/misc/procfs3.sh (revision 3c4ba5f55438f7afd4f4b0b56f88f2bb505fd6a6)
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# procfs(5) test scenario.
30# "panic: wchan 0xc10a4f68 has no wmesg" seen
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34. ../default.cfg
35
36mount | grep -q "/proc " || { mount -t procfs procfs /proc || exit 1; }
37here=`pwd`
38cd /tmp
39sed '1,/^EOF/d' < $here/$0 > procfs3.c
40mycc -o procfs3 -Wall -Wextra -O2 procfs3.c || exit 1
41rm -f procfs3.c
42cd $here
43
44su $testuser -c /tmp/procfs3
45
46rm -f /tmp/procfs3
47exit 0
48EOF
49#include <sys/param.h>
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <fts.h>
54#include <libutil.h>
55#include <signal.h>
56#include <stdint.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <string.h>
60#include <sys/param.h>
61#include <sys/stat.h>
62#include <sys/types.h>
63#include <sys/wait.h>
64#include <unistd.h>
65
66#define PARALLEL 10
67
68void
69handler(int i __unused)
70{
71}
72
73int
74test(void)
75{
76
77	FTS		*fts;
78	FTSENT		*p;
79	int		ftsoptions;
80	char		*args[2];
81	int fd, i;
82	char buf[1629];
83
84	ftsoptions = FTS_PHYSICAL;
85	args[0] = "/proc";
86	args[1] = 0;
87
88	if ((fts = fts_open(args, ftsoptions, NULL)) == NULL)
89		err(1, "fts_open");
90
91	while ((p = fts_read(fts)) != NULL) {
92		switch (p->fts_info) {
93			case FTS_F:			/* Ignore. */
94				break;
95			case FTS_D:			/* Ignore. */
96				continue;
97			case FTS_DP:
98				continue;
99			case FTS_DC:			/* Ignore. */
100				continue;
101			case FTS_SL:			/* Ignore. */
102				continue;
103			case FTS_DNR:
104				continue;
105			case FTS_NS:
106				continue;
107			case FTS_ERR:
108			case FTS_DEFAULT:
109				warnx("%s: %s. fts_info = %d", p->fts_path, strerror(p->fts_errno),
110				    p->fts_info);
111				continue;
112			default:
113				printf("%s: default, %d\n", getprogname(), p->fts_info);
114				break;
115		}
116
117		if ((fd = open(p->fts_path, O_RDONLY)) == -1)
118			continue;
119		signal(SIGALRM, handler);
120		alarm(1);
121
122		for (i = 0; i < 2; i++) {
123			read(fd, buf, 1629);
124		}
125		close(fd);
126	}
127
128	if (errno != 0 && errno != ENOENT)
129		err(1, "fts_read");
130	if (fts_close(fts) == -1)
131		err(1, "fts_close()");
132
133	return (0);
134}
135
136int
137main(void)
138{
139	int i, j;
140
141	for (i = 0; i < PARALLEL; i++) {
142		if (fork() == 0) {
143			for (j = 0; j < 50; j++) {
144				test();
145			}
146			_exit(0);
147		}
148	}
149
150	for (i = 0; i < PARALLEL; i++)
151		wait(NULL);
152
153	return (0);
154}
155