• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *
3  * Copyright 2016 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 #include <grpc/support/port_platform.h>
19 
20 #include "src/core/lib/iomgr/port.h"
21 
22 #ifdef GRPC_HAVE_UNIX_SOCKET
23 
24 #include "src/core/lib/iomgr/sockaddr.h"
25 
26 #include <string.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/un.h>
30 
31 #include "absl/strings/str_cat.h"
32 
33 #include "src/core/lib/iomgr/parse_address.h"
34 #include "src/core/lib/iomgr/unix_sockets_posix.h"
35 
36 #include <grpc/support/alloc.h>
37 #include <grpc/support/log.h>
38 
39 #include "src/core/lib/gpr/useful.h"
40 
grpc_create_socketpair_if_unix(int sv[2])41 void grpc_create_socketpair_if_unix(int sv[2]) {
42   GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
43 }
44 
grpc_resolve_unix_domain_address(const char * name,grpc_resolved_addresses ** addresses)45 grpc_error* grpc_resolve_unix_domain_address(
46     const char* name, grpc_resolved_addresses** addresses) {
47   *addresses = static_cast<grpc_resolved_addresses*>(
48       gpr_malloc(sizeof(grpc_resolved_addresses)));
49   (*addresses)->naddrs = 1;
50   (*addresses)->addrs = static_cast<grpc_resolved_address*>(
51       gpr_malloc(sizeof(grpc_resolved_address)));
52   return grpc_core::UnixSockaddrPopulate(name, (*addresses)->addrs);
53 }
54 
grpc_resolve_unix_abstract_domain_address(const absl::string_view name,grpc_resolved_addresses ** addresses)55 grpc_error* grpc_resolve_unix_abstract_domain_address(
56     const absl::string_view name, grpc_resolved_addresses** addresses) {
57   *addresses = static_cast<grpc_resolved_addresses*>(
58       gpr_malloc(sizeof(grpc_resolved_addresses)));
59   (*addresses)->naddrs = 1;
60   (*addresses)->addrs = static_cast<grpc_resolved_address*>(
61       gpr_malloc(sizeof(grpc_resolved_address)));
62   return grpc_core::UnixAbstractSockaddrPopulate(name, (*addresses)->addrs);
63 }
64 
grpc_is_unix_socket(const grpc_resolved_address * resolved_addr)65 int grpc_is_unix_socket(const grpc_resolved_address* resolved_addr) {
66   const grpc_sockaddr* addr =
67       reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
68   return addr->sa_family == AF_UNIX;
69 }
70 
grpc_unlink_if_unix_domain_socket(const grpc_resolved_address * resolved_addr)71 void grpc_unlink_if_unix_domain_socket(
72     const grpc_resolved_address* resolved_addr) {
73   const grpc_sockaddr* addr =
74       reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
75   if (addr->sa_family != AF_UNIX) {
76     return;
77   }
78   struct sockaddr_un* un = reinterpret_cast<struct sockaddr_un*>(
79       const_cast<char*>(resolved_addr->addr));
80 
81   // There is nothing to unlink for an abstract unix socket
82   if (un->sun_path[0] == '\0' && un->sun_path[1] != '\0') {
83     return;
84   }
85 
86   struct stat st;
87   if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
88     unlink(un->sun_path);
89   }
90 }
91 
grpc_sockaddr_to_uri_unix_if_possible(const grpc_resolved_address * resolved_addr)92 std::string grpc_sockaddr_to_uri_unix_if_possible(
93     const grpc_resolved_address* resolved_addr) {
94   const grpc_sockaddr* addr =
95       reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
96   if (addr->sa_family != AF_UNIX) {
97     return "";
98   }
99   const auto* unix_addr = reinterpret_cast<const struct sockaddr_un*>(addr);
100   if (unix_addr->sun_path[0] == '\0' && unix_addr->sun_path[1] != '\0') {
101     return absl::StrCat(
102         "unix-abstract:",
103         absl::string_view(
104             unix_addr->sun_path + 1,
105             resolved_addr->len - sizeof(unix_addr->sun_family) - 1));
106   }
107   return absl::StrCat("unix:", unix_addr->sun_path);
108 }
109 
110 #endif
111