]> git.proxmox.com Git - mirror_ovs.git/blame - lib/stream-unix.c
dpctl: add examples to the manpage.
[mirror_ovs.git] / lib / stream-unix.c
CommitLineData
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>
29#include "packets.h"
30#include "poll-loop.h"
31#include "socket-util.h"
2c487bc8 32#include "dirs.h"
c34b65c7
BP
33#include "util.h"
34#include "stream-provider.h"
35#include "stream-fd.h"
e6211adc 36#include "openvswitch/vlog.h"
5136ce49 37
d98e6007 38VLOG_DEFINE_THIS_MODULE(stream_unix);
c34b65c7
BP
39
40/* Active UNIX socket. */
41
c34b65c7 42static int
f125905c
MM
43unix_open(const char *name, char *suffix, struct stream **streamp,
44 uint8_t dscp OVS_UNUSED)
c34b65c7 45{
2c487bc8 46 char *connect_path;
c34b65c7
BP
47 int fd;
48
2c487bc8 49 connect_path = abs_file_name(ovs_rundir(), suffix);
5ca92d1d 50 fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path);
2c487bc8 51
c34b65c7 52 if (fd < 0) {
10a89ef0
BP
53 VLOG_DBG("%s: connection failed (%s)",
54 connect_path, ovs_strerror(-fd));
2c487bc8 55 free(connect_path);
c34b65c7
BP
56 return -fd;
57 }
58
2c487bc8 59 free(connect_path);
b7cefbf7
GS
60 return new_fd_stream(name, fd, check_connection_completion(fd),
61 AF_UNIX, streamp);
c34b65c7
BP
62}
63
da327b18 64const struct stream_class unix_stream_class = {
c34b65c7 65 "unix", /* name */
f1936eb6 66 false, /* needs_probes */
c34b65c7
BP
67 unix_open, /* open */
68 NULL, /* close */
69 NULL, /* connect */
70 NULL, /* recv */
71 NULL, /* send */
539e96f6
BP
72 NULL, /* run */
73 NULL, /* run_wait */
c34b65c7
BP
74 NULL, /* wait */
75};
76\f
77/* Passive UNIX socket. */
78
e731d71b
AS
79static int punix_accept(int fd, const struct sockaddr_storage *ss,
80 size_t ss_len, struct stream **streamp);
c34b65c7
BP
81
82static int
c69ee87c 83punix_open(const char *name OVS_UNUSED, char *suffix,
f125905c 84 struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
c34b65c7 85{
2c487bc8 86 char *bind_path;
c34b65c7
BP
87 int fd, error;
88
2c487bc8
PR
89 bind_path = abs_file_name(ovs_rundir(), suffix);
90 fd = make_unix_socket(SOCK_STREAM, true, bind_path, NULL);
c34b65c7 91 if (fd < 0) {
10a89ef0 92 VLOG_ERR("%s: binding failed: %s", bind_path, ovs_strerror(errno));
2c487bc8 93 free(bind_path);
c34b65c7
BP
94 return errno;
95 }
96
29058c4e 97 if (listen(fd, 64) < 0) {
c34b65c7 98 error = errno;
10a89ef0 99 VLOG_ERR("%s: listen: %s", name, ovs_strerror(error));
c34b65c7 100 close(fd);
2c487bc8 101 free(bind_path);
c34b65c7
BP
102 return error;
103 }
104
c2e3cbaf 105 return new_fd_pstream(name, fd, punix_accept, bind_path, pstreamp);
c34b65c7
BP
106}
107
108static int
e731d71b 109punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
c34b65c7
BP
110 struct stream **streamp)
111{
e731d71b 112 const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
dd23522f 113 int name_len = get_unix_name_len(sun, ss_len);
c34b65c7
BP
114 char name[128];
115
116 if (name_len > 0) {
117 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
118 } else {
119 strcpy(name, "unix");
120 }
b7cefbf7 121 return new_fd_stream(name, fd, 0, AF_UNIX, streamp);
c34b65c7
BP
122}
123
da327b18 124const struct pstream_class punix_pstream_class = {
c34b65c7 125 "punix",
f1936eb6 126 false,
c34b65c7
BP
127 punix_open,
128 NULL,
129 NULL,
f89b7ce5 130 NULL,
c34b65c7
BP
131};
132