Remove useless use of <assert.h>.
[sliver-openvswitch.git] / lib / stream-unix.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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.h"
19 #include <errno.h>
20 #include <inttypes.h>
21 #include <netdb.h>
22 #include <poll.h>
23 #include <sys/socket.h>
24 #include <sys/types.h>
25 #include <sys/un.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include "packets.h"
30 #include "poll-loop.h"
31 #include "socket-util.h"
32 #include "util.h"
33 #include "stream-provider.h"
34 #include "stream-fd.h"
35 #include "vlog.h"
36
37 VLOG_DEFINE_THIS_MODULE(stream_unix);
38
39 /* Active UNIX socket. */
40
41 static int
42 unix_open(const char *name, char *suffix, struct stream **streamp,
43           uint8_t dscp OVS_UNUSED)
44 {
45     const char *connect_path = suffix;
46     int fd;
47
48     fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path);
49     if (fd < 0) {
50         VLOG_DBG("%s: connection failed (%s)", connect_path, strerror(-fd));
51         return -fd;
52     }
53
54     return new_fd_stream(name, fd, check_connection_completion(fd), streamp);
55 }
56
57 const struct stream_class unix_stream_class = {
58     "unix",                     /* name */
59     false,                      /* needs_probes */
60     unix_open,                  /* open */
61     NULL,                       /* close */
62     NULL,                       /* connect */
63     NULL,                       /* recv */
64     NULL,                       /* send */
65     NULL,                       /* run */
66     NULL,                       /* run_wait */
67     NULL,                       /* wait */
68 };
69 \f
70 /* Passive UNIX socket. */
71
72 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
73                         struct stream **streamp);
74
75 static int
76 punix_open(const char *name OVS_UNUSED, char *suffix,
77            struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
78 {
79     int fd, error;
80
81     fd = make_unix_socket(SOCK_STREAM, true, suffix, NULL);
82     if (fd < 0) {
83         VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
84         return errno;
85     }
86
87     if (listen(fd, 10) < 0) {
88         error = errno;
89         VLOG_ERR("%s: listen: %s", name, strerror(error));
90         close(fd);
91         return error;
92     }
93
94     return new_fd_pstream(name, fd, punix_accept, NULL,
95                           xstrdup(suffix), pstreamp);
96 }
97
98 static int
99 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
100              struct stream **streamp)
101 {
102     const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
103     int name_len = get_unix_name_len(sa_len);
104     char name[128];
105
106     if (name_len > 0) {
107         snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
108     } else {
109         strcpy(name, "unix");
110     }
111     return new_fd_stream(name, fd, 0, streamp);
112 }
113
114 const struct pstream_class punix_pstream_class = {
115     "punix",
116     false,
117     punix_open,
118     NULL,
119     NULL,
120     NULL,
121     NULL,
122 };
123