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