Mercurial > illumos > onarm
annotate usr/src/cmd/iscsi/iscsitgtd/mgmt.c @ 4:1a15d5aaf794
synchronized with onnv_86 (6202) in onnv-gate
author | Koji Uno <koji.uno@sun.com> |
---|---|
date | Mon, 31 Aug 2009 14:38:03 +0900 |
parents | c9caec207d52 |
children |
rev | line source |
---|---|
0 | 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 (the "License"). | |
6 * You may not use this file except in compliance with the License. | |
7 * | |
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 * or http://www.opensolaris.org/os/licensing. | |
10 * See the License for the specific language governing permissions | |
11 * and limitations under the License. | |
12 * | |
13 * When distributing Covered Code, include this CDDL HEADER in each | |
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 * If applicable, add the following below this CDDL HEADER, with the | |
16 * fields enclosed by brackets "[]" replaced with your own identifying | |
17 * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 * | |
19 * CDDL HEADER END | |
20 */ | |
21 | |
22 /* | |
23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. | |
24 * Use is subject to license terms. | |
25 */ | |
26 | |
4
1a15d5aaf794
synchronized with onnv_86 (6202) in onnv-gate
Koji Uno <koji.uno@sun.com>
parents:
0
diff
changeset
|
27 #pragma ident "%Z%%M% %I% %E% SMI" |
0 | 28 |
29 #include <stdio.h> | |
30 #include <unistd.h> | |
31 #include <strings.h> | |
32 #include <stdlib.h> | |
33 #include <poll.h> | |
34 #include <sys/types.h> | |
35 #include <sys/socket.h> | |
36 #include <sys/select.h> | |
37 #include <netinet/in.h> | |
38 #include <sys/filio.h> | |
39 #include <utility.h> | |
40 #include <synch.h> | |
41 #include <sys/stropts.h> | |
42 #include <libxml/xmlreader.h> | |
43 #include <iscsitgt_impl.h> | |
44 | |
45 #include "queue.h" | |
46 #include "port.h" | |
47 #include "utility.h" | |
48 | |
49 static void | |
50 mgmt_monitor_queue(port_args_t *p) | |
51 { | |
52 target_queue_t *in = p->port_dataq; | |
53 msg_t *m; | |
54 int process = True; | |
55 char *data, | |
56 *output; | |
57 | |
58 do { | |
59 m = queue_message_get(in); | |
60 switch (m->msg_type) { | |
61 case msg_conn_lost: | |
62 process = False; | |
63 break; | |
64 | |
65 case msg_log: | |
66 data = (char *)m->msg_data; | |
67 output = NULL; | |
68 tgt_buf_add(&output, "log", data); | |
69 (void) write(p->port_socket, output, strlen(output)); | |
70 free(output); | |
71 break; | |
72 | |
73 case msg_mgmt_rply: | |
74 data = (char *)m->msg_data; | |
75 output = NULL; | |
76 tgt_buf_add(&output, "mgmt", data); | |
77 (void) write(p->port_socket, output, strlen(output)); | |
78 free(output); | |
79 free(data); | |
80 m->msg_data = NULL; | |
81 break; | |
82 | |
83 default: | |
84 break; | |
85 } | |
86 | |
87 if (m->msg_data) | |
88 free(m->msg_data); | |
89 queue_message_free(m); | |
90 | |
91 } while (process == True); | |
92 } | |
93 | |
94 static void * | |
95 mgmt_process(void *v) | |
96 { | |
97 port_args_t *p = (port_args_t *)v; | |
98 int nbytes, | |
99 nmsgs, | |
100 pval, | |
101 ret; | |
102 char *buf; | |
103 nfds_t nfds = 1; | |
104 struct pollfd fds[1]; | |
105 xmlTextReaderPtr r; | |
106 tgt_node_t *node = NULL; | |
107 mgmt_request_t m; | |
108 | |
109 fds[0].fd = p->port_socket; | |
110 fds[0].events = POLLIN; | |
111 | |
112 m.m_q = p->port_dataq; | |
113 m.m_request = mgmt_parse_xml; | |
114 m.m_time = time(NULL); | |
115 m.m_targ_name = NULL; | |
116 | |
117 while ((pval = poll(fds, nfds, -1)) != -1) { | |
118 if ((nmsgs = ioctl(p->port_socket, FIONREAD, &nbytes)) == -1) { | |
119 | |
120 queue_message_set(p->port_dataq, 0, msg_conn_lost, 0); | |
121 break; | |
122 | |
123 } else if ((nmsgs == 0) && (nbytes == 0)) { | |
124 | |
125 queue_message_set(p->port_dataq, 0, msg_conn_lost, 0); | |
126 break; | |
127 | |
128 } else if ((buf = malloc(nbytes)) == NULL) { | |
129 | |
130 queue_message_set(p->port_dataq, 0, msg_conn_lost, 0); | |
131 break; | |
132 | |
133 } else if (read(p->port_socket, buf, nbytes) != nbytes) { | |
134 | |
135 queue_message_set(p->port_dataq, 0, msg_conn_lost, 0); | |
136 break; | |
137 | |
138 } | |
139 | |
140 buf[nbytes] = '\0'; | |
141 r = (xmlTextReaderPtr)xmlReaderForMemory(buf, nbytes, | |
142 NULL, NULL, 0); | |
143 if (r != NULL) { | |
144 ret = xmlTextReaderRead(r); | |
145 while (ret == 1) { | |
146 if (tgt_node_process(r, &node) == False) | |
147 break; | |
148 ret = xmlTextReaderRead(r); | |
149 } | |
150 if (node != NULL) { | |
151 m.m_u.m_node = node; | |
152 queue_message_set(p->port_mgmtq, 0, | |
153 msg_mgmt_rqst, &m); | |
154 } | |
155 xmlFreeTextReader(r); | |
156 tgt_node_free(node); | |
157 node = NULL; | |
158 } | |
159 | |
160 } | |
161 | |
162 if (pval == -1) | |
163 queue_message_set(p->port_dataq, 0, msg_conn_lost, 0); | |
164 (void) close(p->port_socket); | |
165 p->port_socket = -1; | |
166 return (NULL); | |
167 } | |
168 | |
169 void * | |
170 port_management(void *v) | |
171 { | |
172 int s, | |
173 fd, | |
174 on = 1; | |
175 struct sockaddr_in sin_ip; | |
176 struct sockaddr_in6 sin6_ip; | |
177 socklen_t fromlen; | |
178 struct sockaddr_storage from; | |
179 port_args_t *p = (port_args_t *)v; | |
180 target_queue_t *q = p->port_mgmtq; | |
181 int l; | |
182 pthread_t junk; | |
183 char debug[80]; | |
184 | |
185 if ((s = socket(PF_INET6, SOCK_STREAM, 0)) == -1) { | |
186 if ((s = socket(PF_INET, SOCK_STREAM, 0)) == -1) { | |
187 queue_str(q, Q_GEN_ERRS, msg_status, | |
188 "Can't open socket"); | |
189 return (NULL); | |
190 } else { | |
191 | |
192 bzero(&sin_ip, sizeof (sin_ip)); | |
193 sin_ip.sin_family = AF_INET; | |
194 sin_ip.sin_port = htons(p->port_num); | |
195 sin_ip.sin_addr.s_addr = INADDR_ANY; | |
196 | |
197 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, | |
198 (char *)&on, sizeof (on)); | |
199 | |
200 if ((bind(s, (struct sockaddr *)&sin_ip, | |
201 sizeof (sin_ip))) < 0) { | |
202 (void) snprintf(debug, sizeof (debug), | |
203 "bind on port %d failed\n", p->port_num); | |
204 queue_str(q, Q_GEN_ERRS, msg_status, debug); | |
205 return (NULL); | |
206 } | |
207 } | |
208 } else { | |
209 | |
210 bzero(&sin6_ip, sizeof (sin6_ip)); | |
211 sin6_ip.sin6_family = AF_INET6; | |
212 sin6_ip.sin6_port = htons(p->port_num); | |
213 sin6_ip.sin6_addr = in6addr_any; | |
214 | |
215 (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, | |
216 sizeof (on)); | |
217 | |
218 if ((bind(s, (struct sockaddr *)&sin6_ip, sizeof (sin6_ip))) | |
219 < 0) { | |
220 (void) snprintf(debug, sizeof (debug), | |
221 "bind on port %d failed\n", | |
222 p->port_num); | |
223 queue_str(q, Q_GEN_ERRS, msg_status, debug); | |
224 return (NULL); | |
225 } | |
226 } | |
227 | |
228 if (listen(s, 5) < 0) { | |
229 queue_str(q, Q_GEN_ERRS, msg_status, "listen failed"); | |
230 return (NULL); | |
231 } | |
232 | |
233 /*CONSTANTCONDITION*/ | |
234 while (1) { | |
235 fromlen = sizeof (from); | |
236 if ((fd = accept(s, (struct sockaddr *)&from, | |
237 &fromlen)) < 0) { | |
238 queue_str(q, Q_GEN_ERRS, msg_status, "accept failed"); | |
239 return (NULL); | |
240 } | |
241 | |
242 l = 128 * 1024; | |
243 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *)&l, | |
244 sizeof (l)) < 0) | |
245 queue_str(q, Q_GEN_ERRS, msg_status, | |
246 "setsockopt failed"); | |
247 | |
248 if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *)&l, | |
249 sizeof (l)) < 0) | |
250 queue_str(q, Q_GEN_ERRS, msg_status, | |
251 "setsockopt failed"); | |
252 | |
253 | |
254 p->port_socket = fd; | |
255 (void) pthread_create(&junk, NULL, mgmt_process, p); | |
256 | |
257 mgmt_monitor_queue(p); | |
258 } | |
259 return (NULL); | |
260 } |