1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate * 22*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 23*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 24*7c478bd9Sstevel@tonic-gate */ 25*7c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */ 26*7c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 27*7c478bd9Sstevel@tonic-gate /* 28*7c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988 29*7c478bd9Sstevel@tonic-gate * The Regents of the University of California 30*7c478bd9Sstevel@tonic-gate * All Rights Reserved 31*7c478bd9Sstevel@tonic-gate * 32*7c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from 33*7c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its 34*7c478bd9Sstevel@tonic-gate * contributors. 35*7c478bd9Sstevel@tonic-gate */ 36*7c478bd9Sstevel@tonic-gate 37*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 38*7c478bd9Sstevel@tonic-gate 39*7c478bd9Sstevel@tonic-gate /* 40*7c478bd9Sstevel@tonic-gate * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler 41*7c478bd9Sstevel@tonic-gate */ 42*7c478bd9Sstevel@tonic-gate #include <stdio.h> 43*7c478bd9Sstevel@tonic-gate #include <string.h> 44*7c478bd9Sstevel@tonic-gate #include "rpc_parse.h" 45*7c478bd9Sstevel@tonic-gate #include "rpc_util.h" 46*7c478bd9Sstevel@tonic-gate 47*7c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp"; 48*7c478bd9Sstevel@tonic-gate static char TRANSP[] = "transp"; 49*7c478bd9Sstevel@tonic-gate static char ARG[] = "argument"; 50*7c478bd9Sstevel@tonic-gate static char RESULT[] = "result"; 51*7c478bd9Sstevel@tonic-gate static char ROUTINE[] = "local"; 52*7c478bd9Sstevel@tonic-gate static char ONEWAY_ROUTINE[] = "oneway_local"; 53*7c478bd9Sstevel@tonic-gate static char RETVAL[] = "retval"; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate char _errbuf[256]; /* For all messages */ 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate void internal_proctype(); 58*7c478bd9Sstevel@tonic-gate 59*7c478bd9Sstevel@tonic-gate static 60*7c478bd9Sstevel@tonic-gate p_xdrfunc(rname, typename) 61*7c478bd9Sstevel@tonic-gate char *rname; 62*7c478bd9Sstevel@tonic-gate char *typename; 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate if (Cflag) 65*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", 66*7c478bd9Sstevel@tonic-gate rname, stringfix(typename)); 67*7c478bd9Sstevel@tonic-gate else 68*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_xdr_%s = xdr_%s;\n", 69*7c478bd9Sstevel@tonic-gate rname, stringfix(typename)); 70*7c478bd9Sstevel@tonic-gate } 71*7c478bd9Sstevel@tonic-gate 72*7c478bd9Sstevel@tonic-gate void 73*7c478bd9Sstevel@tonic-gate internal_proctype(plist) 74*7c478bd9Sstevel@tonic-gate proc_list *plist; 75*7c478bd9Sstevel@tonic-gate { 76*7c478bd9Sstevel@tonic-gate f_print(fout, "static "); 77*7c478bd9Sstevel@tonic-gate ptype(plist->res_prefix, plist->res_type, 1); 78*7c478bd9Sstevel@tonic-gate f_print(fout, "*"); 79*7c478bd9Sstevel@tonic-gate } 80*7c478bd9Sstevel@tonic-gate 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate static void 83*7c478bd9Sstevel@tonic-gate write_mtauto() 84*7c478bd9Sstevel@tonic-gate { 85*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n"); 86*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_msgout(\"%s\");\n", 87*7c478bd9Sstevel@tonic-gate "unable to set automatic MT mode."); 88*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n\t}\n"); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate 91*7c478bd9Sstevel@tonic-gate /* 92*7c478bd9Sstevel@tonic-gate * write most of the service, that is, everything but the registrations. 93*7c478bd9Sstevel@tonic-gate */ 94*7c478bd9Sstevel@tonic-gate void 95*7c478bd9Sstevel@tonic-gate write_most(infile, netflag, nomain) 96*7c478bd9Sstevel@tonic-gate char *infile; /* our name */ 97*7c478bd9Sstevel@tonic-gate int netflag; 98*7c478bd9Sstevel@tonic-gate int nomain; 99*7c478bd9Sstevel@tonic-gate { 100*7c478bd9Sstevel@tonic-gate if (inetdflag || pmflag) { 101*7c478bd9Sstevel@tonic-gate char *var_type; 102*7c478bd9Sstevel@tonic-gate var_type = (nomain? "extern" : "static"); 103*7c478bd9Sstevel@tonic-gate f_print(fout, "%s int _rpcpmstart;", var_type); 104*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t/* Started by a port monitor ? */\n"); 105*7c478bd9Sstevel@tonic-gate if (!tirpcflag) { 106*7c478bd9Sstevel@tonic-gate f_print(fout, "%s int _rpcfdtype;", var_type); 107*7c478bd9Sstevel@tonic-gate f_print(fout, 108*7c478bd9Sstevel@tonic-gate "\n\t\t /* Whether Stream or Datagram ? */\n"); 109*7c478bd9Sstevel@tonic-gate } 110*7c478bd9Sstevel@tonic-gate 111*7c478bd9Sstevel@tonic-gate if (timerflag) { 112*7c478bd9Sstevel@tonic-gate f_print(fout, 113*7c478bd9Sstevel@tonic-gate "\n/* States a server can be in wrt request */\n\n"); 114*7c478bd9Sstevel@tonic-gate f_print(fout, "#define\t_IDLE 0\n"); 115*7c478bd9Sstevel@tonic-gate f_print(fout, "#define\t_SERVED 1\n\n"); 116*7c478bd9Sstevel@tonic-gate f_print(fout, "static int _rpcsvcstate = _IDLE;"); 117*7c478bd9Sstevel@tonic-gate f_print(fout, 118*7c478bd9Sstevel@tonic-gate "\t/* Set when a request is serviced */\n"); 119*7c478bd9Sstevel@tonic-gate f_print(fout, "static int _rpcsvccount = 0;"); 120*7c478bd9Sstevel@tonic-gate f_print(fout, 121*7c478bd9Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n"); 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate if (mtflag) { 124*7c478bd9Sstevel@tonic-gate f_print(fout, "mutex_t _svcstate_lock;"); 125*7c478bd9Sstevel@tonic-gate f_print(fout, 126*7c478bd9Sstevel@tonic-gate "\t\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n"); 127*7c478bd9Sstevel@tonic-gate 128*7c478bd9Sstevel@tonic-gate } 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate write_svc_aux(nomain); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate /* write out dispatcher and stubs */ 134*7c478bd9Sstevel@tonic-gate write_programs(nomain? (char *)NULL : "static"); 135*7c478bd9Sstevel@tonic-gate 136*7c478bd9Sstevel@tonic-gate if (nomain) 137*7c478bd9Sstevel@tonic-gate return; 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate f_print(fout, "\nmain()\n"); 140*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 141*7c478bd9Sstevel@tonic-gate if (inetdflag) { 142*7c478bd9Sstevel@tonic-gate write_inetmost(infile); 143*7c478bd9Sstevel@tonic-gate /* Includes call to write_rpc_svc_fg() */ 144*7c478bd9Sstevel@tonic-gate } else { 145*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 146*7c478bd9Sstevel@tonic-gate if (netflag) { 147*7c478bd9Sstevel@tonic-gate f_print(fout, 148*7c478bd9Sstevel@tonic-gate "\tregister SVCXPRT *%s;\n", TRANSP); 149*7c478bd9Sstevel@tonic-gate f_print(fout, 150*7c478bd9Sstevel@tonic-gate "\tstruct netconfig *nconf = NULL;\n"); 151*7c478bd9Sstevel@tonic-gate } 152*7c478bd9Sstevel@tonic-gate f_print(fout, "\tpid_t pid;\n"); 153*7c478bd9Sstevel@tonic-gate f_print(fout, "\tint i;\n"); 154*7c478bd9Sstevel@tonic-gate if (mtauto) { 155*7c478bd9Sstevel@tonic-gate f_print(fout, 156*7c478bd9Sstevel@tonic-gate "\tint mode = RPC_SVC_MT_AUTO;\n\n"); 157*7c478bd9Sstevel@tonic-gate write_mtauto(); 158*7c478bd9Sstevel@tonic-gate } else 159*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 160*7c478bd9Sstevel@tonic-gate 161*7c478bd9Sstevel@tonic-gate if (mtflag & timerflag) 162*7c478bd9Sstevel@tonic-gate f_print(fout, 163*7c478bd9Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n"); 164*7c478bd9Sstevel@tonic-gate 165*7c478bd9Sstevel@tonic-gate write_pm_most(infile, netflag); 166*7c478bd9Sstevel@tonic-gate f_print(fout, "\telse {\n"); 167*7c478bd9Sstevel@tonic-gate write_rpc_svc_fg(infile, "\t\t"); 168*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 169*7c478bd9Sstevel@tonic-gate } else { 170*7c478bd9Sstevel@tonic-gate f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP); 171*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 172*7c478bd9Sstevel@tonic-gate print_pmapunset("\t"); 173*7c478bd9Sstevel@tonic-gate } 174*7c478bd9Sstevel@tonic-gate } 175*7c478bd9Sstevel@tonic-gate 176*7c478bd9Sstevel@tonic-gate if (logflag && !inetdflag) { 177*7c478bd9Sstevel@tonic-gate open_log_file(infile, "\t"); 178*7c478bd9Sstevel@tonic-gate } 179*7c478bd9Sstevel@tonic-gate } 180*7c478bd9Sstevel@tonic-gate 181*7c478bd9Sstevel@tonic-gate /* 182*7c478bd9Sstevel@tonic-gate * write a registration for the given transport 183*7c478bd9Sstevel@tonic-gate */ 184*7c478bd9Sstevel@tonic-gate void 185*7c478bd9Sstevel@tonic-gate write_netid_register(transp) 186*7c478bd9Sstevel@tonic-gate char *transp; 187*7c478bd9Sstevel@tonic-gate { 188*7c478bd9Sstevel@tonic-gate list *l; 189*7c478bd9Sstevel@tonic-gate definition *def; 190*7c478bd9Sstevel@tonic-gate version_list *vp; 191*7c478bd9Sstevel@tonic-gate char *sp; 192*7c478bd9Sstevel@tonic-gate char tmpbuf[32]; 193*7c478bd9Sstevel@tonic-gate 194*7c478bd9Sstevel@tonic-gate sp = ""; 195*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 196*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp); 197*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif (nconf == NULL) {\n", sp); 198*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "cannot find %s netid.", transp); 199*7c478bd9Sstevel@tonic-gate sprintf(tmpbuf, "%s\t\t", sp); 200*7c478bd9Sstevel@tonic-gate print_err_message(tmpbuf); 201*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 202*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t}\n", sp); 203*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n", 204*7c478bd9Sstevel@tonic-gate sp, TRANSP, transp); 205*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP); 206*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "cannot create %s service.", transp); 207*7c478bd9Sstevel@tonic-gate print_err_message(tmpbuf); 208*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 209*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t}\n", sp); 210*7c478bd9Sstevel@tonic-gate 211*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 212*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 213*7c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM) { 214*7c478bd9Sstevel@tonic-gate continue; 215*7c478bd9Sstevel@tonic-gate } 216*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 217*7c478bd9Sstevel@tonic-gate f_print(fout, 218*7c478bd9Sstevel@tonic-gate "%s\t(void) rpcb_unset(%s, %s, nconf);\n", 219*7c478bd9Sstevel@tonic-gate sp, def->def_name, vp->vers_name); 220*7c478bd9Sstevel@tonic-gate f_print(fout, 221*7c478bd9Sstevel@tonic-gate "%s\tif (!svc_reg(%s, %s, %s, ", 222*7c478bd9Sstevel@tonic-gate sp, TRANSP, def->def_name, vp->vers_name); 223*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 224*7c478bd9Sstevel@tonic-gate f_print(fout, ", nconf)) {\n"); 225*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, 226*7c478bd9Sstevel@tonic-gate "unable to register (%s, %s, %s).", 227*7c478bd9Sstevel@tonic-gate def->def_name, vp->vers_name, transp); 228*7c478bd9Sstevel@tonic-gate print_err_message(tmpbuf); 229*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 230*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t}\n", sp); 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp); 234*7c478bd9Sstevel@tonic-gate } 235*7c478bd9Sstevel@tonic-gate 236*7c478bd9Sstevel@tonic-gate /* 237*7c478bd9Sstevel@tonic-gate * write a registration for the given transport for TLI 238*7c478bd9Sstevel@tonic-gate */ 239*7c478bd9Sstevel@tonic-gate void 240*7c478bd9Sstevel@tonic-gate write_nettype_register(transp) 241*7c478bd9Sstevel@tonic-gate char *transp; 242*7c478bd9Sstevel@tonic-gate { 243*7c478bd9Sstevel@tonic-gate list *l; 244*7c478bd9Sstevel@tonic-gate definition *def; 245*7c478bd9Sstevel@tonic-gate version_list *vp; 246*7c478bd9Sstevel@tonic-gate 247*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 248*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 249*7c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM) { 250*7c478bd9Sstevel@tonic-gate continue; 251*7c478bd9Sstevel@tonic-gate } 252*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 253*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (!svc_create("); 254*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 255*7c478bd9Sstevel@tonic-gate f_print(fout, ", %s, %s, \"%s\")) {\n", 256*7c478bd9Sstevel@tonic-gate def->def_name, vp->vers_name, transp); 257*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, 258*7c478bd9Sstevel@tonic-gate "unable to create (%s, %s) for %s.", 259*7c478bd9Sstevel@tonic-gate def->def_name, vp->vers_name, transp); 260*7c478bd9Sstevel@tonic-gate print_err_message("\t\t"); 261*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n"); 262*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 263*7c478bd9Sstevel@tonic-gate } 264*7c478bd9Sstevel@tonic-gate } 265*7c478bd9Sstevel@tonic-gate } 266*7c478bd9Sstevel@tonic-gate 267*7c478bd9Sstevel@tonic-gate /* 268*7c478bd9Sstevel@tonic-gate * write the rest of the service 269*7c478bd9Sstevel@tonic-gate */ 270*7c478bd9Sstevel@tonic-gate void 271*7c478bd9Sstevel@tonic-gate write_rest() 272*7c478bd9Sstevel@tonic-gate { 273*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 274*7c478bd9Sstevel@tonic-gate if (inetdflag) { 275*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP); 276*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "could not create a handle"); 277*7c478bd9Sstevel@tonic-gate print_err_message("\t\t"); 278*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n"); 279*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 280*7c478bd9Sstevel@tonic-gate if (timerflag) { 281*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (_rpcpmstart) {\n"); 282*7c478bd9Sstevel@tonic-gate if (mtflag) { 283*7c478bd9Sstevel@tonic-gate f_print(fout, 284*7c478bd9Sstevel@tonic-gate "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n"); 285*7c478bd9Sstevel@tonic-gate f_print(fout, 286*7c478bd9Sstevel@tonic-gate "\t\t\t_msgout(\"cannot create closedown thread\");\n"); 287*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(1);\n"); 288*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 289*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 290*7c478bd9Sstevel@tonic-gate } else { 291*7c478bd9Sstevel@tonic-gate f_print(fout, 292*7c478bd9Sstevel@tonic-gate "\t\t(void) signal(SIGALRM, %s closedown);\n", 293*7c478bd9Sstevel@tonic-gate Cflag? "(SIG_PF)":"(void(*)())"); 294*7c478bd9Sstevel@tonic-gate f_print(fout, 295*7c478bd9Sstevel@tonic-gate "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n"); 296*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 297*7c478bd9Sstevel@tonic-gate } 298*7c478bd9Sstevel@tonic-gate } 299*7c478bd9Sstevel@tonic-gate } 300*7c478bd9Sstevel@tonic-gate f_print(fout, "\tsvc_run();\n"); 301*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "svc_run returned"); 302*7c478bd9Sstevel@tonic-gate print_err_message("\t"); 303*7c478bd9Sstevel@tonic-gate f_print(fout, "\texit(1);\n"); 304*7c478bd9Sstevel@tonic-gate f_print(fout, "\t/* NOTREACHED */\n"); 305*7c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 306*7c478bd9Sstevel@tonic-gate } 307*7c478bd9Sstevel@tonic-gate 308*7c478bd9Sstevel@tonic-gate void 309*7c478bd9Sstevel@tonic-gate write_programs(storage) 310*7c478bd9Sstevel@tonic-gate char *storage; 311*7c478bd9Sstevel@tonic-gate { 312*7c478bd9Sstevel@tonic-gate list *l; 313*7c478bd9Sstevel@tonic-gate definition *def; 314*7c478bd9Sstevel@tonic-gate 315*7c478bd9Sstevel@tonic-gate /* write out stubs for procedure definitions */ 316*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 317*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 318*7c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_PROGRAM) { 319*7c478bd9Sstevel@tonic-gate write_real_program(def); 320*7c478bd9Sstevel@tonic-gate } 321*7c478bd9Sstevel@tonic-gate } 322*7c478bd9Sstevel@tonic-gate 323*7c478bd9Sstevel@tonic-gate /* write out dispatcher for each program */ 324*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 325*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 326*7c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_PROGRAM) { 327*7c478bd9Sstevel@tonic-gate write_program(def, storage); 328*7c478bd9Sstevel@tonic-gate } 329*7c478bd9Sstevel@tonic-gate } 330*7c478bd9Sstevel@tonic-gate 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate } 333*7c478bd9Sstevel@tonic-gate 334*7c478bd9Sstevel@tonic-gate /* 335*7c478bd9Sstevel@tonic-gate * write out definition of internal function (e.g. _printmsg_1(...)) 336*7c478bd9Sstevel@tonic-gate * which calls server's defintion of actual function (e.g. printmsg_1(...)). 337*7c478bd9Sstevel@tonic-gate * Unpacks single user argument of printmsg_1 to call-by-value format 338*7c478bd9Sstevel@tonic-gate * expected by printmsg_1. 339*7c478bd9Sstevel@tonic-gate */ 340*7c478bd9Sstevel@tonic-gate static 341*7c478bd9Sstevel@tonic-gate write_real_program(def) 342*7c478bd9Sstevel@tonic-gate definition *def; 343*7c478bd9Sstevel@tonic-gate { 344*7c478bd9Sstevel@tonic-gate version_list *vp; 345*7c478bd9Sstevel@tonic-gate proc_list *proc; 346*7c478bd9Sstevel@tonic-gate decl_list *l; 347*7c478bd9Sstevel@tonic-gate 348*7c478bd9Sstevel@tonic-gate if (!newstyle) 349*7c478bd9Sstevel@tonic-gate return; /* not needed for old style */ 350*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 351*7c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) { 352*7c478bd9Sstevel@tonic-gate int oneway = streq(proc->res_type, "oneway"); 353*7c478bd9Sstevel@tonic-gate 354*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 355*7c478bd9Sstevel@tonic-gate if (!mtflag) 356*7c478bd9Sstevel@tonic-gate internal_proctype(proc); 357*7c478bd9Sstevel@tonic-gate else 358*7c478bd9Sstevel@tonic-gate f_print(fout, "int"); 359*7c478bd9Sstevel@tonic-gate f_print(fout, "\n_"); 360*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 361*7c478bd9Sstevel@tonic-gate if (Cflag) { 362*7c478bd9Sstevel@tonic-gate f_print(fout, "("); 363*7c478bd9Sstevel@tonic-gate /* arg name */ 364*7c478bd9Sstevel@tonic-gate if (proc->arg_num > 1) 365*7c478bd9Sstevel@tonic-gate f_print(fout, proc->args.argname); 366*7c478bd9Sstevel@tonic-gate else 367*7c478bd9Sstevel@tonic-gate ptype(proc->args.decls->decl.prefix, 368*7c478bd9Sstevel@tonic-gate proc->args.decls->decl.type, 0); 369*7c478bd9Sstevel@tonic-gate if (mtflag) { 370*7c478bd9Sstevel@tonic-gate f_print(fout, " *argp, "); 371*7c478bd9Sstevel@tonic-gate ptype(proc->res_prefix, 372*7c478bd9Sstevel@tonic-gate proc->res_type, 1); 373*7c478bd9Sstevel@tonic-gate f_print(fout, 374*7c478bd9Sstevel@tonic-gate "*%s, struct svc_req *%s)\n", 375*7c478bd9Sstevel@tonic-gate RESULT, RQSTP); 376*7c478bd9Sstevel@tonic-gate } 377*7c478bd9Sstevel@tonic-gate else 378*7c478bd9Sstevel@tonic-gate f_print(fout, 379*7c478bd9Sstevel@tonic-gate " *argp, struct svc_req *%s)\n", 380*7c478bd9Sstevel@tonic-gate RQSTP); 381*7c478bd9Sstevel@tonic-gate 382*7c478bd9Sstevel@tonic-gate } else { 383*7c478bd9Sstevel@tonic-gate if (mtflag) 384*7c478bd9Sstevel@tonic-gate f_print(fout, "(argp, %s, %s)\n", 385*7c478bd9Sstevel@tonic-gate RESULT, RQSTP); 386*7c478bd9Sstevel@tonic-gate else 387*7c478bd9Sstevel@tonic-gate f_print(fout, "(argp, %s)\n", RQSTP); 388*7c478bd9Sstevel@tonic-gate /* arg name */ 389*7c478bd9Sstevel@tonic-gate if (proc->arg_num > 1) 390*7c478bd9Sstevel@tonic-gate f_print(fout, "\t%s *argp;\n", 391*7c478bd9Sstevel@tonic-gate proc->args.argname); 392*7c478bd9Sstevel@tonic-gate else { 393*7c478bd9Sstevel@tonic-gate f_print(fout, "\t"); 394*7c478bd9Sstevel@tonic-gate ptype(proc->args.decls->decl.prefix, 395*7c478bd9Sstevel@tonic-gate proc->args.decls->decl.type, 0); 396*7c478bd9Sstevel@tonic-gate f_print(fout, " *argp;\n"); 397*7c478bd9Sstevel@tonic-gate } 398*7c478bd9Sstevel@tonic-gate if (mtflag) 399*7c478bd9Sstevel@tonic-gate f_print(fout, "\tvoid *%s;\n", RESULT); 400*7c478bd9Sstevel@tonic-gate f_print(fout, "\tstruct svc_req *%s;\n", RQSTP); 401*7c478bd9Sstevel@tonic-gate } 402*7c478bd9Sstevel@tonic-gate 403*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 404*7c478bd9Sstevel@tonic-gate f_print(fout, "\treturn ("); 405*7c478bd9Sstevel@tonic-gate /* for mtflag, arguments are different */ 406*7c478bd9Sstevel@tonic-gate if (Cflag || mtflag) 407*7c478bd9Sstevel@tonic-gate pvname_svc(proc->proc_name, vp->vers_num); 408*7c478bd9Sstevel@tonic-gate else 409*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 410*7c478bd9Sstevel@tonic-gate f_print(fout, "("); 411*7c478bd9Sstevel@tonic-gate if (proc->arg_num < 2) { /* single argument */ 412*7c478bd9Sstevel@tonic-gate if (!streq(proc->args.decls->decl.type, "void")) 413*7c478bd9Sstevel@tonic-gate f_print(fout, "*argp, "); /* non-void */ 414*7c478bd9Sstevel@tonic-gate } else { 415*7c478bd9Sstevel@tonic-gate for (l = proc->args.decls; l != NULL; 416*7c478bd9Sstevel@tonic-gate l = l->next) 417*7c478bd9Sstevel@tonic-gate f_print(fout, "argp->%s, ", 418*7c478bd9Sstevel@tonic-gate l->decl.name); 419*7c478bd9Sstevel@tonic-gate } 420*7c478bd9Sstevel@tonic-gate if (mtflag && !oneway) 421*7c478bd9Sstevel@tonic-gate f_print(fout, "%s, ", RESULT); 422*7c478bd9Sstevel@tonic-gate f_print(fout, "%s));\n}\n", RQSTP); 423*7c478bd9Sstevel@tonic-gate } 424*7c478bd9Sstevel@tonic-gate } 425*7c478bd9Sstevel@tonic-gate } 426*7c478bd9Sstevel@tonic-gate 427*7c478bd9Sstevel@tonic-gate static 428*7c478bd9Sstevel@tonic-gate write_program(def, storage) 429*7c478bd9Sstevel@tonic-gate definition *def; 430*7c478bd9Sstevel@tonic-gate char *storage; 431*7c478bd9Sstevel@tonic-gate { 432*7c478bd9Sstevel@tonic-gate version_list *vp; 433*7c478bd9Sstevel@tonic-gate proc_list *proc; 434*7c478bd9Sstevel@tonic-gate int filled; 435*7c478bd9Sstevel@tonic-gate 436*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 437*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 438*7c478bd9Sstevel@tonic-gate if (storage != NULL) { 439*7c478bd9Sstevel@tonic-gate f_print(fout, "%s ", storage); 440*7c478bd9Sstevel@tonic-gate } 441*7c478bd9Sstevel@tonic-gate f_print(fout, "void\n"); 442*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 443*7c478bd9Sstevel@tonic-gate 444*7c478bd9Sstevel@tonic-gate if (Cflag) { 445*7c478bd9Sstevel@tonic-gate f_print(fout, "(struct svc_req *%s, ", RQSTP); 446*7c478bd9Sstevel@tonic-gate f_print(fout, "register SVCXPRT *%s)\n", TRANSP); 447*7c478bd9Sstevel@tonic-gate } else { 448*7c478bd9Sstevel@tonic-gate f_print(fout, "(%s, %s)\n", RQSTP, TRANSP); 449*7c478bd9Sstevel@tonic-gate f_print(fout, " struct svc_req *%s;\n", RQSTP); 450*7c478bd9Sstevel@tonic-gate f_print(fout, " register SVCXPRT *%s;\n", TRANSP); 451*7c478bd9Sstevel@tonic-gate } 452*7c478bd9Sstevel@tonic-gate 453*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 454*7c478bd9Sstevel@tonic-gate 455*7c478bd9Sstevel@tonic-gate filled = 0; 456*7c478bd9Sstevel@tonic-gate f_print(fout, "\tunion {\n"); 457*7c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) { 458*7c478bd9Sstevel@tonic-gate if (proc->arg_num < 2) { /* single argument */ 459*7c478bd9Sstevel@tonic-gate if (streq(proc->args.decls->decl.type, 460*7c478bd9Sstevel@tonic-gate "void")) { 461*7c478bd9Sstevel@tonic-gate continue; 462*7c478bd9Sstevel@tonic-gate } 463*7c478bd9Sstevel@tonic-gate filled = 1; 464*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t"); 465*7c478bd9Sstevel@tonic-gate ptype(proc->args.decls->decl.prefix, 466*7c478bd9Sstevel@tonic-gate proc->args.decls->decl.type, 0); 467*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 468*7c478bd9Sstevel@tonic-gate f_print(fout, "_arg;\n"); 469*7c478bd9Sstevel@tonic-gate 470*7c478bd9Sstevel@tonic-gate } else { 471*7c478bd9Sstevel@tonic-gate filled = 1; 472*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t%s", proc->args.argname); 473*7c478bd9Sstevel@tonic-gate f_print(fout, " "); 474*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 475*7c478bd9Sstevel@tonic-gate f_print(fout, "_arg;\n"); 476*7c478bd9Sstevel@tonic-gate } 477*7c478bd9Sstevel@tonic-gate } 478*7c478bd9Sstevel@tonic-gate if (!filled) { 479*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint fill;\n"); 480*7c478bd9Sstevel@tonic-gate } 481*7c478bd9Sstevel@tonic-gate f_print(fout, "\t} %s;\n", ARG); 482*7c478bd9Sstevel@tonic-gate 483*7c478bd9Sstevel@tonic-gate if (mtflag) { 484*7c478bd9Sstevel@tonic-gate filled = 0; 485*7c478bd9Sstevel@tonic-gate f_print(fout, "\tunion {\n"); 486*7c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; 487*7c478bd9Sstevel@tonic-gate proc = proc->next) { 488*7c478bd9Sstevel@tonic-gate if (streq(proc->res_type, "void") || 489*7c478bd9Sstevel@tonic-gate streq(proc->res_type, "oneway")) 490*7c478bd9Sstevel@tonic-gate continue; 491*7c478bd9Sstevel@tonic-gate filled = 1; 492*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t"); 493*7c478bd9Sstevel@tonic-gate ptype(proc->res_prefix, proc->res_type, 0); 494*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 495*7c478bd9Sstevel@tonic-gate f_print(fout, "_res;\n"); 496*7c478bd9Sstevel@tonic-gate } 497*7c478bd9Sstevel@tonic-gate if (!filled) 498*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint fill;\n"); 499*7c478bd9Sstevel@tonic-gate f_print(fout, "\t} %s;\n", RESULT); 500*7c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t %s;\n", RETVAL); 501*7c478bd9Sstevel@tonic-gate 502*7c478bd9Sstevel@tonic-gate } else 503*7c478bd9Sstevel@tonic-gate f_print(fout, "\tchar *%s;\n", RESULT); 504*7c478bd9Sstevel@tonic-gate 505*7c478bd9Sstevel@tonic-gate if (Cflag) { 506*7c478bd9Sstevel@tonic-gate f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", 507*7c478bd9Sstevel@tonic-gate ARG, RESULT); 508*7c478bd9Sstevel@tonic-gate if (mtflag) 509*7c478bd9Sstevel@tonic-gate f_print(fout, 510*7c478bd9Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n", 511*7c478bd9Sstevel@tonic-gate ROUTINE); 512*7c478bd9Sstevel@tonic-gate else 513*7c478bd9Sstevel@tonic-gate f_print(fout, 514*7c478bd9Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n", 515*7c478bd9Sstevel@tonic-gate ROUTINE); 516*7c478bd9Sstevel@tonic-gate } else { 517*7c478bd9Sstevel@tonic-gate f_print(fout, 518*7c478bd9Sstevel@tonic-gate "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", 519*7c478bd9Sstevel@tonic-gate ARG, RESULT); 520*7c478bd9Sstevel@tonic-gate if (mtflag) 521*7c478bd9Sstevel@tonic-gate f_print(fout, "\tbool_t (*%s)();\n", ROUTINE); 522*7c478bd9Sstevel@tonic-gate else 523*7c478bd9Sstevel@tonic-gate f_print(fout, "\tchar *(*%s)();\n", ROUTINE); 524*7c478bd9Sstevel@tonic-gate } 525*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 526*7c478bd9Sstevel@tonic-gate 527*7c478bd9Sstevel@tonic-gate if (timerflag) { 528*7c478bd9Sstevel@tonic-gate if (mtflag) 529*7c478bd9Sstevel@tonic-gate f_print(fout, 530*7c478bd9Sstevel@tonic-gate "\tmutex_lock(&_svcstate_lock);\n"); 531*7c478bd9Sstevel@tonic-gate 532*7c478bd9Sstevel@tonic-gate f_print(fout, "\t_rpcsvccount++;\n"); 533*7c478bd9Sstevel@tonic-gate if (mtflag) 534*7c478bd9Sstevel@tonic-gate f_print(fout, 535*7c478bd9Sstevel@tonic-gate "\tmutex_unlock(&_svcstate_lock);\n"); 536*7c478bd9Sstevel@tonic-gate } 537*7c478bd9Sstevel@tonic-gate 538*7c478bd9Sstevel@tonic-gate f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP); 539*7c478bd9Sstevel@tonic-gate if (!nullproc(vp->procs)) { 540*7c478bd9Sstevel@tonic-gate f_print(fout, "\tcase NULLPROC:\n"); 541*7c478bd9Sstevel@tonic-gate f_print(fout, 542*7c478bd9Sstevel@tonic-gate Cflag ? 543*7c478bd9Sstevel@tonic-gate "\t\t(void) svc_sendreply(%s,\n\t\t\t(xdrproc_t) xdr_void, (char *)NULL);\n" : 544*7c478bd9Sstevel@tonic-gate "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\t(char *)NULL);\n", 545*7c478bd9Sstevel@tonic-gate TRANSP); 546*7c478bd9Sstevel@tonic-gate print_return("\t\t"); 547*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 548*7c478bd9Sstevel@tonic-gate } 549*7c478bd9Sstevel@tonic-gate for (proc = vp->procs; proc != NULL; proc = proc->next) { 550*7c478bd9Sstevel@tonic-gate f_print(fout, "\tcase %s:\n", proc->proc_name); 551*7c478bd9Sstevel@tonic-gate if (proc->arg_num < 2) { /* single argument */ 552*7c478bd9Sstevel@tonic-gate p_xdrfunc(ARG, proc->args.decls->decl.type); 553*7c478bd9Sstevel@tonic-gate } else { 554*7c478bd9Sstevel@tonic-gate p_xdrfunc(ARG, proc->args.argname); 555*7c478bd9Sstevel@tonic-gate } 556*7c478bd9Sstevel@tonic-gate 557*7c478bd9Sstevel@tonic-gate if (streq(proc->res_type, "oneway")) { 558*7c478bd9Sstevel@tonic-gate /* One-way call */ 559*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT); 560*7c478bd9Sstevel@tonic-gate } else { 561*7c478bd9Sstevel@tonic-gate p_xdrfunc(RESULT, proc->res_type); 562*7c478bd9Sstevel@tonic-gate } 563*7c478bd9Sstevel@tonic-gate if (Cflag) { 564*7c478bd9Sstevel@tonic-gate if (mtflag) { 565*7c478bd9Sstevel@tonic-gate f_print(fout, 566*7c478bd9Sstevel@tonic-gate "\t\t%s = (bool_t (*) (char *, " 567*7c478bd9Sstevel@tonic-gate "void *, struct svc_req *))", 568*7c478bd9Sstevel@tonic-gate ROUTINE); 569*7c478bd9Sstevel@tonic-gate } else { 570*7c478bd9Sstevel@tonic-gate f_print(fout, 571*7c478bd9Sstevel@tonic-gate "\t\t%s = (char *(*)(char *, " 572*7c478bd9Sstevel@tonic-gate "struct svc_req *)) ", 573*7c478bd9Sstevel@tonic-gate ROUTINE); 574*7c478bd9Sstevel@tonic-gate } 575*7c478bd9Sstevel@tonic-gate } else { 576*7c478bd9Sstevel@tonic-gate if (mtflag) { 577*7c478bd9Sstevel@tonic-gate f_print(fout, 578*7c478bd9Sstevel@tonic-gate "\t\t%s = (bool_t (*)()) ", 579*7c478bd9Sstevel@tonic-gate ROUTINE); 580*7c478bd9Sstevel@tonic-gate } else { 581*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t%s = (char *(*)()) ", 582*7c478bd9Sstevel@tonic-gate ROUTINE); 583*7c478bd9Sstevel@tonic-gate } 584*7c478bd9Sstevel@tonic-gate } 585*7c478bd9Sstevel@tonic-gate 586*7c478bd9Sstevel@tonic-gate if (newstyle) { /* new style: calls internal routine */ 587*7c478bd9Sstevel@tonic-gate f_print(fout, "_"); 588*7c478bd9Sstevel@tonic-gate } 589*7c478bd9Sstevel@tonic-gate if ((Cflag || mtflag) && !newstyle) 590*7c478bd9Sstevel@tonic-gate pvname_svc(proc->proc_name, vp->vers_num); 591*7c478bd9Sstevel@tonic-gate else 592*7c478bd9Sstevel@tonic-gate pvname(proc->proc_name, vp->vers_num); 593*7c478bd9Sstevel@tonic-gate f_print(fout, ";\n"); 594*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tbreak;\n\n"); 595*7c478bd9Sstevel@tonic-gate } 596*7c478bd9Sstevel@tonic-gate f_print(fout, "\tdefault:\n"); 597*7c478bd9Sstevel@tonic-gate printerr("noproc", TRANSP); 598*7c478bd9Sstevel@tonic-gate print_return("\t\t"); 599*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 600*7c478bd9Sstevel@tonic-gate 601*7c478bd9Sstevel@tonic-gate f_print(fout, 602*7c478bd9Sstevel@tonic-gate "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", 603*7c478bd9Sstevel@tonic-gate ARG, ARG); 604*7c478bd9Sstevel@tonic-gate printif("getargs", TRANSP, "(caddr_t) &", ARG); 605*7c478bd9Sstevel@tonic-gate printerr("decode", TRANSP); 606*7c478bd9Sstevel@tonic-gate print_return("\t\t"); 607*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 608*7c478bd9Sstevel@tonic-gate 609*7c478bd9Sstevel@tonic-gate if (!mtflag) 610*7c478bd9Sstevel@tonic-gate if (Cflag) 611*7c478bd9Sstevel@tonic-gate f_print(fout, 612*7c478bd9Sstevel@tonic-gate "\t%s = (*%s)((char *)&%s, %s);\n", 613*7c478bd9Sstevel@tonic-gate RESULT, ROUTINE, ARG, RQSTP); 614*7c478bd9Sstevel@tonic-gate else 615*7c478bd9Sstevel@tonic-gate f_print(fout, "\t%s = (*%s)(&%s, %s);\n", 616*7c478bd9Sstevel@tonic-gate RESULT, ROUTINE, ARG, RQSTP); 617*7c478bd9Sstevel@tonic-gate else 618*7c478bd9Sstevel@tonic-gate if (Cflag) 619*7c478bd9Sstevel@tonic-gate f_print(fout, 620*7c478bd9Sstevel@tonic-gate "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n", 621*7c478bd9Sstevel@tonic-gate RETVAL, ROUTINE, ARG, RESULT, RQSTP); 622*7c478bd9Sstevel@tonic-gate else 623*7c478bd9Sstevel@tonic-gate f_print(fout, 624*7c478bd9Sstevel@tonic-gate "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n", 625*7c478bd9Sstevel@tonic-gate RETVAL, ROUTINE, ARG, RESULT, RQSTP); 626*7c478bd9Sstevel@tonic-gate 627*7c478bd9Sstevel@tonic-gate 628*7c478bd9Sstevel@tonic-gate 629*7c478bd9Sstevel@tonic-gate 630*7c478bd9Sstevel@tonic-gate if (mtflag) 631*7c478bd9Sstevel@tonic-gate f_print(fout, 632*7c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n", 633*7c478bd9Sstevel@tonic-gate RESULT, RETVAL, TRANSP, RESULT, RESULT); 634*7c478bd9Sstevel@tonic-gate else 635*7c478bd9Sstevel@tonic-gate f_print(fout, 636*7c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n", 637*7c478bd9Sstevel@tonic-gate RESULT, RESULT, TRANSP, RESULT, RESULT); 638*7c478bd9Sstevel@tonic-gate 639*7c478bd9Sstevel@tonic-gate printerr("systemerr", TRANSP); 640*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 641*7c478bd9Sstevel@tonic-gate 642*7c478bd9Sstevel@tonic-gate printif("freeargs", TRANSP, "(caddr_t) &", ARG); 643*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "unable to free arguments"); 644*7c478bd9Sstevel@tonic-gate print_err_message("\t\t"); 645*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n"); 646*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 647*7c478bd9Sstevel@tonic-gate /* print out free routine */ 648*7c478bd9Sstevel@tonic-gate if (mtflag) { 649*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT); 650*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (!"); 651*7c478bd9Sstevel@tonic-gate 652*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 653*7c478bd9Sstevel@tonic-gate f_print(fout, 654*7c478bd9Sstevel@tonic-gate "_freeresult(%s, _xdr_%s, (caddr_t) &%s))\n", 655*7c478bd9Sstevel@tonic-gate TRANSP, RESULT, RESULT); 656*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "unable to free results"); 657*7c478bd9Sstevel@tonic-gate print_err_message("\t\t\t"); 658*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 659*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 660*7c478bd9Sstevel@tonic-gate }; 661*7c478bd9Sstevel@tonic-gate print_return("\t"); 662*7c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 663*7c478bd9Sstevel@tonic-gate } 664*7c478bd9Sstevel@tonic-gate } 665*7c478bd9Sstevel@tonic-gate 666*7c478bd9Sstevel@tonic-gate static 667*7c478bd9Sstevel@tonic-gate printerr(err, transp) 668*7c478bd9Sstevel@tonic-gate char *err; 669*7c478bd9Sstevel@tonic-gate char *transp; 670*7c478bd9Sstevel@tonic-gate { 671*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp); 672*7c478bd9Sstevel@tonic-gate } 673*7c478bd9Sstevel@tonic-gate 674*7c478bd9Sstevel@tonic-gate static 675*7c478bd9Sstevel@tonic-gate printif(proc, transp, prefix, arg) 676*7c478bd9Sstevel@tonic-gate char *proc; 677*7c478bd9Sstevel@tonic-gate char *transp; 678*7c478bd9Sstevel@tonic-gate char *prefix; 679*7c478bd9Sstevel@tonic-gate char *arg; 680*7c478bd9Sstevel@tonic-gate { 681*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n", 682*7c478bd9Sstevel@tonic-gate proc, transp, arg, prefix, arg); 683*7c478bd9Sstevel@tonic-gate } 684*7c478bd9Sstevel@tonic-gate 685*7c478bd9Sstevel@tonic-gate nullproc(proc) 686*7c478bd9Sstevel@tonic-gate proc_list *proc; 687*7c478bd9Sstevel@tonic-gate { 688*7c478bd9Sstevel@tonic-gate for (; proc != NULL; proc = proc->next) { 689*7c478bd9Sstevel@tonic-gate if (streq(proc->proc_num, "0")) { 690*7c478bd9Sstevel@tonic-gate return (1); 691*7c478bd9Sstevel@tonic-gate } 692*7c478bd9Sstevel@tonic-gate } 693*7c478bd9Sstevel@tonic-gate return (0); 694*7c478bd9Sstevel@tonic-gate } 695*7c478bd9Sstevel@tonic-gate 696*7c478bd9Sstevel@tonic-gate static 697*7c478bd9Sstevel@tonic-gate write_inetmost(infile) 698*7c478bd9Sstevel@tonic-gate char *infile; 699*7c478bd9Sstevel@tonic-gate { 700*7c478bd9Sstevel@tonic-gate f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP); 701*7c478bd9Sstevel@tonic-gate f_print(fout, "\tint sock;\n"); 702*7c478bd9Sstevel@tonic-gate f_print(fout, "\tint proto;\n"); 703*7c478bd9Sstevel@tonic-gate f_print(fout, "\tstruct sockaddr_in saddr;\n"); 704*7c478bd9Sstevel@tonic-gate f_print(fout, "\tint asize = sizeof (saddr);\n"); 705*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 706*7c478bd9Sstevel@tonic-gate f_print(fout, 707*7c478bd9Sstevel@tonic-gate "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n"); 708*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint ssize = sizeof (int);\n\n"); 709*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n"); 710*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(1);\n"); 711*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n"); 712*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n"); 713*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(1);\n"); 714*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsock = 0;\n"); 715*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_rpcpmstart = 1;\n"); 716*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tproto = 0;\n"); 717*7c478bd9Sstevel@tonic-gate open_log_file(infile, "\t\t"); 718*7c478bd9Sstevel@tonic-gate f_print(fout, "\t} else {\n"); 719*7c478bd9Sstevel@tonic-gate write_rpc_svc_fg(infile, "\t\t"); 720*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsock = RPC_ANYSOCK;\n"); 721*7c478bd9Sstevel@tonic-gate print_pmapunset("\t\t"); 722*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 723*7c478bd9Sstevel@tonic-gate } 724*7c478bd9Sstevel@tonic-gate 725*7c478bd9Sstevel@tonic-gate static 726*7c478bd9Sstevel@tonic-gate print_return(space) 727*7c478bd9Sstevel@tonic-gate char *space; 728*7c478bd9Sstevel@tonic-gate { 729*7c478bd9Sstevel@tonic-gate if (exitnow) 730*7c478bd9Sstevel@tonic-gate f_print(fout, "%sexit(0);\n", space); 731*7c478bd9Sstevel@tonic-gate else { 732*7c478bd9Sstevel@tonic-gate if (timerflag) { 733*7c478bd9Sstevel@tonic-gate if (mtflag) 734*7c478bd9Sstevel@tonic-gate f_print(fout, 735*7c478bd9Sstevel@tonic-gate "%smutex_lock(&_svcstate_lock);\n", 736*7c478bd9Sstevel@tonic-gate space); 737*7c478bd9Sstevel@tonic-gate f_print(fout, "%s_rpcsvccount--;\n", space); 738*7c478bd9Sstevel@tonic-gate f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space); 739*7c478bd9Sstevel@tonic-gate if (mtflag) 740*7c478bd9Sstevel@tonic-gate f_print(fout, 741*7c478bd9Sstevel@tonic-gate "%smutex_unlock(&_svcstate_lock);\n", 742*7c478bd9Sstevel@tonic-gate space); 743*7c478bd9Sstevel@tonic-gate } 744*7c478bd9Sstevel@tonic-gate f_print(fout, "%sreturn;\n", space); 745*7c478bd9Sstevel@tonic-gate } 746*7c478bd9Sstevel@tonic-gate } 747*7c478bd9Sstevel@tonic-gate 748*7c478bd9Sstevel@tonic-gate static 749*7c478bd9Sstevel@tonic-gate print_pmapunset(space) 750*7c478bd9Sstevel@tonic-gate char *space; 751*7c478bd9Sstevel@tonic-gate { 752*7c478bd9Sstevel@tonic-gate list *l; 753*7c478bd9Sstevel@tonic-gate definition *def; 754*7c478bd9Sstevel@tonic-gate version_list *vp; 755*7c478bd9Sstevel@tonic-gate 756*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 757*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 758*7c478bd9Sstevel@tonic-gate if (def->def_kind == DEF_PROGRAM) { 759*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; 760*7c478bd9Sstevel@tonic-gate vp = vp->next) { 761*7c478bd9Sstevel@tonic-gate f_print(fout, "%s(void) pmap_unset(%s, %s);\n", 762*7c478bd9Sstevel@tonic-gate space, def->def_name, vp->vers_name); 763*7c478bd9Sstevel@tonic-gate } 764*7c478bd9Sstevel@tonic-gate } 765*7c478bd9Sstevel@tonic-gate } 766*7c478bd9Sstevel@tonic-gate } 767*7c478bd9Sstevel@tonic-gate 768*7c478bd9Sstevel@tonic-gate static 769*7c478bd9Sstevel@tonic-gate print_err_message(space) 770*7c478bd9Sstevel@tonic-gate char *space; 771*7c478bd9Sstevel@tonic-gate { 772*7c478bd9Sstevel@tonic-gate if (logflag) 773*7c478bd9Sstevel@tonic-gate f_print(fout, "%ssyslog(LOG_ERR, \"%%s\", \"%s\");\n", space, 774*7c478bd9Sstevel@tonic-gate _errbuf); 775*7c478bd9Sstevel@tonic-gate else if (inetdflag || pmflag) 776*7c478bd9Sstevel@tonic-gate f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf); 777*7c478bd9Sstevel@tonic-gate else 778*7c478bd9Sstevel@tonic-gate f_print(fout, "%sfprintf(stderr, \"%%s\", \"%s\");\n", space, 779*7c478bd9Sstevel@tonic-gate _errbuf); 780*7c478bd9Sstevel@tonic-gate } 781*7c478bd9Sstevel@tonic-gate 782*7c478bd9Sstevel@tonic-gate /* 783*7c478bd9Sstevel@tonic-gate * Write the server auxiliary function (_msgout, timeout) 784*7c478bd9Sstevel@tonic-gate */ 785*7c478bd9Sstevel@tonic-gate void 786*7c478bd9Sstevel@tonic-gate write_svc_aux(nomain) 787*7c478bd9Sstevel@tonic-gate int nomain; 788*7c478bd9Sstevel@tonic-gate { 789*7c478bd9Sstevel@tonic-gate if (!logflag) 790*7c478bd9Sstevel@tonic-gate write_msg_out(); 791*7c478bd9Sstevel@tonic-gate if (!nomain) 792*7c478bd9Sstevel@tonic-gate write_timeout_func(); 793*7c478bd9Sstevel@tonic-gate } 794*7c478bd9Sstevel@tonic-gate 795*7c478bd9Sstevel@tonic-gate /* 796*7c478bd9Sstevel@tonic-gate * Write the _msgout function 797*7c478bd9Sstevel@tonic-gate */ 798*7c478bd9Sstevel@tonic-gate 799*7c478bd9Sstevel@tonic-gate write_msg_out() 800*7c478bd9Sstevel@tonic-gate { 801*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 802*7c478bd9Sstevel@tonic-gate f_print(fout, "static\n"); 803*7c478bd9Sstevel@tonic-gate if (!Cflag) { 804*7c478bd9Sstevel@tonic-gate f_print(fout, "void _msgout(msg)\n"); 805*7c478bd9Sstevel@tonic-gate f_print(fout, "\tchar *msg;\n"); 806*7c478bd9Sstevel@tonic-gate } else { 807*7c478bd9Sstevel@tonic-gate f_print(fout, "void _msgout(char* msg)\n"); 808*7c478bd9Sstevel@tonic-gate } 809*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 810*7c478bd9Sstevel@tonic-gate f_print(fout, "#ifdef RPC_SVC_FG\n"); 811*7c478bd9Sstevel@tonic-gate if (inetdflag || pmflag) 812*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (_rpcpmstart)\n"); 813*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n"); 814*7c478bd9Sstevel@tonic-gate f_print(fout, "\telse\n"); 815*7c478bd9Sstevel@tonic-gate f_print(fout, 816*7c478bd9Sstevel@tonic-gate "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n"); 817*7c478bd9Sstevel@tonic-gate f_print(fout, "#else\n"); 818*7c478bd9Sstevel@tonic-gate f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n"); 819*7c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n"); 820*7c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 821*7c478bd9Sstevel@tonic-gate } 822*7c478bd9Sstevel@tonic-gate 823*7c478bd9Sstevel@tonic-gate /* 824*7c478bd9Sstevel@tonic-gate * Write the timeout function 825*7c478bd9Sstevel@tonic-gate */ 826*7c478bd9Sstevel@tonic-gate static 827*7c478bd9Sstevel@tonic-gate write_timeout_func() 828*7c478bd9Sstevel@tonic-gate { 829*7c478bd9Sstevel@tonic-gate if (!timerflag) 830*7c478bd9Sstevel@tonic-gate return; 831*7c478bd9Sstevel@tonic-gate 832*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 833*7c478bd9Sstevel@tonic-gate if (mtflag) { 834*7c478bd9Sstevel@tonic-gate f_print(fout, "/*ARGSUSED*/\n"); 835*7c478bd9Sstevel@tonic-gate f_print(fout, "static void *\n"); 836*7c478bd9Sstevel@tonic-gate if (!Cflag) { 837*7c478bd9Sstevel@tonic-gate f_print(fout, "closedown(arg)\n"); 838*7c478bd9Sstevel@tonic-gate f_print(fout, "\tvoid *arg;\n"); 839*7c478bd9Sstevel@tonic-gate } else 840*7c478bd9Sstevel@tonic-gate f_print(fout, "closedown(void *arg)\n"); 841*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 842*7c478bd9Sstevel@tonic-gate f_print(fout, "\t/*CONSTCOND*/\n"); 843*7c478bd9Sstevel@tonic-gate f_print(fout, "\twhile (1) {\n"); 844*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n"); 845*7c478bd9Sstevel@tonic-gate f_print(fout, 846*7c478bd9Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n"); 847*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tcontinue;\n\n"); 848*7c478bd9Sstevel@tonic-gate f_print(fout, 849*7c478bd9Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n"); 850*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 851*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tint size;\n"); 852*7c478bd9Sstevel@tonic-gate } else { 853*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\textern fd_set svc_fdset;\n"); 854*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tstatic int size;\n"); 855*7c478bd9Sstevel@tonic-gate } 856*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tint i, openfd = 0;\n\n"); 857*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 858*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tsize = svc_max_pollfd;\n"); 859*7c478bd9Sstevel@tonic-gate } else { 860*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (size == 0) {\n"); 861*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tsize = getdtablesize();\n"); 862*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n"); 863*7c478bd9Sstevel@tonic-gate } 864*7c478bd9Sstevel@tonic-gate f_print(fout, 865*7c478bd9Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n"); 866*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 867*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n"); 868*7c478bd9Sstevel@tonic-gate } else { 869*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n"); 870*7c478bd9Sstevel@tonic-gate } 871*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\t\topenfd++;\n"); 872*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (openfd <= 1)\n"); 873*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\texit(0);\n"); 874*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t} else\n"); 875*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n"); 876*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tmutex_unlock(&_svcstate_lock);\n"); 877*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 878*7c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 879*7c478bd9Sstevel@tonic-gate return; 880*7c478bd9Sstevel@tonic-gate } 881*7c478bd9Sstevel@tonic-gate 882*7c478bd9Sstevel@tonic-gate f_print(fout, "static void\n"); 883*7c478bd9Sstevel@tonic-gate if (!Cflag) { 884*7c478bd9Sstevel@tonic-gate f_print(fout, "closedown(sig)\n"); 885*7c478bd9Sstevel@tonic-gate f_print(fout, "\tint sig;\n"); 886*7c478bd9Sstevel@tonic-gate } else 887*7c478bd9Sstevel@tonic-gate f_print(fout, "closedown(int sig)\n"); 888*7c478bd9Sstevel@tonic-gate f_print(fout, "{\n"); 889*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n"); 890*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 891*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint size;\n"); 892*7c478bd9Sstevel@tonic-gate } else { 893*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\textern fd_set svc_fdset;\n"); 894*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tstatic int size;\n"); 895*7c478bd9Sstevel@tonic-gate } 896*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint i, openfd = 0;\n\n"); 897*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 898*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsize = svc_max_pollfd;\n"); 899*7c478bd9Sstevel@tonic-gate } else { 900*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (size == 0) {\n"); 901*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tsize = getdtablesize();\n"); 902*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 903*7c478bd9Sstevel@tonic-gate } 904*7c478bd9Sstevel@tonic-gate f_print(fout, 905*7c478bd9Sstevel@tonic-gate "\t\tfor (i = 0; i < size && openfd < 2; i++)\n"); 906*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 907*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n"); 908*7c478bd9Sstevel@tonic-gate } else { 909*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n"); 910*7c478bd9Sstevel@tonic-gate } 911*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\topenfd++;\n"); 912*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (openfd <= 1)\n"); 913*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(0);\n"); 914*7c478bd9Sstevel@tonic-gate f_print(fout, "\t} else\n"); 915*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n"); 916*7c478bd9Sstevel@tonic-gate 917*7c478bd9Sstevel@tonic-gate f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n", 918*7c478bd9Sstevel@tonic-gate Cflag? "(SIG_PF)" : "(void(*)())"); 919*7c478bd9Sstevel@tonic-gate f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n"); 920*7c478bd9Sstevel@tonic-gate f_print(fout, "}\n"); 921*7c478bd9Sstevel@tonic-gate } 922*7c478bd9Sstevel@tonic-gate 923*7c478bd9Sstevel@tonic-gate /* 924*7c478bd9Sstevel@tonic-gate * Write the most of port monitor support 925*7c478bd9Sstevel@tonic-gate */ 926*7c478bd9Sstevel@tonic-gate static 927*7c478bd9Sstevel@tonic-gate write_pm_most(infile, netflag) 928*7c478bd9Sstevel@tonic-gate char *infile; 929*7c478bd9Sstevel@tonic-gate int netflag; 930*7c478bd9Sstevel@tonic-gate { 931*7c478bd9Sstevel@tonic-gate list *l; 932*7c478bd9Sstevel@tonic-gate definition *def; 933*7c478bd9Sstevel@tonic-gate version_list *vp; 934*7c478bd9Sstevel@tonic-gate 935*7c478bd9Sstevel@tonic-gate f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n"); 936*7c478bd9Sstevel@tonic-gate f_print(fout, "\t/*\n"); 937*7c478bd9Sstevel@tonic-gate f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n"); 938*7c478bd9Sstevel@tonic-gate f_print(fout, "\t * that we were started by a port monitor. If\n"); 939*7c478bd9Sstevel@tonic-gate f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n"); 940*7c478bd9Sstevel@tonic-gate f_print(fout, "\t * TLI endpoint.\n"); 941*7c478bd9Sstevel@tonic-gate f_print(fout, "\t */\n"); 942*7c478bd9Sstevel@tonic-gate f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n"); 943*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tchar *netid;\n"); 944*7c478bd9Sstevel@tonic-gate if (!netflag) { /* Not included by -n option */ 945*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n"); 946*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP); 947*7c478bd9Sstevel@tonic-gate } 948*7c478bd9Sstevel@tonic-gate if (timerflag) 949*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tint pmclose;\n"); 950*7c478bd9Sstevel@tonic-gate /* 951*7c478bd9Sstevel@tonic-gate * Not necessary, defined in /usr/include/stdlib 952*7c478bd9Sstevel@tonic-gate * f_print(fout, "\t\textern char *getenv();\n"); 953*7c478bd9Sstevel@tonic-gate */ 954*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 955*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t_rpcpmstart = 1;\n"); 956*7c478bd9Sstevel@tonic-gate open_log_file(infile, "\t\t"); 957*7c478bd9Sstevel@tonic-gate f_print(fout, 958*7c478bd9Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n"); 959*7c478bd9Sstevel@tonic-gate 960*7c478bd9Sstevel@tonic-gate if (timerflag) { 961*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t/* started from inetd */\n"); 962*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tpmclose = 1;\n"); 963*7c478bd9Sstevel@tonic-gate } 964*7c478bd9Sstevel@tonic-gate f_print(fout, 965*7c478bd9Sstevel@tonic-gate "\t\t} else {\n"); 966*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n"); 967*7c478bd9Sstevel@tonic-gate sprintf(_errbuf, "cannot get transport info"); 968*7c478bd9Sstevel@tonic-gate print_err_message("\t\t\t\t"); 969*7c478bd9Sstevel@tonic-gate if (timerflag) 970*7c478bd9Sstevel@tonic-gate f_print(fout, 971*7c478bd9Sstevel@tonic-gate "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n"); 972*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 973*7c478bd9Sstevel@tonic-gate f_print(fout, 974*7c478bd9Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n", 975*7c478bd9Sstevel@tonic-gate TRANSP); 976*7c478bd9Sstevel@tonic-gate sprintf(_errbuf, "cannot create server handle"); 977*7c478bd9Sstevel@tonic-gate print_err_message("\t\t\t"); 978*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(1);\n"); 979*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 980*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (nconf)\n"); 981*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\tfreenetconfigent(nconf);\n"); 982*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 983*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 984*7c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM) { 985*7c478bd9Sstevel@tonic-gate continue; 986*7c478bd9Sstevel@tonic-gate } 987*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 988*7c478bd9Sstevel@tonic-gate f_print(fout, 989*7c478bd9Sstevel@tonic-gate "\t\tif (!svc_reg(%s, %s, %s, ", 990*7c478bd9Sstevel@tonic-gate TRANSP, def->def_name, vp->vers_name); 991*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 992*7c478bd9Sstevel@tonic-gate f_print(fout, ", 0)) {\n"); 993*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "unable to register (%s, %s).", 994*7c478bd9Sstevel@tonic-gate def->def_name, vp->vers_name); 995*7c478bd9Sstevel@tonic-gate print_err_message("\t\t\t"); 996*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\texit(1);\n"); 997*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 998*7c478bd9Sstevel@tonic-gate } 999*7c478bd9Sstevel@tonic-gate } 1000*7c478bd9Sstevel@tonic-gate if (timerflag) { 1001*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tif (pmclose) {\n"); 1002*7c478bd9Sstevel@tonic-gate if (mtflag) { 1003*7c478bd9Sstevel@tonic-gate f_print(fout, 1004*7c478bd9Sstevel@tonic-gate "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t\t\t0, NULL) != 0) {\n"); 1005*7c478bd9Sstevel@tonic-gate f_print(fout, 1006*7c478bd9Sstevel@tonic-gate "\t\t\t\t_msgout(\"cannot create closedown thread\");\n"); 1007*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t\texit(1);\n"); 1008*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t\t}\n"); 1009*7c478bd9Sstevel@tonic-gate } else { 1010*7c478bd9Sstevel@tonic-gate f_print(fout, 1011*7c478bd9Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n", 1012*7c478bd9Sstevel@tonic-gate Cflag? "(SIG_PF)" : "(void(*)())"); 1013*7c478bd9Sstevel@tonic-gate f_print(fout, 1014*7c478bd9Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n"); 1015*7c478bd9Sstevel@tonic-gate } 1016*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t}\n"); 1017*7c478bd9Sstevel@tonic-gate } 1018*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\tsvc_run();\n"); 1019*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\texit(1);\n"); 1020*7c478bd9Sstevel@tonic-gate f_print(fout, "\t\t/* NOTREACHED */\n"); 1021*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}"); 1022*7c478bd9Sstevel@tonic-gate } 1023*7c478bd9Sstevel@tonic-gate 1024*7c478bd9Sstevel@tonic-gate /* 1025*7c478bd9Sstevel@tonic-gate * Support for backgrounding the server if self started. 1026*7c478bd9Sstevel@tonic-gate */ 1027*7c478bd9Sstevel@tonic-gate static 1028*7c478bd9Sstevel@tonic-gate write_rpc_svc_fg(infile, sp) 1029*7c478bd9Sstevel@tonic-gate char *infile; 1030*7c478bd9Sstevel@tonic-gate char *sp; 1031*7c478bd9Sstevel@tonic-gate { 1032*7c478bd9Sstevel@tonic-gate f_print(fout, "#ifndef RPC_SVC_FG\n"); 1033*7c478bd9Sstevel@tonic-gate f_print(fout, "#pragma weak closefrom\n"); 1034*7c478bd9Sstevel@tonic-gate f_print(fout, "%sextern void closefrom();\n", sp); 1035*7c478bd9Sstevel@tonic-gate f_print(fout, "%sint size;\n", sp); 1036*7c478bd9Sstevel@tonic-gate if (tirpcflag) 1037*7c478bd9Sstevel@tonic-gate f_print(fout, "%sstruct rlimit rl;\n", sp); 1038*7c478bd9Sstevel@tonic-gate if (inetdflag) 1039*7c478bd9Sstevel@tonic-gate f_print(fout, "%sint pid, i;\n\n", sp); 1040*7c478bd9Sstevel@tonic-gate f_print(fout, "%spid = fork();\n", sp); 1041*7c478bd9Sstevel@tonic-gate f_print(fout, "%sif (pid < 0) {\n", sp); 1042*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tperror(\"cannot fork\");\n", sp); 1043*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\texit(1);\n", sp); 1044*7c478bd9Sstevel@tonic-gate f_print(fout, "%s}\n", sp); 1045*7c478bd9Sstevel@tonic-gate f_print(fout, "%sif (pid)\n", sp); 1046*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\texit(0);\n", sp); 1047*7c478bd9Sstevel@tonic-gate /* close all file descriptors */ 1048*7c478bd9Sstevel@tonic-gate if (tirpcflag) { 1049*7c478bd9Sstevel@tonic-gate f_print(fout, "%sif (closefrom != NULL)\n", sp); 1050*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tclosefrom(0);\n", sp); 1051*7c478bd9Sstevel@tonic-gate f_print(fout, "%selse {\n", sp); 1052*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\trl.rlim_max = 0;\n", sp); 1053*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp); 1054*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp); 1055*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 1056*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp); 1057*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\t(void) close(i);\n", sp); 1058*7c478bd9Sstevel@tonic-gate f_print(fout, "%s}\n", sp); 1059*7c478bd9Sstevel@tonic-gate } else { 1060*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tsize = getdtablesize();\n", sp); 1061*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp); 1062*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\t(void) close(i);\n", sp); 1063*7c478bd9Sstevel@tonic-gate } 1064*7c478bd9Sstevel@tonic-gate /* Redirect stderr and stdout to /dev/null */ 1065*7c478bd9Sstevel@tonic-gate f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp); 1066*7c478bd9Sstevel@tonic-gate f_print(fout, "%s(void) dup2(i, 1);\n", sp); 1067*7c478bd9Sstevel@tonic-gate f_print(fout, "%s(void) dup2(i, 2);\n", sp); 1068*7c478bd9Sstevel@tonic-gate /* This removes control of the controlling terminal */ 1069*7c478bd9Sstevel@tonic-gate if (tirpcflag) 1070*7c478bd9Sstevel@tonic-gate f_print(fout, "%ssetsid();\n", sp); 1071*7c478bd9Sstevel@tonic-gate else { 1072*7c478bd9Sstevel@tonic-gate f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp); 1073*7c478bd9Sstevel@tonic-gate f_print(fout, "%sif (i >= 0) {\n", sp); 1074*7c478bd9Sstevel@tonic-gate f_print(fout, 1075*7c478bd9Sstevel@tonic-gate "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp); 1076*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t(void) close(i);\n", sp); 1077*7c478bd9Sstevel@tonic-gate f_print(fout, "%s}\n", sp); 1078*7c478bd9Sstevel@tonic-gate } 1079*7c478bd9Sstevel@tonic-gate if (!logflag) 1080*7c478bd9Sstevel@tonic-gate open_log_file(infile, sp); 1081*7c478bd9Sstevel@tonic-gate f_print(fout, "#endif\n"); 1082*7c478bd9Sstevel@tonic-gate if (logflag) 1083*7c478bd9Sstevel@tonic-gate open_log_file(infile, sp); 1084*7c478bd9Sstevel@tonic-gate } 1085*7c478bd9Sstevel@tonic-gate 1086*7c478bd9Sstevel@tonic-gate static 1087*7c478bd9Sstevel@tonic-gate open_log_file(infile, sp) 1088*7c478bd9Sstevel@tonic-gate char *infile; 1089*7c478bd9Sstevel@tonic-gate char *sp; 1090*7c478bd9Sstevel@tonic-gate { 1091*7c478bd9Sstevel@tonic-gate char *s; 1092*7c478bd9Sstevel@tonic-gate 1093*7c478bd9Sstevel@tonic-gate s = strrchr(infile, '.'); 1094*7c478bd9Sstevel@tonic-gate if (s) 1095*7c478bd9Sstevel@tonic-gate *s = '\0'; 1096*7c478bd9Sstevel@tonic-gate f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile); 1097*7c478bd9Sstevel@tonic-gate if (s) 1098*7c478bd9Sstevel@tonic-gate *s = '.'; 1099*7c478bd9Sstevel@tonic-gate } 1100*7c478bd9Sstevel@tonic-gate 1101*7c478bd9Sstevel@tonic-gate 1102*7c478bd9Sstevel@tonic-gate 1103*7c478bd9Sstevel@tonic-gate 1104*7c478bd9Sstevel@tonic-gate /* 1105*7c478bd9Sstevel@tonic-gate * write a registration for the given transport for Inetd 1106*7c478bd9Sstevel@tonic-gate */ 1107*7c478bd9Sstevel@tonic-gate void 1108*7c478bd9Sstevel@tonic-gate write_inetd_register(transp) 1109*7c478bd9Sstevel@tonic-gate char *transp; 1110*7c478bd9Sstevel@tonic-gate { 1111*7c478bd9Sstevel@tonic-gate list *l; 1112*7c478bd9Sstevel@tonic-gate definition *def; 1113*7c478bd9Sstevel@tonic-gate version_list *vp; 1114*7c478bd9Sstevel@tonic-gate char *sp; 1115*7c478bd9Sstevel@tonic-gate int isudp; 1116*7c478bd9Sstevel@tonic-gate char tmpbuf[32]; 1117*7c478bd9Sstevel@tonic-gate 1118*7c478bd9Sstevel@tonic-gate if (inetdflag) 1119*7c478bd9Sstevel@tonic-gate sp = "\t"; 1120*7c478bd9Sstevel@tonic-gate else 1121*7c478bd9Sstevel@tonic-gate sp = ""; 1122*7c478bd9Sstevel@tonic-gate if (streq(transp, "udp")) 1123*7c478bd9Sstevel@tonic-gate isudp = 1; 1124*7c478bd9Sstevel@tonic-gate else 1125*7c478bd9Sstevel@tonic-gate isudp = 0; 1126*7c478bd9Sstevel@tonic-gate f_print(fout, "\n"); 1127*7c478bd9Sstevel@tonic-gate if (inetdflag) { 1128*7c478bd9Sstevel@tonic-gate f_print(fout, 1129*7c478bd9Sstevel@tonic-gate "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n", 1130*7c478bd9Sstevel@tonic-gate isudp ? "SOCK_DGRAM" : "SOCK_STREAM"); 1131*7c478bd9Sstevel@tonic-gate } 1132*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t%s = svc%s_create(%s", 1133*7c478bd9Sstevel@tonic-gate sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK"); 1134*7c478bd9Sstevel@tonic-gate if (!isudp) 1135*7c478bd9Sstevel@tonic-gate f_print(fout, ", 0, 0"); 1136*7c478bd9Sstevel@tonic-gate f_print(fout, ");\n"); 1137*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP); 1138*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, "cannot create %s service.", transp); 1139*7c478bd9Sstevel@tonic-gate (void) sprintf(tmpbuf, "%s\t\t", sp); 1140*7c478bd9Sstevel@tonic-gate print_err_message(tmpbuf); 1141*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 1142*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t}\n", sp); 1143*7c478bd9Sstevel@tonic-gate 1144*7c478bd9Sstevel@tonic-gate if (inetdflag) { 1145*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp); 1146*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tproto = IPPROTO_%s;\n", 1147*7c478bd9Sstevel@tonic-gate sp, isudp ? "UDP": "TCP"); 1148*7c478bd9Sstevel@tonic-gate } 1149*7c478bd9Sstevel@tonic-gate for (l = defined; l != NULL; l = l->next) { 1150*7c478bd9Sstevel@tonic-gate def = (definition *) l->val; 1151*7c478bd9Sstevel@tonic-gate if (def->def_kind != DEF_PROGRAM) { 1152*7c478bd9Sstevel@tonic-gate continue; 1153*7c478bd9Sstevel@tonic-gate } 1154*7c478bd9Sstevel@tonic-gate for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 1155*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ", 1156*7c478bd9Sstevel@tonic-gate sp, TRANSP, def->def_name, vp->vers_name); 1157*7c478bd9Sstevel@tonic-gate pvname(def->def_name, vp->vers_num); 1158*7c478bd9Sstevel@tonic-gate if (inetdflag) 1159*7c478bd9Sstevel@tonic-gate f_print(fout, ", proto)) {\n"); 1160*7c478bd9Sstevel@tonic-gate else 1161*7c478bd9Sstevel@tonic-gate f_print(fout, ", IPPROTO_%s)) {\n", 1162*7c478bd9Sstevel@tonic-gate isudp ? "UDP": "TCP"); 1163*7c478bd9Sstevel@tonic-gate (void) sprintf(_errbuf, 1164*7c478bd9Sstevel@tonic-gate "unable to register (%s, %s, %s).", 1165*7c478bd9Sstevel@tonic-gate def->def_name, vp->vers_name, transp); 1166*7c478bd9Sstevel@tonic-gate print_err_message(tmpbuf); 1167*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t\texit(1);\n", sp); 1168*7c478bd9Sstevel@tonic-gate f_print(fout, "%s\t}\n", sp); 1169*7c478bd9Sstevel@tonic-gate } 1170*7c478bd9Sstevel@tonic-gate } 1171*7c478bd9Sstevel@tonic-gate if (inetdflag) 1172*7c478bd9Sstevel@tonic-gate f_print(fout, "\t}\n"); 1173*7c478bd9Sstevel@tonic-gate } 1174