blob: 030acd98119fc442db688238d68a0f4822e79b90 [file] [log] [blame]
ahedberg80d6b122016-03-17 17:37:35 -04001/*
2 *
ahedberg8afb88d2016-03-22 13:57:47 -04003 * Copyright 2016, Google Inc.
ahedberg80d6b122016-03-17 17:37:35 -04004 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
murgatroid9954070892016-08-08 17:01:18 -070033#include "src/core/lib/iomgr/port.h"
ahedberg80d6b122016-03-17 17:37:35 -040034
murgatroid99623dd4f2016-08-08 17:31:27 -070035#ifdef GRPC_HAVE_UNIX_SOCKET
ahedberg80d6b122016-03-17 17:37:35 -040036
murgatroid997871f732016-09-23 13:49:05 -070037#include "src/core/lib/iomgr/sockaddr.h"
38
ahedberg799fd4b2016-03-22 13:47:46 -040039#include <string.h>
ahedberg43df2952016-03-18 10:46:38 -040040#include <sys/stat.h>
Craig Tillerf40df232016-03-25 13:38:14 -070041#include <sys/types.h>
ahedberg80d6b122016-03-17 17:37:35 -040042#include <sys/un.h>
43
murgatroid9954070892016-08-08 17:01:18 -070044#include "src/core/lib/iomgr/unix_sockets_posix.h"
45
ahedberg80d6b122016-03-17 17:37:35 -040046#include <grpc/support/alloc.h>
Craig Tillerf82ddc42016-04-05 17:15:07 -070047#include <grpc/support/log.h>
ahedberg80d6b122016-03-17 17:37:35 -040048
49void grpc_create_socketpair_if_unix(int sv[2]) {
50 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
51}
52
Craig Tiller2b782d82016-05-04 16:29:57 -070053grpc_error *grpc_resolve_unix_domain_address(const char *name,
54 grpc_resolved_addresses **addrs) {
ahedberg80d6b122016-03-17 17:37:35 -040055 struct sockaddr_un *un;
56
Craig Tiller2b782d82016-05-04 16:29:57 -070057 *addrs = gpr_malloc(sizeof(grpc_resolved_addresses));
58 (*addrs)->naddrs = 1;
59 (*addrs)->addrs = gpr_malloc(sizeof(grpc_resolved_address));
60 un = (struct sockaddr_un *)(*addrs)->addrs->addr;
ahedberg80d6b122016-03-17 17:37:35 -040061 un->sun_family = AF_UNIX;
62 strcpy(un->sun_path, name);
Craig Tiller2b782d82016-05-04 16:29:57 -070063 (*addrs)->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1;
64 return GRPC_ERROR_NONE;
ahedberg80d6b122016-03-17 17:37:35 -040065}
66
murgatroid997871f732016-09-23 13:49:05 -070067int grpc_is_unix_socket(const grpc_resolved_address *resolved_addr) {
68 const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr;
ahedberg43df2952016-03-18 10:46:38 -040069 return addr->sa_family == AF_UNIX;
ahedberg80d6b122016-03-17 17:37:35 -040070}
71
murgatroid99dedb9232016-09-26 13:54:04 -070072void grpc_unlink_if_unix_domain_socket(
73 const grpc_resolved_address *resolved_addr) {
murgatroid997871f732016-09-23 13:49:05 -070074 const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr;
ahedberg80d6b122016-03-17 17:37:35 -040075 if (addr->sa_family != AF_UNIX) {
76 return;
77 }
murgatroid997871f732016-09-23 13:49:05 -070078 struct sockaddr_un *un = (struct sockaddr_un *)resolved_addr->addr;
ahedberg80d6b122016-03-17 17:37:35 -040079 struct stat st;
80
81 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
82 unlink(un->sun_path);
83 }
84}
85
murgatroid99dedb9232016-09-26 13:54:04 -070086char *grpc_sockaddr_to_uri_unix_if_possible(
87 const grpc_resolved_address *resolved_addr) {
murgatroid997871f732016-09-23 13:49:05 -070088 const struct sockaddr *addr = (const struct sockaddr *)resolved_addr->addr;
ahedberg80d6b122016-03-17 17:37:35 -040089 if (addr->sa_family != AF_UNIX) {
90 return NULL;
91 }
92
ahedberg8afb88d2016-03-22 13:57:47 -040093 char *result;
murgatroid99dedb9232016-09-26 13:54:04 -070094 gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path);
ahedberg80d6b122016-03-17 17:37:35 -040095 return result;
96}
97
98#endif