2009-11-04 15:02:32 -08:00
|
|
|
|
/*
|
2015-02-20 08:44:48 -08:00
|
|
|
|
* Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
|
2009-11-04 15:02:32 -08:00
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include "stream.h"
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
#include <netdb.h>
|
|
|
|
|
#include <poll.h>
|
2010-05-26 10:37:39 -07:00
|
|
|
|
#include <sys/socket.h>
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include "packets.h"
|
2017-11-03 13:53:53 +08:00
|
|
|
|
#include "openvswitch/poll-loop.h"
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include "socket-util.h"
|
2013-02-08 12:37:18 -08:00
|
|
|
|
#include "dirs.h"
|
2009-11-04 15:02:32 -08:00
|
|
|
|
#include "util.h"
|
|
|
|
|
#include "stream-provider.h"
|
|
|
|
|
#include "stream-fd.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2010-07-16 11:02:49 -07:00
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(stream_unix);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
|
|
|
|
|
/* Active UNIX socket. */
|
|
|
|
|
|
|
|
|
|
static int
|
2012-03-10 15:58:10 -08:00
|
|
|
|
unix_open(const char *name, char *suffix, struct stream **streamp,
|
|
|
|
|
uint8_t dscp OVS_UNUSED)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
2013-02-08 12:37:18 -08:00
|
|
|
|
char *connect_path;
|
2009-11-04 15:02:32 -08:00
|
|
|
|
int fd;
|
|
|
|
|
|
2013-02-08 12:37:18 -08:00
|
|
|
|
connect_path = abs_file_name(ovs_rundir(), suffix);
|
2012-05-14 14:32:14 -07:00
|
|
|
|
fd = make_unix_socket(SOCK_STREAM, true, NULL, connect_path);
|
2013-02-08 12:37:18 -08:00
|
|
|
|
|
2009-11-04 15:02:32 -08:00
|
|
|
|
if (fd < 0) {
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_DBG("%s: connection failed (%s)",
|
|
|
|
|
connect_path, ovs_strerror(-fd));
|
2013-02-08 12:37:18 -08:00
|
|
|
|
free(connect_path);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
return -fd;
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-08 12:37:18 -08:00
|
|
|
|
free(connect_path);
|
2017-07-14 14:33:44 -07:00
|
|
|
|
return new_fd_stream(xstrdup(name), fd, check_connection_completion(fd),
|
2014-10-22 15:35:18 -07:00
|
|
|
|
AF_UNIX, streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 10:22:22 +09:00
|
|
|
|
const struct stream_class unix_stream_class = {
|
2009-11-04 15:02:32 -08:00
|
|
|
|
"unix", /* name */
|
2012-04-11 20:18:34 -07:00
|
|
|
|
false, /* needs_probes */
|
2009-11-04 15:02:32 -08:00
|
|
|
|
unix_open, /* open */
|
|
|
|
|
NULL, /* close */
|
|
|
|
|
NULL, /* connect */
|
|
|
|
|
NULL, /* recv */
|
|
|
|
|
NULL, /* send */
|
2010-01-06 14:26:48 -08:00
|
|
|
|
NULL, /* run */
|
|
|
|
|
NULL, /* run_wait */
|
2009-11-04 15:02:32 -08:00
|
|
|
|
NULL, /* wait */
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Passive UNIX socket. */
|
|
|
|
|
|
2014-02-06 16:04:05 -08:00
|
|
|
|
static int punix_accept(int fd, const struct sockaddr_storage *ss,
|
|
|
|
|
size_t ss_len, struct stream **streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
|
|
|
|
|
static int
|
2010-02-11 11:11:23 -08:00
|
|
|
|
punix_open(const char *name OVS_UNUSED, char *suffix,
|
2012-03-10 15:58:10 -08:00
|
|
|
|
struct pstream **pstreamp, uint8_t dscp OVS_UNUSED)
|
2009-11-04 15:02:32 -08:00
|
|
|
|
{
|
2013-02-08 12:37:18 -08:00
|
|
|
|
char *bind_path;
|
2009-11-04 15:02:32 -08:00
|
|
|
|
int fd, error;
|
|
|
|
|
|
2013-02-08 12:37:18 -08:00
|
|
|
|
bind_path = abs_file_name(ovs_rundir(), suffix);
|
|
|
|
|
fd = make_unix_socket(SOCK_STREAM, true, bind_path, NULL);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
if (fd < 0) {
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_ERR("%s: binding failed: %s", bind_path, ovs_strerror(errno));
|
2013-02-08 12:37:18 -08:00
|
|
|
|
free(bind_path);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
return errno;
|
|
|
|
|
}
|
|
|
|
|
|
2014-09-30 16:55:25 +08:00
|
|
|
|
if (listen(fd, 64) < 0) {
|
2009-11-04 15:02:32 -08:00
|
|
|
|
error = errno;
|
2013-06-24 10:54:49 -07:00
|
|
|
|
VLOG_ERR("%s: listen: %s", name, ovs_strerror(error));
|
2009-11-04 15:02:32 -08:00
|
|
|
|
close(fd);
|
2013-02-08 12:37:18 -08:00
|
|
|
|
free(bind_path);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
return error;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-14 14:33:44 -07:00
|
|
|
|
return new_fd_pstream(xstrdup(name), fd,
|
|
|
|
|
punix_accept, bind_path, pstreamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
2014-02-06 16:04:05 -08:00
|
|
|
|
punix_accept(int fd, const struct sockaddr_storage *ss, size_t ss_len,
|
2009-11-04 15:02:32 -08:00
|
|
|
|
struct stream **streamp)
|
|
|
|
|
{
|
2014-02-06 16:04:05 -08:00
|
|
|
|
const struct sockaddr_un *sun = (const struct sockaddr_un *) ss;
|
2016-07-19 17:05:51 -03:00
|
|
|
|
int name_len = get_unix_name_len(sun, ss_len);
|
2017-07-14 14:33:44 -07:00
|
|
|
|
char *bound_name = (name_len > 0
|
|
|
|
|
? xasprintf("unix:%.*s", name_len, sun->sun_path)
|
|
|
|
|
: xstrdup("unix"));
|
|
|
|
|
return new_fd_stream(bound_name, fd, 0, AF_UNIX, streamp);
|
2009-11-04 15:02:32 -08:00
|
|
|
|
}
|
|
|
|
|
|
2011-11-24 10:22:22 +09:00
|
|
|
|
const struct pstream_class punix_pstream_class = {
|
2009-11-04 15:02:32 -08:00
|
|
|
|
"punix",
|
2012-04-11 20:18:34 -07:00
|
|
|
|
false,
|
2009-11-04 15:02:32 -08:00
|
|
|
|
punix_open,
|
|
|
|
|
NULL,
|
|
|
|
|
NULL,
|
2012-09-27 11:18:16 +09:00
|
|
|
|
NULL,
|
2009-11-04 15:02:32 -08:00
|
|
|
|
};
|
|
|
|
|
|