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