2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <sys/socket.h>
24 #include <sys/types.h>
30 #include "poll-loop.h"
31 #include "socket-util.h"
34 #include "stream-provider.h"
35 #include "stream-fd.h"
38 VLOG_DEFINE_THIS_MODULE(stream_unix);
40 /* Active UNIX socket. */
43 unix_open(const char *name, char *suffix, struct stream **streamp,
44 uint8_t dscp OVS_UNUSED)
49 connect_path = abs_file_name(ovs_rundir(), suffix);
50 fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path);
53 VLOG_DBG("%s: connection failed (%s)",
54 connect_path, ovs_strerror(-fd));
60 return new_fd_stream(name, fd, check_connection_completion(fd),
64 const struct stream_class unix_stream_class = {
66 false, /* needs_probes */
77 /* Passive UNIX socket. */
79 static int punix_accept(int fd, const struct sockaddr_storage *ss,
80 size_t ss_len, struct stream **streamp);
83 punix_open(const char *name OVS_UNUSED, char *suffix,
84 struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
89 bind_path = abs_file_name(ovs_rundir(), suffix);
90 fd = make_unix_socket(SOCK_STREAM, true, bind_path, NULL);
92 VLOG_ERR("%s: binding failed: %s", bind_path, ovs_strerror(errno));
97 if (listen(fd, 64) < 0) {
99 VLOG_ERR("%s: listen: %s", name, ovs_strerror(error));
105 return new_fd_pstream(name, fd, punix_accept, NULL, bind_path, pstreamp);
109 punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
110 struct stream **streamp)
112 const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
113 int name_len = get_unix_name_len(ss_len);
117 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
119 strcpy(name, "unix");
121 return new_fd_stream(name, fd, 0, AF_UNIX, streamp);
124 const struct pstream_class punix_pstream_class = {