1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright (c) 1999 by Sun Microsystems, Inc. 24 * All rights reserved. 25 */ 26 27 #include <stdio.h> 28 #include <stdlib.h> 29 #include <thread.h> 30 #include <synch.h> 31 #include <syslog.h> 32 #include <arpa/inet.h> 33 #include <slp-internal.h> 34 #include <slp_net_utils.h> 35 36 SLPError SLPOpen(const char *pcLang, SLPBoolean isAsync, SLPHandle *phSLP) { 37 slp_handle_impl_t *hp; 38 39 if (!pcLang || !phSLP) { 40 return (SLP_PARAMETER_BAD); 41 } 42 43 /* allocate the handle */ 44 if (!(hp = malloc(sizeof (*hp)))) { 45 slp_err(LOG_CRIT, 0, "SLPOpen", "out of memory"); 46 return (SLP_MEMORY_ALLOC_FAILED); 47 } 48 49 /* initialize outcall synchronization */ 50 hp->pending_outcall = SLP_FALSE; 51 (void) mutex_init(&(hp->outcall_lock), USYNC_THREAD, NULL); 52 (void) cond_init(&(hp->outcall_cv), USYNC_THREAD, NULL); 53 hp->close_on_end = SLP_FALSE; 54 hp->consumer_tid = 0; 55 56 /* locale property overrides argument */ 57 if (!(hp->locale = SLPGetProperty(SLP_CONFIG_LOCALE))) { 58 hp->locale = pcLang; 59 } 60 /* Make sure the language string is under our ownership */ 61 if (!(hp->locale = strdup(hp->locale))) { 62 free(hp); 63 slp_err(LOG_CRIT, 0, "SLPOpen", "out of memory"); 64 return (SLP_MEMORY_ALLOC_FAILED); 65 } 66 67 hp->cancel = 0; 68 69 /* Asynchronous operation? */ 70 if (isAsync) 71 hp->async = SLP_TRUE; 72 else 73 hp->async = SLP_FALSE; 74 75 /* TCP vars -- these are NULL until actually needed */ 76 hp->tcp_lock = NULL; 77 hp->tcp_wait = NULL; 78 hp->tcp_ref_cnt = 0; 79 80 /* Consumer / Producer pipe */ 81 hp->q = NULL; 82 83 /* Interface info, loaded on demand */ 84 hp->ifinfo = NULL; 85 86 /* force multicast, false by default */ 87 hp->force_multicast = SLP_FALSE; 88 89 /* internal call, false by default */ 90 hp->internal_call = SLP_FALSE; 91 92 *phSLP = hp; 93 return (SLP_OK); 94 } 95 96 void slp_cleanup_handle(slp_handle_impl_t *hp) { 97 /* free the handle */ 98 if (hp->tcp_lock) free(hp->tcp_lock); 99 if (hp->tcp_wait) free(hp->tcp_wait); 100 if (hp->ifinfo) { 101 slp_free_ifinfo(hp->ifinfo); 102 free(hp->ifinfo); 103 } 104 free((void *) hp->locale); 105 free(hp); 106 } 107 108 void SLPClose(SLPHandle hSLP) { 109 slp_handle_impl_t *hp = (slp_handle_impl_t *)hSLP; 110 111 if (!hSLP) { 112 return; 113 } 114 115 /* 116 * If an outcall is pending on this handle: 117 * If we are being called from a callback resulting 118 * from the outcall associated with this handle or 119 * if close_on_end has already been set: 120 * just set close on end and return -- the cleanup 121 * will be done when the outcall is finished. 122 * else 123 * wait on the outcall cv for the outcall to complete 124 * Proceed with cleanup 125 */ 126 (void) mutex_lock(&(hp->outcall_lock)); 127 if (hp->pending_outcall) { 128 /* end the consumer thread */ 129 /* this will also kill the producer thread and close net */ 130 hp->cancel = 1; 131 if (hp->q) { 132 if (slp_enqueue_at_head(hp->q, NULL) != SLP_OK) { 133 goto cleanup; 134 } 135 } 136 137 if (thr_self() == hp->consumer_tid || hp->close_on_end) { 138 /* SLPClose called from callback */ 139 hp->close_on_end = SLP_TRUE; 140 (void) mutex_unlock(&(hp->outcall_lock)); 141 return; 142 } 143 /* else not called from callback; wait for outcall to end */ 144 while (hp->pending_outcall) { 145 (void) cond_wait(&(hp->outcall_cv), &(hp->outcall_lock)); 146 } 147 } 148 (void) mutex_unlock(&(hp->outcall_lock)); 149 150 cleanup: 151 slp_cleanup_handle(hp); 152 } 153