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