1d7b0e9cngie/* $NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $ */
25eef524ngie
35eef524ngie/*-
45eef524ngie * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
55eef524ngie * All rights reserved.
65eef524ngie *
75eef524ngie * This code is derived from software contributed to The NetBSD Foundation
85eef524ngie * by Luke Mewburn and Jaromir Dolecek.
95eef524ngie *
105eef524ngie * Redistribution and use in source and binary forms, with or without
115eef524ngie * modification, are permitted provided that the following conditions
125eef524ngie * are met:
135eef524ngie * 1. Redistributions of source code must retain the above copyright
145eef524ngie *    notice, this list of conditions and the following disclaimer.
155eef524ngie * 2. Redistributions in binary form must reproduce the above copyright
165eef524ngie *    notice, this list of conditions and the following disclaimer in the
175eef524ngie *    documentation and/or other materials provided with the distribution.
185eef524ngie *
195eef524ngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205eef524ngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215eef524ngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225eef524ngie * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235eef524ngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245eef524ngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255eef524ngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265eef524ngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275eef524ngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285eef524ngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295eef524ngie * POSSIBILITY OF SUCH DAMAGE.
305eef524ngie */
315eef524ngie
325eef524ngie#include <sys/cdefs.h>
335eef524ngie__COPYRIGHT("@(#) Copyright (c) 2008\
345eef524ngie The NetBSD Foundation, inc. All rights reserved.");
35d7b0e9cngie__RCSID("$NetBSD: t_proc1.c,v 1.3 2017/01/13 21:30:41 christos Exp $");
365eef524ngie
375eef524ngie/*
385eef524ngie * this also used to trigger problem fixed in
395eef524ngie * rev. 1.1.1.1.2.13 of sys/kern/kern_event.c
405eef524ngie */
415eef524ngie
425eef524ngie#include <sys/param.h>
435eef524ngie#include <sys/event.h>
445eef524ngie#include <sys/wait.h>
455eef524ngie
465eef524ngie#include <err.h>
475eef524ngie#include <stdio.h>
485eef524ngie#include <stdlib.h>
495eef524ngie#include <unistd.h>
505eef524ngie#include <inttypes.h>
515eef524ngie
525eef524ngie#include <atf-c.h>
535eef524ngie
54d7b0e9cngie#include "h_macros.h"
555eef524ngie
565eef524ngiestatic int
575eef524ngiechild(void)
585eef524ngie{
595eef524ngie	pid_t ch;
605eef524ngie	int status;
615eef524ngie	char *argv[] = { NULL, NULL };
625eef524ngie	char *envp[] = { NULL, NULL };
635eef524ngie
645eef524ngie	if ((argv[0] = strdup("true")) == NULL)
655eef524ngie		err(EXIT_FAILURE, "strdup(\"true\")");
665eef524ngie
675eef524ngie	if ((envp[0] = strdup("FOO=BAZ")) == NULL)
685eef524ngie		err(EXIT_FAILURE, "strdup(\"FOO=BAZ\")");
695eef524ngie
705eef524ngie	/* Ensure parent is ready */
715eef524ngie	(void)sleep(2);
725eef524ngie
735eef524ngie	/* Do fork */
745eef524ngie	switch (ch = fork()) {
755eef524ngie	case -1:
765eef524ngie		return EXIT_FAILURE;
775eef524ngie		/* NOTREACHED */
785eef524ngie	case 0:
795eef524ngie		return EXIT_SUCCESS;
805eef524ngie		/* NOTREACHED */
815eef524ngie	default:
825eef524ngie		wait(&status);
835eef524ngie		break;
845eef524ngie	}
855eef524ngie
865eef524ngie	/* Exec */
875eef524ngie	execve("/usr/bin/true", argv, envp);
885eef524ngie
895eef524ngie	/* NOTREACHED */
905eef524ngie	return EXIT_FAILURE;
915eef524ngie}
925eef524ngie
935eef524ngieATF_TC(proc1);
945eef524ngieATF_TC_HEAD(proc1, tc)
955eef524ngie{
965eef524ngie	atf_tc_set_md_var(tc, "descr", "Checks EVFILT_PROC");
975eef524ngie}
985eef524ngieATF_TC_BODY(proc1, tc)
995eef524ngie{
1005eef524ngie	struct kevent event[1];
1015eef524ngie	pid_t pid;
102c84c9cangie	int kq, status;
103c84c9cangie	u_int want;
1045eef524ngie
1055eef524ngie	RL(kq = kqueue());
1065eef524ngie
1075eef524ngie	/* fork a child for doing the events */
1085eef524ngie	RL(pid = fork());
1095eef524ngie	if (pid == 0) {
1105eef524ngie		_exit(child());
1115eef524ngie		/* NOTREACHED */
1125eef524ngie	}
1135eef524ngie
1145eef524ngie	(void)sleep(1); /* give child some time to come up */
1155eef524ngie
116c84c9cangie	event[0].ident = (uintptr_t)pid;
1175eef524ngie	event[0].filter = EVFILT_PROC;
1185eef524ngie	event[0].flags = EV_ADD | EV_ENABLE;
1195eef524ngie	event[0].fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC; /* | NOTE_TRACK;*/
1205eef524ngie	want = NOTE_EXIT | NOTE_FORK | NOTE_EXEC;
1215eef524ngie
1225eef524ngie	RL(kevent(kq, event, 1, NULL, 0, NULL));
1235eef524ngie
1245eef524ngie	/* wait until we get all events we want */
1255eef524ngie	while (want) {
1265eef524ngie		RL(kevent(kq, NULL, 0, event, 1, NULL));
1275eef524ngie		printf("%ld:", (long)event[0].ident);
1285eef524ngie
1295eef524ngie		if (event[0].fflags & NOTE_EXIT) {
1305eef524ngie			want &= ~NOTE_EXIT;
1315eef524ngie			printf(" NOTE_EXIT");
1325eef524ngie		}
1335eef524ngie		if (event[0].fflags & NOTE_EXEC) {
1345eef524ngie			want &= ~NOTE_EXEC;
1355eef524ngie			printf(" NOTE_EXEC");
1365eef524ngie		}
1375eef524ngie		if (event[0].fflags & NOTE_FORK) {
1385eef524ngie			want &= ~NOTE_FORK;
1395eef524ngie			printf(" NOTE_FORK");
1405eef524ngie		}
1415eef524ngie		if (event[0].fflags & NOTE_CHILD)
1425eef524ngie			printf(" NOTE_CHILD, parent = %" PRId64, event[0].data);
1435eef524ngie
1445eef524ngie		printf("\n");
1455eef524ngie	}
1465eef524ngie
1475eef524ngie	(void)waitpid(pid, &status, 0);
1485eef524ngie}
1495eef524ngie
1505eef524ngieATF_TP_ADD_TCS(tp)
1515eef524ngie{
1525eef524ngie	ATF_TP_ADD_TC(tp, proc1);
1535eef524ngie
1545eef524ngie	return atf_no_error();
1555eef524ngie}
156