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