17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate * All rights reserved.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <stdio.h>
287c478bd9Sstevel@tonic-gate #include <stdlib.h>
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/socket.h>
317c478bd9Sstevel@tonic-gate #include <string.h>
327c478bd9Sstevel@tonic-gate #include <errno.h>
337c478bd9Sstevel@tonic-gate #include <arpa/inet.h>
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <syslog.h>
367c478bd9Sstevel@tonic-gate #include <thread.h>
377c478bd9Sstevel@tonic-gate #include <synch.h>
387c478bd9Sstevel@tonic-gate #include <netinet/in.h>
397c478bd9Sstevel@tonic-gate #include <signal.h>
407c478bd9Sstevel@tonic-gate #include <slp-internal.h>
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate #define IPC_FD_LIFETIME 30
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Cached parameters and thread synchronization
467c478bd9Sstevel@tonic-gate */
477c478bd9Sstevel@tonic-gate static int slpdfd; /* cached FD to slpd */
487c478bd9Sstevel@tonic-gate static mutex_t ipc_lock = DEFAULTMUTEX; /* serializes IPC */
497c478bd9Sstevel@tonic-gate
507c478bd9Sstevel@tonic-gate /* synch for the FD management thread */
517c478bd9Sstevel@tonic-gate static mutex_t ipc_wait_lock = DEFAULTMUTEX;
527c478bd9Sstevel@tonic-gate static cond_t ipc_wait_var;
537c478bd9Sstevel@tonic-gate static int ipc_used;
547c478bd9Sstevel@tonic-gate static int ipc_thr_running;
557c478bd9Sstevel@tonic-gate
567c478bd9Sstevel@tonic-gate static struct sockaddr_in *local_sin; /* slpd addr, set on first use */
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate static SLPError open_ipc();
597c478bd9Sstevel@tonic-gate static void close_ipc();
607c478bd9Sstevel@tonic-gate static void get_localhost_sin();
61*48d1bcbbSToomas Soome static void *ipc_manage_thr(void *);
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * Locking should be handled by the caller
657c478bd9Sstevel@tonic-gate */
open_ipc()667c478bd9Sstevel@tonic-gate static SLPError open_ipc() {
677c478bd9Sstevel@tonic-gate int terr;
687c478bd9Sstevel@tonic-gate int retries = 0;
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate if (slpdfd)
717c478bd9Sstevel@tonic-gate return (SLP_OK);
727c478bd9Sstevel@tonic-gate
737c478bd9Sstevel@tonic-gate /* Make sure the local host's sockaddr_in is set */
747c478bd9Sstevel@tonic-gate if (!local_sin) {
757c478bd9Sstevel@tonic-gate get_localhost_sin();
767c478bd9Sstevel@tonic-gate if (!local_sin) {
777c478bd9Sstevel@tonic-gate slpdfd = 0;
787c478bd9Sstevel@tonic-gate return (SLP_INTERNAL_SYSTEM_ERROR);
797c478bd9Sstevel@tonic-gate }
807c478bd9Sstevel@tonic-gate }
817c478bd9Sstevel@tonic-gate
827c478bd9Sstevel@tonic-gate for (;;) {
837c478bd9Sstevel@tonic-gate int errno_kept;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if ((slpdfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
867c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_open_ipc",
877c478bd9Sstevel@tonic-gate "could not create socket: %s", strerror(errno));
887c478bd9Sstevel@tonic-gate slpdfd = 0;
897c478bd9Sstevel@tonic-gate return (SLP_INTERNAL_SYSTEM_ERROR);
907c478bd9Sstevel@tonic-gate }
917c478bd9Sstevel@tonic-gate
927c478bd9Sstevel@tonic-gate
937c478bd9Sstevel@tonic-gate if (connect(slpdfd, (struct sockaddr *)local_sin,
947c478bd9Sstevel@tonic-gate sizeof (*local_sin)) == 0) {
957c478bd9Sstevel@tonic-gate break;
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate /* else error condition */
997c478bd9Sstevel@tonic-gate errno_kept = errno; /* in case errno is reset by slp_err */
1007c478bd9Sstevel@tonic-gate if (retries++ == 2) {
1017c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "slp_open_ipc",
1027c478bd9Sstevel@tonic-gate "could not connect to slpd: %s", strerror(errno));
1037c478bd9Sstevel@tonic-gate if (errno_kept == ECONNREFUSED)
1047c478bd9Sstevel@tonic-gate slp_err(LOG_INFO, 0, "slp_open_ipc",
1057c478bd9Sstevel@tonic-gate "is slpd running?");
1067c478bd9Sstevel@tonic-gate (void) close(slpdfd);
1077c478bd9Sstevel@tonic-gate slpdfd = 0;
1087c478bd9Sstevel@tonic-gate return (SLP_NETWORK_ERROR);
1097c478bd9Sstevel@tonic-gate } else {
1107c478bd9Sstevel@tonic-gate /* back off a little */
1117c478bd9Sstevel@tonic-gate (void) close(slpdfd);
1127c478bd9Sstevel@tonic-gate (void) sleep(1);
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate }
1157c478bd9Sstevel@tonic-gate
1167c478bd9Sstevel@tonic-gate /* We now know slpd is reachable; start the management thread */
1177c478bd9Sstevel@tonic-gate if (!ipc_thr_running) {
118*48d1bcbbSToomas Soome if ((terr = thr_create(0, 0, ipc_manage_thr,
119*48d1bcbbSToomas Soome NULL, 0, NULL)) != 0) {
1207c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_open_ipc",
1217c478bd9Sstevel@tonic-gate "could not start thread: %s",
1227c478bd9Sstevel@tonic-gate strerror(terr));
1237c478bd9Sstevel@tonic-gate return (SLP_INTERNAL_SYSTEM_ERROR);
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate ipc_thr_running = 1;
1277c478bd9Sstevel@tonic-gate
1287c478bd9Sstevel@tonic-gate return (SLP_OK);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate
close_ipc()1317c478bd9Sstevel@tonic-gate static void close_ipc() {
1327c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_lock);
1337c478bd9Sstevel@tonic-gate if (!slpdfd) {
1347c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
1357c478bd9Sstevel@tonic-gate return;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate (void) close(slpdfd);
1387c478bd9Sstevel@tonic-gate slpdfd = 0;
1397c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate /*
1437c478bd9Sstevel@tonic-gate * Sends 'msg' to slpd, placing the response in 'reply'. Caller should
1447c478bd9Sstevel@tonic-gate * free memory associated with 'reply'. All IPC is handled transparantly
1457c478bd9Sstevel@tonic-gate * by this call. Note that this call is a wrapper for slp_send2slpd_iov.
1467c478bd9Sstevel@tonic-gate * Returns SLP_NETWORK_ERROR if slpd is unreachable, SLP_OK otherwise.
1477c478bd9Sstevel@tonic-gate */
slp_send2slpd(const char * msg,char ** reply)1487c478bd9Sstevel@tonic-gate SLPError slp_send2slpd(const char *msg, char **reply) {
1497c478bd9Sstevel@tonic-gate struct iovec iov[1];
1507c478bd9Sstevel@tonic-gate iov->iov_base = (caddr_t)msg;
1517c478bd9Sstevel@tonic-gate iov->iov_len = slp_get_length(msg);
1527c478bd9Sstevel@tonic-gate
1537c478bd9Sstevel@tonic-gate return (slp_send2slpd_iov(iov, 1, reply));
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
slp_send2slpd_iov(struct iovec * msg,int iovlen,char ** reply)1567c478bd9Sstevel@tonic-gate SLPError slp_send2slpd_iov(struct iovec *msg, int iovlen, char **reply) {
1577c478bd9Sstevel@tonic-gate SLPError err;
1587c478bd9Sstevel@tonic-gate int retries = 0;
1597c478bd9Sstevel@tonic-gate struct msghdr msghdr[1];
1607c478bd9Sstevel@tonic-gate struct sigaction new, old;
1617c478bd9Sstevel@tonic-gate
1627c478bd9Sstevel@tonic-gate *reply = NULL;
1637c478bd9Sstevel@tonic-gate
1647c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_lock);
1657c478bd9Sstevel@tonic-gate /* is the connection open? */
1667c478bd9Sstevel@tonic-gate if (!slpdfd) {
1677c478bd9Sstevel@tonic-gate if ((err = open_ipc()) != SLP_OK) {
1687c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
1697c478bd9Sstevel@tonic-gate return (err);
1707c478bd9Sstevel@tonic-gate }
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate
1737c478bd9Sstevel@tonic-gate /* populate the msghdr for sendmsg */
1747c478bd9Sstevel@tonic-gate msghdr->msg_name = NULL;
1757c478bd9Sstevel@tonic-gate msghdr->msg_namelen = 0;
1767c478bd9Sstevel@tonic-gate msghdr->msg_iov = msg;
1777c478bd9Sstevel@tonic-gate msghdr->msg_iovlen = iovlen;
1787c478bd9Sstevel@tonic-gate msghdr->msg_accrights = NULL;
1797c478bd9Sstevel@tonic-gate msghdr->msg_accrightslen = 0;
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate * If slpd has been restarted while this connection is
1837c478bd9Sstevel@tonic-gate * still open, we will get a SIGPIPE when we try to write
1847c478bd9Sstevel@tonic-gate * to it. So we need to ignore SIGPIPEs for the duration of
1857c478bd9Sstevel@tonic-gate * the communication with slpd.
1867c478bd9Sstevel@tonic-gate */
1877c478bd9Sstevel@tonic-gate new.sa_handler = SIG_IGN;
1887c478bd9Sstevel@tonic-gate new.sa_flags = 0;
1897c478bd9Sstevel@tonic-gate (void) sigemptyset(&new.sa_mask);
1907c478bd9Sstevel@tonic-gate (void) sigaction(SIGPIPE, &new, &old); /* preserve old disposition */
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate while (sendmsg(slpdfd, msghdr, 0) == -1) {
1937c478bd9Sstevel@tonic-gate int errno_kept = errno;
1947c478bd9Sstevel@tonic-gate
1957c478bd9Sstevel@tonic-gate switch (errno) {
1967c478bd9Sstevel@tonic-gate case EINTR:
1977c478bd9Sstevel@tonic-gate case ENOBUFS:
1987c478bd9Sstevel@tonic-gate case ENOSR:
1997c478bd9Sstevel@tonic-gate continue;
2007c478bd9Sstevel@tonic-gate case EBADF:
2017c478bd9Sstevel@tonic-gate case ECONNRESET:
2027c478bd9Sstevel@tonic-gate case ENOTCONN:
2037c478bd9Sstevel@tonic-gate default:
2047c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
2057c478bd9Sstevel@tonic-gate close_ipc();
2067c478bd9Sstevel@tonic-gate if (retries++) {
2077c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "slp_send2slpd",
2087c478bd9Sstevel@tonic-gate "could not talk to slpd: %s",
2097c478bd9Sstevel@tonic-gate strerror(errno_kept));
2107c478bd9Sstevel@tonic-gate err = SLP_NETWORK_ERROR;
2117c478bd9Sstevel@tonic-gate goto done;
2127c478bd9Sstevel@tonic-gate }
2137c478bd9Sstevel@tonic-gate /* try re-opening the connection to slpd */
2147c478bd9Sstevel@tonic-gate if (open_ipc() == SLP_OK) {
2157c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_lock);
2167c478bd9Sstevel@tonic-gate continue;
2177c478bd9Sstevel@tonic-gate } else {
2187c478bd9Sstevel@tonic-gate err = SLP_NETWORK_ERROR;
2197c478bd9Sstevel@tonic-gate goto done;
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate }
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate err = slp_tcp_read(slpdfd, reply);
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate /*
2277c478bd9Sstevel@tonic-gate * On error slpd may close the socket; there can be a race
2287c478bd9Sstevel@tonic-gate * condition here where a following call (attempting to reuse
2297c478bd9Sstevel@tonic-gate * the socket) may send to slpd before it has closed the socket.
2307c478bd9Sstevel@tonic-gate * To prevent this, we must also close the socket on error.
2317c478bd9Sstevel@tonic-gate */
2327c478bd9Sstevel@tonic-gate if (err == SLP_OK && slp_get_errcode(*reply) != 0) {
2337c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
2347c478bd9Sstevel@tonic-gate close_ipc();
2357c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_lock);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /* notify ipc thread of call */
2397c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_wait_lock);
2407c478bd9Sstevel@tonic-gate ipc_used = 1;
2417c478bd9Sstevel@tonic-gate (void) cond_signal(&ipc_wait_var);
2427c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_wait_lock);
2437c478bd9Sstevel@tonic-gate
2447c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_lock);
2457c478bd9Sstevel@tonic-gate
2467c478bd9Sstevel@tonic-gate done:
2477c478bd9Sstevel@tonic-gate /* restore original signal disposition for SIGPIPE */
2487c478bd9Sstevel@tonic-gate (void) sigaction(SIGPIPE, &old, NULL);
2497c478bd9Sstevel@tonic-gate return (err);
2507c478bd9Sstevel@tonic-gate }
2517c478bd9Sstevel@tonic-gate
2527c478bd9Sstevel@tonic-gate /*
2537c478bd9Sstevel@tonic-gate * Sets up a sockaddr_in pointing at slpd.
2547c478bd9Sstevel@tonic-gate * After the first call, the address of slpd is cached in local_sin.
2557c478bd9Sstevel@tonic-gate *
2567c478bd9Sstevel@tonic-gate * side effect: local_sin is set to an address for slpd.
2577c478bd9Sstevel@tonic-gate */
get_localhost_sin()2587c478bd9Sstevel@tonic-gate static void get_localhost_sin() {
2597c478bd9Sstevel@tonic-gate struct sockaddr_in *sin;
2607c478bd9Sstevel@tonic-gate static mutex_t lhlock = DEFAULTMUTEX;
2617c478bd9Sstevel@tonic-gate
2627c478bd9Sstevel@tonic-gate (void) mutex_lock(&lhlock);
2637c478bd9Sstevel@tonic-gate if (local_sin) {
2647c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lhlock);
2657c478bd9Sstevel@tonic-gate return;
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate if (!(sin = calloc(1, sizeof (*sin)))) {
2697c478bd9Sstevel@tonic-gate slp_err(LOG_CRIT, 0, "get_localhost_sin", "out of memory");
2707c478bd9Sstevel@tonic-gate goto done;
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate
2737c478bd9Sstevel@tonic-gate IN_SET_LOOPBACK_ADDR(sin);
2747c478bd9Sstevel@tonic-gate sin->sin_family = AF_INET;
2757c478bd9Sstevel@tonic-gate sin->sin_port = htons(SLP_PORT);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate done:
2787c478bd9Sstevel@tonic-gate local_sin = sin;
2797c478bd9Sstevel@tonic-gate (void) mutex_unlock(&lhlock);
2807c478bd9Sstevel@tonic-gate }
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate /*
2837c478bd9Sstevel@tonic-gate * IPC management: the FD to slpd is kept open and cached to improve
2847c478bd9Sstevel@tonic-gate * performance on successive calls. The IPC management thread waits
2857c478bd9Sstevel@tonic-gate * on a condition variable; the condition is if an IPC call has been
2867c478bd9Sstevel@tonic-gate * made. If so, the thread advances the FD's expiration by IPC_FD_LIFETIME
2877c478bd9Sstevel@tonic-gate * and continues waiting for the next IPC call. After the FD has expired,
2887c478bd9Sstevel@tonic-gate * the thread closes IPC and shuts itself down.
2897c478bd9Sstevel@tonic-gate */
290*48d1bcbbSToomas Soome static void *
ipc_manage_thr(void * arg __unused)291*48d1bcbbSToomas Soome ipc_manage_thr(void *arg __unused)
292*48d1bcbbSToomas Soome {
2937c478bd9Sstevel@tonic-gate timestruc_t timeout;
2947c478bd9Sstevel@tonic-gate
2957c478bd9Sstevel@tonic-gate timeout.tv_nsec = 0;
2967c478bd9Sstevel@tonic-gate (void) mutex_lock(&ipc_wait_lock);
2977c478bd9Sstevel@tonic-gate ipc_used = 0;
2987c478bd9Sstevel@tonic-gate
2997c478bd9Sstevel@tonic-gate while (ipc_used == 0) {
3007c478bd9Sstevel@tonic-gate int err;
3017c478bd9Sstevel@tonic-gate
3027c478bd9Sstevel@tonic-gate timeout.tv_sec = IPC_FD_LIFETIME;
3037c478bd9Sstevel@tonic-gate err = cond_reltimedwait(&ipc_wait_var, &ipc_wait_lock,
304*48d1bcbbSToomas Soome &timeout);
3057c478bd9Sstevel@tonic-gate
3067c478bd9Sstevel@tonic-gate if (err == ETIME) {
3077c478bd9Sstevel@tonic-gate /* shutdown */
3087c478bd9Sstevel@tonic-gate close_ipc();
3097c478bd9Sstevel@tonic-gate ipc_thr_running = 0;
3107c478bd9Sstevel@tonic-gate (void) mutex_unlock(&ipc_wait_lock);
3117c478bd9Sstevel@tonic-gate thr_exit(NULL);
3127c478bd9Sstevel@tonic-gate } else {
3137c478bd9Sstevel@tonic-gate /* reset condition variable */
3147c478bd9Sstevel@tonic-gate ipc_used = 0;
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate }
317*48d1bcbbSToomas Soome return (NULL);
3187c478bd9Sstevel@tonic-gate }
319