add changelog for 1.13
[beanstalkd.git] / sunos.c
bloba0b10ddf8177f1ba1c3737229dbefdad53ef8433
1 #include <stdint.h>
2 #include <fcntl.h>
3 #include <stdlib.h>
4 #include <sys/types.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <poll.h>
8 #include <port.h>
9 #include "dat.h"
11 static int portfd;
13 int
14 sockinit(void)
16 portfd = port_create();
17 if (portfd == -1) {
18 twarn("port_create");
19 return -1;
21 return 0;
25 int
26 sockwant(Socket *s, int rw)
28 int events = 0;
30 if (rw) {
31 switch (rw) {
32 case 'r':
33 events |= POLLIN;
34 break;
35 case 'w':
36 events |= POLLOUT;
37 break;
41 events |= POLLPRI;
43 if (!s->added && !rw) {
44 return 0;
45 } else if (!s->added && rw) {
46 s->added = 1;
47 return port_associate(portfd, PORT_SOURCE_FD, s->fd, events, (void *)s);
48 } else if (!rw) {
49 return port_dissociate(portfd, PORT_SOURCE_FD, s->fd);
50 } else {
51 port_dissociate(portfd, PORT_SOURCE_FD, s->fd);
52 return port_associate(portfd, PORT_SOURCE_FD, s->fd, events, (void *)s);
57 int
58 socknext(Socket **s, int64 timeout)
60 int r;
61 uint_t n = 1;
62 struct port_event pe;
63 struct timespec ts;
65 ts.tv_sec = timeout / 1000000000;
66 ts.tv_nsec = timeout % 1000000000;
67 r = port_getn(portfd, &pe, 1, &n, &ts);
68 if (r == -1 && errno != ETIME && errno != EINTR) {
69 twarn("port_getn");
70 return -1;
73 if (r == 0) {
74 *s = pe.portev_user;
75 if (pe.portev_events & POLLHUP) {
76 return 'h';
77 } else if (pe.portev_events & POLLIN) {
78 if (sockwant(*s, 'r') == -1) {
79 return -1;
81 return 'r';
82 } else if (pe.portev_events & POLLOUT) {
83 if (sockwant(*s, 'w') == -1) {
84 return -1;
86 return 'w';
90 return 0;