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