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/socket.h>
32
33 #include <stdlib.h>
34
35 #include <errno.h>
36 #include <poll.h>
37 #include <pthread.h>
38 #include <sched.h>
39 #include <string.h>
40
41 #include "compat_stdbool.h"
42
43 #include "logmsg.h"
44 #include "network.h"
45
46 static struct pollfd *__fds = NULL;
47 static nfds_t __nfds = 0;
48
49 bool
sock_init(int * socks)50 sock_init(int *socks)
51 {
52 nfds_t i;
53
54 for (i = 0 ; socks[i] != -1 ; i++)
55 __nfds++;
56 if (__nfds == 0)
57 return (false);
58 if ((__fds = malloc(__nfds * sizeof(*__fds))) == NULL) {
59 logmsg_err("%s", strerror(errno));
60 return (false);
61 }
62 for (i = 0 ; i < __nfds ; i++) {
63 __fds[i].fd = socks[i];
64 __fds[i].events = POLLIN;
65 }
66
67 return (true);
68 }
69
70 void
sock_destroy(void)71 sock_destroy(void)
72 {
73 free(__fds);
74 }
75
76 bool
sock_select(void)77 sock_select(void)
78 {
79 nfds_t i;
80
81 for (i = 0 ; i < __nfds ; i++)
82 __fds[i].revents = 0;
83
84 if (poll(__fds, __nfds, 1000 /* 1sec */) == -1)
85 return (false);
86
87 return (true);
88 }
89
90 int
sock_accept(void)91 sock_accept(void)
92 {
93 nfds_t i;
94 int sock = -1;
95
96 for (i = 0 ; i < __nfds ; i++) {
97 if (!(__fds[i].revents & POLLIN))
98 continue;
99
100 if ((sock = accept(__fds[i].fd, NULL, NULL)) == -1) {
101 if (errno == EINTR) {
102 logmsg_intr();
103 return (-1);
104 }
105 if ((errno != EAGAIN) && (errno != EWOULDBLOCK)) {
106 logmsg_err("%s", strerror(errno));
107 return (-1);
108 }
109 continue;
110 }
111
112 break;
113 }
114
115 return (sock);
116 }
117
118 bool
sock_wait(int sock,int dir)119 sock_wait(int sock, int dir)
120 {
121 struct pollfd fds[1];
122 short events;
123 int rv;
124
125 if (dir == CVSYNC_SOCKDIR_OUT)
126 events = POLLOUT;
127 else /* dir == CVSYNC_SOCKDIR_IN */
128 events = POLLIN;
129
130 fds[0].fd = sock;
131 fds[0].events = events;
132 fds[0].revents = 0;
133
134 for (;;) {
135 if ((rv = poll(fds, 1, CVSYNC_TICKS)) == -1) {
136 if (errno != EINTR) {
137 logmsg_err("Socket Error: poll: %s",
138 strerror(errno));
139 return (false);
140 }
141 rv = 0;
142 }
143 if (rv == 0) {
144 if (sched_yield() == -1) {
145 logmsg_err("Socket Error: yield: %s",
146 strerror(errno));
147 }
148 continue;
149 }
150 if (!(fds[0].revents & events)) {
151 logmsg_err("Socket Error: poll");
152 return (false);
153 }
154 break;
155 }
156
157 return (true);
158 }
159