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/unix_sockets_posix.h"
34
35 #include <grpc/support/alloc.h>
36 #include <grpc/support/log.h>
37
38 #include "src/core/lib/gpr/useful.h"
39
grpc_create_socketpair_if_unix(int sv[2])40 void grpc_create_socketpair_if_unix(int sv[2]) {
41 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
42 }
43
grpc_resolve_unix_domain_address(const char * name,grpc_resolved_addresses ** addrs)44 grpc_error* grpc_resolve_unix_domain_address(const char* name,
45 grpc_resolved_addresses** addrs) {
46 struct sockaddr_un* un;
47 if (strlen(name) >
48 GPR_ARRAY_SIZE(((struct sockaddr_un*)nullptr)->sun_path) - 1) {
49 return GRPC_ERROR_CREATE_FROM_COPIED_STRING(
50 absl::StrCat("Path name should not have more than ",
51 GPR_ARRAY_SIZE(un->sun_path) - 1, " characters")
52 .c_str());
53 }
54 *addrs = static_cast<grpc_resolved_addresses*>(
55 gpr_malloc(sizeof(grpc_resolved_addresses)));
56 (*addrs)->naddrs = 1;
57 (*addrs)->addrs = static_cast<grpc_resolved_address*>(
58 gpr_malloc(sizeof(grpc_resolved_address)));
59 un = reinterpret_cast<struct sockaddr_un*>((*addrs)->addrs->addr);
60 un->sun_family = AF_UNIX;
61 strncpy(un->sun_path, name, sizeof(un->sun_path));
62 (*addrs)->addrs->len =
63 static_cast<socklen_t>(strlen(un->sun_path) + sizeof(un->sun_family) + 1);
64 return GRPC_ERROR_NONE;
65 }
66
grpc_is_unix_socket(const grpc_resolved_address * resolved_addr)67 int grpc_is_unix_socket(const grpc_resolved_address* resolved_addr) {
68 const grpc_sockaddr* addr =
69 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
70 return addr->sa_family == AF_UNIX;
71 }
72
grpc_unlink_if_unix_domain_socket(const grpc_resolved_address * resolved_addr)73 void grpc_unlink_if_unix_domain_socket(
74 const grpc_resolved_address* resolved_addr) {
75 const grpc_sockaddr* addr =
76 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
77 if (addr->sa_family != AF_UNIX) {
78 return;
79 }
80 struct sockaddr_un* un = reinterpret_cast<struct sockaddr_un*>(
81 const_cast<char*>(resolved_addr->addr));
82 struct stat st;
83
84 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
85 unlink(un->sun_path);
86 }
87 }
88
grpc_sockaddr_to_uri_unix_if_possible(const grpc_resolved_address * resolved_addr)89 std::string grpc_sockaddr_to_uri_unix_if_possible(
90 const grpc_resolved_address* resolved_addr) {
91 const grpc_sockaddr* addr =
92 reinterpret_cast<const grpc_sockaddr*>(resolved_addr->addr);
93 if (addr->sa_family != AF_UNIX) {
94 return "";
95 }
96 return absl::StrCat("unix:", ((struct sockaddr_un*)addr)->sun_path);
97 }
98
99 #endif
100