xref: /freebsd/tools/test/stress2/misc/killpg.sh (revision 2774f206809b8fd3a4904fe945f029a414fbc642)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 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# Regression test for r241859:
30# Return EPERM if processes were found but they were unable to be signaled.
31
32[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
33
34. ../default.cfg
35
36here=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $here/$0 > killpg.c
39mycc -o killpg -Wall -Wextra killpg.c || exit 1
40rm -f killpg.c
41
42/tmp/killpg
43
44rm -f /tmp/killpg
45exit 0
46EOF
47#include <sys/types.h>
48#include <sys/wait.h>
49
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <pwd.h>
54#include <signal.h>
55#include <stdio.h>
56#include <stdlib.h>
57#include <unistd.h>
58
59static pid_t gid, pid;
60
61#define LOOPS 100
62
63static void
64hand(int i __unused) {	/* handler */
65	_exit(0);
66}
67
68static void
69nlooper(void)
70{
71	int i;
72
73	setproctitle("nlooper");
74	setpgrp(0, getpid());
75	for (i = 0; i < LOOPS; i++) {
76		if ((pid = fork()) == 0) {
77			signal(SIGINFO, hand);
78			if (arc4random() % 100 < 10)
79				usleep(arc4random() % 100);
80			_exit(0);
81		}
82		wait(NULL);
83	}
84	_exit(0);
85}
86
87static void
88looper(void)
89{
90	int i;
91
92	setproctitle("looper");
93	setpgrp(0, getpid());
94	for (i = 0; i < LOOPS; i++) {
95		if ((pid = fork()) == 0) {
96			signal(SIGINFO, hand);
97			if (arc4random() % 100 < 10)
98				usleep(arc4random() % 100);
99			_exit(0);
100		}
101		wait(NULL);
102	}
103	unlink("cont");
104	_exit(0);
105}
106
107static void
108killer(void)
109{
110	struct passwd *pw;
111
112	setproctitle("killer");
113	if ((pw = getpwnam("nobody")) == NULL)
114		err(1, "no such user: nobody");
115
116	if (setgroups(1, &pw->pw_gid) ||
117	    setegid(pw->pw_gid) || setgid(pw->pw_gid) ||
118	    seteuid(pw->pw_uid) || setuid(pw->pw_uid))
119		err(1, "Can't drop privileges to \"nobody\"");
120	endpwent();
121
122	if ((gid = fork()) == 0)
123		nlooper();	/* nobody looper */
124
125	sleep(1);
126	while (access("cont", R_OK) == 0) {
127		if (killpg(gid, SIGINFO) == -1) {
128			if (errno == EPERM)
129				continue;
130			warn("FAIL killpg");
131		}
132	}
133	_exit(0);
134}
135
136int
137main(void)
138{
139	int fd;
140
141	if ((fd = open("cont", O_RDWR | O_CREAT, 0666)) == -1)
142		err(1, "creat");
143	close(fd);
144	signal(SIGINFO, SIG_IGN);
145
146	if ((gid = fork()) == 0)
147		looper();
148	if (fork() == 0)
149		killer();
150
151	wait(NULL);
152	wait(NULL);
153
154	return (0);
155}
156