]>
Commit | Line | Data |
---|---|---|
c34b65c7 | 1 | /* |
c2e3cbaf | 2 | * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc. |
c34b65c7 BP |
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" | |
c34b65c7 BP |
19 | #include <errno.h> |
20 | #include <inttypes.h> | |
21 | #include <netdb.h> | |
22 | #include <poll.h> | |
3762274e | 23 | #include <sys/socket.h> |
c34b65c7 BP |
24 | #include <sys/types.h> |
25 | #include <sys/un.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <unistd.h> | |
14347f3e | 29 | #include "ovs-atomic.h" |
c34b65c7 | 30 | #include "packets.h" |
fd016ae3 | 31 | #include "openvswitch/poll-loop.h" |
c34b65c7 | 32 | #include "socket-util.h" |
2c487bc8 | 33 | #include "dirs.h" |
c34b65c7 BP |
34 | #include "util.h" |
35 | #include "stream-provider.h" | |
36 | #include "stream-fd.h" | |
e6211adc | 37 | #include "openvswitch/vlog.h" |
5136ce49 | 38 | |
d98e6007 | 39 | VLOG_DEFINE_THIS_MODULE(stream_unix); |
c34b65c7 BP |
40 | |
41 | /* Active UNIX socket. */ | |
42 | ||
c34b65c7 | 43 | static int |
f125905c MM |
44 | unix_open(const char *name, char *suffix, struct stream **streamp, |
45 | uint8_t dscp OVS_UNUSED) | |
c34b65c7 | 46 | { |
2c487bc8 | 47 | char *connect_path; |
c34b65c7 BP |
48 | int fd; |
49 | ||
2c487bc8 | 50 | connect_path = abs_file_name(ovs_rundir(), suffix); |
5ca92d1d | 51 | fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path); |
2c487bc8 | 52 | |
c34b65c7 | 53 | if (fd < 0) { |
10a89ef0 BP |
54 | VLOG_DBG("%s: connection failed (%s)", |
55 | connect_path, ovs_strerror(-fd)); | |
2c487bc8 | 56 | free(connect_path); |
c34b65c7 BP |
57 | return -fd; |
58 | } | |
59 | ||
2c487bc8 | 60 | free(connect_path); |
b7636967 | 61 | return new_fd_stream(xstrdup(name), fd, check_connection_completion(fd), |
b7cefbf7 | 62 | AF_UNIX, streamp); |
c34b65c7 BP |
63 | } |
64 | ||
da327b18 | 65 | const struct stream_class unix_stream_class = { |
c34b65c7 | 66 | "unix", /* name */ |
f1936eb6 | 67 | false, /* needs_probes */ |
c34b65c7 BP |
68 | unix_open, /* open */ |
69 | NULL, /* close */ | |
70 | NULL, /* connect */ | |
71 | NULL, /* recv */ | |
72 | NULL, /* send */ | |
539e96f6 BP |
73 | NULL, /* run */ |
74 | NULL, /* run_wait */ | |
c34b65c7 BP |
75 | NULL, /* wait */ |
76 | }; | |
77 | \f | |
78 | /* Passive UNIX socket. */ | |
79 | ||
e731d71b AS |
80 | static int punix_accept(int fd, const struct sockaddr_storage *ss, |
81 | size_t ss_len, struct stream **streamp); | |
c34b65c7 BP |
82 | |
83 | static int | |
c69ee87c | 84 | punix_open(const char *name OVS_UNUSED, char *suffix, |
f125905c | 85 | struct pstream **pstreamp, uint8_t dscp OVS_UNUSED) |
c34b65c7 | 86 | { |
2c487bc8 | 87 | char *bind_path; |
c34b65c7 BP |
88 | int fd, error; |
89 | ||
2c487bc8 PR |
90 | bind_path = abs_file_name(ovs_rundir(), suffix); |
91 | fd = make_unix_socket(SOCK_STREAM, true, bind_path, NULL); | |
c34b65c7 | 92 | if (fd < 0) { |
10a89ef0 | 93 | VLOG_ERR("%s: binding failed: %s", bind_path, ovs_strerror(errno)); |
2c487bc8 | 94 | free(bind_path); |
c34b65c7 BP |
95 | return errno; |
96 | } | |
97 | ||
29058c4e | 98 | if (listen(fd, 64) < 0) { |
c34b65c7 | 99 | error = errno; |
10a89ef0 | 100 | VLOG_ERR("%s: listen: %s", name, ovs_strerror(error)); |
c34b65c7 | 101 | close(fd); |
2c487bc8 | 102 | free(bind_path); |
c34b65c7 BP |
103 | return error; |
104 | } | |
105 | ||
b7636967 BP |
106 | return new_fd_pstream(xstrdup(name), fd, |
107 | punix_accept, bind_path, pstreamp); | |
c34b65c7 BP |
108 | } |
109 | ||
110 | static int | |
e731d71b | 111 | punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len, |
c34b65c7 BP |
112 | struct stream **streamp) |
113 | { | |
e731d71b | 114 | const struct sockaddr_un *sun = (const struct sockaddr_un *) ss; |
dd23522f | 115 | int name_len = get_unix_name_len(sun, ss_len); |
14347f3e BP |
116 | char *bound_name; |
117 | ||
118 | if (name_len > 0) { | |
119 | bound_name = xasprintf("unix:%.*s", name_len, sun->sun_path); | |
120 | } else { | |
121 | /* When a Unix socket connects to us without first binding a name, we | |
122 | * don't get any name for it. It's useful nevertheless to be able to | |
123 | * distinguish separate sockets in log messages, so use a counter. */ | |
124 | static atomic_count next_idx = ATOMIC_COUNT_INIT(0); | |
125 | bound_name = xasprintf("unix#%u", atomic_count_inc(&next_idx)); | |
126 | } | |
b7636967 | 127 | return new_fd_stream(bound_name, fd, 0, AF_UNIX, streamp); |
c34b65c7 BP |
128 | } |
129 | ||
da327b18 | 130 | const struct pstream_class punix_pstream_class = { |
c34b65c7 | 131 | "punix", |
f1936eb6 | 132 | false, |
c34b65c7 BP |
133 | punix_open, |
134 | NULL, | |
135 | NULL, | |
f89b7ce5 | 136 | NULL, |
c34b65c7 BP |
137 | }; |
138 |