]> git.proxmox.com Git - mirror_ovs.git/blob - lib/stream-unix.c
async-append: New library to allow asynchronous appending to a log file.
[mirror_ovs.git] / lib / stream-unix.c
1 /*
2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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 <errno.h>
20 #include <inttypes.h>
21 #include <netdb.h>
22 #include <poll.h>
23 #include <sys/socket.h>
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"
32 #include "dirs.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 static int
43 unix_open(const char *name, char *suffix, struct stream **streamp,
44 uint8_t dscp OVS_UNUSED)
45 {
46 char *connect_path;
47 int fd;
48
49 connect_path = abs_file_name(ovs_rundir(), suffix);
50 fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path);
51
52 if (fd < 0) {
53 VLOG_DBG("%s: connection failed (%s)",
54 connect_path, ovs_strerror(-fd));
55 free(connect_path);
56 return -fd;
57 }
58
59 free(connect_path);
60 return new_fd_stream(name, fd, check_connection_completion(fd), streamp);
61 }
62
63 const struct stream_class unix_stream_class = {
64 "unix", /* name */
65 false, /* needs_probes */
66 unix_open, /* open */
67 NULL, /* close */
68 NULL, /* connect */
69 NULL, /* recv */
70 NULL, /* send */
71 NULL, /* run */
72 NULL, /* run_wait */
73 NULL, /* wait */
74 };
75 \f
76 /* Passive UNIX socket. */
77
78 static int punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
79 struct stream **streamp);
80
81 static int
82 punix_open(const char *name OVS_UNUSED, char *suffix,
83 struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
84 {
85 char *bind_path;
86 int fd, error;
87
88 bind_path = abs_file_name(ovs_rundir(), suffix);
89 fd = make_unix_socket(SOCK_STREAM, true, bind_path, NULL);
90 if (fd < 0) {
91 VLOG_ERR("%s: binding failed: %s", bind_path, ovs_strerror(errno));
92 free(bind_path);
93 return errno;
94 }
95
96 if (listen(fd, 10) < 0) {
97 error = errno;
98 VLOG_ERR("%s: listen: %s", name, ovs_strerror(error));
99 close(fd);
100 free(bind_path);
101 return error;
102 }
103
104 return new_fd_pstream(name, fd, punix_accept, NULL, bind_path, pstreamp);
105 }
106
107 static int
108 punix_accept(int fd, const struct sockaddr *sa, size_t sa_len,
109 struct stream **streamp)
110 {
111 const struct sockaddr_un *sun = (const struct sockaddr_un *) sa;
112 int name_len = get_unix_name_len(sa_len);
113 char name[128];
114
115 if (name_len > 0) {
116 snprintf(name, sizeof name, "unix:%.*s", name_len, sun->sun_path);
117 } else {
118 strcpy(name, "unix");
119 }
120 return new_fd_stream(name, fd, 0, streamp);
121 }
122
123 const struct pstream_class punix_pstream_class = {
124 "punix",
125 false,
126 punix_open,
127 NULL,
128 NULL,
129 NULL,
130 NULL,
131 };
132