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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
|
/* $NetBSD: isns_util.c,v 1.2 2012/03/21 05:33:27 matt Exp $ */
/*-
* Copyright (c) 2004,2009 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Wasabi Systems, Inc.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/*
* isns_util.c
*/
#include <sys/cdefs.h>
__RCSID("$NetBSD: isns_util.c,v 1.2 2012/03/21 05:33:27 matt Exp $");
#include <sys/types.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "isns.h"
#include "isns_config.h"
#define ISNS_MAX_DISCONNECTS_PER_TRANS 3
int
isns_issue_cmd(struct isns_config_s *cfg_p, uint8_t cmd_type)
{
return (int)write(cfg_p->pipe_fds[1], &cmd_type, 1);
}
int
isns_issue_cmd_with_data(struct isns_config_s *cfg_p, uint8_t cmd_type,
uint8_t *data_p, int data_len)
{
struct iovec iov[2];
iov[0].iov_base = &cmd_type;
iov[0].iov_len = 1;
iov[1].iov_base = data_p;
iov[1].iov_len = data_len;
return (int)isns_file_writev(cfg_p->pipe_fds[1], iov, 2);
}
int
isns_change_kevent_list(struct isns_config_s *cfg_p,
uintptr_t ident, uint32_t filter, uint32_t flags, int64_t data, intptr_t udata)
{
struct kevent evt;
EV_SET(&evt, ident, filter, flags, 0, data, udata);
return kevent(cfg_p->kq, &evt, 1, NULL, 0, NULL);
}
struct isns_config_s *
isns_new_config(void)
{
struct isns_config_s *cfg_p;
pthread_mutexattr_t mutexattr;
cfg_p = (struct isns_config_s *)
isns_malloc(sizeof(struct isns_config_s));
if (cfg_p == NULL) {
DBG("isns_new_config: error on isns_malloc() [1]\n");
return NULL;
}
cfg_p->kq = -1;
cfg_p->pipe_fds[0] = -1;
cfg_p->pipe_fds[1] = -1;
cfg_p->curtask_p = NULL;
cfg_p->sd_connected = 0;
cfg_p->ai_p = NULL;
cfg_p->pdu_in_p = NULL;
cfg_p->refresh_p = NULL;
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, ISNS_MUTEX_TYPE_NORMAL);
if (pthread_mutex_init(&cfg_p->taskq_mutex, &mutexattr) != 0) {
DBG("isns_new_config: error on pthread_mutex_init() [1]\n");
isns_free(cfg_p);
return NULL;
}
pthread_mutexattr_init(&mutexattr);
pthread_mutexattr_settype(&mutexattr, ISNS_MUTEX_TYPE_NORMAL);
if (pthread_mutex_init(&cfg_p->trans_mutex, &mutexattr) != 0) {
DBG("isns_new_config: error on pthread_mutex_init() [2]\n");
pthread_mutex_destroy(&cfg_p->taskq_mutex);
isns_free(cfg_p);
return NULL;
}
SIMPLEQ_INIT(&cfg_p->taskq_head);
cfg_p->control_thread_p = (pthread_t *)isns_malloc(sizeof(pthread_t));
if (cfg_p->control_thread_p == NULL) {
DBG("isns_new_config: error on isns_malloc() [1]\n");
isns_destroy_config(cfg_p);
return NULL;
}
return cfg_p;
}
void
isns_destroy_config(struct isns_config_s *cfg_p)
{
struct isns_task_s *task_p;
if (cfg_p != NULL) {
if (cfg_p->kq != -1)
close(cfg_p->kq);
if (cfg_p->pipe_fds[0] != -1)
close(cfg_p->pipe_fds[0]);
if (cfg_p->pipe_fds[1] != -1)
close(cfg_p->pipe_fds[1]);
if (cfg_p->control_thread_p != NULL)
isns_free(cfg_p->control_thread_p);
if (cfg_p->refresh_p != NULL) {
if (cfg_p->refresh_p->trans_p != NULL)
isns_free_trans(cfg_p->refresh_p->trans_p);
isns_free(cfg_p->refresh_p);
}
/* Free the current task, if necessary. */
if ((task_p = cfg_p->curtask_p) != NULL) {
if (task_p->task_type == ISNS_TASK_SEND_PDU)
isns_complete_trans(task_p->var.send_pdu.trans_p);
isns_free_task(task_p);
}
/* Empty the task queue of any pending tasks and free them. */
while ((task_p = isns_taskq_remove(cfg_p)) != NULL) {
if (task_p->task_type == ISNS_TASK_SEND_PDU)
isns_complete_trans(task_p->var.send_pdu.trans_p);
isns_free_task(task_p);
}
pthread_mutex_destroy(&cfg_p->taskq_mutex);
pthread_mutex_destroy(&cfg_p->trans_mutex);
if (cfg_p->ai_p != NULL) {
if (cfg_p->ai_p->ai_canonname != NULL)
isns_free(cfg_p->ai_p->ai_canonname);
if (cfg_p->ai_p->ai_addr != NULL)
isns_free(cfg_p->ai_p->ai_addr);
isns_free(cfg_p->ai_p);
}
isns_free(cfg_p);
}
}
/*
* isns_thread_create()
*/
int
isns_thread_create(struct isns_config_s *cfg_p)
{
char namebuf[ISNS_THREAD_MAX_NAMELEN];
int error;
pthread_attr_t attr;
DBG("isns_thread_create: entered\n");
strcpy(namebuf, "isns_control");
error = pthread_attr_init(&attr);
if (error != 0) {
DBG("isns_thread_create: error on pthread_threadattr_init\n");
return error;
}
error = pthread_attr_setname_np(&attr, namebuf, NULL);
if (error != 0) {
DBG("isns_thread_create: "
"error on pthread_threadattr_setname\n");
pthread_attr_destroy(&attr);
return error;
}
error = pthread_create(cfg_p->control_thread_p,
&attr, isns_control_thread, cfg_p);
pthread_attr_destroy(&attr);
if (error != 0) {
DBG("isns_thread_create: error on pthread_thread_create\n");
return error;
}
return error;
}
/*
* isns_thread_destroy()
*/
void
isns_thread_destroy(struct isns_config_s *cfg_p)
{
int error;
void *rv;
DBG("isns_thread_destroy: entered\n");
if ((cfg_p == NULL) || (cfg_p->control_thread_p == NULL))
return;
DBG("isns_thread_destroy: about to wait (join) on thread\n");
error = pthread_join(*cfg_p->control_thread_p, &rv);
if (error) {
DBG("isns_thread_destroy: error on pthread_thread_join\n");
return;
}
DBG("isns_thread_destroy: done waiting on thread\n");
}
/*
*
*/
void
isns_process_connection_loss(struct isns_config_s *cfg_p)
{
struct isns_trans_s *trans_p;
struct isns_pdu_s *pdu_p, *free_pdu_p;
DBG("isns_process_connection_loss: entered\n");
if (cfg_p->curtask_p != NULL) {
trans_p = cfg_p->curtask_p->var.send_pdu.trans_p;
if (trans_p->disconnect_cnt == ISNS_MAX_DISCONNECTS_PER_TRANS) {
isns_complete_trans(trans_p);
isns_end_task(cfg_p->curtask_p);
if (cfg_p->pdu_in_p != NULL) {
isns_free_pdu(cfg_p->pdu_in_p);
cfg_p->pdu_in_p = NULL;
}
} else {
trans_p->disconnect_cnt++;
if (trans_p->pdu_rsp_list != NULL) {
pdu_p = trans_p->pdu_rsp_list;
while (pdu_p != NULL) {
free_pdu_p = pdu_p;
pdu_p = pdu_p->next;
isns_free_pdu(free_pdu_p);
}
}
isns_taskq_insert_head(cfg_p, cfg_p->curtask_p);
cfg_p->curtask_p = NULL;
isns_issue_cmd(cfg_p, ISNS_CMD_PROCESS_TASKQ);
}
}
}
|