Blame ev_port.c

Packit aac759
/*
Packit aac759
 * libev solaris event port backend
Packit aac759
 *
Packit aac759
 * Copyright (c) 2007,2008,2009,2010,2011 Marc Alexander Lehmann <libev@schmorp.de>
Packit aac759
 * All rights reserved.
Packit aac759
 *
Packit aac759
 * Redistribution and use in source and binary forms, with or without modifica-
Packit aac759
 * tion, are permitted provided that the following conditions are met:
Packit aac759
 *
Packit aac759
 *   1.  Redistributions of source code must retain the above copyright notice,
Packit aac759
 *       this list of conditions and the following disclaimer.
Packit aac759
 *
Packit aac759
 *   2.  Redistributions in binary form must reproduce the above copyright
Packit aac759
 *       notice, this list of conditions and the following disclaimer in the
Packit aac759
 *       documentation and/or other materials provided with the distribution.
Packit aac759
 *
Packit aac759
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
Packit aac759
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MER-
Packit aac759
 * CHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit aac759
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPE-
Packit aac759
 * CIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
Packit aac759
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
Packit aac759
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
Packit aac759
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTH-
Packit aac759
 * ERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
Packit aac759
 * OF THE POSSIBILITY OF SUCH DAMAGE.
Packit aac759
 *
Packit aac759
 * Alternatively, the contents of this file may be used under the terms of
Packit aac759
 * the GNU General Public License ("GPL") version 2 or any later version,
Packit aac759
 * in which case the provisions of the GPL are applicable instead of
Packit aac759
 * the above. If you wish to allow the use of your version of this file
Packit aac759
 * only under the terms of the GPL and not to allow others to use your
Packit aac759
 * version of this file under the BSD license, indicate your decision
Packit aac759
 * by deleting the provisions above and replace them with the notice
Packit aac759
 * and other provisions required by the GPL. If you do not delete the
Packit aac759
 * provisions above, a recipient may use your version of this file under
Packit aac759
 * either the BSD or the GPL.
Packit aac759
 */
Packit aac759
Packit aac759
/* useful reading:
Packit aac759
 *
Packit aac759
 * http://bugs.opensolaris.org/view_bug.do?bug_id=6268715 (random results)
Packit aac759
 * http://bugs.opensolaris.org/view_bug.do?bug_id=6455223 (just totally broken)
Packit aac759
 * http://bugs.opensolaris.org/view_bug.do?bug_id=6873782 (manpage ETIME)
Packit aac759
 * http://bugs.opensolaris.org/view_bug.do?bug_id=6874410 (implementation ETIME)
Packit aac759
 * http://www.mail-archive.com/networking-discuss@opensolaris.org/msg11898.html ETIME vs. nget
Packit aac759
 * http://src.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/lib/libc/port/gen/event_port.c (libc)
Packit aac759
 * http://cvs.opensolaris.org/source/xref/onnv/onnv-gate/usr/src/uts/common/fs/portfs/port.c#1325 (kernel)
Packit aac759
 */
