Home | History | Annotate | Line # | Download | only in kernel
      1  1.2  christos /*	$NetBSD: t_epoll.c,v 1.2 2023/07/30 18:31:14 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.2  christos __RCSID("$NetBSD: t_epoll.c,v 1.2 2023/07/30 18:31:14 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.2  christos #include <sys/fcntl.h>
     38  1.1  christos #include <errno.h>
     39  1.1  christos 
     40  1.1  christos #include <atf-c.h>
     41  1.1  christos 
     42  1.1  christos #include "h_macros.h"
     43  1.1  christos 
     44  1.1  christos ATF_TC(create_size);
     45  1.1  christos ATF_TC_HEAD(create_size, tc)
     46  1.1  christos {
     47  1.1  christos 
     48  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     49  1.1  christos 	    "Checks that epoll_create requires a non-positive size");
     50  1.1  christos }
     51  1.1  christos ATF_TC_BODY(create_size, tc)
     52  1.1  christos {
     53  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_create(-1), -1,
     54  1.1  christos 	    "epoll_create succeeded unexpectedly");
     55  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
     56  1.1  christos 
     57  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_create(0), -1,
     58  1.1  christos 	    "epoll_create succeeded unexpectedly");
     59  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
     60  1.1  christos 
     61  1.1  christos 	RL(epoll_create(1));
     62  1.1  christos }
     63  1.1  christos 
     64  1.2  christos ATF_TC(create_cloexec);
     65  1.2  christos ATF_TC_HEAD(create_cloexec, tc)
     66  1.2  christos {
     67  1.2  christos 
     68  1.2  christos 	atf_tc_set_md_var(tc, "descr",
     69  1.2  christos 	    "Checks that epoll_create1 sets close on exec when desired");
     70  1.2  christos }
     71  1.2  christos ATF_TC_BODY(create_cloexec, tc)
     72  1.2  christos {
     73  1.2  christos 	int fd;
     74  1.2  christos 
     75  1.2  christos 	RL(fd = epoll_create1(0));
     76  1.2  christos 	ATF_REQUIRE_MSG((fcntl(fd, F_GETFD) & FD_CLOEXEC) == 0,
     77  1.2  christos 	    "Close on exec set unexpectedly.");
     78  1.2  christos 
     79  1.2  christos 	RL(fd = epoll_create1(EPOLL_CLOEXEC));
     80  1.2  christos 	ATF_REQUIRE_MSG((fcntl(fd, F_GETFD) & FD_CLOEXEC) != 0,
     81  1.2  christos 	    "Close on exec was not set.");
     82  1.2  christos }
     83  1.2  christos 
     84  1.1  christos ATF_TC(bad_epfd);
     85  1.1  christos ATF_TC_HEAD(bad_epfd, tc)
     86  1.1  christos {
     87  1.1  christos 
     88  1.1  christos 	atf_tc_set_md_var(tc, "descr",
     89  1.1  christos 	    "Checks that epoll_ctl detects an invalid epfd");
     90  1.1  christos }
     91  1.1  christos ATF_TC_BODY(bad_epfd, tc)
     92  1.1  christos {
     93  1.1  christos 	int fd;
     94  1.1  christos 	struct epoll_event event;
     95  1.1  christos 
     96  1.1  christos 	RL(fd = epoll_create1(0));
     97  1.1  christos 	event.events = EPOLLIN;
     98  1.1  christos 
     99  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(-1, EPOLL_CTL_ADD, fd, &event), -1,
    100  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    101  1.1  christos 	ATF_REQUIRE_ERRNO(EBADF, true);
    102  1.1  christos }
    103  1.1  christos 
    104  1.1  christos ATF_TC(bad_fd);
    105  1.1  christos ATF_TC_HEAD(bad_fd, tc)
    106  1.1  christos {
    107  1.1  christos 
    108  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    109  1.1  christos 	    "Checks that epoll_ctl detects an invalid fd");
    110  1.1  christos }
    111  1.1  christos ATF_TC_BODY(bad_fd, tc)
    112  1.1  christos {
    113  1.1  christos 	int epfd;
    114  1.1  christos 	struct epoll_event event;
    115  1.1  christos 
    116  1.1  christos 	RL(epfd = epoll_create1(0));
    117  1.1  christos 	event.events = EPOLLIN;
    118  1.1  christos 
    119  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, -1, &event), -1,
    120  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    121  1.1  christos 	ATF_REQUIRE_ERRNO(EBADF, true);
    122  1.1  christos }
    123  1.1  christos 
    124  1.1  christos ATF_TC(double_add);
    125  1.1  christos ATF_TC_HEAD(double_add, tc)
    126  1.1  christos {
    127  1.1  christos 
    128  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    129  1.1  christos 	    "Checks that epoll_ctl detects if a fd has already been added");
    130  1.1  christos }
    131  1.1  christos ATF_TC_BODY(double_add, tc)
    132  1.1  christos {
    133  1.1  christos 	int epfd, fd;
    134  1.1  christos 	struct epoll_event event;
    135  1.1  christos 
    136  1.1  christos 	RL(epfd = epoll_create1(0));
    137  1.1  christos 	RL(fd = epoll_create1(0));
    138  1.1  christos 	event.events = EPOLLIN;
    139  1.1  christos 
    140  1.1  christos 	RL(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event));
    141  1.1  christos 
    142  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, fd, &event), -1,
    143  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    144  1.1  christos 	ATF_REQUIRE_ERRNO(EEXIST, true);
    145  1.1  christos }
    146  1.1  christos 
    147  1.1  christos ATF_TC(not_added);
    148  1.1  christos ATF_TC_HEAD(not_added, tc)
    149  1.1  christos {
    150  1.1  christos 
    151  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    152  1.1  christos 	    "Checks that epoll_ctl detects if a fd has not been added");
    153  1.1  christos }
    154  1.1  christos ATF_TC_BODY(not_added, tc)
    155  1.1  christos {
    156  1.1  christos 	int epfd, fd;
    157  1.1  christos 	struct epoll_event event;
    158  1.1  christos 
    159  1.1  christos 	RL(epfd = epoll_create1(0));
    160  1.1  christos 	RL(fd = epoll_create1(0));
    161  1.1  christos 	event.events = EPOLLIN;
    162  1.1  christos 
    163  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &event), -1,
    164  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    165  1.1  christos 	ATF_REQUIRE_ERRNO(ENOENT, true);
    166  1.1  christos 
    167  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_DEL, fd, NULL), -1,
    168  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    169  1.1  christos 	ATF_REQUIRE_ERRNO(ENOENT, true);
    170  1.1  christos }
    171  1.1  christos 
    172  1.1  christos ATF_TC(watching_self);
    173  1.1  christos ATF_TC_HEAD(watching_self, tc)
    174  1.1  christos {
    175  1.1  christos 
    176  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    177  1.1  christos 	    "Checks that epoll disallows watching itself");
    178  1.1  christos }
    179  1.1  christos ATF_TC_BODY(watching_self, tc)
    180  1.1  christos {
    181  1.1  christos 	int epfd;
    182  1.1  christos 	struct epoll_event event;
    183  1.1  christos 
    184  1.1  christos 	RL(epfd = epoll_create1(0));
    185  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd, EPOLL_CTL_ADD, epfd, &event), -1,
    186  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    187  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
    188  1.1  christos }
    189  1.1  christos 
    190  1.1  christos ATF_TC(watch_loops);
    191  1.1  christos ATF_TC_HEAD(watch_loops, tc)
    192  1.1  christos {
    193  1.1  christos 
    194  1.1  christos 	atf_tc_set_md_var(tc, "descr", "Checks that epoll disallows loops");
    195  1.1  christos }
    196  1.1  christos ATF_TC_BODY(watch_loops, tc)
    197  1.1  christos {
    198  1.1  christos         int epfd1, epfd2;
    199  1.1  christos 	struct epoll_event event;
    200  1.1  christos 
    201  1.1  christos 	event.events = EPOLLIN;
    202  1.1  christos 	RL(epfd1 = epoll_create1(0));
    203  1.1  christos 	RL(epfd2 = epoll_create1(0));
    204  1.1  christos 	RL(epoll_ctl(epfd1, EPOLL_CTL_ADD, epfd2, &event));
    205  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(epfd2, EPOLL_CTL_ADD, epfd1, &event), -1,
    206  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    207  1.1  christos 	ATF_REQUIRE_ERRNO(ELOOP, true);
    208  1.1  christos }
    209  1.1  christos 
    210  1.1  christos ATF_TC(watch_depth);
    211  1.1  christos ATF_TC_HEAD(watch_depth, tc)
    212  1.1  christos {
    213  1.1  christos 
    214  1.1  christos 	atf_tc_set_md_var(tc, "descr",
    215  1.1  christos 	    "Checks that epoll fails when the watch depth exceeds 5");
    216  1.1  christos }
    217  1.1  christos ATF_TC_BODY(watch_depth, tc)
    218  1.1  christos {
    219  1.1  christos 	int epfd, tmp;
    220  1.1  christos 	struct epoll_event event;
    221  1.1  christos 
    222  1.1  christos 	event.events = EPOLLIN;
    223  1.1  christos 	RL(epfd = epoll_create1(0));
    224  1.1  christos 	for (size_t i = 0; i < 4; i++) {
    225  1.1  christos 		RL(tmp = epoll_create1(0));
    226  1.1  christos 		RL(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event));
    227  1.1  christos 		epfd = tmp;
    228  1.1  christos 	}
    229  1.1  christos 	RL(tmp = epoll_create1(0));
    230  1.1  christos 	ATF_REQUIRE_EQ_MSG(epoll_ctl(tmp, EPOLL_CTL_ADD, epfd, &event), -1,
    231  1.1  christos 	    "epoll_ctl succeeded unexpectedly");
    232  1.1  christos 	ATF_REQUIRE_ERRNO(EINVAL, true);
    233  1.1  christos }
    234  1.1  christos 
    235  1.1  christos ATF_TP_ADD_TCS(tp)
    236  1.1  christos {
    237  1.1  christos 	ATF_TP_ADD_TC(tp, create_size);
    238  1.2  christos 	ATF_TP_ADD_TC(tp, create_cloexec);
    239  1.1  christos 	ATF_TP_ADD_TC(tp, bad_epfd);
    240  1.1  christos 	ATF_TP_ADD_TC(tp, bad_fd);
    241  1.1  christos 	ATF_TP_ADD_TC(tp, not_added);
    242  1.1  christos 	ATF_TP_ADD_TC(tp, watching_self);
    243  1.1  christos 	ATF_TP_ADD_TC(tp, watch_loops);
    244  1.1  christos 	ATF_TP_ADD_TC(tp, watch_depth);
    245  1.1  christos 
    246  1.1  christos 	return atf_no_error();
    247  1.1  christos }
    248