xref: /netbsd-src/lib/libc/sys/epoll.c (revision d11110f47395fad20b98cd0acd8c15e342942014)
1*d11110f4Schristos /*	$NetBSD: epoll.c,v 1.1 2023/07/28 18:19:00 christos Exp $	*/
2*d11110f4Schristos 
3*d11110f4Schristos /*-
4*d11110f4Schristos  * Copyright (c) 2023 The NetBSD Foundation, Inc.
5*d11110f4Schristos  * All rights reserved.
6*d11110f4Schristos  *
7*d11110f4Schristos  * Redistribution and use in source and binary forms, with or without
8*d11110f4Schristos  * modification, are permitted provided that the following conditions
9*d11110f4Schristos  * are met:
10*d11110f4Schristos  * 1. Redistributions of source code must retain the above copyright
11*d11110f4Schristos  *    notice, this list of conditions and the following disclaimer.
12*d11110f4Schristos  * 2. Redistributions in binary form must reproduce the above copyright
13*d11110f4Schristos  *    notice, this list of conditions and the following disclaimer in the
14*d11110f4Schristos  *    documentation and/or other materials provided with the distribution.
15*d11110f4Schristos  *
16*d11110f4Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17*d11110f4Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18*d11110f4Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19*d11110f4Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20*d11110f4Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*d11110f4Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*d11110f4Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*d11110f4Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*d11110f4Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*d11110f4Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*d11110f4Schristos  * POSSIBILITY OF SUCH DAMAGE.
27*d11110f4Schristos  */
28*d11110f4Schristos #include <sys/cdefs.h>
29*d11110f4Schristos __RCSID("$NetBSD: epoll.c,v 1.1 2023/07/28 18:19:00 christos Exp $");
30*d11110f4Schristos 
31*d11110f4Schristos #include <sys/epoll.h>
32*d11110f4Schristos #include <sys/sigtypes.h>
33*d11110f4Schristos #include <sys/time.h>
34*d11110f4Schristos 
35*d11110f4Schristos #include <errno.h>
36*d11110f4Schristos #include <stddef.h>
37*d11110f4Schristos 
38*d11110f4Schristos int
epoll_create(int size)39*d11110f4Schristos epoll_create(int size)
40*d11110f4Schristos {
41*d11110f4Schristos 	if (size <= 0) {
42*d11110f4Schristos 		errno = EINVAL;
43*d11110f4Schristos 		return -1;
44*d11110f4Schristos 	}
45*d11110f4Schristos 
46*d11110f4Schristos 	return epoll_create1(0);
47*d11110f4Schristos }
48*d11110f4Schristos 
49*d11110f4Schristos int
epoll_wait(int epfd,struct epoll_event * events,int maxevents,int timeout)50*d11110f4Schristos epoll_wait(int epfd, struct epoll_event *events, int maxevents, int timeout)
51*d11110f4Schristos {
52*d11110f4Schristos 	return epoll_pwait(epfd, events, maxevents, timeout, NULL);
53*d11110f4Schristos }
54*d11110f4Schristos 
55*d11110f4Schristos int
epoll_pwait(int epfd,struct epoll_event * events,int maxevents,int timeout,const sigset_t * sigmask)56*d11110f4Schristos epoll_pwait(int epfd, struct epoll_event *events, int maxevents, int timeout,
57*d11110f4Schristos     const sigset_t *sigmask)
58*d11110f4Schristos {
59*d11110f4Schristos 	struct timespec ts, *tsp;
60*d11110f4Schristos 
61*d11110f4Schristos 	if (timeout >= 0) {
62*d11110f4Schristos 		ts.tv_sec = timeout / 1000;
63*d11110f4Schristos 		ts.tv_nsec = (timeout % 1000) * 1000000;
64*d11110f4Schristos 		tsp = &ts;
65*d11110f4Schristos 	} else
66*d11110f4Schristos 		tsp = NULL;
67*d11110f4Schristos 
68*d11110f4Schristos 	return epoll_pwait2(epfd, events, maxevents, tsp, sigmask);
69*d11110f4Schristos }
70