xref: /libev/ev_port.c (revision 93823e6c)
1e3a38431SPaul Bohm /*
2e3a38431SPaul Bohm  * libev solaris event port backend
3e3a38431SPaul Bohm  *
4e3a38431SPaul Bohm  * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <[email protected]>
5e3a38431SPaul Bohm  * All rights reserved.
6e3a38431SPaul Bohm  *
7e3a38431SPaul Bohm  * Redistribution and use in source and binary forms, with or without modifica-
8e3a38431SPaul Bohm  * tion, are permitted provided that the following conditions are met:
9e3a38431SPaul Bohm  *
10e3a38431SPaul Bohm  *   1.  Redistributions of source code must retain the above copyright notice,
11e3a38431SPaul Bohm  *       this list of conditions and the following disclaimer.
12e3a38431SPaul Bohm  *
13e3a38431SPaul Bohm  *   2.  Redistributions in binary form must reproduce the above copyright
14e3a38431SPaul Bohm  *       notice, this list of conditions and the following disclaimer in the
15e3a38431SPaul Bohm  *       documentation and/or other materials provided with the distribution.
16e3a38431SPaul Bohm  *
17e3a38431SPaul Bohm  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18e3a38431SPaul Bohm  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
19e3a38431SPaul Bohm  * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
20e3a38431SPaul Bohm  * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
21e3a38431SPaul Bohm  * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22e3a38431SPaul Bohm  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23e3a38431SPaul Bohm  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24e3a38431SPaul Bohm  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
25e3a38431SPaul Bohm  * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
26e3a38431SPaul Bohm  * OF THE POSSIBILITY OF SUCH DAMAGE.
27e3a38431SPaul Bohm  *
28e3a38431SPaul Bohm  * Alternatively, the contents of this file may be used under the terms of
29e3a38431SPaul Bohm  * the GNU General Public License ("GPL") version 2 or any later version,
30e3a38431SPaul Bohm  * in which case the provisions of the GPL are applicable instead of
31e3a38431SPaul Bohm  * the above. If you wish to allow the use of your version of this file
32e3a38431SPaul Bohm  * only under the terms of the GPL and not to allow others to use your
33e3a38431SPaul Bohm  * version of this file under the BSD license, indicate your decision
34e3a38431SPaul Bohm  * by deleting the provisions above and replace them with the notice
35e3a38431SPaul Bohm  * and other provisions required by the GPL. If you do not delete the
36e3a38431SPaul Bohm  * provisions above, a recipient may use your version of this file under
37e3a38431SPaul Bohm  * either the BSD or the GPL.
38e3a38431SPaul Bohm  */
39e3a38431SPaul Bohm 
40e3a38431SPaul Bohm /* useful reading:
41e3a38431SPaul Bohm  *
42e3a38431SPaul Bohm  * http://bugs.opensolaris.org/view_bug.do?bug_id=6268715 (random results)
43e3a38431SPaul Bohm  * http://bugs.opensolaris.org/view_bug.do?bug_id=6455223 (just totally broken)
44e3a38431SPaul Bohm  * http://bugs.opensolaris.org/view_bug.do?bug_id=6873782 (manpage ETIME)
45e3a38431SPaul Bohm  * http://bugs.opensolaris.org/view_bug.do?bug_id=6874410 (implementation ETIME)
46e3a38431SPaul Bohm  * http://www.mail-archive.com/[email protected]/msg11898.html ETIME vs. nget
47e3a38431SPaul Bohm  * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/event_port.c (libc)
48e3a38431SPaul Bohm  * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/fs/portfs/port.c#1325 (kernel)
49e3a38431SPaul Bohm  */
50e3a38431SPaul Bohm 
51e3a38431SPaul Bohm #include <sys/types.h>
52e3a38431SPaul Bohm #include <sys/time.h>
53e3a38431SPaul Bohm #include <poll.h>
54e3a38431SPaul Bohm #include <port.h>
55e3a38431SPaul Bohm #include <string.h>
56e3a38431SPaul Bohm #include <errno.h>
57e3a38431SPaul Bohm 
58e3a38431SPaul Bohm void inline_speed
port_associate_and_check(EV_P_ int fd,int ev)59e3a38431SPaul Bohm port_associate_and_check (EV_P_ int fd, int ev)
60e3a38431SPaul Bohm {
61e3a38431SPaul Bohm   if (0 >
62e3a38431SPaul Bohm       port_associate (
63e3a38431SPaul Bohm          backend_fd, PORT_SOURCE_FD, fd,
64e3a38431SPaul Bohm          (ev & EV_READ ? POLLIN : 0)
65e3a38431SPaul Bohm          | (ev & EV_WRITE ? POLLOUT : 0),
66e3a38431SPaul Bohm          0
67e3a38431SPaul Bohm       )
68e3a38431SPaul Bohm   )
69e3a38431SPaul Bohm     {
70e3a38431SPaul Bohm       if (errno == EBADFD)
71e3a38431SPaul Bohm         fd_kill (EV_A_ fd);
72e3a38431SPaul Bohm       else
73e3a38431SPaul Bohm         ev_syserr ("(libev) port_associate");
74e3a38431SPaul Bohm     }
75e3a38431SPaul Bohm }
76e3a38431SPaul Bohm 
77e3a38431SPaul Bohm static void
port_modify(EV_P_ int fd,int oev,int nev)78e3a38431SPaul Bohm port_modify (EV_P_ int fd, int oev, int nev)
79e3a38431SPaul Bohm {
80e3a38431SPaul Bohm   /* we need to reassociate no matter what, as closes are
81e3a38431SPaul Bohm    * once more silently being discarded.
82e3a38431SPaul Bohm    */
83e3a38431SPaul Bohm   if (!nev)
84e3a38431SPaul Bohm     {
85e3a38431SPaul Bohm       if (oev)
86e3a38431SPaul Bohm         port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
87e3a38431SPaul Bohm     }
88e3a38431SPaul Bohm   else
89e3a38431SPaul Bohm     port_associate_and_check (EV_A_ fd, nev);
90e3a38431SPaul Bohm }
91e3a38431SPaul Bohm 
92e3a38431SPaul Bohm static void
port_poll(EV_P_ ev_tstamp timeout)93e3a38431SPaul Bohm port_poll (EV_P_ ev_tstamp timeout)
94e3a38431SPaul Bohm {
95e3a38431SPaul Bohm   int res, i;
96e3a38431SPaul Bohm   struct timespec ts;
97e3a38431SPaul Bohm   uint_t nget = 1;
98e3a38431SPaul Bohm 
99e3a38431SPaul Bohm   /* we initialise this to something we will skip in the loop, as */
100e3a38431SPaul Bohm   /* port_getn can return with nget unchanged, but no indication */
101e3a38431SPaul Bohm   /* whether it was the original value or has been updated :/ */
102e3a38431SPaul Bohm   port_events [0].portev_source = 0;
103e3a38431SPaul Bohm 
104e3a38431SPaul Bohm   EV_RELEASE_CB;
105e3a38431SPaul Bohm   EV_TS_SET (ts, timeout);
106e3a38431SPaul Bohm   res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
107e3a38431SPaul Bohm   EV_ACQUIRE_CB;
108e3a38431SPaul Bohm 
109e3a38431SPaul Bohm   /* port_getn may or may not set nget on error */
110e3a38431SPaul Bohm   /* so we rely on port_events [0].portev_source not being updated */
111e3a38431SPaul Bohm   if (res == -1 && errno != ETIME && errno != EINTR)
112e3a38431SPaul Bohm     ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)");
113e3a38431SPaul Bohm 
114e3a38431SPaul Bohm   for (i = 0; i < nget; ++i)
115e3a38431SPaul Bohm     {
116e3a38431SPaul Bohm       if (port_events [i].portev_source == PORT_SOURCE_FD)
117e3a38431SPaul Bohm         {
118e3a38431SPaul Bohm           int fd = port_events [i].portev_object;
119e3a38431SPaul Bohm 
120e3a38431SPaul Bohm           fd_event (
121e3a38431SPaul Bohm             EV_A_
122e3a38431SPaul Bohm             fd,
123e3a38431SPaul Bohm             (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
124e3a38431SPaul Bohm             | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
125e3a38431SPaul Bohm           );
126e3a38431SPaul Bohm 
127e3a38431SPaul Bohm           fd_change (EV_A_ fd, EV__IOFDSET);
128e3a38431SPaul Bohm         }
129e3a38431SPaul Bohm     }
130e3a38431SPaul Bohm 
131e3a38431SPaul Bohm   if (expect_false (nget == port_eventmax))
132e3a38431SPaul Bohm     {
133e3a38431SPaul Bohm       ev_free (port_events);
134e3a38431SPaul Bohm       port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1);
135e3a38431SPaul Bohm       port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
136e3a38431SPaul Bohm     }
137e3a38431SPaul Bohm }
138e3a38431SPaul Bohm 
139e3a38431SPaul Bohm int inline_size
port_init(EV_P_ int flags)140e3a38431SPaul Bohm port_init (EV_P_ int flags)
141e3a38431SPaul Bohm {
142e3a38431SPaul Bohm   /* Initialize the kernel queue */
143e3a38431SPaul Bohm   if ((backend_fd = port_create ()) < 0)
144e3a38431SPaul Bohm     return 0;
145e3a38431SPaul Bohm 
146e3a38431SPaul Bohm   assert (("libev: PORT_SOURCE_FD must not be zero", PORT_SOURCE_FD));
147e3a38431SPaul Bohm 
148e3a38431SPaul Bohm   fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
149e3a38431SPaul Bohm 
150*93823e6cSPaul Bohm   /* if my reading of the opensolaris kernel sources are correct, then
151*93823e6cSPaul Bohm    * opensolaris does something very stupid: it checks if the time has already
152*93823e6cSPaul Bohm    * elapsed and doesn't round up if that is the case,m otherwise it DOES round
153*93823e6cSPaul Bohm    * up. Since we can't know what the case is, we need to guess by using a
154*93823e6cSPaul Bohm    * "large enough" timeout. Normally, 1e-9 would be correct.
155*93823e6cSPaul Bohm    */
156*93823e6cSPaul Bohm   backend_mintime = 1e-3; /* needed to compensate for port_getn returning early */
157e3a38431SPaul Bohm   backend_modify  = port_modify;
158e3a38431SPaul Bohm   backend_poll    = port_poll;
159e3a38431SPaul Bohm 
160e3a38431SPaul Bohm   port_eventmax = 64; /* initial number of events receivable per poll */
161e3a38431SPaul Bohm   port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
162e3a38431SPaul Bohm 
163e3a38431SPaul Bohm   return EVBACKEND_PORT;
164e3a38431SPaul Bohm }
165e3a38431SPaul Bohm 
166e3a38431SPaul Bohm void inline_size
port_destroy(EV_P)167e3a38431SPaul Bohm port_destroy (EV_P)
168e3a38431SPaul Bohm {
169e3a38431SPaul Bohm   ev_free (port_events);
170e3a38431SPaul Bohm }
171e3a38431SPaul Bohm 
172e3a38431SPaul Bohm void inline_size
port_fork(EV_P)173e3a38431SPaul Bohm port_fork (EV_P)
174e3a38431SPaul Bohm {
175e3a38431SPaul Bohm   close (backend_fd);
176e3a38431SPaul Bohm 
177e3a38431SPaul Bohm   while ((backend_fd = port_create ()) < 0)
178e3a38431SPaul Bohm     ev_syserr ("(libev) port");
179e3a38431SPaul Bohm 
180e3a38431SPaul Bohm   fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
181e3a38431SPaul Bohm 
182e3a38431SPaul Bohm   /* re-register interest in fds */
183e3a38431SPaul Bohm   fd_rearm_all (EV_A);
184e3a38431SPaul Bohm }
185e3a38431SPaul Bohm 
186