xref: /netbsd-src/tests/kernel/kqueue/t_proc3.c (revision c54cb81102ced2313cb40993fe05548aca9933a1)
1*c54cb811Schristos /* $NetBSD: t_proc3.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
2f611942eSjoerg 
3f611942eSjoerg /*-
4f611942eSjoerg  * Copyright (c) 2012 The NetBSD Foundation, Inc.
5f611942eSjoerg  * All rights reserved.
6f611942eSjoerg  *
7f611942eSjoerg  * This code is derived from software contributed to The NetBSD Foundation
8f611942eSjoerg  * by Joerg Sonnenberger.
9f611942eSjoerg  *
10f611942eSjoerg  * Redistribution and use in source and binary forms, with or without
11f611942eSjoerg  * modification, are permitted provided that the following conditions
12f611942eSjoerg  * are met:
13f611942eSjoerg  * 1. Redistributions of source code must retain the above copyright
14f611942eSjoerg  *    notice, this list of conditions and the following disclaimer.
15f611942eSjoerg  * 2. Redistributions in binary form must reproduce the above copyright
16f611942eSjoerg  *    notice, this list of conditions and the following disclaimer in the
17f611942eSjoerg  *    documentation and/or other materials provided with the distribution.
18f611942eSjoerg  *
19f611942eSjoerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20f611942eSjoerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21f611942eSjoerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22f611942eSjoerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23f611942eSjoerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24f611942eSjoerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25f611942eSjoerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26f611942eSjoerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27f611942eSjoerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28f611942eSjoerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29f611942eSjoerg  * POSSIBILITY OF SUCH DAMAGE.
30f611942eSjoerg  */
31f611942eSjoerg 
32f611942eSjoerg #include <sys/cdefs.h>
33*c54cb811Schristos __RCSID("$NetBSD: t_proc3.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
34f611942eSjoerg 
35f611942eSjoerg #include <sys/event.h>
36f611942eSjoerg #include <sys/time.h>
37f611942eSjoerg #include <sys/types.h>
38f611942eSjoerg #include <sys/wait.h>
39f611942eSjoerg 
40f611942eSjoerg #include <err.h>
41f611942eSjoerg #include <pwd.h>
42f611942eSjoerg #include <stdio.h>
43f611942eSjoerg #include <stdlib.h>
44f611942eSjoerg #include <unistd.h>
45f611942eSjoerg 
46f611942eSjoerg #include <atf-c.h>
47f611942eSjoerg 
48*c54cb811Schristos #include "h_macros.h"
49f611942eSjoerg 
50f611942eSjoerg ATF_TC(proc3);
ATF_TC_HEAD(proc3,tc)51f611942eSjoerg ATF_TC_HEAD(proc3, tc)
52f611942eSjoerg {
53f611942eSjoerg 	atf_tc_set_md_var(tc, "descr",
54f611942eSjoerg 	    "Checks EVFILT_PROC for NOTE_TRACK on self bug ");
55f611942eSjoerg }
56f611942eSjoerg 
ATF_TC_BODY(proc3,tc)57f611942eSjoerg ATF_TC_BODY(proc3, tc)
58f611942eSjoerg {
59f611942eSjoerg 	pid_t pid = 0;
60f611942eSjoerg 	int kq, status;
61f611942eSjoerg 	struct kevent ke;
62f611942eSjoerg 	struct timespec timeout;
63f611942eSjoerg 
64f611942eSjoerg 	RL(kq = kqueue());
65f611942eSjoerg 
66d0b8ae57Schristos 	EV_SET(&ke, (uintptr_t)getpid(), EVFILT_PROC, EV_ADD, NOTE_TRACK, 0, 0);
67f611942eSjoerg 
68f611942eSjoerg 	RL(kevent(kq, &ke, 1, NULL, 0, NULL));
69f611942eSjoerg 
70f611942eSjoerg 	RL(pid = fork());
71f611942eSjoerg 	if (pid == 0) {
72f611942eSjoerg 		_exit(EXIT_SUCCESS);
73f611942eSjoerg 		/* NOTREACHED */
74f611942eSjoerg 	}
75f611942eSjoerg 
76f611942eSjoerg 	RL(waitpid(pid, &status, 0));
77f611942eSjoerg 	ATF_REQUIRE(WIFEXITED(status));
78f611942eSjoerg 	ATF_REQUIRE_EQ(WEXITSTATUS(status), EXIT_SUCCESS);
79f611942eSjoerg 
80f611942eSjoerg 	timeout.tv_sec = 0;
81f611942eSjoerg 	timeout.tv_nsec = 0;
82f611942eSjoerg 	ke.ident = 0;
83f611942eSjoerg 	ke.fflags = 0;
84f611942eSjoerg 	ke.flags = EV_ENABLE;
85f611942eSjoerg 
86f611942eSjoerg 	RL(kevent(kq, NULL, 0, &ke, 1, &timeout));
87f611942eSjoerg 	RL(close(kq));
88f611942eSjoerg 
89f611942eSjoerg 	ATF_REQUIRE(ke.fflags & NOTE_CHILD);
90f611942eSjoerg 	ATF_REQUIRE((ke.fflags & NOTE_TRACKERR) == 0);
91f611942eSjoerg 	ATF_REQUIRE_EQ((pid_t)ke.ident, pid);
92f611942eSjoerg }
93f611942eSjoerg 
ATF_TP_ADD_TCS(tp)94f611942eSjoerg ATF_TP_ADD_TCS(tp)
95f611942eSjoerg {
96f611942eSjoerg 	ATF_TP_ADD_TC(tp, proc3);
97f611942eSjoerg 
98f611942eSjoerg 	return atf_no_error();
99f611942eSjoerg }
100