xref: /netbsd/tests/kernel/kqueue/t_proc1.c (revision 77102b8e)
1*77102b8eSchristos /* $NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
299635d94Sjmmv 
399635d94Sjmmv /*-
499635d94Sjmmv  * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
599635d94Sjmmv  * All rights reserved.
699635d94Sjmmv  *
799635d94Sjmmv  * This code is derived from software contributed to The NetBSD Foundation
899635d94Sjmmv  * by Luke Mewburn and Jaromir Dolecek.
999635d94Sjmmv  *
1099635d94Sjmmv  * Redistribution and use in source and binary forms, with or without
1199635d94Sjmmv  * modification, are permitted provided that the following conditions
1299635d94Sjmmv  * are met:
1399635d94Sjmmv  * 1. Redistributions of source code must retain the above copyright
1499635d94Sjmmv  *    notice, this list of conditions and the following disclaimer.
1599635d94Sjmmv  * 2. Redistributions in binary form must reproduce the above copyright
1699635d94Sjmmv  *    notice, this list of conditions and the following disclaimer in the
1799635d94Sjmmv  *    documentation and/or other materials provided with the distribution.
1899635d94Sjmmv  *
1999635d94Sjmmv  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2099635d94Sjmmv  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2199635d94Sjmmv  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2299635d94Sjmmv  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2399635d94Sjmmv  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2499635d94Sjmmv  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2599635d94Sjmmv  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2699635d94Sjmmv  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2799635d94Sjmmv  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2899635d94Sjmmv  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2999635d94Sjmmv  * POSSIBILITY OF SUCH DAMAGE.
3099635d94Sjmmv  */
3199635d94Sjmmv 
3299635d94Sjmmv #include <sys/cdefs.h>
3399635d94Sjmmv __COPYRIGHT("@(#) Copyright (c) 2008\
3499635d94Sjmmv  The NetBSD Foundation, inc. All rights reserved.");
35*77102b8eSchristos __RCSID("$NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
3699635d94Sjmmv 
3799635d94Sjmmv /*
3899635d94Sjmmv  * this also used to trigger problem fixed in
3999635d94Sjmmv  * rev. 1.1.1.1.2.13 of sys/kern/kern_event.c
4099635d94Sjmmv  */
4199635d94Sjmmv 
4299635d94Sjmmv #include <sys/param.h>
4399635d94Sjmmv #include <sys/event.h>
4499635d94Sjmmv #include <sys/wait.h>
4599635d94Sjmmv 
4699635d94Sjmmv #include <err.h>
4799635d94Sjmmv #include <stdio.h>
4899635d94Sjmmv #include <stdlib.h>
4999635d94Sjmmv #include <unistd.h>
5099635d94Sjmmv #include <inttypes.h>
5199635d94Sjmmv 
5299635d94Sjmmv #include <atf-c.h>
5399635d94Sjmmv 
54*77102b8eSchristos #include "h_macros.h"
5599635d94Sjmmv 
5699635d94Sjmmv static int
child(void)5799635d94Sjmmv child(void)
5899635d94Sjmmv {
5999635d94Sjmmv 	pid_t ch;
6099635d94Sjmmv 	int status;
6199635d94Sjmmv 	char *argv[] = { NULL, NULL };
6299635d94Sjmmv 	char *envp[] = { NULL, NULL };
6399635d94Sjmmv 
6499635d94Sjmmv 	if ((argv[0] = strdup("true")) == NULL)
6599635d94Sjmmv 		err(EXIT_FAILURE, "strdup(\"true\")");
6699635d94Sjmmv 
6799635d94Sjmmv 	if ((envp[0] = strdup("FOO=BAZ")) == NULL)
6899635d94Sjmmv 		err(EXIT_FAILURE, "strdup(\"FOO=BAZ\")");
6999635d94Sjmmv 
7099635d94Sjmmv 	/* Ensure parent is ready */
7199635d94Sjmmv 	(void)sleep(2);
7299635d94Sjmmv 
7399635d94Sjmmv 	/* Do fork */
7499635d94Sjmmv 	switch (ch = fork()) {
7599635d94Sjmmv 	case -1:
7699635d94Sjmmv 		return EXIT_FAILURE;
7799635d94Sjmmv 		/* NOTREACHED */
7899635d94Sjmmv 	case 0:
7999635d94Sjmmv 		return EXIT_SUCCESS;
8099635d94Sjmmv 		/* NOTREACHED */
8199635d94Sjmmv 	default:
8299635d94Sjmmv 		wait(&status);
8399635d94Sjmmv 		break;
8499635d94Sjmmv 	}
8599635d94Sjmmv 
8699635d94Sjmmv 	/* Exec */
8799635d94Sjmmv 	execve("/usr/bin/true", argv, envp);
8899635d94Sjmmv 
8999635d94Sjmmv 	/* NOTREACHED */
9099635d94Sjmmv 	return EXIT_FAILURE;
9199635d94Sjmmv }
9299635d94Sjmmv 
9399635d94Sjmmv ATF_TC(proc1);
ATF_TC_HEAD(proc1,tc)9499635d94Sjmmv ATF_TC_HEAD(proc1, tc)
9599635d94Sjmmv {
9699635d94Sjmmv 	atf_tc_set_md_var(tc, "descr", "Checks EVFILT_PROC");
9799635d94Sjmmv }
ATF_TC_BODY(proc1,tc)9899635d94Sjmmv ATF_TC_BODY(proc1, tc)
9999635d94Sjmmv {
10099635d94Sjmmv 	struct kevent event[1];
10199635d94Sjmmv 	pid_t pid;
102a9b460f7Schristos 	int kq, status;
103a9b460f7Schristos 	u_int want;
10499635d94Sjmmv 
10599635d94Sjmmv 	RL(kq = kqueue());
10699635d94Sjmmv 
10799635d94Sjmmv 	/* fork a child for doing the events */
10899635d94Sjmmv 	RL(pid = fork());
10999635d94Sjmmv 	if (pid == 0) {
11099635d94Sjmmv 		_exit(child());
11199635d94Sjmmv 		/* NOTREACHED */
11299635d94Sjmmv 	}
11399635d94Sjmmv 
11499635d94Sjmmv 	(void)sleep(1); /* give child some time to come up */
11599635d94Sjmmv 
116a9b460f7Schristos 	event[0].ident = (uintptr_t)pid;
11799635d94Sjmmv 	event[0].filter = EVFILT_PROC;
11899635d94Sjmmv 	event[0].flags = EV_ADD | EV_ENABLE;
11999635d94Sjmmv 	event[0].fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC; /* | NOTE_TRACK;*/
12099635d94Sjmmv 	want = NOTE_EXIT | NOTE_FORK | NOTE_EXEC;
12199635d94Sjmmv 
12299635d94Sjmmv 	RL(kevent(kq, event, 1, NULL, 0, NULL));
12399635d94Sjmmv 
12499635d94Sjmmv 	/* wait until we get all events we want */
12599635d94Sjmmv 	while (want) {
12699635d94Sjmmv 		RL(kevent(kq, NULL, 0, event, 1, NULL));
12799635d94Sjmmv 		printf("%ld:", (long)event[0].ident);
12899635d94Sjmmv 
12999635d94Sjmmv 		if (event[0].fflags & NOTE_EXIT) {
13099635d94Sjmmv 			want &= ~NOTE_EXIT;
13199635d94Sjmmv 			printf(" NOTE_EXIT");
13299635d94Sjmmv 		}
13399635d94Sjmmv 		if (event[0].fflags & NOTE_EXEC) {
13499635d94Sjmmv 			want &= ~NOTE_EXEC;
13599635d94Sjmmv 			printf(" NOTE_EXEC");
13699635d94Sjmmv 		}
13799635d94Sjmmv 		if (event[0].fflags & NOTE_FORK) {
13899635d94Sjmmv 			want &= ~NOTE_FORK;
13999635d94Sjmmv 			printf(" NOTE_FORK");
14099635d94Sjmmv 		}
14199635d94Sjmmv 		if (event[0].fflags & NOTE_CHILD)
14299635d94Sjmmv 			printf(" NOTE_CHILD, parent = %" PRId64, event[0].data);
14399635d94Sjmmv 
14499635d94Sjmmv 		printf("\n");
14599635d94Sjmmv 	}
14699635d94Sjmmv 
14799635d94Sjmmv 	(void)waitpid(pid, &status, 0);
14899635d94Sjmmv }
14999635d94Sjmmv 
ATF_TP_ADD_TCS(tp)15099635d94Sjmmv ATF_TP_ADD_TCS(tp)
15199635d94Sjmmv {
15299635d94Sjmmv 	ATF_TP_ADD_TC(tp, proc1);
15399635d94Sjmmv 
15499635d94Sjmmv 	return atf_no_error();
15599635d94Sjmmv }
156