xref: /freebsd/tools/test/stress2/misc/kinfo.sh (revision 19fae0f66023a97a9b464b3beeeabb2081f575b3)
1#!/bin/sh
2
3#
4# Copyright (c) 2008 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 by marcus@freebsd.org
30
31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
32
33. ../default.cfg
34
35odir=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $odir/$0 > kinfo.c
38mycc -o kinfo -Wall kinfo.c -lutil || exit 1
39rm -f kinfo.c
40
41mount | grep -q procfs || mount -t procfs procfs /proc
42for i in `jot 20`; do
43	for j in `jot 5`; do
44		/tmp/kinfo &
45	done
46
47	for j in `jot 5`; do
48		wait
49	done
50done
51
52rm -f /tmp/kinfo
53exit
54EOF
55
56#include <sys/types.h>
57#include <stdio.h>
58#include <stdlib.h>
59#include <unistd.h>
60#include <sys/sysctl.h>
61#include <sys/param.h>
62#include <sys/user.h>
63#include <sys/signal.h>
64#include <fcntl.h>
65#include <err.h>
66#include <strings.h>
67#include <sys/wait.h>
68#include <libutil.h>
69
70char buf[8096];
71
72void
73handler(int i) {
74	exit(0);
75}
76
77/* Stir /dev/proc */
78int
79churning(void) {
80	pid_t r;
81	int fd, status;
82
83	for (;;) {
84		r = fork();
85		if (r == 0) {
86			if ((fd = open("/proc/curproc/mem", O_RDONLY)) == -1)
87				err(1, "open(/proc/curproc/mem)");
88			bzero(buf, sizeof(buf));
89			exit(0);
90		}
91		if (r < 0) {
92			perror("fork");
93			exit(2);
94		}
95		wait(&status);
96	}
97}
98
99/* Get files for each proc */
100void
101list(void)
102{
103        struct kinfo_file *freep;
104	struct kinfo_vmentry *freep_vm;
105	long i;
106	int cnt, name[4];
107	struct kinfo_proc *kipp;
108	size_t len;
109
110	name[0] = CTL_KERN;
111	name[1] = KERN_PROC;
112	name[2] = KERN_PROC_PROC;
113
114	len = 0;
115	if (sysctl(name, 3, NULL, &len, NULL, 0) < 0)
116		err(-1, "sysctl: kern.proc.all");
117
118	kipp = malloc(len);
119	if (kipp == NULL)
120		err(1, "malloc");
121
122	if (sysctl(name, 3, kipp, &len, NULL, 0) < 0) {
123		free(kipp);
124//		warn("sysctl: kern.proc.all");
125		return;
126	}
127
128	for (i = 0; i < len / sizeof(*kipp); i++) {
129
130		/* The test starts here */
131		freep = kinfo_getfile(kipp[i].ki_pid, &cnt);
132		free(freep);
133
134		freep_vm = kinfo_getvmmap(kipp[i].ki_pid, &cnt);
135		free(freep_vm);
136		/* End test */
137	}
138	free(kipp);
139}
140
141int
142main(int argc, char **argv)
143{
144	pid_t r;
145	signal(SIGALRM, handler);
146	alarm(60);
147
148	if ((r = fork()) == 0) {
149		alarm(60);
150		for (;;)
151			churning();
152	}
153	if (r < 0) {
154		perror("fork");
155		exit(2);
156	}
157
158	for (;;)
159		list();
160
161	return (0);
162}
163