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