Home | History | Annotate | Line # | Download | only in rpcgen
rpc_svcout.c revision 1.13
      1 /*	$NetBSD: rpc_svcout.c,v 1.13 2001/03/21 19:20:18 mycroft Exp $	*/
      2 /*
      3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
      4  * unrestricted use provided that this legend is included on all tape
      5  * media and as a part of the software program in whole or part.  Users
      6  * may copy or modify Sun RPC without charge, but are not authorized
      7  * to license or distribute it to anyone else except as part of a product or
      8  * program developed by the user or with the express written consent of
      9  * Sun Microsystems, Inc.
     10  *
     11  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
     12  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
     13  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
     14  *
     15  * Sun RPC is provided with no support and without any obligation on the
     16  * part of Sun Microsystems, Inc. to assist in its use, correction,
     17  * modification or enhancement.
     18  *
     19  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
     20  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
     21  * OR ANY PART THEREOF.
     22  *
     23  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
     24  * or profits or other special, indirect and consequential damages, even if
     25  * Sun has been advised of the possibility of such damages.
     26  *
     27  * Sun Microsystems, Inc.
     28  * 2550 Garcia Avenue
     29  * Mountain View, California  94043
     30  */
     31 
     32 #include <sys/cdefs.h>
     33 #ifndef lint
     34 #if 0
     35 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
     36 #else
     37 __RCSID("$NetBSD: rpc_svcout.c,v 1.13 2001/03/21 19:20:18 mycroft Exp $");
     38 #endif
     39 #endif
     40 
     41 /*
     42  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
     43  */
     44 #include <stdio.h>
     45 #include <string.h>
     46 #include "rpc_scan.h"
     47 #include "rpc_parse.h"
     48 #include "rpc_util.h"
     49 
     50 static char RQSTP[] = "rqstp";
     51 static char TRANSP[] = "transp";
     52 static char ARG[] = "argument";
     53 static char RESULT[] = "result";
     54 static char ROUTINE[] = "local";
     55 
     56 static void p_xdrfunc __P((char *, char *));
     57 static void internal_proctype __P((proc_list *));
     58 static void write_real_program __P((definition *));
     59 static void write_program __P((definition *, char *));
     60 static void printerr __P((char *, char *));
     61 static void printif __P((char *, char *, char *, char *));
     62 static void write_inetmost __P((char *));
     63 static void print_return __P((char *));
     64 static void print_pmapunset __P((char *));
     65 static void print_err_message __P((char *));
     66 static void write_timeout_func __P((void));
     67 static void write_caller_func __P((void));
     68 static void write_pm_most __P((char *, int));
     69 static void write_rpc_svc_fg __P((char *, char *));
     70 static void open_log_file __P((char *, char *));
     71 
     72 char    _errbuf[256];		/* For all messages */
     73 
     74 static void
     75 p_xdrfunc(rname, typename)
     76 	char   *rname;
     77 	char   *typename;
     78 {
     79 	if (Cflag)
     80 		f_print(fout, "\t\txdr_%s = (xdrproc_t)xdr_%s;\n", rname,
     81 		    stringfix(typename));
     82 	else
     83 		f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix(typename));
     84 }
     85 
     86 static void
     87 internal_proctype(plist)
     88 	proc_list *plist;
     89 {
     90 	f_print(fout, "static ");
     91 	ptype(plist->res_prefix, plist->res_type, 1);
     92 	f_print(fout, "*");
     93 }
     94 
     95 
     96 /*
     97  * write most of the service, that is, everything but the registrations.
     98  */
     99 void
    100 write_most(infile, netflag, nomain)
    101 	char   *infile;		/* our name */
    102 	int     netflag;
    103 	int     nomain;
    104 {
    105 	if (inetdflag || pmflag) {
    106 		char   *var_type;
    107 		var_type = (nomain ? "extern" : "static");
    108 		f_print(fout, "%s int _rpcpmstart;", var_type);
    109 		f_print(fout, "\t\t/* Started by a port monitor ? */\n");
    110 		f_print(fout, "%s int _rpcfdtype;", var_type);
    111 		f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
    112 		if (timerflag) {
    113 			f_print(fout, "%s int _rpcsvcdirty;", var_type);
    114 			f_print(fout, "\t/* Still serving ? */\n");
    115 		}
    116 		write_svc_aux(nomain);
    117 	}
    118 	/* write out dispatcher and stubs */
    119 	write_programs(nomain ? (char *) NULL : "static");
    120 
    121 	if (nomain)
    122 		return;
    123 
    124 	f_print(fout, "\n\n");
    125 	if (Cflag)
    126 		f_print(fout, "int main( int argc, char* argv[] );\n");
    127 	f_print(fout, "\nint\n");
    128 	if (Cflag)
    129 		f_print(fout, "main( int argc, char* argv[] )\n");
    130 	else
    131 		f_print(fout, "main(argc, argv)\nint argc;\nchar *argv[];\n");
    132 	f_print(fout, "{\n");
    133 	if (inetdflag) {
    134 		write_inetmost(infile);	/* Includes call to write_rpc_svc_fg() */
    135 	} else {
    136 		if (tirpcflag) {
    137 			if (netflag) {
    138 				f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
    139 				f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
    140 			}
    141 			f_print(fout, "\tpid_t pid;\n");
    142 			f_print(fout, "\tint i;\n");
    143 			f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
    144 			write_pm_most(infile, netflag);
    145 			f_print(fout, "\telse {\n");
    146 			write_rpc_svc_fg(infile, "\t\t");
    147 			f_print(fout, "\t}\n");
    148 		} else {
    149 			f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
    150 			f_print(fout, "\n");
    151 			print_pmapunset("\t");
    152 		}
    153 	}
    154 
    155 	if (logflag && !inetdflag) {
    156 		open_log_file(infile, "\t");
    157 	}
    158 }
    159 /*
    160  * write a registration for the given transport
    161  */
    162 void
    163 write_netid_register(transp)
    164 	char   *transp;
    165 {
    166 	list   *l;
    167 	definition *def;
    168 	version_list *vp;
    169 	char   *sp;
    170 	char    tmpbuf[32];
    171 
    172 	sp = "";
    173 	f_print(fout, "\n");
    174 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
    175 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
    176 	(void) sprintf(_errbuf, "cannot find %s netid.", transp);
    177 	sprintf(tmpbuf, "%s\t\t", sp);
    178 	print_err_message(tmpbuf);
    179 	f_print(fout, "%s\t\texit(1);\n", sp);
    180 	f_print(fout, "%s\t}\n", sp);
    181 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
    182 	    sp, TRANSP);
    183 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
    184 	(void) sprintf(_errbuf, "cannot create %s service.", transp);
    185 	print_err_message(tmpbuf);
    186 	f_print(fout, "%s\t\texit(1);\n", sp);
    187 	f_print(fout, "%s\t}\n", sp);
    188 
    189 	for (l = defined; l != NULL; l = l->next) {
    190 		def = (definition *) l->val;
    191 		if (def->def_kind != DEF_PROGRAM) {
    192 			continue;
    193 		}
    194 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    195 			f_print(fout,
    196 			    "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
    197 			    sp, def->def_name, vp->vers_name);
    198 			f_print(fout,
    199 			    "%s\tif (!svc_reg(%s, %s, %s, ",
    200 			    sp, TRANSP, def->def_name, vp->vers_name);
    201 			pvname(def->def_name, vp->vers_num);
    202 			f_print(fout, ", nconf)) {\n");
    203 			(void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
    204 			    def->def_name, vp->vers_name, transp);
    205 			print_err_message(tmpbuf);
    206 			f_print(fout, "%s\t\texit(1);\n", sp);
    207 			f_print(fout, "%s\t}\n", sp);
    208 		}
    209 	}
    210 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
    211 }
    212 /*
    213  * write a registration for the given transport for TLI
    214  */
    215 void
    216 write_nettype_register(transp)
    217 	char   *transp;
    218 {
    219 	list   *l;
    220 	definition *def;
    221 	version_list *vp;
    222 
    223 	for (l = defined; l != NULL; l = l->next) {
    224 		def = (definition *) l->val;
    225 		if (def->def_kind != DEF_PROGRAM) {
    226 			continue;
    227 		}
    228 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    229 			f_print(fout, "\tif (!svc_create(");
    230 			pvname(def->def_name, vp->vers_num);
    231 			f_print(fout, ", %s, %s, \"%s\")) {\n ",
    232 			    def->def_name, vp->vers_name, transp);
    233 			(void) sprintf(_errbuf,
    234 			    "unable to create (%s, %s) for %s.",
    235 			    def->def_name, vp->vers_name, transp);
    236 			print_err_message("\t\t");
    237 			f_print(fout, "\t\texit(1);\n");
    238 			f_print(fout, "\t}\n");
    239 		}
    240 	}
    241 }
    242 /*
    243  * write the rest of the service
    244  */
    245 void
    246 write_rest()
    247 {
    248 	f_print(fout, "\n");
    249 	if (inetdflag) {
    250 		f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
    251 		(void) sprintf(_errbuf, "could not create a handle");
    252 		print_err_message("\t\t");
    253 		f_print(fout, "\t\texit(1);\n");
    254 		f_print(fout, "\t}\n");
    255 		if (timerflag) {
    256 			f_print(fout, "\tif (_rpcpmstart) {\n");
    257 			f_print(fout,
    258 			    "\t\t(void) signal(SIGALRM, %s closedown);\n",
    259 			    Cflag ? "(SIG_PF)" : "(void(*)())");
    260 			f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
    261 			f_print(fout, "\t}\n");
    262 		}
    263 	}
    264 	f_print(fout, "\tsvc_run();\n");
    265 	(void) sprintf(_errbuf, "svc_run returned");
    266 	print_err_message("\t");
    267 	f_print(fout, "\texit(1);\n");
    268 	f_print(fout, "\t/* NOTREACHED */\n");
    269 	f_print(fout, "}\n");
    270 }
    271 
    272 void
    273 write_programs(storage)
    274 	char   *storage;
    275 {
    276 	list   *l;
    277 	definition *def;
    278 
    279 	/* write out stubs for procedure  definitions */
    280 	for (l = defined; l != NULL; l = l->next) {
    281 		def = (definition *) l->val;
    282 		if (def->def_kind == DEF_PROGRAM) {
    283 			write_real_program(def);
    284 		}
    285 	}
    286 
    287 	/* write out dispatcher for each program */
    288 	for (l = defined; l != NULL; l = l->next) {
    289 		def = (definition *) l->val;
    290 		if (def->def_kind == DEF_PROGRAM) {
    291 			write_program(def, storage);
    292 		}
    293 	}
    294 
    295 
    296 }
    297 /* write out definition of internal function (e.g. _printmsg_1(...))
    298    which calls server's defintion of actual function (e.g. printmsg_1(...)).
    299    Unpacks single user argument of printmsg_1 to call-by-value format
    300    expected by printmsg_1. */
    301 static void
    302 write_real_program(def)
    303 	definition *def;
    304 {
    305 	version_list *vp;
    306 	proc_list *proc;
    307 	decl_list *l;
    308 
    309 	if (!newstyle)
    310 		return;		/* not needed for old style */
    311 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    312 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
    313 			f_print(fout, "\n");
    314 			internal_proctype(proc);
    315 			f_print(fout, "\n_");
    316 			pvname(proc->proc_name, vp->vers_num);
    317 			if (Cflag) {
    318 				f_print(fout, "(");
    319 				/* arg name */
    320 				if (proc->arg_num > 1)
    321 					f_print(fout, "%s",
    322 						proc->args.argname);
    323 				else
    324 					ptype(proc->args.decls->decl.prefix,
    325 					    proc->args.decls->decl.type, 0);
    326 				f_print(fout, " *argp, struct svc_req *%s)\n",
    327 				    RQSTP);
    328 			} else {
    329 				f_print(fout, "(argp, %s)\n", RQSTP);
    330 				/* arg name */
    331 				if (proc->arg_num > 1)
    332 					f_print(fout, "\t%s *argp;\n", proc->args.argname);
    333 				else {
    334 					f_print(fout, "\t");
    335 					ptype(proc->args.decls->decl.prefix,
    336 					    proc->args.decls->decl.type, 0);
    337 					f_print(fout, " *argp;\n");
    338 				}
    339 				f_print(fout, "	struct svc_req *%s;\n", RQSTP);
    340 			}
    341 
    342 			f_print(fout, "{\n");
    343 			f_print(fout, "\treturn(");
    344 			pvname_svc(proc->proc_name, vp->vers_num);
    345 			f_print(fout, "(");
    346 			if (proc->arg_num < 2) {	/* single argument */
    347 				if (!streq(proc->args.decls->decl.type, "void"))
    348 					f_print(fout, "*argp, ");	/* non-void */
    349 			} else {
    350 				for (l = proc->args.decls; l != NULL; l = l->next)
    351 					f_print(fout, "argp->%s, ", l->decl.name);
    352 			}
    353 			f_print(fout, "%s));\n}\n", RQSTP);
    354 		}
    355 	}
    356 }
    357 
    358 static void
    359 write_program(def, storage)
    360 	definition *def;
    361 	char   *storage;
    362 {
    363 	version_list *vp;
    364 	proc_list *proc;
    365 	int     filled;
    366 
    367 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    368 		if (Cflag) {
    369 			f_print(fout, "\n");
    370 			if (storage != NULL) {
    371 				f_print(fout, "%s ", storage);
    372 			}
    373 			f_print(fout, "void ");
    374 			pvname(def->def_name, vp->vers_num);
    375 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
    376 			f_print(fout, "SVCXPRT *%s);\n", TRANSP);
    377 		}
    378 		f_print(fout, "\n");
    379 		if (storage != NULL) {
    380 			f_print(fout, "%s ", storage);
    381 		}
    382 		f_print(fout, "void\n");
    383 		pvname(def->def_name, vp->vers_num);
    384 
    385 		if (Cflag) {
    386 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
    387 			f_print(fout, "SVCXPRT *%s)\n", TRANSP);
    388 		} else {
    389 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
    390 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
    391 			f_print(fout, "	SVCXPRT *%s;\n", TRANSP);
    392 		}
    393 
    394 		f_print(fout, "{\n");
    395 
    396 		filled = 0;
    397 		f_print(fout, "\tunion {\n");
    398 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
    399 			if (proc->arg_num < 2) {	/* single argument */
    400 				if (streq(proc->args.decls->decl.type,
    401 					"void")) {
    402 					continue;
    403 				}
    404 				filled = 1;
    405 				f_print(fout, "\t\t");
    406 				ptype(proc->args.decls->decl.prefix,
    407 				    proc->args.decls->decl.type, 0);
    408 				pvname(proc->proc_name, vp->vers_num);
    409 				f_print(fout, "_arg;\n");
    410 
    411 			} else {
    412 				filled = 1;
    413 				f_print(fout, "\t\t%s", proc->args.argname);
    414 				f_print(fout, " ");
    415 				pvname(proc->proc_name, vp->vers_num);
    416 				f_print(fout, "_arg;\n");
    417 			}
    418 		}
    419 		if (!filled) {
    420 			f_print(fout, "\t\tint fill;\n");
    421 		}
    422 		f_print(fout, "\t} %s;\n", ARG);
    423 		if (Mflag) {
    424 			f_print(fout, "\tunion {\n");
    425 			for (proc = vp->procs; proc != NULL; proc = proc->next) {
    426 				f_print(fout, "\t\t");
    427 				if (streq(proc->res_type, "void"))
    428 					f_print(fout, "char ");
    429 				else
    430 					ptype(proc->res_prefix, proc->res_type,
    431 					    1);
    432 				pvname(proc->proc_name, vp->vers_num);
    433 				f_print(fout, "_res;\n");
    434 			}
    435 			f_print(fout, "\t} %s;\n", RESULT);
    436 			f_print(fout, "\tbool_t retval;\n");
    437 		} else
    438 			f_print(fout, "\tchar *%s;\n", RESULT);
    439 
    440 		if (Cflag) {
    441 			f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
    442 			if (Mflag)
    443 				f_print(fout,
    444 				    "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
    445 				    ROUTINE);
    446 			else
    447 				f_print(fout,
    448 				    "\tchar *(*%s)(char *, struct svc_req *);\n",
    449 				    ROUTINE);
    450 		} else {
    451 			f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
    452 			if (Mflag)
    453 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
    454 			else
    455 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
    456 		}
    457 
    458 		f_print(fout, "\n");
    459 
    460 		if (callerflag)
    461 			f_print(fout, "\tcaller = transp;\n");	/* EVAS */
    462 		if (timerflag)
    463 			f_print(fout, "\t_rpcsvcdirty = 1;\n");
    464 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
    465 		if (!nullproc(vp->procs)) {
    466 			f_print(fout, "\tcase NULLPROC:\n");
    467 			if (Cflag) {
    468 			  	f_print(fout,
    469 					"\t\t(void) svc_sendreply(%s, (xdrproc_t)xdr_void, (char *)NULL);\n", TRANSP);
    470 			} else {
    471 			  	f_print(fout,
    472 					"\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
    473 					TRANSP);
    474 			}
    475 			print_return("\t\t");
    476 			f_print(fout, "\n");
    477 		}
    478 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
    479 			f_print(fout, "\tcase %s:\n", proc->proc_name);
    480 			if (proc->arg_num < 2) {	/* single argument */
    481 				p_xdrfunc(ARG, proc->args.decls->decl.type);
    482 			} else {
    483 				p_xdrfunc(ARG, proc->args.argname);
    484 			}
    485 			p_xdrfunc(RESULT, proc->res_type);
    486 			if (Cflag) {
    487 				if (Mflag)
    488 					f_print(fout,
    489 					    "\t\t%s = (bool_t (*)(char *, void *, struct svc_req *))",
    490 					    ROUTINE);
    491 				else
    492 					f_print(fout,
    493 					    "\t\t%s = (char *(*)(char *, struct svc_req *))",
    494 					    ROUTINE);
    495 			} else {
    496 				if (Mflag)
    497 					f_print(fout, "\t\t%s = (bool_t (*)())", ROUTINE);
    498 				else
    499 					f_print(fout, "\t\t%s = (char *(*)())", ROUTINE);
    500 			}
    501 
    502 			if (newstyle)	/* new style: calls internal routine */
    503 				f_print(fout, "_");
    504 			if (Cflag)
    505 				pvname_svc(proc->proc_name, vp->vers_num);
    506 			else
    507 				pvname(proc->proc_name, vp->vers_num);
    508 			f_print(fout, ";\n");
    509 			f_print(fout, "\t\tbreak;\n\n");
    510 		}
    511 		f_print(fout, "\tdefault:\n");
    512 		printerr("noproc", TRANSP);
    513 		print_return("\t\t");
    514 		f_print(fout, "\t}\n");
    515 
    516 		f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
    517 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
    518 		printerr("decode", TRANSP);
    519 		print_return("\t\t");
    520 		f_print(fout, "\t}\n");
    521 
    522 		if (Cflag) {
    523 			if (Mflag)
    524 				f_print(fout, "\tretval = (*%s)((char *)&%s, (void *)&%s, %s);\n",
    525 				    ROUTINE, ARG, RESULT, RQSTP);
    526 			else
    527 				f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
    528 				    RESULT, ROUTINE, ARG, RQSTP);
    529 		} else {
    530 			if (Mflag)
    531 				f_print(fout, "\tretval = (*%s)(&%s, &%s, %s);\n",
    532 				    ROUTINE, ARG, RESULT, RQSTP);
    533 			else
    534 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
    535 				    RESULT, ROUTINE, ARG, RQSTP);
    536 		}
    537 		if (Mflag)
    538 			f_print(fout,
    539 			    "\tif (retval > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
    540 			    TRANSP, RESULT, RESULT);
    541 		else
    542 			f_print(fout,
    543 			    "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
    544 			    RESULT, TRANSP, RESULT, RESULT);
    545 		printerr("systemerr", TRANSP);
    546 		f_print(fout, "\t}\n");
    547 
    548 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
    549 		(void) sprintf(_errbuf, "unable to free arguments");
    550 		print_err_message("\t\t");
    551 		f_print(fout, "\t\texit(1);\n");
    552 		f_print(fout, "\t}\n");
    553 
    554 		if (Mflag) {
    555 			f_print(fout, "\tif (!");
    556 			pvname(def->def_name, vp->vers_num);
    557 			f_print(fout, "_freeresult");
    558 			f_print(fout, "(%s, xdr_%s, (caddr_t)&%s)) {\n",
    559 			    TRANSP, RESULT, RESULT);
    560 			(void) sprintf(_errbuf, "unable to free results");
    561 			print_err_message("\t\t");
    562 			f_print(fout, "\t\texit(1);\n");
    563 			f_print(fout, "\t}\n");
    564 		}
    565 
    566 		print_return("\t");
    567 		f_print(fout, "}\n");
    568 	}
    569 }
    570 
    571 static void
    572 printerr(err, transp)
    573 	char   *err;
    574 	char   *transp;
    575 {
    576 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
    577 }
    578 
    579 static void
    580 printif(proc, transp, prefix, arg)
    581 	char   *proc;
    582 	char   *transp;
    583 	char   *prefix;
    584 	char   *arg;
    585 {
    586 	f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
    587 	    proc, transp, arg, prefix, arg);
    588 }
    589 
    590 int
    591 nullproc(proc)
    592 	proc_list *proc;
    593 {
    594 	for (; proc != NULL; proc = proc->next) {
    595 		if (streq(proc->proc_num, "0")) {
    596 			return (1);
    597 		}
    598 	}
    599 	return (0);
    600 }
    601 
    602 static void
    603 write_inetmost(infile)
    604 	char   *infile;
    605 {
    606 	f_print(fout, "\tSVCXPRT *%s = NULL;\n", TRANSP);
    607 	f_print(fout, "\tint sock;\n");
    608 	f_print(fout, "\tint proto = 0;\n");
    609 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
    610 	f_print(fout, "\tint asize = sizeof (saddr);\n");
    611 	f_print(fout, "\n");
    612 	f_print(fout,
    613 	    "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
    614 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
    615 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
    616 	f_print(fout, "\t\t\texit(1);\n");
    617 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
    618 	f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
    619 	f_print(fout, "\t\t\texit(1);\n");
    620 	f_print(fout, "\t\tsock = 0;\n");
    621 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
    622 	f_print(fout, "\t\tproto = 0;\n");
    623 	open_log_file(infile, "\t\t");
    624 	f_print(fout, "\t} else {\n");
    625 	write_rpc_svc_fg(infile, "\t\t");
    626 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
    627 	print_pmapunset("\t\t");
    628 	f_print(fout, "\t}\n");
    629 }
    630 
    631 static void
    632 print_return(space)
    633 	char   *space;
    634 {
    635 	if (exitnow)
    636 		f_print(fout, "%sexit(0);\n", space);
    637 	else {
    638 		if (timerflag)
    639 			f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
    640 		f_print(fout, "%sreturn;\n", space);
    641 	}
    642 }
    643 
    644 static void
    645 print_pmapunset(space)
    646 	char   *space;
    647 {
    648 	list   *l;
    649 	definition *def;
    650 	version_list *vp;
    651 
    652 	for (l = defined; l != NULL; l = l->next) {
    653 		def = (definition *) l->val;
    654 		if (def->def_kind == DEF_PROGRAM) {
    655 			for (vp = def->def.pr.versions; vp != NULL;
    656 			    vp = vp->next) {
    657 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
    658 				    space, def->def_name, vp->vers_name);
    659 			}
    660 		}
    661 	}
    662 }
    663 
    664 static void
    665 print_err_message(space)
    666 	char   *space;
    667 {
    668 	if (logflag)
    669 		f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
    670 	else
    671 		if (inetdflag || pmflag)
    672 			f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
    673 		else
    674 			f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
    675 }
    676 /*
    677  * Write the server auxiliary function ( _msgout, timeout)
    678  */
    679 void
    680 write_svc_aux(nomain)
    681 	int     nomain;
    682 {
    683 	if (!logflag)
    684 		write_msg_out();
    685 	if (!nomain)
    686 		write_timeout_func();
    687 	if (callerflag)		/* EVAS */
    688 		write_caller_func();	/* EVAS */
    689 }
    690 /*
    691  * Write the _msgout function
    692  */
    693 void
    694 write_msg_out()
    695 {
    696 	f_print(fout, "\n");
    697 	f_print(fout, "static\n");
    698 	if (!Cflag) {
    699 		f_print(fout, "void _msgout(msg)\n");
    700 		f_print(fout, "\tchar *msg;\n");
    701 	} else {
    702 		f_print(fout, "void _msgout(char* msg)\n");
    703 	}
    704 	f_print(fout, "{\n");
    705 	f_print(fout, "#ifdef RPC_SVC_FG\n");
    706 	if (inetdflag || pmflag)
    707 		f_print(fout, "\tif (_rpcpmstart)\n");
    708 	f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
    709 	f_print(fout, "\telse\n");
    710 	f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
    711 	f_print(fout, "#else\n");
    712 	f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
    713 	f_print(fout, "#endif\n");
    714 	f_print(fout, "}\n");
    715 }
    716 /*
    717  * Write the timeout function
    718  */
    719 static void
    720 write_timeout_func()
    721 {
    722 	if (!timerflag)
    723 		return;
    724 	if (Cflag) {
    725 		f_print(fout, "\n");
    726 		f_print(fout, "static void closedown(void);\n");
    727 	}
    728 	f_print(fout, "\n");
    729 	f_print(fout, "static void\n");
    730 	f_print(fout, "closedown()\n");
    731 	f_print(fout, "{\n");
    732 	f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
    733 	f_print(fout, "\t\textern fd_set svc_fdset;\n");
    734 	f_print(fout, "\t\tstatic int size;\n");
    735 	f_print(fout, "\t\tint i, openfd;\n");
    736 	if (tirpcflag && pmflag) {
    737 		f_print(fout, "\t\tstruct t_info tinfo;\n\n");
    738 		f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
    739 	} else {
    740 		f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
    741 	}
    742 	f_print(fout, "\t\t\texit(0);\n");
    743 	f_print(fout, "\t\tif (size == 0) {\n");
    744 	if (tirpcflag) {
    745 		f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
    746 		f_print(fout, "\t\t\trl.rlim_max = 0;\n");
    747 		f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
    748 		f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
    749 		f_print(fout, "\t\t\t\treturn;\n");
    750 	} else {
    751 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
    752 	}
    753 	f_print(fout, "\t\t}\n");
    754 	f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
    755 	f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
    756 	f_print(fout, "\t\t\t\topenfd++;\n");
    757 	f_print(fout, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
    758 	f_print(fout, "\t\t\texit(0);\n");
    759 	f_print(fout, "\t}\n");
    760 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
    761 	f_print(fout, "}\n");
    762 }
    763 
    764 static void
    765 write_caller_func()
    766 {				/* EVAS */
    767 #define	P(s)	f_print(fout, s);
    768 
    769 	P("\n");
    770 	P("char *svc_caller()\n");
    771 	P("{\n");
    772 	P("	struct sockaddr_in actual;\n");
    773 	P("	struct hostent *hp;\n");
    774 	P("	static struct in_addr prev;\n");
    775 	P("	static char cname[128];\n\n");
    776 
    777 	P("	actual = *svc_getcaller(caller);\n\n");
    778 
    779 	P("	if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
    780 	P("		 sizeof(struct in_addr)) == 0)\n");
    781 	P("		return (cname);\n\n");
    782 
    783 	P("	prev = actual.sin_addr;\n\n");
    784 
    785 	P("	hp = gethostbyaddr((char *) &actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
    786 	P("	if (hp == NULL) {                       /* dummy one up */\n");
    787 	P("		extern char *inet_ntoa();\n");
    788 	P("		strcpy(cname, inet_ntoa(actual.sin_addr));\n");
    789 	P("	} else {\n");
    790 	P("		strcpy(cname, hp->h_name);\n");
    791 	P("	}\n\n");
    792 
    793 	P("	return (cname);\n");
    794 	P("}\n");
    795 
    796 #undef P
    797 }
    798 /*
    799  * Write the most of port monitor support
    800  */
    801 static void
    802 write_pm_most(infile, netflag)
    803 	char   *infile;
    804 	int     netflag;
    805 {
    806 	list   *l;
    807 	definition *def;
    808 	version_list *vp;
    809 
    810 	f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
    811 	f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
    812 	f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
    813 	f_print(fout, "\t\tchar *netid;\n");
    814 	if (!netflag) {		/* Not included by -n option */
    815 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
    816 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
    817 	}
    818 	if (timerflag)
    819 		f_print(fout, "\t\tint pmclose;\n");
    820 /* not necessary, defined in /usr/include/stdlib */
    821 /*	f_print(fout, "\t\textern char *getenv();\n");*/
    822 	f_print(fout, "\n");
    823 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
    824 	if (logflag)
    825 		open_log_file(infile, "\t\t");
    826 	f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
    827 	sprintf(_errbuf, "cannot get transport name");
    828 	print_err_message("\t\t\t");
    829 	f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
    830 	sprintf(_errbuf, "cannot get transport info");
    831 	print_err_message("\t\t\t");
    832 	f_print(fout, "\t\t}\n");
    833 	/*
    834 	 * A kludgy support for inetd services. Inetd only works with
    835 	 * sockmod, and RPC works only with timod, hence all this jugglery
    836 	 */
    837 	f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
    838 	f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
    839 	sprintf(_errbuf, "could not get the right module");
    840 	print_err_message("\t\t\t\t");
    841 	f_print(fout, "\t\t\t\texit(1);\n");
    842 	f_print(fout, "\t\t\t}\n");
    843 	f_print(fout, "\t\t}\n");
    844 	if (timerflag)
    845 		f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
    846 	f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
    847 	    TRANSP);
    848 	sprintf(_errbuf, "cannot create server handle");
    849 	print_err_message("\t\t\t");
    850 	f_print(fout, "\t\t\texit(1);\n");
    851 	f_print(fout, "\t\t}\n");
    852 	f_print(fout, "\t\tif (nconf)\n");
    853 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
    854 	for (l = defined; l != NULL; l = l->next) {
    855 		def = (definition *) l->val;
    856 		if (def->def_kind != DEF_PROGRAM) {
    857 			continue;
    858 		}
    859 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
    860 			f_print(fout,
    861 			    "\t\tif (!svc_reg(%s, %s, %s, ",
    862 			    TRANSP, def->def_name, vp->vers_name);
    863 			pvname(def->def_name, vp->vers_num);
    864 			f_print(fout, ", 0)) {\n");
    865 			(void) sprintf(_errbuf, "unable to register (%s, %s).",
    866 			    def->def_name, vp->vers_name);
    867 			print_err_message("\t\t\t");
    868 			f_print(fout, "\t\t\texit(1);\n");
    869 			f_print(fout, "\t\t}\n");
    870 		}
    871 	}
    872 	if (timerflag) {
    873 		f_print(fout, "\t\tif (pmclose) {\n");
    874 		f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
    875 		    Cflag ? "(SIG_PF)" : "(void(*)())");
    876 		f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
    877 		f_print(fout, "\t\t}\n");
    878 	}
    879 	f_print(fout, "\t\tsvc_run();\n");
    880 	f_print(fout, "\t\texit(1);\n");
    881 	f_print(fout, "\t\t/* NOTREACHED */\n");
    882 	f_print(fout, "\t}\n");
    883 }
    884 /*
    885  * Support for backgrounding the server if self started.
    886  */
    887 static void
    888 write_rpc_svc_fg(infile, sp)
    889 	char   *infile;
    890 	char   *sp;
    891 {
    892 	f_print(fout, "#ifndef RPC_SVC_FG\n");
    893 	f_print(fout, "%sint size;\n", sp);
    894 	if (tirpcflag)
    895 		f_print(fout, "%sstruct rlimit rl;\n", sp);
    896 	if (inetdflag)
    897 		f_print(fout, "%sint pid, i;\n\n", sp);
    898 	f_print(fout, "%spid = fork();\n", sp);
    899 	f_print(fout, "%sif (pid < 0) {\n", sp);
    900 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
    901 	f_print(fout, "%s\texit(1);\n", sp);
    902 	f_print(fout, "%s}\n", sp);
    903 	f_print(fout, "%sif (pid)\n", sp);
    904 	f_print(fout, "%s\texit(0);\n", sp);
    905 	/* get number of file descriptors */
    906 	if (tirpcflag) {
    907 		f_print(fout, "%srl.rlim_max = 0;\n", sp);
    908 		f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
    909 		f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
    910 		f_print(fout, "%s\texit(1);\n", sp);
    911 	} else {
    912 		f_print(fout, "%ssize = getdtablesize();\n", sp);
    913 	}
    914 
    915 	f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
    916 	f_print(fout, "%s\t(void) close(i);\n", sp);
    917 	/* Redirect stderr and stdout to console */
    918 	f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
    919 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
    920 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
    921 	/* This removes control of the controlling terminal */
    922 	if (tirpcflag)
    923 		f_print(fout, "%ssetsid();\n", sp);
    924 	else {
    925 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
    926 		f_print(fout, "%sif (i >= 0) {\n", sp);
    927 		f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
    928 		f_print(fout, "%s\t(void) close(i);\n", sp);
    929 		f_print(fout, "%s}\n", sp);
    930 	}
    931 	if (!logflag)
    932 		open_log_file(infile, sp);
    933 	f_print(fout, "#endif\n");
    934 	if (logflag)
    935 		open_log_file(infile, sp);
    936 }
    937 
    938 static void
    939 open_log_file(infile, sp)
    940 	char   *infile;
    941 	char   *sp;
    942 {
    943 	char   *s, *p;
    944 
    945 	s = strrchr(infile, '.');
    946 	if (s)
    947 		*s = '\0';
    948 	p = strrchr(infile, '/');
    949 	if (p)
    950 		p++;
    951 	else
    952 		p = infile;
    953 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, p);
    954 	if (s)
    955 		*s = '.';
    956 }
    957 
    958 /*
    959  * write a registration for the given transport for Inetd
    960  */
    961 void
    962 write_inetd_register(transp)
    963 	char   *transp;
    964 {
    965 	list   *l;
    966 	definition *def;
    967 	version_list *vp;
    968 	char   *sp;
    969 	int     isudp;
    970 	char    tmpbuf[32];
    971 
    972 	if (inetdflag)
    973 		sp = "\t";
    974 	else
    975 		sp = "";
    976 	if (streq(transp, "udp"))
    977 		isudp = 1;
    978 	else
    979 		isudp = 0;
    980 	f_print(fout, "\n");
    981 	if (inetdflag) {
    982 		f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
    983 		    isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
    984 	}
    985 	if (inetdflag && streq(transp, "tcp")) {
    986 		f_print(fout, "%s\tif (_rpcpmstart)\n", sp);
    987 
    988 		f_print(fout, "%s\t\t%s = svc%s_create(%s",
    989 		    sp, TRANSP, "fd", inetdflag ? "sock" : "RPC_ANYSOCK");
    990 		if (!isudp)
    991 			f_print(fout, ", 0, 0");
    992 		f_print(fout, ");\n");
    993 
    994 		f_print(fout, "%s\telse\n", sp);
    995 
    996 		f_print(fout, "%s\t\t%s = svc%s_create(%s",
    997 		    sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
    998 		if (!isudp)
    999 			f_print(fout, ", 0, 0");
   1000 		f_print(fout, ");\n");
   1001 
   1002 	} else {
   1003 		f_print(fout, "%s\t%s = svc%s_create(%s",
   1004 		    sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
   1005 		if (!isudp)
   1006 			f_print(fout, ", 0, 0");
   1007 		f_print(fout, ");\n");
   1008 	}
   1009 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
   1010 	(void) sprintf(_errbuf, "cannot create %s service.", transp);
   1011 	(void) sprintf(tmpbuf, "%s\t\t", sp);
   1012 	print_err_message(tmpbuf);
   1013 	f_print(fout, "%s\t\texit(1);\n", sp);
   1014 	f_print(fout, "%s\t}\n", sp);
   1015 
   1016 	if (inetdflag) {
   1017 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
   1018 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
   1019 		    sp, isudp ? "UDP" : "TCP");
   1020 	}
   1021 	for (l = defined; l != NULL; l = l->next) {
   1022 		def = (definition *) l->val;
   1023 		if (def->def_kind != DEF_PROGRAM) {
   1024 			continue;
   1025 		}
   1026 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
   1027 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
   1028 			    sp, TRANSP, def->def_name, vp->vers_name);
   1029 			pvname(def->def_name, vp->vers_num);
   1030 			if (inetdflag)
   1031 				f_print(fout, ", proto)) {\n");
   1032 			else
   1033 				f_print(fout, ", IPPROTO_%s)) {\n",
   1034 				    isudp ? "UDP" : "TCP");
   1035 			(void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
   1036 			    def->def_name, vp->vers_name, transp);
   1037 			print_err_message(tmpbuf);
   1038 			f_print(fout, "%s\t\texit(1);\n", sp);
   1039 			f_print(fout, "%s\t}\n", sp);
   1040 		}
   1041 	}
   1042 	if (inetdflag)
   1043 		f_print(fout, "\t}\n");
   1044 }
   1045