Home | History | Annotate | Line # | Download | only in kernel
t_epoll.c revision 1.1
      1  1.1  christos /*	$NetBSD: t_epoll.c,v 1.1 2023/07/28 18:19:01 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*-
      4  1.1  christos  * Copyright (c) 2023 The NetBSD Foundation, Inc.
      5  1.1  christos  * All rights reserved.
      6  1.1  christos  *
      7  1.1  christos  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  christos  * by Theodore Preduta.
      9  1.1  christos  *
     10  1.1  christos  * Redistribution and use in source and binary forms, with or without
     11  1.1  christos  * modification, are permitted provided that the following conditions
     12  1.1  christos  * are met:
     13  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     15  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  christos  *    documentation and/or other materials provided with the distribution.
     18  1.1  christos  *
     19  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  christos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  christos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  christos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  christos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  christos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  christos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  christos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  christos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  christos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  christos  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  christos  */
     31  1.1  christos #include <sys/cdefs.h>
     32  1.1  christos __RCSID("$NetBSD: t_epoll.c,v 1.1 2023/07/28 18:19:01 christos Exp $");
     33  1.1  christos 
     34  1.1  christos #include <sys/param.h>
     35  1.1  christos #include <sys/types.h>
     36  1.1  christos #include <sys/epoll.h>
     37  1.1  christos #include <errno.h>
     38  1.1  christos 
     39  1.1  christos #include <atf-c.h>
     40  1.1  christos 
     41  1.1  christos #include "h_macros.h"
     42  1.1  christos 
     43  1.1  christos ATF_TC(create_size);
     44  1.1  christos ATF_TC_HEAD(create_size, tc)
     45  1.1  christos {
     46  1.1  christos 
     47  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     48  1.1  christos 	    "Checks that epoll_create requires a non-positive size");
     49  1.1  christos }
     50  1.1  christos ATF_TC_BODY(create_size, tc)
     51  1.1  christos {
     52  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_create(-1), -1,
     53  1.1  christos 	    "epoll_create succeeded unexpectedly");
     54  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
     55  1.1  christos 
     56  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_create(0), -1,
     57  1.1  christos 	    "epoll_create succeeded unexpectedly");
     58  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
     59  1.1  christos 
     60  1.1  christos 	RL(epoll_create(1));
     61  1.1  christos }
     62  1.1  christos 
     63  1.1  christos ATF_TC(bad_epfd);
     64  1.1  christos ATF_TC_HEAD(bad_epfd, tc)
     65  1.1  christos {
     66  1.1  christos 
     67  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     68  1.1  christos 	    "Checks that epoll_ctl detects an invalid epfd");
     69  1.1  christos }
     70  1.1  christos ATF_TC_BODY(bad_epfd, tc)
     71  1.1  christos {
     72  1.1  christos 	int fd;
     73  1.1  christos 	struct epoll_event event;
     74  1.1  christos 
     75  1.1  christos 	RL(fd = epoll_create1(0));
     76  1.1  christos 	event.events = EPOLLIN;
     77  1.1  christos 
     78  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(-1, EPOLL_CTL_ADD, fd, &event), -1,
     79  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
     80  1.1  christos 	ATF_REQUIRE_ERRNO(EBADF, true);
     81  1.1  christos }
     82  1.1  christos 
     83  1.1  christos ATF_TC(bad_fd);
     84  1.1  christos ATF_TC_HEAD(bad_fd, tc)
     85  1.1  christos {
     86  1.1  christos 
     87  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     88  1.1  christos 	    "Checks that epoll_ctl detects an invalid fd");
     89  1.1  christos }
     90  1.1  christos ATF_TC_BODY(bad_fd, tc)
     91  1.1  christos {
     92  1.1  christos 	int epfd;
     93  1.1  christos 	struct epoll_event event;
     94  1.1  christos 
     95  1.1  christos 	RL(epfd = epoll_create1(0));
     96  1.1  christos 	event.events = EPOLLIN;
     97  1.1  christos 
     98  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, -1, &event), -1,
     99  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    100  1.1  christos 	ATF_REQUIRE_ERRNO(EBADF, true);
    101  1.1  christos }
    102  1.1  christos 
    103  1.1  christos ATF_TC(double_add);
    104  1.1  christos ATF_TC_HEAD(double_add, tc)
    105  1.1  christos {
    106  1.1  christos 
    107  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    108  1.1  christos 	    "Checks that epoll_ctl detects if a fd has already been added");
    109  1.1  christos }
    110  1.1  christos ATF_TC_BODY(double_add, tc)
    111  1.1  christos {
    112  1.1  christos 	int epfd, fd;
    113  1.1  christos 	struct epoll_event event;
    114  1.1  christos 
    115  1.1  christos 	RL(epfd = epoll_create1(0));
    116  1.1  christos 	RL(fd = epoll_create1(0));
    117  1.1  christos 	event.events = EPOLLIN;
    118  1.1  christos 
    119  1.1  christos 	RL(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event));
    120  1.1  christos 
    121  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event), -1,
    122  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    123  1.1  christos 	ATF_REQUIRE_ERRNO(EEXIST, true);
    124  1.1  christos }
    125  1.1  christos 
    126  1.1  christos ATF_TC(not_added);
    127  1.1  christos ATF_TC_HEAD(not_added, tc)
    128  1.1  christos {
    129  1.1  christos 
    130  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    131  1.1  christos 	    "Checks that epoll_ctl detects if a fd has not been added");
    132  1.1  christos }
    133  1.1  christos ATF_TC_BODY(not_added, tc)
    134  1.1  christos {
    135  1.1  christos 	int epfd, fd;
    136  1.1  christos 	struct epoll_event event;
    137  1.1  christos 
    138  1.1  christos 	RL(epfd = epoll_create1(0));
    139  1.1  christos 	RL(fd = epoll_create1(0));
    140  1.1  christos 	event.events = EPOLLIN;
    141  1.1  christos 
    142  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &event), -1,
    143  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    144  1.1  christos 	ATF_REQUIRE_ERRNO(ENOENT, true);
    145  1.1  christos 
    146  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL), -1,
    147  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    148  1.1  christos 	ATF_REQUIRE_ERRNO(ENOENT, true);
    149  1.1  christos }
    150  1.1  christos 
    151  1.1  christos ATF_TC(watching_self);
    152  1.1  christos ATF_TC_HEAD(watching_self, tc)
    153  1.1  christos {
    154  1.1  christos 
    155  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    156  1.1  christos 	    "Checks that epoll disallows watching itself");
    157  1.1  christos }
    158  1.1  christos ATF_TC_BODY(watching_self, tc)
    159  1.1  christos {
    160  1.1  christos 	int epfd;
    161  1.1  christos 	struct epoll_event event;
    162  1.1  christos 
    163  1.1  christos 	RL(epfd = epoll_create1(0));
    164  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, epfd, &event), -1,
    165  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    166  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
    167  1.1  christos }
    168  1.1  christos 
    169  1.1  christos ATF_TC(watch_loops);
    170  1.1  christos ATF_TC_HEAD(watch_loops, tc)
    171  1.1  christos {
    172  1.1  christos 
    173  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Checks that epoll disallows loops");
    174  1.1  christos }
    175  1.1  christos ATF_TC_BODY(watch_loops, tc)
    176  1.1  christos {
    177  1.1  christos         int epfd1, epfd2;
    178  1.1  christos 	struct epoll_event event;
    179  1.1  christos 
    180  1.1  christos 	event.events = EPOLLIN;
    181  1.1  christos 	RL(epfd1 = epoll_create1(0));
    182  1.1  christos 	RL(epfd2 = epoll_create1(0));
    183  1.1  christos 	RL(epoll_ctl(epfd1, EPOLL_CTL_ADD, epfd2, &event));
    184  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd2, EPOLL_CTL_ADD, epfd1, &event), -1,
    185  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    186  1.1  christos 	ATF_REQUIRE_ERRNO(ELOOP, true);
    187  1.1  christos }
    188  1.1  christos 
    189  1.1  christos ATF_TC(watch_depth);
    190  1.1  christos ATF_TC_HEAD(watch_depth, tc)
    191  1.1  christos {
    192  1.1  christos 
    193  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    194  1.1  christos 	    "Checks that epoll fails when the watch depth exceeds 5");
    195  1.1  christos }
    196  1.1  christos ATF_TC_BODY(watch_depth, tc)
    197  1.1  christos {
    198  1.1  christos 	int epfd, tmp;
    199  1.1  christos 	struct epoll_event event;
    200  1.1  christos 
    201  1.1  christos 	event.events = EPOLLIN;
    202  1.1  christos 	RL(epfd = epoll_create1(0));
    203  1.1  christos 	for (size_t i = 0; i < 4; i++) {
    204  1.1  christos 		RL(tmp = epoll_create1(0));
    205  1.1  christos 		RL(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event));
    206  1.1  christos 		epfd = tmp;
    207  1.1  christos 	}
    208  1.1  christos 	RL(tmp = epoll_create1(0));
    209  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event), -1,
    210  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    211  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
    212  1.1  christos }
    213  1.1  christos 
    214  1.1  christos ATF_TP_ADD_TCS(tp)
    215  1.1  christos {
    216  1.1  christos 	ATF_TP_ADD_TC(tp, create_size);
    217  1.1  christos 	ATF_TP_ADD_TC(tp, bad_epfd);
    218  1.1  christos 	ATF_TP_ADD_TC(tp, bad_fd);
    219  1.1  christos 	ATF_TP_ADD_TC(tp, not_added);
    220  1.1  christos 	ATF_TP_ADD_TC(tp, watching_self);
    221  1.1  christos 	ATF_TP_ADD_TC(tp, watch_loops);
    222  1.1  christos 	ATF_TP_ADD_TC(tp, watch_depth);
    223  1.1  christos 
    224  1.1  christos 	return atf_no_error();
    225  1.1  christos }
    226