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