1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * CDDL HEADER START 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*0Sstevel@tonic-gate * with the License. 8*0Sstevel@tonic-gate * 9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*0Sstevel@tonic-gate * See the License for the specific language governing permissions 12*0Sstevel@tonic-gate * and limitations under the License. 13*0Sstevel@tonic-gate * 14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*0Sstevel@tonic-gate * 20*0Sstevel@tonic-gate * CDDL HEADER END 21*0Sstevel@tonic-gate */ 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gate /* 24*0Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 25*0Sstevel@tonic-gate * Use is subject to license terms. 26*0Sstevel@tonic-gate */ 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate #include <sys/lvm/mdmn_commd.h> 31*0Sstevel@tonic-gate #include <stdio.h> 32*0Sstevel@tonic-gate #include <stdlib.h> /* getenv, exit */ 33*0Sstevel@tonic-gate #include <signal.h> 34*0Sstevel@tonic-gate #include <unistd.h> 35*0Sstevel@tonic-gate #include <sys/types.h> 36*0Sstevel@tonic-gate #include <memory.h> 37*0Sstevel@tonic-gate #include <stropts.h> 38*0Sstevel@tonic-gate #include <netconfig.h> 39*0Sstevel@tonic-gate #include <sys/resource.h> /* rlimit */ 40*0Sstevel@tonic-gate #include <syslog.h> 41*0Sstevel@tonic-gate #include <meta.h> 42*0Sstevel@tonic-gate 43*0Sstevel@tonic-gate #ifdef DEBUG 44*0Sstevel@tonic-gate #define RPC_SVC_FG 45*0Sstevel@tonic-gate #endif 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gate /* 48*0Sstevel@tonic-gate * This means we shutdown rpc.mdcommd at some time in the window 49*0Sstevel@tonic-gate * after 1201 seconds and before 2400 seconds of inactivity. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate #define _RPCSVC_CLOSEDOWN 2400 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #ifdef RPC_SVC_FG 54*0Sstevel@tonic-gate static int _rpcpmstart; /* Started by a port monitor ? */ 55*0Sstevel@tonic-gate #endif /* RPC_SVC_FG */ 56*0Sstevel@tonic-gate /* States a server can be in wrt request */ 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gate #define _IDLE 0 59*0Sstevel@tonic-gate #define _SERVED 1 60*0Sstevel@tonic-gate 61*0Sstevel@tonic-gate static int _rpcsvcstate = _IDLE; /* Set when a request is serviced */ 62*0Sstevel@tonic-gate static int _rpcsvccount = 0; /* Number of requests being serviced */ 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate extern md_mn_result_t *mdmn_send_svc_1(); 65*0Sstevel@tonic-gate extern int *mdmn_work_svc_1(); 66*0Sstevel@tonic-gate extern int *mdmn_wakeup_initiator_svc_1(); 67*0Sstevel@tonic-gate extern int *mdmn_wakeup_master_svc_1(); 68*0Sstevel@tonic-gate extern int *mdmn_comm_lock_svc_1(); 69*0Sstevel@tonic-gate extern int *mdmn_comm_unlock_svc_1(); 70*0Sstevel@tonic-gate extern int *mdmn_comm_suspend_svc_1(); 71*0Sstevel@tonic-gate extern int *mdmn_comm_resume_svc_1(); 72*0Sstevel@tonic-gate extern int *mdmn_comm_reinit_set_svc_1(); 73*0Sstevel@tonic-gate extern int *mdmn_comm_msglock_svc_1(); 74*0Sstevel@tonic-gate 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate static void 77*0Sstevel@tonic-gate _msgout(msg) 78*0Sstevel@tonic-gate char *msg; 79*0Sstevel@tonic-gate { 80*0Sstevel@tonic-gate #ifdef RPC_SVC_FG 81*0Sstevel@tonic-gate if (_rpcpmstart) 82*0Sstevel@tonic-gate syslog(LOG_ERR, "%s", msg); 83*0Sstevel@tonic-gate else 84*0Sstevel@tonic-gate (void) fprintf(stderr, "%s\n", msg); 85*0Sstevel@tonic-gate #else 86*0Sstevel@tonic-gate syslog(LOG_ERR, "%s", msg); 87*0Sstevel@tonic-gate #endif 88*0Sstevel@tonic-gate } 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate static void 91*0Sstevel@tonic-gate closedown(void) 92*0Sstevel@tonic-gate { 93*0Sstevel@tonic-gate if (_rpcsvcstate == _IDLE && _rpcsvccount == 0) { 94*0Sstevel@tonic-gate int size; 95*0Sstevel@tonic-gate int i, openfd = 0; 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate size = svc_max_pollfd; 98*0Sstevel@tonic-gate for (i = 0; i < size && openfd < 2; i++) 99*0Sstevel@tonic-gate if (svc_pollfd[i].fd >= 0) 100*0Sstevel@tonic-gate openfd++; 101*0Sstevel@tonic-gate if (openfd <= 1) 102*0Sstevel@tonic-gate exit(0); 103*0Sstevel@tonic-gate } else 104*0Sstevel@tonic-gate _rpcsvcstate = _IDLE; 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate (void) signal(SIGALRM, (void(*)()) closedown); 107*0Sstevel@tonic-gate (void) alarm(_RPCSVC_CLOSEDOWN/2); 108*0Sstevel@tonic-gate } 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate static void 111*0Sstevel@tonic-gate mdmn_commd_1(rqstp, transp) 112*0Sstevel@tonic-gate struct svc_req *rqstp; 113*0Sstevel@tonic-gate register SVCXPRT *transp; 114*0Sstevel@tonic-gate { 115*0Sstevel@tonic-gate union { 116*0Sstevel@tonic-gate md_mn_msg_t mdmn_send_1_arg; 117*0Sstevel@tonic-gate md_mn_msg_t mdmn_work_1_arg; 118*0Sstevel@tonic-gate md_mn_result_t mdmn_wakeup_1_arg; 119*0Sstevel@tonic-gate md_mn_msgclass_t mdmn_comm_lock_1_arg; 120*0Sstevel@tonic-gate md_mn_msgclass_t mdmn_comm_unlock_1_arg; 121*0Sstevel@tonic-gate uint_t mdmn_comm_reinit_1_arg; 122*0Sstevel@tonic-gate } argument; 123*0Sstevel@tonic-gate char *result; 124*0Sstevel@tonic-gate bool_t (*_xdr_argument)(), (*_xdr_result)(); 125*0Sstevel@tonic-gate char *(*local)(); 126*0Sstevel@tonic-gate int free_result = 0; 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate _rpcsvccount++; 130*0Sstevel@tonic-gate switch (rqstp->rq_proc) { 131*0Sstevel@tonic-gate case NULLPROC: 132*0Sstevel@tonic-gate (void) svc_sendreply(transp, xdr_void, 133*0Sstevel@tonic-gate (char *)NULL); 134*0Sstevel@tonic-gate _rpcsvccount--; 135*0Sstevel@tonic-gate _rpcsvcstate = _SERVED; 136*0Sstevel@tonic-gate return; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate case mdmn_send: 139*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_msg_t; 140*0Sstevel@tonic-gate _xdr_result = xdr_md_mn_result_t; 141*0Sstevel@tonic-gate (void) memset((char *)&argument, 0, sizeof (argument)); 142*0Sstevel@tonic-gate if (!svc_getargs(transp, _xdr_argument, (caddr_t)&argument)) { 143*0Sstevel@tonic-gate svcerr_decode(transp); 144*0Sstevel@tonic-gate _rpcsvccount--; 145*0Sstevel@tonic-gate _rpcsvcstate = _SERVED; 146*0Sstevel@tonic-gate return; 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate /* 149*0Sstevel@tonic-gate * mdmn_send_1 will not always do a sendreply. 150*0Sstevel@tonic-gate * it will register in a table and let the mdmn_wakeup1 151*0Sstevel@tonic-gate * do the sendreply for that call. 152*0Sstevel@tonic-gate * in order to register properly we need the transp handle 153*0Sstevel@tonic-gate */ 154*0Sstevel@tonic-gate (void) mdmn_send_svc_1((md_mn_msg_t *)&argument, rqstp); 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate return; /* xdr_free is called by mdmn_wakeup_initiator_svc_1 */ 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate case mdmn_work: 159*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_msg_t; 160*0Sstevel@tonic-gate _xdr_result = xdr_int; 161*0Sstevel@tonic-gate local = (char *(*)()) mdmn_work_svc_1; 162*0Sstevel@tonic-gate free_result = 1; 163*0Sstevel@tonic-gate break; 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate case mdmn_wakeup_master: 166*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_result_t; 167*0Sstevel@tonic-gate _xdr_result = xdr_int; 168*0Sstevel@tonic-gate local = (char *(*)()) mdmn_wakeup_master_svc_1; 169*0Sstevel@tonic-gate free_result = 1; 170*0Sstevel@tonic-gate break; 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate case mdmn_wakeup_initiator: 173*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_result_t; 174*0Sstevel@tonic-gate _xdr_result = xdr_int; 175*0Sstevel@tonic-gate local = (char *(*)()) mdmn_wakeup_initiator_svc_1; 176*0Sstevel@tonic-gate free_result = 1; 177*0Sstevel@tonic-gate break; 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate case mdmn_comm_lock: 180*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_set_and_class_t; 181*0Sstevel@tonic-gate _xdr_result = xdr_int; 182*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_lock_svc_1; 183*0Sstevel@tonic-gate break; 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate case mdmn_comm_unlock: 186*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_set_and_class_t; 187*0Sstevel@tonic-gate _xdr_result = xdr_int; 188*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_unlock_svc_1; 189*0Sstevel@tonic-gate break; 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gate case mdmn_comm_suspend: 192*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_set_and_class_t; 193*0Sstevel@tonic-gate _xdr_result = xdr_int; 194*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_suspend_svc_1; 195*0Sstevel@tonic-gate break; 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gate case mdmn_comm_resume: 198*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_set_and_class_t; 199*0Sstevel@tonic-gate _xdr_result = xdr_int; 200*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_resume_svc_1; 201*0Sstevel@tonic-gate break; 202*0Sstevel@tonic-gate 203*0Sstevel@tonic-gate case mdmn_comm_reinit_set: 204*0Sstevel@tonic-gate _xdr_argument = xdr_u_int; 205*0Sstevel@tonic-gate _xdr_result = xdr_int; 206*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_reinit_set_svc_1; 207*0Sstevel@tonic-gate break; 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate case mdmn_comm_msglock: 210*0Sstevel@tonic-gate _xdr_argument = xdr_md_mn_type_and_lock_t; 211*0Sstevel@tonic-gate _xdr_result = xdr_int; 212*0Sstevel@tonic-gate local = (char *(*)()) mdmn_comm_msglock_svc_1; 213*0Sstevel@tonic-gate break; 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gate default: 216*0Sstevel@tonic-gate svcerr_noproc(transp); 217*0Sstevel@tonic-gate _rpcsvccount--; 218*0Sstevel@tonic-gate _rpcsvcstate = _SERVED; 219*0Sstevel@tonic-gate return; 220*0Sstevel@tonic-gate } 221*0Sstevel@tonic-gate (void) memset((char *)&argument, 0, sizeof (argument)); 222*0Sstevel@tonic-gate if (!svc_getargs(transp, _xdr_argument, (caddr_t)&argument)) { 223*0Sstevel@tonic-gate svcerr_decode(transp); 224*0Sstevel@tonic-gate _rpcsvccount--; 225*0Sstevel@tonic-gate _rpcsvcstate = _SERVED; 226*0Sstevel@tonic-gate return; 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate result = (*local)(&argument, rqstp); 229*0Sstevel@tonic-gate if (_xdr_result && result != NULL && 230*0Sstevel@tonic-gate !svc_sendreply(transp, _xdr_result, result)) { 231*0Sstevel@tonic-gate svcerr_systemerr(transp); 232*0Sstevel@tonic-gate } 233*0Sstevel@tonic-gate if (!svc_freeargs(transp, _xdr_argument, (caddr_t)&argument)) { 234*0Sstevel@tonic-gate _msgout(gettext("unable to free arguments")); 235*0Sstevel@tonic-gate exit(1); 236*0Sstevel@tonic-gate } 237*0Sstevel@tonic-gate 238*0Sstevel@tonic-gate if (free_result == 1) { 239*0Sstevel@tonic-gate free(result); 240*0Sstevel@tonic-gate } 241*0Sstevel@tonic-gate _rpcsvccount--; 242*0Sstevel@tonic-gate _rpcsvcstate = _SERVED; 243*0Sstevel@tonic-gate } 244*0Sstevel@tonic-gate 245*0Sstevel@tonic-gate /* 246*0Sstevel@tonic-gate * atexit handler to flag the lack of commd to the kernel so that we don't 247*0Sstevel@tonic-gate * panic due to RPC failures when the commd has been killed. 248*0Sstevel@tonic-gate */ 249*0Sstevel@tonic-gate static void 250*0Sstevel@tonic-gate exit_commd() 251*0Sstevel@tonic-gate { 252*0Sstevel@tonic-gate md_error_t ep = mdnullerror; 253*0Sstevel@tonic-gate (void) metaioctl(MD_MN_SET_COMMD_RUNNING, 0, &ep, "rpc.mdcommd"); 254*0Sstevel@tonic-gate } 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gate void 257*0Sstevel@tonic-gate main() 258*0Sstevel@tonic-gate { 259*0Sstevel@tonic-gate pid_t pid; 260*0Sstevel@tonic-gate int i; 261*0Sstevel@tonic-gate md_error_t ep = mdnullerror; 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gate (void) sigset(SIGPIPE, SIG_IGN); 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate /* 266*0Sstevel@tonic-gate * If stdin looks like a TLI endpoint, we assume 267*0Sstevel@tonic-gate * that we were started by a port monitor. If 268*0Sstevel@tonic-gate * t_getstate fails with TBADF, this is not a 269*0Sstevel@tonic-gate * TLI endpoint. 270*0Sstevel@tonic-gate */ 271*0Sstevel@tonic-gate if (t_getstate(0) != -1 || t_errno != TBADF) { 272*0Sstevel@tonic-gate char *netid; 273*0Sstevel@tonic-gate struct netconfig *nconf = NULL; 274*0Sstevel@tonic-gate SVCXPRT *transp; 275*0Sstevel@tonic-gate int pmclose; 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate #ifdef RPC_SVC_FG 278*0Sstevel@tonic-gate _rpcpmstart = 1; 279*0Sstevel@tonic-gate #endif /* RPC_SVC_FG */ 280*0Sstevel@tonic-gate openlog("mdmn_commd", LOG_PID, LOG_DAEMON); 281*0Sstevel@tonic-gate 282*0Sstevel@tonic-gate if ((netid = getenv("NLSPROVIDER")) == NULL) { 283*0Sstevel@tonic-gate /* started from inetd */ 284*0Sstevel@tonic-gate pmclose = 1; 285*0Sstevel@tonic-gate } else { 286*0Sstevel@tonic-gate if ((nconf = getnetconfigent(netid)) == NULL) 287*0Sstevel@tonic-gate _msgout(gettext("cannot get transport info")); 288*0Sstevel@tonic-gate 289*0Sstevel@tonic-gate pmclose = (t_getstate(0) != T_DATAXFER); 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate if ((transp = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) { 292*0Sstevel@tonic-gate _msgout(gettext("cannot create server handle")); 293*0Sstevel@tonic-gate exit(1); 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate if (nconf) 296*0Sstevel@tonic-gate freenetconfigent(nconf); 297*0Sstevel@tonic-gate if (!svc_reg(transp, MDMN_COMMD, ONE, mdmn_commd_1, 0)) { 298*0Sstevel@tonic-gate _msgout(gettext( 299*0Sstevel@tonic-gate "unable to register (MDMN_COMMD, ONE).")); 300*0Sstevel@tonic-gate exit(1); 301*0Sstevel@tonic-gate } 302*0Sstevel@tonic-gate 303*0Sstevel@tonic-gate atexit(exit_commd); 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate if (pmclose) { 306*0Sstevel@tonic-gate (void) signal(SIGALRM, (void(*)()) closedown); 307*0Sstevel@tonic-gate (void) alarm(_RPCSVC_CLOSEDOWN/2); 308*0Sstevel@tonic-gate } 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate (void) metaioctl(MD_MN_SET_COMMD_RUNNING, (void *)1, &ep, 311*0Sstevel@tonic-gate "rpc.mdcommd"); 312*0Sstevel@tonic-gate svc_run(); 313*0Sstevel@tonic-gate exit(1); 314*0Sstevel@tonic-gate /* NOTREACHED */ 315*0Sstevel@tonic-gate } else { 316*0Sstevel@tonic-gate #ifndef RPC_SVC_FG 317*0Sstevel@tonic-gate #pragma weak closefrom 318*0Sstevel@tonic-gate /* LINTED */ 319*0Sstevel@tonic-gate extern void closefrom(); 320*0Sstevel@tonic-gate int size; 321*0Sstevel@tonic-gate struct rlimit rl; 322*0Sstevel@tonic-gate pid = fork(); 323*0Sstevel@tonic-gate if (pid < 0) { 324*0Sstevel@tonic-gate perror(gettext("cannot fork")); 325*0Sstevel@tonic-gate exit(1); 326*0Sstevel@tonic-gate } 327*0Sstevel@tonic-gate if (pid) 328*0Sstevel@tonic-gate exit(0); 329*0Sstevel@tonic-gate if (closefrom != NULL) 330*0Sstevel@tonic-gate closefrom(0); 331*0Sstevel@tonic-gate else { 332*0Sstevel@tonic-gate rl.rlim_max = 0; 333*0Sstevel@tonic-gate getrlimit(RLIMIT_NOFILE, &rl); 334*0Sstevel@tonic-gate if ((size = rl.rlim_max) == 0) 335*0Sstevel@tonic-gate exit(1); 336*0Sstevel@tonic-gate for (i = 0; i < size; i++) 337*0Sstevel@tonic-gate (void) close(i); 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate i = open("/dev/null", 2); 340*0Sstevel@tonic-gate (void) dup2(i, 1); 341*0Sstevel@tonic-gate (void) dup2(i, 2); 342*0Sstevel@tonic-gate setsid(); 343*0Sstevel@tonic-gate openlog("mdmn_commd", LOG_PID, LOG_DAEMON); 344*0Sstevel@tonic-gate #endif 345*0Sstevel@tonic-gate } 346*0Sstevel@tonic-gate if (!svc_create(mdmn_commd_1, MDMN_COMMD, ONE, "tcp")) { 347*0Sstevel@tonic-gate _msgout(gettext("unable to create (MDMN_COMMD, ONE) for tcp.")); 348*0Sstevel@tonic-gate exit(1); 349*0Sstevel@tonic-gate } 350*0Sstevel@tonic-gate 351*0Sstevel@tonic-gate atexit(exit_commd); 352*0Sstevel@tonic-gate (void) metaioctl(MD_MN_SET_COMMD_RUNNING, (void *)1, &ep, 353*0Sstevel@tonic-gate "rpc.mdcommd"); 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate svc_run(); 356*0Sstevel@tonic-gate _msgout(gettext("svc_run returned")); 357*0Sstevel@tonic-gate exit(1); 358*0Sstevel@tonic-gate /* NOTREACHED */ 359*0Sstevel@tonic-gate } 360