1fcf3ce44SJohn Forte /* 2fcf3ce44SJohn Forte * CDDL HEADER START 3fcf3ce44SJohn Forte * 4fcf3ce44SJohn Forte * The contents of this file are subject to the terms of the 5fcf3ce44SJohn Forte * Common Development and Distribution License (the "License"). 6fcf3ce44SJohn Forte * You may not use this file except in compliance with the License. 7fcf3ce44SJohn Forte * 8fcf3ce44SJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fcf3ce44SJohn Forte * or http://www.opensolaris.org/os/licensing. 10fcf3ce44SJohn Forte * See the License for the specific language governing permissions 11fcf3ce44SJohn Forte * and limitations under the License. 12fcf3ce44SJohn Forte * 13fcf3ce44SJohn Forte * When distributing Covered Code, include this CDDL HEADER in each 14fcf3ce44SJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fcf3ce44SJohn Forte * If applicable, add the following below this CDDL HEADER, with the 16fcf3ce44SJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying 17fcf3ce44SJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner] 18fcf3ce44SJohn Forte * 19fcf3ce44SJohn Forte * CDDL HEADER END 20fcf3ce44SJohn Forte */ 21570de38fSSurya Prakki 22fcf3ce44SJohn Forte /* 23570de38fSSurya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 24fcf3ce44SJohn Forte * Use is subject to license terms. 25fcf3ce44SJohn Forte */ 26*55393845SMarcel Telka /* 27*55393845SMarcel Telka * Copyright 2014 Nexenta Systems, Inc. All rights reserved. 28*55393845SMarcel Telka */ 29fcf3ce44SJohn Forte 30fcf3ce44SJohn Forte /* 31fcf3ce44SJohn Forte * Network SNDR/ncall-ip server - based on nfsd 32fcf3ce44SJohn Forte */ 33fcf3ce44SJohn Forte #include <sys/types.h> 34fcf3ce44SJohn Forte #include <rpc/types.h> 35fcf3ce44SJohn Forte #include <errno.h> 36fcf3ce44SJohn Forte #include <netdb.h> 37fcf3ce44SJohn Forte #include <sys/socket.h> 38fcf3ce44SJohn Forte #include <netconfig.h> 39fcf3ce44SJohn Forte #include <stropts.h> 40fcf3ce44SJohn Forte #include <fcntl.h> 41fcf3ce44SJohn Forte #include <stdio.h> 42fcf3ce44SJohn Forte #include <strings.h> 43fcf3ce44SJohn Forte #include <signal.h> 44fcf3ce44SJohn Forte #include <unistd.h> 45fcf3ce44SJohn Forte #include <stdlib.h> 46fcf3ce44SJohn Forte #include <netdir.h> 47fcf3ce44SJohn Forte #include <rpc/rpc_com.h> 48fcf3ce44SJohn Forte #include <rpc/rpc.h> 49fcf3ce44SJohn Forte #include <tiuser.h> 50fcf3ce44SJohn Forte #include <netinet/tcp.h> 51fcf3ce44SJohn Forte #include <netinet/in.h> 52fcf3ce44SJohn Forte #include <syslog.h> 53fcf3ce44SJohn Forte #include <locale.h> 54fcf3ce44SJohn Forte #include <langinfo.h> 55fcf3ce44SJohn Forte #include <libintl.h> 56fcf3ce44SJohn Forte #include <libgen.h> 57fcf3ce44SJohn Forte #include <deflt.h> 58fcf3ce44SJohn Forte #include <sys/resource.h> 59fcf3ce44SJohn Forte 60fcf3ce44SJohn Forte #include <sys/nsctl/nsctl.h> 61fcf3ce44SJohn Forte 62fcf3ce44SJohn Forte #ifdef __NCALL__ 63fcf3ce44SJohn Forte 64fcf3ce44SJohn Forte #include <sys/ncall/ncall.h> 65fcf3ce44SJohn Forte #include <sys/ncall/ncall_ip.h> 66fcf3ce44SJohn Forte #include <sys/nsctl/libncall.h> 67fcf3ce44SJohn Forte 68fcf3ce44SJohn Forte #define RDC_POOL_CREATE NC_IOC_POOL_CREATE 69fcf3ce44SJohn Forte #define RDC_POOL_RUN NC_IOC_POOL_RUN 70fcf3ce44SJohn Forte #define RDC_POOL_WAIT NC_IOC_POOL_WAIT 71fcf3ce44SJohn Forte #define RDC_PROGRAM NCALL_PROGRAM 72fcf3ce44SJohn Forte #define RDC_SERVICE "ncall" 73fcf3ce44SJohn Forte #undef RDC_SVCPOOL_ID /* We are overloading this value */ 74fcf3ce44SJohn Forte #define RDC_SVCPOOL_ID NCALL_SVCPOOL_ID 75fcf3ce44SJohn Forte #define RDC_SVC_NAME "NCALL" 76fcf3ce44SJohn Forte #define RDC_VERS_MIN NCALL_VERS_MIN 77fcf3ce44SJohn Forte #define RDC_VERS_MAX NCALL_VERS_MAX 78fcf3ce44SJohn Forte 79fcf3ce44SJohn Forte #else /* !__NCALL__ */ 80fcf3ce44SJohn Forte 81fcf3ce44SJohn Forte #include <sys/nsctl/rdc_ioctl.h> 82fcf3ce44SJohn Forte #include <sys/nsctl/rdc_io.h> 83fcf3ce44SJohn Forte #include <sys/nsctl/librdc.h> 84fcf3ce44SJohn Forte 85fcf3ce44SJohn Forte #define RDC_SERVICE "rdc" 86fcf3ce44SJohn Forte #define RDC_SVC_NAME "RDC" 87fcf3ce44SJohn Forte 88fcf3ce44SJohn Forte #endif /* __NCALL__ */ 89fcf3ce44SJohn Forte 90fcf3ce44SJohn Forte #define RDCADMIN "/etc/default/sndr" 91fcf3ce44SJohn Forte 92fcf3ce44SJohn Forte #include <nsctl.h> 93fcf3ce44SJohn Forte 94fcf3ce44SJohn Forte struct conn_ind { 95fcf3ce44SJohn Forte struct conn_ind *conn_next; 96fcf3ce44SJohn Forte struct conn_ind *conn_prev; 97fcf3ce44SJohn Forte struct t_call *conn_call; 98fcf3ce44SJohn Forte }; 99fcf3ce44SJohn Forte 100fcf3ce44SJohn Forte struct conn_entry { 101fcf3ce44SJohn Forte bool_t closing; 102fcf3ce44SJohn Forte struct netconfig nc; 103fcf3ce44SJohn Forte }; 104fcf3ce44SJohn Forte 105fcf3ce44SJohn Forte static char *progname; 106fcf3ce44SJohn Forte static struct conn_entry *conn_polled; 107fcf3ce44SJohn Forte static int num_conns; /* Current number of connections */ 108fcf3ce44SJohn Forte static struct pollfd *poll_array; /* array of poll descriptors for poll */ 109fcf3ce44SJohn Forte static size_t num_fds = 0; /* number of transport fds opened */ 110fcf3ce44SJohn Forte static void poll_for_action(); 111fcf3ce44SJohn Forte static void remove_from_poll_list(int); 112fcf3ce44SJohn Forte static int do_poll_cots_action(int, int); 113fcf3ce44SJohn Forte static int do_poll_clts_action(int, int); 114fcf3ce44SJohn Forte static void add_to_poll_list(int, struct netconfig *); 115fcf3ce44SJohn Forte static int bind_to_provider(char *, char *, struct netbuf **, 116fcf3ce44SJohn Forte struct netconfig **); 117fcf3ce44SJohn Forte static int set_addrmask(int, struct netconfig *, struct netbuf *); 118fcf3ce44SJohn Forte static void conn_close_oldest(void); 119fcf3ce44SJohn Forte static boolean_t conn_get(int, struct netconfig *, struct conn_ind **); 120fcf3ce44SJohn Forte static void cots_listen_event(int, int); 121fcf3ce44SJohn Forte static int discon_get(int, struct netconfig *, struct conn_ind **); 122fcf3ce44SJohn Forte static int nofile_increase(int); 123fcf3ce44SJohn Forte static int is_listen_fd_index(int); 124fcf3ce44SJohn Forte #if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8) 125fcf3ce44SJohn Forte static int sndrsvcpool(int); 126fcf3ce44SJohn Forte static int svcwait(int id); 127fcf3ce44SJohn Forte #endif 128fcf3ce44SJohn Forte 129fcf3ce44SJohn Forte 130fcf3ce44SJohn Forte /* 131fcf3ce44SJohn Forte * RPC protocol block. Useful for passing registration information. 132fcf3ce44SJohn Forte */ 133fcf3ce44SJohn Forte struct protob { 134fcf3ce44SJohn Forte char *serv; /* ASCII service name, e.g. "RDC" */ 135fcf3ce44SJohn Forte int versmin; /* minimum version no. to be registered */ 136fcf3ce44SJohn Forte int versmax; /* maximum version no. to be registered */ 137fcf3ce44SJohn Forte int program; /* program no. to be registered */ 138fcf3ce44SJohn Forte struct protob *next; /* next entry on list */ 139fcf3ce44SJohn Forte }; 140fcf3ce44SJohn Forte 141fcf3ce44SJohn Forte 142fcf3ce44SJohn Forte 143fcf3ce44SJohn Forte static size_t end_listen_fds; 144fcf3ce44SJohn Forte static int debugflg = 0; 145fcf3ce44SJohn Forte static int max_conns_allowed = -1; 146fcf3ce44SJohn Forte static int listen_backlog = 10; 147fcf3ce44SJohn Forte static char *trans_provider = (char *)NULL; 148fcf3ce44SJohn Forte static int rdcsvc(int, struct netbuf, struct netconfig *); 149fcf3ce44SJohn Forte 150fcf3ce44SJohn Forte /* used by cots_listen_event() */ 151fcf3ce44SJohn Forte static int (*Mysvc)(int, struct netbuf, struct netconfig *) = rdcsvc; 152fcf3ce44SJohn Forte 153fcf3ce44SJohn Forte /* 154fcf3ce44SJohn Forte * Determine valid semantics for rdc. 155fcf3ce44SJohn Forte */ 156fcf3ce44SJohn Forte #define OK_TPI_TYPE(_nconf) \ 157fcf3ce44SJohn Forte (_nconf->nc_semantics == NC_TPI_CLTS || \ 158fcf3ce44SJohn Forte _nconf->nc_semantics == NC_TPI_COTS || \ 159fcf3ce44SJohn Forte _nconf->nc_semantics == NC_TPI_COTS_ORD) 160fcf3ce44SJohn Forte 161fcf3ce44SJohn Forte #define BE32_TO_U32(a) \ 162fcf3ce44SJohn Forte ((((uint32_t)((uchar_t *)a)[0] & 0xFF) << (uint32_t)24) |\ 163fcf3ce44SJohn Forte (((uint32_t)((uchar_t *)a)[1] & 0xFF) << (uint32_t)16) |\ 164fcf3ce44SJohn Forte (((uint32_t)((uchar_t *)a)[2] & 0xFF) << (uint32_t)8) |\ 165fcf3ce44SJohn Forte ((uint32_t)((uchar_t *)a)[3] & 0xFF)) 166fcf3ce44SJohn Forte 167fcf3ce44SJohn Forte #ifdef DEBUG 168fcf3ce44SJohn Forte /* 169fcf3ce44SJohn Forte * Only support UDP in DEBUG mode for now 170fcf3ce44SJohn Forte */ 171fcf3ce44SJohn Forte static char *defaultproviders[] = { "/dev/tcp", "/dev/tcp6", "/dev/udp", 172fcf3ce44SJohn Forte "/dev/udp6", NULL }; 173fcf3ce44SJohn Forte #else 174fcf3ce44SJohn Forte static char *defaultproviders[] = { "/dev/tcp6", "/dev/tcp", NULL }; 175fcf3ce44SJohn Forte #endif 176fcf3ce44SJohn Forte 177fcf3ce44SJohn Forte /* 178fcf3ce44SJohn Forte * Number of elements to add to the poll array on each allocation. 179fcf3ce44SJohn Forte */ 180fcf3ce44SJohn Forte #define POLL_ARRAY_INC_SIZE 64 181fcf3ce44SJohn Forte #define NOFILE_INC_SIZE 64 182fcf3ce44SJohn Forte 183fcf3ce44SJohn Forte #ifdef __NCALL__ 184fcf3ce44SJohn Forte const char *rdc_devr = "/dev/ncallip"; 185fcf3ce44SJohn Forte #else 186fcf3ce44SJohn Forte const char *rdc_devr = "/dev/rdc"; 187fcf3ce44SJohn Forte #endif 188fcf3ce44SJohn Forte 189fcf3ce44SJohn Forte static int rdc_fdr; 190fcf3ce44SJohn Forte static int 191fcf3ce44SJohn Forte 192fcf3ce44SJohn Forte open_rdc(void) 193fcf3ce44SJohn Forte { 194fcf3ce44SJohn Forte int fd = open(rdc_devr, O_RDONLY); 195fcf3ce44SJohn Forte 196fcf3ce44SJohn Forte if (fd < 0) 197fcf3ce44SJohn Forte return (-1); 198fcf3ce44SJohn Forte 199fcf3ce44SJohn Forte return (rdc_fdr = fd); 200fcf3ce44SJohn Forte } 201fcf3ce44SJohn Forte 202fcf3ce44SJohn Forte static int 203fcf3ce44SJohn Forte sndrsys(int type, void *arg) 204fcf3ce44SJohn Forte { 205fcf3ce44SJohn Forte int ret = -1; 206fcf3ce44SJohn Forte if (!rdc_fdr && open_rdc() < 0) { /* open failed */ 207fcf3ce44SJohn Forte syslog(LOG_ERR, "open_rdc() failed: %m\n"); 208fcf3ce44SJohn Forte } else { 209fcf3ce44SJohn Forte if ((ret = ioctl(rdc_fdr, type, arg)) < 0) { 210fcf3ce44SJohn Forte syslog(LOG_ERR, "ioctl(rdc_ioctl) failed: %m\n"); 211fcf3ce44SJohn Forte } 212fcf3ce44SJohn Forte } 213fcf3ce44SJohn Forte return (ret); 214fcf3ce44SJohn Forte } 215fcf3ce44SJohn Forte 216fcf3ce44SJohn Forte int 217fcf3ce44SJohn Forte rdc_transport_open(struct netconfig *nconf) 218fcf3ce44SJohn Forte { 219fcf3ce44SJohn Forte int fd; 220fcf3ce44SJohn Forte struct strioctl strioc; 221fcf3ce44SJohn Forte 222fcf3ce44SJohn Forte if ((nconf == (struct netconfig *)NULL) || 223fcf3ce44SJohn Forte (nconf->nc_device == (char *)NULL)) { 224fcf3ce44SJohn Forte syslog(LOG_ERR, "No netconfig device"); 225fcf3ce44SJohn Forte return (-1); 226fcf3ce44SJohn Forte } 227fcf3ce44SJohn Forte 228fcf3ce44SJohn Forte /* 229fcf3ce44SJohn Forte * Open the transport device. 230fcf3ce44SJohn Forte */ 231fcf3ce44SJohn Forte fd = t_open(nconf->nc_device, O_RDWR, (struct t_info *)NULL); 232fcf3ce44SJohn Forte if (fd == -1) { 233fcf3ce44SJohn Forte if (t_errno == TSYSERR && errno == EMFILE && 234fcf3ce44SJohn Forte (nofile_increase(0) == 0)) { 235fcf3ce44SJohn Forte /* Try again with a higher NOFILE limit. */ 236*55393845SMarcel Telka fd = t_open(nconf->nc_device, O_RDWR, NULL); 237fcf3ce44SJohn Forte } 238fcf3ce44SJohn Forte if (fd == -1) { 239fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 240fcf3ce44SJohn Forte syslog(LOG_ERR, "t_open failed: %m"); 241fcf3ce44SJohn Forte } else { 242fcf3ce44SJohn Forte syslog(LOG_ERR, "t_open failed: %s", 243fcf3ce44SJohn Forte t_errlist[t_errno]); 244fcf3ce44SJohn Forte } 245fcf3ce44SJohn Forte return (-1); 246fcf3ce44SJohn Forte } 247fcf3ce44SJohn Forte } 248fcf3ce44SJohn Forte 249fcf3ce44SJohn Forte /* 250fcf3ce44SJohn Forte * Pop timod because the RPC module must be as close as possible 251fcf3ce44SJohn Forte * to the transport. 252fcf3ce44SJohn Forte */ 253fcf3ce44SJohn Forte if (ioctl(fd, I_POP, 0) < 0) { 254fcf3ce44SJohn Forte syslog(LOG_ERR, "I_POP of timod failed: %m"); 255fcf3ce44SJohn Forte if (t_close(fd) == -1) { 256fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 257fcf3ce44SJohn Forte syslog(LOG_ERR, "t_close failed on %d: %m", fd); 258fcf3ce44SJohn Forte } else { 259fcf3ce44SJohn Forte syslog(LOG_ERR, "t_close failed on %d: %s", 260fcf3ce44SJohn Forte fd, t_errlist[t_errno]); 261fcf3ce44SJohn Forte } 262fcf3ce44SJohn Forte } 263fcf3ce44SJohn Forte return (-1); 264fcf3ce44SJohn Forte } 265fcf3ce44SJohn Forte 266fcf3ce44SJohn Forte if (nconf->nc_semantics == NC_TPI_CLTS) { 267fcf3ce44SJohn Forte /* 268fcf3ce44SJohn Forte * Push rpcmod to filter data traffic to KRPC. 269fcf3ce44SJohn Forte */ 270fcf3ce44SJohn Forte if (ioctl(fd, I_PUSH, "rpcmod") < 0) { 271fcf3ce44SJohn Forte syslog(LOG_ERR, "I_PUSH of rpcmod failed: %m"); 272fcf3ce44SJohn Forte (void) t_close(fd); 273fcf3ce44SJohn Forte return (-1); 274fcf3ce44SJohn Forte } 275fcf3ce44SJohn Forte } else { 276fcf3ce44SJohn Forte if (ioctl(fd, I_PUSH, "rpcmod") < 0) { 277fcf3ce44SJohn Forte syslog(LOG_ERR, "I_PUSH of CONS rpcmod failed: %m"); 278fcf3ce44SJohn Forte if (t_close(fd) == -1) { 279fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 280fcf3ce44SJohn Forte syslog(LOG_ERR, 281fcf3ce44SJohn Forte "t_close failed on %d: %m", fd); 282fcf3ce44SJohn Forte } else { 283fcf3ce44SJohn Forte syslog(LOG_ERR, 284fcf3ce44SJohn Forte "t_close failed on %d: %s", 285fcf3ce44SJohn Forte fd, t_errlist[t_errno]); 286fcf3ce44SJohn Forte } 287fcf3ce44SJohn Forte } 288fcf3ce44SJohn Forte return (-1); 289fcf3ce44SJohn Forte } 290fcf3ce44SJohn Forte 291fcf3ce44SJohn Forte strioc.ic_cmd = RPC_SERVER; 292fcf3ce44SJohn Forte strioc.ic_dp = (char *)0; 293fcf3ce44SJohn Forte strioc.ic_len = 0; 294fcf3ce44SJohn Forte strioc.ic_timout = -1; 295fcf3ce44SJohn Forte /* Tell CONS rpcmod to act like a server stream. */ 296fcf3ce44SJohn Forte if (ioctl(fd, I_STR, &strioc) < 0) { 297fcf3ce44SJohn Forte syslog(LOG_ERR, "CONS rpcmod set-up ioctl failed: %m"); 298fcf3ce44SJohn Forte if (t_close(fd) == -1) { 299fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 300fcf3ce44SJohn Forte syslog(LOG_ERR, 301fcf3ce44SJohn Forte "t_close failed on %d: %m", fd); 302fcf3ce44SJohn Forte } else { 303fcf3ce44SJohn Forte syslog(LOG_ERR, 304fcf3ce44SJohn Forte "t_close failed on %d: %s", 305fcf3ce44SJohn Forte fd, t_errlist[t_errno]); 306fcf3ce44SJohn Forte } 307fcf3ce44SJohn Forte } 308fcf3ce44SJohn Forte return (-1); 309fcf3ce44SJohn Forte } 310fcf3ce44SJohn Forte } 311fcf3ce44SJohn Forte 312fcf3ce44SJohn Forte /* 313fcf3ce44SJohn Forte * Re-push timod so that we will still be doing TLI 314fcf3ce44SJohn Forte * operations on the descriptor. 315fcf3ce44SJohn Forte */ 316fcf3ce44SJohn Forte if (ioctl(fd, I_PUSH, "timod") < 0) { 317fcf3ce44SJohn Forte syslog(LOG_ERR, "I_PUSH of timod failed: %m"); 318fcf3ce44SJohn Forte if (t_close(fd) == -1) { 319fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 320fcf3ce44SJohn Forte syslog(LOG_ERR, "t_close failed on %d: %m", fd); 321fcf3ce44SJohn Forte } else { 322fcf3ce44SJohn Forte syslog(LOG_ERR, "t_close failed on %d: %s", 323fcf3ce44SJohn Forte fd, t_errlist[t_errno]); 324fcf3ce44SJohn Forte } 325fcf3ce44SJohn Forte } 326fcf3ce44SJohn Forte return (-1); 327fcf3ce44SJohn Forte } 328fcf3ce44SJohn Forte 329fcf3ce44SJohn Forte return (fd); 330fcf3ce44SJohn Forte } 331fcf3ce44SJohn Forte 332fcf3ce44SJohn Forte 333fcf3ce44SJohn Forte void 334fcf3ce44SJohn Forte rdcd_log_tli_error(char *tli_name, int fd, struct netconfig *nconf) 335fcf3ce44SJohn Forte { 336fcf3ce44SJohn Forte int error; 337fcf3ce44SJohn Forte 338fcf3ce44SJohn Forte /* 339fcf3ce44SJohn Forte * Save the error code across syslog(), just in case syslog() 340fcf3ce44SJohn Forte * gets its own error and, therefore, overwrites errno. 341fcf3ce44SJohn Forte */ 342fcf3ce44SJohn Forte error = errno; 343fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 344fcf3ce44SJohn Forte syslog(LOG_ERR, "%s(file descriptor %d/transport %s) %m", 345fcf3ce44SJohn Forte tli_name, fd, nconf->nc_proto); 346fcf3ce44SJohn Forte } else { 347fcf3ce44SJohn Forte syslog(LOG_ERR, 348fcf3ce44SJohn Forte "%s(file descriptor %d/transport %s) TLI error %d", 349fcf3ce44SJohn Forte tli_name, fd, nconf->nc_proto, t_errno); 350fcf3ce44SJohn Forte } 351fcf3ce44SJohn Forte errno = error; 352fcf3ce44SJohn Forte } 353fcf3ce44SJohn Forte 354fcf3ce44SJohn Forte /* 355fcf3ce44SJohn Forte * Called to set up service over a particular transport 356fcf3ce44SJohn Forte */ 357fcf3ce44SJohn Forte void 358fcf3ce44SJohn Forte do_one(char *provider, char *proto, struct protob *protobp0, 359fcf3ce44SJohn Forte int (*svc)(int, struct netbuf, struct netconfig *)) 360fcf3ce44SJohn Forte { 361fcf3ce44SJohn Forte struct netbuf *retaddr; 362fcf3ce44SJohn Forte struct netconfig *retnconf; 363fcf3ce44SJohn Forte struct netbuf addrmask; 364fcf3ce44SJohn Forte int vers; 365fcf3ce44SJohn Forte int sock; 366fcf3ce44SJohn Forte 367fcf3ce44SJohn Forte if (provider) { 368fcf3ce44SJohn Forte sock = bind_to_provider(provider, protobp0->serv, &retaddr, 369fcf3ce44SJohn Forte &retnconf); 370fcf3ce44SJohn Forte } else { 371fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 372fcf3ce44SJohn Forte "Cannot establish %s service over %s: transport setup problem.", 373fcf3ce44SJohn Forte protobp0->serv, provider ? provider : proto); 374fcf3ce44SJohn Forte return; 375fcf3ce44SJohn Forte } 376fcf3ce44SJohn Forte 377fcf3ce44SJohn Forte if (sock == -1) { 378fcf3ce44SJohn Forte if ((Is_ipv6present() && 379fcf3ce44SJohn Forte (strcmp(provider, "/dev/tcp6") == 0)) || 380fcf3ce44SJohn Forte (!Is_ipv6present() && (strcmp(provider, "/dev/tcp") == 0))) 381fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 382fcf3ce44SJohn Forte "Cannot establish %s service over %s: transport " 383fcf3ce44SJohn Forte "setup problem.", 384fcf3ce44SJohn Forte protobp0->serv, provider ? provider : proto); 385fcf3ce44SJohn Forte return; 386fcf3ce44SJohn Forte } 387fcf3ce44SJohn Forte 388fcf3ce44SJohn Forte if (set_addrmask(sock, retnconf, &addrmask) < 0) { 389fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 390fcf3ce44SJohn Forte "Cannot set address mask for %s", retnconf->nc_netid); 391fcf3ce44SJohn Forte return; 392fcf3ce44SJohn Forte } 393fcf3ce44SJohn Forte 394fcf3ce44SJohn Forte 395fcf3ce44SJohn Forte /* 396fcf3ce44SJohn Forte * Register all versions of the programs in the protocol block list 397fcf3ce44SJohn Forte */ 398fcf3ce44SJohn Forte for (vers = protobp0->versmin; vers <= protobp0->versmax; vers++) { 399fcf3ce44SJohn Forte (void) rpcb_unset(protobp0->program, vers, retnconf); 400fcf3ce44SJohn Forte (void) rpcb_set(protobp0->program, vers, retnconf, retaddr); 401fcf3ce44SJohn Forte } 402fcf3ce44SJohn Forte 403fcf3ce44SJohn Forte if (retnconf->nc_semantics == NC_TPI_CLTS) { 404fcf3ce44SJohn Forte /* Don't drop core if supporting module(s) aren't loaded. */ 405fcf3ce44SJohn Forte (void) signal(SIGSYS, SIG_IGN); 406fcf3ce44SJohn Forte 407fcf3ce44SJohn Forte /* 408fcf3ce44SJohn Forte * svc() doesn't block, it returns success or failure. 409fcf3ce44SJohn Forte */ 410fcf3ce44SJohn Forte if ((*svc)(sock, addrmask, retnconf) < 0) { 411*55393845SMarcel Telka (void) syslog(LOG_ERR, "Cannot establish %s service " 412*55393845SMarcel Telka "over <file desc. %d, protocol %s> : %m. Exiting", 413fcf3ce44SJohn Forte protobp0->serv, sock, retnconf->nc_proto); 414fcf3ce44SJohn Forte exit(1); 415fcf3ce44SJohn Forte } 416fcf3ce44SJohn Forte } 417fcf3ce44SJohn Forte /* 418fcf3ce44SJohn Forte * We successfully set up the server over this transport. 419fcf3ce44SJohn Forte * Add this descriptor to the one being polled on. 420fcf3ce44SJohn Forte */ 421fcf3ce44SJohn Forte add_to_poll_list(sock, retnconf); 422fcf3ce44SJohn Forte } 423fcf3ce44SJohn Forte 424fcf3ce44SJohn Forte /* 425fcf3ce44SJohn Forte * Set up the SNDR/ncall-ip service over all the available transports. 426fcf3ce44SJohn Forte * Returns -1 for failure, 0 for success. 427fcf3ce44SJohn Forte */ 428fcf3ce44SJohn Forte int 429fcf3ce44SJohn Forte do_all(struct protob *protobp, 430fcf3ce44SJohn Forte int (*svc)(int, struct netbuf, struct netconfig *)) 431fcf3ce44SJohn Forte { 432fcf3ce44SJohn Forte struct netconfig *nconf; 433fcf3ce44SJohn Forte NCONF_HANDLE *nc; 434fcf3ce44SJohn Forte 435fcf3ce44SJohn Forte if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) { 436fcf3ce44SJohn Forte syslog(LOG_ERR, "setnetconfig failed: %m"); 437fcf3ce44SJohn Forte return (-1); 438fcf3ce44SJohn Forte } 439fcf3ce44SJohn Forte while (nconf = getnetconfig(nc)) { 440fcf3ce44SJohn Forte if ((nconf->nc_flag & NC_VISIBLE) && 441fcf3ce44SJohn Forte strcmp(nconf->nc_protofmly, "loopback") != 0 && 442fcf3ce44SJohn Forte OK_TPI_TYPE(nconf)) 443*55393845SMarcel Telka do_one(nconf->nc_device, nconf->nc_proto, protobp, svc); 444fcf3ce44SJohn Forte } 445fcf3ce44SJohn Forte (void) endnetconfig(nc); 446fcf3ce44SJohn Forte return (0); 447fcf3ce44SJohn Forte } 448fcf3ce44SJohn Forte 449fcf3ce44SJohn Forte /* 450fcf3ce44SJohn Forte * Read the /etc/default/sndr configuration file to determine if the 451fcf3ce44SJohn Forte * client has been configured for number of threads, backlog or transport 452fcf3ce44SJohn Forte * provider. 453fcf3ce44SJohn Forte */ 454fcf3ce44SJohn Forte 455fcf3ce44SJohn Forte static void 456fcf3ce44SJohn Forte read_default(void) 457fcf3ce44SJohn Forte { 458fcf3ce44SJohn Forte char *defval, *tmp_str; 459fcf3ce44SJohn Forte int errno; 460fcf3ce44SJohn Forte int tmp; 461fcf3ce44SJohn Forte 462fcf3ce44SJohn Forte /* Fail silently if error in opening the default rdc config file */ 463fcf3ce44SJohn Forte if ((defopen(RDCADMIN)) == 0) { 464fcf3ce44SJohn Forte if ((defval = defread("SNDR_THREADS=")) != NULL) { 465fcf3ce44SJohn Forte errno = 0; 466fcf3ce44SJohn Forte tmp = strtol(defval, (char **)NULL, 10); 467fcf3ce44SJohn Forte if (errno == 0) { 468fcf3ce44SJohn Forte max_conns_allowed = tmp; 469fcf3ce44SJohn Forte } 470fcf3ce44SJohn Forte } 471fcf3ce44SJohn Forte if ((defval = defread("SNDR_LISTEN_BACKLOG=")) != NULL) { 472fcf3ce44SJohn Forte errno = 0; 473fcf3ce44SJohn Forte tmp = strtol(defval, (char **)NULL, 10); 474fcf3ce44SJohn Forte if (errno == 0) { 475fcf3ce44SJohn Forte listen_backlog = tmp; 476fcf3ce44SJohn Forte } 477fcf3ce44SJohn Forte } 478fcf3ce44SJohn Forte if ((defval = defread("SNDR_TRANSPORT=")) != NULL) { 479fcf3ce44SJohn Forte errno = 0; 480fcf3ce44SJohn Forte tmp_str = strdup(defval); 481fcf3ce44SJohn Forte if (errno == 0) { 482fcf3ce44SJohn Forte trans_provider = tmp_str; 483fcf3ce44SJohn Forte } 484fcf3ce44SJohn Forte } 485fcf3ce44SJohn Forte /* close defaults file */ 486570de38fSSurya Prakki (void) defopen(NULL); 487fcf3ce44SJohn Forte } 488fcf3ce44SJohn Forte } 489fcf3ce44SJohn Forte #ifdef lint 490fcf3ce44SJohn Forte int 491fcf3ce44SJohn Forte sndrd_lintmain(int ac, char **av) 492fcf3ce44SJohn Forte #else 493fcf3ce44SJohn Forte int 494fcf3ce44SJohn Forte main(int ac, char **av) 495fcf3ce44SJohn Forte #endif 496fcf3ce44SJohn Forte { 497fcf3ce44SJohn Forte const char *dir = "/"; 498fcf3ce44SJohn Forte int allflag = 0; 499fcf3ce44SJohn Forte int pid; 500fcf3ce44SJohn Forte int i, rc; 501fcf3ce44SJohn Forte struct protob *protobp0, *protobp; 502fcf3ce44SJohn Forte char **providerp; 503fcf3ce44SJohn Forte char *required; 504fcf3ce44SJohn Forte #if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8) 505fcf3ce44SJohn Forte int maxservers; 506fcf3ce44SJohn Forte #endif 507fcf3ce44SJohn Forte 508fcf3ce44SJohn Forte (void) setlocale(LC_ALL, ""); 509fcf3ce44SJohn Forte #ifdef __NCALL__ 510fcf3ce44SJohn Forte (void) textdomain("ncall"); 511fcf3ce44SJohn Forte #else 512fcf3ce44SJohn Forte (void) textdomain("rdc"); 513fcf3ce44SJohn Forte #endif 514fcf3ce44SJohn Forte 515fcf3ce44SJohn Forte progname = basename(av[0]); 516fcf3ce44SJohn Forte 517fcf3ce44SJohn Forte #ifdef __NCALL__ 518fcf3ce44SJohn Forte rc = ncall_check_release(&required); 519fcf3ce44SJohn Forte #else 520fcf3ce44SJohn Forte rc = rdc_check_release(&required); 521fcf3ce44SJohn Forte #endif 522fcf3ce44SJohn Forte if (rc < 0) { 523fcf3ce44SJohn Forte (void) fprintf(stderr, 524fcf3ce44SJohn Forte gettext("%s: unable to determine the current " 525fcf3ce44SJohn Forte "Solaris release: %s\n"), progname, strerror(errno)); 526fcf3ce44SJohn Forte exit(1); 527fcf3ce44SJohn Forte } else if (rc == FALSE) { 528fcf3ce44SJohn Forte (void) fprintf(stderr, 529fcf3ce44SJohn Forte gettext("%s: incorrect Solaris release (requires %s)\n"), 530fcf3ce44SJohn Forte progname, required); 531fcf3ce44SJohn Forte exit(1); 532fcf3ce44SJohn Forte } 533fcf3ce44SJohn Forte 534fcf3ce44SJohn Forte openlog(progname, LOG_PID|LOG_CONS, LOG_DAEMON); 535fcf3ce44SJohn Forte read_default(); 536fcf3ce44SJohn Forte 537fcf3ce44SJohn Forte /* 538fcf3ce44SJohn Forte * Usage: <progname> [-c <number of threads>] [-t protocol] \ 539fcf3ce44SJohn Forte * [-d] [-l <listen backlog>] 540fcf3ce44SJohn Forte */ 541fcf3ce44SJohn Forte while ((i = getopt(ac, av, "ac:t:dl:")) != EOF) { 542fcf3ce44SJohn Forte switch (i) { 543fcf3ce44SJohn Forte case 'a': 544fcf3ce44SJohn Forte allflag = 1; 545fcf3ce44SJohn Forte break; 546fcf3ce44SJohn Forte case 'c': 547fcf3ce44SJohn Forte max_conns_allowed = atoi(optarg); 548fcf3ce44SJohn Forte if (max_conns_allowed <= 0) 549fcf3ce44SJohn Forte max_conns_allowed = 16; 550fcf3ce44SJohn Forte break; 551fcf3ce44SJohn Forte 552fcf3ce44SJohn Forte case 'd': 553fcf3ce44SJohn Forte debugflg++; 554fcf3ce44SJohn Forte break; 555fcf3ce44SJohn Forte 556fcf3ce44SJohn Forte case 't': 557fcf3ce44SJohn Forte trans_provider = optarg; 558fcf3ce44SJohn Forte break; 559fcf3ce44SJohn Forte 560fcf3ce44SJohn Forte case 'l': 561fcf3ce44SJohn Forte listen_backlog = atoi(optarg); 562fcf3ce44SJohn Forte if (listen_backlog < 0) 563fcf3ce44SJohn Forte listen_backlog = 32; 564fcf3ce44SJohn Forte break; 565fcf3ce44SJohn Forte 566fcf3ce44SJohn Forte default: 567fcf3ce44SJohn Forte syslog(LOG_ERR, 568fcf3ce44SJohn Forte "Usage: %s [-c <number of threads>] " 569fcf3ce44SJohn Forte "[-d] [-t protocol] " 570fcf3ce44SJohn Forte "[-l <listen backlog>]\n", progname); 571fcf3ce44SJohn Forte exit(1); 572fcf3ce44SJohn Forte break; 573fcf3ce44SJohn Forte } 574fcf3ce44SJohn Forte } 575fcf3ce44SJohn Forte 576fcf3ce44SJohn Forte if (chroot(dir) < 0) { 577fcf3ce44SJohn Forte syslog(LOG_ERR, "chroot failed: %m"); 578fcf3ce44SJohn Forte exit(1); 579fcf3ce44SJohn Forte } 580fcf3ce44SJohn Forte 581fcf3ce44SJohn Forte if (chdir(dir) < 0) { 582fcf3ce44SJohn Forte syslog(LOG_ERR, "chdir failed: %m"); 583fcf3ce44SJohn Forte exit(1); 584fcf3ce44SJohn Forte } 585fcf3ce44SJohn Forte 586fcf3ce44SJohn Forte if (!debugflg) { 587fcf3ce44SJohn Forte pid = fork(); 588fcf3ce44SJohn Forte if (pid < 0) { 589fcf3ce44SJohn Forte syslog(LOG_ERR, "Fork failed\n"); 590fcf3ce44SJohn Forte exit(1); 591fcf3ce44SJohn Forte } 592fcf3ce44SJohn Forte if (pid != 0) 593fcf3ce44SJohn Forte exit(0); 594fcf3ce44SJohn Forte 595fcf3ce44SJohn Forte /* 596fcf3ce44SJohn Forte * Close existing file descriptors, open "/dev/null" as 597fcf3ce44SJohn Forte * standard input, output, and error, and detach from 598fcf3ce44SJohn Forte * controlling terminal. 599fcf3ce44SJohn Forte */ 600fcf3ce44SJohn Forte #if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8) 601fcf3ce44SJohn Forte /* use closefrom(3C) from PSARC/2000/193 when possible */ 602fcf3ce44SJohn Forte closefrom(0); 603fcf3ce44SJohn Forte #else 604fcf3ce44SJohn Forte for (i = 0; i < _NFILE; i++) 605fcf3ce44SJohn Forte (void) close(i); 606fcf3ce44SJohn Forte #endif 607fcf3ce44SJohn Forte (void) open("/dev/null", O_RDONLY); 608fcf3ce44SJohn Forte (void) open("/dev/null", O_WRONLY); 609fcf3ce44SJohn Forte (void) dup(1); 610fcf3ce44SJohn Forte (void) setsid(); 611fcf3ce44SJohn Forte 612fcf3ce44SJohn Forte /* 613fcf3ce44SJohn Forte * ignore all signals apart from SIGTERM. 614fcf3ce44SJohn Forte */ 615fcf3ce44SJohn Forte for (i = 1; i < _sys_nsig; i++) 616fcf3ce44SJohn Forte (void) sigset(i, SIG_IGN); 617fcf3ce44SJohn Forte 618fcf3ce44SJohn Forte (void) sigset(SIGTERM, SIG_DFL); 619fcf3ce44SJohn Forte } 620fcf3ce44SJohn Forte 621fcf3ce44SJohn Forte #if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8) 622fcf3ce44SJohn Forte /* 623fcf3ce44SJohn Forte * Set up kernel RPC thread pool for the SNDR/ncall-ip server. 624fcf3ce44SJohn Forte */ 625fcf3ce44SJohn Forte maxservers = (max_conns_allowed < 0 ? 16 : max_conns_allowed); 626fcf3ce44SJohn Forte if (sndrsvcpool(maxservers)) { 627fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 628fcf3ce44SJohn Forte "Can't set up kernel %s service: %m. Exiting", progname); 629fcf3ce44SJohn Forte exit(1); 630fcf3ce44SJohn Forte } 631fcf3ce44SJohn Forte 632fcf3ce44SJohn Forte /* 633fcf3ce44SJohn Forte * Set up blocked thread to do LWP creation on behalf of the kernel. 634fcf3ce44SJohn Forte */ 635fcf3ce44SJohn Forte if (svcwait(RDC_SVCPOOL_ID)) { 636fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 637fcf3ce44SJohn Forte "Can't set up %s pool creator: %m, Exiting", progname); 638fcf3ce44SJohn Forte exit(1); 639fcf3ce44SJohn Forte } 640fcf3ce44SJohn Forte #endif 641fcf3ce44SJohn Forte 642fcf3ce44SJohn Forte /* 643fcf3ce44SJohn Forte * Build a protocol block list for registration. 644fcf3ce44SJohn Forte */ 645fcf3ce44SJohn Forte protobp0 = protobp = (struct protob *)malloc(sizeof (struct protob)); 646fcf3ce44SJohn Forte protobp->serv = RDC_SVC_NAME; 647fcf3ce44SJohn Forte protobp->versmin = RDC_VERS_MIN; 648fcf3ce44SJohn Forte protobp->versmax = RDC_VERS_MAX; 649fcf3ce44SJohn Forte protobp->program = RDC_PROGRAM; 650fcf3ce44SJohn Forte protobp->next = (struct protob *)NULL; 651fcf3ce44SJohn Forte 652fcf3ce44SJohn Forte if (allflag) { 653fcf3ce44SJohn Forte if (do_all(protobp0, rdcsvc) == -1) 654fcf3ce44SJohn Forte exit(1); 655fcf3ce44SJohn Forte } else if (trans_provider) 656fcf3ce44SJohn Forte do_one(trans_provider, NULL, protobp0, rdcsvc); 657fcf3ce44SJohn Forte else { 658fcf3ce44SJohn Forte for (providerp = defaultproviders; 659fcf3ce44SJohn Forte *providerp != NULL; providerp++) { 660fcf3ce44SJohn Forte trans_provider = *providerp; 661fcf3ce44SJohn Forte do_one(trans_provider, NULL, protobp0, rdcsvc); 662fcf3ce44SJohn Forte } 663fcf3ce44SJohn Forte } 664fcf3ce44SJohn Forte 665fcf3ce44SJohn Forte done: 666fcf3ce44SJohn Forte free(protobp); 667fcf3ce44SJohn Forte 668fcf3ce44SJohn Forte end_listen_fds = num_fds; 669fcf3ce44SJohn Forte /* 670fcf3ce44SJohn Forte * Poll for non-data control events on the transport descriptors. 671fcf3ce44SJohn Forte */ 672fcf3ce44SJohn Forte poll_for_action(); 673fcf3ce44SJohn Forte 674fcf3ce44SJohn Forte syslog(LOG_ERR, "%s fatal server error\n", progname); 675fcf3ce44SJohn Forte 676fcf3ce44SJohn Forte return (-1); 677fcf3ce44SJohn Forte } 678fcf3ce44SJohn Forte 679fcf3ce44SJohn Forte static int 680fcf3ce44SJohn Forte reuseaddr(int fd) 681fcf3ce44SJohn Forte { 682fcf3ce44SJohn Forte struct t_optmgmt req, resp; 683fcf3ce44SJohn Forte struct opthdr *opt; 684fcf3ce44SJohn Forte char reqbuf[128]; 685fcf3ce44SJohn Forte int *ip; 686fcf3ce44SJohn Forte 687fcf3ce44SJohn Forte /* LINTED pointer alignment */ 688fcf3ce44SJohn Forte opt = (struct opthdr *)reqbuf; 689fcf3ce44SJohn Forte opt->level = SOL_SOCKET; 690fcf3ce44SJohn Forte opt->name = SO_REUSEADDR; 691fcf3ce44SJohn Forte opt->len = sizeof (int); 692fcf3ce44SJohn Forte 693fcf3ce44SJohn Forte /* LINTED pointer alignment */ 694fcf3ce44SJohn Forte ip = (int *)&reqbuf[sizeof (struct opthdr)]; 695fcf3ce44SJohn Forte *ip = 1; 696fcf3ce44SJohn Forte 697fcf3ce44SJohn Forte req.flags = T_NEGOTIATE; 698fcf3ce44SJohn Forte req.opt.len = sizeof (struct opthdr) + opt->len; 699fcf3ce44SJohn Forte req.opt.buf = (char *)opt; 700fcf3ce44SJohn Forte 701fcf3ce44SJohn Forte resp.flags = 0; 702fcf3ce44SJohn Forte resp.opt.buf = reqbuf; 703fcf3ce44SJohn Forte resp.opt.maxlen = sizeof (reqbuf); 704fcf3ce44SJohn Forte 705fcf3ce44SJohn Forte if (t_optmgmt(fd, &req, &resp) < 0 || resp.flags != T_SUCCESS) { 706fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 707fcf3ce44SJohn Forte syslog(LOG_ERR, "reuseaddr() t_optmgmt failed: %m\n"); 708fcf3ce44SJohn Forte } else { 709fcf3ce44SJohn Forte syslog(LOG_ERR, "reuseaddr() t_optmgmt failed: %s\n", 710fcf3ce44SJohn Forte t_errlist[t_errno]); 711fcf3ce44SJohn Forte } 712fcf3ce44SJohn Forte return (-1); 713fcf3ce44SJohn Forte } 714fcf3ce44SJohn Forte return (0); 715fcf3ce44SJohn Forte } 716fcf3ce44SJohn Forte 717fcf3ce44SJohn Forte /* 718fcf3ce44SJohn Forte * poll on the open transport descriptors for events and errors. 719fcf3ce44SJohn Forte */ 720fcf3ce44SJohn Forte void 721fcf3ce44SJohn Forte poll_for_action(void) 722fcf3ce44SJohn Forte { 723fcf3ce44SJohn Forte int nfds; 724fcf3ce44SJohn Forte int i; 725fcf3ce44SJohn Forte 726fcf3ce44SJohn Forte /* 727fcf3ce44SJohn Forte * Keep polling until all transports have been closed. When this 728fcf3ce44SJohn Forte * happens, we return. 729fcf3ce44SJohn Forte */ 730fcf3ce44SJohn Forte while ((int)num_fds > 0) { 731fcf3ce44SJohn Forte nfds = poll(poll_array, num_fds, INFTIM); 732fcf3ce44SJohn Forte switch (nfds) { 733fcf3ce44SJohn Forte case 0: 734fcf3ce44SJohn Forte continue; 735fcf3ce44SJohn Forte 736fcf3ce44SJohn Forte case -1: 737fcf3ce44SJohn Forte /* 738fcf3ce44SJohn Forte * Some errors from poll could be 739fcf3ce44SJohn Forte * due to temporary conditions, and we try to 740fcf3ce44SJohn Forte * be robust in the face of them. Other 741fcf3ce44SJohn Forte * errors (should never happen in theory) 742fcf3ce44SJohn Forte * are fatal (eg. EINVAL, EFAULT). 743fcf3ce44SJohn Forte */ 744fcf3ce44SJohn Forte switch (errno) { 745fcf3ce44SJohn Forte case EINTR: 746fcf3ce44SJohn Forte continue; 747fcf3ce44SJohn Forte 748fcf3ce44SJohn Forte case EAGAIN: 749fcf3ce44SJohn Forte case ENOMEM: 750fcf3ce44SJohn Forte (void) sleep(10); 751fcf3ce44SJohn Forte continue; 752fcf3ce44SJohn Forte 753fcf3ce44SJohn Forte default: 754fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 755fcf3ce44SJohn Forte "poll failed: %m. Exiting"); 756fcf3ce44SJohn Forte exit(1); 757fcf3ce44SJohn Forte } 758fcf3ce44SJohn Forte default: 759fcf3ce44SJohn Forte break; 760fcf3ce44SJohn Forte } 761fcf3ce44SJohn Forte 762fcf3ce44SJohn Forte /* 763fcf3ce44SJohn Forte * Go through the poll list looking for events. 764fcf3ce44SJohn Forte */ 765fcf3ce44SJohn Forte for (i = 0; i < num_fds && nfds > 0; i++) { 766fcf3ce44SJohn Forte if (poll_array[i].revents) { 767fcf3ce44SJohn Forte nfds--; 768fcf3ce44SJohn Forte /* 769fcf3ce44SJohn Forte * We have a message, so try to read it. 770fcf3ce44SJohn Forte * Record the error return in errno, 771fcf3ce44SJohn Forte * so that syslog(LOG_ERR, "...%m") 772fcf3ce44SJohn Forte * dumps the corresponding error string. 773fcf3ce44SJohn Forte */ 774fcf3ce44SJohn Forte if (conn_polled[i].nc.nc_semantics == 775fcf3ce44SJohn Forte NC_TPI_CLTS) { 776fcf3ce44SJohn Forte errno = do_poll_clts_action( 777fcf3ce44SJohn Forte poll_array[i].fd, i); 778fcf3ce44SJohn Forte } else { 779fcf3ce44SJohn Forte errno = do_poll_cots_action( 780fcf3ce44SJohn Forte poll_array[i].fd, i); 781fcf3ce44SJohn Forte } 782fcf3ce44SJohn Forte 783fcf3ce44SJohn Forte if (errno == 0) 784fcf3ce44SJohn Forte continue; 785fcf3ce44SJohn Forte /* 786fcf3ce44SJohn Forte * Most returned error codes mean that there is 787fcf3ce44SJohn Forte * fatal condition which we can only deal with 788fcf3ce44SJohn Forte * by closing the transport. 789fcf3ce44SJohn Forte */ 790fcf3ce44SJohn Forte if (errno != EAGAIN && errno != ENOMEM) { 791fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 792fcf3ce44SJohn Forte "Error (%m) reading descriptor %d" 793fcf3ce44SJohn Forte "/transport %s. Closing it.", 794fcf3ce44SJohn Forte poll_array[i].fd, 795fcf3ce44SJohn Forte conn_polled[i].nc.nc_proto); 796fcf3ce44SJohn Forte (void) t_close(poll_array[i].fd); 797fcf3ce44SJohn Forte remove_from_poll_list(poll_array[i].fd); 798fcf3ce44SJohn Forte } else if (errno == ENOMEM) 799fcf3ce44SJohn Forte (void) sleep(5); 800fcf3ce44SJohn Forte } 801fcf3ce44SJohn Forte } 802fcf3ce44SJohn Forte } 803fcf3ce44SJohn Forte 804fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 805fcf3ce44SJohn Forte "All transports have been closed with errors. Exiting."); 806fcf3ce44SJohn Forte } 807fcf3ce44SJohn Forte 808fcf3ce44SJohn Forte /* 809fcf3ce44SJohn Forte * Allocate poll/transport array entries for this descriptor. 810fcf3ce44SJohn Forte */ 811fcf3ce44SJohn Forte static void 812fcf3ce44SJohn Forte add_to_poll_list(int fd, struct netconfig *nconf) 813fcf3ce44SJohn Forte { 814fcf3ce44SJohn Forte static int poll_array_size = 0; 815fcf3ce44SJohn Forte 816fcf3ce44SJohn Forte /* 817fcf3ce44SJohn Forte * If the arrays are full, allocate new ones. 818fcf3ce44SJohn Forte */ 819fcf3ce44SJohn Forte if (num_fds == poll_array_size) { 820fcf3ce44SJohn Forte struct pollfd *tpa; 821fcf3ce44SJohn Forte struct conn_entry *tnp; 822fcf3ce44SJohn Forte 823fcf3ce44SJohn Forte if (poll_array_size != 0) { 824fcf3ce44SJohn Forte tpa = poll_array; 825fcf3ce44SJohn Forte tnp = conn_polled; 826fcf3ce44SJohn Forte } else 827fcf3ce44SJohn Forte tpa = (struct pollfd *)0; 828fcf3ce44SJohn Forte 829fcf3ce44SJohn Forte poll_array_size += POLL_ARRAY_INC_SIZE; 830fcf3ce44SJohn Forte 831fcf3ce44SJohn Forte /* 832fcf3ce44SJohn Forte * Allocate new arrays. 833fcf3ce44SJohn Forte */ 834fcf3ce44SJohn Forte poll_array = (struct pollfd *) 835fcf3ce44SJohn Forte malloc(poll_array_size * sizeof (struct pollfd) + 256); 836fcf3ce44SJohn Forte conn_polled = (struct conn_entry *) 837fcf3ce44SJohn Forte malloc(poll_array_size * sizeof (struct conn_entry) + 256); 838fcf3ce44SJohn Forte if (poll_array == (struct pollfd *)NULL || 839fcf3ce44SJohn Forte conn_polled == (struct conn_entry *)NULL) { 840fcf3ce44SJohn Forte syslog(LOG_ERR, "malloc failed for poll array"); 841fcf3ce44SJohn Forte exit(1); 842fcf3ce44SJohn Forte } 843fcf3ce44SJohn Forte 844fcf3ce44SJohn Forte /* 845fcf3ce44SJohn Forte * Copy the data of the old ones into new arrays, and 846fcf3ce44SJohn Forte * free the old ones. 847fcf3ce44SJohn Forte * num_fds is guaranteed to be less than 848fcf3ce44SJohn Forte * poll_array_size, so this memcpy is safe. 849fcf3ce44SJohn Forte */ 850fcf3ce44SJohn Forte if (tpa) { 851fcf3ce44SJohn Forte (void) memcpy((void *)poll_array, (void *)tpa, 852fcf3ce44SJohn Forte num_fds * sizeof (struct pollfd)); 853fcf3ce44SJohn Forte (void) memcpy((void *)conn_polled, (void *)tnp, 854fcf3ce44SJohn Forte num_fds * sizeof (struct conn_entry)); 855fcf3ce44SJohn Forte free((void *)tpa); 856fcf3ce44SJohn Forte free((void *)tnp); 857fcf3ce44SJohn Forte } 858fcf3ce44SJohn Forte } 859fcf3ce44SJohn Forte 860fcf3ce44SJohn Forte /* 861fcf3ce44SJohn Forte * Set the descriptor and event list. All possible events are 862fcf3ce44SJohn Forte * polled for. 863fcf3ce44SJohn Forte */ 864fcf3ce44SJohn Forte poll_array[num_fds].fd = fd; 865fcf3ce44SJohn Forte poll_array[num_fds].events = POLLIN|POLLRDNORM|POLLRDBAND|POLLPRI; 866fcf3ce44SJohn Forte 867fcf3ce44SJohn Forte /* 868fcf3ce44SJohn Forte * Copy the transport data over too. 869fcf3ce44SJohn Forte */ 870fcf3ce44SJohn Forte conn_polled[num_fds].nc = *nconf; /* structure copy */ 871fcf3ce44SJohn Forte conn_polled[num_fds].closing = 0; 872fcf3ce44SJohn Forte 873fcf3ce44SJohn Forte /* 874fcf3ce44SJohn Forte * Set the descriptor to non-blocking. Avoids a race 875fcf3ce44SJohn Forte * between data arriving on the stream and then having it 876fcf3ce44SJohn Forte * flushed before we can read it. 877fcf3ce44SJohn Forte */ 878fcf3ce44SJohn Forte if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 879fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 880fcf3ce44SJohn Forte "fcntl(file desc. %d/transport %s, F_SETFL, " 881fcf3ce44SJohn Forte "O_NONBLOCK): %m. Exiting", 882fcf3ce44SJohn Forte num_fds, nconf->nc_proto); 883fcf3ce44SJohn Forte exit(1); 884fcf3ce44SJohn Forte } 885fcf3ce44SJohn Forte 886fcf3ce44SJohn Forte /* 887fcf3ce44SJohn Forte * Count this descriptor. 888fcf3ce44SJohn Forte */ 889fcf3ce44SJohn Forte ++num_fds; 890fcf3ce44SJohn Forte } 891fcf3ce44SJohn Forte 892fcf3ce44SJohn Forte static void 893fcf3ce44SJohn Forte remove_from_poll_list(int fd) 894fcf3ce44SJohn Forte { 895fcf3ce44SJohn Forte int i; 896fcf3ce44SJohn Forte int num_to_copy; 897fcf3ce44SJohn Forte 898fcf3ce44SJohn Forte for (i = 0; i < num_fds; i++) { 899fcf3ce44SJohn Forte if (poll_array[i].fd == fd) { 900fcf3ce44SJohn Forte --num_fds; 901fcf3ce44SJohn Forte num_to_copy = num_fds - i; 902fcf3ce44SJohn Forte (void) memcpy((void *)&poll_array[i], 903fcf3ce44SJohn Forte (void *)&poll_array[i+1], 904fcf3ce44SJohn Forte num_to_copy * sizeof (struct pollfd)); 905fcf3ce44SJohn Forte (void) memset((void *)&poll_array[num_fds], 0, 906fcf3ce44SJohn Forte sizeof (struct pollfd)); 907fcf3ce44SJohn Forte (void) memcpy((void *)&conn_polled[i], 908fcf3ce44SJohn Forte (void *)&conn_polled[i+1], 909fcf3ce44SJohn Forte num_to_copy * sizeof (struct conn_entry)); 910fcf3ce44SJohn Forte (void) memset((void *)&conn_polled[num_fds], 0, 911fcf3ce44SJohn Forte sizeof (struct conn_entry)); 912fcf3ce44SJohn Forte return; 913fcf3ce44SJohn Forte } 914fcf3ce44SJohn Forte } 915fcf3ce44SJohn Forte syslog(LOG_ERR, "attempt to remove nonexistent fd from poll list"); 916fcf3ce44SJohn Forte 917fcf3ce44SJohn Forte } 918fcf3ce44SJohn Forte 919fcf3ce44SJohn Forte static void 920fcf3ce44SJohn Forte conn_close_oldest(void) 921fcf3ce44SJohn Forte { 922fcf3ce44SJohn Forte int fd; 923fcf3ce44SJohn Forte int i1; 924fcf3ce44SJohn Forte 925fcf3ce44SJohn Forte /* 926fcf3ce44SJohn Forte * Find the oldest connection that is not already in the 927fcf3ce44SJohn Forte * process of shutting down. 928fcf3ce44SJohn Forte */ 929fcf3ce44SJohn Forte for (i1 = end_listen_fds; /* no conditional expression */; i1++) { 930fcf3ce44SJohn Forte if (i1 >= num_fds) 931fcf3ce44SJohn Forte return; 932fcf3ce44SJohn Forte if (conn_polled[i1].closing == 0) 933fcf3ce44SJohn Forte break; 934fcf3ce44SJohn Forte } 935fcf3ce44SJohn Forte #ifdef DEBUG 936fcf3ce44SJohn Forte (void) printf("too many connections (%d), releasing oldest (%d)\n", 937fcf3ce44SJohn Forte num_conns, poll_array[i1].fd); 938fcf3ce44SJohn Forte #else 939fcf3ce44SJohn Forte syslog(LOG_WARNING, "too many connections (%d), releasing oldest (%d)", 940fcf3ce44SJohn Forte num_conns, poll_array[i1].fd); 941fcf3ce44SJohn Forte #endif 942fcf3ce44SJohn Forte fd = poll_array[i1].fd; 943fcf3ce44SJohn Forte if (conn_polled[i1].nc.nc_semantics == NC_TPI_COTS) { 944fcf3ce44SJohn Forte /* 945fcf3ce44SJohn Forte * For politeness, send a T_DISCON_REQ to the transport 946fcf3ce44SJohn Forte * provider. We close the stream anyway. 947fcf3ce44SJohn Forte */ 948fcf3ce44SJohn Forte (void) t_snddis(fd, (struct t_call *)0); 949fcf3ce44SJohn Forte num_conns--; 950fcf3ce44SJohn Forte remove_from_poll_list(fd); 951fcf3ce44SJohn Forte (void) t_close(fd); 952fcf3ce44SJohn Forte } else { 953fcf3ce44SJohn Forte /* 954fcf3ce44SJohn Forte * For orderly release, we do not close the stream 955fcf3ce44SJohn Forte * until the T_ORDREL_IND arrives to complete 956fcf3ce44SJohn Forte * the handshake. 957fcf3ce44SJohn Forte */ 958fcf3ce44SJohn Forte if (t_sndrel(fd) == 0) 959fcf3ce44SJohn Forte conn_polled[i1].closing = 1; 960fcf3ce44SJohn Forte } 961fcf3ce44SJohn Forte } 962fcf3ce44SJohn Forte 963fcf3ce44SJohn Forte static boolean_t 964fcf3ce44SJohn Forte conn_get(int fd, struct netconfig *nconf, struct conn_ind **connp) 965fcf3ce44SJohn Forte { 966fcf3ce44SJohn Forte struct conn_ind *conn; 967fcf3ce44SJohn Forte struct conn_ind *next_conn; 968fcf3ce44SJohn Forte 969fcf3ce44SJohn Forte conn = (struct conn_ind *)malloc(sizeof (*conn)); 970fcf3ce44SJohn Forte if (conn == NULL) { 971fcf3ce44SJohn Forte syslog(LOG_ERR, "malloc for listen indication failed"); 972fcf3ce44SJohn Forte return (FALSE); 973fcf3ce44SJohn Forte } 974fcf3ce44SJohn Forte 975fcf3ce44SJohn Forte /* LINTED pointer alignment */ 976fcf3ce44SJohn Forte conn->conn_call = (struct t_call *)t_alloc(fd, T_CALL, T_ALL); 977fcf3ce44SJohn Forte if (conn->conn_call == NULL) { 978fcf3ce44SJohn Forte free((char *)conn); 979fcf3ce44SJohn Forte rdcd_log_tli_error("t_alloc", fd, nconf); 980fcf3ce44SJohn Forte return (FALSE); 981fcf3ce44SJohn Forte } 982fcf3ce44SJohn Forte 983fcf3ce44SJohn Forte if (t_listen(fd, conn->conn_call) == -1) { 984fcf3ce44SJohn Forte rdcd_log_tli_error("t_listen", fd, nconf); 985fcf3ce44SJohn Forte (void) t_free((char *)conn->conn_call, T_CALL); 986fcf3ce44SJohn Forte free((char *)conn); 987fcf3ce44SJohn Forte return (FALSE); 988fcf3ce44SJohn Forte } 989fcf3ce44SJohn Forte 990fcf3ce44SJohn Forte if (conn->conn_call->udata.len > 0) { 991fcf3ce44SJohn Forte syslog(LOG_WARNING, 992fcf3ce44SJohn Forte "rejecting inbound connection(%s) with %d bytes " 993fcf3ce44SJohn Forte "of connect data", 994fcf3ce44SJohn Forte nconf->nc_proto, conn->conn_call->udata.len); 995fcf3ce44SJohn Forte 996fcf3ce44SJohn Forte conn->conn_call->udata.len = 0; 997fcf3ce44SJohn Forte (void) t_snddis(fd, conn->conn_call); 998fcf3ce44SJohn Forte (void) t_free((char *)conn->conn_call, T_CALL); 999fcf3ce44SJohn Forte free((char *)conn); 1000fcf3ce44SJohn Forte return (FALSE); 1001fcf3ce44SJohn Forte } 1002fcf3ce44SJohn Forte 1003fcf3ce44SJohn Forte if ((next_conn = *connp) != NULL) { 1004fcf3ce44SJohn Forte next_conn->conn_prev->conn_next = conn; 1005fcf3ce44SJohn Forte conn->conn_next = next_conn; 1006fcf3ce44SJohn Forte conn->conn_prev = next_conn->conn_prev; 1007fcf3ce44SJohn Forte next_conn->conn_prev = conn; 1008fcf3ce44SJohn Forte } else { 1009fcf3ce44SJohn Forte conn->conn_next = conn; 1010fcf3ce44SJohn Forte conn->conn_prev = conn; 1011fcf3ce44SJohn Forte *connp = conn; 1012fcf3ce44SJohn Forte } 1013fcf3ce44SJohn Forte return (TRUE); 1014fcf3ce44SJohn Forte } 1015fcf3ce44SJohn Forte 1016fcf3ce44SJohn Forte static int 1017fcf3ce44SJohn Forte discon_get(int fd, struct netconfig *nconf, struct conn_ind **connp) 1018fcf3ce44SJohn Forte { 1019fcf3ce44SJohn Forte struct conn_ind *conn; 1020fcf3ce44SJohn Forte struct t_discon discon; 1021fcf3ce44SJohn Forte 1022fcf3ce44SJohn Forte discon.udata.buf = (char *)0; 1023fcf3ce44SJohn Forte discon.udata.maxlen = 0; 1024fcf3ce44SJohn Forte if (t_rcvdis(fd, &discon) == -1) { 1025fcf3ce44SJohn Forte rdcd_log_tli_error("t_rcvdis", fd, nconf); 1026fcf3ce44SJohn Forte return (-1); 1027fcf3ce44SJohn Forte } 1028fcf3ce44SJohn Forte 1029fcf3ce44SJohn Forte conn = *connp; 1030fcf3ce44SJohn Forte if (conn == NULL) 1031fcf3ce44SJohn Forte return (0); 1032fcf3ce44SJohn Forte 1033fcf3ce44SJohn Forte do { 1034fcf3ce44SJohn Forte if (conn->conn_call->sequence == discon.sequence) { 1035fcf3ce44SJohn Forte if (conn->conn_next == conn) 1036fcf3ce44SJohn Forte *connp = (struct conn_ind *)0; 1037fcf3ce44SJohn Forte else { 1038fcf3ce44SJohn Forte if (conn == *connp) { 1039fcf3ce44SJohn Forte *connp = conn->conn_next; 1040fcf3ce44SJohn Forte } 1041fcf3ce44SJohn Forte conn->conn_next->conn_prev = conn->conn_prev; 1042fcf3ce44SJohn Forte conn->conn_prev->conn_next = conn->conn_next; 1043fcf3ce44SJohn Forte } 1044fcf3ce44SJohn Forte free((char *)conn); 1045fcf3ce44SJohn Forte break; 1046fcf3ce44SJohn Forte } 1047fcf3ce44SJohn Forte conn = conn->conn_next; 1048fcf3ce44SJohn Forte } while (conn != *connp); 1049fcf3ce44SJohn Forte 1050fcf3ce44SJohn Forte return (0); 1051fcf3ce44SJohn Forte } 1052fcf3ce44SJohn Forte 1053fcf3ce44SJohn Forte static void 1054fcf3ce44SJohn Forte cots_listen_event(int fd, int conn_index) 1055fcf3ce44SJohn Forte { 1056fcf3ce44SJohn Forte struct t_call *call; 1057fcf3ce44SJohn Forte struct conn_ind *conn; 1058fcf3ce44SJohn Forte struct conn_ind *conn_head; 1059fcf3ce44SJohn Forte int event; 1060fcf3ce44SJohn Forte struct netconfig *nconf = &conn_polled[conn_index].nc; 1061fcf3ce44SJohn Forte int new_fd; 1062fcf3ce44SJohn Forte struct netbuf addrmask; 1063fcf3ce44SJohn Forte int ret = 0; 1064fcf3ce44SJohn Forte 1065*55393845SMarcel Telka conn_head = NULL; 1066fcf3ce44SJohn Forte (void) conn_get(fd, nconf, &conn_head); 1067fcf3ce44SJohn Forte 1068fcf3ce44SJohn Forte while ((conn = conn_head) != NULL) { 1069fcf3ce44SJohn Forte conn_head = conn->conn_next; 1070fcf3ce44SJohn Forte if (conn_head == conn) 1071*55393845SMarcel Telka conn_head = NULL; 1072fcf3ce44SJohn Forte else { 1073fcf3ce44SJohn Forte conn_head->conn_prev = conn->conn_prev; 1074fcf3ce44SJohn Forte conn->conn_prev->conn_next = conn_head; 1075fcf3ce44SJohn Forte } 1076fcf3ce44SJohn Forte call = conn->conn_call; 1077*55393845SMarcel Telka free(conn); 1078fcf3ce44SJohn Forte 1079fcf3ce44SJohn Forte /* 1080fcf3ce44SJohn Forte * If we have already accepted the maximum number of 1081fcf3ce44SJohn Forte * connections allowed on the command line, then drop 1082fcf3ce44SJohn Forte * the oldest connection (for any protocol) before 1083fcf3ce44SJohn Forte * accepting the new connection. Unless explicitly 1084fcf3ce44SJohn Forte * set on the command line, max_conns_allowed is -1. 1085fcf3ce44SJohn Forte */ 1086fcf3ce44SJohn Forte if (max_conns_allowed != -1 && num_conns >= max_conns_allowed) 1087fcf3ce44SJohn Forte conn_close_oldest(); 1088fcf3ce44SJohn Forte 1089fcf3ce44SJohn Forte /* 1090fcf3ce44SJohn Forte * Create a new transport endpoint for the same proto as 1091fcf3ce44SJohn Forte * the listener. 1092fcf3ce44SJohn Forte */ 1093fcf3ce44SJohn Forte new_fd = rdc_transport_open(nconf); 1094fcf3ce44SJohn Forte if (new_fd == -1) { 1095fcf3ce44SJohn Forte call->udata.len = 0; 1096fcf3ce44SJohn Forte (void) t_snddis(fd, call); 1097fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1098fcf3ce44SJohn Forte syslog(LOG_ERR, "Cannot establish transport over %s", 1099fcf3ce44SJohn Forte nconf->nc_device); 1100fcf3ce44SJohn Forte continue; 1101fcf3ce44SJohn Forte } 1102fcf3ce44SJohn Forte 1103fcf3ce44SJohn Forte /* Bind to a generic address/port for the accepting stream. */ 1104*55393845SMarcel Telka if (t_bind(new_fd, NULL, NULL) == -1) { 1105fcf3ce44SJohn Forte rdcd_log_tli_error("t_bind", new_fd, nconf); 1106fcf3ce44SJohn Forte call->udata.len = 0; 1107fcf3ce44SJohn Forte (void) t_snddis(fd, call); 1108fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1109fcf3ce44SJohn Forte (void) t_close(new_fd); 1110fcf3ce44SJohn Forte continue; 1111fcf3ce44SJohn Forte } 1112fcf3ce44SJohn Forte 1113fcf3ce44SJohn Forte while (t_accept(fd, new_fd, call) == -1) { 1114fcf3ce44SJohn Forte if (t_errno != TLOOK) { 1115fcf3ce44SJohn Forte rdcd_log_tli_error("t_accept", fd, nconf); 1116fcf3ce44SJohn Forte call->udata.len = 0; 1117fcf3ce44SJohn Forte (void) t_snddis(fd, call); 1118fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1119fcf3ce44SJohn Forte (void) t_close(new_fd); 1120fcf3ce44SJohn Forte goto do_next_conn; 1121fcf3ce44SJohn Forte } 1122fcf3ce44SJohn Forte while (event = t_look(fd)) { 1123fcf3ce44SJohn Forte switch (event) { 1124fcf3ce44SJohn Forte case T_LISTEN: 1125fcf3ce44SJohn Forte (void) conn_get(fd, nconf, &conn_head); 1126fcf3ce44SJohn Forte continue; 1127fcf3ce44SJohn Forte 1128fcf3ce44SJohn Forte case T_DISCONNECT: 1129fcf3ce44SJohn Forte (void) discon_get(fd, nconf, 1130fcf3ce44SJohn Forte &conn_head); 1131fcf3ce44SJohn Forte continue; 1132fcf3ce44SJohn Forte 1133fcf3ce44SJohn Forte default: 1134fcf3ce44SJohn Forte syslog(LOG_ERR, 1135fcf3ce44SJohn Forte "unexpected event 0x%x during " 1136fcf3ce44SJohn Forte "accept processing (%s)", 1137fcf3ce44SJohn Forte event, nconf->nc_proto); 1138fcf3ce44SJohn Forte call->udata.len = 0; 1139fcf3ce44SJohn Forte (void) t_snddis(fd, call); 1140fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1141fcf3ce44SJohn Forte (void) t_close(new_fd); 1142fcf3ce44SJohn Forte goto do_next_conn; 1143fcf3ce44SJohn Forte } 1144fcf3ce44SJohn Forte } 1145fcf3ce44SJohn Forte } 1146fcf3ce44SJohn Forte 1147fcf3ce44SJohn Forte if (set_addrmask(new_fd, nconf, &addrmask) < 0) { 1148fcf3ce44SJohn Forte (void) syslog(LOG_ERR, "Cannot set address mask for %s", 1149fcf3ce44SJohn Forte nconf->nc_netid); 1150*55393845SMarcel Telka (void) t_snddis(new_fd, NULL); 1151*55393845SMarcel Telka (void) t_free((char *)call, T_CALL); 1152*55393845SMarcel Telka (void) t_close(new_fd); 1153*55393845SMarcel Telka continue; 1154fcf3ce44SJohn Forte } 1155fcf3ce44SJohn Forte 1156*55393845SMarcel Telka /* Tell kRPC about the new stream. */ 1157fcf3ce44SJohn Forte ret = (*Mysvc)(new_fd, addrmask, nconf); 1158fcf3ce44SJohn Forte if (ret < 0) { 1159fcf3ce44SJohn Forte syslog(LOG_ERR, 1160fcf3ce44SJohn Forte "unable to register with kernel rpc: %m"); 1161fcf3ce44SJohn Forte free(addrmask.buf); 1162*55393845SMarcel Telka (void) t_snddis(new_fd, NULL); 1163fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1164fcf3ce44SJohn Forte (void) t_close(new_fd); 1165fcf3ce44SJohn Forte goto do_next_conn; 1166fcf3ce44SJohn Forte } 1167fcf3ce44SJohn Forte 1168fcf3ce44SJohn Forte free(addrmask.buf); 1169fcf3ce44SJohn Forte (void) t_free((char *)call, T_CALL); 1170fcf3ce44SJohn Forte 1171fcf3ce44SJohn Forte /* 1172fcf3ce44SJohn Forte * Poll on the new descriptor so that we get disconnect 1173fcf3ce44SJohn Forte * and orderly release indications. 1174fcf3ce44SJohn Forte */ 1175fcf3ce44SJohn Forte num_conns++; 1176fcf3ce44SJohn Forte add_to_poll_list(new_fd, nconf); 1177fcf3ce44SJohn Forte 1178fcf3ce44SJohn Forte /* Reset nconf in case it has been moved. */ 1179fcf3ce44SJohn Forte nconf = &conn_polled[conn_index].nc; 1180fcf3ce44SJohn Forte do_next_conn:; 1181fcf3ce44SJohn Forte } 1182fcf3ce44SJohn Forte } 1183fcf3ce44SJohn Forte 1184fcf3ce44SJohn Forte static int 1185fcf3ce44SJohn Forte do_poll_cots_action(int fd, int conn_index) 1186fcf3ce44SJohn Forte { 1187fcf3ce44SJohn Forte char buf[256]; 1188fcf3ce44SJohn Forte int event; 1189fcf3ce44SJohn Forte int i1; 1190fcf3ce44SJohn Forte int flags; 1191fcf3ce44SJohn Forte struct conn_entry *connent = &conn_polled[conn_index]; 1192fcf3ce44SJohn Forte struct netconfig *nconf = &(connent->nc); 1193fcf3ce44SJohn Forte const char *errorstr; 1194fcf3ce44SJohn Forte 1195fcf3ce44SJohn Forte while (event = t_look(fd)) { 1196fcf3ce44SJohn Forte switch (event) { 1197fcf3ce44SJohn Forte case T_LISTEN: 1198fcf3ce44SJohn Forte cots_listen_event(fd, conn_index); 1199fcf3ce44SJohn Forte break; 1200fcf3ce44SJohn Forte 1201fcf3ce44SJohn Forte case T_DATA: 1202fcf3ce44SJohn Forte /* 1203fcf3ce44SJohn Forte * Receive a private notification from CONS rpcmod. 1204fcf3ce44SJohn Forte */ 1205fcf3ce44SJohn Forte i1 = t_rcv(fd, buf, sizeof (buf), &flags); 1206fcf3ce44SJohn Forte if (i1 == -1) { 1207fcf3ce44SJohn Forte syslog(LOG_ERR, "t_rcv failed"); 1208fcf3ce44SJohn Forte break; 1209fcf3ce44SJohn Forte } 1210fcf3ce44SJohn Forte if (i1 < sizeof (int)) 1211fcf3ce44SJohn Forte break; 1212fcf3ce44SJohn Forte i1 = BE32_TO_U32(buf); 1213fcf3ce44SJohn Forte if (i1 == 1 || i1 == 2) { 1214fcf3ce44SJohn Forte /* 1215fcf3ce44SJohn Forte * This connection has been idle for too long, 1216fcf3ce44SJohn Forte * so release it as politely as we can. If we 1217fcf3ce44SJohn Forte * have already initiated an orderly release 1218fcf3ce44SJohn Forte * and we get notified that the stream is 1219fcf3ce44SJohn Forte * still idle, pull the plug. This prevents 1220fcf3ce44SJohn Forte * hung connections from continuing to consume 1221fcf3ce44SJohn Forte * resources. 1222fcf3ce44SJohn Forte */ 1223fcf3ce44SJohn Forte if (nconf->nc_semantics == NC_TPI_COTS || 1224fcf3ce44SJohn Forte connent->closing != 0) { 1225fcf3ce44SJohn Forte (void) t_snddis(fd, (struct t_call *)0); 1226fcf3ce44SJohn Forte goto fdclose; 1227fcf3ce44SJohn Forte } 1228fcf3ce44SJohn Forte /* 1229fcf3ce44SJohn Forte * For NC_TPI_COTS_ORD, the stream is closed 1230fcf3ce44SJohn Forte * and removed from the poll list when the 1231fcf3ce44SJohn Forte * T_ORDREL is received from the provider. We 1232fcf3ce44SJohn Forte * don't wait for it here because it may take 1233fcf3ce44SJohn Forte * a while for the transport to shut down. 1234fcf3ce44SJohn Forte */ 1235fcf3ce44SJohn Forte if (t_sndrel(fd) == -1) { 1236fcf3ce44SJohn Forte syslog(LOG_ERR, 1237fcf3ce44SJohn Forte "unable to send orderly release %m"); 1238fcf3ce44SJohn Forte } 1239fcf3ce44SJohn Forte connent->closing = 1; 1240fcf3ce44SJohn Forte } else 1241fcf3ce44SJohn Forte syslog(LOG_ERR, 1242fcf3ce44SJohn Forte "unexpected event from CONS rpcmod %d", i1); 1243fcf3ce44SJohn Forte break; 1244fcf3ce44SJohn Forte 1245fcf3ce44SJohn Forte case T_ORDREL: 1246fcf3ce44SJohn Forte /* Perform an orderly release. */ 1247fcf3ce44SJohn Forte if (t_rcvrel(fd) == 0) { 1248fcf3ce44SJohn Forte /* T_ORDREL on listen fd's should be ignored */ 1249fcf3ce44SJohn Forte if (!is_listen_fd_index(fd)) { 1250fcf3ce44SJohn Forte (void) t_sndrel(fd); 1251fcf3ce44SJohn Forte goto fdclose; 1252fcf3ce44SJohn Forte } 1253fcf3ce44SJohn Forte break; 1254fcf3ce44SJohn Forte 1255fcf3ce44SJohn Forte } else if (t_errno == TLOOK) { 1256fcf3ce44SJohn Forte break; 1257fcf3ce44SJohn Forte } else { 1258fcf3ce44SJohn Forte rdcd_log_tli_error("t_rcvrel", fd, nconf); 1259fcf3ce44SJohn Forte /* 1260fcf3ce44SJohn Forte * check to make sure we do not close 1261fcf3ce44SJohn Forte * listen fd 1262fcf3ce44SJohn Forte */ 1263fcf3ce44SJohn Forte if (!is_listen_fd_index(fd)) 1264fcf3ce44SJohn Forte break; 1265fcf3ce44SJohn Forte else 1266fcf3ce44SJohn Forte goto fdclose; 1267fcf3ce44SJohn Forte } 1268fcf3ce44SJohn Forte 1269fcf3ce44SJohn Forte case T_DISCONNECT: 1270fcf3ce44SJohn Forte if (t_rcvdis(fd, (struct t_discon *)NULL) == -1) 1271fcf3ce44SJohn Forte rdcd_log_tli_error("t_rcvdis", fd, nconf); 1272fcf3ce44SJohn Forte 1273fcf3ce44SJohn Forte /* 1274fcf3ce44SJohn Forte * T_DISCONNECT on listen fd's should be ignored. 1275fcf3ce44SJohn Forte */ 1276fcf3ce44SJohn Forte if (!is_listen_fd_index(fd)) 1277fcf3ce44SJohn Forte break; 1278fcf3ce44SJohn Forte else 1279fcf3ce44SJohn Forte goto fdclose; 1280fcf3ce44SJohn Forte 1281fcf3ce44SJohn Forte case T_ERROR: 1282fcf3ce44SJohn Forte default: 1283fcf3ce44SJohn Forte if (event == T_ERROR || t_errno == TSYSERR) { 1284fcf3ce44SJohn Forte if ((errorstr = strerror(errno)) == NULL) { 1285fcf3ce44SJohn Forte (void) snprintf(buf, sizeof (buf), 1286fcf3ce44SJohn Forte "Unknown error num %d", errno); 1287fcf3ce44SJohn Forte errorstr = (const char *)buf; 1288fcf3ce44SJohn Forte } 1289fcf3ce44SJohn Forte } else if (event == -1) 1290fcf3ce44SJohn Forte errorstr = t_strerror(t_errno); 1291fcf3ce44SJohn Forte else 1292fcf3ce44SJohn Forte errorstr = ""; 1293fcf3ce44SJohn Forte #ifdef DEBUG 1294fcf3ce44SJohn Forte syslog(LOG_ERR, 1295fcf3ce44SJohn Forte "unexpected TLI event (0x%x) on " 1296fcf3ce44SJohn Forte "connection-oriented transport(%s, %d):%s", 1297fcf3ce44SJohn Forte event, nconf->nc_proto, fd, errorstr); 1298fcf3ce44SJohn Forte #endif 1299fcf3ce44SJohn Forte 1300fcf3ce44SJohn Forte fdclose: 1301fcf3ce44SJohn Forte num_conns--; 1302fcf3ce44SJohn Forte remove_from_poll_list(fd); 1303fcf3ce44SJohn Forte (void) t_close(fd); 1304fcf3ce44SJohn Forte return (0); 1305fcf3ce44SJohn Forte } 1306fcf3ce44SJohn Forte } 1307fcf3ce44SJohn Forte 1308fcf3ce44SJohn Forte return (0); 1309fcf3ce44SJohn Forte } 1310fcf3ce44SJohn Forte 1311fcf3ce44SJohn Forte 1312fcf3ce44SJohn Forte /* 1313fcf3ce44SJohn Forte * Called to read and interpret the event on a connectionless descriptor. 1314fcf3ce44SJohn Forte * Returns 0 if successful, or a UNIX error code if failure. 1315fcf3ce44SJohn Forte */ 1316fcf3ce44SJohn Forte static int 1317fcf3ce44SJohn Forte do_poll_clts_action(int fd, int conn_index) 1318fcf3ce44SJohn Forte { 1319fcf3ce44SJohn Forte int error; 1320fcf3ce44SJohn Forte int ret; 1321fcf3ce44SJohn Forte int flags; 1322fcf3ce44SJohn Forte struct netconfig *nconf = &conn_polled[conn_index].nc; 1323fcf3ce44SJohn Forte static struct t_unitdata *unitdata = NULL; 1324fcf3ce44SJohn Forte static struct t_uderr *uderr = NULL; 1325fcf3ce44SJohn Forte static int oldfd = -1; 1326fcf3ce44SJohn Forte struct nd_hostservlist *host = NULL; 1327fcf3ce44SJohn Forte struct strbuf ctl[1], data[1]; 1328fcf3ce44SJohn Forte /* 1329fcf3ce44SJohn Forte * We just need to have some space to consume the 1330fcf3ce44SJohn Forte * message in the event we can't use the TLI interface to do the 1331fcf3ce44SJohn Forte * job. 1332fcf3ce44SJohn Forte * 1333fcf3ce44SJohn Forte * We flush the message using getmsg(). For the control part 1334fcf3ce44SJohn Forte * we allocate enough for any TPI header plus 32 bytes for address 1335fcf3ce44SJohn Forte * and options. For the data part, there is nothing magic about 1336fcf3ce44SJohn Forte * the size of the array, but 256 bytes is probably better than 1337fcf3ce44SJohn Forte * 1 byte, and we don't expect any data portion anyway. 1338fcf3ce44SJohn Forte * 1339fcf3ce44SJohn Forte * If the array sizes are too small, we handle this because getmsg() 1340fcf3ce44SJohn Forte * (called to consume the message) will return MOREDATA|MORECTL. 1341fcf3ce44SJohn Forte * Thus we just call getmsg() until it's read the message. 1342fcf3ce44SJohn Forte */ 1343fcf3ce44SJohn Forte char ctlbuf[sizeof (union T_primitives) + 32]; 1344fcf3ce44SJohn Forte char databuf[256]; 1345fcf3ce44SJohn Forte 1346fcf3ce44SJohn Forte /* 1347fcf3ce44SJohn Forte * If this is the same descriptor as the last time 1348fcf3ce44SJohn Forte * do_poll_clts_action was called, we can save some 1349fcf3ce44SJohn Forte * de-allocation and allocation. 1350fcf3ce44SJohn Forte */ 1351fcf3ce44SJohn Forte if (oldfd != fd) { 1352fcf3ce44SJohn Forte oldfd = fd; 1353fcf3ce44SJohn Forte 1354fcf3ce44SJohn Forte if (unitdata) { 1355fcf3ce44SJohn Forte (void) t_free((char *)unitdata, T_UNITDATA); 1356fcf3ce44SJohn Forte unitdata = NULL; 1357fcf3ce44SJohn Forte } 1358fcf3ce44SJohn Forte if (uderr) { 1359fcf3ce44SJohn Forte (void) t_free((char *)uderr, T_UDERROR); 1360fcf3ce44SJohn Forte uderr = NULL; 1361fcf3ce44SJohn Forte } 1362fcf3ce44SJohn Forte } 1363fcf3ce44SJohn Forte 1364fcf3ce44SJohn Forte /* 1365fcf3ce44SJohn Forte * Allocate a unitdata structure for receiving the event. 1366fcf3ce44SJohn Forte */ 1367fcf3ce44SJohn Forte if (unitdata == NULL) { 1368fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1369fcf3ce44SJohn Forte unitdata = (struct t_unitdata *)t_alloc(fd, T_UNITDATA, T_ALL); 1370fcf3ce44SJohn Forte if (unitdata == NULL) { 1371fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 1372fcf3ce44SJohn Forte /* 1373fcf3ce44SJohn Forte * Save the error code across 1374fcf3ce44SJohn Forte * syslog(), just in case 1375fcf3ce44SJohn Forte * syslog() gets its own error 1376fcf3ce44SJohn Forte * and therefore overwrites errno. 1377fcf3ce44SJohn Forte */ 1378fcf3ce44SJohn Forte error = errno; 1379fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1380*55393845SMarcel Telka "t_alloc(file descriptor %d/transport %s, " 1381*55393845SMarcel Telka "T_UNITDATA) failed: %m", 1382fcf3ce44SJohn Forte fd, nconf->nc_proto); 1383fcf3ce44SJohn Forte return (error); 1384fcf3ce44SJohn Forte } 1385*55393845SMarcel Telka (void) syslog(LOG_ERR, "t_alloc(file descriptor %d/" 1386*55393845SMarcel Telka "transport %s, T_UNITDATA) failed TLI error %d", 1387fcf3ce44SJohn Forte fd, nconf->nc_proto, t_errno); 1388fcf3ce44SJohn Forte goto flush_it; 1389fcf3ce44SJohn Forte } 1390fcf3ce44SJohn Forte } 1391fcf3ce44SJohn Forte 1392fcf3ce44SJohn Forte try_again: 1393fcf3ce44SJohn Forte flags = 0; 1394fcf3ce44SJohn Forte 1395fcf3ce44SJohn Forte /* 1396fcf3ce44SJohn Forte * The idea is we wait for T_UNITDATA_IND's. Of course, 1397fcf3ce44SJohn Forte * we don't get any, because rpcmod filters them out. 1398fcf3ce44SJohn Forte * However, we need to call t_rcvudata() to let TLI 1399fcf3ce44SJohn Forte * tell us we have a T_UDERROR_IND. 1400fcf3ce44SJohn Forte * 1401fcf3ce44SJohn Forte * algorithm is: 1402fcf3ce44SJohn Forte * t_rcvudata(), expecting TLOOK. 1403fcf3ce44SJohn Forte * t_look(), expecting T_UDERR. 1404fcf3ce44SJohn Forte * t_rcvuderr(), expecting success (0). 1405fcf3ce44SJohn Forte * expand destination address into ASCII, 1406fcf3ce44SJohn Forte * and dump it. 1407fcf3ce44SJohn Forte */ 1408fcf3ce44SJohn Forte 1409fcf3ce44SJohn Forte ret = t_rcvudata(fd, unitdata, &flags); 1410fcf3ce44SJohn Forte if (ret == 0 || t_errno == TBUFOVFLW) { 1411*55393845SMarcel Telka (void) syslog(LOG_WARNING, "t_rcvudata(file descriptor %d/" 1412*55393845SMarcel Telka "transport %s) got unexpected data, %d bytes", 1413fcf3ce44SJohn Forte fd, nconf->nc_proto, unitdata->udata.len); 1414fcf3ce44SJohn Forte 1415fcf3ce44SJohn Forte /* 1416fcf3ce44SJohn Forte * Even though we don't expect any data, in case we do, 1417fcf3ce44SJohn Forte * keep reading until there is no more. 1418fcf3ce44SJohn Forte */ 1419fcf3ce44SJohn Forte if (flags & T_MORE) 1420fcf3ce44SJohn Forte goto try_again; 1421fcf3ce44SJohn Forte 1422fcf3ce44SJohn Forte return (0); 1423fcf3ce44SJohn Forte } 1424fcf3ce44SJohn Forte 1425fcf3ce44SJohn Forte switch (t_errno) { 1426fcf3ce44SJohn Forte case TNODATA: 1427fcf3ce44SJohn Forte return (0); 1428fcf3ce44SJohn Forte case TSYSERR: 1429fcf3ce44SJohn Forte /* 1430fcf3ce44SJohn Forte * System errors are returned to caller. 1431fcf3ce44SJohn Forte * Save the error code across 1432fcf3ce44SJohn Forte * syslog(), just in case 1433fcf3ce44SJohn Forte * syslog() gets its own error 1434fcf3ce44SJohn Forte * and therefore overwrites errno. 1435fcf3ce44SJohn Forte */ 1436fcf3ce44SJohn Forte error = errno; 1437fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1438fcf3ce44SJohn Forte "t_rcvudata(file descriptor %d/transport %s) %m", 1439fcf3ce44SJohn Forte fd, nconf->nc_proto); 1440fcf3ce44SJohn Forte return (error); 1441fcf3ce44SJohn Forte case TLOOK: 1442fcf3ce44SJohn Forte break; 1443fcf3ce44SJohn Forte default: 1444fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1445fcf3ce44SJohn Forte "t_rcvudata(file descriptor %d/transport %s) TLI error %d", 1446fcf3ce44SJohn Forte fd, nconf->nc_proto, t_errno); 1447fcf3ce44SJohn Forte goto flush_it; 1448fcf3ce44SJohn Forte } 1449fcf3ce44SJohn Forte 1450fcf3ce44SJohn Forte ret = t_look(fd); 1451fcf3ce44SJohn Forte switch (ret) { 1452fcf3ce44SJohn Forte case 0: 1453fcf3ce44SJohn Forte return (0); 1454fcf3ce44SJohn Forte case -1: 1455fcf3ce44SJohn Forte /* 1456fcf3ce44SJohn Forte * System errors are returned to caller. 1457fcf3ce44SJohn Forte */ 1458fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 1459fcf3ce44SJohn Forte /* 1460fcf3ce44SJohn Forte * Save the error code across 1461fcf3ce44SJohn Forte * syslog(), just in case 1462fcf3ce44SJohn Forte * syslog() gets its own error 1463fcf3ce44SJohn Forte * and therefore overwrites errno. 1464fcf3ce44SJohn Forte */ 1465fcf3ce44SJohn Forte error = errno; 1466fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1467fcf3ce44SJohn Forte "t_look(file descriptor %d/transport %s) %m", 1468fcf3ce44SJohn Forte fd, nconf->nc_proto); 1469fcf3ce44SJohn Forte return (error); 1470fcf3ce44SJohn Forte } 1471fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1472fcf3ce44SJohn Forte "t_look(file descriptor %d/transport %s) TLI error %d", 1473fcf3ce44SJohn Forte fd, nconf->nc_proto, t_errno); 1474fcf3ce44SJohn Forte goto flush_it; 1475fcf3ce44SJohn Forte case T_UDERR: 1476fcf3ce44SJohn Forte break; 1477fcf3ce44SJohn Forte default: 1478*55393845SMarcel Telka (void) syslog(LOG_WARNING, "t_look(file descriptor %d/" 1479*55393845SMarcel Telka "transport %s) returned %d not T_UDERR (%d)", 1480fcf3ce44SJohn Forte fd, nconf->nc_proto, ret, T_UDERR); 1481fcf3ce44SJohn Forte } 1482fcf3ce44SJohn Forte 1483fcf3ce44SJohn Forte if (uderr == NULL) { 1484fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1485fcf3ce44SJohn Forte uderr = (struct t_uderr *)t_alloc(fd, T_UDERROR, T_ALL); 1486fcf3ce44SJohn Forte if (uderr == NULL) { 1487fcf3ce44SJohn Forte if (t_errno == TSYSERR) { 1488fcf3ce44SJohn Forte /* 1489fcf3ce44SJohn Forte * Save the error code across 1490fcf3ce44SJohn Forte * syslog(), just in case 1491fcf3ce44SJohn Forte * syslog() gets its own error 1492fcf3ce44SJohn Forte * and therefore overwrites errno. 1493fcf3ce44SJohn Forte */ 1494fcf3ce44SJohn Forte error = errno; 1495fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1496*55393845SMarcel Telka "t_alloc(file descriptor %d/transport %s, " 1497*55393845SMarcel Telka "T_UDERROR) failed: %m", 1498fcf3ce44SJohn Forte fd, nconf->nc_proto); 1499fcf3ce44SJohn Forte return (error); 1500fcf3ce44SJohn Forte } 1501*55393845SMarcel Telka (void) syslog(LOG_ERR, "t_alloc(file descriptor %d/" 1502*55393845SMarcel Telka "transport %s, T_UDERROR) failed TLI error: %d", 1503fcf3ce44SJohn Forte fd, nconf->nc_proto, t_errno); 1504fcf3ce44SJohn Forte goto flush_it; 1505fcf3ce44SJohn Forte } 1506fcf3ce44SJohn Forte } 1507fcf3ce44SJohn Forte 1508fcf3ce44SJohn Forte ret = t_rcvuderr(fd, uderr); 1509fcf3ce44SJohn Forte if (ret == 0) { 1510fcf3ce44SJohn Forte 1511fcf3ce44SJohn Forte /* 1512fcf3ce44SJohn Forte * Save the datagram error in errno, so that the 1513fcf3ce44SJohn Forte * %m argument to syslog picks up the error string. 1514fcf3ce44SJohn Forte */ 1515fcf3ce44SJohn Forte errno = uderr->error; 1516fcf3ce44SJohn Forte 1517fcf3ce44SJohn Forte /* 1518fcf3ce44SJohn Forte * Log the datagram error, then log the host that 1519fcf3ce44SJohn Forte * probably triggerred. Cannot log both in the 1520fcf3ce44SJohn Forte * same transaction because of packet size limitations 1521fcf3ce44SJohn Forte * in /dev/log. 1522fcf3ce44SJohn Forte */ 1523fcf3ce44SJohn Forte (void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING, 1524fcf3ce44SJohn Forte "%s response over <file descriptor %d/transport %s> " 1525fcf3ce44SJohn Forte "generated error: %m", 1526fcf3ce44SJohn Forte progname, fd, nconf->nc_proto); 1527fcf3ce44SJohn Forte 1528fcf3ce44SJohn Forte /* 1529fcf3ce44SJohn Forte * Try to map the client's address back to a 1530fcf3ce44SJohn Forte * name. 1531fcf3ce44SJohn Forte */ 1532fcf3ce44SJohn Forte ret = netdir_getbyaddr(nconf, &host, &uderr->addr); 1533fcf3ce44SJohn Forte if (ret != -1 && host && host->h_cnt > 0 && 1534fcf3ce44SJohn Forte host->h_hostservs) { 1535fcf3ce44SJohn Forte (void) syslog((errno == ECONNREFUSED) ? LOG_DEBUG : LOG_WARNING, 1536fcf3ce44SJohn Forte "Bad %s response was sent to client with " 1537fcf3ce44SJohn Forte "host name: %s; service port: %s", 1538fcf3ce44SJohn Forte progname, host->h_hostservs->h_host, 1539fcf3ce44SJohn Forte host->h_hostservs->h_serv); 1540fcf3ce44SJohn Forte } else { 1541fcf3ce44SJohn Forte int i, j; 1542fcf3ce44SJohn Forte char *buf; 1543fcf3ce44SJohn Forte char *hex = "0123456789abcdef"; 1544fcf3ce44SJohn Forte 1545fcf3ce44SJohn Forte /* 1546fcf3ce44SJohn Forte * Mapping failed, print the whole thing 1547fcf3ce44SJohn Forte * in ASCII hex. 1548fcf3ce44SJohn Forte */ 1549fcf3ce44SJohn Forte buf = (char *)malloc(uderr->addr.len * 2 + 1); 1550fcf3ce44SJohn Forte for (i = 0, j = 0; i < uderr->addr.len; i++, j += 2) { 1551fcf3ce44SJohn Forte buf[j] = hex[((uderr->addr.buf[i]) >> 4) & 0xf]; 1552fcf3ce44SJohn Forte buf[j+1] = hex[uderr->addr.buf[i] & 0xf]; 1553fcf3ce44SJohn Forte } 1554fcf3ce44SJohn Forte buf[j] = '\0'; 1555fcf3ce44SJohn Forte (void) syslog((errno == ECONNREFUSED) ? 1556fcf3ce44SJohn Forte LOG_DEBUG : LOG_WARNING, 1557fcf3ce44SJohn Forte "Bad %s response was sent to client with " 1558fcf3ce44SJohn Forte "transport address: 0x%s", 1559fcf3ce44SJohn Forte progname, buf); 1560fcf3ce44SJohn Forte free((void *)buf); 1561fcf3ce44SJohn Forte } 1562fcf3ce44SJohn Forte 1563fcf3ce44SJohn Forte if (ret == 0 && host != NULL) 1564fcf3ce44SJohn Forte netdir_free((void *)host, ND_HOSTSERVLIST); 1565fcf3ce44SJohn Forte return (0); 1566fcf3ce44SJohn Forte } 1567fcf3ce44SJohn Forte 1568fcf3ce44SJohn Forte switch (t_errno) { 1569fcf3ce44SJohn Forte case TNOUDERR: 1570fcf3ce44SJohn Forte goto flush_it; 1571fcf3ce44SJohn Forte case TSYSERR: 1572fcf3ce44SJohn Forte /* 1573fcf3ce44SJohn Forte * System errors are returned to caller. 1574fcf3ce44SJohn Forte * Save the error code across 1575fcf3ce44SJohn Forte * syslog(), just in case 1576fcf3ce44SJohn Forte * syslog() gets its own error 1577fcf3ce44SJohn Forte * and therefore overwrites errno. 1578fcf3ce44SJohn Forte */ 1579fcf3ce44SJohn Forte error = errno; 1580fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1581fcf3ce44SJohn Forte "t_rcvuderr(file descriptor %d/transport %s) %m", 1582fcf3ce44SJohn Forte fd, nconf->nc_proto); 1583fcf3ce44SJohn Forte return (error); 1584fcf3ce44SJohn Forte default: 1585fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1586fcf3ce44SJohn Forte "t_rcvuderr(file descriptor %d/transport %s) TLI error %d", 1587fcf3ce44SJohn Forte fd, nconf->nc_proto, t_errno); 1588fcf3ce44SJohn Forte goto flush_it; 1589fcf3ce44SJohn Forte } 1590fcf3ce44SJohn Forte 1591fcf3ce44SJohn Forte flush_it: 1592fcf3ce44SJohn Forte /* 1593fcf3ce44SJohn Forte * If we get here, then we could not cope with whatever message 1594fcf3ce44SJohn Forte * we attempted to read, so flush it. If we did read a message, 1595fcf3ce44SJohn Forte * and one isn't present, that is all right, because fd is in 1596fcf3ce44SJohn Forte * nonblocking mode. 1597fcf3ce44SJohn Forte */ 1598fcf3ce44SJohn Forte (void) syslog(LOG_ERR, 1599fcf3ce44SJohn Forte "Flushing one input message from <file descriptor %d/transport %s>", 1600fcf3ce44SJohn Forte fd, nconf->nc_proto); 1601fcf3ce44SJohn Forte 1602fcf3ce44SJohn Forte /* 1603fcf3ce44SJohn Forte * Read and discard the message. Do this this until there is 1604fcf3ce44SJohn Forte * no more control/data in the message or until we get an error. 1605fcf3ce44SJohn Forte */ 1606fcf3ce44SJohn Forte do { 1607fcf3ce44SJohn Forte ctl->maxlen = sizeof (ctlbuf); 1608fcf3ce44SJohn Forte ctl->buf = ctlbuf; 1609fcf3ce44SJohn Forte data->maxlen = sizeof (databuf); 1610fcf3ce44SJohn Forte data->buf = databuf; 1611fcf3ce44SJohn Forte flags = 0; 1612fcf3ce44SJohn Forte ret = getmsg(fd, ctl, data, &flags); 1613fcf3ce44SJohn Forte if (ret == -1) 1614fcf3ce44SJohn Forte return (errno); 1615fcf3ce44SJohn Forte } while (ret != 0); 1616fcf3ce44SJohn Forte 1617fcf3ce44SJohn Forte return (0); 1618fcf3ce44SJohn Forte } 1619fcf3ce44SJohn Forte 1620fcf3ce44SJohn Forte /* 1621fcf3ce44SJohn Forte * Establish service thread. 1622fcf3ce44SJohn Forte */ 1623fcf3ce44SJohn Forte static int 1624fcf3ce44SJohn Forte rdcsvc(int fd, struct netbuf addrmask, struct netconfig *nconf) 1625fcf3ce44SJohn Forte { 1626fcf3ce44SJohn Forte #ifdef __NCALL__ 1627fcf3ce44SJohn Forte struct ncall_svc_args nsa; 1628fcf3ce44SJohn Forte #else /* !__NCALL__ */ 1629fcf3ce44SJohn Forte struct rdc_svc_args nsa; 1630fcf3ce44SJohn Forte _rdc_ioctl_t rdc_args = { 0, }; 1631fcf3ce44SJohn Forte #endif /* __NCALL__ */ 1632fcf3ce44SJohn Forte 1633fcf3ce44SJohn Forte nsa.fd = fd; 1634fcf3ce44SJohn Forte nsa.nthr = (max_conns_allowed < 0 ? 16 : max_conns_allowed); 1635570de38fSSurya Prakki (void) strncpy(nsa.netid, nconf->nc_netid, sizeof (nsa.netid)); 1636fcf3ce44SJohn Forte nsa.addrmask.len = addrmask.len; 1637fcf3ce44SJohn Forte nsa.addrmask.maxlen = addrmask.maxlen; 1638fcf3ce44SJohn Forte nsa.addrmask.buf = addrmask.buf; 1639fcf3ce44SJohn Forte 1640fcf3ce44SJohn Forte #ifdef __NCALL__ 1641fcf3ce44SJohn Forte return (sndrsys(NC_IOC_SERVER, &nsa)); 1642fcf3ce44SJohn Forte #else /* !__NCALL__ */ 1643fcf3ce44SJohn Forte rdc_args.arg0 = (long)&nsa; 1644fcf3ce44SJohn Forte return (sndrsys(RDC_ENABLE_SVR, &rdc_args)); 1645fcf3ce44SJohn Forte #endif /* __NCALL__ */ 1646fcf3ce44SJohn Forte } 1647fcf3ce44SJohn Forte 1648fcf3ce44SJohn Forte 1649fcf3ce44SJohn Forte 1650fcf3ce44SJohn Forte static int 1651fcf3ce44SJohn Forte nofile_increase(int limit) 1652fcf3ce44SJohn Forte { 1653fcf3ce44SJohn Forte struct rlimit rl; 1654fcf3ce44SJohn Forte 1655fcf3ce44SJohn Forte if (getrlimit(RLIMIT_NOFILE, &rl) == -1) { 1656fcf3ce44SJohn Forte syslog(LOG_ERR, 1657fcf3ce44SJohn Forte "nofile_increase() getrlimit of NOFILE failed: %m"); 1658fcf3ce44SJohn Forte return (-1); 1659fcf3ce44SJohn Forte } 1660fcf3ce44SJohn Forte 1661fcf3ce44SJohn Forte if (limit > 0) 1662fcf3ce44SJohn Forte rl.rlim_cur = limit; 1663fcf3ce44SJohn Forte else 1664fcf3ce44SJohn Forte rl.rlim_cur += NOFILE_INC_SIZE; 1665fcf3ce44SJohn Forte 1666fcf3ce44SJohn Forte if (rl.rlim_cur > rl.rlim_max && rl.rlim_max != RLIM_INFINITY) 1667fcf3ce44SJohn Forte rl.rlim_max = rl.rlim_cur; 1668fcf3ce44SJohn Forte 1669fcf3ce44SJohn Forte if (setrlimit(RLIMIT_NOFILE, &rl) == -1) { 1670fcf3ce44SJohn Forte syslog(LOG_ERR, 1671fcf3ce44SJohn Forte "nofile_increase() setrlimit of NOFILE to %d failed: %m", 1672fcf3ce44SJohn Forte rl.rlim_cur); 1673fcf3ce44SJohn Forte return (-1); 1674fcf3ce44SJohn Forte } 1675fcf3ce44SJohn Forte 1676fcf3ce44SJohn Forte return (0); 1677fcf3ce44SJohn Forte } 1678fcf3ce44SJohn Forte 1679fcf3ce44SJohn Forte int 1680fcf3ce44SJohn Forte rdcd_bindit(struct netconfig *nconf, struct netbuf **addr, 1681fcf3ce44SJohn Forte struct nd_hostserv *hs, int backlog) 1682fcf3ce44SJohn Forte { 1683fcf3ce44SJohn Forte int fd; 1684fcf3ce44SJohn Forte struct t_bind *ntb; 1685fcf3ce44SJohn Forte struct t_bind tb; 1686fcf3ce44SJohn Forte struct nd_addrlist *addrlist; 1687fcf3ce44SJohn Forte struct t_optmgmt req, resp; 1688fcf3ce44SJohn Forte struct opthdr *opt; 1689fcf3ce44SJohn Forte char reqbuf[128]; 1690fcf3ce44SJohn Forte 1691fcf3ce44SJohn Forte if ((fd = rdc_transport_open(nconf)) == -1) { 1692fcf3ce44SJohn Forte syslog(LOG_ERR, "cannot establish transport service over %s", 1693fcf3ce44SJohn Forte nconf->nc_device); 1694fcf3ce44SJohn Forte return (-1); 1695fcf3ce44SJohn Forte } 1696fcf3ce44SJohn Forte 1697fcf3ce44SJohn Forte addrlist = (struct nd_addrlist *)NULL; 1698fcf3ce44SJohn Forte if (netdir_getbyname(nconf, hs, &addrlist) != 0) { 1699fcf3ce44SJohn Forte if (strncmp(nconf->nc_netid, "udp", 3) != 0) { 1700fcf3ce44SJohn Forte syslog(LOG_ERR, "Cannot get address for transport " 1701fcf3ce44SJohn Forte "%s host %s service %s", 1702fcf3ce44SJohn Forte nconf->nc_netid, hs->h_host, hs->h_serv); 1703fcf3ce44SJohn Forte } 1704fcf3ce44SJohn Forte (void) t_close(fd); 1705fcf3ce44SJohn Forte return (-1); 1706fcf3ce44SJohn Forte } 1707fcf3ce44SJohn Forte 1708fcf3ce44SJohn Forte if (strcmp(nconf->nc_proto, "tcp") == 0) { 1709fcf3ce44SJohn Forte /* 1710fcf3ce44SJohn Forte * If we're running over TCP, then set the 1711fcf3ce44SJohn Forte * SO_REUSEADDR option so that we can bind 1712fcf3ce44SJohn Forte * to our preferred address even if previously 1713fcf3ce44SJohn Forte * left connections exist in FIN_WAIT states. 1714fcf3ce44SJohn Forte * This is somewhat bogus, but otherwise you have 1715fcf3ce44SJohn Forte * to wait 2 minutes to restart after killing it. 1716fcf3ce44SJohn Forte */ 1717fcf3ce44SJohn Forte if (reuseaddr(fd) == -1) { 1718fcf3ce44SJohn Forte syslog(LOG_WARNING, 1719fcf3ce44SJohn Forte "couldn't set SO_REUSEADDR option on transport"); 1720fcf3ce44SJohn Forte } 1721fcf3ce44SJohn Forte } 1722fcf3ce44SJohn Forte 1723fcf3ce44SJohn Forte if (nconf->nc_semantics == NC_TPI_CLTS) 1724fcf3ce44SJohn Forte tb.qlen = 0; 1725fcf3ce44SJohn Forte else 1726fcf3ce44SJohn Forte tb.qlen = backlog; 1727fcf3ce44SJohn Forte 1728fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1729fcf3ce44SJohn Forte ntb = (struct t_bind *)t_alloc(fd, T_BIND, T_ALL); 1730fcf3ce44SJohn Forte if (ntb == (struct t_bind *)NULL) { 1731fcf3ce44SJohn Forte syslog(LOG_ERR, "t_alloc failed: t_errno %d, %m", t_errno); 1732fcf3ce44SJohn Forte (void) t_close(fd); 1733fcf3ce44SJohn Forte netdir_free((void *)addrlist, ND_ADDRLIST); 1734fcf3ce44SJohn Forte return (-1); 1735fcf3ce44SJohn Forte } 1736fcf3ce44SJohn Forte 1737fcf3ce44SJohn Forte tb.addr = *(addrlist->n_addrs); /* structure copy */ 1738fcf3ce44SJohn Forte 1739fcf3ce44SJohn Forte if (t_bind(fd, &tb, ntb) == -1) { 1740fcf3ce44SJohn Forte syslog(LOG_ERR, "t_bind failed: t_errno %d, %m", t_errno); 1741fcf3ce44SJohn Forte (void) t_free((char *)ntb, T_BIND); 1742fcf3ce44SJohn Forte netdir_free((void *)addrlist, ND_ADDRLIST); 1743fcf3ce44SJohn Forte (void) t_close(fd); 1744fcf3ce44SJohn Forte return (-1); 1745fcf3ce44SJohn Forte } 1746fcf3ce44SJohn Forte 1747fcf3ce44SJohn Forte /* make sure we bound to the right address */ 1748fcf3ce44SJohn Forte if (tb.addr.len != ntb->addr.len || 1749fcf3ce44SJohn Forte memcmp(tb.addr.buf, ntb->addr.buf, tb.addr.len) != 0) { 1750fcf3ce44SJohn Forte syslog(LOG_ERR, "t_bind to wrong address"); 1751fcf3ce44SJohn Forte (void) t_free((char *)ntb, T_BIND); 1752fcf3ce44SJohn Forte netdir_free((void *)addrlist, ND_ADDRLIST); 1753fcf3ce44SJohn Forte (void) t_close(fd); 1754fcf3ce44SJohn Forte return (-1); 1755fcf3ce44SJohn Forte } 1756fcf3ce44SJohn Forte 1757fcf3ce44SJohn Forte *addr = &ntb->addr; 1758fcf3ce44SJohn Forte netdir_free((void *)addrlist, ND_ADDRLIST); 1759fcf3ce44SJohn Forte 1760fcf3ce44SJohn Forte if (strcmp(nconf->nc_proto, "tcp") == 0 || 1761fcf3ce44SJohn Forte strcmp(nconf->nc_proto, "tcp6") == 0) { 1762fcf3ce44SJohn Forte /* 1763fcf3ce44SJohn Forte * Disable the Nagle algorithm on TCP connections. 1764fcf3ce44SJohn Forte * Connections accepted from this listener will 1765fcf3ce44SJohn Forte * inherit the listener options. 1766fcf3ce44SJohn Forte */ 1767fcf3ce44SJohn Forte 1768fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1769fcf3ce44SJohn Forte opt = (struct opthdr *)reqbuf; 1770fcf3ce44SJohn Forte opt->level = IPPROTO_TCP; 1771fcf3ce44SJohn Forte opt->name = TCP_NODELAY; 1772fcf3ce44SJohn Forte opt->len = sizeof (int); 1773fcf3ce44SJohn Forte 1774fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1775fcf3ce44SJohn Forte *(int *)((char *)opt + sizeof (*opt)) = 1; 1776fcf3ce44SJohn Forte 1777fcf3ce44SJohn Forte req.flags = T_NEGOTIATE; 1778fcf3ce44SJohn Forte req.opt.len = sizeof (*opt) + opt->len; 1779fcf3ce44SJohn Forte req.opt.buf = (char *)opt; 1780fcf3ce44SJohn Forte resp.flags = 0; 1781fcf3ce44SJohn Forte resp.opt.buf = reqbuf; 1782fcf3ce44SJohn Forte resp.opt.maxlen = sizeof (reqbuf); 1783fcf3ce44SJohn Forte 1784fcf3ce44SJohn Forte if (t_optmgmt(fd, &req, &resp) < 0 || 1785fcf3ce44SJohn Forte resp.flags != T_SUCCESS) { 1786*55393845SMarcel Telka syslog(LOG_ERR, "couldn't set NODELAY option for " 1787*55393845SMarcel Telka "proto %s: t_errno = %d, %m", nconf->nc_proto, 1788*55393845SMarcel Telka t_errno); 1789fcf3ce44SJohn Forte } 1790fcf3ce44SJohn Forte } 1791fcf3ce44SJohn Forte 1792fcf3ce44SJohn Forte return (fd); 1793fcf3ce44SJohn Forte } 1794fcf3ce44SJohn Forte 1795fcf3ce44SJohn Forte 1796fcf3ce44SJohn Forte /* ARGSUSED */ 1797fcf3ce44SJohn Forte static int 1798fcf3ce44SJohn Forte bind_to_provider(char *provider, char *serv, struct netbuf **addr, 1799fcf3ce44SJohn Forte struct netconfig **retnconf) 1800fcf3ce44SJohn Forte { 1801fcf3ce44SJohn Forte struct netconfig *nconf; 1802fcf3ce44SJohn Forte NCONF_HANDLE *nc; 1803fcf3ce44SJohn Forte struct nd_hostserv hs; 1804fcf3ce44SJohn Forte 1805fcf3ce44SJohn Forte hs.h_host = HOST_SELF; 1806fcf3ce44SJohn Forte hs.h_serv = RDC_SERVICE; /* serv_name_to_port_name(serv); */ 1807fcf3ce44SJohn Forte 1808fcf3ce44SJohn Forte if ((nc = setnetconfig()) == (NCONF_HANDLE *)NULL) { 1809fcf3ce44SJohn Forte syslog(LOG_ERR, "setnetconfig failed: %m"); 1810fcf3ce44SJohn Forte return (-1); 1811fcf3ce44SJohn Forte } 1812fcf3ce44SJohn Forte while (nconf = getnetconfig(nc)) { 1813fcf3ce44SJohn Forte if (OK_TPI_TYPE(nconf) && 1814fcf3ce44SJohn Forte strcmp(nconf->nc_device, provider) == 0) { 1815fcf3ce44SJohn Forte *retnconf = nconf; 1816fcf3ce44SJohn Forte return (rdcd_bindit(nconf, addr, &hs, listen_backlog)); 1817fcf3ce44SJohn Forte } 1818fcf3ce44SJohn Forte } 1819fcf3ce44SJohn Forte (void) endnetconfig(nc); 1820fcf3ce44SJohn Forte if ((Is_ipv6present() && (strcmp(provider, "/dev/tcp6") == 0)) || 1821fcf3ce44SJohn Forte (!Is_ipv6present() && (strcmp(provider, "/dev/tcp") == 0))) 1822fcf3ce44SJohn Forte syslog(LOG_ERR, "couldn't find netconfig entry for provider %s", 1823fcf3ce44SJohn Forte provider); 1824fcf3ce44SJohn Forte return (-1); 1825fcf3ce44SJohn Forte } 1826fcf3ce44SJohn Forte 1827fcf3ce44SJohn Forte 1828fcf3ce44SJohn Forte /* 1829fcf3ce44SJohn Forte * For listen fd's index is always less than end_listen_fds. 1830fcf3ce44SJohn Forte * It's value is equal to the number of open file descriptors after the 1831fcf3ce44SJohn Forte * last listen end point was opened but before any connection was accepted. 1832fcf3ce44SJohn Forte */ 1833fcf3ce44SJohn Forte static int 1834fcf3ce44SJohn Forte is_listen_fd_index(int index) 1835fcf3ce44SJohn Forte { 1836fcf3ce44SJohn Forte return (index < end_listen_fds); 1837fcf3ce44SJohn Forte } 1838fcf3ce44SJohn Forte 1839fcf3ce44SJohn Forte 1840fcf3ce44SJohn Forte /* 1841fcf3ce44SJohn Forte * Create an address mask appropriate for the transport. 1842fcf3ce44SJohn Forte * The mask is used to obtain the host-specific part of 1843fcf3ce44SJohn Forte * a network address when comparing addresses. 1844fcf3ce44SJohn Forte * For an internet address the host-specific part is just 1845fcf3ce44SJohn Forte * the 32 bit IP address and this part of the mask is set 1846fcf3ce44SJohn Forte * to all-ones. The port number part of the mask is zeroes. 1847fcf3ce44SJohn Forte */ 1848fcf3ce44SJohn Forte static int 1849fcf3ce44SJohn Forte set_addrmask(int fd, struct netconfig *nconf, struct netbuf *mask) 1850fcf3ce44SJohn Forte { 1851fcf3ce44SJohn Forte struct t_info info; 1852fcf3ce44SJohn Forte 1853fcf3ce44SJohn Forte /* 1854fcf3ce44SJohn Forte * Find the size of the address we need to mask. 1855fcf3ce44SJohn Forte */ 1856fcf3ce44SJohn Forte if (t_getinfo(fd, &info) < 0) { 1857fcf3ce44SJohn Forte t_error("t_getinfo"); 1858fcf3ce44SJohn Forte return (-1); 1859fcf3ce44SJohn Forte } 1860fcf3ce44SJohn Forte mask->len = mask->maxlen = info.addr; 1861fcf3ce44SJohn Forte if (info.addr <= 0) { 1862*55393845SMarcel Telka syslog(LOG_ERR, "set_addrmask: address size: %ld", info.addr); 1863fcf3ce44SJohn Forte return (-1); 1864fcf3ce44SJohn Forte } 1865fcf3ce44SJohn Forte 1866fcf3ce44SJohn Forte mask->buf = (char *)malloc(mask->len); 1867fcf3ce44SJohn Forte if (mask->buf == NULL) { 1868fcf3ce44SJohn Forte syslog(LOG_ERR, "set_addrmask: no memory"); 1869fcf3ce44SJohn Forte return (-1); 1870fcf3ce44SJohn Forte } 1871fcf3ce44SJohn Forte (void) memset(mask->buf, 0, mask->len); /* reset all mask bits */ 1872fcf3ce44SJohn Forte 1873fcf3ce44SJohn Forte if (strcmp(nconf->nc_protofmly, NC_INET) == 0) { 1874fcf3ce44SJohn Forte /* 1875fcf3ce44SJohn Forte * Set the mask so that the port is ignored. 1876fcf3ce44SJohn Forte */ 1877fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1878fcf3ce44SJohn Forte ((struct sockaddr_in *)mask->buf)->sin_addr.s_addr = 1879fcf3ce44SJohn Forte (in_addr_t)~0; 1880fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1881fcf3ce44SJohn Forte ((struct sockaddr_in *)mask->buf)->sin_family = (sa_family_t)~0; 1882fcf3ce44SJohn Forte } 1883fcf3ce44SJohn Forte #ifdef NC_INET6 1884fcf3ce44SJohn Forte else if (strcmp(nconf->nc_protofmly, NC_INET6) == 0) { 1885fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1886fcf3ce44SJohn Forte (void) memset(&((struct sockaddr_in6 *)mask->buf)->sin6_addr, 1887fcf3ce44SJohn Forte (uchar_t)~0, sizeof (struct in6_addr)); 1888fcf3ce44SJohn Forte /* LINTED pointer alignment */ 1889fcf3ce44SJohn Forte ((struct sockaddr_in6 *)mask->buf)->sin6_family = 1890fcf3ce44SJohn Forte (sa_family_t)~0; 1891fcf3ce44SJohn Forte } 1892fcf3ce44SJohn Forte #endif 1893fcf3ce44SJohn Forte else { 1894fcf3ce44SJohn Forte /* 1895fcf3ce44SJohn Forte * Set all mask bits. 1896fcf3ce44SJohn Forte */ 1897fcf3ce44SJohn Forte (void) memset(mask->buf, (uchar_t)~0, mask->len); 1898fcf3ce44SJohn Forte } 1899fcf3ce44SJohn Forte return (0); 1900fcf3ce44SJohn Forte } 1901fcf3ce44SJohn Forte 1902fcf3ce44SJohn Forte #if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8) 1903fcf3ce44SJohn Forte 1904fcf3ce44SJohn Forte static int 1905fcf3ce44SJohn Forte sndrsvcpool(int maxservers) 1906fcf3ce44SJohn Forte { 1907fcf3ce44SJohn Forte struct svcpool_args npa; 1908fcf3ce44SJohn Forte 1909fcf3ce44SJohn Forte npa.id = RDC_SVCPOOL_ID; 1910fcf3ce44SJohn Forte npa.maxthreads = maxservers; 1911fcf3ce44SJohn Forte npa.redline = 0; 1912fcf3ce44SJohn Forte npa.qsize = 0; 1913fcf3ce44SJohn Forte npa.timeout = 0; 1914fcf3ce44SJohn Forte npa.stksize = 0; 1915fcf3ce44SJohn Forte npa.max_same_xprt = 0; 1916fcf3ce44SJohn Forte return (sndrsys(RDC_POOL_CREATE, &npa)); 1917fcf3ce44SJohn Forte } 1918fcf3ce44SJohn Forte 1919fcf3ce44SJohn Forte 1920fcf3ce44SJohn Forte /* 1921fcf3ce44SJohn Forte * The following stolen from cmd/fs.d/nfs/lib/thrpool.c 1922fcf3ce44SJohn Forte */ 1923fcf3ce44SJohn Forte 1924fcf3ce44SJohn Forte #include <thread.h> 1925fcf3ce44SJohn Forte 1926fcf3ce44SJohn Forte /* 1927fcf3ce44SJohn Forte * Thread to call into the kernel and do work on behalf of SNDR/ncall-ip. 1928fcf3ce44SJohn Forte */ 1929fcf3ce44SJohn Forte static void * 1930fcf3ce44SJohn Forte svcstart(void *arg) 1931fcf3ce44SJohn Forte { 1932fcf3ce44SJohn Forte int id = (int)arg; 1933fcf3ce44SJohn Forte int err; 1934fcf3ce44SJohn Forte 1935fcf3ce44SJohn Forte while ((err = sndrsys(RDC_POOL_RUN, &id)) != 0) { 1936fcf3ce44SJohn Forte /* 1937fcf3ce44SJohn Forte * Interrupted by a signal while in the kernel. 1938fcf3ce44SJohn Forte * this process is still alive, try again. 1939fcf3ce44SJohn Forte */ 1940fcf3ce44SJohn Forte if (err == EINTR) 1941fcf3ce44SJohn Forte continue; 1942fcf3ce44SJohn Forte else 1943fcf3ce44SJohn Forte break; 1944fcf3ce44SJohn Forte } 1945fcf3ce44SJohn Forte 1946fcf3ce44SJohn Forte /* 1947fcf3ce44SJohn Forte * If we weren't interrupted by a signal, but did 1948fcf3ce44SJohn Forte * return from the kernel, this thread's work is done, 1949fcf3ce44SJohn Forte * and it should exit. 1950fcf3ce44SJohn Forte */ 1951fcf3ce44SJohn Forte thr_exit(NULL); 1952fcf3ce44SJohn Forte return (NULL); 1953fcf3ce44SJohn Forte } 1954fcf3ce44SJohn Forte 1955fcf3ce44SJohn Forte /* 1956fcf3ce44SJohn Forte * User-space "creator" thread. This thread blocks in the kernel 1957fcf3ce44SJohn Forte * until new worker threads need to be created for the service 1958fcf3ce44SJohn Forte * pool. On return to userspace, if there is no error, create a 1959fcf3ce44SJohn Forte * new thread for the service pool. 1960fcf3ce44SJohn Forte */ 1961fcf3ce44SJohn Forte static void * 1962fcf3ce44SJohn Forte svcblock(void *arg) 1963fcf3ce44SJohn Forte { 1964fcf3ce44SJohn Forte int id = (int)arg; 1965fcf3ce44SJohn Forte 1966fcf3ce44SJohn Forte /* CONSTCOND */ 1967fcf3ce44SJohn Forte while (1) { 1968fcf3ce44SJohn Forte thread_t tid; 1969fcf3ce44SJohn Forte int err; 1970fcf3ce44SJohn Forte 1971fcf3ce44SJohn Forte /* 1972fcf3ce44SJohn Forte * Call into the kernel, and hang out there 1973fcf3ce44SJohn Forte * until a thread needs to be created. 1974fcf3ce44SJohn Forte */ 1975fcf3ce44SJohn Forte if (err = sndrsys(RDC_POOL_WAIT, &id)) { 1976fcf3ce44SJohn Forte if (err == ECANCELED || err == EBUSY) 1977fcf3ce44SJohn Forte /* 1978fcf3ce44SJohn Forte * If we get back ECANCELED, the service 1979fcf3ce44SJohn Forte * pool is exiting, and we may as well 1980fcf3ce44SJohn Forte * clean up this thread. If EBUSY is 1981fcf3ce44SJohn Forte * returned, there's already a thread 1982fcf3ce44SJohn Forte * looping on this pool, so we should 1983fcf3ce44SJohn Forte * give up. 1984fcf3ce44SJohn Forte */ 1985fcf3ce44SJohn Forte break; 1986fcf3ce44SJohn Forte else 1987fcf3ce44SJohn Forte continue; 1988fcf3ce44SJohn Forte } 1989fcf3ce44SJohn Forte 1990fcf3ce44SJohn Forte (void) thr_create(NULL, NULL, svcstart, (void *)id, 1991fcf3ce44SJohn Forte THR_BOUND | THR_DETACHED, &tid); 1992fcf3ce44SJohn Forte } 1993fcf3ce44SJohn Forte 1994fcf3ce44SJohn Forte thr_exit(NULL); 1995fcf3ce44SJohn Forte return (NULL); 1996fcf3ce44SJohn Forte } 1997fcf3ce44SJohn Forte 1998fcf3ce44SJohn Forte static int 1999fcf3ce44SJohn Forte svcwait(int id) 2000fcf3ce44SJohn Forte { 2001fcf3ce44SJohn Forte thread_t tid; 2002fcf3ce44SJohn Forte 2003fcf3ce44SJohn Forte /* 2004fcf3ce44SJohn Forte * Create a bound thread to wait for kernel LWPs that 2005fcf3ce44SJohn Forte * need to be created. 2006fcf3ce44SJohn Forte */ 2007fcf3ce44SJohn Forte if (thr_create(NULL, NULL, svcblock, (void *)id, 2008fcf3ce44SJohn Forte THR_BOUND | THR_DETACHED, &tid)) 2009fcf3ce44SJohn Forte return (1); 2010fcf3ce44SJohn Forte 2011fcf3ce44SJohn Forte return (0); 2012fcf3ce44SJohn Forte } 2013fcf3ce44SJohn Forte #endif /* Solaris 9+ */ 2014