2 * Copyright (c) 2008, 2009 Nicira Networks.
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.
24 #include <sys/types.h>
30 #include "poll-loop.h"
31 #include "socket-util.h"
33 #include "stream-provider.h"
34 #include "stream-fd.h"
37 #define THIS_MODULE VLM_stream_unix
39 /* Active UNIX socket. */
41 /* Number of unix sockets created so far, to ensure binding path uniqueness. */
42 static int n_unix_sockets;
45 unix_open(const char *name, char *suffix, struct stream **streamp)
47 const char *connect_path = suffix;
51 bind_path = xasprintf("/tmp/stream-unix.%ld.%d",
52 (long int) getpid(), n_unix_sockets++);
53 fd = make_unix_socket(SOCK_STREAM, true, false, bind_path, connect_path);
55 VLOG_ERR("%s: connection to %s failed: %s",
56 bind_path, connect_path, strerror(-fd));
61 return new_fd_stream(name, fd, check_connection_completion(fd),
65 struct stream_class unix_stream_class = {
75 /* Passive UNIX socket. */
77 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
78 struct stream **streamp);
81 punix_open(const char *name UNUSED, char *suffix, struct pstream **pstreamp)
85 fd = make_unix_socket(SOCK_STREAM, true, true, suffix, NULL);
87 VLOG_ERR("%s: binding failed: %s", suffix, strerror(errno));
91 if (listen(fd, 10) < 0) {
93 VLOG_ERR("%s: listen: %s", name, strerror(error));
98 return new_fd_pstream("punix", fd, punix_accept,
99 xstrdup(suffix), pstreamp);
103 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
104 struct stream **streamp)
106 const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
107 int name_len = get_unix_name_len(sa_len);
111 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
113 strcpy(name, "unix");
115 return new_fd_stream(name, fd, 0, NULL, streamp);
118 struct pstream_class punix_pstream_class = {