1 /*-
2 * Copyright (c) 2003-2005 MAEKAWA Masahide <maekawa@cvsync.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the author nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/types.h>
31 #include <sys/event.h>
32 #include <sys/socket.h>
33 #include <sys/time.h>
34
35 #include <stdlib.h>
36
37 #include <errno.h>
38 #include <string.h>
39
40 #include "compat_stdbool.h"
41
42 #include "logmsg.h"
43 #include "network.h"
44
45 static struct kevent *__changelist;
46 static size_t __nchanges = 0;
47 static int __kq, __nevents;
48
49 bool
sock_init(int * socks)50 sock_init(int *socks)
51 {
52 size_t i;
53
54 for (i = 0 ; socks[i] != -1 ; i++)
55 __nchanges++;
56 if (__nchanges == 0)
57 return (false);
58 __changelist = malloc(__nchanges * sizeof(*__changelist));
59 if (__changelist == NULL) {
60 logmsg_err("%s", strerror(errno));
61 return (false);
62 }
63 for (i = 0 ; i < __nchanges ; i++) {
64 EV_SET(&__changelist[i], socks[i], EVFILT_READ, EV_ADD,
65 0, 0, NULL);
66 }
67 if ((__kq = kqueue()) == -1) {
68 logmsg_err("Socket Error: kqueue");
69 free(__changelist);
70 return (false);
71 }
72 if (kevent(__kq, __changelist, __nchanges, NULL, 0, NULL) == -1) {
73 logmsg_err("Socket Error: kqueue: %s", strerror(errno));
74 (void)close(__kq);
75 free(__changelist);
76 return (false);
77 }
78
79 return (true);
80 }
81
82 void
sock_destroy(void)83 sock_destroy(void)
84 {
85 free(__changelist);
86 if (close(__kq) == -1)
87 logmsg_err("Socket Error: kqueue: %s", strerror(errno));
88 }
89
90 bool
sock_select(void)91 sock_select(void)
92 {
93 struct timespec ts;
94
95 ts.tv_sec = 1;
96 ts.tv_nsec = 0;
97
98 if ((__nevents = kevent(__kq, NULL, 0, __changelist, __nchanges,
99 &ts)) == -1) {
100 logmsg_err("Socket Error: kqueue: %s", strerror(errno));
101 return (false);
102 }
103
104 return (true);
105 }
106
107 int
sock_accept(void)108 sock_accept(void)
109 {
110 int sock = -1, i;
111
112 for (i = 0 ; i < __nevents ; i++) {
113 if ((sock = accept((int)__changelist[i].ident, NULL,
114 NULL)) == -1) {
115 if (errno == EINTR) {
116 logmsg_intr();
117 return (-1);
118 }
119 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
120 logmsg_err("%s", strerror(errno));
121 return (-1);
122 }
123 continue;
124 }
125
126 break;
127 }
128
129 return (sock);
130 }
131