xref: /titanic_50/usr/src/cmd/rpcgen/rpc_svcout.c (revision a465d40c6d1ee8281eb10d594fa9da26c7dab707)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6a2f144d1SJordan Brown  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2061961e0fSrobinson  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
23a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
43*a465d40cSJordan Brown #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
457c478bd9Sstevel@tonic-gate #include "rpc_util.h"
467c478bd9Sstevel@tonic-gate 
4761961e0fSrobinson extern int nullproc(proc_list *);
4861961e0fSrobinson 
497c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp";
507c478bd9Sstevel@tonic-gate static char TRANSP[] = "transp";
517c478bd9Sstevel@tonic-gate static char ARG[] = "argument";
527c478bd9Sstevel@tonic-gate static char RESULT[] = "result";
537c478bd9Sstevel@tonic-gate static char ROUTINE[] = "local";
547c478bd9Sstevel@tonic-gate static char RETVAL[] = "retval";
557c478bd9Sstevel@tonic-gate 
5661961e0fSrobinson #define	ERRBUFLEN	256
577c478bd9Sstevel@tonic-gate 
5861961e0fSrobinson static void internal_proctype(proc_list *);
5961961e0fSrobinson static void write_real_program(definition *);
6061961e0fSrobinson static void write_programs(char *);
6161961e0fSrobinson static void write_program(definition *, char *);
6261961e0fSrobinson static void printerr(char *, char *);
6361961e0fSrobinson static void write_svc_aux(int);
6461961e0fSrobinson static void printif(char *, char *, char *, char *);
6561961e0fSrobinson static void write_inetmost(char *);
6661961e0fSrobinson static void print_return(char *);
6761961e0fSrobinson static void print_pmapunset(char *);
68*a465d40cSJordan Brown static void print_err_message(const char *, const char *, ...);
6961961e0fSrobinson static void write_msg_out(void);
7061961e0fSrobinson static void write_timeout_func(void);
7161961e0fSrobinson static void write_pm_most(char *, int);
7261961e0fSrobinson static void write_rpc_svc_fg(char *, char *);
7361961e0fSrobinson static void open_log_file(char *, char *);
747c478bd9Sstevel@tonic-gate 
7561961e0fSrobinson static void
p_xdrfunc(char * rname,char * typename)7661961e0fSrobinson p_xdrfunc(char *rname, char *typename)
777c478bd9Sstevel@tonic-gate {
78*a465d40cSJordan Brown 	if (Cflag) {
79*a465d40cSJordan Brown 		f_print(fout, "\t\t_xdr_%s = (xdrproc_t)\n", rname);
80*a465d40cSJordan Brown 		f_print(fout, "\t\t    xdr_%s;\n", stringfix(typename));
81*a465d40cSJordan Brown 	} else {
827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
837c478bd9Sstevel@tonic-gate 		    rname, stringfix(typename));
847c478bd9Sstevel@tonic-gate 	}
85*a465d40cSJordan Brown }
867c478bd9Sstevel@tonic-gate 
8761961e0fSrobinson static void
internal_proctype(proc_list * plist)8861961e0fSrobinson internal_proctype(proc_list *plist)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	f_print(fout, "static ");
917c478bd9Sstevel@tonic-gate 	ptype(plist->res_prefix, plist->res_type, 1);
927c478bd9Sstevel@tonic-gate 	f_print(fout, "*");
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void
write_mtauto(void)9761961e0fSrobinson write_mtauto(void)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
100*a465d40cSJordan Brown 	print_err_message("\t\t", "unable to set automatic MT mode.");
1017c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n\t}\n");
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * write most of the service, that is, everything but the registrations.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate void
write_most(char * infile,int netflag,int nomain)10861961e0fSrobinson write_most(char *infile, int netflag, int nomain)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag) {
1117c478bd9Sstevel@tonic-gate 		char *var_type;
1127c478bd9Sstevel@tonic-gate 		var_type = (nomain? "extern" : "static");
1137c478bd9Sstevel@tonic-gate 		f_print(fout, "%s int _rpcpmstart;", var_type);
1147c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* Started by a port monitor ? */\n");
1157c478bd9Sstevel@tonic-gate 		if (!tirpcflag) {
1167c478bd9Sstevel@tonic-gate 			f_print(fout, "%s int _rpcfdtype;", var_type);
1177c478bd9Sstevel@tonic-gate 			f_print(fout,
118*a465d40cSJordan Brown 			    "\t\t /* Whether Stream or Datagram ? */\n");
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		if (timerflag) {
1227c478bd9Sstevel@tonic-gate 			f_print(fout,
1237c478bd9Sstevel@tonic-gate "\n/* States a server can be in wrt request */\n\n");
1247c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_IDLE 0\n");
1257c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_SERVED 1\n\n");
126*a465d40cSJordan Brown 			if (nomain) {
127*a465d40cSJordan Brown 				f_print(fout,
128*a465d40cSJordan Brown 				    "/* LINTED static unused if no main */\n");
129*a465d40cSJordan Brown 			}
130*a465d40cSJordan Brown 			f_print(fout,
131*a465d40cSJordan Brown 			    "static int _rpcsvcstate = _IDLE;");
1327c478bd9Sstevel@tonic-gate 			f_print(fout,
1337c478bd9Sstevel@tonic-gate "\t/* Set when a request is serviced */\n");
1347c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvccount = 0;");
1357c478bd9Sstevel@tonic-gate 			f_print(fout,
1367c478bd9Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n");
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 			if (mtflag) {
1397c478bd9Sstevel@tonic-gate 				f_print(fout, "mutex_t _svcstate_lock;");
1407c478bd9Sstevel@tonic-gate 				f_print(fout,
141*a465d40cSJordan Brown "\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 			}
1447c478bd9Sstevel@tonic-gate 		}
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		write_svc_aux(nomain);
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 	/* write out dispatcher and stubs */
14961961e0fSrobinson 	write_programs(nomain ? NULL : "static");
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	if (nomain)
1527c478bd9Sstevel@tonic-gate 		return;
1537c478bd9Sstevel@tonic-gate 
15461961e0fSrobinson 	f_print(fout, "\nint\nmain()\n");
1557c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
1567c478bd9Sstevel@tonic-gate 	if (inetdflag) {
1577c478bd9Sstevel@tonic-gate 		write_inetmost(infile);
1587c478bd9Sstevel@tonic-gate 		/* Includes call to write_rpc_svc_fg() */
1597c478bd9Sstevel@tonic-gate 	} else {
1607c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
1617c478bd9Sstevel@tonic-gate 			if (netflag) {
1627c478bd9Sstevel@tonic-gate 				f_print(fout,
1637c478bd9Sstevel@tonic-gate 				    "\tregister SVCXPRT *%s;\n", TRANSP);
1647c478bd9Sstevel@tonic-gate 				f_print(fout,
1657c478bd9Sstevel@tonic-gate 				    "\tstruct netconfig *nconf = NULL;\n");
1667c478bd9Sstevel@tonic-gate 			}
1677c478bd9Sstevel@tonic-gate 			f_print(fout, "\tpid_t pid;\n");
1687c478bd9Sstevel@tonic-gate 			f_print(fout, "\tint i;\n");
1697c478bd9Sstevel@tonic-gate 			if (mtauto) {
1707c478bd9Sstevel@tonic-gate 				f_print(fout,
1717c478bd9Sstevel@tonic-gate 				    "\tint mode = RPC_SVC_MT_AUTO;\n\n");
1727c478bd9Sstevel@tonic-gate 				write_mtauto();
1737c478bd9Sstevel@tonic-gate 			} else
1747c478bd9Sstevel@tonic-gate 				f_print(fout, "\n");
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 			if (mtflag & timerflag)
1777c478bd9Sstevel@tonic-gate 				f_print(fout,
1787c478bd9Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 			write_pm_most(infile, netflag);
1817c478bd9Sstevel@tonic-gate 			f_print(fout, "\telse {\n");
1827c478bd9Sstevel@tonic-gate 			write_rpc_svc_fg(infile, "\t\t");
1837c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
1847c478bd9Sstevel@tonic-gate 		} else {
1857c478bd9Sstevel@tonic-gate 			f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
1867c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
1877c478bd9Sstevel@tonic-gate 			print_pmapunset("\t");
1887c478bd9Sstevel@tonic-gate 		}
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	if (logflag && !inetdflag) {
1927c478bd9Sstevel@tonic-gate 		open_log_file(infile, "\t");
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate }
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate /*
1977c478bd9Sstevel@tonic-gate  * write a registration for the given transport
1987c478bd9Sstevel@tonic-gate  */
1997c478bd9Sstevel@tonic-gate void
write_netid_register(char * transp)20061961e0fSrobinson write_netid_register(char *transp)
2017c478bd9Sstevel@tonic-gate {
2027c478bd9Sstevel@tonic-gate 	list *l;
2037c478bd9Sstevel@tonic-gate 	definition *def;
2047c478bd9Sstevel@tonic-gate 	version_list *vp;
2057c478bd9Sstevel@tonic-gate 	char *sp;
2067c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	sp = "";
2097c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2107c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
2117c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
21261961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
213*a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot find %s netid.", transp);
2147c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2157c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2167c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
21761961e0fSrobinson 	    sp, TRANSP);
2187c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
219*a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot create %s service.", transp);
2207c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2217c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2247c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2257c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2267c478bd9Sstevel@tonic-gate 			continue;
2277c478bd9Sstevel@tonic-gate 		}
2287c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2297c478bd9Sstevel@tonic-gate 			f_print(fout,
2307c478bd9Sstevel@tonic-gate 			    "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
2317c478bd9Sstevel@tonic-gate 			    sp, def->def_name, vp->vers_name);
2327c478bd9Sstevel@tonic-gate 			f_print(fout,
2337c478bd9Sstevel@tonic-gate 			    "%s\tif (!svc_reg(%s, %s, %s, ",
2347c478bd9Sstevel@tonic-gate 			    sp, TRANSP, def->def_name, vp->vers_name);
2357c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
2367c478bd9Sstevel@tonic-gate 			f_print(fout, ", nconf)) {\n");
237*a465d40cSJordan Brown 			print_err_message(tmpbuf,
2387c478bd9Sstevel@tonic-gate 			    "unable to register (%s, %s, %s).",
2397c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
2407c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
2417c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
2457c478bd9Sstevel@tonic-gate }
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate /*
2487c478bd9Sstevel@tonic-gate  * write a registration for the given transport for TLI
2497c478bd9Sstevel@tonic-gate  */
2507c478bd9Sstevel@tonic-gate void
write_nettype_register(char * transp)25161961e0fSrobinson write_nettype_register(char *transp)
2527c478bd9Sstevel@tonic-gate {
2537c478bd9Sstevel@tonic-gate 	list *l;
2547c478bd9Sstevel@tonic-gate 	definition *def;
2557c478bd9Sstevel@tonic-gate 	version_list *vp;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2587c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2597c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2607c478bd9Sstevel@tonic-gate 			continue;
2617c478bd9Sstevel@tonic-gate 		}
2627c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
263*a465d40cSJordan Brown 			f_print(fout, "\tif (!svc_create(\n");
264*a465d40cSJordan Brown 			f_print(fout, "\t    ");
2657c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
266*a465d40cSJordan Brown 			f_print(fout, ", %s, %s,\n",
267*a465d40cSJordan Brown 			    def->def_name, vp->vers_name);
268*a465d40cSJordan Brown 			f_print(fout, "\t    \"%s\")) {\n", transp);
269*a465d40cSJordan Brown 			print_err_message("\t\t",
2707c478bd9Sstevel@tonic-gate 			    "unable to create (%s, %s) for %s.",
2717c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
2727c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\texit(1);\n");
2737c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
2747c478bd9Sstevel@tonic-gate 		}
2757c478bd9Sstevel@tonic-gate 	}
2767c478bd9Sstevel@tonic-gate }
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate /*
2797c478bd9Sstevel@tonic-gate  * write the rest of the service
2807c478bd9Sstevel@tonic-gate  */
2817c478bd9Sstevel@tonic-gate void
write_rest(void)28261961e0fSrobinson write_rest(void)
2837c478bd9Sstevel@tonic-gate {
2847c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2857c478bd9Sstevel@tonic-gate 	if (inetdflag) {
2867c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
287*a465d40cSJordan Brown 		print_err_message("\t\t", "could not create a handle");
2887c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
2897c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
2907c478bd9Sstevel@tonic-gate 		if (timerflag) {
2917c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_rpcpmstart) {\n");
2927c478bd9Sstevel@tonic-gate 			if (mtflag) {
2937c478bd9Sstevel@tonic-gate 				f_print(fout,
2947c478bd9Sstevel@tonic-gate "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
295*a465d40cSJordan Brown 				print_err_message("\t\t\t",
296*a465d40cSJordan Brown 				    "cannot create closedown thread");
2977c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t\texit(1);\n");
2987c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t}\n");
2997c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3007c478bd9Sstevel@tonic-gate 			} else {
3017c478bd9Sstevel@tonic-gate 				f_print(fout,
3027c478bd9Sstevel@tonic-gate 				"\t\t(void) signal(SIGALRM, %s closedown);\n",
3037c478bd9Sstevel@tonic-gate 				    Cflag? "(SIG_PF)":"(void(*)())");
3047c478bd9Sstevel@tonic-gate 				f_print(fout,
3057c478bd9Sstevel@tonic-gate "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
3067c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 		}
3097c478bd9Sstevel@tonic-gate 	}
3107c478bd9Sstevel@tonic-gate 	f_print(fout, "\tsvc_run();\n");
311*a465d40cSJordan Brown 	print_err_message("\t", "svc_run returned");
3127c478bd9Sstevel@tonic-gate 	f_print(fout, "\texit(1);\n");
3137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/* NOTREACHED */\n");
3147c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
3157c478bd9Sstevel@tonic-gate }
3167c478bd9Sstevel@tonic-gate 
31761961e0fSrobinson static void
write_programs(char * storage)31861961e0fSrobinson write_programs(char *storage)
3197c478bd9Sstevel@tonic-gate {
3207c478bd9Sstevel@tonic-gate 	list *l;
3217c478bd9Sstevel@tonic-gate 	definition *def;
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	/* write out stubs for procedure  definitions */
3247c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3257c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
32661961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3277c478bd9Sstevel@tonic-gate 			write_real_program(def);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/* write out dispatcher for each program */
3317c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3327c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
33361961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3347c478bd9Sstevel@tonic-gate 			write_program(def, storage);
3357c478bd9Sstevel@tonic-gate 	}
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * write out definition of internal function (e.g. _printmsg_1(...))
3427c478bd9Sstevel@tonic-gate  *  which calls server's defintion of actual function (e.g. printmsg_1(...)).
3437c478bd9Sstevel@tonic-gate  *  Unpacks single user argument of printmsg_1 to call-by-value format
3447c478bd9Sstevel@tonic-gate  *  expected by printmsg_1.
3457c478bd9Sstevel@tonic-gate  */
34661961e0fSrobinson static void
write_real_program(definition * def)34761961e0fSrobinson write_real_program(definition *def)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	version_list *vp;
3507c478bd9Sstevel@tonic-gate 	proc_list *proc;
3517c478bd9Sstevel@tonic-gate 	decl_list *l;
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	if (!newstyle)
3547c478bd9Sstevel@tonic-gate 		return;  /* not needed for old style */
3557c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
3567c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
3577c478bd9Sstevel@tonic-gate 			int oneway = streq(proc->res_type, "oneway");
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
360*a465d40cSJordan Brown 			if (proc->arg_num < 2 &&
361*a465d40cSJordan Brown 			    streq(proc->args.decls->decl.type, "void")) {
362*a465d40cSJordan Brown 				f_print(fout, "/* ARGSUSED */\n");
363*a465d40cSJordan Brown 			}
3647c478bd9Sstevel@tonic-gate 			if (!mtflag)
3657c478bd9Sstevel@tonic-gate 				internal_proctype(proc);
3667c478bd9Sstevel@tonic-gate 			else
3677c478bd9Sstevel@tonic-gate 				f_print(fout, "int");
3687c478bd9Sstevel@tonic-gate 			f_print(fout, "\n_");
3697c478bd9Sstevel@tonic-gate 			pvname(proc->proc_name, vp->vers_num);
3707c478bd9Sstevel@tonic-gate 			if (Cflag) {
371*a465d40cSJordan Brown 				f_print(fout, "(\n");
372*a465d40cSJordan Brown 				f_print(fout, "    ");
3737c478bd9Sstevel@tonic-gate 				/* arg name */
3747c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
37561961e0fSrobinson 					/* LINTED variable format */
3767c478bd9Sstevel@tonic-gate 					f_print(fout, proc->args.argname);
3777c478bd9Sstevel@tonic-gate 				else
3787c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
3797c478bd9Sstevel@tonic-gate 					    proc->args.decls->decl.type, 0);
380*a465d40cSJordan Brown 				f_print(fout, " *argp,\n");
3817c478bd9Sstevel@tonic-gate 				if (mtflag) {
382*a465d40cSJordan Brown 					f_print(fout, "    ");
3837c478bd9Sstevel@tonic-gate 					ptype(proc->res_prefix,
3847c478bd9Sstevel@tonic-gate 					    proc->res_type, 1);
385*a465d40cSJordan Brown 					f_print(fout, "*%s,\n", RESULT);
3867c478bd9Sstevel@tonic-gate 				}
387*a465d40cSJordan Brown 				f_print(fout, "    struct svc_req *%s)\n",
3887c478bd9Sstevel@tonic-gate 				    RQSTP);
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 			} else {
3917c478bd9Sstevel@tonic-gate 				if (mtflag)
3927c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s, %s)\n",
3937c478bd9Sstevel@tonic-gate 					    RESULT, RQSTP);
3947c478bd9Sstevel@tonic-gate 				else
3957c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s)\n", RQSTP);
3967c478bd9Sstevel@tonic-gate 				/* arg name */
3977c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
3987c478bd9Sstevel@tonic-gate 					f_print(fout, "\t%s *argp;\n",
3997c478bd9Sstevel@tonic-gate 					    proc->args.argname);
4007c478bd9Sstevel@tonic-gate 				else {
4017c478bd9Sstevel@tonic-gate 					f_print(fout, "\t");
4027c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
4037c478bd9Sstevel@tonic-gate 					    proc->args.decls->decl.type, 0);
4047c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp;\n");
4057c478bd9Sstevel@tonic-gate 				}
4067c478bd9Sstevel@tonic-gate 				if (mtflag)
4077c478bd9Sstevel@tonic-gate 					f_print(fout, "\tvoid *%s;\n", RESULT);
4087c478bd9Sstevel@tonic-gate 				f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate 			f_print(fout, "{\n");
4127c478bd9Sstevel@tonic-gate 			f_print(fout, "\treturn (");
4137c478bd9Sstevel@tonic-gate 			/* for mtflag, arguments are different */
4147c478bd9Sstevel@tonic-gate 			if (Cflag || mtflag)
4157c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
4167c478bd9Sstevel@tonic-gate 			else
4177c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4187c478bd9Sstevel@tonic-gate 			f_print(fout, "(");
4197c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
420*a465d40cSJordan Brown 				/* only print if non-void */
4217c478bd9Sstevel@tonic-gate 				if (!streq(proc->args.decls->decl.type, "void"))
422*a465d40cSJordan Brown 					f_print(fout, "*argp, ");
4237c478bd9Sstevel@tonic-gate 			} else {
424*a465d40cSJordan Brown 				f_print(fout, "\n");
4257c478bd9Sstevel@tonic-gate 				for (l = proc->args.decls;  l != NULL;
4267c478bd9Sstevel@tonic-gate 				    l = l->next)
427*a465d40cSJordan Brown 					f_print(fout, "\t    argp->%s,\n",
4287c478bd9Sstevel@tonic-gate 					    l->decl.name);
429*a465d40cSJordan Brown 				f_print(fout, "\t    ");
4307c478bd9Sstevel@tonic-gate 			}
4317c478bd9Sstevel@tonic-gate 			if (mtflag && !oneway)
4327c478bd9Sstevel@tonic-gate 				f_print(fout, "%s, ", RESULT);
4337c478bd9Sstevel@tonic-gate 			f_print(fout, "%s));\n}\n", RQSTP);
4347c478bd9Sstevel@tonic-gate 		}
4357c478bd9Sstevel@tonic-gate 	}
4367c478bd9Sstevel@tonic-gate }
4377c478bd9Sstevel@tonic-gate 
43861961e0fSrobinson static void
write_program(definition * def,char * storage)43961961e0fSrobinson write_program(definition *def, char *storage)
4407c478bd9Sstevel@tonic-gate {
4417c478bd9Sstevel@tonic-gate 	version_list *vp;
4427c478bd9Sstevel@tonic-gate 	proc_list *proc;
4437c478bd9Sstevel@tonic-gate 	int filled;
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
4467c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
4477c478bd9Sstevel@tonic-gate 		if (storage != NULL) {
4487c478bd9Sstevel@tonic-gate 			f_print(fout, "%s ", storage);
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 		f_print(fout, "void\n");
4517c478bd9Sstevel@tonic-gate 		pvname(def->def_name, vp->vers_num);
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 		if (Cflag) {
4547c478bd9Sstevel@tonic-gate 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
4557c478bd9Sstevel@tonic-gate 			f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
4567c478bd9Sstevel@tonic-gate 		} else {
4577c478bd9Sstevel@tonic-gate 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
4587c478bd9Sstevel@tonic-gate 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
4597c478bd9Sstevel@tonic-gate 			f_print(fout, "	register SVCXPRT *%s;\n", TRANSP);
4607c478bd9Sstevel@tonic-gate 		}
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 		filled = 0;
4657c478bd9Sstevel@tonic-gate 		f_print(fout, "\tunion {\n");
4667c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
4677c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4687c478bd9Sstevel@tonic-gate 				if (streq(proc->args.decls->decl.type,
4697c478bd9Sstevel@tonic-gate 				    "void")) {
4707c478bd9Sstevel@tonic-gate 					continue;
4717c478bd9Sstevel@tonic-gate 				}
4727c478bd9Sstevel@tonic-gate 				filled = 1;
4737c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
4747c478bd9Sstevel@tonic-gate 				ptype(proc->args.decls->decl.prefix,
4757c478bd9Sstevel@tonic-gate 				    proc->args.decls->decl.type, 0);
4767c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4777c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 			} else {
4807c478bd9Sstevel@tonic-gate 				filled = 1;
4817c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t%s", proc->args.argname);
4827c478bd9Sstevel@tonic-gate 				f_print(fout, " ");
4837c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4847c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4857c478bd9Sstevel@tonic-gate 			}
4867c478bd9Sstevel@tonic-gate 		}
4877c478bd9Sstevel@tonic-gate 		if (!filled) {
4887c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tint fill;\n");
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 		f_print(fout, "\t} %s;\n", ARG);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 		if (mtflag) {
4937c478bd9Sstevel@tonic-gate 			filled = 0;
4947c478bd9Sstevel@tonic-gate 			f_print(fout, "\tunion {\n");
4957c478bd9Sstevel@tonic-gate 			for (proc = vp->procs; proc != NULL;
4967c478bd9Sstevel@tonic-gate 			    proc = proc->next) {
4977c478bd9Sstevel@tonic-gate 				if (streq(proc->res_type, "void") ||
4987c478bd9Sstevel@tonic-gate 				    streq(proc->res_type, "oneway"))
4997c478bd9Sstevel@tonic-gate 					continue;
5007c478bd9Sstevel@tonic-gate 				filled = 1;
5017c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
5027c478bd9Sstevel@tonic-gate 				ptype(proc->res_prefix, proc->res_type, 0);
5037c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
5047c478bd9Sstevel@tonic-gate 				f_print(fout, "_res;\n");
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 			if (!filled)
5077c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\tint fill;\n");
5087c478bd9Sstevel@tonic-gate 			f_print(fout, "\t} %s;\n", RESULT);
5097c478bd9Sstevel@tonic-gate 			f_print(fout, "\tbool_t %s;\n", RETVAL);
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 		} else
5127c478bd9Sstevel@tonic-gate 			f_print(fout, "\tchar *%s;\n", RESULT);
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 		if (Cflag) {
5157c478bd9Sstevel@tonic-gate 			f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
5167c478bd9Sstevel@tonic-gate 			    ARG, RESULT);
5177c478bd9Sstevel@tonic-gate 			if (mtflag)
5187c478bd9Sstevel@tonic-gate 				f_print(fout,
5197c478bd9Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
5207c478bd9Sstevel@tonic-gate 				    ROUTINE);
5217c478bd9Sstevel@tonic-gate 			else
5227c478bd9Sstevel@tonic-gate 				f_print(fout,
5237c478bd9Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n",
5247c478bd9Sstevel@tonic-gate 				    ROUTINE);
5257c478bd9Sstevel@tonic-gate 		} else {
5267c478bd9Sstevel@tonic-gate 			f_print(fout,
5277c478bd9Sstevel@tonic-gate 			    "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
5287c478bd9Sstevel@tonic-gate 			    ARG, RESULT);
5297c478bd9Sstevel@tonic-gate 			if (mtflag)
5307c478bd9Sstevel@tonic-gate 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
5317c478bd9Sstevel@tonic-gate 			else
5327c478bd9Sstevel@tonic-gate 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 		if (timerflag) {
5377c478bd9Sstevel@tonic-gate 			if (mtflag)
5387c478bd9Sstevel@tonic-gate 				f_print(fout,
539*a465d40cSJordan Brown "\t(void) mutex_lock(&_svcstate_lock);\n");
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 			f_print(fout, "\t_rpcsvccount++;\n");
5427c478bd9Sstevel@tonic-gate 			if (mtflag)
5437c478bd9Sstevel@tonic-gate 				f_print(fout,
544*a465d40cSJordan Brown "\t(void) mutex_unlock(&_svcstate_lock);\n");
5457c478bd9Sstevel@tonic-gate 		}
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
5487c478bd9Sstevel@tonic-gate 		if (!nullproc(vp->procs)) {
5497c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase NULLPROC:\n");
5507c478bd9Sstevel@tonic-gate 			f_print(fout,
5517c478bd9Sstevel@tonic-gate 			    Cflag ?
552*a465d40cSJordan Brown "\t\t(void) svc_sendreply(%s,\n\t\t    (xdrproc_t)xdr_void, NULL);\n" :
55361961e0fSrobinson "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
5547c478bd9Sstevel@tonic-gate 			    TRANSP);
5557c478bd9Sstevel@tonic-gate 			print_return("\t\t");
5567c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
5577c478bd9Sstevel@tonic-gate 		}
5587c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
5597c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase %s:\n", proc->proc_name);
5607c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
5617c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.decls->decl.type);
5627c478bd9Sstevel@tonic-gate 			} else {
5637c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.argname);
5647c478bd9Sstevel@tonic-gate 			}
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 			if (streq(proc->res_type, "oneway")) {
5677c478bd9Sstevel@tonic-gate 				/* One-way call */
5687c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
5697c478bd9Sstevel@tonic-gate 			} else {
5707c478bd9Sstevel@tonic-gate 				p_xdrfunc(RESULT, proc->res_type);
5717c478bd9Sstevel@tonic-gate 			}
5727c478bd9Sstevel@tonic-gate 			if (Cflag) {
5737c478bd9Sstevel@tonic-gate 				if (mtflag) {
5747c478bd9Sstevel@tonic-gate 					f_print(fout,
5757c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*) (char *,  "
5767c478bd9Sstevel@tonic-gate 					    "void *,  struct svc_req *))",
5777c478bd9Sstevel@tonic-gate 					    ROUTINE);
5787c478bd9Sstevel@tonic-gate 				} else {
5797c478bd9Sstevel@tonic-gate 					f_print(fout,
5807c478bd9Sstevel@tonic-gate 					    "\t\t%s = (char *(*)(char *, "
5817c478bd9Sstevel@tonic-gate 					    "struct svc_req *))",
5827c478bd9Sstevel@tonic-gate 					    ROUTINE);
5837c478bd9Sstevel@tonic-gate 				}
5847c478bd9Sstevel@tonic-gate 			} else {
5857c478bd9Sstevel@tonic-gate 				if (mtflag) {
5867c478bd9Sstevel@tonic-gate 					f_print(fout,
5877c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*)())",
5887c478bd9Sstevel@tonic-gate 					    ROUTINE);
5897c478bd9Sstevel@tonic-gate 				} else {
5907c478bd9Sstevel@tonic-gate 					f_print(fout, "\t\t%s = (char *(*)())",
5917c478bd9Sstevel@tonic-gate 					    ROUTINE);
5927c478bd9Sstevel@tonic-gate 				}
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 
595*a465d40cSJordan Brown 			f_print(fout, "\n\t\t    ");
5967c478bd9Sstevel@tonic-gate 			if (newstyle) { /* new style: calls internal routine */
5977c478bd9Sstevel@tonic-gate 				f_print(fout, "_");
5987c478bd9Sstevel@tonic-gate 			}
5997c478bd9Sstevel@tonic-gate 			if ((Cflag || mtflag) && !newstyle)
6007c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
6017c478bd9Sstevel@tonic-gate 			else
6027c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
6037c478bd9Sstevel@tonic-gate 			f_print(fout, ";\n");
6047c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n\n");
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
6077c478bd9Sstevel@tonic-gate 		printerr("noproc", TRANSP);
6087c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6097c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6107c478bd9Sstevel@tonic-gate 
6117c478bd9Sstevel@tonic-gate 		f_print(fout,
6127c478bd9Sstevel@tonic-gate 		    "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
6137c478bd9Sstevel@tonic-gate 		    ARG, ARG);
6147c478bd9Sstevel@tonic-gate 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
6157c478bd9Sstevel@tonic-gate 		printerr("decode", TRANSP);
6167c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6177c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		if (!mtflag)
6207c478bd9Sstevel@tonic-gate 			if (Cflag)
6217c478bd9Sstevel@tonic-gate 				f_print(fout,
6227c478bd9Sstevel@tonic-gate 				    "\t%s = (*%s)((char *)&%s, %s);\n",
6237c478bd9Sstevel@tonic-gate 				    RESULT, ROUTINE, ARG, RQSTP);
6247c478bd9Sstevel@tonic-gate 			else
6257c478bd9Sstevel@tonic-gate 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
6267c478bd9Sstevel@tonic-gate 				    RESULT, ROUTINE, ARG, RQSTP);
6277c478bd9Sstevel@tonic-gate 		else
6287c478bd9Sstevel@tonic-gate 			if (Cflag)
6297c478bd9Sstevel@tonic-gate 				f_print(fout,
6307c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
6317c478bd9Sstevel@tonic-gate 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6327c478bd9Sstevel@tonic-gate 			else
6337c478bd9Sstevel@tonic-gate 				f_print(fout,
6347c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
6357c478bd9Sstevel@tonic-gate 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 		if (mtflag)
6417c478bd9Sstevel@tonic-gate 			f_print(fout,
642*a465d40cSJordan Brown "\tif (_xdr_%s && %s > 0 &&\n"
643*a465d40cSJordan Brown "\t    !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
6447c478bd9Sstevel@tonic-gate 			    RESULT, RETVAL, TRANSP, RESULT, RESULT);
6457c478bd9Sstevel@tonic-gate 		else
6467c478bd9Sstevel@tonic-gate 			f_print(fout,
647*a465d40cSJordan Brown "\tif (_xdr_%s && %s != NULL &&\n"
648*a465d40cSJordan Brown "\t    !svc_sendreply(%s, _xdr_%s, %s)) {\n",
6497c478bd9Sstevel@tonic-gate 			    RESULT, RESULT, TRANSP, RESULT, RESULT);
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		printerr("systemerr", TRANSP);
6527c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
655*a465d40cSJordan Brown 		print_err_message("\t\t", "unable to free arguments");
6567c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
6577c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6587c478bd9Sstevel@tonic-gate 		/* print out free routine */
6597c478bd9Sstevel@tonic-gate 		if (mtflag) {
6607c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
6617c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tif (!");
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
664*a465d40cSJordan Brown 			f_print(fout, "_freeresult(%s, _xdr_%s,\n",
665*a465d40cSJordan Brown 			    TRANSP, RESULT);
666*a465d40cSJordan Brown 			f_print(fout, "\t\t    (caddr_t)&%s))\n",
667*a465d40cSJordan Brown 			    RESULT);
668*a465d40cSJordan Brown 			print_err_message("\t\t\t", "unable to free results");
6697c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
6707c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
6717c478bd9Sstevel@tonic-gate 		};
6727c478bd9Sstevel@tonic-gate 		print_return("\t");
6737c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
67761961e0fSrobinson static void
printerr(char * err,char * transp)67861961e0fSrobinson printerr(char *err, char *transp)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
68361961e0fSrobinson static void
printif(char * proc,char * transp,char * prefix,char * arg)68461961e0fSrobinson printif(char *proc, char *transp, char *prefix, char *arg)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
6877c478bd9Sstevel@tonic-gate 	    proc, transp, arg, prefix, arg);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
69061961e0fSrobinson int
nullproc(proc_list * proc)69161961e0fSrobinson nullproc(proc_list *proc)
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	for (; proc != NULL; proc = proc->next) {
69461961e0fSrobinson 		if (streq(proc->proc_num, "0"))
6957c478bd9Sstevel@tonic-gate 			return (1);
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate 	return (0);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
70061961e0fSrobinson static void
write_inetmost(char * infile)70161961e0fSrobinson write_inetmost(char *infile)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
7047c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint sock;\n");
7057c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint proto;\n");
7067c478bd9Sstevel@tonic-gate 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
7077c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint asize = sizeof (saddr);\n");
7087c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
7097c478bd9Sstevel@tonic-gate 	f_print(fout,
7107c478bd9Sstevel@tonic-gate 	"\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
7117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
7127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
7137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
715*a465d40cSJordan Brown 	f_print(fout, "\t\t    (char *)&_rpcfdtype, &ssize) == -1)\n");
7167c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7177c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = 0;\n");
7187c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
7197c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tproto = 0;\n");
7207c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
7217c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else {\n");
7227c478bd9Sstevel@tonic-gate 	write_rpc_svc_fg(infile, "\t\t");
7237c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
7247c478bd9Sstevel@tonic-gate 	print_pmapunset("\t\t");
7257c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}\n");
7267c478bd9Sstevel@tonic-gate }
7277c478bd9Sstevel@tonic-gate 
72861961e0fSrobinson static void
print_return(char * space)72961961e0fSrobinson print_return(char *space)
7307c478bd9Sstevel@tonic-gate {
73161961e0fSrobinson 	if (exitnow) {
7327c478bd9Sstevel@tonic-gate 		f_print(fout, "%sexit(0);\n", space);
73361961e0fSrobinson 		return;
73461961e0fSrobinson 	}
7357c478bd9Sstevel@tonic-gate 	if (timerflag) {
736*a465d40cSJordan Brown 		if (mtflag) {
737*a465d40cSJordan Brown 			f_print(fout,
738*a465d40cSJordan Brown 			    "%s(void) mutex_lock(&_svcstate_lock);\n",
7397c478bd9Sstevel@tonic-gate 			    space);
7407c478bd9Sstevel@tonic-gate 		}
741*a465d40cSJordan Brown 		f_print(fout, "%s_rpcsvccount--;\n", space);
742*a465d40cSJordan Brown 		f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
743*a465d40cSJordan Brown 		if (mtflag) {
744*a465d40cSJordan Brown 			f_print(fout,
745*a465d40cSJordan Brown 			    "%s(void) mutex_unlock(&_svcstate_lock);\n",
746*a465d40cSJordan Brown 			    space);
747*a465d40cSJordan Brown 		}
748*a465d40cSJordan Brown 	}
749*a465d40cSJordan Brown 	f_print(fout, "%sreturn; /* CSTYLED */\n", space);
7507c478bd9Sstevel@tonic-gate }
7517c478bd9Sstevel@tonic-gate 
75261961e0fSrobinson static void
print_pmapunset(char * space)75361961e0fSrobinson print_pmapunset(char *space)
7547c478bd9Sstevel@tonic-gate {
7557c478bd9Sstevel@tonic-gate 	list *l;
7567c478bd9Sstevel@tonic-gate 	definition *def;
7577c478bd9Sstevel@tonic-gate 	version_list *vp;
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
7607c478bd9Sstevel@tonic-gate 		def = (definition *)l->val;
7617c478bd9Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
7627c478bd9Sstevel@tonic-gate 			for (vp = def->def.pr.versions; vp != NULL;
7637c478bd9Sstevel@tonic-gate 			    vp = vp->next) {
7647c478bd9Sstevel@tonic-gate 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
7657c478bd9Sstevel@tonic-gate 				    space, def->def_name, vp->vers_name);
7667c478bd9Sstevel@tonic-gate 			}
7677c478bd9Sstevel@tonic-gate 		}
7687c478bd9Sstevel@tonic-gate 	}
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
77161961e0fSrobinson static void
print_err_message(const char * space,const char * fmt,...)772*a465d40cSJordan Brown print_err_message(const char *space, const char *fmt, ...)
7737c478bd9Sstevel@tonic-gate {
774*a465d40cSJordan Brown 	char errbuf[ERRBUFLEN];
775*a465d40cSJordan Brown 	va_list va;
776*a465d40cSJordan Brown 
777*a465d40cSJordan Brown 	va_start(va, fmt);
778*a465d40cSJordan Brown 	(void) vsnprintf(errbuf, sizeof (errbuf), fmt, va);
779*a465d40cSJordan Brown 	va_end(va);
780*a465d40cSJordan Brown 
7817c478bd9Sstevel@tonic-gate 	if (logflag)
782*a465d40cSJordan Brown 		f_print(fout, "%ssyslog(LOG_ERR, \"%%s\",\n", space);
7837c478bd9Sstevel@tonic-gate 	else if (inetdflag || pmflag)
784*a465d40cSJordan Brown 		f_print(fout, "%sRPC_MSGOUT(\"%%s\",\n", space);
7857c478bd9Sstevel@tonic-gate 	else
786*a465d40cSJordan Brown 		f_print(fout, "%sfprintf(stderr, \"%%s\",\n", space);
787*a465d40cSJordan Brown 	f_print(fout, "%s    \"%s\");\n", space, errbuf);
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
791*a465d40cSJordan Brown  * Write the server auxiliary function (RPC_MSGOUT, timeout)
7927c478bd9Sstevel@tonic-gate  */
79361961e0fSrobinson static void
write_svc_aux(int nomain)79461961e0fSrobinson write_svc_aux(int nomain)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	if (!logflag)
7977c478bd9Sstevel@tonic-gate 		write_msg_out();
7987c478bd9Sstevel@tonic-gate 	if (!nomain)
7997c478bd9Sstevel@tonic-gate 		write_timeout_func();
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate /*
803*a465d40cSJordan Brown  * Write the RPC_MSGOUT function
804*a465d40cSJordan Brown  *
805*a465d40cSJordan Brown  * Note that while we define RPC_MSGOUT to be printf-like, all existing
806*a465d40cSJordan Brown  * calls are of the form "%s","<msg>" and this implementation assumes that
807*a465d40cSJordan Brown  * trivial case.  If in the future it's desirable to generate richer calls
808*a465d40cSJordan Brown  * this implementation can change to match.  This way we don't (yet) have
809*a465d40cSJordan Brown  * to introduce varargs into the generated code.
8107c478bd9Sstevel@tonic-gate  */
81161961e0fSrobinson static void
write_msg_out(void)81261961e0fSrobinson write_msg_out(void)
8137c478bd9Sstevel@tonic-gate {
8147c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
815*a465d40cSJordan Brown 	f_print(fout, "#if\tdefined(RPC_MSGOUT)\n");
816*a465d40cSJordan Brown 	if (!Cflag) {
817*a465d40cSJordan Brown 		f_print(fout, "extern void RPC_MSGOUT();\n");
818*a465d40cSJordan Brown 	} else {
819*a465d40cSJordan Brown 		f_print(fout, "extern void RPC_MSGOUT(const char *, ...);\n");
820*a465d40cSJordan Brown 	}
821*a465d40cSJordan Brown 	f_print(fout, "#else\t/* defined(RPC_MSGOUT) */\n");
82261961e0fSrobinson 	f_print(fout, "static ");
8237c478bd9Sstevel@tonic-gate 	if (!Cflag) {
824*a465d40cSJordan Brown 		f_print(fout, "void\nRPC_MSGOUT(fmt, msg)\n");
825*a465d40cSJordan Brown 		f_print(fout, "\tchar *fmt;\n");
8267c478bd9Sstevel@tonic-gate 		f_print(fout, "\tchar *msg;\n");
8277c478bd9Sstevel@tonic-gate 	} else {
828*a465d40cSJordan Brown 		f_print(fout, "void\nRPC_MSGOUT(const char *fmt, char *msg)\n");
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8317c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifdef RPC_SVC_FG\n");
8327c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag)
8337c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (_rpcpmstart)\n");
834*a465d40cSJordan Brown 	f_print(fout, "\t\tsyslog(LOG_ERR, fmt, msg);\n");
835*a465d40cSJordan Brown 	f_print(fout, "\telse {\n");
836*a465d40cSJordan Brown 	f_print(fout, "\t\t(void) fprintf(stderr, fmt, msg);\n");
837*a465d40cSJordan Brown 	f_print(fout, "\t\t(void) putc('\\n', stderr);\n");
838*a465d40cSJordan Brown 	f_print(fout, "\t}\n");
8397c478bd9Sstevel@tonic-gate 	f_print(fout, "#else\n");
840*a465d40cSJordan Brown 	f_print(fout, "\tsyslog(LOG_ERR, fmt, msg);\n");
8417c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
8427c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
843*a465d40cSJordan Brown 	f_print(fout, "#endif\t/* defined(RPC_MSGOUT) */\n");
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Write the timeout function
8487c478bd9Sstevel@tonic-gate  */
84961961e0fSrobinson static void
write_timeout_func(void)85061961e0fSrobinson write_timeout_func(void)
8517c478bd9Sstevel@tonic-gate {
8527c478bd9Sstevel@tonic-gate 	if (!timerflag)
8537c478bd9Sstevel@tonic-gate 		return;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
8567c478bd9Sstevel@tonic-gate 	if (mtflag) {
8577c478bd9Sstevel@tonic-gate 		f_print(fout, "/*ARGSUSED*/\n");
8587c478bd9Sstevel@tonic-gate 		f_print(fout, "static void *\n");
8597c478bd9Sstevel@tonic-gate 		if (!Cflag) {
8607c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(arg)\n");
8617c478bd9Sstevel@tonic-gate 			f_print(fout, "\tvoid *arg;\n");
8627c478bd9Sstevel@tonic-gate 		} else
8637c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(void *arg)\n");
8647c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
8657c478bd9Sstevel@tonic-gate 		f_print(fout, "\t/*CONSTCOND*/\n");
8667c478bd9Sstevel@tonic-gate 		f_print(fout, "\twhile (1) {\n");
8677c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
8687c478bd9Sstevel@tonic-gate 		f_print(fout,
8697c478bd9Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
8707c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tcontinue;\n\n");
8717c478bd9Sstevel@tonic-gate 		f_print(fout,
8727c478bd9Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8737c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8747c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tint size;\n");
8757c478bd9Sstevel@tonic-gate 		} else {
8767c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
8777c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tstatic int size;\n");
8787c478bd9Sstevel@tonic-gate 		}
8797c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
8807c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8817c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
8827c478bd9Sstevel@tonic-gate 		} else {
8837c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tif (size == 0) {\n");
8847c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
8857c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
8867c478bd9Sstevel@tonic-gate 		}
8877c478bd9Sstevel@tonic-gate 		f_print(fout,
8887c478bd9Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
8897c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8907c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
8917c478bd9Sstevel@tonic-gate 		} else {
8927c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
8937c478bd9Sstevel@tonic-gate 		}
8947c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\t\topenfd++;\n");
8957c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (openfd <= 1)\n");
8967c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\texit(0);\n");
8977c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t} else\n");
8987c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
899*a465d40cSJordan Brown 		f_print(fout, "\t\t(void) mutex_unlock(&_svcstate_lock);\n");
9007c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
9017c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
9027c478bd9Sstevel@tonic-gate 		return;
9037c478bd9Sstevel@tonic-gate 	}
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	f_print(fout, "static void\n");
9067c478bd9Sstevel@tonic-gate 	if (!Cflag) {
9077c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(sig)\n");
9087c478bd9Sstevel@tonic-gate 		f_print(fout, "\tint sig;\n");
9097c478bd9Sstevel@tonic-gate 	} else
9107c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(int sig)\n");
9117c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
9127c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
9137c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9147c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint size;\n");
9157c478bd9Sstevel@tonic-gate 	} else {
9167c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\textern fd_set svc_fdset;\n");
9177c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstatic int size;\n");
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint i, openfd = 0;\n\n");
9207c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9217c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tsize = svc_max_pollfd;\n");
9227c478bd9Sstevel@tonic-gate 	} else {
9237c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (size == 0) {\n");
9247c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
9257c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
9267c478bd9Sstevel@tonic-gate 	}
9277c478bd9Sstevel@tonic-gate 	f_print(fout,
9287c478bd9Sstevel@tonic-gate 	    "\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
9297c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9307c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
9317c478bd9Sstevel@tonic-gate 	} else {
9327c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\topenfd++;\n");
9357c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (openfd <= 1)\n");
9367c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(0);\n");
9377c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else\n");
9387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
9417c478bd9Sstevel@tonic-gate 	    Cflag? "(SIG_PF)" : "(void(*)())");
9427c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
9437c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
9447c478bd9Sstevel@tonic-gate }
9457c478bd9Sstevel@tonic-gate 
9467c478bd9Sstevel@tonic-gate /*
9477c478bd9Sstevel@tonic-gate  * Write the most of port monitor support
9487c478bd9Sstevel@tonic-gate  */
94961961e0fSrobinson static void
write_pm_most(char * infile,int netflag)95061961e0fSrobinson write_pm_most(char *infile, int netflag)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	list *l;
9537c478bd9Sstevel@tonic-gate 	definition *def;
9547c478bd9Sstevel@tonic-gate 	version_list *vp;
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
9577c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/*\n");
9587c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
9597c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * that we were started by a port monitor. If\n");
9607c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
9617c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * TLI endpoint.\n");
9627c478bd9Sstevel@tonic-gate 	f_print(fout, "\t */\n");
9637c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
9647c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tchar *netid;\n");
9657c478bd9Sstevel@tonic-gate 	if (!netflag) {	/* Not included by -n option */
9667c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
9677c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
9687c478bd9Sstevel@tonic-gate 	}
9697c478bd9Sstevel@tonic-gate 	if (timerflag)
9707c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint pmclose;\n");
9717c478bd9Sstevel@tonic-gate /*
9727c478bd9Sstevel@tonic-gate  *  Not necessary, defined in /usr/include/stdlib
9737c478bd9Sstevel@tonic-gate  *  f_print(fout, "\t\textern char *getenv();\n");
9747c478bd9Sstevel@tonic-gate  */
9757c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
9767c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
9777c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
9787c478bd9Sstevel@tonic-gate 	f_print(fout,
9797c478bd9Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	if (timerflag) {
9827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* started from inetd */\n");
9837c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tpmclose = 1;\n");
9847c478bd9Sstevel@tonic-gate 	}
98561961e0fSrobinson 	f_print(fout, "\t\t} else {\n");
9867c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
987*a465d40cSJordan Brown 	print_err_message("\t\t\t\t", "cannot get transport info");
9887c478bd9Sstevel@tonic-gate 	if (timerflag)
9897c478bd9Sstevel@tonic-gate 		f_print(fout,
9907c478bd9Sstevel@tonic-gate 		    "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
9917c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9927c478bd9Sstevel@tonic-gate 	f_print(fout,
9937c478bd9Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
9947c478bd9Sstevel@tonic-gate 	    TRANSP);
995*a465d40cSJordan Brown 	print_err_message("\t\t\t", "cannot create server handle");
9967c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
9977c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9987c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (nconf)\n");
9997c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
10007c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
10017c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
10027c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
10037c478bd9Sstevel@tonic-gate 			continue;
10047c478bd9Sstevel@tonic-gate 		}
10057c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
10067c478bd9Sstevel@tonic-gate 			f_print(fout,
1007*a465d40cSJordan Brown 			    "\t\tif (!svc_reg(%s, %s, %s,\n",
10087c478bd9Sstevel@tonic-gate 			    TRANSP, def->def_name, vp->vers_name);
1009*a465d40cSJordan Brown 			f_print(fout, "\t\t    ");
10107c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
10117c478bd9Sstevel@tonic-gate 			f_print(fout, ", 0)) {\n");
1012*a465d40cSJordan Brown 			print_err_message("\t\t\t",
101361961e0fSrobinson 			    "unable to register (%s, %s).",
10147c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name);
10157c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\texit(1);\n");
10167c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t}\n");
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 	}
10197c478bd9Sstevel@tonic-gate 	if (timerflag) {
10207c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (pmclose) {\n");
10217c478bd9Sstevel@tonic-gate 		if (mtflag) {
10227c478bd9Sstevel@tonic-gate 			f_print(fout,
1023*a465d40cSJordan Brown "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t    0, NULL) != 0) {\n");
1024*a465d40cSJordan Brown 			print_err_message("\t\t\t\t",
1025*a465d40cSJordan Brown 			    "cannot create closedown thread");
10267c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\texit(1);\n");
10277c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
10287c478bd9Sstevel@tonic-gate 		} else {
10297c478bd9Sstevel@tonic-gate 			f_print(fout,
10307c478bd9Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
10317c478bd9Sstevel@tonic-gate 			    Cflag? "(SIG_PF)" : "(void(*)())");
10327c478bd9Sstevel@tonic-gate 			f_print(fout,
10337c478bd9Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvc_run();\n");
10387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n");
10397c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t/* NOTREACHED */\n");
10407c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}");
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate /*
10447c478bd9Sstevel@tonic-gate  * Support for backgrounding the server if self started.
10457c478bd9Sstevel@tonic-gate  */
104661961e0fSrobinson static void
write_rpc_svc_fg(char * infile,char * sp)104761961e0fSrobinson write_rpc_svc_fg(char *infile, char *sp)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef RPC_SVC_FG\n");
10507c478bd9Sstevel@tonic-gate 	f_print(fout, "#pragma weak closefrom\n");
10517c478bd9Sstevel@tonic-gate 	f_print(fout, "%sextern void closefrom();\n", sp);
10527c478bd9Sstevel@tonic-gate 	f_print(fout, "%sint size;\n", sp);
10537c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10547c478bd9Sstevel@tonic-gate 		f_print(fout, "%sstruct rlimit rl;\n", sp);
10557c478bd9Sstevel@tonic-gate 	if (inetdflag)
10567c478bd9Sstevel@tonic-gate 		f_print(fout, "%sint pid, i;\n\n", sp);
10577c478bd9Sstevel@tonic-gate 	f_print(fout, "%spid = fork();\n", sp);
10587c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid < 0) {\n", sp);
10597c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
10607c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(1);\n", sp);
10617c478bd9Sstevel@tonic-gate 	f_print(fout, "%s}\n", sp);
10627c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid)\n", sp);
10637c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(0);\n", sp);
10647c478bd9Sstevel@tonic-gate 	/* close all file descriptors */
10657c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
10667c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (closefrom != NULL)\n", sp);
10677c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tclosefrom(0);\n", sp);
10687c478bd9Sstevel@tonic-gate 		f_print(fout, "%selse {\n", sp);
10697c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
10707c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
10717c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
10727c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\texit(1);\n", sp);
10737c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10747c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10757c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10767c478bd9Sstevel@tonic-gate 	} else {
10777c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tsize = getdtablesize();\n", sp);
10787c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10797c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10807c478bd9Sstevel@tonic-gate 	}
10817c478bd9Sstevel@tonic-gate 	/* Redirect stderr and stdout to /dev/null */
10827c478bd9Sstevel@tonic-gate 	f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
10837c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
10847c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
10857c478bd9Sstevel@tonic-gate 	/* This removes control of the controlling terminal */
10867c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10877c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssetsid();\n", sp);
10887c478bd9Sstevel@tonic-gate 	else {
10897c478bd9Sstevel@tonic-gate 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
10907c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (i >= 0) {\n", sp);
10917c478bd9Sstevel@tonic-gate 		f_print(fout,
10927c478bd9Sstevel@tonic-gate 		    "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
10937c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t(void) close(i);\n", sp);
10947c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10957c478bd9Sstevel@tonic-gate 	}
10967c478bd9Sstevel@tonic-gate 	if (!logflag)
10977c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10987c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
10997c478bd9Sstevel@tonic-gate 	if (logflag)
11007c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
11017c478bd9Sstevel@tonic-gate }
11027c478bd9Sstevel@tonic-gate 
110361961e0fSrobinson static void
open_log_file(char * infile,char * sp)110461961e0fSrobinson open_log_file(char *infile, char *sp)
11057c478bd9Sstevel@tonic-gate {
11067c478bd9Sstevel@tonic-gate 	char *s;
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate 	s = strrchr(infile, '.');
11097c478bd9Sstevel@tonic-gate 	if (s)
11107c478bd9Sstevel@tonic-gate 		*s = '\0';
11117c478bd9Sstevel@tonic-gate 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
11127c478bd9Sstevel@tonic-gate 	if (s)
11137c478bd9Sstevel@tonic-gate 		*s = '.';
11147c478bd9Sstevel@tonic-gate }
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate /*
11177c478bd9Sstevel@tonic-gate  * write a registration for the given transport for Inetd
11187c478bd9Sstevel@tonic-gate  */
11197c478bd9Sstevel@tonic-gate void
write_inetd_register(char * transp)112061961e0fSrobinson write_inetd_register(char *transp)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	list *l;
11237c478bd9Sstevel@tonic-gate 	definition *def;
11247c478bd9Sstevel@tonic-gate 	version_list *vp;
11257c478bd9Sstevel@tonic-gate 	char *sp;
11267c478bd9Sstevel@tonic-gate 	int isudp;
11277c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (inetdflag)
11307c478bd9Sstevel@tonic-gate 		sp = "\t";
11317c478bd9Sstevel@tonic-gate 	else
11327c478bd9Sstevel@tonic-gate 		sp = "";
11337c478bd9Sstevel@tonic-gate 	if (streq(transp, "udp"))
11347c478bd9Sstevel@tonic-gate 		isudp = 1;
11357c478bd9Sstevel@tonic-gate 	else
11367c478bd9Sstevel@tonic-gate 		isudp = 0;
11377c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
11387c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11397c478bd9Sstevel@tonic-gate 		f_print(fout,
11407c478bd9Sstevel@tonic-gate 		    "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
11417c478bd9Sstevel@tonic-gate 		    isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
11427c478bd9Sstevel@tonic-gate 	}
11437c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc%s_create(%s",
11447c478bd9Sstevel@tonic-gate 	    sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
11457c478bd9Sstevel@tonic-gate 	if (!isudp)
11467c478bd9Sstevel@tonic-gate 		f_print(fout, ", 0, 0");
11477c478bd9Sstevel@tonic-gate 	f_print(fout, ");\n");
11487c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
114961961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
1150*a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot create %s service.", transp);
11517c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
11527c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
11537c478bd9Sstevel@tonic-gate 
11547c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11557c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
11567c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
11577c478bd9Sstevel@tonic-gate 		    sp, isudp ? "UDP": "TCP");
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
11607c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
116161961e0fSrobinson 		if (def->def_kind != DEF_PROGRAM)
11627c478bd9Sstevel@tonic-gate 			continue;
11637c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1164*a465d40cSJordan Brown 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s,\n",
11657c478bd9Sstevel@tonic-gate 			    sp, TRANSP, def->def_name, vp->vers_name);
1166*a465d40cSJordan Brown 			f_print(fout, "%s\t    ", sp);
11677c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
11687c478bd9Sstevel@tonic-gate 			if (inetdflag)
11697c478bd9Sstevel@tonic-gate 				f_print(fout, ", proto)) {\n");
11707c478bd9Sstevel@tonic-gate 			else
11717c478bd9Sstevel@tonic-gate 				f_print(fout, ", IPPROTO_%s)) {\n",
11727c478bd9Sstevel@tonic-gate 				    isudp ? "UDP": "TCP");
1173*a465d40cSJordan Brown 			print_err_message(tmpbuf,
11747c478bd9Sstevel@tonic-gate 			    "unable to register (%s, %s, %s).",
11757c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
11767c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
11777c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
11787c478bd9Sstevel@tonic-gate 		}
11797c478bd9Sstevel@tonic-gate 	}
11807c478bd9Sstevel@tonic-gate 	if (inetdflag)
11817c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
11827c478bd9Sstevel@tonic-gate }
1183