ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 1 | /* |
| 2 | * |
ahedberg | 8afb88d | 2016-03-22 13:57:47 -0400 | [diff] [blame] | 3 | * Copyright 2016, Google Inc. |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following disclaimer |
| 14 | * in the documentation and/or other materials provided with the |
| 15 | * distribution. |
| 16 | * * Neither the name of Google Inc. nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived from |
| 18 | * this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | */ |
| 33 | |
Craig Tiller | 9533d04 | 2016-03-25 17:11:06 -0700 | [diff] [blame] | 34 | #include "src/core/lib/iomgr/unix_sockets_posix.h" |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 35 | |
| 36 | #ifdef GPR_HAVE_UNIX_SOCKET |
| 37 | |
ahedberg | 799fd4b | 2016-03-22 13:47:46 -0400 | [diff] [blame] | 38 | #include <string.h> |
ahedberg | 43df295 | 2016-03-18 10:46:38 -0400 | [diff] [blame] | 39 | #include <sys/stat.h> |
Craig Tiller | f40df23 | 2016-03-25 13:38:14 -0700 | [diff] [blame] | 40 | #include <sys/types.h> |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 41 | #include <sys/un.h> |
| 42 | |
| 43 | #include <grpc/support/alloc.h> |
Craig Tiller | f82ddc4 | 2016-04-05 17:15:07 -0700 | [diff] [blame] | 44 | #include <grpc/support/log.h> |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 45 | |
| 46 | void grpc_create_socketpair_if_unix(int sv[2]) { |
| 47 | GPR_ASSERT(socketpair(AF_UNIX, SOCK_STREAM, 0, sv) == 0); |
| 48 | } |
| 49 | |
Craig Tiller | 2b782d8 | 2016-05-04 16:29:57 -0700 | [diff] [blame] | 50 | grpc_error *grpc_resolve_unix_domain_address(const char *name, |
| 51 | grpc_resolved_addresses **addrs) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 52 | struct sockaddr_un *un; |
| 53 | |
Craig Tiller | 2b782d8 | 2016-05-04 16:29:57 -0700 | [diff] [blame] | 54 | *addrs = gpr_malloc(sizeof(grpc_resolved_addresses)); |
| 55 | (*addrs)->naddrs = 1; |
| 56 | (*addrs)->addrs = gpr_malloc(sizeof(grpc_resolved_address)); |
| 57 | un = (struct sockaddr_un *)(*addrs)->addrs->addr; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 58 | un->sun_family = AF_UNIX; |
| 59 | strcpy(un->sun_path, name); |
Craig Tiller | 2b782d8 | 2016-05-04 16:29:57 -0700 | [diff] [blame] | 60 | (*addrs)->addrs->len = strlen(un->sun_path) + sizeof(un->sun_family) + 1; |
| 61 | return GRPC_ERROR_NONE; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 62 | } |
| 63 | |
ahedberg | 43df295 | 2016-03-18 10:46:38 -0400 | [diff] [blame] | 64 | int grpc_is_unix_socket(const struct sockaddr *addr) { |
| 65 | return addr->sa_family == AF_UNIX; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 66 | } |
| 67 | |
ahedberg | fcbcbff | 2016-03-22 13:23:47 -0400 | [diff] [blame] | 68 | void grpc_unlink_if_unix_domain_socket(const struct sockaddr *addr) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 69 | if (addr->sa_family != AF_UNIX) { |
| 70 | return; |
| 71 | } |
| 72 | struct sockaddr_un *un = (struct sockaddr_un *)addr; |
| 73 | struct stat st; |
| 74 | |
| 75 | if (stat(un->sun_path, &st) == 0 && (st.st_mode & S_IFMT) == S_IFSOCK) { |
| 76 | unlink(un->sun_path); |
| 77 | } |
| 78 | } |
| 79 | |
ahedberg | 43df295 | 2016-03-18 10:46:38 -0400 | [diff] [blame] | 80 | char *grpc_sockaddr_to_uri_unix_if_possible(const struct sockaddr *addr) { |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 81 | if (addr->sa_family != AF_UNIX) { |
| 82 | return NULL; |
| 83 | } |
| 84 | |
ahedberg | 8afb88d | 2016-03-22 13:57:47 -0400 | [diff] [blame] | 85 | char *result; |
ahedberg | 80d6b12 | 2016-03-17 17:37:35 -0400 | [diff] [blame] | 86 | gpr_asprintf(&result, "unix:%s", ((struct sockaddr_un *)addr)->sun_path); |
| 87 | return result; |
| 88 | } |
| 89 | |
| 90 | #endif |