]> git.proxmox.com Git - mirror_ovs.git/blob - lib/stream-fd.c
stream-unix: Do not bind a name for client sockets.
[mirror_ovs.git] / lib / stream-fd.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2012 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "stream-fd.h"
19 #include <assert.h>
20 #include <errno.h>
21 #include <poll.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <sys/socket.h>
25 #include <sys/types.h>
26 #include <unistd.h>
27 #include "fatal-signal.h"
28 #include "leak-checker.h"
29 #include "poll-loop.h"
30 #include "socket-util.h"
31 #include "stress.h"
32 #include "util.h"
33 #include "stream-provider.h"
34 #include "stream.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(stream_fd);
38
39 /* Active file descriptor stream. */
40
41 struct stream_fd
42 {
43 struct stream stream;
44 int fd;
45 };
46
47 static const struct stream_class stream_fd_class;
48
49 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
50
51 static void maybe_unlink_and_free(char *path);
52
53 /* Creates a new stream named 'name' that will send and receive data on 'fd'
54 * and stores a pointer to the stream in '*streamp'. Initial connection status
55 * 'connect_status' is interpreted as described for stream_init().
56 *
57 * Returns 0 if successful, otherwise a positive errno value. (The current
58 * implementation never fails.) */
59 int
60 new_fd_stream(const char *name, int fd, int connect_status,
61 struct stream **streamp)
62 {
63 struct stream_fd *s;
64
65 s = xmalloc(sizeof *s);
66 stream_init(&s->stream, &stream_fd_class, connect_status, name);
67 s->fd = fd;
68 *streamp = &s->stream;
69 return 0;
70 }
71
72 static struct stream_fd *
73 stream_fd_cast(struct stream *stream)
74 {
75 stream_assert_class(stream, &stream_fd_class);
76 return CONTAINER_OF(stream, struct stream_fd, stream);
77 }
78
79 static void
80 fd_close(struct stream *stream)
81 {
82 struct stream_fd *s = stream_fd_cast(stream);
83 close(s->fd);
84 free(s);
85 }
86
87 static int
88 fd_connect(struct stream *stream)
89 {
90 struct stream_fd *s = stream_fd_cast(stream);
91 return check_connection_completion(s->fd);
92 }
93
94 STRESS_OPTION(
95 stream_flaky_recv, "simulate failure of fd stream recvs",
96 100, 0, -1, 0);
97
98 static ssize_t
99 fd_recv(struct stream *stream, void *buffer, size_t n)
100 {
101 struct stream_fd *s = stream_fd_cast(stream);
102 ssize_t retval;
103
104 if (STRESS(stream_flaky_recv)) {
105 return -EIO;
106 }
107
108 retval = read(s->fd, buffer, n);
109 return retval >= 0 ? retval : -errno;
110 }
111
112 STRESS_OPTION(
113 stream_flaky_send, "simulate failure of fd stream sends",
114 100, 0, -1, 0);
115
116 static ssize_t
117 fd_send(struct stream *stream, const void *buffer, size_t n)
118 {
119 struct stream_fd *s = stream_fd_cast(stream);
120 ssize_t retval;
121
122 if (STRESS(stream_flaky_send)) {
123 return -EIO;
124 }
125
126 retval = write(s->fd, buffer, n);
127 return (retval > 0 ? retval
128 : retval == 0 ? -EAGAIN
129 : -errno);
130 }
131
132 static void
133 fd_wait(struct stream *stream, enum stream_wait_type wait)
134 {
135 struct stream_fd *s = stream_fd_cast(stream);
136 switch (wait) {
137 case STREAM_CONNECT:
138 case STREAM_SEND:
139 poll_fd_wait(s->fd, POLLOUT);
140 break;
141
142 case STREAM_RECV:
143 poll_fd_wait(s->fd, POLLIN);
144 break;
145
146 default:
147 NOT_REACHED();
148 }
149 }
150
151 static const struct stream_class stream_fd_class = {
152 "fd", /* name */
153 NULL, /* open */
154 fd_close, /* close */
155 fd_connect, /* connect */
156 fd_recv, /* recv */
157 fd_send, /* send */
158 NULL, /* run */
159 NULL, /* run_wait */
160 fd_wait, /* wait */
161 };
162 \f
163 /* Passive file descriptor stream. */
164
165 struct fd_pstream
166 {
167 struct pstream pstream;
168 int fd;
169 int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
170 struct stream **);
171 char *unlink_path;
172 };
173
174 static struct pstream_class fd_pstream_class;
175
176 static struct fd_pstream *
177 fd_pstream_cast(struct pstream *pstream)
178 {
179 pstream_assert_class(pstream, &fd_pstream_class);
180 return CONTAINER_OF(pstream, struct fd_pstream, pstream);
181 }
182
183 /* Creates a new pstream named 'name' that will accept new socket connections
184 * on 'fd' and stores a pointer to the stream in '*pstreamp'.
185 *
186 * When a connection has been accepted, 'accept_cb' will be called with the new
187 * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
188 * accept_cb must return 0 if the connection is successful, in which case it
189 * must initialize '*streamp' to the new stream, or a positive errno value on
190 * error. In either case accept_cb takes ownership of the 'fd' passed in.
191 *
192 * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
193 * to fatal_signal_unlink_file_now() and freed with free().
194 *
195 * Returns 0 if successful, otherwise a positive errno value. (The current
196 * implementation never fails.) */
197 int
198 new_fd_pstream(const char *name, int fd,
199 int (*accept_cb)(int fd, const struct sockaddr *sa,
200 size_t sa_len, struct stream **streamp),
201 char *unlink_path, struct pstream **pstreamp)
202 {
203 struct fd_pstream *ps = xmalloc(sizeof *ps);
204 pstream_init(&ps->pstream, &fd_pstream_class, name);
205 ps->fd = fd;
206 ps->accept_cb = accept_cb;
207 ps->unlink_path = unlink_path;
208 *pstreamp = &ps->pstream;
209 return 0;
210 }
211
212 static void
213 pfd_close(struct pstream *pstream)
214 {
215 struct fd_pstream *ps = fd_pstream_cast(pstream);
216 close(ps->fd);
217 maybe_unlink_and_free(ps->unlink_path);
218 free(ps);
219 }
220
221 static int
222 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
223 {
224 struct fd_pstream *ps = fd_pstream_cast(pstream);
225 struct sockaddr_storage ss;
226 socklen_t ss_len = sizeof ss;
227 int new_fd;
228 int retval;
229
230 new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
231 if (new_fd < 0) {
232 retval = errno;
233 if (retval != EAGAIN) {
234 VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
235 }
236 return retval;
237 }
238
239 retval = set_nonblocking(new_fd);
240 if (retval) {
241 close(new_fd);
242 return retval;
243 }
244
245 return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
246 new_streamp);
247 }
248
249 static void
250 pfd_wait(struct pstream *pstream)
251 {
252 struct fd_pstream *ps = fd_pstream_cast(pstream);
253 poll_fd_wait(ps->fd, POLLIN);
254 }
255
256 static struct pstream_class fd_pstream_class = {
257 "pstream",
258 NULL,
259 pfd_close,
260 pfd_accept,
261 pfd_wait
262 };
263 \f
264 /* Helper functions. */
265 static void
266 maybe_unlink_and_free(char *path)
267 {
268 if (path) {
269 fatal_signal_unlink_file_now(path);
270 free(path);
271 }
272 }