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