1*b4c8cdcdSthorpej /* $NetBSD: t_proc4.c,v 1.1 2021/10/10 17:43:15 thorpej Exp $ */
2*b4c8cdcdSthorpej
3*b4c8cdcdSthorpej /*-
4*b4c8cdcdSthorpej * Copyright (c) 2021 The NetBSD Foundation, Inc.
5*b4c8cdcdSthorpej * All rights reserved.
6*b4c8cdcdSthorpej *
7*b4c8cdcdSthorpej * Redistribution and use in source and binary forms, with or without
8*b4c8cdcdSthorpej * modification, are permitted provided that the following conditions
9*b4c8cdcdSthorpej * are met:
10*b4c8cdcdSthorpej * 1. Redistributions of source code must retain the above copyright
11*b4c8cdcdSthorpej * notice, this list of conditions and the following disclaimer.
12*b4c8cdcdSthorpej * 2. Redistributions in binary form must reproduce the above copyright
13*b4c8cdcdSthorpej * notice, this list of conditions and the following disclaimer in the
14*b4c8cdcdSthorpej * documentation and/or other materials provided with the distribution.
15*b4c8cdcdSthorpej *
16*b4c8cdcdSthorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*b4c8cdcdSthorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*b4c8cdcdSthorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*b4c8cdcdSthorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*b4c8cdcdSthorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*b4c8cdcdSthorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*b4c8cdcdSthorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*b4c8cdcdSthorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*b4c8cdcdSthorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*b4c8cdcdSthorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*b4c8cdcdSthorpej * POSSIBILITY OF SUCH DAMAGE.
27*b4c8cdcdSthorpej */
28*b4c8cdcdSthorpej
29*b4c8cdcdSthorpej #include <sys/cdefs.h>
30*b4c8cdcdSthorpej __RCSID("$NetBSD: t_proc4.c,v 1.1 2021/10/10 17:43:15 thorpej Exp $");
31*b4c8cdcdSthorpej
32*b4c8cdcdSthorpej #include <sys/event.h>
33*b4c8cdcdSthorpej #include <sys/time.h>
34*b4c8cdcdSthorpej #include <sys/types.h>
35*b4c8cdcdSthorpej #include <sys/wait.h>
36*b4c8cdcdSthorpej
37*b4c8cdcdSthorpej #include <err.h>
38*b4c8cdcdSthorpej #include <limits.h>
39*b4c8cdcdSthorpej #include <pwd.h>
40*b4c8cdcdSthorpej #include <stdio.h>
41*b4c8cdcdSthorpej #include <stdlib.h>
42*b4c8cdcdSthorpej #include <unistd.h>
43*b4c8cdcdSthorpej
44*b4c8cdcdSthorpej #include <atf-c.h>
45*b4c8cdcdSthorpej
46*b4c8cdcdSthorpej /* 10 children each create 10 grandchildren. */
47*b4c8cdcdSthorpej #define TOTAL_CHILDREN 10
48*b4c8cdcdSthorpej #define TOTAL_GRANDCHILDREN (TOTAL_CHILDREN * TOTAL_CHILDREN)
49*b4c8cdcdSthorpej #define TOTAL_DESCENDENTS (TOTAL_CHILDREN + TOTAL_GRANDCHILDREN)
50*b4c8cdcdSthorpej
51*b4c8cdcdSthorpej #define EXIT_CHILD 66
52*b4c8cdcdSthorpej #define EXIT_GRANDCHILD 76
53*b4c8cdcdSthorpej
54*b4c8cdcdSthorpej static void
child(void)55*b4c8cdcdSthorpej child(void)
56*b4c8cdcdSthorpej {
57*b4c8cdcdSthorpej int i, status;
58*b4c8cdcdSthorpej pid_t pid;
59*b4c8cdcdSthorpej
60*b4c8cdcdSthorpej for (i = 0; i < TOTAL_CHILDREN; i++) {
61*b4c8cdcdSthorpej ATF_REQUIRE((pid = fork()) != -1);
62*b4c8cdcdSthorpej if (pid == 0) {
63*b4c8cdcdSthorpej /* grandchild */
64*b4c8cdcdSthorpej _exit(EXIT_GRANDCHILD);
65*b4c8cdcdSthorpej }
66*b4c8cdcdSthorpej }
67*b4c8cdcdSthorpej
68*b4c8cdcdSthorpej for (i = 0; i < TOTAL_CHILDREN; i++) {
69*b4c8cdcdSthorpej ATF_REQUIRE((pid = wait(&status)) != -1);
70*b4c8cdcdSthorpej }
71*b4c8cdcdSthorpej _exit(EXIT_CHILD);
72*b4c8cdcdSthorpej }
73*b4c8cdcdSthorpej
74*b4c8cdcdSthorpej ATF_TC(proc4);
ATF_TC_HEAD(proc4,tc)75*b4c8cdcdSthorpej ATF_TC_HEAD(proc4, tc)
76*b4c8cdcdSthorpej {
77*b4c8cdcdSthorpej atf_tc_set_md_var(tc, "descr", "Exercises EVFILT_PROC + NOTE_TRACK");
78*b4c8cdcdSthorpej }
79*b4c8cdcdSthorpej
ATF_TC_BODY(proc4,tc)80*b4c8cdcdSthorpej ATF_TC_BODY(proc4, tc)
81*b4c8cdcdSthorpej {
82*b4c8cdcdSthorpej int total_tracks = 0;
83*b4c8cdcdSthorpej int child_exits = 0;
84*b4c8cdcdSthorpej int grandchild_exits = 0;
85*b4c8cdcdSthorpej pid_t pid;
86*b4c8cdcdSthorpej int kq, status;
87*b4c8cdcdSthorpej struct kevent event;
88*b4c8cdcdSthorpej struct timespec ts = { 0, 0 };
89*b4c8cdcdSthorpej int i, rv;
90*b4c8cdcdSthorpej
91*b4c8cdcdSthorpej ATF_REQUIRE((kq = kqueue()) >= 0);
92*b4c8cdcdSthorpej
93*b4c8cdcdSthorpej EV_SET(&event, getpid(), EVFILT_PROC, EV_ADD,
94*b4c8cdcdSthorpej NOTE_TRACK | NOTE_EXIT, 0, 0);
95*b4c8cdcdSthorpej
96*b4c8cdcdSthorpej ATF_REQUIRE(kevent(kq, &event, 1, NULL, 0, NULL) == 0);
97*b4c8cdcdSthorpej
98*b4c8cdcdSthorpej for (i = 0; i < TOTAL_CHILDREN; i++) {
99*b4c8cdcdSthorpej ATF_REQUIRE((pid = fork()) != -1);
100*b4c8cdcdSthorpej if (pid == 0) {
101*b4c8cdcdSthorpej child();
102*b4c8cdcdSthorpej /* NOTREACHED */
103*b4c8cdcdSthorpej }
104*b4c8cdcdSthorpej }
105*b4c8cdcdSthorpej
106*b4c8cdcdSthorpej for (;;) {
107*b4c8cdcdSthorpej if (total_tracks == TOTAL_DESCENDENTS &&
108*b4c8cdcdSthorpej child_exits == TOTAL_CHILDREN &&
109*b4c8cdcdSthorpej grandchild_exits == TOTAL_GRANDCHILDREN) {
110*b4c8cdcdSthorpej break;
111*b4c8cdcdSthorpej }
112*b4c8cdcdSthorpej memset(&event, 0, sizeof(event));
113*b4c8cdcdSthorpej rv = kevent(kq, NULL, 0, &event, 1, NULL);
114*b4c8cdcdSthorpej ATF_REQUIRE(rv != -1);
115*b4c8cdcdSthorpej ATF_REQUIRE(rv == 1);
116*b4c8cdcdSthorpej if (event.fflags & NOTE_CHILD) {
117*b4c8cdcdSthorpej total_tracks++;
118*b4c8cdcdSthorpej ATF_REQUIRE(total_tracks <= TOTAL_DESCENDENTS);
119*b4c8cdcdSthorpej } else if (event.fflags & NOTE_EXIT) {
120*b4c8cdcdSthorpej ATF_REQUIRE(event.flags & EV_EOF);
121*b4c8cdcdSthorpej ATF_REQUIRE(event.data >= 0);
122*b4c8cdcdSthorpej ATF_REQUIRE(event.data <= UINT_MAX);
123*b4c8cdcdSthorpej status = (int)event.data;
124*b4c8cdcdSthorpej ATF_REQUIRE(WIFEXITED(status));
125*b4c8cdcdSthorpej ATF_REQUIRE(WEXITSTATUS(status) == EXIT_CHILD ||
126*b4c8cdcdSthorpej WEXITSTATUS(status) == EXIT_GRANDCHILD);
127*b4c8cdcdSthorpej if (WEXITSTATUS(status) == EXIT_CHILD) {
128*b4c8cdcdSthorpej child_exits++;
129*b4c8cdcdSthorpej ATF_REQUIRE(child_exits <= TOTAL_CHILDREN);
130*b4c8cdcdSthorpej ATF_REQUIRE(wait4((pid_t)event.ident,
131*b4c8cdcdSthorpej &status,
132*b4c8cdcdSthorpej WNOHANG,
133*b4c8cdcdSthorpej NULL) != -1);
134*b4c8cdcdSthorpej ATF_REQUIRE(status == (int)event.data);
135*b4c8cdcdSthorpej } else {
136*b4c8cdcdSthorpej grandchild_exits++;
137*b4c8cdcdSthorpej ATF_REQUIRE(grandchild_exits <=
138*b4c8cdcdSthorpej TOTAL_GRANDCHILDREN);
139*b4c8cdcdSthorpej }
140*b4c8cdcdSthorpej } else {
141*b4c8cdcdSthorpej /*
142*b4c8cdcdSthorpej * We didn't ask for NOTE_FORK, so we don't
143*b4c8cdcdSthorpej * expect to ever see it, even though we are
144*b4c8cdcdSthorpej * getting NOTE_CHILD as the result of the
145*b4c8cdcdSthorpej * NOTE_TRACK.
146*b4c8cdcdSthorpej */
147*b4c8cdcdSthorpej ATF_REQUIRE((event.fflags & NOTE_FORK) == 0);
148*b4c8cdcdSthorpej
149*b4c8cdcdSthorpej /*
150*b4c8cdcdSthorpej * Bomb out of we are getting a TRACKERR.
151*b4c8cdcdSthorpej * XXX This could legitimately happen if
152*b4c8cdcdSthorpej * the kernel is low on memory because the
153*b4c8cdcdSthorpej * code path involved specifically chooses
154*b4c8cdcdSthorpej * not to block when allocating memory.
155*b4c8cdcdSthorpej */
156*b4c8cdcdSthorpej ATF_REQUIRE((event.fflags & NOTE_TRACKERR) == 0);
157*b4c8cdcdSthorpej }
158*b4c8cdcdSthorpej }
159*b4c8cdcdSthorpej
160*b4c8cdcdSthorpej ATF_REQUIRE(kevent(kq, NULL, 0, &event, 1, &ts) == 0);
161*b4c8cdcdSthorpej
162*b4c8cdcdSthorpej (void)close(kq);
163*b4c8cdcdSthorpej }
164*b4c8cdcdSthorpej
ATF_TP_ADD_TCS(tp)165*b4c8cdcdSthorpej ATF_TP_ADD_TCS(tp)
166*b4c8cdcdSthorpej {
167*b4c8cdcdSthorpej ATF_TP_ADD_TC(tp, proc4);
168*b4c8cdcdSthorpej
169*b4c8cdcdSthorpej return atf_no_error();
170*b4c8cdcdSthorpej }
171