1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_proc1.c,v 1.2 2015/01/14 22:22:32 christos Exp $ */
211be35a1SLionel Sambuc
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc * Copyright (c) 2002, 2008 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc * All rights reserved.
611be35a1SLionel Sambuc *
711be35a1SLionel Sambuc * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc * by Luke Mewburn and Jaromir Dolecek.
911be35a1SLionel Sambuc *
1011be35a1SLionel Sambuc * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc * are met:
1311be35a1SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc * documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc *
1911be35a1SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc */
3111be35a1SLionel Sambuc
3211be35a1SLionel Sambuc #include <sys/cdefs.h>
3311be35a1SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 2008\
3411be35a1SLionel Sambuc The NetBSD Foundation, inc. All rights reserved.");
35*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_proc1.c,v 1.2 2015/01/14 22:22:32 christos Exp $");
3611be35a1SLionel Sambuc
3711be35a1SLionel Sambuc /*
3811be35a1SLionel Sambuc * this also used to trigger problem fixed in
3911be35a1SLionel Sambuc * rev. 1.1.1.1.2.13 of sys/kern/kern_event.c
4011be35a1SLionel Sambuc */
4111be35a1SLionel Sambuc
4211be35a1SLionel Sambuc #include <sys/param.h>
4311be35a1SLionel Sambuc #include <sys/event.h>
4411be35a1SLionel Sambuc #include <sys/wait.h>
4511be35a1SLionel Sambuc
4611be35a1SLionel Sambuc #include <err.h>
4711be35a1SLionel Sambuc #include <stdio.h>
4811be35a1SLionel Sambuc #include <stdlib.h>
4911be35a1SLionel Sambuc #include <unistd.h>
5011be35a1SLionel Sambuc #include <inttypes.h>
5111be35a1SLionel Sambuc
5211be35a1SLionel Sambuc #include <atf-c.h>
5311be35a1SLionel Sambuc
5411be35a1SLionel Sambuc #include "../../h_macros.h"
5511be35a1SLionel Sambuc
5611be35a1SLionel Sambuc static int
child(void)5711be35a1SLionel Sambuc child(void)
5811be35a1SLionel Sambuc {
5911be35a1SLionel Sambuc pid_t ch;
6011be35a1SLionel Sambuc int status;
6111be35a1SLionel Sambuc char *argv[] = { NULL, NULL };
6211be35a1SLionel Sambuc char *envp[] = { NULL, NULL };
6311be35a1SLionel Sambuc
6411be35a1SLionel Sambuc if ((argv[0] = strdup("true")) == NULL)
6511be35a1SLionel Sambuc err(EXIT_FAILURE, "strdup(\"true\")");
6611be35a1SLionel Sambuc
6711be35a1SLionel Sambuc if ((envp[0] = strdup("FOO=BAZ")) == NULL)
6811be35a1SLionel Sambuc err(EXIT_FAILURE, "strdup(\"FOO=BAZ\")");
6911be35a1SLionel Sambuc
7011be35a1SLionel Sambuc /* Ensure parent is ready */
7111be35a1SLionel Sambuc (void)sleep(2);
7211be35a1SLionel Sambuc
7311be35a1SLionel Sambuc /* Do fork */
7411be35a1SLionel Sambuc switch (ch = fork()) {
7511be35a1SLionel Sambuc case -1:
7611be35a1SLionel Sambuc return EXIT_FAILURE;
7711be35a1SLionel Sambuc /* NOTREACHED */
7811be35a1SLionel Sambuc case 0:
7911be35a1SLionel Sambuc return EXIT_SUCCESS;
8011be35a1SLionel Sambuc /* NOTREACHED */
8111be35a1SLionel Sambuc default:
8211be35a1SLionel Sambuc wait(&status);
8311be35a1SLionel Sambuc break;
8411be35a1SLionel Sambuc }
8511be35a1SLionel Sambuc
8611be35a1SLionel Sambuc /* Exec */
8711be35a1SLionel Sambuc execve("/usr/bin/true", argv, envp);
8811be35a1SLionel Sambuc
8911be35a1SLionel Sambuc /* NOTREACHED */
9011be35a1SLionel Sambuc return EXIT_FAILURE;
9111be35a1SLionel Sambuc }
9211be35a1SLionel Sambuc
9311be35a1SLionel Sambuc ATF_TC(proc1);
ATF_TC_HEAD(proc1,tc)9411be35a1SLionel Sambuc ATF_TC_HEAD(proc1, tc)
9511be35a1SLionel Sambuc {
9611be35a1SLionel Sambuc atf_tc_set_md_var(tc, "descr", "Checks EVFILT_PROC");
9711be35a1SLionel Sambuc }
ATF_TC_BODY(proc1,tc)9811be35a1SLionel Sambuc ATF_TC_BODY(proc1, tc)
9911be35a1SLionel Sambuc {
10011be35a1SLionel Sambuc struct kevent event[1];
10111be35a1SLionel Sambuc pid_t pid;
102*0a6a1f1dSLionel Sambuc int kq, status;
103*0a6a1f1dSLionel Sambuc u_int want;
10411be35a1SLionel Sambuc
10511be35a1SLionel Sambuc RL(kq = kqueue());
10611be35a1SLionel Sambuc
10711be35a1SLionel Sambuc /* fork a child for doing the events */
10811be35a1SLionel Sambuc RL(pid = fork());
10911be35a1SLionel Sambuc if (pid == 0) {
11011be35a1SLionel Sambuc _exit(child());
11111be35a1SLionel Sambuc /* NOTREACHED */
11211be35a1SLionel Sambuc }
11311be35a1SLionel Sambuc
11411be35a1SLionel Sambuc (void)sleep(1); /* give child some time to come up */
11511be35a1SLionel Sambuc
116*0a6a1f1dSLionel Sambuc event[0].ident = (uintptr_t)pid;
11711be35a1SLionel Sambuc event[0].filter = EVFILT_PROC;
11811be35a1SLionel Sambuc event[0].flags = EV_ADD | EV_ENABLE;
11911be35a1SLionel Sambuc event[0].fflags = NOTE_EXIT | NOTE_FORK | NOTE_EXEC; /* | NOTE_TRACK;*/
12011be35a1SLionel Sambuc want = NOTE_EXIT | NOTE_FORK | NOTE_EXEC;
12111be35a1SLionel Sambuc
12211be35a1SLionel Sambuc RL(kevent(kq, event, 1, NULL, 0, NULL));
12311be35a1SLionel Sambuc
12411be35a1SLionel Sambuc /* wait until we get all events we want */
12511be35a1SLionel Sambuc while (want) {
12611be35a1SLionel Sambuc RL(kevent(kq, NULL, 0, event, 1, NULL));
12711be35a1SLionel Sambuc printf("%ld:", (long)event[0].ident);
12811be35a1SLionel Sambuc
12911be35a1SLionel Sambuc if (event[0].fflags & NOTE_EXIT) {
13011be35a1SLionel Sambuc want &= ~NOTE_EXIT;
13111be35a1SLionel Sambuc printf(" NOTE_EXIT");
13211be35a1SLionel Sambuc }
13311be35a1SLionel Sambuc if (event[0].fflags & NOTE_EXEC) {
13411be35a1SLionel Sambuc want &= ~NOTE_EXEC;
13511be35a1SLionel Sambuc printf(" NOTE_EXEC");
13611be35a1SLionel Sambuc }
13711be35a1SLionel Sambuc if (event[0].fflags & NOTE_FORK) {
13811be35a1SLionel Sambuc want &= ~NOTE_FORK;
13911be35a1SLionel Sambuc printf(" NOTE_FORK");
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc if (event[0].fflags & NOTE_CHILD)
14211be35a1SLionel Sambuc printf(" NOTE_CHILD, parent = %" PRId64, event[0].data);
14311be35a1SLionel Sambuc
14411be35a1SLionel Sambuc printf("\n");
14511be35a1SLionel Sambuc }
14611be35a1SLionel Sambuc
14711be35a1SLionel Sambuc (void)waitpid(pid, &status, 0);
14811be35a1SLionel Sambuc }
14911be35a1SLionel Sambuc
ATF_TP_ADD_TCS(tp)15011be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
15111be35a1SLionel Sambuc {
15211be35a1SLionel Sambuc ATF_TP_ADD_TC(tp, proc1);
15311be35a1SLionel Sambuc
15411be35a1SLionel Sambuc return atf_no_error();
15511be35a1SLionel Sambuc }
156