Debian Codegraph @

src/core/lib/iomgr/unix_sockets_posix.cc

raw
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 #include <sys/un.h>
28
29 #include "absl/strings/str_cat.h"
30
31 #include <grpc/support/alloc.h>
32 #include <grpc/support/log.h>
33
34 #include "src/core/lib/address_utils/parse_address.h"
35 #include "src/core/lib/gpr/useful.h"
36 #include "src/core/lib/iomgr/sockaddr.h"
37 #include "src/core/lib/iomgr/unix_sockets_posix.h"
38 #include "src/core/lib/transport/error_utils.h"
39
40 void grpc_create_socketpair_if_unix(int sv[2]) {
41 GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0);
42 }
43
44 absl::StatusOr<std::vector<grpc_resolved_address>>
45 grpc_resolve_unix_domain_address(absl::string_view name) {
46 grpc_resolved_address addr;
47 grpc_error_handle error = grpc_core::UnixSockaddrPopulate(name, &addr);
48 if (error.ok()) {
49 return std::vector<grpc_resolved_address>({addr});
50 }
51 auto result = grpc_error_to_absl_status(error);
52 return result;
53 }
54
55 absl::StatusOr<std::vector<grpc_resolved_address>>
56 grpc_resolve_unix_abstract_domain_address(const absl::string_view name) {
57 grpc_resolved_address addr;
58 grpc_error_handle error =
59 grpc_core::UnixAbstractSockaddrPopulate(name, &addr);
60 if (error.ok()) {
61 return std::vector<grpc_resolved_address>({addr});
62 }
63 auto result = grpc_error_to_absl_status(error);
64 return result;
65 }
66
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
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
83 // There is nothing to unlink for an abstract unix socket
84 if (un->sun_path[0] == '\0' && un->sun_path[1] != '\0') {
85 return;
86 }
87
88 struct stat st;
89 if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) {
90 unlink(un->sun_path);
91 }
92 }
93
94 #endif