xref: /illumos-gate/usr/src/lib/libslp/clib/slp_ipc.c (revision 1ca4e8df0656724dae6eea0884d84d8d4c1aabb2)
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, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <arpa/inet.h>
34 #include <unistd.h>
35 #include <syslog.h>
36 #include <thread.h>
37 #include <synch.h>
38 #include <netinet/in.h>
39 #include <signal.h>
40 #include <slp-internal.h>
41 
42 #define	IPC_FD_LIFETIME	30
43 
44 /*
45  * Cached parameters and thread synchronization
46  */
47 static int slpdfd;			/* cached FD to slpd */
48 static mutex_t ipc_lock = DEFAULTMUTEX;	/* serializes IPC */
49 
50 /* synch for the FD management thread */
51 static mutex_t ipc_wait_lock = DEFAULTMUTEX;
52 static cond_t ipc_wait_var;
53 static int ipc_used;
54 static int ipc_thr_running;
55 
56 static struct sockaddr_in *local_sin;	/* slpd addr, set on first use */
57 
58 static SLPError open_ipc();
59 static void close_ipc();
60 static void get_localhost_sin();
61 static void ipc_manage_thr();
62 
63 /*
64  * Locking should be handled by the caller
65  */
66 static SLPError open_ipc() {
67 	int terr;
68 	int retries = 0;
69 
70 	if (slpdfd)
71 		return (SLP_OK);
72 
73 	/* Make sure the local host's sockaddr_in is set */
74 	if (!local_sin) {
75 		get_localhost_sin();
76 		if (!local_sin) {
77 			slpdfd = 0;
78 			return (SLP_INTERNAL_SYSTEM_ERROR);
79 		}
80 	}
81 
82 	for (;;) {
83 	    int errno_kept;
84 
85 	    if ((slpdfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
86 		slp_err(LOG_CRIT, 0, "slp_open_ipc",
87 			"could not create socket: %s", strerror(errno));
88 		slpdfd = 0;
89 		return (SLP_INTERNAL_SYSTEM_ERROR);
90 	    }
91 
92 
93 	    if (connect(slpdfd, (struct sockaddr *)local_sin,
94 			sizeof (*local_sin)) == 0) {
95 		break;
96 	    }
97 
98 	    /* else error condition */
99 	    errno_kept = errno; /* in case errno is reset by slp_err */
100 	    if (retries++ == 2) {
101 		slp_err(LOG_INFO, 0, "slp_open_ipc",
102 			"could not connect to slpd: %s", strerror(errno));
103 		if (errno_kept == ECONNREFUSED)
104 		    slp_err(LOG_INFO, 0, "slp_open_ipc",
105 			    "is slpd running?");
106 		(void) close(slpdfd);
107 		slpdfd = 0;
108 		return (SLP_NETWORK_ERROR);
109 	    } else {
110 		/* back off a little */
111 		(void) close(slpdfd);
112 		(void) sleep(1);
113 	    }
114 	}
115 
116 	/* We now know slpd is reachable; start the management thread */
117 	if (!ipc_thr_running) {
118 		if ((terr = thr_create(
119 			0, 0,
120 			(void *(*)(void *)) ipc_manage_thr,
121 			NULL, 0, NULL)) != 0) {
122 			slp_err(LOG_CRIT, 0, "slp_open_ipc",
123 				"could not start thread: %s",
124 				strerror(terr));
125 			return (SLP_INTERNAL_SYSTEM_ERROR);
126 		}
127 	}
128 	ipc_thr_running = 1;
129 
130 	return (SLP_OK);
131 }
132 
133 static void close_ipc() {
134 	(void) mutex_lock(&ipc_lock);
135 	if (!slpdfd) {
136 		(void) mutex_unlock(&ipc_lock);
137 		return;
138 	}
139 	(void) close(slpdfd);
140 	slpdfd = 0;
141 	(void) mutex_unlock(&ipc_lock);
142 }
143 
144 /*
145  * Sends 'msg' to slpd, placing the response in 'reply'. Caller should
146  * free memory associated with 'reply'. All IPC is handled transparantly
147  * by this call. Note that this call is a wrapper for slp_send2slpd_iov.
148  * Returns SLP_NETWORK_ERROR if slpd is unreachable, SLP_OK otherwise.
149  */
150 SLPError slp_send2slpd(const char *msg, char **reply) {
151 	struct iovec iov[1];
152 	iov->iov_base = (caddr_t)msg;
153 	iov->iov_len = slp_get_length(msg);
154 
155 	return (slp_send2slpd_iov(iov, 1, reply));
156 }
157 
158 SLPError slp_send2slpd_iov(struct iovec *msg, int iovlen, char **reply) {
159 	SLPError err;
160 	int retries = 0;
161 	struct msghdr msghdr[1];
162 	struct sigaction new, old;
163 
164 	*reply = NULL;
165 
166 	(void) mutex_lock(&ipc_lock);
167 	/* is the connection open? */
168 	if (!slpdfd) {
169 		if ((err = open_ipc()) != SLP_OK) {
170 			(void) mutex_unlock(&ipc_lock);
171 			return (err);
172 		}
173 	}
174 
175 	/* populate the msghdr for sendmsg */
176 	msghdr->msg_name = NULL;
177 	msghdr->msg_namelen = 0;
178 	msghdr->msg_iov = msg;
179 	msghdr->msg_iovlen = iovlen;
180 	msghdr->msg_accrights = NULL;
181 	msghdr->msg_accrightslen = 0;
182 
183 	/*
184 	 * If slpd has been restarted while this connection is
185 	 * still open, we will get a SIGPIPE when we try to write
186 	 * to it. So we need to ignore SIGPIPEs for the duration of
187 	 * the communication with slpd.
188 	 */
189 	new.sa_handler = SIG_IGN;
190 	new.sa_flags = 0;
191 	(void) sigemptyset(&new.sa_mask);
192 	(void) sigaction(SIGPIPE, &new, &old);	/* preserve old disposition */
193 
194 	while (sendmsg(slpdfd, msghdr, 0) == -1) {
195 		int errno_kept = errno;
196 
197 		switch (errno) {
198 		case EINTR:
199 		case ENOBUFS:
200 		case ENOSR:
201 			continue;
202 		case EBADF:
203 		case ECONNRESET:
204 		case ENOTCONN:
205 		default:
206 			(void) mutex_unlock(&ipc_lock);
207 			close_ipc();
208 			if (retries++) {
209 				slp_err(LOG_CRIT, 0, "slp_send2slpd",
210 					"could not talk to slpd: %s",
211 					strerror(errno_kept));
212 				err = SLP_NETWORK_ERROR;
213 				goto done;
214 			}
215 			/* try re-opening the connection to slpd */
216 			if (open_ipc() == SLP_OK) {
217 				(void) mutex_lock(&ipc_lock);
218 				continue;
219 			} else {
220 				err = SLP_NETWORK_ERROR;
221 				goto done;
222 			}
223 		}
224 	}
225 
226 	err = slp_tcp_read(slpdfd, reply);
227 
228 	/*
229 	 * On error slpd may close the socket; there can be a race
230 	 * condition here where a following call (attempting to reuse
231 	 * the socket) may send to slpd before it has closed the socket.
232 	 * To prevent this, we must also close the socket on error.
233 	 */
234 	if (err == SLP_OK && slp_get_errcode(*reply) != 0) {
235 		(void) mutex_unlock(&ipc_lock);
236 		close_ipc();
237 		(void) mutex_lock(&ipc_lock);
238 	}
239 
240 	/* notify ipc thread of call */
241 	(void) mutex_lock(&ipc_wait_lock);
242 	ipc_used = 1;
243 	(void) cond_signal(&ipc_wait_var);
244 	(void) mutex_unlock(&ipc_wait_lock);
245 
246 	(void) mutex_unlock(&ipc_lock);
247 
248 done:
249 	/* restore original signal disposition for SIGPIPE */
250 	(void) sigaction(SIGPIPE, &old, NULL);
251 	return (err);
252 }
253 
254 /*
255  * Sets up a sockaddr_in pointing at slpd.
256  * After the first call, the address of slpd is cached in local_sin.
257  *
258  * side effect: local_sin is set to an address for slpd.
259  */
260 static void get_localhost_sin() {
261 	struct sockaddr_in *sin;
262 	static mutex_t lhlock = DEFAULTMUTEX;
263 
264 	(void) mutex_lock(&lhlock);
265 	if (local_sin) {
266 		(void) mutex_unlock(&lhlock);
267 		return;
268 	}
269 
270 	if (!(sin = calloc(1, sizeof (*sin)))) {
271 		slp_err(LOG_CRIT, 0, "get_localhost_sin", "out of memory");
272 		goto done;
273 	}
274 
275 	IN_SET_LOOPBACK_ADDR(sin);
276 	sin->sin_family = AF_INET;
277 	sin->sin_port = htons(SLP_PORT);
278 
279 done:
280 	local_sin = sin;
281 	(void) mutex_unlock(&lhlock);
282 }
283 
284 /*
285  * IPC management: the FD to slpd is kept open and cached to improve
286  * performance on successive calls. The IPC management thread waits
287  * on a condition variable; the condition is if an IPC call has been
288  * made. If so, the thread advances the FD's expiration by IPC_FD_LIFETIME
289  * and continues waiting for the next IPC call. After the FD has expired,
290  * the thread closes IPC and shuts itself down.
291  */
292 static void ipc_manage_thr() {
293 	timestruc_t timeout;
294 
295 	timeout.tv_nsec = 0;
296 	(void) mutex_lock(&ipc_wait_lock);
297 	ipc_used = 0;
298 
299 	while (ipc_used == 0) {
300 		int err;
301 
302 		timeout.tv_sec = IPC_FD_LIFETIME;
303 		err = cond_reltimedwait(&ipc_wait_var, &ipc_wait_lock,
304 			&timeout);
305 
306 		if (err == ETIME) {
307 			/* shutdown */
308 			close_ipc();
309 			ipc_thr_running = 0;
310 			(void) mutex_unlock(&ipc_wait_lock);
311 			thr_exit(NULL);
312 		} else {
313 			/* reset condition variable */
314 			ipc_used = 0;
315 		}
316 	}
317 }
318