xref: /illumos-gate/usr/src/lib/libslp/clib/SLPOpen.c (revision 88f8b78a88cbdc6d8c1af5c3e54bc49d25095c98)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <thread.h>
32 #include <synch.h>
33 #include <syslog.h>
34 #include <arpa/inet.h>
35 #include <slp-internal.h>
36 #include <slp_net_utils.h>
37 
38 SLPError SLPOpen(const char *pcLang, SLPBoolean isAsync, SLPHandle *phSLP) {
39 	slp_handle_impl_t *hp;
40 
41 	if (!pcLang || !phSLP) {
42 		return (SLP_PARAMETER_BAD);
43 	}
44 
45 	/* allocate the handle */
46 	if (!(hp = malloc(sizeof (*hp)))) {
47 		slp_err(LOG_CRIT, 0, "SLPOpen", "out of memory");
48 		return (SLP_MEMORY_ALLOC_FAILED);
49 	}
50 
51 	/* initialize outcall synchronization */
52 	hp->pending_outcall = SLP_FALSE;
53 	(void) mutex_init(&(hp->outcall_lock), NULL, NULL);
54 	(void) cond_init(&(hp->outcall_cv), NULL, NULL);
55 	hp->close_on_end = SLP_FALSE;
56 	hp->consumer_tid = 0;
57 
58 	/* locale property overrides argument */
59 	if (!(hp->locale = SLPGetProperty(SLP_CONFIG_LOCALE))) {
60 		hp->locale = pcLang;
61 	}
62 	/* Make sure the language string is under our ownership */
63 	if (!(hp->locale = strdup(hp->locale))) {
64 		free(hp);
65 		slp_err(LOG_CRIT, 0, "SLPOpen", "out of memory");
66 		return (SLP_MEMORY_ALLOC_FAILED);
67 	}
68 
69 	hp->cancel = 0;
70 
71 	/* Asynchronous operation? */
72 	if (isAsync)
73 		hp->async = SLP_TRUE;
74 	else
75 		hp->async = SLP_FALSE;
76 
77 	/* TCP vars -- these are NULL until actually needed */
78 	hp->tcp_lock = NULL;
79 	hp->tcp_wait = NULL;
80 	hp->tcp_ref_cnt = 0;
81 
82 	/* Consumer / Producer pipe */
83 	hp->q = NULL;
84 
85 	/* Interface info, loaded on demand */
86 	hp->ifinfo = NULL;
87 
88 	/* force multicast, false by default */
89 	hp->force_multicast = SLP_FALSE;
90 
91 	/* internal call, false by default */
92 	hp->internal_call = SLP_FALSE;
93 
94 	*phSLP = hp;
95 	return (SLP_OK);
96 }
97 
98 void slp_cleanup_handle(slp_handle_impl_t *hp) {
99 	/* free the handle */
100 	if (hp->tcp_lock) free(hp->tcp_lock);
101 	if (hp->tcp_wait) free(hp->tcp_wait);
102 	if (hp->ifinfo) {
103 		slp_free_ifinfo(hp->ifinfo);
104 		free(hp->ifinfo);
105 	}
106 	free((void *) hp->locale);
107 	free(hp);
108 }
109 
110 void SLPClose(SLPHandle hSLP) {
111 	slp_handle_impl_t *hp = (slp_handle_impl_t *)hSLP;
112 
113 	if (!hSLP) {
114 		return;
115 	}
116 
117 	/*
118 	 * If an outcall is pending on this handle:
119 	 *   If we are being called from a callback resulting
120 	 *   from the outcall associated with this handle or
121 	 *   if close_on_end has already been set:
122 	 *	just set close on end and return -- the cleanup
123 	 *	will be done when the outcall is finished.
124 	 *   else
125 	 *	wait on the outcall cv for the outcall to complete
126 	 * Proceed with cleanup
127 	 */
128 	(void) mutex_lock(&(hp->outcall_lock));
129 	if (hp->pending_outcall) {
130 	    /* end the consumer thread */
131 	    /* this will also kill the producer thread and close net */
132 	    hp->cancel = 1;
133 	    if (hp->q) {
134 		if (slp_enqueue_at_head(hp->q, NULL) != SLP_OK) {
135 		    goto cleanup;
136 		}
137 	    }
138 
139 	    if (thr_self() == hp->consumer_tid || hp->close_on_end) {
140 		/* SLPClose called from callback */
141 		hp->close_on_end = SLP_TRUE;
142 		(void) mutex_unlock(&(hp->outcall_lock));
143 		return;
144 	    }
145 	    /* else not called from callback; wait for outcall to end */
146 	    while (hp->pending_outcall) {
147 		(void) cond_wait(&(hp->outcall_cv), &(hp->outcall_lock));
148 	    }
149 	}
150 	(void) mutex_unlock(&(hp->outcall_lock));
151 
152 cleanup:
153 	slp_cleanup_handle(hp);
154 }
155