2016-03-19 15:14:35 +00:00
|
|
|
#include "first.h"
|
|
|
|
|
2017-11-01 04:16:38 +00:00
|
|
|
#include "fdevent_impl.h"
|
2011-06-13 17:34:57 +00:00
|
|
|
#include "fdevent.h"
|
|
|
|
#include "buffer.h"
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
2017-11-01 04:16:38 +00:00
|
|
|
#ifdef FDEVENT_USE_SOLARIS_PORT
|
2011-06-13 17:34:57 +00:00
|
|
|
|
2018-01-23 00:54:15 +00:00
|
|
|
#include <sys/poll.h>
|
2011-06-13 17:34:57 +00:00
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
static int fdevent_solaris_port_event_del(fdevents *ev, fdnode *fdn) {
|
|
|
|
return port_dissociate(ev->port_fd, PORT_SOURCE_FD, fdn->fd);
|
2011-06-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
static int fdevent_solaris_port_event_set(fdevents *ev, fdnode *fdn, int events) {
|
2019-05-30 04:19:06 +00:00
|
|
|
int fd = fdn->fde_ndx = fdn->fd;
|
2019-02-23 06:35:58 +00:00
|
|
|
intptr_t ud = events & (POLLIN|POLLOUT);
|
|
|
|
return port_associate(ev->port_fd,PORT_SOURCE_FD,fd,(int)ud,(void*)ud);
|
2011-06-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
__attribute_cold__
|
2011-06-13 17:34:57 +00:00
|
|
|
static void fdevent_solaris_port_free(fdevents *ev) {
|
|
|
|
close(ev->port_fd);
|
|
|
|
free(ev->port_events);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* if there is any error it will return the return values of port_getn, otherwise it will return number of events **/
|
|
|
|
static int fdevent_solaris_port_poll(fdevents *ev, int timeout_ms) {
|
2019-02-23 06:35:58 +00:00
|
|
|
const int pfd = ev->port_fd;
|
2011-06-13 17:34:57 +00:00
|
|
|
int ret;
|
|
|
|
unsigned int available_events, wait_for_events = 0;
|
|
|
|
|
|
|
|
struct timespec timeout;
|
|
|
|
|
|
|
|
timeout.tv_sec = timeout_ms/1000L;
|
|
|
|
timeout.tv_nsec = (timeout_ms % 1000L) * 1000000L;
|
|
|
|
|
|
|
|
/* get the number of file descriptors with events */
|
2019-02-23 06:35:58 +00:00
|
|
|
if ((ret = port_getn(pfd, ev->port_events, 0, &wait_for_events, &timeout)) < 0) return ret;
|
2011-06-13 17:34:57 +00:00
|
|
|
|
|
|
|
/* wait for at least one event */
|
|
|
|
if (0 == wait_for_events) wait_for_events = 1;
|
|
|
|
|
|
|
|
available_events = wait_for_events;
|
|
|
|
|
|
|
|
/* get the events of the file descriptors */
|
2019-02-23 06:35:58 +00:00
|
|
|
if ((ret = port_getn(pfd, ev->port_events, ev->maxfds, &available_events, &timeout)) < 0) {
|
2011-06-13 17:34:57 +00:00
|
|
|
/* if errno == ETIME and available_event == wait_for_events we didn't get any events */
|
|
|
|
/* for other errors we didn't get any events either */
|
|
|
|
if (!(errno == ETIME && wait_for_events != available_events)) return ret;
|
|
|
|
}
|
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
for (int i = 0; i < (int)available_events; ++i) {
|
|
|
|
int fd = (int)ev->port_events[i].portev_object;
|
|
|
|
fdnode * const fdn = ev->fdarray[fd];
|
|
|
|
const intptr_t ud = (intptr_t)ev->port_events[i].portev_user;
|
2019-02-17 23:35:05 +00:00
|
|
|
int revents = ev->port_events[i].portev_events;
|
2019-02-14 02:34:59 +00:00
|
|
|
if (0 == ((uintptr_t)fdn & 0x3)) {
|
2019-02-23 06:35:58 +00:00
|
|
|
if (port_associate(pfd,PORT_SOURCE_FD,fd,(int)ud,(void*)ud) < 0) {
|
2019-12-25 04:15:02 +00:00
|
|
|
log_error(ev->errh,__FILE__,__LINE__,"port_associate failed");
|
2019-02-23 06:35:58 +00:00
|
|
|
}
|
2019-12-21 03:03:51 +00:00
|
|
|
(*fdn->handler)(fdn->ctx, revents);
|
2019-02-14 02:34:59 +00:00
|
|
|
}
|
2019-02-23 06:35:58 +00:00
|
|
|
else {
|
|
|
|
fdn->fde_ndx = -1;
|
|
|
|
}
|
2019-02-14 02:34:59 +00:00
|
|
|
}
|
|
|
|
return available_events;
|
2011-06-13 17:34:57 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
__attribute_cold__
|
2011-06-13 17:34:57 +00:00
|
|
|
int fdevent_solaris_port_init(fdevents *ev) {
|
2019-02-17 23:35:05 +00:00
|
|
|
force_assert(POLLIN == FDEVENT_IN);
|
|
|
|
force_assert(POLLPRI == FDEVENT_PRI);
|
|
|
|
force_assert(POLLOUT == FDEVENT_OUT);
|
|
|
|
force_assert(POLLERR == FDEVENT_ERR);
|
|
|
|
force_assert(POLLHUP == FDEVENT_HUP);
|
|
|
|
force_assert(POLLNVAL == FDEVENT_NVAL);
|
2019-10-24 01:26:35 +00:00
|
|
|
#ifdef POLLRDHUP
|
2019-02-17 23:35:05 +00:00
|
|
|
force_assert(POLLRDHUP == FDEVENT_RDHUP);
|
2019-10-24 01:26:35 +00:00
|
|
|
#endif
|
2011-06-13 17:34:57 +00:00
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
ev->type = FDEVENT_HANDLER_SOLARIS_PORT;
|
|
|
|
ev->event_set = fdevent_solaris_port_event_set;
|
|
|
|
ev->event_del = fdevent_solaris_port_event_del;
|
|
|
|
ev->poll = fdevent_solaris_port_poll;
|
|
|
|
ev->free = fdevent_solaris_port_free;
|
2011-06-13 17:34:57 +00:00
|
|
|
ev->port_events = malloc(ev->maxfds * sizeof(*ev->port_events));
|
2016-01-30 13:59:07 +00:00
|
|
|
force_assert(NULL != ev->port_events);
|
2011-06-13 17:34:57 +00:00
|
|
|
|
2019-02-23 06:35:58 +00:00
|
|
|
if ((ev->port_fd = port_create()) < 0) return -1;
|
|
|
|
|
2011-06-13 17:34:57 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|