Merge "next" branch into "master".
[cascardo/ovs.git] / lib / stream-fd.c
1 /*
2  * Copyright (c) 2008, 2009 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/types.h>
25 #include <unistd.h>
26 #include "fatal-signal.h"
27 #include "leak-checker.h"
28 #include "poll-loop.h"
29 #include "socket-util.h"
30 #include "util.h"
31 #include "stream-provider.h"
32 #include "stream.h"
33
34 #include "vlog.h"
35 #define THIS_MODULE VLM_stream_fd
36
37 /* Active file descriptor stream. */
38
39 struct stream_fd
40 {
41     struct stream stream;
42     int fd;
43     char *unlink_path;
44 };
45
46 static struct stream_class stream_fd_class;
47
48 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(10, 25);
49
50 static void maybe_unlink_and_free(char *path);
51
52 /* Creates a new stream named 'name' that will send and receive data on 'fd'
53  * and stores a pointer to the stream in '*streamp'.  Initial connection status
54  * 'connect_status' is interpreted as described for stream_init().
55  *
56  * When '*streamp' is closed, then 'unlink_path' (if nonnull) will be passed to
57  * fatal_signal_unlink_file_now() and then freed with free().
58  *
59  * Returns 0 if successful, otherwise a positive errno value.  (The current
60  * implementation never fails.) */
61 int
62 new_fd_stream(const char *name, int fd, int connect_status,
63               char *unlink_path, struct stream **streamp)
64 {
65     struct stream_fd *s;
66
67     s = xmalloc(sizeof *s);
68     stream_init(&s->stream, &stream_fd_class, connect_status, name);
69     s->fd = fd;
70     s->unlink_path = unlink_path;
71     *streamp = &s->stream;
72     return 0;
73 }
74
75 static struct stream_fd *
76 stream_fd_cast(struct stream *stream)
77 {
78     stream_assert_class(stream, &stream_fd_class);
79     return CONTAINER_OF(stream, struct stream_fd, stream);
80 }
81
82 static void
83 fd_close(struct stream *stream)
84 {
85     struct stream_fd *s = stream_fd_cast(stream);
86     close(s->fd);
87     maybe_unlink_and_free(s->unlink_path);
88     free(s);
89 }
90
91 static int
92 fd_connect(struct stream *stream)
93 {
94     struct stream_fd *s = stream_fd_cast(stream);
95     return check_connection_completion(s->fd);
96 }
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 = read(s->fd, buffer, n);
103     return retval >= 0 ? retval : -errno;
104 }
105
106 static ssize_t
107 fd_send(struct stream *stream, const void *buffer, size_t n)
108 {
109     struct stream_fd *s = stream_fd_cast(stream);
110     ssize_t retval = write(s->fd, buffer, n);
111     return (retval > 0 ? retval
112             : retval == 0 ? -EAGAIN
113             : -errno);
114 }
115
116 static void
117 fd_wait(struct stream *stream, enum stream_wait_type wait)
118 {
119     struct stream_fd *s = stream_fd_cast(stream);
120     switch (wait) {
121     case STREAM_CONNECT:
122     case STREAM_SEND:
123         poll_fd_wait(s->fd, POLLOUT);
124         break;
125
126     case STREAM_RECV:
127         poll_fd_wait(s->fd, POLLIN);
128         break;
129
130     default:
131         NOT_REACHED();
132     }
133 }
134
135 static struct stream_class stream_fd_class = {
136     "fd",                       /* name */
137     NULL,                       /* open */
138     fd_close,                   /* close */
139     fd_connect,                 /* connect */
140     fd_recv,                    /* recv */
141     fd_send,                    /* send */
142     NULL,                       /* run */
143     NULL,                       /* run_wait */
144     fd_wait,                    /* wait */
145 };
146 \f
147 /* Passive file descriptor stream. */
148
149 struct fd_pstream
150 {
151     struct pstream pstream;
152     int fd;
153     int (*accept_cb)(int fd, const struct sockaddr *, size_t sa_len,
154                      struct stream **);
155     char *unlink_path;
156 };
157
158 static struct pstream_class fd_pstream_class;
159
160 static struct fd_pstream *
161 fd_pstream_cast(struct pstream *pstream)
162 {
163     pstream_assert_class(pstream, &fd_pstream_class);
164     return CONTAINER_OF(pstream, struct fd_pstream, pstream);
165 }
166
167 /* Creates a new pstream named 'name' that will accept new socket connections
168  * on 'fd' and stores a pointer to the stream in '*pstreamp'.
169  *
170  * When a connection has been accepted, 'accept_cb' will be called with the new
171  * socket fd 'fd' and the remote address of the connection 'sa' and 'sa_len'.
172  * accept_cb must return 0 if the connection is successful, in which case it
173  * must initialize '*streamp' to the new stream, or a positive errno value on
174  * error.  In either case accept_cb takes ownership of the 'fd' passed in.
175  *
176  * When '*pstreamp' is closed, then 'unlink_path' (if nonnull) will be passed
177  * to fatal_signal_unlink_file_now() and freed with free().
178  *
179  * Returns 0 if successful, otherwise a positive errno value.  (The current
180  * implementation never fails.) */
181 int
182 new_fd_pstream(const char *name, int fd,
183                int (*accept_cb)(int fd, const struct sockaddr *sa,
184                                 size_t sa_len, struct stream **streamp),
185                char *unlink_path, struct pstream **pstreamp)
186 {
187     struct fd_pstream *ps = xmalloc(sizeof *ps);
188     pstream_init(&ps->pstream, &fd_pstream_class, name);
189     ps->fd = fd;
190     ps->accept_cb = accept_cb;
191     ps->unlink_path = unlink_path;
192     *pstreamp = &ps->pstream;
193     return 0;
194 }
195
196 static void
197 pfd_close(struct pstream *pstream)
198 {
199     struct fd_pstream *ps = fd_pstream_cast(pstream);
200     close(ps->fd);
201     maybe_unlink_and_free(ps->unlink_path);
202     free(ps);
203 }
204
205 static int
206 pfd_accept(struct pstream *pstream, struct stream **new_streamp)
207 {
208     struct fd_pstream *ps = fd_pstream_cast(pstream);
209     struct sockaddr_storage ss;
210     socklen_t ss_len = sizeof ss;
211     int new_fd;
212     int retval;
213
214     new_fd = accept(ps->fd, (struct sockaddr *) &ss, &ss_len);
215     if (new_fd < 0) {
216         int retval = errno;
217         if (retval != EAGAIN) {
218             VLOG_DBG_RL(&rl, "accept: %s", strerror(retval));
219         }
220         return retval;
221     }
222
223     retval = set_nonblocking(new_fd);
224     if (retval) {
225         close(new_fd);
226         return retval;
227     }
228
229     return ps->accept_cb(new_fd, (const struct sockaddr *) &ss, ss_len,
230                          new_streamp);
231 }
232
233 static void
234 pfd_wait(struct pstream *pstream)
235 {
236     struct fd_pstream *ps = fd_pstream_cast(pstream);
237     poll_fd_wait(ps->fd, POLLIN);
238 }
239
240 static struct pstream_class fd_pstream_class = {
241     "pstream",
242     NULL,
243     pfd_close,
244     pfd_accept,
245     pfd_wait
246 };
247 \f
248 /* Helper functions. */
249 static void
250 maybe_unlink_and_free(char *path)
251 {
252     if (path) {
253         fatal_signal_unlink_file_now(path);
254         free(path);
255     }
256 }