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 <string.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27 #ifdef GPR_WINDOWS
28 // clang-format off
29 #include <ws2def.h>
30 #include <afunix.h>
31 // clang-format on
32 #else
33 #include <sys/un.h>
34 #endif // GPR_WINDOWS
35
36 #include <grpc/support/alloc.h>
37
38 #include "absl/log/check.h"
39 #include "absl/strings/str_cat.h"
40 #include "src/core/lib/address_utils/parse_address.h"
41 #include "src/core/lib/iomgr/sockaddr.h"
42 #include "src/core/lib/iomgr/unix_sockets_posix.h"
43 #include "src/core/lib/transport/error_utils.h"
44 #include "src/core/util/crash.h"
45 #include "src/core/util/useful.h"
46
grpc_create_socketpair_if_unix(int sv[2])47 void grpc_create_socketpair_if_unix(int sv[2]) {
48 #ifdef GPR_WINDOWS
49 grpc_core::Crash("AF_UNIX socket pairs are not supported on Windows");
50 #else
51 CHECK_EQ(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
52 #endif
53 }
54
55 absl::StatusOr<std::vector<grpc_resolved_address>>
grpc_resolve_unix_domain_address(absl::string_view name)56 grpc_resolve_unix_domain_address(absl::string_view name) {
57 grpc_resolved_address addr;
58 grpc_error_handle error = grpc_core::UnixSockaddrPopulate(name, &addr);
59 if (error.ok()) {
60 return std::vector<grpc_resolved_address>({addr});
61 }
62 auto result = grpc_error_to_absl_status(error);
63 return result;
64 }
65
66 absl::StatusOr<std::vector<grpc_resolved_address>>
grpc_resolve_unix_abstract_domain_address(const absl::string_view name)67 grpc_resolve_unix_abstract_domain_address(const absl::string_view name) {
68 grpc_resolved_address addr;
69 grpc_error_handle error =
70 grpc_core::UnixAbstractSockaddrPopulate(name, &addr);
71 if (error.ok()) {
72 return std::vector<grpc_resolved_address>({addr});
73 }
74 auto result = grpc_error_to_absl_status(error);
75 return result;
76 }
77
grpc_is_unix_socket(const grpc_resolved_address * resolved_addr)78 int grpc_is_unix_socket(const grpc_resolved_address* resolved_addr) {
79 const grpc_sockaddr* addr =
80 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
81 return addr->sa_family == AF_UNIX;
82 }
83
grpc_unlink_if_unix_domain_socket(const grpc_resolved_address * resolved_addr)84 void grpc_unlink_if_unix_domain_socket(
85 const grpc_resolved_address* resolved_addr) {
86 const grpc_sockaddr* addr =
87 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
88 if (addr->sa_family != AF_UNIX) {
89 return;
90 }
91 struct sockaddr_un* un = reinterpret_cast<struct sockaddr_un*>(
92 const_cast<char*>(resolved_addr->addr));
93
94 // There is nothing to unlink for an abstract unix socket
95 if (un->sun_path[0] == '\0' && un->sun_path[1] != '\0') {
96 return;
97 }
98
99 #ifndef GPR_WINDOWS
100 struct stat st;
101 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
102 unlink(un->sun_path);
103 }
104 #endif
105 }
106
107 #endif
108