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