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