xref: /freebsd/tools/test/stress2/misc/execve.sh (revision 9768746b)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
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 of execve(2) from a threaded program.
30# "load: 5.40  cmd: bash 24517 [vmmaps] 2.45r 0.00u 0.00s 0% 3448k" seen.
31# Fixed by r282708.
32
33. ../default.cfg
34
35here=`pwd`
36cd /tmp
37sed '1,/^EOF/d' < $here/$0 > execve.c
38mycc -o execve -Wall -Wextra -O2 execve.c -lpthread || exit 1
39rm -f execve.c
40
41daemon sh -c "(cd $here/../testcases/swap; ./swap -t 20m -i 20 -l 100)" > \
42    /dev/null 2>&1
43sleep `jot -r 1 1 9`
44for i in `jot 2`; do
45	/tmp/execve
46done
47while pgrep -q swap; do
48	pkill -9 swap
49done
50
51rm -f /tmp/execve /tmp/execve.core
52exit 0
53EOF
54#include <sys/types.h>
55#include <sys/resource.h>
56#include <sys/stat.h>
57#include <sys/wait.h>
58
59#include <err.h>
60#include <errno.h>
61#include <fcntl.h>
62#include <pthread.h>
63#include <sched.h>
64#include <stdio.h>
65#include <stdlib.h>
66#include <string.h>
67#include <unistd.h>
68
69#define LOOPS 50
70#define PARALLEL 50
71
72volatile int go;
73
74void *
75texecve(void *arg __unused)
76{
77	char *cmdline[] = { "/usr/bin/true", NULL };
78
79	while (go == 0)
80		usleep(100);
81        if (execve(cmdline[0], cmdline, NULL) == -1)
82		err(1, "execve");
83
84	return (NULL);
85}
86
87void
88test(void)
89{
90	pthread_t tid[5];
91	int i, rc;
92
93	go = 0;
94
95	for (i = 0; i < 5; i++) {
96		if ((rc = pthread_create(&tid[i], NULL, texecve, NULL)) != 0)
97			errc(1, rc, "texecve()");
98	}
99
100	usleep(arc4random() % 2000);
101	go = 1;
102
103	for (i = 0; i < 5; i++)
104		if ((rc = pthread_join(tid[i], NULL)) != 0)
105			errc(1, rc, "pthread_join(%d)", i);
106	_exit(0);
107}
108
109int
110main(void)
111{
112	struct rlimit rl;
113	int i, j;
114
115	rl.rlim_max = rl.rlim_cur = 0;
116	if (setrlimit(RLIMIT_CORE, &rl) == -1)
117		warn("setrlimit");
118
119	for (i = 0; i < LOOPS; i++) {
120		for (j = 0; j < PARALLEL; j++) {
121			if (fork() == 0)
122				test();
123		}
124
125		for (j = 0; j < PARALLEL; j++)
126			wait(NULL);
127	}
128
129	return (0);
130}
131