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