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