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 19 #include "src/core/lib/iomgr/resolve_address.h" 20 21 #include <string.h> 22 #include <sys/un.h> 23 24 #include <grpc/grpc.h> 25 #include <grpc/support/alloc.h> 26 #include <grpc/support/log.h> 27 #include <grpc/support/sync.h> 28 #include <grpc/support/time.h> 29 30 #include "src/core/lib/gpr/useful.h" 31 #include "src/core/lib/gprpp/thd.h" 32 #include "src/core/lib/iomgr/executor.h" 33 #include "src/core/lib/iomgr/iomgr.h" 34 #include "test/core/util/test_config.h" 35 36 static gpr_timespec test_deadline(void) { 37 return grpc_timeout_seconds_to_deadline(100); 38 } 39 40 typedef struct args_struct { 41 grpc_core::Thread thd; 42 gpr_event ev; 43 grpc_resolved_addresses* addrs; 44 gpr_atm done_atm; 45 gpr_mu* mu; 46 grpc_pollset* pollset; 47 grpc_pollset_set* pollset_set; 48 } args_struct; 49 50 static void do_nothing(void* arg, grpc_error* error) {} 51 52 void args_init(args_struct* args) { 53 gpr_event_init(&args->ev); 54 args->pollset = static_cast<grpc_pollset*>(gpr_zalloc(grpc_pollset_size())); 55 grpc_pollset_init(args->pollset, &args->mu); 56 args->pollset_set = grpc_pollset_set_create(); 57 grpc_pollset_set_add_pollset(args->pollset_set, args->pollset); 58 args->addrs = nullptr; 59 } 60 61 void args_finish(args_struct* args) { 62 GPR_ASSERT(gpr_event_wait(&args->ev, test_deadline())); 63 args->thd.Join(); 64 // Don't need to explicitly destruct args->thd since 65 // args is actually going to be destructed, not just freed 66 grpc_resolved_addresses_destroy(args->addrs); 67 grpc_pollset_set_del_pollset(args->pollset_set, args->pollset); 68 grpc_pollset_set_destroy(args->pollset_set); 69 grpc_closure do_nothing_cb; 70 GRPC_CLOSURE_INIT(&do_nothing_cb, do_nothing, nullptr, 71 grpc_schedule_on_exec_ctx); 72 grpc_pollset_shutdown(args->pollset, &do_nothing_cb); 73 // exec_ctx needs to be flushed before calling grpc_pollset_destroy() 74 grpc_core::ExecCtx::Get()->Flush(); 75 grpc_pollset_destroy(args->pollset); 76 gpr_free(args->pollset); 77 } 78 79 static grpc_millis n_sec_deadline(int seconds) { 80 return grpc_timespec_to_millis_round_up( 81 grpc_timeout_seconds_to_deadline(seconds)); 82 } 83 84 static void actually_poll(void* argsp) { 85 args_struct* args = static_cast<args_struct*>(argsp); 86 grpc_millis deadline = n_sec_deadline(10); 87 while (true) { 88 grpc_core::ExecCtx exec_ctx; 89 bool done = gpr_atm_acq_load(&args->done_atm) != 0; 90 if (done) { 91 break; 92 } 93 grpc_millis time_left = deadline - grpc_core::ExecCtx::Get()->Now(); 94 gpr_log(GPR_DEBUG, "done=%d, time_left=%" PRId64, done, time_left); 95 GPR_ASSERT(time_left >= 0); 96 grpc_pollset_worker* worker = nullptr; 97 gpr_mu_lock(args->mu); 98 GRPC_LOG_IF_ERROR("pollset_work", grpc_pollset_work(args->pollset, &worker, 99 n_sec_deadline(1))); 100 gpr_mu_unlock(args->mu); 101 grpc_core::ExecCtx::Get()->Flush(); 102 } 103 gpr_event_set(&args->ev, (void*)1); 104 } 105 106 static void poll_pollset_until_request_done(args_struct* args) { 107 gpr_atm_rel_store(&args->done_atm, 0); 108 args->thd = grpc_core::Thread("grpc_poll_pollset", actually_poll, args); 109 args->thd.Start(); 110 } 111 112 static void must_succeed(void* argsp, grpc_error* err) { 113 args_struct* args = static_cast<args_struct*>(argsp); 114 GPR_ASSERT(err == GRPC_ERROR_NONE); 115 GPR_ASSERT(args->addrs != nullptr); 116 GPR_ASSERT(args->addrs->naddrs > 0); 117 gpr_atm_rel_store(&args->done_atm, 1); 118 } 119 120 static void must_fail(void* argsp, grpc_error* err) { 121 args_struct* args = static_cast<args_struct*>(argsp); 122 GPR_ASSERT(err != GRPC_ERROR_NONE); 123 gpr_atm_rel_store(&args->done_atm, 1); 124 } 125 126 static void test_unix_socket(void) { 127 grpc_core::ExecCtx exec_ctx; 128 args_struct args; 129 args_init(&args); 130 poll_pollset_until_request_done(&args); 131 grpc_resolve_address( 132 "unix:/path/name", nullptr, args.pollset_set, 133 GRPC_CLOSURE_CREATE(must_succeed, &args, grpc_schedule_on_exec_ctx), 134 &args.addrs); 135 args_finish(&args); 136 } 137 138 static void test_unix_socket_path_name_too_long(void) { 139 grpc_core::ExecCtx exec_ctx; 140 args_struct args; 141 args_init(&args); 142 const char prefix[] = "unix:/path/name"; 143 size_t path_name_length = 144 GPR_ARRAY_SIZE(((struct sockaddr_un*)nullptr)->sun_path) + 6; 145 char* path_name = 146 static_cast<char*>(gpr_malloc(sizeof(char) * path_name_length)); 147 memset(path_name, 'a', path_name_length); 148 memcpy(path_name, prefix, strlen(prefix) - 1); 149 path_name[path_name_length - 1] = '\0'; 150 151 poll_pollset_until_request_done(&args); 152 grpc_resolve_address( 153 path_name, nullptr, args.pollset_set, 154 GRPC_CLOSURE_CREATE(must_fail, &args, grpc_schedule_on_exec_ctx), 155 &args.addrs); 156 gpr_free(path_name); 157 args_finish(&args); 158 } 159 160 int main(int argc, char** argv) { 161 grpc_test_init(argc, argv); 162 grpc_init(); 163 164 { 165 grpc_core::ExecCtx exec_ctx; 166 test_unix_socket(); 167 test_unix_socket_path_name_too_long(); 168 } 169 170 grpc_shutdown(); 171 return 0; 172 } 173