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