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