xref: /minix3/tests/lib/libc/sys/t_kevent.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: t_kevent.c,v 1.7 2015/02/05 13:55:37 isaki Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundatiom
811be35a1SLionel Sambuc  * by Christos Zoulas.
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 #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_kevent.c,v 1.7 2015/02/05 13:55:37 isaki Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <sys/types.h>
3511be35a1SLionel Sambuc #include <sys/event.h>
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc #include <errno.h>
3911be35a1SLionel Sambuc #include <time.h>
4011be35a1SLionel Sambuc #include <stdio.h>
4111be35a1SLionel Sambuc #include <stdlib.h>
4211be35a1SLionel Sambuc #include <string.h>
4311be35a1SLionel Sambuc #include <unistd.h>
4411be35a1SLionel Sambuc #include <fcntl.h>
4511be35a1SLionel Sambuc #include <err.h>
4611be35a1SLionel Sambuc #include <sys/drvctlio.h>
4711be35a1SLionel Sambuc #include <sys/event.h>
4811be35a1SLionel Sambuc #include <sys/time.h>
4911be35a1SLionel Sambuc #include <sys/socket.h>
5011be35a1SLionel Sambuc #include <sys/wait.h>
5111be35a1SLionel Sambuc 
5211be35a1SLionel Sambuc ATF_TC(kevent_zerotimer);
ATF_TC_HEAD(kevent_zerotimer,tc)5311be35a1SLionel Sambuc ATF_TC_HEAD(kevent_zerotimer, tc)
5411be35a1SLionel Sambuc {
5511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that kevent with a 0 timer "
5611be35a1SLionel Sambuc 	    "does not crash the system (PR lib/45618)");
5711be35a1SLionel Sambuc }
5811be35a1SLionel Sambuc 
ATF_TC_BODY(kevent_zerotimer,tc)5911be35a1SLionel Sambuc ATF_TC_BODY(kevent_zerotimer, tc)
6011be35a1SLionel Sambuc {
6111be35a1SLionel Sambuc 	struct kevent ev;
6211be35a1SLionel Sambuc 	int kq;
6311be35a1SLionel Sambuc 
6411be35a1SLionel Sambuc 	ATF_REQUIRE((kq = kqueue()) != -1);
6511be35a1SLionel Sambuc 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
6611be35a1SLionel Sambuc 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
6711be35a1SLionel Sambuc 	ATF_REQUIRE(kevent(kq, NULL, 0, &ev, 1, NULL) == 1);
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc ATF_TC(kqueue_desc_passing);
ATF_TC_HEAD(kqueue_desc_passing,tc)7111be35a1SLionel Sambuc ATF_TC_HEAD(kqueue_desc_passing, tc)
7211be35a1SLionel Sambuc {
7311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that passing a kqueue to "
7411be35a1SLionel Sambuc 		"another process does not crash the kernel (PR 46463)");
7511be35a1SLionel Sambuc }
7611be35a1SLionel Sambuc 
ATF_TC_BODY(kqueue_desc_passing,tc)7711be35a1SLionel Sambuc ATF_TC_BODY(kqueue_desc_passing, tc)
7811be35a1SLionel Sambuc {
7911be35a1SLionel Sambuc 	pid_t child;
8011be35a1SLionel Sambuc 	int s[2], storage, status, kq;
8111be35a1SLionel Sambuc 	struct cmsghdr *msg;
8211be35a1SLionel Sambuc 	struct iovec iov;
8311be35a1SLionel Sambuc 	struct msghdr m;
8411be35a1SLionel Sambuc 	struct kevent ev;
8511be35a1SLionel Sambuc 
8611be35a1SLionel Sambuc 	ATF_REQUIRE((kq = kqueue()) != -1);
8711be35a1SLionel Sambuc 
8811be35a1SLionel Sambuc 	// atf_tc_skip("crashes kernel (PR 46463)");
8911be35a1SLionel Sambuc 
9011be35a1SLionel Sambuc 	ATF_REQUIRE(socketpair(AF_LOCAL, SOCK_STREAM, 0, s) != -1);
9111be35a1SLionel Sambuc 	msg = malloc(CMSG_SPACE(sizeof(int)));
9211be35a1SLionel Sambuc 	m.msg_iov = &iov;
9311be35a1SLionel Sambuc 	m.msg_iovlen = 1;
9411be35a1SLionel Sambuc 	m.msg_name = NULL;
9511be35a1SLionel Sambuc 	m.msg_namelen = 0;
9611be35a1SLionel Sambuc 	m.msg_control = msg;
9711be35a1SLionel Sambuc 	m.msg_controllen = CMSG_SPACE(sizeof(int));
9811be35a1SLionel Sambuc 
9911be35a1SLionel Sambuc 	child = fork();
10011be35a1SLionel Sambuc 	if (child == 0) {
10111be35a1SLionel Sambuc 		close(s[0]);
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc 		iov.iov_base = &storage;
10411be35a1SLionel Sambuc 		iov.iov_len = sizeof(int);
10511be35a1SLionel Sambuc 		m.msg_iov = &iov;
10611be35a1SLionel Sambuc 		m.msg_iovlen = 1;
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 		if (recvmsg(s[1], &m, 0) == -1)
10911be35a1SLionel Sambuc 			err(1, "child: could not recvmsg");
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc 		kq = *(int *)CMSG_DATA(msg);
11211be35a1SLionel Sambuc 		printf("child (pid %d): received kq fd %d\n", getpid(), kq);
11311be35a1SLionel Sambuc 		exit(0);
11411be35a1SLionel Sambuc 	}
11511be35a1SLionel Sambuc 
11611be35a1SLionel Sambuc 	close(s[1]);
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc 	iov.iov_base = &storage;
11911be35a1SLionel Sambuc 	iov.iov_len = sizeof(int);
12011be35a1SLionel Sambuc 
12111be35a1SLionel Sambuc 	msg->cmsg_level = SOL_SOCKET;
12211be35a1SLionel Sambuc 	msg->cmsg_type = SCM_RIGHTS;
12311be35a1SLionel Sambuc 	msg->cmsg_len = CMSG_LEN(sizeof(int));
12411be35a1SLionel Sambuc 
12511be35a1SLionel Sambuc 	*(int *)CMSG_DATA(msg) = kq;
12611be35a1SLionel Sambuc 
12711be35a1SLionel Sambuc 	EV_SET(&ev, 1, EVFILT_TIMER, EV_ADD|EV_ENABLE, 0, 1, 0);
12811be35a1SLionel Sambuc 	ATF_CHECK(kevent(kq, &ev, 1, NULL, 0, NULL) != -1);
12911be35a1SLionel Sambuc 
13011be35a1SLionel Sambuc 	printf("parent (pid %d): sending kq fd %d\n", getpid(), kq);
13111be35a1SLionel Sambuc 	if (sendmsg(s[0], &m, 0) == -1) {
13211be35a1SLionel Sambuc 		ATF_REQUIRE_EQ_MSG(errno, EBADF, "errno is %d", errno);
13311be35a1SLionel Sambuc 		atf_tc_skip("PR kern/46523");
13411be35a1SLionel Sambuc 	}
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc 	close(kq);
13711be35a1SLionel Sambuc 
13811be35a1SLionel Sambuc 	waitpid(child, &status, 0);
13911be35a1SLionel Sambuc 	ATF_CHECK(WIFEXITED(status) && WEXITSTATUS(status)==0);
14011be35a1SLionel Sambuc }
14111be35a1SLionel Sambuc 
14211be35a1SLionel Sambuc ATF_TC(kqueue_unsupported_fd);
ATF_TC_HEAD(kqueue_unsupported_fd,tc)14311be35a1SLionel Sambuc ATF_TC_HEAD(kqueue_unsupported_fd, tc)
14411be35a1SLionel Sambuc {
14511be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Checks that watching an fd whose"
14611be35a1SLionel Sambuc 	    " type is not supported does not crash the kernel");
14711be35a1SLionel Sambuc }
14811be35a1SLionel Sambuc 
ATF_TC_BODY(kqueue_unsupported_fd,tc)14911be35a1SLionel Sambuc ATF_TC_BODY(kqueue_unsupported_fd, tc)
15011be35a1SLionel Sambuc {
15111be35a1SLionel Sambuc 	/* mqueue and semaphore use fnullop_kqueue also */
15211be35a1SLionel Sambuc 	int fd, kq;
15311be35a1SLionel Sambuc 	struct kevent ev;
15411be35a1SLionel Sambuc 
15511be35a1SLionel Sambuc 	fd = open(DRVCTLDEV, O_RDONLY);
156*0a6a1f1dSLionel Sambuc 	if (fd == -1) {
157*0a6a1f1dSLionel Sambuc 		switch (errno) {
158*0a6a1f1dSLionel Sambuc 		case ENOENT:
159*0a6a1f1dSLionel Sambuc 		case ENXIO:
16011be35a1SLionel Sambuc 			atf_tc_skip("no " DRVCTLDEV " available for testing");
161*0a6a1f1dSLionel Sambuc 			break;
162*0a6a1f1dSLionel Sambuc 		}
163*0a6a1f1dSLionel Sambuc 	}
16411be35a1SLionel Sambuc 	ATF_REQUIRE(fd != -1);
16511be35a1SLionel Sambuc 	ATF_REQUIRE((kq = kqueue()) != -1);
16611be35a1SLionel Sambuc 
16711be35a1SLionel Sambuc 	EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
16811be35a1SLionel Sambuc 	   NOTE_DELETE|NOTE_WRITE|NOTE_EXTEND|NOTE_ATTRIB|NOTE_LINK|
16911be35a1SLionel Sambuc 	   NOTE_RENAME|NOTE_REVOKE, 0, 0);
17011be35a1SLionel Sambuc 
17111be35a1SLionel Sambuc 	ATF_REQUIRE(kevent(kq, &ev, 1, NULL, 0, NULL) == -1);
17211be35a1SLionel Sambuc 	ATF_REQUIRE_ERRNO(EOPNOTSUPP, true);
17311be35a1SLionel Sambuc 
17411be35a1SLionel Sambuc 	(void)close(fd);
17511be35a1SLionel Sambuc }
17611be35a1SLionel Sambuc 
17711be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)17811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
17911be35a1SLionel Sambuc {
18011be35a1SLionel Sambuc 
18111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kevent_zerotimer);
18211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kqueue_desc_passing);
18311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, kqueue_unsupported_fd);
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc 	return atf_no_error();
18611be35a1SLionel Sambuc }
187