author | Andrzej Szeszo <aszeszo@gmail.com> |
Wed, 01 Jun 2011 13:04:21 +0100 | |
changeset 2389 | 30ea7b982e91 |
parent 2349 | 4b4ff1617157 |
permissions | -rw-r--r-- |
2338 | 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 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved. |
|
23 |
*/ |
|
24 |
||
25 |
/* |
|
26 |
* The zone proxy daemon and zone proxy client. |
|
27 |
* |
|
28 |
* For package operations in zones, the system must make available a certain |
|
29 |
* group of publishers and repositories to client zones. This ensures that data |
|
30 |
* necessary for installing or updating a zone is always available to zones |
|
31 |
* consumers, regardless of the exact network configuration within the local |
|
32 |
* zone. In order to accomplish this, the proxy daemon and proxy client provide |
|
33 |
* a TCP proxy to a special repository that is maintained in the global zone. |
|
34 |
* |
|
35 |
* The zone-proxy client is responsible for creating a listening TCP socket in a |
|
36 |
* pre-determined location, and then passing control of that socket to the proxy |
|
37 |
* daemon. Once the proxy client has completed this hand-off, it sleeps in the |
|
38 |
* local zone, waiting for notification of any changes in the global zone. If |
|
39 |
* the proxy daemon exits, or is re-configured, the proxy client creates a new |
|
40 |
* socket, and the process is repeated. |
|
41 |
* |
|
42 |
* The proxy daemon listens on the sockets passed to it by the proxy client, and |
|
43 |
* when it gets a new connection, establishes a connection to the zones |
|
44 |
* repository that serves packaging information. The proxy daemon and client |
|
45 |
* pass information through a door. The daemon also listens for notifications |
|
46 |
* about zone startup and shutdown on the door. (zoneadmd, knows to poke the |
|
47 |
* daemon when zones are created or destroyed). When a zone is created, the |
|
48 |
* proxy daemon enters the zone, and creates a new door there, so that the |
|
49 |
* client and daemon can rendezvous. The proxy daemon manages a pool of thread |
|
50 |
* workers for handling network connections, and has some door callbacks to |
|
51 |
* manage a pool of IPC threads. |
|
52 |
* |
|
53 |
* Each new connection generates a pair of sockets. The data transfer algorithm |
|
54 |
* here is lockless, and depends upon event ports as the polling mechanism. The |
|
55 |
* socket is dup'd, and one is always used for reading, and the other always |
|
56 |
* used for writing. As long as no thread reads and writes the same fd, |
|
57 |
* operation is atomic, and correct. When a thread needs data, the event |
|
58 |
* mechanism is used either to wait for data, or to wait to write data. |
|
59 |
* Although each proxy connection has a buffer, we try our best to drain that |
|
60 |
* buffer ASAP, especially before getting more data. |
|
61 |
*/ |
|
62 |
#include <alloca.h> |
|
63 |
#include <atomic.h> |
|
64 |
#include <door.h> |
|
65 |
#include <errno.h> |
|
66 |
#include <fcntl.h> |
|
67 |
#include <libcontract.h> |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
68 |
#include <libscf.h> |
2338 | 69 |
#include <limits.h> |
70 |
#include <netdb.h> |
|
71 |
#include <port.h> |
|
72 |
#include <priv.h> |
|
73 |
#include <pthread.h> |
|
74 |
#include <synch.h> |
|
75 |
#include <signal.h> |
|
76 |
#include <stddef.h> |
|
77 |
#include <stdio.h> |
|
78 |
#include <stdlib.h> |
|
79 |
#include <string.h> |
|
80 |
#include <stropts.h> |
|
81 |
#include <thread.h> |
|
82 |
#include <ucred.h> |
|
83 |
#include <unistd.h> |
|
84 |
#include <zone.h> |
|
85 |
#include <zoneproxy_impl.h> |
|
86 |
#include <sys/ctfs.h> |
|
87 |
#include <sys/contract/process.h> |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
88 |
#include <sys/queue.h> |
2338 | 89 |
#include <sys/resource.h> |
90 |
#include <sys/socket.h> |
|
91 |
#include <sys/stat.h> |
|
92 |
#include <sys/sysmacros.h> |
|
93 |
#include <sys/types.h> |
|
94 |
#include <sys/wait.h> |
|
95 |
||
96 |
#define PROXY_THREAD_DEFAULT 8 |
|
97 |
#define PROXY_THREAD_MAX 20 |
|
98 |
#define DOOR_THREAD_MAX 5 |
|
99 |
#define DEFAULT_LOCK_ALIGN 64 |
|
100 |
#define DEFAULT_TIMEOUT 30 |
|
101 |
#define TIMEOUT_COUNT 4 |
|
102 |
#define MAX_FDS_DEFAULT 6000 |
|
103 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
104 |
#define SYSREPO_FMRI "svc:/application/pkg/system-repository:default" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
105 |
#define SYSREPO_PG "config" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
106 |
#define SYSREPO_HOST "host" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
107 |
#define SYSREPO_PORT "port" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
108 |
#define DEFAULT_HOST "127.0.0.1" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
109 |
#define DEFAULT_PORT "1008" |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
110 |
|
2338 | 111 |
#define BUFFER_SIZ 8168 |
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
112 |
#define CONF_STR_SZ 2048 |
2338 | 113 |
|
114 |
typedef enum { |
|
115 |
PROXY_USER_GENERIC = 0, |
|
116 |
PROXY_USER_LISTENER, |
|
117 |
PROXY_USER_PAIR, |
|
118 |
PROXY_USER_END |
|
119 |
} pu_type_t; |
|
120 |
||
121 |
typedef enum { |
|
122 |
PROXY_STATE_INIT = 0, |
|
123 |
PROXY_STATE_WAIT_CONNECT, |
|
124 |
PROXY_STATE_WAIT_DATA, |
|
125 |
PROXY_STATE_CLOSING, |
|
126 |
PROXY_STATE_FREED, |
|
127 |
PROXY_STATE_END |
|
128 |
} proxy_state_t; |
|
129 |
||
130 |
/* structure definitions */ |
|
131 |
||
132 |
struct proxy_user { |
|
133 |
pu_type_t pu_type; |
|
134 |
void (*pu_callback)(struct proxy_user *, port_event_t *); |
|
135 |
}; |
|
136 |
||
137 |
struct proxy_listener { |
|
138 |
pu_type_t pl_type; |
|
139 |
void (*pl_callback)(struct proxy_listener *, port_event_t *); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
140 |
TAILQ_ENTRY(proxy_listener) pl_list_link; |
2338 | 141 |
zoneid_t pl_zid; |
142 |
int pl_fd; |
|
143 |
mutex_t pl_lock; |
|
144 |
boolean_t pl_cleanup; |
|
145 |
int pl_pipefd; |
|
146 |
int pl_closefd; |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
147 |
char *pl_proxy_host; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
148 |
char *pl_proxy_port; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
149 |
uint64_t pl_gen; |
2338 | 150 |
}; |
151 |
||
152 |
struct proxy_pair { |
|
153 |
pu_type_t pp_type; |
|
154 |
void (*pp_callback)(struct proxy_pair *, port_event_t *); |
|
155 |
int pp_readfd; |
|
156 |
int pp_writefd; |
|
157 |
size_t pp_fbcnt; |
|
158 |
proxy_state_t pp_state; |
|
159 |
char pp_buffer[BUFFER_SIZ]; |
|
160 |
}; |
|
161 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
162 |
struct proxy_config { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
163 |
mutex_t pc_lock; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
164 |
scf_handle_t *pc_hdl; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
165 |
scf_instance_t *pc_inst; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
166 |
scf_propertygroup_t *pc_pg; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
167 |
scf_property_t *pc_prop; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
168 |
scf_value_t *pc_val; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
169 |
char *pc_proxy_host; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
170 |
char *pc_proxy_port; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
171 |
uint64_t pc_gen; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
172 |
}; |
2338 | 173 |
|
174 |
/* global variables */ |
|
175 |
int g_port; |
|
176 |
int g_door; |
|
177 |
int g_pipe_fd; |
|
178 |
int g_max_door_thread = DOOR_THREAD_MAX; |
|
179 |
int g_door_thread_count = 0; |
|
180 |
uint_t g_proxy_pair_count = 0; |
|
181 |
thread_key_t g_thr_info_key; |
|
182 |
mutex_t *g_door_thr_lock; |
|
183 |
mutex_t *g_listener_lock; |
|
184 |
mutex_t *g_thr_pool_lock; |
|
185 |
||
186 |
/* global variables protected by g_thr_pool_lock */ |
|
187 |
int g_tp_running_threads; |
|
188 |
int g_tp_exited_threads; |
|
189 |
int g_tp_max_threads = PROXY_THREAD_MAX; |
|
190 |
int g_tp_min_threads = PROXY_THREAD_DEFAULT; |
|
191 |
cond_t g_thr_pool_cv = DEFAULTCV; |
|
192 |
||
193 |
/* global variables shared between main thread and s_handler thread */ |
|
194 |
mutex_t g_quit_lock = DEFAULTMUTEX; |
|
195 |
cond_t g_quit_cv = DEFAULTCV; |
|
196 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
197 |
/* proxy config protected by internal lock */ |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
198 |
struct proxy_config *g_proxy_config; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
199 |
boolean_t g_config_smf; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
200 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
201 |
TAILQ_HEAD(zq_queuehead, proxy_listener); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
202 |
struct zq_queuehead zone_listener_list; |
2338 | 203 |
static volatile boolean_t g_quit; |
204 |
||
205 |
/* function declarations */ |
|
206 |
static struct proxy_listener *alloc_proxy_listener(void); |
|
207 |
static struct proxy_pair *alloc_proxy_pair(void); |
|
208 |
static int check_connect(struct proxy_pair *); |
|
209 |
static int clone_and_register(struct proxy_pair *); |
|
210 |
static void close_door_descs(door_desc_t *, uint_t); |
|
211 |
static int close_on_exec(int); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
212 |
static struct proxy_config *config_alloc(void); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
213 |
static void config_free(struct proxy_config *); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
214 |
static int config_read(struct proxy_config *); |
2338 | 215 |
static int contract_abandon_id(ctid_t); |
216 |
static int contract_latest(ctid_t *); |
|
217 |
static int contract_open(ctid_t, const char *, const char *, int); |
|
218 |
static void daemonize_ready(char); |
|
219 |
static int daemonize_start(void); |
|
220 |
static int do_fattach(int, char *, boolean_t); |
|
221 |
static void drop_privs(void); |
|
222 |
static void escalate_privs(void); |
|
223 |
static void fattach_all_zones(boolean_t); |
|
224 |
static void free_proxy_listener(struct proxy_listener *); |
|
225 |
static void free_proxy_pair(struct proxy_pair *); |
|
226 |
static int init_template(void); |
|
227 |
static void listen_func(struct proxy_listener *, port_event_t *); |
|
228 |
static void proxy_func(struct proxy_pair *, port_event_t *); |
|
229 |
static void *proxy_thread_loop(void *); |
|
230 |
static void s_handler(void); |
|
231 |
static int send_recv_data(struct proxy_pair *); |
|
232 |
static void shutdown_proxypair(struct proxy_pair *); |
|
233 |
static void thread_exiting(void *); |
|
234 |
static void *thread_manager(void *); |
|
235 |
static void usage(void); |
|
236 |
static int zpd_add_listener(zoneid_t, int, int, int); |
|
237 |
static void zpd_door_create_thread(door_info_t *); |
|
238 |
static void *zpd_door_loop(void *); |
|
239 |
static void zpd_door_server(void *, char *, size_t, door_desc_t *, uint_t); |
|
240 |
static void zpd_fattach_zone(zoneid_t, int, boolean_t); |
|
241 |
static struct proxy_listener *zpd_find_listener(zoneid_t); |
|
242 |
static void zpd_listener_cleanup(struct proxy_listener *); |
|
243 |
static int zpd_perm_check(int, zoneid_t); |
|
244 |
static void zpd_remove_listener(struct proxy_listener *); |
|
245 |
static int zpd_remove_zone(zoneid_t); |
|
246 |
||
247 |
static void |
|
248 |
usage(void) |
|
249 |
{ |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
250 |
(void) fprintf(stderr, "Usage: zoneproxyd [-s host:port]\n"); |
2338 | 251 |
exit(EXIT_FAILURE); |
252 |
} |
|
253 |
||
254 |
static int |
|
255 |
set_noblocking(int fd) |
|
256 |
{ |
|
257 |
int flags; |
|
258 |
||
259 |
if ((flags = fcntl(fd, F_GETFL, 0)) < 0) { |
|
260 |
perror("fcntl (GETFL)"); |
|
261 |
return (-1); |
|
262 |
} |
|
263 |
||
264 |
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0) { |
|
265 |
perror("fcntl (SETFL)"); |
|
266 |
return (-1); |
|
267 |
} |
|
268 |
||
269 |
return (0); |
|
270 |
} |
|
271 |
||
272 |
static struct proxy_listener * |
|
273 |
alloc_proxy_listener(void) |
|
274 |
{ |
|
275 |
struct proxy_listener *listener; |
|
276 |
||
277 |
listener = malloc(sizeof (struct proxy_listener)); |
|
278 |
if (listener == NULL) { |
|
279 |
perror("malloc"); |
|
280 |
return (NULL); |
|
281 |
} |
|
282 |
(void) memset(listener, 0, sizeof (struct proxy_listener)); |
|
283 |
listener->pl_type = PROXY_USER_LISTENER; |
|
284 |
listener->pl_callback = listen_func; |
|
285 |
listener->pl_fd = -1; |
|
286 |
listener->pl_pipefd = -1; |
|
287 |
listener->pl_closefd = -1; |
|
288 |
listener->pl_cleanup = B_FALSE; |
|
289 |
if (mutex_init(&listener->pl_lock, USYNC_THREAD, NULL) < 0) { |
|
290 |
perror("mutex_init"); |
|
291 |
return (NULL); |
|
292 |
} |
|
293 |
||
294 |
return (listener); |
|
295 |
} |
|
296 |
||
297 |
||
298 |
static struct proxy_pair * |
|
299 |
alloc_proxy_pair(void) |
|
300 |
{ |
|
301 |
struct proxy_pair *pair; |
|
302 |
||
303 |
pair = malloc(sizeof (struct proxy_pair)); |
|
304 |
if (pair == NULL) { |
|
305 |
perror("malloc"); |
|
306 |
return (NULL); |
|
307 |
} |
|
308 |
(void) memset(pair, 0, sizeof (struct proxy_pair)); |
|
309 |
pair->pp_type = PROXY_USER_PAIR; |
|
310 |
pair->pp_callback = proxy_func; |
|
311 |
pair->pp_readfd = -1; |
|
312 |
pair->pp_writefd = -1; |
|
313 |
||
314 |
return (pair); |
|
315 |
} |
|
316 |
||
317 |
static void |
|
318 |
free_proxy_listener(struct proxy_listener *listener) |
|
319 |
{ |
|
320 |
if (listener->pl_fd > -1) { |
|
321 |
if (close(listener->pl_fd) < 0) { |
|
322 |
perror("close"); |
|
323 |
exit(EXIT_FAILURE); |
|
324 |
} |
|
325 |
} |
|
326 |
if (listener->pl_pipefd > -1) { |
|
327 |
if (close(listener->pl_pipefd) < 0) { |
|
328 |
perror("close"); |
|
329 |
exit(EXIT_FAILURE); |
|
330 |
} |
|
331 |
} |
|
332 |
if (listener->pl_closefd > -1) { |
|
333 |
if (close(listener->pl_closefd) < 0) { |
|
334 |
perror("close"); |
|
335 |
exit(EXIT_FAILURE); |
|
336 |
} |
|
337 |
} |
|
338 |
(void) mutex_destroy(&listener->pl_lock); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
339 |
free(listener->pl_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
340 |
free(listener->pl_proxy_port); |
2338 | 341 |
free(listener); |
342 |
} |
|
343 |
||
344 |
static void |
|
345 |
free_proxy_pair(struct proxy_pair *pair) |
|
346 |
{ |
|
347 |
if (pair->pp_readfd > -1) { |
|
348 |
if (close(pair->pp_readfd) < 0) { |
|
349 |
perror("close"); |
|
350 |
exit(EXIT_FAILURE); |
|
351 |
} |
|
352 |
} |
|
353 |
if (pair->pp_writefd > -1) { |
|
354 |
if (close(pair->pp_writefd) < 0) { |
|
355 |
perror("close"); |
|
356 |
exit(EXIT_FAILURE); |
|
357 |
} |
|
358 |
} |
|
359 |
pair->pp_state = PROXY_STATE_FREED; |
|
360 |
free(pair); |
|
361 |
} |
|
362 |
||
363 |
/* |
|
364 |
* Once a pair has been connected, dup the file descriptors, switching read and |
|
365 |
* write, so that both pair can be seperately queued for events. |
|
366 |
*/ |
|
367 |
static int |
|
368 |
clone_and_register(struct proxy_pair *pair) |
|
369 |
{ |
|
370 |
struct proxy_pair *op_pair; |
|
371 |
int fd; |
|
372 |
||
373 |
/* Allocate another proxy_pair object */ |
|
374 |
op_pair = alloc_proxy_pair(); |
|
375 |
if (op_pair == NULL) { |
|
376 |
return (-1); |
|
377 |
} |
|
378 |
||
379 |
/* Copy state */ |
|
380 |
op_pair->pp_state = pair->pp_state; |
|
381 |
||
382 |
/* Dup fd's switching read for write */ |
|
383 |
if ((fd = dup(pair->pp_readfd)) < 0) { |
|
384 |
perror("dup"); |
|
385 |
free_proxy_pair(op_pair); |
|
386 |
return (-1); |
|
387 |
} |
|
388 |
||
389 |
op_pair->pp_writefd = fd; |
|
390 |
||
391 |
if ((fd = dup(pair->pp_writefd)) < 0) { |
|
392 |
perror("dup"); |
|
393 |
free_proxy_pair(op_pair); |
|
394 |
return (-1); |
|
395 |
} |
|
396 |
||
397 |
op_pair->pp_readfd = fd; |
|
398 |
||
399 |
/* Register each pair to wait for input */ |
|
400 |
if (port_associate(g_port, PORT_SOURCE_FD, op_pair->pp_readfd, |
|
401 |
POLLIN, op_pair) < 0) { |
|
402 |
perror("port_associate"); |
|
403 |
free_proxy_pair(op_pair); |
|
404 |
return (-1); |
|
405 |
} |
|
406 |
||
407 |
if (port_associate(g_port, PORT_SOURCE_FD, pair->pp_readfd, |
|
408 |
POLLIN, pair) < 0) { |
|
409 |
perror("port_associate"); |
|
410 |
return (-1); |
|
411 |
} |
|
412 |
||
413 |
/* |
|
414 |
* Increment the proxy count by two, since there are two proxy-pair |
|
415 |
* objects per connection, each representing one direction of the flow. |
|
416 |
* The objects are shutdown separately, so each will decrment the |
|
417 |
* count by one in its shutdown method. |
|
418 |
*/ |
|
419 |
atomic_add_int(&g_proxy_pair_count, 2); |
|
420 |
/* |
|
421 |
* Try to poke the thread manager. If someone else is poking him, or |
|
422 |
* he's already running, just return. In the worst case the manager |
|
423 |
* will double-check the number of threads after the timeout. |
|
424 |
*/ |
|
425 |
if (mutex_trylock(g_thr_pool_lock) == 0) { |
|
426 |
(void) cond_signal(&g_thr_pool_cv); |
|
427 |
(void) mutex_unlock(g_thr_pool_lock); |
|
428 |
} |
|
429 |
||
430 |
return (0); |
|
431 |
} |
|
432 |
||
433 |
static void |
|
434 |
shutdown_proxypair(struct proxy_pair *pair) |
|
435 |
{ |
|
436 |
(void) port_dissociate(g_port, PORT_SOURCE_FD, pair->pp_readfd); |
|
437 |
(void) port_dissociate(g_port, PORT_SOURCE_FD, pair->pp_writefd); |
|
438 |
||
439 |
if (pair->pp_fbcnt > 0) |
|
440 |
(void) write(pair->pp_writefd, pair->pp_buffer, pair->pp_fbcnt); |
|
441 |
||
442 |
(void) shutdown(pair->pp_readfd, SHUT_RD); |
|
443 |
(void) shutdown(pair->pp_writefd, SHUT_WR); |
|
444 |
free_proxy_pair(pair); |
|
445 |
atomic_dec_uint(&g_proxy_pair_count); |
|
446 |
} |
|
447 |
||
448 |
static int |
|
449 |
send_recv_data(struct proxy_pair *pair) |
|
450 |
{ |
|
451 |
int b_wr; |
|
452 |
int b_rd; |
|
453 |
int read_needed = 0; |
|
454 |
int write_needed = 0; |
|
455 |
||
456 |
if (pair->pp_fbcnt == 0) /* need to read */ { |
|
457 |
b_rd = read(pair->pp_readfd, pair->pp_buffer, BUFFER_SIZ); |
|
458 |
if (b_rd < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || |
|
459 |
errno == EINTR)) { |
|
460 |
b_rd = 0; |
|
461 |
read_needed = 1; |
|
462 |
} else if (b_rd <= 0) { |
|
463 |
return (-1); |
|
464 |
} |
|
465 |
pair->pp_fbcnt = b_rd; |
|
466 |
} |
|
467 |
||
468 |
if (pair->pp_fbcnt > 0) { |
|
469 |
b_wr = write(pair->pp_writefd, pair->pp_buffer, pair->pp_fbcnt); |
|
470 |
||
471 |
if (b_wr < 0) { |
|
472 |
if (errno != EAGAIN && errno != EWOULDBLOCK) { |
|
473 |
return (-1); |
|
474 |
} |
|
475 |
b_wr = 0; |
|
476 |
} |
|
477 |
||
478 |
if (b_wr < pair->pp_fbcnt) { |
|
479 |
if (b_wr != 0) { |
|
480 |
(void) memmove(pair->pp_buffer, |
|
481 |
pair->pp_buffer + b_wr, |
|
482 |
pair->pp_fbcnt - b_wr); |
|
483 |
pair->pp_fbcnt -= b_wr; |
|
484 |
} |
|
485 |
write_needed = 1; |
|
486 |
/* If the write side is slow, disable read here */ |
|
487 |
read_needed = 0; |
|
488 |
} else { |
|
489 |
pair->pp_fbcnt = 0; |
|
490 |
read_needed = 1; |
|
491 |
} |
|
492 |
} |
|
493 |
||
494 |
if (read_needed) { |
|
495 |
if (port_associate(g_port, PORT_SOURCE_FD, pair->pp_readfd, |
|
496 |
POLLIN, pair) < 0) { |
|
497 |
perror("port_associate"); |
|
498 |
return (-1); |
|
499 |
} |
|
500 |
} |
|
501 |
||
502 |
if (write_needed) { |
|
503 |
if (port_associate(g_port, PORT_SOURCE_FD, pair->pp_writefd, |
|
504 |
POLLOUT, pair) < 0) { |
|
505 |
perror("port_associate"); |
|
506 |
return (-1); |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
return (0); |
|
511 |
||
512 |
} |
|
513 |
||
514 |
static void * |
|
515 |
proxy_thread_loop(void *arg) |
|
516 |
{ |
|
517 |
port_event_t ev; |
|
518 |
struct proxy_user *pu; |
|
519 |
int timeouts = 0; |
|
520 |
timespec_t tmot; |
|
521 |
boolean_t timed_out = B_FALSE; |
|
522 |
boolean_t should_exit = B_FALSE; |
|
523 |
||
524 |
for (;;) { |
|
525 |
tmot.tv_sec = DEFAULT_TIMEOUT; |
|
526 |
tmot.tv_nsec = 0; |
|
527 |
||
528 |
if (port_get(g_port, &ev, &tmot) < 0) { |
|
529 |
if (errno == ETIME) { |
|
530 |
timed_out = B_TRUE; |
|
531 |
timeouts++; |
|
532 |
} else { |
|
533 |
/* |
|
534 |
* Unexpected error. Adjust thread |
|
535 |
* bean counters and exit. |
|
536 |
*/ |
|
537 |
(void) mutex_lock(g_thr_pool_lock); |
|
538 |
g_tp_exited_threads++; |
|
539 |
g_tp_running_threads--; |
|
540 |
(void) cond_signal(&g_thr_pool_cv); |
|
541 |
(void) mutex_unlock(g_thr_pool_lock); |
|
542 |
perror("port_get"); |
|
543 |
thr_exit(NULL); |
|
544 |
} |
|
545 |
} else { |
|
546 |
timeouts = 0; |
|
547 |
} |
|
548 |
||
549 |
/* |
|
550 |
* Reached timeout count. Check to see if thread |
|
551 |
* should exit. |
|
552 |
*/ |
|
553 |
if (timed_out && timeouts > TIMEOUT_COUNT) { |
|
554 |
(void) mutex_lock(g_thr_pool_lock); |
|
555 |
if ((g_proxy_pair_count < g_tp_running_threads) && |
|
556 |
(g_tp_running_threads > g_tp_min_threads)) { |
|
557 |
g_tp_exited_threads++; |
|
558 |
g_tp_running_threads--; |
|
559 |
should_exit = B_TRUE; |
|
560 |
(void) cond_signal(&g_thr_pool_cv); |
|
561 |
} |
|
562 |
(void) mutex_unlock(g_thr_pool_lock); |
|
563 |
||
564 |
if (should_exit) { |
|
565 |
thr_exit(NULL); |
|
566 |
} |
|
567 |
||
568 |
/* |
|
569 |
* Reached timeout count, but not allowed to |
|
570 |
* exit. Reset counters and continue. |
|
571 |
*/ |
|
572 |
timed_out = B_FALSE; |
|
573 |
timeouts = 0; |
|
574 |
continue; |
|
575 |
} else if (timed_out) { |
|
576 |
/* |
|
577 |
* If port_get timed out, but this thread hasn't |
|
578 |
* reached its timeout count just continue. |
|
579 |
*/ |
|
580 |
timed_out = B_FALSE; |
|
581 |
continue; |
|
582 |
} |
|
583 |
||
584 |
/* |
|
585 |
* Event handling code. This is what we do when we don't |
|
586 |
* timeout. |
|
587 |
*/ |
|
588 |
if (ev.portev_source == PORT_SOURCE_FD) { |
|
589 |
pu = (struct proxy_user *)ev.portev_user; |
|
590 |
pu->pu_callback(pu, &ev); |
|
591 |
} else { |
|
592 |
/* |
|
593 |
* Exit requested. Don't bother adjusting counters |
|
594 |
* since cleanup here is handled by main thread, |
|
595 |
* not manager thread. |
|
596 |
*/ |
|
597 |
break; |
|
598 |
} |
|
599 |
} |
|
600 |
||
601 |
return (arg); |
|
602 |
} |
|
603 |
||
604 |
static int |
|
605 |
check_connect(struct proxy_pair *pair) |
|
606 |
{ |
|
607 |
int error; |
|
608 |
socklen_t len; |
|
609 |
||
610 |
len = sizeof(error); |
|
611 |
||
612 |
if (getsockopt(pair->pp_writefd, SOL_SOCKET, SO_ERROR, &error, |
|
613 |
&len) < 0) { |
|
614 |
return (-1); |
|
615 |
} |
|
616 |
||
617 |
if (error) { |
|
618 |
errno = error; |
|
619 |
return (-1); |
|
620 |
} |
|
621 |
||
622 |
return (0); |
|
623 |
} |
|
624 |
||
625 |
static void |
|
626 |
proxy_func(struct proxy_pair *pair, port_event_t *ev) |
|
627 |
{ |
|
628 |
int rc; |
|
629 |
||
630 |
if (ev->portev_events & (POLLERR | POLLHUP | POLLNVAL)) { |
|
631 |
pair->pp_state = PROXY_STATE_CLOSING; |
|
632 |
shutdown_proxypair(pair); |
|
633 |
return; |
|
634 |
} |
|
635 |
||
636 |
switch (pair->pp_state) { |
|
637 |
case PROXY_STATE_WAIT_CONNECT: |
|
638 |
rc = check_connect(pair); |
|
639 |
if (rc < 0) { |
|
640 |
/* break out early if connect failed */ |
|
641 |
break; |
|
642 |
} |
|
643 |
pair->pp_state = PROXY_STATE_WAIT_DATA; |
|
644 |
rc = clone_and_register(pair); |
|
645 |
break; |
|
646 |
case PROXY_STATE_WAIT_DATA: |
|
647 |
rc = send_recv_data(pair); |
|
648 |
break; |
|
649 |
} |
|
650 |
||
651 |
if (rc < 0) { |
|
652 |
pair->pp_state = PROXY_STATE_CLOSING; |
|
653 |
shutdown_proxypair(pair); |
|
654 |
} |
|
655 |
} |
|
656 |
||
657 |
/* ARGSUSED */ |
|
658 |
static void |
|
659 |
listen_func(struct proxy_listener *listener, port_event_t *ev) |
|
660 |
{ |
|
661 |
struct proxy_pair *pair; |
|
662 |
int newffd; |
|
663 |
int newbfd; |
|
664 |
int err_code; |
|
665 |
struct addrinfo hints; |
|
666 |
struct addrinfo *ai = NULL; |
|
667 |
||
668 |
/* |
|
669 |
* Hold listener's lock, check if cleanup has been requested. |
|
670 |
*/ |
|
671 |
(void) mutex_lock(&listener->pl_lock); |
|
672 |
||
673 |
/* |
|
674 |
* pl_closefd is the other half of the pipe that we weren't able |
|
675 |
* to close before calling door_return. Close it now, if it's set to |
|
676 |
* something that's a fd. |
|
677 |
*/ |
|
678 |
if (listener->pl_closefd > -1) { |
|
679 |
if (close(listener->pl_closefd) < 0) { |
|
680 |
perror("close"); |
|
681 |
exit(EXIT_FAILURE); |
|
682 |
} |
|
683 |
listener->pl_closefd = -1; |
|
684 |
} |
|
685 |
||
686 |
if (listener->pl_cleanup) { |
|
687 |
(void) mutex_unlock(&listener->pl_lock); |
|
688 |
zpd_remove_listener(listener); |
|
689 |
return; |
|
690 |
} |
|
691 |
||
692 |
newffd = accept(listener->pl_fd, NULL, 0); |
|
693 |
if (newffd < 0 && (errno == ECONNABORTED || errno == EINTR || |
|
694 |
errno == EWOULDBLOCK)) { |
|
695 |
(void) mutex_unlock(&listener->pl_lock); |
|
696 |
goto out; |
|
697 |
} else if (newffd < 0) { |
|
698 |
perror("accept"); |
|
699 |
(void) mutex_unlock(&listener->pl_lock); |
|
700 |
(void) zpd_remove_listener(listener); |
|
701 |
return; |
|
702 |
} |
|
703 |
(void) mutex_unlock(&listener->pl_lock); |
|
704 |
||
705 |
pair = alloc_proxy_pair(); |
|
706 |
if (pair == NULL) { |
|
707 |
goto out; |
|
708 |
} |
|
709 |
pair->pp_readfd = newffd; |
|
710 |
||
711 |
/* mark newffd as non-blocking */ |
|
712 |
if (set_noblocking(newffd) < 0) { |
|
713 |
free_proxy_pair(pair); |
|
714 |
goto out; |
|
715 |
} |
|
716 |
||
717 |
(void) memset(&hints, 0, sizeof (struct addrinfo)); |
|
718 |
hints.ai_flags = AI_ADDRCONFIG; |
|
719 |
hints.ai_family = PF_UNSPEC; |
|
720 |
hints.ai_socktype = SOCK_STREAM; |
|
721 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
722 |
/* If proxy config has changed, pull the new info into the listener. */ |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
723 |
if (g_proxy_config->pc_gen > listener->pl_gen) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
724 |
mutex_lock(&g_proxy_config->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
725 |
free(listener->pl_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
726 |
free(listener->pl_proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
727 |
listener->pl_proxy_host = strdup(g_proxy_config->pc_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
728 |
listener->pl_proxy_port = strdup(g_proxy_config->pc_proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
729 |
if (listener->pl_proxy_host == NULL || |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
730 |
listener->pl_proxy_port == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
731 |
(void) fprintf(stderr, "Unable to allocate memory for " |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
732 |
"listener configuration.\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
733 |
exit(EXIT_FAILURE); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
734 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
735 |
listener->pl_gen = g_proxy_config->pc_gen; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
736 |
mutex_unlock(&g_proxy_config->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
737 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
738 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
739 |
if ((err_code = getaddrinfo(listener->pl_proxy_host, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
740 |
listener->pl_proxy_port, &hints, &ai)) != 0) { |
2338 | 741 |
(void) fprintf(stderr, "zoneproxyd: Unable to " |
742 |
"perform name lookup\n"); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
743 |
(void) fprintf(stderr, "%s: %s\n", listener->pl_proxy_host, |
2338 | 744 |
gai_strerror(err_code)); |
745 |
free_proxy_pair(pair); |
|
746 |
goto out; |
|
747 |
} |
|
748 |
||
749 |
if ((newbfd = socket(ai->ai_family, SOCK_STREAM, 0)) < 0) { |
|
750 |
perror("socket"); |
|
751 |
free_proxy_pair(pair); |
|
752 |
goto out; |
|
753 |
} |
|
754 |
||
755 |
/* mark newbfd as non-blocking */ |
|
756 |
if (set_noblocking(newbfd) < 0) { |
|
757 |
if (close(newbfd) < 0) { |
|
758 |
perror("close"); |
|
759 |
exit(EXIT_FAILURE); |
|
760 |
} |
|
761 |
free_proxy_pair(pair); |
|
762 |
goto out; |
|
763 |
} |
|
764 |
||
765 |
/* Connect to the proxy backend */ |
|
766 |
err_code = connect(newbfd, ai->ai_addr, ai->ai_addrlen); |
|
767 |
if (err_code < 0 && errno == EINPROGRESS) { |
|
768 |
pair->pp_state = PROXY_STATE_WAIT_CONNECT; |
|
769 |
pair->pp_writefd = newbfd; |
|
770 |
/* receipt of POLLOUT means we're connected */ |
|
771 |
if (port_associate(g_port, PORT_SOURCE_FD, pair->pp_writefd, |
|
772 |
POLLOUT, pair) < 0) { |
|
773 |
perror("port_associate"); |
|
774 |
if (close(newbfd) < 0) { |
|
775 |
perror("close"); |
|
776 |
exit(EXIT_FAILURE); |
|
777 |
} |
|
778 |
free_proxy_pair(pair); |
|
779 |
goto out; |
|
780 |
} |
|
781 |
} else if (err_code < 0) { |
|
782 |
/* Error, cleanup */ |
|
783 |
if (close(newbfd) < 0) { |
|
784 |
perror("close"); |
|
785 |
exit(EXIT_FAILURE); |
|
786 |
} |
|
787 |
free_proxy_pair(pair); |
|
788 |
goto out; |
|
789 |
} else { |
|
790 |
/* connected without waiting! */ |
|
791 |
pair->pp_state = PROXY_STATE_WAIT_DATA; |
|
792 |
pair->pp_writefd = newbfd; |
|
793 |
||
794 |
if (clone_and_register(pair) < 0) { |
|
795 |
pair->pp_state = PROXY_STATE_CLOSING; |
|
796 |
shutdown_proxypair(pair); |
|
797 |
} |
|
798 |
||
799 |
} |
|
800 |
||
801 |
out: |
|
802 |
if (ai) { |
|
803 |
freeaddrinfo(ai); |
|
804 |
} |
|
805 |
||
806 |
/* |
|
807 |
* Check to make sure that cleanup hasn't been requested before calling |
|
808 |
* port_associate to get another connection. |
|
809 |
*/ |
|
810 |
(void) mutex_lock(&listener->pl_lock); |
|
811 |
||
812 |
if (listener->pl_cleanup) { |
|
813 |
(void) mutex_unlock(&listener->pl_lock); |
|
814 |
zpd_remove_listener(listener); |
|
815 |
return; |
|
816 |
} |
|
817 |
||
818 |
/* re-associate listener; accept further connections */ |
|
819 |
if (port_associate(g_port, PORT_SOURCE_FD, listener->pl_fd, POLLIN, |
|
820 |
listener) < 0) { |
|
821 |
perror("port_associate"); |
|
822 |
(void) mutex_unlock(&listener->pl_lock); |
|
823 |
zpd_remove_listener(listener); |
|
824 |
return; |
|
825 |
} |
|
826 |
(void) mutex_unlock(&listener->pl_lock); |
|
827 |
} |
|
828 |
||
829 |
/* ARGSUSED */ |
|
830 |
static void * |
|
831 |
zpd_door_loop(void *arg) |
|
832 |
{ |
|
833 |
thread_t *tid; |
|
834 |
||
835 |
/* Bind to door's private pool */ |
|
836 |
if (door_bind(g_door) < 0) { |
|
837 |
perror("door_bind"); |
|
838 |
return (NULL); |
|
839 |
} |
|
840 |
||
841 |
/* |
|
842 |
* Disable cancellation. Solaris threads have no cancellation |
|
843 |
* mechanism, but are interchangeable with PThreads. This means we must |
|
844 |
* use the pthread interface to disable cancellation. |
|
845 |
*/ |
|
846 |
(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); |
|
847 |
||
848 |
/* |
|
849 |
* Threads use the thr_keycreate interface to register a destructor. |
|
850 |
* The destructor allows us to decrement the door thread count when a |
|
851 |
* thread exits. In order for the destructor to be called, each thread |
|
852 |
* must register a non-NULL value through thr_setspecfic. Do this |
|
853 |
* here. |
|
854 |
*/ |
|
855 |
tid = malloc(sizeof (thread_t)); |
|
856 |
if (tid == NULL) { |
|
857 |
perror("malloc"); |
|
858 |
return (NULL); |
|
859 |
} |
|
860 |
*tid = thr_self(); |
|
861 |
(void) thr_setspecific(g_thr_info_key, tid); |
|
862 |
||
863 |
/* Invoke door_return to wait for door_call. */ |
|
864 |
(void) door_return(NULL, 0, NULL, 0); |
|
865 |
return (NULL); |
|
866 |
} |
|
867 |
||
868 |
/* ARGSUSED */ |
|
869 |
static void |
|
870 |
thread_exiting(void *arg) |
|
871 |
{ |
|
872 |
free(arg); |
|
873 |
(void) mutex_lock(g_door_thr_lock); |
|
874 |
g_door_thread_count--; |
|
875 |
(void) mutex_unlock(g_door_thr_lock); |
|
876 |
} |
|
877 |
||
878 |
/* ARGSUSED */ |
|
879 |
static void |
|
880 |
zpd_door_create_thread(door_info_t *dip) |
|
881 |
{ |
|
882 |
int rc; |
|
883 |
||
884 |
/* |
|
885 |
* Only create threads for DOOR_PRIVATE pools. |
|
886 |
*/ |
|
887 |
if (dip == NULL) |
|
888 |
return; |
|
889 |
||
890 |
(void) mutex_lock(g_door_thr_lock); |
|
891 |
if (g_door_thread_count < g_max_door_thread && !g_quit) { |
|
892 |
rc = thr_create(NULL, 0, zpd_door_loop, NULL, THR_DAEMON, |
|
893 |
NULL); |
|
894 |
if (rc < 0) { |
|
895 |
perror("thr_create"); |
|
896 |
} else { |
|
897 |
g_door_thread_count++; |
|
898 |
} |
|
899 |
} |
|
900 |
(void) mutex_unlock(g_door_thr_lock); |
|
901 |
} |
|
902 |
||
903 |
/* |
|
904 |
* Thread responsible for creating/joining proxy threads |
|
905 |
* during normal operation of zoneproxyd. |
|
906 |
*/ |
|
907 |
static void * |
|
908 |
thread_manager(void *arg) |
|
909 |
{ |
|
910 |
int i; |
|
911 |
int rc; |
|
912 |
int nthr = 0; |
|
913 |
timestruc_t tmo; |
|
914 |
||
915 |
(void) mutex_lock(g_thr_pool_lock); |
|
916 |
g_tp_exited_threads = 0; |
|
917 |
g_tp_running_threads = 0; |
|
918 |
||
919 |
/* Start proxy threads */ |
|
920 |
for (i = 0; i < g_tp_min_threads; i++) { |
|
921 |
rc = thr_create(NULL, 0, proxy_thread_loop, NULL, |
|
922 |
THR_BOUND, NULL); |
|
923 |
if (rc < 0) { |
|
924 |
perror("thr_create"); |
|
925 |
exit(EXIT_FAILURE); |
|
926 |
} |
|
927 |
g_tp_running_threads++; |
|
928 |
} |
|
929 |
||
930 |
/* Loop waiting for threads to exit, or need to be started */ |
|
931 |
for (;;) { |
|
932 |
if (g_quit == B_TRUE) { |
|
933 |
break; |
|
934 |
} |
|
935 |
||
936 |
while (g_tp_exited_threads > 0) { |
|
937 |
if (thr_join(0, NULL, NULL) < 0) { |
|
938 |
perror("thr_join"); |
|
939 |
exit(EXIT_FAILURE); |
|
940 |
} |
|
941 |
g_tp_exited_threads--; |
|
942 |
} |
|
943 |
||
944 |
/* Compute number of threads to create, if any */ |
|
945 |
if (g_tp_running_threads < g_tp_min_threads) { |
|
946 |
nthr = g_tp_min_threads - g_tp_running_threads; |
|
947 |
} else if ((g_tp_running_threads < g_tp_max_threads) && |
|
948 |
(g_proxy_pair_count > g_tp_running_threads)) { |
|
949 |
nthr = MIN(g_proxy_pair_count, |
|
950 |
g_tp_max_threads - g_tp_running_threads); |
|
951 |
} else { |
|
952 |
nthr = 0; |
|
953 |
} |
|
954 |
||
955 |
for (i = 0; i < nthr; i++) { |
|
956 |
rc = thr_create(NULL, 0, proxy_thread_loop, NULL, |
|
957 |
THR_BOUND, NULL); |
|
958 |
if (rc < 0) { |
|
959 |
perror("thr_create"); |
|
960 |
exit(EXIT_FAILURE); |
|
961 |
} |
|
962 |
g_tp_running_threads++; |
|
963 |
} |
|
964 |
||
965 |
/* sleep, waiting for timeout or cond_signal */ |
|
966 |
tmo.tv_sec = DEFAULT_TIMEOUT; |
|
967 |
tmo.tv_nsec = 0; |
|
968 |
(void) cond_reltimedwait(&g_thr_pool_cv, g_thr_pool_lock, &tmo); |
|
969 |
} |
|
970 |
(void) mutex_unlock(g_thr_pool_lock); |
|
971 |
||
972 |
return (arg); |
|
973 |
} |
|
974 |
||
975 |
/* Contract stuff for zone_enter() */ |
|
976 |
static int |
|
977 |
init_template(void) |
|
978 |
{ |
|
979 |
int fd; |
|
980 |
int err = 0; |
|
981 |
||
982 |
fd = open(CTFS_ROOT "/process/template", O_RDWR); |
|
983 |
if (fd == -1) |
|
984 |
return (-1); |
|
985 |
||
986 |
/* |
|
987 |
* For now, zoneadmd doesn't do anything with the contract. |
|
988 |
* Deliver no events, don't inherit, and allow it to be orphaned. |
|
989 |
*/ |
|
990 |
err |= ct_tmpl_set_critical(fd, 0); |
|
991 |
err |= ct_tmpl_set_informative(fd, 0); |
|
992 |
err |= ct_pr_tmpl_set_fatal(fd, CT_PR_EV_HWERR); |
|
993 |
err |= ct_pr_tmpl_set_param(fd, CT_PR_PGRPONLY | CT_PR_REGENT); |
|
994 |
if (err || ct_tmpl_activate(fd)) { |
|
995 |
if (close(fd) < 0) { |
|
996 |
perror("close"); |
|
997 |
exit(EXIT_FAILURE); |
|
998 |
} |
|
999 |
return (-1); |
|
1000 |
} |
|
1001 |
||
1002 |
return (fd); |
|
1003 |
} |
|
1004 |
||
1005 |
/* |
|
1006 |
* Contract stuff for zone_enter() |
|
1007 |
*/ |
|
1008 |
static int |
|
1009 |
contract_latest(ctid_t *id) |
|
1010 |
{ |
|
1011 |
int cfd, r; |
|
1012 |
ct_stathdl_t st; |
|
1013 |
ctid_t result; |
|
1014 |
||
1015 |
if ((cfd = open(CTFS_ROOT "/process/latest", O_RDONLY)) == -1) |
|
1016 |
return (errno); |
|
1017 |
||
1018 |
if ((r = ct_status_read(cfd, CTD_COMMON, &st)) != 0) { |
|
1019 |
if (close(cfd) < 0) { |
|
1020 |
perror("close"); |
|
1021 |
exit(EXIT_FAILURE); |
|
1022 |
} |
|
1023 |
return (r); |
|
1024 |
} |
|
1025 |
||
1026 |
result = ct_status_get_id(st); |
|
1027 |
ct_status_free(st); |
|
1028 |
if (close(cfd) < 0) { |
|
1029 |
perror("close"); |
|
1030 |
exit(EXIT_FAILURE); |
|
1031 |
} |
|
1032 |
||
1033 |
*id = result; |
|
1034 |
return (0); |
|
1035 |
} |
|
1036 |
||
1037 |
/* |
|
1038 |
* Boilerplate for contract abandon stuff. This program doesn't currently exec |
|
1039 |
* but this is set just in case. |
|
1040 |
*/ |
|
1041 |
static int |
|
1042 |
close_on_exec(int fd) |
|
1043 |
{ |
|
1044 |
int flags = fcntl(fd, F_GETFD, 0); |
|
1045 |
if ((flags != -1) && (fcntl(fd, F_SETFD, flags | FD_CLOEXEC) != -1)) |
|
1046 |
return (0); |
|
1047 |
return (-1); |
|
1048 |
} |
|
1049 |
||
1050 |
static int |
|
1051 |
contract_open(ctid_t ctid, const char *type, const char *file, int oflag) |
|
1052 |
{ |
|
1053 |
char path[PATH_MAX]; |
|
1054 |
int n, fd; |
|
1055 |
||
1056 |
if (type == NULL) |
|
1057 |
type = "all"; |
|
1058 |
||
1059 |
n = snprintf(path, PATH_MAX, CTFS_ROOT "/%s/%ld/%s", type, ctid, file); |
|
1060 |
if (n >= sizeof (path)) { |
|
1061 |
errno = ENAMETOOLONG; |
|
1062 |
return (-1); |
|
1063 |
} |
|
1064 |
||
1065 |
fd = open(path, oflag); |
|
1066 |
if (fd != -1) { |
|
1067 |
if (close_on_exec(fd) == -1) { |
|
1068 |
int err = errno; |
|
1069 |
if (close(fd) < 0) { |
|
1070 |
perror("close"); |
|
1071 |
exit(EXIT_FAILURE); |
|
1072 |
} |
|
1073 |
errno = err; |
|
1074 |
return (-1); |
|
1075 |
} |
|
1076 |
} |
|
1077 |
return (fd); |
|
1078 |
} |
|
1079 |
||
1080 |
static int |
|
1081 |
contract_abandon_id(ctid_t ctid) |
|
1082 |
{ |
|
1083 |
int fd, err; |
|
1084 |
||
1085 |
fd = contract_open(ctid, "all", "ctl", O_WRONLY); |
|
1086 |
if (fd == -1) |
|
1087 |
return (errno); |
|
1088 |
||
1089 |
err = ct_ctl_abandon(fd); |
|
1090 |
if (close(fd) < 0) { |
|
1091 |
perror("close"); |
|
1092 |
exit(EXIT_FAILURE); |
|
1093 |
} |
|
1094 |
||
1095 |
return (err); |
|
1096 |
} |
|
1097 |
||
1098 |
static int |
|
1099 |
do_fattach(int door, char *path, boolean_t detach_only) |
|
1100 |
{ |
|
1101 |
int fd; |
|
1102 |
||
1103 |
(void) fdetach(path); |
|
1104 |
(void) unlink(path); |
|
1105 |
if (detach_only) |
|
1106 |
return (0); |
|
1107 |
/* Only priviliged processes should open this file */ |
|
1108 |
fd = open(path, O_CREAT|O_RDWR, 0600); |
|
1109 |
if (fd < 0) |
|
1110 |
return (2); |
|
1111 |
if (fattach(door, path) != 0) |
|
1112 |
return (3); |
|
1113 |
if (close(fd) < 0) { |
|
1114 |
perror("close"); |
|
1115 |
exit(EXIT_FAILURE); |
|
1116 |
} |
|
1117 |
return (0); |
|
1118 |
} |
|
1119 |
||
1120 |
static void |
|
1121 |
zpd_fattach_zone(zoneid_t zid, int door, boolean_t detach_only) |
|
1122 |
{ |
|
1123 |
char *path = ZP_DOOR_PATH; |
|
1124 |
int pid, stat, tmpl_fd; |
|
1125 |
ctid_t ct; |
|
1126 |
||
1127 |
escalate_privs(); |
|
1128 |
||
1129 |
/* Don't bother forking if fattach is happening in the global zone. */ |
|
1130 |
if (zid == 0) { |
|
1131 |
int rc; |
|
1132 |
||
1133 |
rc = do_fattach(door, path, detach_only); |
|
1134 |
if (rc == 2) |
|
1135 |
(void) fprintf(stderr, |
|
1136 |
"Unable to create door file: %s\n", path); |
|
1137 |
else if (rc == 3) |
|
1138 |
(void) fprintf(stderr, |
|
1139 |
"Unable to fattach file: %s\n", path); |
|
1140 |
drop_privs(); |
|
1141 |
return; |
|
1142 |
} |
|
1143 |
||
1144 |
if ((tmpl_fd = init_template()) == -1) { |
|
1145 |
(void) fprintf(stderr, "Unable to init template\n"); |
|
1146 |
drop_privs(); |
|
1147 |
return; |
|
1148 |
} |
|
1149 |
||
1150 |
pid = fork1(); |
|
1151 |
if (pid < 0) { |
|
1152 |
(void) ct_tmpl_clear(tmpl_fd); |
|
1153 |
(void) fprintf(stderr, |
|
1154 |
"Can't fork to add zoneproxy door to zoneid %ld\n", zid); |
|
1155 |
drop_privs(); |
|
1156 |
return; |
|
1157 |
} |
|
1158 |
||
1159 |
if (pid == 0) { |
|
1160 |
(void) ct_tmpl_clear(tmpl_fd); |
|
1161 |
if (close(tmpl_fd) < 0) { |
|
1162 |
perror("close"); |
|
1163 |
exit(EXIT_FAILURE); |
|
1164 |
} |
|
1165 |
if (zone_enter(zid) != 0) { |
|
1166 |
if (errno == EINVAL) { |
|
1167 |
_exit(EXIT_SUCCESS); |
|
1168 |
} |
|
1169 |
_exit(EXIT_FAILURE); |
|
1170 |
} |
|
1171 |
_exit(do_fattach(door, path, detach_only)); |
|
1172 |
} |
|
1173 |
if (contract_latest(&ct) == -1) |
|
1174 |
ct = -1; |
|
1175 |
(void) ct_tmpl_clear(tmpl_fd); |
|
1176 |
if (close(tmpl_fd) < 0) { |
|
1177 |
perror("close"); |
|
1178 |
exit(EXIT_FAILURE); |
|
1179 |
} |
|
1180 |
(void) contract_abandon_id(ct); |
|
1181 |
while (waitpid(pid, &stat, 0) != pid) |
|
1182 |
; |
|
1183 |
if (WIFEXITED(stat) && WEXITSTATUS(stat) == 0) { |
|
1184 |
drop_privs(); |
|
1185 |
return; |
|
1186 |
} |
|
1187 |
||
1188 |
(void) fprintf(stderr, "Unable to attach door to zoneid: %ld\n", zid); |
|
1189 |
||
1190 |
if (WEXITSTATUS(stat) == 1) |
|
1191 |
(void) fprintf(stderr, "Cannot enter zone\n"); |
|
1192 |
else if (WEXITSTATUS(stat) == 2) |
|
1193 |
(void) fprintf(stderr, "Unable to create door file: %s\n", |
|
1194 |
path); |
|
1195 |
else if (WEXITSTATUS(stat) == 3) |
|
1196 |
(void) fprintf(stderr, "Unable to fattach file: %s\n", path); |
|
1197 |
||
1198 |
(void) fprintf(stderr, "Internal error entering zone: %ld\n", zid); |
|
1199 |
drop_privs(); |
|
1200 |
} |
|
1201 |
||
1202 |
static void |
|
1203 |
fattach_all_zones(boolean_t detach_only) |
|
1204 |
{ |
|
1205 |
zoneid_t *zids; |
|
2389
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1206 |
uint_t nzids, nzids_last; |
2338 | 1207 |
int i; |
1208 |
||
2389
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1209 |
again: |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1210 |
(void) zone_list(NULL, &nzids); |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1211 |
nzids_last = nzids; |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1212 |
zids = (zoneid_t *)malloc(sizeof (zoneid_t) * nzids_last); |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1213 |
if (zids == NULL) |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1214 |
(void) fprintf(stderr, "Out of memory"); |
2338 | 1215 |
|
2389
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1216 |
(void) zone_list(zids, &nzids); |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1217 |
if (nzids > nzids_last) { |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1218 |
free(zids); |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1219 |
goto again; |
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1220 |
} |
2338 | 1221 |
for (i = 0; i < nzids; i++) |
1222 |
zpd_fattach_zone(zids[i], g_door, detach_only); |
|
2389
30ea7b982e91
Make zoneproxy to compile on OI 151
Andrzej Szeszo <aszeszo@gmail.com>
parents:
2349
diff
changeset
|
1223 |
|
2338 | 1224 |
free(zids); |
1225 |
} |
|
1226 |
||
1227 |
static void |
|
1228 |
close_door_descs(door_desc_t *dp, uint_t ndesc) |
|
1229 |
{ |
|
1230 |
int fd; |
|
1231 |
||
1232 |
while (ndesc > 0) { |
|
1233 |
fd = dp->d_data.d_desc.d_descriptor; |
|
1234 |
if (dp->d_attributes & DOOR_DESCRIPTOR) { |
|
1235 |
if (close(fd) < 0) { |
|
1236 |
perror("close"); |
|
1237 |
exit(EXIT_FAILURE); |
|
1238 |
} |
|
1239 |
} |
|
1240 |
dp++; |
|
1241 |
ndesc--; |
|
1242 |
} |
|
1243 |
} |
|
1244 |
||
1245 |
static int |
|
1246 |
zpd_perm_check(int cmd, zoneid_t zid) |
|
1247 |
{ |
|
1248 |
ucred_t *ucred; |
|
1249 |
zoneid_t uzid; |
|
1250 |
||
1251 |
ucred = alloca(ucred_size()); |
|
1252 |
if (door_ucred(&ucred) != 0) { |
|
1253 |
return (-1); |
|
1254 |
} |
|
1255 |
||
1256 |
uzid = ucred_getzoneid(ucred); |
|
1257 |
||
1258 |
/* |
|
1259 |
* Enforce the following permission checks: |
|
1260 |
* |
|
1261 |
* If the command is ADD/REMOVE zone, the caller must be the global |
|
1262 |
* zone. |
|
1263 |
* |
|
1264 |
* If the command is NEW_LISTENER, the caller must be a non-global zone |
|
1265 |
* and the supplied zoneid must match the caller's zoneid. |
|
1266 |
*/ |
|
1267 |
||
1268 |
switch (cmd) { |
|
1269 |
case ZP_CMD_PING: |
|
1270 |
/* Always OK to ping */ |
|
1271 |
return (0); |
|
1272 |
case ZP_CMD_REMOVE_LISTENER: |
|
1273 |
case ZP_CMD_NEW_LISTENER: |
|
1274 |
if (uzid == 0) { |
|
1275 |
return (-1); |
|
1276 |
} |
|
1277 |
if (uzid != zid) { |
|
1278 |
return (-1); |
|
1279 |
} |
|
1280 |
return(0); |
|
1281 |
case ZP_CMD_ZONE_ADDED: |
|
1282 |
case ZP_CMD_ZONE_REMOVED: |
|
1283 |
if (uzid != 0) { |
|
1284 |
return (-1); |
|
1285 |
} |
|
1286 |
return (0); |
|
1287 |
default: |
|
1288 |
break; |
|
1289 |
} |
|
1290 |
||
1291 |
return (-1); |
|
1292 |
} |
|
1293 |
||
1294 |
static struct proxy_listener * |
|
1295 |
zpd_find_listener(zoneid_t zid) |
|
1296 |
{ |
|
1297 |
struct proxy_listener *wl; |
|
1298 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1299 |
for (wl = TAILQ_FIRST(&zone_listener_list); wl != NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1300 |
wl = TAILQ_NEXT(wl, pl_list_link)) { |
2338 | 1301 |
|
1302 |
if (wl->pl_zid == zid) |
|
1303 |
return (wl); |
|
1304 |
} |
|
1305 |
||
1306 |
return (NULL); |
|
1307 |
} |
|
1308 |
||
1309 |
static int |
|
1310 |
zpd_add_listener(zoneid_t zid, int fd, int pipefd, int closefd) |
|
1311 |
{ |
|
1312 |
struct proxy_listener *old_listener; |
|
1313 |
struct proxy_listener *listener; |
|
1314 |
||
1315 |
(void) mutex_lock(g_listener_lock); |
|
1316 |
old_listener = zpd_find_listener(zid); |
|
1317 |
if (old_listener) { |
|
1318 |
zpd_listener_cleanup(old_listener); |
|
1319 |
} |
|
1320 |
||
1321 |
listener = alloc_proxy_listener(); |
|
1322 |
if (listener == NULL) { |
|
1323 |
goto fail; |
|
1324 |
} |
|
1325 |
listener->pl_fd = fd; |
|
1326 |
listener->pl_zid = zid; |
|
1327 |
listener->pl_pipefd = pipefd; |
|
1328 |
listener->pl_closefd = closefd; |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1329 |
TAILQ_INSERT_TAIL(&zone_listener_list, listener, pl_list_link); |
2338 | 1330 |
if (set_noblocking(fd) < 0) { |
1331 |
goto fail; |
|
1332 |
} |
|
1333 |
if (set_noblocking(pipefd) < 0) { |
|
1334 |
goto fail; |
|
1335 |
} |
|
1336 |
if (port_associate(g_port, PORT_SOURCE_FD, listener->pl_fd, POLLIN, |
|
1337 |
listener) < 0) { |
|
1338 |
perror("port_associate"); |
|
1339 |
goto fail; |
|
1340 |
} |
|
1341 |
(void) mutex_unlock(g_listener_lock); |
|
1342 |
return (0); |
|
1343 |
||
1344 |
fail: |
|
1345 |
if (listener) { |
|
1346 |
/* No cleanup required, since list lock was never dropped */ |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1347 |
TAILQ_REMOVE(&zone_listener_list, listener, pl_list_link); |
2338 | 1348 |
free_proxy_listener(listener); |
1349 |
} |
|
1350 |
(void) mutex_unlock(g_listener_lock); |
|
1351 |
return (-1); |
|
1352 |
} |
|
1353 |
||
1354 |
/* |
|
1355 |
* This method has to perform an intricate dance to cleanup a listener. If it |
|
1356 |
* is able to dissociate the listener from the port, it may remove the listener. |
|
1357 |
* Otherwise, it must set the cleanup flag and let the thread running the |
|
1358 |
* listener perform the removal. |
|
1359 |
* |
|
1360 |
* Caller should hold the listener list lock. |
|
1361 |
*/ |
|
1362 |
static void |
|
1363 |
zpd_listener_cleanup(struct proxy_listener *listener) |
|
1364 |
{ |
|
1365 |
int rc; |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1366 |
struct proxy_listener *wl; |
2338 | 1367 |
|
1368 |
(void) mutex_lock(&listener->pl_lock); |
|
1369 |
if (listener->pl_cleanup) { |
|
1370 |
(void) mutex_unlock(&listener->pl_lock); |
|
1371 |
return; |
|
1372 |
} |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1373 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1374 |
for (wl = TAILQ_FIRST(&zone_listener_list); wl != NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1375 |
wl = TAILQ_NEXT(wl, pl_list_link)) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1376 |
if (wl == listener) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1377 |
TAILQ_REMOVE(&zone_listener_list, listener, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1378 |
pl_list_link); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1379 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1380 |
} |
2338 | 1381 |
} |
1382 |
rc = port_dissociate(g_port, PORT_SOURCE_FD, listener->pl_fd); |
|
1383 |
if (rc == 0) { |
|
1384 |
/* successfully got the object, remove it. */ |
|
1385 |
(void) mutex_unlock(&listener->pl_lock); |
|
1386 |
free_proxy_listener(listener); |
|
1387 |
return; |
|
1388 |
} else if (rc < 0 && errno == ENOENT) { |
|
1389 |
/* |
|
1390 |
* Didn't find the event associated with the port. Another |
|
1391 |
* thread must be concurrently processing events for the |
|
1392 |
* fd. |
|
1393 |
*/ |
|
1394 |
listener->pl_cleanup = B_TRUE; |
|
1395 |
} else { |
|
1396 |
/* Unexpected error */ |
|
1397 |
perror("port_dissociate"); |
|
1398 |
exit(EXIT_FAILURE); |
|
1399 |
} |
|
1400 |
(void) mutex_unlock(&listener->pl_lock); |
|
1401 |
} |
|
1402 |
||
1403 |
static void |
|
1404 |
zpd_remove_listener(struct proxy_listener *listener) |
|
1405 |
{ |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1406 |
struct proxy_listener *wl; |
2338 | 1407 |
|
1408 |
/* |
|
1409 |
* Add and remove operations hold the list lock for the duration of |
|
1410 |
* their execution. When this routine acquires the list lock and |
|
1411 |
* removes the listener, it should no longer be reachable by any other |
|
1412 |
* thread. |
|
1413 |
*/ |
|
1414 |
(void) mutex_lock(g_listener_lock); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1415 |
for (wl = TAILQ_FIRST(&zone_listener_list); wl != NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1416 |
wl = TAILQ_NEXT(wl, pl_list_link)) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1417 |
if (wl == listener) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1418 |
TAILQ_REMOVE(&zone_listener_list, listener, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1419 |
pl_list_link); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1420 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1421 |
} |
2338 | 1422 |
} |
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1423 |
|
2338 | 1424 |
(void) mutex_unlock(g_listener_lock); |
1425 |
||
1426 |
free_proxy_listener(listener); |
|
1427 |
} |
|
1428 |
||
1429 |
/* |
|
1430 |
* Zone removal call. This routine cannot fdetach the door in the zone because |
|
1431 |
* the zone is in shutdown state and cannot be zone_enter'd. This means that |
|
1432 |
* the add_zone code must always fdetach and unlink the existing door before |
|
1433 |
* creating a new one. |
|
1434 |
*/ |
|
1435 |
static int |
|
1436 |
zpd_remove_zone(zoneid_t zid) |
|
1437 |
{ |
|
1438 |
struct proxy_listener *listener; |
|
1439 |
||
1440 |
(void) mutex_lock(g_listener_lock); |
|
1441 |
listener = zpd_find_listener(zid); |
|
1442 |
if (listener) { |
|
1443 |
zpd_listener_cleanup(listener); |
|
1444 |
} |
|
1445 |
(void) mutex_unlock(g_listener_lock); |
|
1446 |
||
1447 |
return (0); |
|
1448 |
} |
|
1449 |
||
1450 |
/* ARGSUSED */ |
|
1451 |
static void |
|
1452 |
zpd_door_server(void *cookie, char *argp, size_t arg_size, |
|
1453 |
door_desc_t *dp, uint_t n_desc) |
|
1454 |
{ |
|
1455 |
int *args, cmd; |
|
1456 |
int pipefd[2]; |
|
1457 |
uint_t nexpected_desc; |
|
1458 |
door_desc_t *r_dp = NULL; |
|
1459 |
door_desc_t rdesc; |
|
1460 |
uint_t r_n_desc = 0; |
|
1461 |
||
1462 |
if (argp == DOOR_UNREF_DATA) { |
|
1463 |
(void) door_return(NULL, 0, NULL, 0); |
|
1464 |
} |
|
1465 |
||
1466 |
if (arg_size != sizeof (cmd) * 2) { |
|
1467 |
close_door_descs(dp, n_desc); |
|
1468 |
(void) door_return(NULL, 0, NULL, 0); |
|
1469 |
} |
|
1470 |
||
1471 |
/* LINTED */ |
|
1472 |
args = (int *)argp; |
|
1473 |
cmd = args[0]; |
|
1474 |
||
1475 |
/* |
|
1476 |
* Caller may have passed more descriptors than expected. |
|
1477 |
* If so, close the extraneous fds. |
|
1478 |
*/ |
|
1479 |
nexpected_desc = (cmd == ZP_CMD_NEW_LISTENER) ? 1 : 0; |
|
1480 |
if (n_desc > nexpected_desc) { |
|
1481 |
close_door_descs(dp + nexpected_desc, n_desc - nexpected_desc); |
|
1482 |
} |
|
1483 |
||
1484 |
switch (cmd) { |
|
1485 |
case ZP_CMD_NEW_LISTENER: |
|
1486 |
if (zpd_perm_check(cmd, args[1]) < 0) { |
|
1487 |
close_door_descs(dp, n_desc); |
|
1488 |
args[1] = ZP_STATUS_PERMISSION; |
|
1489 |
goto out; |
|
1490 |
} |
|
1491 |
if (n_desc < 1 || (dp->d_attributes & DOOR_DESCRIPTOR) == 0) { |
|
1492 |
args[1] = ZP_STATUS_INVALID; |
|
1493 |
goto out; |
|
1494 |
} |
|
1495 |
if (pipe(pipefd) < 0) { |
|
1496 |
args[1] = ZP_STATUS_ERROR; |
|
1497 |
goto out; |
|
1498 |
} |
|
1499 |
if (zpd_add_listener(args[1], |
|
1500 |
dp->d_data.d_desc.d_descriptor, pipefd[0], pipefd[1]) < 0) { |
|
1501 |
close_door_descs(dp, n_desc); |
|
1502 |
if (close(pipefd[0]) < 0) { |
|
1503 |
perror("close"); |
|
1504 |
exit(EXIT_FAILURE); |
|
1505 |
} |
|
1506 |
if (close(pipefd[1]) < 0) { |
|
1507 |
perror("close"); |
|
1508 |
exit(EXIT_FAILURE); |
|
1509 |
} |
|
1510 |
args[1] = ZP_STATUS_ERROR; |
|
1511 |
goto out; |
|
1512 |
} |
|
1513 |
||
1514 |
r_dp = &rdesc; |
|
1515 |
r_dp->d_attributes = DOOR_DESCRIPTOR; |
|
1516 |
r_dp->d_data.d_desc.d_descriptor = pipefd[1]; |
|
1517 |
r_n_desc = 1; |
|
1518 |
args[1] = ZP_STATUS_OK; |
|
1519 |
break; |
|
1520 |
case ZP_CMD_ZONE_ADDED: |
|
1521 |
if (zpd_perm_check(cmd, args[1]) < 0) { |
|
1522 |
args[1] = ZP_STATUS_PERMISSION; |
|
1523 |
goto out; |
|
1524 |
} |
|
1525 |
zpd_fattach_zone(args[1], g_door, B_FALSE); |
|
1526 |
args[1] = ZP_STATUS_OK; |
|
1527 |
break; |
|
1528 |
case ZP_CMD_REMOVE_LISTENER: |
|
1529 |
case ZP_CMD_ZONE_REMOVED: |
|
1530 |
if (zpd_perm_check(cmd, args[1]) < 0) { |
|
1531 |
args[1] = ZP_STATUS_PERMISSION; |
|
1532 |
goto out; |
|
1533 |
} |
|
1534 |
if (zpd_remove_zone(args[1]) < 0) { |
|
1535 |
args[1] = ZP_STATUS_ERROR; |
|
1536 |
goto out; |
|
1537 |
} |
|
1538 |
args[1] = ZP_STATUS_OK; |
|
1539 |
break; |
|
1540 |
case ZP_CMD_PING: |
|
1541 |
if (zpd_perm_check(cmd, args[1]) < 0) { |
|
1542 |
args[1] = ZP_STATUS_PERMISSION; |
|
1543 |
goto out; |
|
1544 |
} |
|
1545 |
args[1] = ZP_STATUS_OK; |
|
1546 |
break; |
|
1547 |
default: |
|
1548 |
args[1] = ZP_STATUS_UNKNOWN; |
|
1549 |
break; |
|
1550 |
} |
|
1551 |
out: |
|
1552 |
(void) door_return(argp, sizeof (cmd) * 2, r_dp, r_n_desc); |
|
1553 |
} |
|
1554 |
||
1555 |
static void |
|
1556 |
daemonize_ready(char status) |
|
1557 |
{ |
|
1558 |
/* |
|
1559 |
* wake the parent with a clue |
|
1560 |
*/ |
|
1561 |
(void) write(g_pipe_fd, &status, 1); |
|
1562 |
if (close(g_pipe_fd) < 0) { |
|
1563 |
perror("close"); |
|
1564 |
exit(EXIT_FAILURE); |
|
1565 |
} |
|
1566 |
} |
|
1567 |
||
1568 |
static int |
|
1569 |
daemonize_start(void) |
|
1570 |
{ |
|
1571 |
char data; |
|
1572 |
int status; |
|
1573 |
||
1574 |
int filedes[2]; |
|
1575 |
pid_t pid; |
|
1576 |
||
1577 |
if (close(0) < 0) { |
|
1578 |
perror("close"); |
|
1579 |
exit(EXIT_FAILURE); |
|
1580 |
} |
|
1581 |
if (dup2(2, 1) < 0) { |
|
1582 |
perror("dup2"); |
|
1583 |
exit(EXIT_FAILURE); |
|
1584 |
} |
|
1585 |
||
1586 |
if (pipe(filedes) < 0) |
|
1587 |
return (-1); |
|
1588 |
||
1589 |
(void) fflush(NULL); |
|
1590 |
||
1591 |
if ((pid = fork1()) < 0) |
|
1592 |
return (-1); |
|
1593 |
||
1594 |
if (pid != 0) { |
|
1595 |
/* |
|
1596 |
* parent |
|
1597 |
*/ |
|
1598 |
if (close(filedes[1]) < 0) { |
|
1599 |
perror("close"); |
|
1600 |
exit(EXIT_FAILURE); |
|
1601 |
} |
|
1602 |
if (read(filedes[0], &data, 1) == 1) { |
|
1603 |
/* forward ready code via exit status */ |
|
1604 |
exit(data); |
|
1605 |
} |
|
1606 |
status = -1; |
|
1607 |
(void) wait4(pid, &status, 0, NULL); |
|
1608 |
/* daemon process exited before becoming ready */ |
|
1609 |
if (WIFEXITED(status)) { |
|
1610 |
/* assume daemon process printed useful message */ |
|
1611 |
exit(WEXITSTATUS(status)); |
|
1612 |
} else { |
|
1613 |
(void) fprintf(stderr, |
|
1614 |
"daemon process killed or died\n"); |
|
1615 |
exit(EXIT_FAILURE); |
|
1616 |
} |
|
1617 |
} |
|
1618 |
||
1619 |
/* |
|
1620 |
* child |
|
1621 |
*/ |
|
1622 |
g_pipe_fd = filedes[1]; |
|
1623 |
if (close(filedes[0]) < 0) { |
|
1624 |
perror("close"); |
|
1625 |
exit(EXIT_FAILURE); |
|
1626 |
} |
|
1627 |
||
1628 |
/* |
|
1629 |
* generic Unix setup |
|
1630 |
*/ |
|
1631 |
(void) setsid(); |
|
1632 |
(void) umask(0000); |
|
1633 |
||
1634 |
return (0); |
|
1635 |
} |
|
1636 |
||
1637 |
static void |
|
1638 |
drop_privs(void) |
|
1639 |
{ |
|
1640 |
priv_set_t *ePrivSet = NULL; |
|
1641 |
priv_set_t *lPrivSet = NULL; |
|
1642 |
||
1643 |
if ((ePrivSet = priv_str_to_set("basic", ",", NULL)) == NULL) { |
|
1644 |
(void) fprintf(stderr, "Unable to get 'basic' privset\n"); |
|
1645 |
exit(EXIT_FAILURE); |
|
1646 |
} |
|
1647 |
||
1648 |
/* Drop any privs out of the basic set that we won't need */ |
|
1649 |
(void) priv_delset(ePrivSet, PRIV_FILE_LINK_ANY); |
|
1650 |
(void) priv_delset(ePrivSet, PRIV_PROC_INFO); |
|
1651 |
(void) priv_delset(ePrivSet, PRIV_PROC_SESSION); |
|
1652 |
(void) priv_delset(ePrivSet, PRIV_PROC_EXEC); |
|
1653 |
||
1654 |
/* Add privs needed for daemon operation */ |
|
1655 |
(void) priv_addset(ePrivSet, PRIV_CONTRACT_EVENT); |
|
1656 |
(void) priv_addset(ePrivSet, PRIV_CONTRACT_IDENTITY); |
|
1657 |
||
1658 |
/* Set effective set */ |
|
1659 |
if (setppriv(PRIV_SET, PRIV_EFFECTIVE, ePrivSet) != 0) { |
|
1660 |
(void) fprintf(stderr, "Unable to drop privs\n"); |
|
1661 |
exit(EXIT_FAILURE); |
|
1662 |
} |
|
1663 |
||
1664 |
/* clear limit set */ |
|
1665 |
if ((lPrivSet = priv_allocset()) == NULL) { |
|
1666 |
(void) fprintf(stderr, "Unable to allocate privset\n"); |
|
1667 |
exit(EXIT_FAILURE); |
|
1668 |
} |
|
1669 |
||
1670 |
priv_emptyset(lPrivSet); |
|
1671 |
||
1672 |
if (setppriv(PRIV_SET, PRIV_LIMIT, lPrivSet) != 0) { |
|
1673 |
(void) fprintf(stderr, "Unable to set limit set\n"); |
|
1674 |
exit(EXIT_FAILURE); |
|
1675 |
} |
|
1676 |
||
1677 |
priv_freeset(lPrivSet); |
|
1678 |
priv_freeset(ePrivSet); |
|
1679 |
||
1680 |
} |
|
1681 |
||
1682 |
/* |
|
1683 |
* zone_enter requires that the process have the full privilege set. We try to |
|
1684 |
* run with the lowest possible set, but in the case where we zone-enter, we |
|
1685 |
* must re-set the effective set to be all privs. |
|
1686 |
*/ |
|
1687 |
static void |
|
1688 |
escalate_privs(void) |
|
1689 |
{ |
|
1690 |
priv_set_t *ePrivSet = NULL; |
|
1691 |
||
1692 |
if ((ePrivSet = priv_allocset()) == NULL) { |
|
1693 |
(void) fprintf(stderr, "Unable to allocate privset\n"); |
|
1694 |
exit(EXIT_FAILURE); |
|
1695 |
} |
|
1696 |
||
1697 |
priv_fillset(ePrivSet); |
|
1698 |
||
1699 |
if (setppriv(PRIV_SET, PRIV_EFFECTIVE, ePrivSet) != 0) { |
|
1700 |
(void) fprintf(stderr, "Unable to set effective priv set\n"); |
|
1701 |
exit(EXIT_FAILURE); |
|
1702 |
} |
|
1703 |
||
1704 |
priv_freeset(ePrivSet); |
|
1705 |
} |
|
1706 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1707 |
static struct proxy_config * |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1708 |
config_alloc(void) |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1709 |
{ |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1710 |
struct proxy_config *pc = NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1711 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1712 |
if ((pc = malloc(sizeof (struct proxy_config))) == NULL) |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1713 |
return (NULL); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1714 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1715 |
(void) memset(pc, 0, sizeof (struct proxy_config)); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1716 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1717 |
if (mutex_init(&pc->pc_lock, USYNC_THREAD, NULL) < 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1718 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1719 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1720 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1721 |
if ((pc->pc_hdl = scf_handle_create(SCF_VERSION)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1722 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1723 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1724 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1725 |
if ((pc->pc_inst = scf_instance_create(pc->pc_hdl)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1726 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1727 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1728 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1729 |
if ((pc->pc_pg = scf_pg_create(pc->pc_hdl)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1730 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1731 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1732 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1733 |
if ((pc->pc_prop = scf_property_create(pc->pc_hdl)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1734 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1735 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1736 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1737 |
if ((pc->pc_val = scf_value_create(pc->pc_hdl)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1738 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1739 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1740 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1741 |
if ((pc->pc_proxy_host = strdup(DEFAULT_HOST)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1742 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1743 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1744 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1745 |
if ((pc->pc_proxy_port = strdup(DEFAULT_PORT)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1746 |
goto out; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1747 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1748 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1749 |
pc->pc_gen = 1; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1750 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1751 |
return (pc); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1752 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1753 |
out: |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1754 |
config_free(pc); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1755 |
return (NULL); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1756 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1757 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1758 |
static void |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1759 |
config_free(struct proxy_config *pc) |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1760 |
{ |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1761 |
if (pc == NULL) |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1762 |
return; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1763 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1764 |
if (pc->pc_inst != NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1765 |
scf_instance_destroy(pc->pc_inst); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1766 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1767 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1768 |
if (pc->pc_pg != NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1769 |
scf_pg_destroy(pc->pc_pg); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1770 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1771 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1772 |
if (pc->pc_prop != NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1773 |
scf_property_destroy(pc->pc_prop); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1774 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1775 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1776 |
if (pc->pc_val != NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1777 |
scf_value_destroy(pc->pc_val); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1778 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1779 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1780 |
if (pc->pc_hdl != NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1781 |
scf_handle_destroy(pc->pc_hdl); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1782 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1783 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1784 |
free(pc->pc_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1785 |
free(pc->pc_proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1786 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1787 |
(void) mutex_destroy(&pc->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1788 |
free(pc); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1789 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1790 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1791 |
static int |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1792 |
config_read(struct proxy_config *pc) |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1793 |
{ |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1794 |
char *host = NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1795 |
char *port = NULL; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1796 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1797 |
if ((host = malloc(CONF_STR_SZ)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1798 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1799 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1800 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1801 |
if ((port = malloc(CONF_STR_SZ)) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1802 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1803 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1804 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1805 |
mutex_lock(&pc->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1806 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1807 |
if (scf_handle_bind(pc->pc_hdl) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1808 |
(void) fprintf(stderr, "scf_handle_bind failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1809 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1810 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1811 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1812 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1813 |
if (scf_handle_decode_fmri(pc->pc_hdl, SYSREPO_FMRI, NULL, NULL, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1814 |
pc->pc_inst, NULL, NULL, SCF_DECODE_FMRI_REQUIRE_INSTANCE) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1815 |
(void) fprintf(stderr, "scf_handle_decode_fmri failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1816 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1817 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1818 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1819 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1820 |
if (scf_instance_get_pg(pc->pc_inst, SYSREPO_PG, pc->pc_pg) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1821 |
(void) fprintf(stderr, "scf_instance_get_pg failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1822 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1823 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1824 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1825 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1826 |
if (scf_pg_get_property(pc->pc_pg, SYSREPO_HOST, pc->pc_prop) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1827 |
(void) fprintf(stderr, "scf_pg_get_property failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1828 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1829 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1830 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1831 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1832 |
if (scf_property_get_value(pc->pc_prop, pc->pc_val) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1833 |
(void) fprintf(stderr, "scf_property_get_value failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1834 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1835 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1836 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1837 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1838 |
if (scf_value_get_as_string_typed(pc->pc_val, SCF_TYPE_ASTRING, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1839 |
host, CONF_STR_SZ) < 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1840 |
(void) fprintf(stderr, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1841 |
"scf_value_get_as_string_typed failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1842 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1843 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1844 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1845 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1846 |
if (scf_pg_get_property(pc->pc_pg, SYSREPO_PORT, pc->pc_prop) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1847 |
(void) fprintf(stderr, "scf_pg_get_property failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1848 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1849 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1850 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1851 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1852 |
if (scf_property_get_value(pc->pc_prop, pc->pc_val) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1853 |
(void) fprintf(stderr, "scf_property_get_value failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1854 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1855 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1856 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1857 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1858 |
if (scf_value_get_as_string_typed(pc->pc_val, SCF_TYPE_COUNT, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1859 |
port, CONF_STR_SZ) < 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1860 |
(void) fprintf(stderr, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1861 |
"scf_value_get_as_string_typed failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1862 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1863 |
goto fail; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1864 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1865 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1866 |
if (scf_handle_unbind(pc->pc_hdl) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1867 |
(void) fprintf(stderr, "scf_handle_unbind failed; %s\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1868 |
scf_strerror(scf_error())); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1869 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1870 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1871 |
free(pc->pc_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1872 |
free(pc->pc_proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1873 |
pc->pc_proxy_host = host; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1874 |
pc->pc_proxy_port = port; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1875 |
pc->pc_gen++; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1876 |
mutex_unlock(&pc->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1877 |
return (0); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1878 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1879 |
fail: |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1880 |
mutex_unlock(&pc->pc_lock); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1881 |
free(host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1882 |
free(port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1883 |
return (-1); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1884 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1885 |
|
2338 | 1886 |
static void |
1887 |
s_handler(void) |
|
1888 |
{ |
|
1889 |
sigset_t get_sigs; |
|
1890 |
int rc; |
|
1891 |
||
1892 |
(void) sigfillset(&get_sigs); |
|
1893 |
while (g_quit == B_FALSE) { |
|
1894 |
rc = sigwait(&get_sigs); |
|
1895 |
||
1896 |
if (rc == SIGINT || rc == SIGTERM || rc == SIGHUP) { |
|
1897 |
(void) mutex_lock(&g_quit_lock); |
|
1898 |
g_quit = B_TRUE; |
|
1899 |
(void) cond_signal(&g_quit_cv); |
|
1900 |
(void) mutex_unlock(&g_quit_lock); |
|
1901 |
} |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1902 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1903 |
if (g_config_smf && rc == SIGUSR1) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1904 |
if (config_read(g_proxy_config) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1905 |
(void) fprintf(stderr, "Unable to re-load " |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1906 |
"proxy configuration from SMF.\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1907 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1908 |
} |
2338 | 1909 |
} |
1910 |
} |
|
1911 |
||
1912 |
int |
|
1913 |
main(int argc, char **argv) |
|
1914 |
{ |
|
1915 |
extern char *optarg; |
|
1916 |
char *proxystr = NULL; |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1917 |
char *proxy_host, *proxy_port; |
2338 | 1918 |
int rc; |
1919 |
int ncpu; |
|
1920 |
struct proxy_listener *wl; |
|
1921 |
sigset_t blockset; |
|
1922 |
struct rlimit rlp = {0}; |
|
1923 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1924 |
while ((rc = getopt(argc, argv, "s:")) != -1) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1925 |
switch (rc) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1926 |
case 's': |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1927 |
proxystr = optarg; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1928 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1929 |
case ':': |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1930 |
(void) fprintf(stderr, "Option -%c requires operand\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1931 |
optopt); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1932 |
usage(); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1933 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1934 |
case '?': |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1935 |
(void) fprintf(stderr, "Unrecognized option -%c\n", |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1936 |
optopt); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1937 |
usage(); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1938 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1939 |
default: |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1940 |
break; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1941 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1942 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1943 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1944 |
g_config_smf = (proxystr == NULL) ? B_TRUE : B_FALSE; |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1945 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1946 |
if (!g_config_smf) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1947 |
proxy_host = strtok(proxystr, ":"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1948 |
if (proxy_host == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1949 |
(void) fprintf(stderr, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1950 |
"host must be of format hostname:port\n"); |
2338 | 1951 |
usage(); |
1952 |
} |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1953 |
proxy_port = strtok(NULL, ":"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1954 |
if (proxy_port == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1955 |
(void) fprintf(stderr, |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1956 |
"host must be of format hostname:port\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1957 |
usage(); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1958 |
} |
2338 | 1959 |
} |
1960 |
||
1961 |
g_quit = B_FALSE; |
|
1962 |
(void) signal(SIGPIPE, SIG_IGN); |
|
1963 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1964 |
if ((g_proxy_config = config_alloc()) == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1965 |
(void) fprintf(stderr, "Unable to allocate proxy config\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1966 |
exit(EXIT_FAILURE); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1967 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1968 |
|
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1969 |
if (g_config_smf) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1970 |
if (config_read(g_proxy_config) != 0) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1971 |
(void) fprintf(stderr, "Unable to read proxy config. " |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1972 |
"Falling back to defaults.\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1973 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1974 |
} else { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1975 |
free(g_proxy_config->pc_proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1976 |
free(g_proxy_config->pc_proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1977 |
g_proxy_config->pc_proxy_host = strdup(proxy_host); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1978 |
g_proxy_config->pc_proxy_port = strdup(proxy_port); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1979 |
if (g_proxy_config->pc_proxy_host == NULL || |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1980 |
g_proxy_config->pc_proxy_port == NULL) { |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1981 |
(void) fprintf(stderr, "Unable to allocate memory for " |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1982 |
"proxy configuration strings\n"); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1983 |
exit(EXIT_FAILURE); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1984 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1985 |
} |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
1986 |
|
2338 | 1987 |
if (daemonize_start() < 0) |
1988 |
(void) fprintf(stderr, "Unable to start daemon\n"); |
|
1989 |
||
1990 |
drop_privs(); |
|
1991 |
||
1992 |
(void) sigfillset(&blockset); |
|
1993 |
||
1994 |
if (thr_sigsetmask(SIG_BLOCK, &blockset, NULL) < 0) { |
|
1995 |
perror("thr_sigsetmask"); |
|
1996 |
exit(EXIT_FAILURE); |
|
1997 |
} |
|
1998 |
||
1999 |
/* Increase the number of maximum file descriptors */ |
|
2000 |
(void) getrlimit(RLIMIT_NOFILE, &rlp); |
|
2001 |
rlp.rlim_cur = MAX_FDS_DEFAULT; |
|
2002 |
if (setrlimit(RLIMIT_NOFILE, &rlp) < 0) { |
|
2003 |
perror("setrlimit"); |
|
2004 |
exit(EXIT_FAILURE); |
|
2005 |
} |
|
2006 |
||
2007 |
/* Create single global event port */ |
|
2008 |
if ((g_port = port_create()) < 0) { |
|
2009 |
perror("port_create"); |
|
2010 |
exit(EXIT_FAILURE); |
|
2011 |
} |
|
2012 |
||
2013 |
/* Setup listener list. */ |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
2014 |
TAILQ_INIT(&zone_listener_list); |
2338 | 2015 |
|
2016 |
/* Initialize locks */ |
|
2017 |
g_door_thr_lock = memalign(DEFAULT_LOCK_ALIGN, sizeof (mutex_t)); |
|
2018 |
if (g_door_thr_lock == NULL) { |
|
2019 |
(void) fprintf(stderr, "Unable to allocate g_door_thr_lock\n"); |
|
2020 |
exit(EXIT_FAILURE); |
|
2021 |
} |
|
2022 |
if (mutex_init(g_door_thr_lock, USYNC_THREAD, NULL) < 0) { |
|
2023 |
perror("mutex_init"); |
|
2024 |
exit(EXIT_FAILURE); |
|
2025 |
} |
|
2026 |
||
2027 |
g_listener_lock = memalign(DEFAULT_LOCK_ALIGN, sizeof (mutex_t)); |
|
2028 |
if (g_listener_lock == NULL) { |
|
2029 |
(void) fprintf(stderr, "Unable to allocate g_listener_lock\n"); |
|
2030 |
exit(EXIT_FAILURE); |
|
2031 |
} |
|
2032 |
if (mutex_init(g_listener_lock, USYNC_THREAD, NULL) < 0) { |
|
2033 |
perror("mutex_init"); |
|
2034 |
exit(EXIT_FAILURE); |
|
2035 |
} |
|
2036 |
||
2037 |
g_thr_pool_lock = memalign(DEFAULT_LOCK_ALIGN, sizeof (mutex_t)); |
|
2038 |
if (g_thr_pool_lock == NULL) { |
|
2039 |
(void) fprintf(stderr, "Unable to alloc g_thr_pool_lock\n"); |
|
2040 |
exit(EXIT_FAILURE); |
|
2041 |
} |
|
2042 |
if (mutex_init(g_thr_pool_lock, USYNC_THREAD, NULL) < 0) { |
|
2043 |
perror("mutex_init"); |
|
2044 |
exit(EXIT_FAILURE); |
|
2045 |
} |
|
2046 |
||
2047 |
if (thr_keycreate(&g_thr_info_key, thread_exiting) < 0) { |
|
2048 |
perror("thr_keycreate"); |
|
2049 |
exit(EXIT_FAILURE); |
|
2050 |
} |
|
2051 |
||
2052 |
/* Auto-tune min/max threads based upon number of cpus in system */ |
|
2053 |
ncpu = sysconf(_SC_NPROCESSORS_ONLN); |
|
2054 |
if (ncpu < 0) { |
|
2055 |
perror("sysconf"); |
|
2056 |
exit(EXIT_FAILURE); |
|
2057 |
} |
|
2058 |
||
2059 |
/* Paranoia. */ |
|
2060 |
if (ncpu == 0) { |
|
2061 |
(void) fprintf(stderr, "0 cpus online. How is this running?\n"); |
|
2062 |
exit(EXIT_FAILURE); |
|
2063 |
} |
|
2064 |
||
2065 |
g_tp_min_threads = MIN(g_tp_min_threads, ncpu); |
|
2066 |
g_tp_max_threads = MAX(g_tp_max_threads, ncpu/4); |
|
2067 |
||
2068 |
/* Setup door */ |
|
2069 |
(void) door_server_create(zpd_door_create_thread); |
|
2070 |
||
2071 |
g_door = door_create(zpd_door_server, NULL, |
|
2072 |
DOOR_PRIVATE | DOOR_NO_CANCEL); |
|
2073 |
if (g_door < 0) { |
|
2074 |
perror("door_create"); |
|
2075 |
exit(EXIT_FAILURE); |
|
2076 |
} |
|
2077 |
||
2078 |
/* |
|
2079 |
* Set a limit on the size of the data that may be passed |
|
2080 |
* through the door, as well as the number of FDs that may be passed in |
|
2081 |
* any particular call. |
|
2082 |
*/ |
|
2083 |
if (door_setparam(g_door, DOOR_PARAM_DATA_MAX, sizeof (int) * 2) < 0) { |
|
2084 |
perror("door_setparam"); |
|
2085 |
exit(EXIT_FAILURE); |
|
2086 |
} |
|
2087 |
if (door_setparam(g_door, DOOR_PARAM_DESC_MAX, 1) < 0) { |
|
2088 |
perror("door_setparam"); |
|
2089 |
exit(EXIT_FAILURE); |
|
2090 |
} |
|
2091 |
||
2092 |
fattach_all_zones(B_FALSE); |
|
2093 |
||
2094 |
/* start signal handling thread */ |
|
2095 |
rc = thr_create(NULL, 0, (void *(*)(void *))s_handler, NULL, |
|
2096 |
THR_BOUND, NULL); |
|
2097 |
if (rc < 0) { |
|
2098 |
perror("thr_create"); |
|
2099 |
exit(EXIT_FAILURE); |
|
2100 |
} |
|
2101 |
||
2102 |
/* Start thread pool manager */ |
|
2103 |
rc = thr_create(NULL, 0, thread_manager, NULL, THR_BOUND, NULL); |
|
2104 |
if (rc < 0) { |
|
2105 |
perror("thr_create"); |
|
2106 |
exit(EXIT_FAILURE); |
|
2107 |
} |
|
2108 |
||
2109 |
daemonize_ready(0); |
|
2110 |
||
2111 |
/* Wait for signal handling thread to notify us to quit */ |
|
2112 |
while (g_quit == B_FALSE) { |
|
2113 |
(void) mutex_lock(&g_quit_lock); |
|
2114 |
(void) cond_wait(&g_quit_cv, &g_quit_lock); |
|
2115 |
(void) mutex_unlock(&g_quit_lock); |
|
2116 |
} |
|
2117 |
||
2118 |
/* Wake up manager thread, so it will exit */ |
|
2119 |
(void) mutex_lock(g_thr_pool_lock); |
|
2120 |
(void) cond_signal(&g_thr_pool_cv); |
|
2121 |
(void) mutex_unlock(g_thr_pool_lock); |
|
2122 |
||
2123 |
/* set port alert to wake any sleeping threads */ |
|
2124 |
if (port_alert(g_port, PORT_ALERT_SET, 1, NULL) < 0) { |
|
2125 |
perror("port_alert"); |
|
2126 |
exit(EXIT_FAILURE); |
|
2127 |
} |
|
2128 |
||
2129 |
/* detach doors */ |
|
2130 |
fattach_all_zones(B_TRUE); |
|
2131 |
||
2132 |
(void) door_revoke(g_door); |
|
2133 |
if (close(g_port) < 0) { |
|
2134 |
perror("close"); |
|
2135 |
exit(EXIT_FAILURE); |
|
2136 |
} |
|
2137 |
||
2138 |
/* Wait for threads to exit */ |
|
2139 |
while (thr_join(0, NULL, NULL) == 0) |
|
2140 |
; |
|
2141 |
||
2142 |
/* |
|
2143 |
* Tell any waiting listeners that we're quitting. Walk the |
|
2144 |
* listener list, writing a byte to each pipe. Then teardown |
|
2145 |
* any remaining listener structures. |
|
2146 |
*/ |
|
2147 |
(void) mutex_lock(g_listener_lock); |
|
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
2148 |
while (!TAILQ_EMPTY(&zone_listener_list)) { |
2338 | 2149 |
char pipeval = '0'; |
2150 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
2151 |
wl = TAILQ_FIRST(&zone_listener_list); |
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
2152 |
TAILQ_REMOVE(&zone_listener_list, wl, pl_list_link); |
2338 | 2153 |
(void) write(wl->pl_pipefd, &pipeval, 1); |
2154 |
free_proxy_listener(wl); |
|
2155 |
} |
|
2156 |
(void) mutex_unlock(g_listener_lock); |
|
2157 |
||
2349
4b4ff1617157
18277 zoneproxy client should release socket on orderly shutdown
johansen <johansen@opensolaris.org>
parents:
2338
diff
changeset
|
2158 |
config_free(g_proxy_config); |
2338 | 2159 |
|
2160 |
(void) mutex_destroy(g_door_thr_lock); |
|
2161 |
(void) mutex_destroy(g_listener_lock); |
|
2162 |
(void) mutex_destroy(g_thr_pool_lock); |
|
2163 |
free(g_door_thr_lock); |
|
2164 |
free(g_listener_lock); |
|
2165 |
free(g_thr_pool_lock); |
|
2166 |
||
2167 |
return (0); |
|
2168 |
} |