Packit aac759
Packit aac759
#include <sys/types.h>
Packit aac759
#include <sys/time.h>
Packit aac759
#include <poll.h>
Packit aac759
#include <port.h>
Packit aac759
#include <string.h>
Packit aac759
#include <errno.h>
Packit aac759
Packit aac759
inline_speed
Packit aac759
void
Packit aac759
port_associate_and_check (EV_P_ int fd, int ev)
Packit aac759
{
Packit aac759
  if (0 >
Packit aac759
      port_associate (
Packit aac759
         backend_fd, PORT_SOURCE_FD, fd,
Packit aac759
         (ev & EV_READ ? POLLIN : 0)
Packit aac759
         | (ev & EV_WRITE ? POLLOUT : 0),
Packit aac759
         0
Packit aac759
      )
Packit aac759
  )
Packit aac759
    {
Packit aac759
      if (errno == EBADFD)
Packit aac759
        fd_kill (EV_A_ fd);
Packit aac759
      else
Packit aac759
        ev_syserr ("(libev) port_associate");
Packit aac759
    }
Packit aac759
}
Packit aac759
Packit aac759
static void
Packit aac759
port_modify (EV_P_ int fd, int oev, int nev)
Packit aac759
{
Packit aac759
  /* we need to reassociate no matter what, as closes are
Packit aac759
   * once more silently being discarded.
Packit aac759
   */
Packit aac759
  if (!nev)
Packit aac759
    {
Packit aac759
      if (oev)
Packit aac759
        port_dissociate (backend_fd, PORT_SOURCE_FD, fd);
Packit aac759
    }
Packit aac759
  else
Packit aac759
    port_associate_and_check (EV_A_ fd, nev);
Packit aac759
}
Packit aac759
Packit aac759
static void
Packit aac759
port_poll (EV_P_ ev_tstamp timeout)
Packit aac759
{
Packit aac759
  int res, i;
Packit aac759
  struct timespec ts;
Packit aac759
  uint_t nget = 1;
Packit aac759
Packit aac759
  /* we initialise this to something we will skip in the loop, as */
Packit aac759
  /* port_getn can return with nget unchanged, but no indication */
Packit aac759
  /* whether it was the original value or has been updated :/ */
Packit aac759
  port_events [0].portev_source = 0;
Packit aac759
Packit aac759
  EV_RELEASE_CB;
Packit aac759
  EV_TS_SET (ts, timeout);
Packit aac759
  res = port_getn (backend_fd, port_events, port_eventmax, &nget, &ts);
Packit aac759
  EV_ACQUIRE_CB;
Packit aac759
Packit aac759
  /* port_getn may or may not set nget on error */
Packit aac759
  /* so we rely on port_events [0].portev_source not being updated */
Packit aac759
  if (res == -1 && errno != ETIME && errno != EINTR)
Packit aac759
    ev_syserr ("(libev) port_getn (see http://bugs.opensolaris.org/view_bug.do?bug_id=6268715, try LIBEV_FLAGS=3 env variable)");
Packit aac759
Packit aac759
  for (i = 0; i < nget; ++i)
Packit aac759
    {
Packit aac759
      if (port_events [i].portev_source == PORT_SOURCE_FD)
Packit aac759
        {
Packit aac759
          int fd = port_events [i].portev_object;
Packit aac759
Packit aac759
          fd_event (
Packit aac759
            EV_A_
Packit aac759
            fd,
Packit aac759
            (port_events [i].portev_events & (POLLOUT | POLLERR | POLLHUP) ? EV_WRITE : 0)
Packit aac759
            | (port_events [i].portev_events & (POLLIN | POLLERR | POLLHUP) ? EV_READ : 0)
Packit aac759
          );
Packit aac759
Packit aac759
          fd_change (EV_A_ fd, EV__IOFDSET);
Packit aac759
        }
Packit aac759
    }
Packit aac759
Packit aac759
  if (expect_false (nget == port_eventmax))
Packit aac759
    {
Packit aac759
      ev_free (port_events);
Packit aac759
      port_eventmax = array_nextsize (sizeof (port_event_t), port_eventmax, port_eventmax + 1);
Packit aac759
      port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
Packit aac759
    }
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
int
Packit aac759
port_init (EV_P_ int flags)
Packit aac759
{
Packit aac759
  /* Initialize the kernel queue */
Packit aac759
  if ((backend_fd = port_create ()) < 0)
Packit aac759
    return 0;
Packit aac759
Packit aac759
  assert (("libev: PORT_SOURCE_FD must not be zero", PORT_SOURCE_FD));
Packit aac759
Packit aac759
  fcntl (backend_fd, F_SETFD, FD_CLOEXEC); /* not sure if necessary, hopefully doesn't hurt */
Packit aac759
Packit aac759
  /* if my reading of the opensolaris kernel sources are correct, then
Packit aac759
   * opensolaris does something very stupid: it checks if the time has already
Packit aac759
   * elapsed and doesn't round up if that is the case,m otherwise it DOES round
Packit aac759
   * up. Since we can't know what the case is, we need to guess by using a
Packit aac759
   * "large enough" timeout. Normally, 1e-9 would be correct.
Packit aac759
   */
Packit aac759
  backend_mintime = 1e-3; /* needed to compensate for port_getn returning early */
Packit aac759
  backend_modify  = port_modify;
Packit aac759
  backend_poll    = port_poll;
Packit aac759
Packit aac759
  port_eventmax = 64; /* initial number of events receivable per poll */
Packit aac759
  port_events = (port_event_t *)ev_malloc (sizeof (port_event_t) * port_eventmax);
Packit aac759
Packit aac759
  return EVBACKEND_PORT;
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
port_destroy (EV_P)
Packit aac759
{
Packit aac759
  ev_free (port_events);
Packit aac759
}
Packit aac759
Packit aac759
inline_size
Packit aac759
void
Packit aac759
port_fork (EV_P)
Packit aac759
{
Packit aac759
  close (backend_fd);
Packit aac759
Packit aac759
  while ((backend_fd = port_create ()) < 0)
Packit aac759
    ev_syserr ("(libev) port");
Packit aac759
Packit aac759
  fcntl (backend_fd, F_SETFD, FD_CLOEXEC);
Packit aac759
Packit aac759
  /* re-register interest in fds */
Packit aac759
  fd_rearm_all (EV_A);
Packit aac759
}
Packit aac759