xref: /titanic_52/usr/src/cmd/rpcgen/rpc_svcout.c (revision 61961e0f20c7637a3846bb39786bb9dffa91dfb9)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*61961e0fSrobinson  */
22*61961e0fSrobinson 
23*61961e0fSrobinson /*
24*61961e0fSrobinson  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
257c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
287c478bd9Sstevel@tonic-gate /* All Rights Reserved */
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
317c478bd9Sstevel@tonic-gate  * The Regents of the University of California
327c478bd9Sstevel@tonic-gate  * All Rights Reserved
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
357c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
367c478bd9Sstevel@tonic-gate  * contributors.
377c478bd9Sstevel@tonic-gate  */
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate /*
427c478bd9Sstevel@tonic-gate  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate #include <stdio.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
477c478bd9Sstevel@tonic-gate #include "rpc_util.h"
487c478bd9Sstevel@tonic-gate 
49*61961e0fSrobinson extern int nullproc(proc_list *);
50*61961e0fSrobinson 
517c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp";
527c478bd9Sstevel@tonic-gate static char TRANSP[] = "transp";
537c478bd9Sstevel@tonic-gate static char ARG[] = "argument";
547c478bd9Sstevel@tonic-gate static char RESULT[] = "result";
557c478bd9Sstevel@tonic-gate static char ROUTINE[] = "local";
567c478bd9Sstevel@tonic-gate static char RETVAL[] = "retval";
577c478bd9Sstevel@tonic-gate 
58*61961e0fSrobinson #define	ERRBUFLEN	256
59*61961e0fSrobinson char _errbuf[ERRBUFLEN];	/* For all messages */
607c478bd9Sstevel@tonic-gate 
61*61961e0fSrobinson static void internal_proctype(proc_list *);
62*61961e0fSrobinson static void write_real_program(definition *);
63*61961e0fSrobinson static void write_programs(char *);
64*61961e0fSrobinson static void write_program(definition *, char *);
65*61961e0fSrobinson static void printerr(char *, char *);
66*61961e0fSrobinson static void write_svc_aux(int);
67*61961e0fSrobinson static void printif(char *, char *, char *, char *);
68*61961e0fSrobinson static void write_inetmost(char *);
69*61961e0fSrobinson static void print_return(char *);
70*61961e0fSrobinson static void print_pmapunset(char *);
71*61961e0fSrobinson static void print_err_message(char *);
72*61961e0fSrobinson static void write_msg_out(void);
73*61961e0fSrobinson static void write_timeout_func(void);
74*61961e0fSrobinson static void write_pm_most(char *, int);
75*61961e0fSrobinson static void write_rpc_svc_fg(char *, char *);
76*61961e0fSrobinson static void open_log_file(char *, char *);
777c478bd9Sstevel@tonic-gate 
78*61961e0fSrobinson static void
79*61961e0fSrobinson p_xdrfunc(char *rname, char *typename)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate 	if (Cflag)
827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = (xdrproc_t)xdr_%s;\n",
837c478bd9Sstevel@tonic-gate 			rname, stringfix(typename));
847c478bd9Sstevel@tonic-gate 	else
857c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
867c478bd9Sstevel@tonic-gate 			rname, stringfix(typename));
877c478bd9Sstevel@tonic-gate }
887c478bd9Sstevel@tonic-gate 
89*61961e0fSrobinson static void
90*61961e0fSrobinson internal_proctype(proc_list *plist)
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	f_print(fout, "static ");
937c478bd9Sstevel@tonic-gate 	ptype(plist->res_prefix, plist->res_type, 1);
947c478bd9Sstevel@tonic-gate 	f_print(fout, "*");
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate static void
99*61961e0fSrobinson write_mtauto(void)
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
1027c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_msgout(\"%s\");\n",
1037c478bd9Sstevel@tonic-gate 				"unable to set automatic MT mode.");
1047c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n\t}\n");
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * write most of the service, that is, everything but the registrations.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate void
111*61961e0fSrobinson write_most(char *infile, int netflag, int nomain)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag) {
1147c478bd9Sstevel@tonic-gate 		char *var_type;
1157c478bd9Sstevel@tonic-gate 		var_type = (nomain? "extern" : "static");
1167c478bd9Sstevel@tonic-gate 		f_print(fout, "%s int _rpcpmstart;", var_type);
1177c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* Started by a port monitor ? */\n");
1187c478bd9Sstevel@tonic-gate 		if (!tirpcflag) {
1197c478bd9Sstevel@tonic-gate 			f_print(fout, "%s int _rpcfdtype;", var_type);
1207c478bd9Sstevel@tonic-gate 			f_print(fout,
1217c478bd9Sstevel@tonic-gate "\n\t\t /* Whether Stream or Datagram ? */\n");
1227c478bd9Sstevel@tonic-gate 		}
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 		if (timerflag) {
1257c478bd9Sstevel@tonic-gate 			f_print(fout,
1267c478bd9Sstevel@tonic-gate "\n/* States a server can be in wrt request */\n\n");
1277c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_IDLE 0\n");
1287c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_SERVED 1\n\n");
1297c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvcstate = _IDLE;");
1307c478bd9Sstevel@tonic-gate 			f_print(fout,
1317c478bd9Sstevel@tonic-gate "\t/* Set when a request is serviced */\n");
1327c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvccount = 0;");
1337c478bd9Sstevel@tonic-gate 			f_print(fout,
1347c478bd9Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n");
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate 			if (mtflag) {
1377c478bd9Sstevel@tonic-gate 				f_print(fout, "mutex_t _svcstate_lock;");
1387c478bd9Sstevel@tonic-gate 				f_print(fout,
1397c478bd9Sstevel@tonic-gate "\t\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 			}
1427c478bd9Sstevel@tonic-gate 		}
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 		write_svc_aux(nomain);
1457c478bd9Sstevel@tonic-gate 	}
1467c478bd9Sstevel@tonic-gate 	/* write out dispatcher and stubs */
147*61961e0fSrobinson 	write_programs(nomain ? NULL : "static");
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (nomain)
1507c478bd9Sstevel@tonic-gate 		return;
1517c478bd9Sstevel@tonic-gate 
152*61961e0fSrobinson 	f_print(fout, "\nint\nmain()\n");
1537c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
1547c478bd9Sstevel@tonic-gate 	if (inetdflag) {
1557c478bd9Sstevel@tonic-gate 		write_inetmost(infile);
1567c478bd9Sstevel@tonic-gate 		/* Includes call to write_rpc_svc_fg() */
1577c478bd9Sstevel@tonic-gate 	} else {
1587c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
1597c478bd9Sstevel@tonic-gate 			if (netflag) {
1607c478bd9Sstevel@tonic-gate 				f_print(fout,
1617c478bd9Sstevel@tonic-gate 					"\tregister SVCXPRT *%s;\n", TRANSP);
1627c478bd9Sstevel@tonic-gate 				f_print(fout,
1637c478bd9Sstevel@tonic-gate 					"\tstruct netconfig *nconf = NULL;\n");
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			f_print(fout, "\tpid_t pid;\n");
1667c478bd9Sstevel@tonic-gate 			f_print(fout, "\tint i;\n");
1677c478bd9Sstevel@tonic-gate 			if (mtauto) {
1687c478bd9Sstevel@tonic-gate 				f_print(fout,
1697c478bd9Sstevel@tonic-gate 					"\tint mode = RPC_SVC_MT_AUTO;\n\n");
1707c478bd9Sstevel@tonic-gate 				write_mtauto();
1717c478bd9Sstevel@tonic-gate 			} else
1727c478bd9Sstevel@tonic-gate 				f_print(fout, "\n");
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 			if (mtflag & timerflag)
1757c478bd9Sstevel@tonic-gate 				f_print(fout,
1767c478bd9Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 			write_pm_most(infile, netflag);
1797c478bd9Sstevel@tonic-gate 			f_print(fout, "\telse {\n");
1807c478bd9Sstevel@tonic-gate 			write_rpc_svc_fg(infile, "\t\t");
1817c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
1827c478bd9Sstevel@tonic-gate 		} else {
1837c478bd9Sstevel@tonic-gate 			f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
1847c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
1857c478bd9Sstevel@tonic-gate 			print_pmapunset("\t");
1867c478bd9Sstevel@tonic-gate 		}
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	if (logflag && !inetdflag) {
1907c478bd9Sstevel@tonic-gate 		open_log_file(infile, "\t");
1917c478bd9Sstevel@tonic-gate 	}
1927c478bd9Sstevel@tonic-gate }
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate /*
1957c478bd9Sstevel@tonic-gate  * write a registration for the given transport
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate void
198*61961e0fSrobinson write_netid_register(char *transp)
1997c478bd9Sstevel@tonic-gate {
2007c478bd9Sstevel@tonic-gate 	list *l;
2017c478bd9Sstevel@tonic-gate 	definition *def;
2027c478bd9Sstevel@tonic-gate 	version_list *vp;
2037c478bd9Sstevel@tonic-gate 	char *sp;
2047c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	sp = "";
2077c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2087c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
2097c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
210*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot find %s netid.", transp);
211*61961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
2127c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
2137c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2147c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2157c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
216*61961e0fSrobinson 			sp, TRANSP);
2177c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
218*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create %s service.",
219*61961e0fSrobinson 								transp);
2207c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
2217c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2227c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2257c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2267c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2277c478bd9Sstevel@tonic-gate 			continue;
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2307c478bd9Sstevel@tonic-gate 			f_print(fout,
2317c478bd9Sstevel@tonic-gate 				"%s\t(void) rpcb_unset(%s, %s, nconf);\n",
2327c478bd9Sstevel@tonic-gate 				sp, def->def_name, vp->vers_name);
2337c478bd9Sstevel@tonic-gate 			f_print(fout,
2347c478bd9Sstevel@tonic-gate 				"%s\tif (!svc_reg(%s, %s, %s, ",
2357c478bd9Sstevel@tonic-gate 				sp, TRANSP, def->def_name, vp->vers_name);
2367c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
2377c478bd9Sstevel@tonic-gate 			f_print(fout, ", nconf)) {\n");
238*61961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
2397c478bd9Sstevel@tonic-gate 				"unable to register (%s, %s, %s).",
2407c478bd9Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
2417c478bd9Sstevel@tonic-gate 			print_err_message(tmpbuf);
2427c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
2437c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
2447c478bd9Sstevel@tonic-gate 		}
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
2477c478bd9Sstevel@tonic-gate }
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate /*
2507c478bd9Sstevel@tonic-gate  * write a registration for the given transport for TLI
2517c478bd9Sstevel@tonic-gate  */
2527c478bd9Sstevel@tonic-gate void
253*61961e0fSrobinson write_nettype_register(char *transp)
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	list *l;
2567c478bd9Sstevel@tonic-gate 	definition *def;
2577c478bd9Sstevel@tonic-gate 	version_list *vp;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2607c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2617c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2627c478bd9Sstevel@tonic-gate 			continue;
2637c478bd9Sstevel@tonic-gate 		}
2647c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2657c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (!svc_create(");
2667c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
2677c478bd9Sstevel@tonic-gate 			f_print(fout, ", %s, %s, \"%s\")) {\n",
2687c478bd9Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
269*61961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
2707c478bd9Sstevel@tonic-gate 				"unable to create (%s, %s) for %s.",
2717c478bd9Sstevel@tonic-gate 					def->def_name, vp->vers_name, transp);
2727c478bd9Sstevel@tonic-gate 			print_err_message("\t\t");
2737c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\texit(1);\n");
2747c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
2757c478bd9Sstevel@tonic-gate 		}
2767c478bd9Sstevel@tonic-gate 	}
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate /*
2807c478bd9Sstevel@tonic-gate  * write the rest of the service
2817c478bd9Sstevel@tonic-gate  */
2827c478bd9Sstevel@tonic-gate void
283*61961e0fSrobinson write_rest(void)
2847c478bd9Sstevel@tonic-gate {
2857c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2867c478bd9Sstevel@tonic-gate 	if (inetdflag) {
2877c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
288*61961e0fSrobinson 		(void) snprintf(_errbuf, ERRBUFLEN,
289*61961e0fSrobinson 						"could not create a handle");
2907c478bd9Sstevel@tonic-gate 		print_err_message("\t\t");
2917c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
2927c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
2937c478bd9Sstevel@tonic-gate 		if (timerflag) {
2947c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_rpcpmstart) {\n");
2957c478bd9Sstevel@tonic-gate 			if (mtflag) {
2967c478bd9Sstevel@tonic-gate 				f_print(fout,
2977c478bd9Sstevel@tonic-gate "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
2987c478bd9Sstevel@tonic-gate 				f_print(fout,
2997c478bd9Sstevel@tonic-gate "\t\t\t_msgout(\"cannot create closedown thread\");\n");
3007c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t\texit(1);\n");
3017c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t}\n");
3027c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3037c478bd9Sstevel@tonic-gate 			} else {
3047c478bd9Sstevel@tonic-gate 				f_print(fout,
3057c478bd9Sstevel@tonic-gate 				"\t\t(void) signal(SIGALRM, %s closedown);\n",
3067c478bd9Sstevel@tonic-gate 					Cflag? "(SIG_PF)":"(void(*)())");
3077c478bd9Sstevel@tonic-gate 				f_print(fout,
3087c478bd9Sstevel@tonic-gate "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
3097c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3107c478bd9Sstevel@tonic-gate 			}
3117c478bd9Sstevel@tonic-gate 		}
3127c478bd9Sstevel@tonic-gate 	}
3137c478bd9Sstevel@tonic-gate 	f_print(fout, "\tsvc_run();\n");
314*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "svc_run returned");
3157c478bd9Sstevel@tonic-gate 	print_err_message("\t");
3167c478bd9Sstevel@tonic-gate 	f_print(fout, "\texit(1);\n");
3177c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/* NOTREACHED */\n");
3187c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
321*61961e0fSrobinson static void
322*61961e0fSrobinson write_programs(char *storage)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	list *l;
3257c478bd9Sstevel@tonic-gate 	definition *def;
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate 	/* write out stubs for procedure  definitions */
3287c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3297c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
330*61961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3317c478bd9Sstevel@tonic-gate 			write_real_program(def);
3327c478bd9Sstevel@tonic-gate 	}
3337c478bd9Sstevel@tonic-gate 
3347c478bd9Sstevel@tonic-gate 	/* write out dispatcher for each program */
3357c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3367c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
337*61961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3387c478bd9Sstevel@tonic-gate 			write_program(def, storage);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate }
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * write out definition of internal function (e.g. _printmsg_1(...))
3467c478bd9Sstevel@tonic-gate  *  which calls server's defintion of actual function (e.g. printmsg_1(...)).
3477c478bd9Sstevel@tonic-gate  *  Unpacks single user argument of printmsg_1 to call-by-value format
3487c478bd9Sstevel@tonic-gate  *  expected by printmsg_1.
3497c478bd9Sstevel@tonic-gate  */
350*61961e0fSrobinson static void
351*61961e0fSrobinson write_real_program(definition *def)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 	version_list *vp;
3547c478bd9Sstevel@tonic-gate 	proc_list *proc;
3557c478bd9Sstevel@tonic-gate 	decl_list *l;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (!newstyle)
3587c478bd9Sstevel@tonic-gate 		return;  /* not needed for old style */
3597c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
3607c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
3617c478bd9Sstevel@tonic-gate 			int oneway = streq(proc->res_type, "oneway");
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
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) {
3717c478bd9Sstevel@tonic-gate 				f_print(fout, "(");
3727c478bd9Sstevel@tonic-gate 				/* arg name */
3737c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
374*61961e0fSrobinson 					/* LINTED variable format */
3757c478bd9Sstevel@tonic-gate 					f_print(fout, proc->args.argname);
3767c478bd9Sstevel@tonic-gate 				else
3777c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
3787c478bd9Sstevel@tonic-gate 						proc->args.decls->decl.type, 0);
3797c478bd9Sstevel@tonic-gate 				if (mtflag) {
3807c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp, ");
3817c478bd9Sstevel@tonic-gate 					ptype(proc->res_prefix,
3827c478bd9Sstevel@tonic-gate 							proc->res_type, 1);
3837c478bd9Sstevel@tonic-gate 					f_print(fout,
3847c478bd9Sstevel@tonic-gate 						"*%s, struct svc_req *%s)\n",
3857c478bd9Sstevel@tonic-gate 						RESULT, RQSTP);
3867c478bd9Sstevel@tonic-gate 				}
3877c478bd9Sstevel@tonic-gate 				else
3887c478bd9Sstevel@tonic-gate 					f_print(fout,
3897c478bd9Sstevel@tonic-gate 						" *argp, struct svc_req *%s)\n",
3907c478bd9Sstevel@tonic-gate 						RQSTP);
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 			} else {
3937c478bd9Sstevel@tonic-gate 				if (mtflag)
3947c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s, %s)\n",
3957c478bd9Sstevel@tonic-gate 							RESULT, RQSTP);
3967c478bd9Sstevel@tonic-gate 				else
3977c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s)\n", RQSTP);
3987c478bd9Sstevel@tonic-gate 				/* arg name */
3997c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
4007c478bd9Sstevel@tonic-gate 					f_print(fout, "\t%s *argp;\n",
4017c478bd9Sstevel@tonic-gate 						proc->args.argname);
4027c478bd9Sstevel@tonic-gate 				else {
4037c478bd9Sstevel@tonic-gate 					f_print(fout, "\t");
4047c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
4057c478bd9Sstevel@tonic-gate 						proc->args.decls->decl.type, 0);
4067c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp;\n");
4077c478bd9Sstevel@tonic-gate 				}
4087c478bd9Sstevel@tonic-gate 				if (mtflag)
4097c478bd9Sstevel@tonic-gate 					f_print(fout, "\tvoid *%s;\n", RESULT);
4107c478bd9Sstevel@tonic-gate 				f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
4117c478bd9Sstevel@tonic-gate 			}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 			f_print(fout, "{\n");
4147c478bd9Sstevel@tonic-gate 			f_print(fout, "\treturn (");
4157c478bd9Sstevel@tonic-gate 			/* for mtflag, arguments are different */
4167c478bd9Sstevel@tonic-gate 			if (Cflag || mtflag)
4177c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
4187c478bd9Sstevel@tonic-gate 			else
4197c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4207c478bd9Sstevel@tonic-gate 			f_print(fout, "(");
4217c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4227c478bd9Sstevel@tonic-gate 				if (!streq(proc->args.decls->decl.type, "void"))
4237c478bd9Sstevel@tonic-gate 					f_print(fout, "*argp, "); /* non-void */
4247c478bd9Sstevel@tonic-gate 			} else {
4257c478bd9Sstevel@tonic-gate 				for (l = proc->args.decls;  l != NULL;
4267c478bd9Sstevel@tonic-gate 								l = l->next)
4277c478bd9Sstevel@tonic-gate 					f_print(fout, "argp->%s, ",
4287c478bd9Sstevel@tonic-gate 						l->decl.name);
4297c478bd9Sstevel@tonic-gate 			}
4307c478bd9Sstevel@tonic-gate 			if (mtflag && !oneway)
4317c478bd9Sstevel@tonic-gate 				f_print(fout, "%s, ", RESULT);
4327c478bd9Sstevel@tonic-gate 			f_print(fout, "%s));\n}\n", RQSTP);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	}
4357c478bd9Sstevel@tonic-gate }
4367c478bd9Sstevel@tonic-gate 
437*61961e0fSrobinson static void
438*61961e0fSrobinson write_program(definition *def, char *storage)
4397c478bd9Sstevel@tonic-gate {
4407c478bd9Sstevel@tonic-gate 	version_list *vp;
4417c478bd9Sstevel@tonic-gate 	proc_list *proc;
4427c478bd9Sstevel@tonic-gate 	int filled;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
4457c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
4467c478bd9Sstevel@tonic-gate 		if (storage != NULL) {
4477c478bd9Sstevel@tonic-gate 			f_print(fout, "%s ", storage);
4487c478bd9Sstevel@tonic-gate 		}
4497c478bd9Sstevel@tonic-gate 		f_print(fout, "void\n");
4507c478bd9Sstevel@tonic-gate 		pvname(def->def_name, vp->vers_num);
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 		if (Cflag) {
4537c478bd9Sstevel@tonic-gate 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
4547c478bd9Sstevel@tonic-gate 			f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
4557c478bd9Sstevel@tonic-gate 		} else {
4567c478bd9Sstevel@tonic-gate 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
4577c478bd9Sstevel@tonic-gate 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
4587c478bd9Sstevel@tonic-gate 			f_print(fout, "	register SVCXPRT *%s;\n", TRANSP);
4597c478bd9Sstevel@tonic-gate 		}
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		filled = 0;
4647c478bd9Sstevel@tonic-gate 		f_print(fout, "\tunion {\n");
4657c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
4667c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4677c478bd9Sstevel@tonic-gate 				if (streq(proc->args.decls->decl.type,
4687c478bd9Sstevel@tonic-gate 								"void")) {
4697c478bd9Sstevel@tonic-gate 					continue;
4707c478bd9Sstevel@tonic-gate 				}
4717c478bd9Sstevel@tonic-gate 				filled = 1;
4727c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
4737c478bd9Sstevel@tonic-gate 				ptype(proc->args.decls->decl.prefix,
4747c478bd9Sstevel@tonic-gate 					proc->args.decls->decl.type, 0);
4757c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4767c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 			} else {
4797c478bd9Sstevel@tonic-gate 				filled = 1;
4807c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t%s", proc->args.argname);
4817c478bd9Sstevel@tonic-gate 				f_print(fout, " ");
4827c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4837c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4847c478bd9Sstevel@tonic-gate 			}
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		if (!filled) {
4877c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tint fill;\n");
4887c478bd9Sstevel@tonic-gate 		}
4897c478bd9Sstevel@tonic-gate 		f_print(fout, "\t} %s;\n", ARG);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		if (mtflag) {
4927c478bd9Sstevel@tonic-gate 			filled = 0;
4937c478bd9Sstevel@tonic-gate 			f_print(fout, "\tunion {\n");
4947c478bd9Sstevel@tonic-gate 			for (proc = vp->procs; proc != NULL;
4957c478bd9Sstevel@tonic-gate 							proc = proc->next) {
4967c478bd9Sstevel@tonic-gate 				if (streq(proc->res_type, "void") ||
4977c478bd9Sstevel@tonic-gate 				    streq(proc->res_type, "oneway"))
4987c478bd9Sstevel@tonic-gate 					continue;
4997c478bd9Sstevel@tonic-gate 				filled = 1;
5007c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
5017c478bd9Sstevel@tonic-gate 				ptype(proc->res_prefix, proc->res_type, 0);
5027c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
5037c478bd9Sstevel@tonic-gate 				f_print(fout, "_res;\n");
5047c478bd9Sstevel@tonic-gate 			}
5057c478bd9Sstevel@tonic-gate 			if (!filled)
5067c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\tint fill;\n");
5077c478bd9Sstevel@tonic-gate 			f_print(fout, "\t} %s;\n", RESULT);
5087c478bd9Sstevel@tonic-gate 			f_print(fout, "\tbool_t %s;\n", RETVAL);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		} else
5117c478bd9Sstevel@tonic-gate 			f_print(fout, "\tchar *%s;\n", RESULT);
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 		if (Cflag) {
5147c478bd9Sstevel@tonic-gate 			f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
5157c478bd9Sstevel@tonic-gate 				ARG, RESULT);
5167c478bd9Sstevel@tonic-gate 			if (mtflag)
5177c478bd9Sstevel@tonic-gate 				f_print(fout,
5187c478bd9Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
5197c478bd9Sstevel@tonic-gate 					ROUTINE);
5207c478bd9Sstevel@tonic-gate 			else
5217c478bd9Sstevel@tonic-gate 				f_print(fout,
5227c478bd9Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n",
5237c478bd9Sstevel@tonic-gate 					ROUTINE);
5247c478bd9Sstevel@tonic-gate 		} else {
5257c478bd9Sstevel@tonic-gate 			f_print(fout,
5267c478bd9Sstevel@tonic-gate 				"\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
5277c478bd9Sstevel@tonic-gate 				ARG, RESULT);
5287c478bd9Sstevel@tonic-gate 			if (mtflag)
5297c478bd9Sstevel@tonic-gate 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
5307c478bd9Sstevel@tonic-gate 			else
5317c478bd9Sstevel@tonic-gate 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		if (timerflag) {
5367c478bd9Sstevel@tonic-gate 			if (mtflag)
5377c478bd9Sstevel@tonic-gate 				f_print(fout,
5387c478bd9Sstevel@tonic-gate "\tmutex_lock(&_svcstate_lock);\n");
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 			f_print(fout, "\t_rpcsvccount++;\n");
5417c478bd9Sstevel@tonic-gate 			if (mtflag)
5427c478bd9Sstevel@tonic-gate 				f_print(fout,
5437c478bd9Sstevel@tonic-gate "\tmutex_unlock(&_svcstate_lock);\n");
5447c478bd9Sstevel@tonic-gate 		}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
5477c478bd9Sstevel@tonic-gate 		if (!nullproc(vp->procs)) {
5487c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase NULLPROC:\n");
5497c478bd9Sstevel@tonic-gate 			f_print(fout,
5507c478bd9Sstevel@tonic-gate 			    Cflag ?
551*61961e0fSrobinson "\t\t(void) svc_sendreply(%s,\n\t\t\t(xdrproc_t)xdr_void, NULL);\n" :
552*61961e0fSrobinson "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
5537c478bd9Sstevel@tonic-gate 				TRANSP);
5547c478bd9Sstevel@tonic-gate 			print_return("\t\t");
5557c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
5567c478bd9Sstevel@tonic-gate 		}
5577c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
5587c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase %s:\n", proc->proc_name);
5597c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
5607c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.decls->decl.type);
5617c478bd9Sstevel@tonic-gate 			} else {
5627c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.argname);
5637c478bd9Sstevel@tonic-gate 			}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 			if (streq(proc->res_type, "oneway")) {
5667c478bd9Sstevel@tonic-gate 				/* One-way call */
5677c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
5687c478bd9Sstevel@tonic-gate 			} else {
5697c478bd9Sstevel@tonic-gate 				p_xdrfunc(RESULT, proc->res_type);
5707c478bd9Sstevel@tonic-gate 			}
5717c478bd9Sstevel@tonic-gate 			if (Cflag) {
5727c478bd9Sstevel@tonic-gate 				if (mtflag) {
5737c478bd9Sstevel@tonic-gate 					f_print(fout,
5747c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*) (char *,  "
5757c478bd9Sstevel@tonic-gate 					    "void *,  struct svc_req *))",
5767c478bd9Sstevel@tonic-gate 					    ROUTINE);
5777c478bd9Sstevel@tonic-gate 				} else {
5787c478bd9Sstevel@tonic-gate 					f_print(fout,
5797c478bd9Sstevel@tonic-gate 					    "\t\t%s = (char *(*)(char *, "
5807c478bd9Sstevel@tonic-gate 					    "struct svc_req *)) ",
5817c478bd9Sstevel@tonic-gate 					    ROUTINE);
5827c478bd9Sstevel@tonic-gate 				}
5837c478bd9Sstevel@tonic-gate 			} else {
5847c478bd9Sstevel@tonic-gate 				if (mtflag) {
5857c478bd9Sstevel@tonic-gate 					f_print(fout,
5867c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*)()) ",
5877c478bd9Sstevel@tonic-gate 					    ROUTINE);
5887c478bd9Sstevel@tonic-gate 				} else {
5897c478bd9Sstevel@tonic-gate 					f_print(fout, "\t\t%s = (char *(*)()) ",
5907c478bd9Sstevel@tonic-gate 					    ROUTINE);
5917c478bd9Sstevel@tonic-gate 				}
5927c478bd9Sstevel@tonic-gate 			}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			if (newstyle) { /* new style: calls internal routine */
5957c478bd9Sstevel@tonic-gate 				f_print(fout, "_");
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 			if ((Cflag || mtflag) && !newstyle)
5987c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
5997c478bd9Sstevel@tonic-gate 			else
6007c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
6017c478bd9Sstevel@tonic-gate 			f_print(fout, ";\n");
6027c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n\n");
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
6057c478bd9Sstevel@tonic-gate 		printerr("noproc", TRANSP);
6067c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6077c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 		f_print(fout,
6107c478bd9Sstevel@tonic-gate 			"\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
6117c478bd9Sstevel@tonic-gate 			ARG, ARG);
6127c478bd9Sstevel@tonic-gate 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
6137c478bd9Sstevel@tonic-gate 		printerr("decode", TRANSP);
6147c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6157c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 		if (!mtflag)
6187c478bd9Sstevel@tonic-gate 			if (Cflag)
6197c478bd9Sstevel@tonic-gate 				f_print(fout,
6207c478bd9Sstevel@tonic-gate 					"\t%s = (*%s)((char *)&%s, %s);\n",
6217c478bd9Sstevel@tonic-gate 					RESULT, ROUTINE, ARG, RQSTP);
6227c478bd9Sstevel@tonic-gate 			else
6237c478bd9Sstevel@tonic-gate 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
6247c478bd9Sstevel@tonic-gate 					RESULT, ROUTINE, ARG, RQSTP);
6257c478bd9Sstevel@tonic-gate 		else
6267c478bd9Sstevel@tonic-gate 			if (Cflag)
6277c478bd9Sstevel@tonic-gate 				f_print(fout,
6287c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
6297c478bd9Sstevel@tonic-gate 					RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6307c478bd9Sstevel@tonic-gate 			else
6317c478bd9Sstevel@tonic-gate 				f_print(fout,
6327c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
6337c478bd9Sstevel@tonic-gate 					RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 		if (mtflag)
6397c478bd9Sstevel@tonic-gate 			f_print(fout,
6407c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
6417c478bd9Sstevel@tonic-gate 				RESULT, RETVAL, TRANSP, RESULT, RESULT);
6427c478bd9Sstevel@tonic-gate 		else
6437c478bd9Sstevel@tonic-gate 			f_print(fout,
6447c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n",
6457c478bd9Sstevel@tonic-gate 				RESULT, RESULT, TRANSP, RESULT, RESULT);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		printerr("systemerr", TRANSP);
6487c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
651*61961e0fSrobinson 		(void) snprintf(_errbuf, ERRBUFLEN, "unable to free arguments");
6527c478bd9Sstevel@tonic-gate 		print_err_message("\t\t");
6537c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
6547c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6557c478bd9Sstevel@tonic-gate 		/* print out free routine */
6567c478bd9Sstevel@tonic-gate 		if (mtflag) {
6577c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
6587c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tif (!");
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
6617c478bd9Sstevel@tonic-gate 			f_print(fout,
6627c478bd9Sstevel@tonic-gate "_freeresult(%s, _xdr_%s, (caddr_t)&%s))\n",
6637c478bd9Sstevel@tonic-gate 				TRANSP, RESULT, RESULT);
664*61961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
665*61961e0fSrobinson 						"unable to free results");
6667c478bd9Sstevel@tonic-gate 			print_err_message("\t\t\t");
6677c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
6687c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
6697c478bd9Sstevel@tonic-gate 		};
6707c478bd9Sstevel@tonic-gate 		print_return("\t");
6717c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate }
6747c478bd9Sstevel@tonic-gate 
675*61961e0fSrobinson static void
676*61961e0fSrobinson printerr(char *err, char *transp)
6777c478bd9Sstevel@tonic-gate {
6787c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
681*61961e0fSrobinson static void
682*61961e0fSrobinson printif(char *proc, char *transp, char *prefix, char *arg)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
6857c478bd9Sstevel@tonic-gate 		proc, transp, arg, prefix, arg);
6867c478bd9Sstevel@tonic-gate }
6877c478bd9Sstevel@tonic-gate 
688*61961e0fSrobinson int
689*61961e0fSrobinson nullproc(proc_list *proc)
6907c478bd9Sstevel@tonic-gate {
6917c478bd9Sstevel@tonic-gate 	for (; proc != NULL; proc = proc->next) {
692*61961e0fSrobinson 		if (streq(proc->proc_num, "0"))
6937c478bd9Sstevel@tonic-gate 			return (1);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 	return (0);
6967c478bd9Sstevel@tonic-gate }
6977c478bd9Sstevel@tonic-gate 
698*61961e0fSrobinson static void
699*61961e0fSrobinson write_inetmost(char *infile)
7007c478bd9Sstevel@tonic-gate {
7017c478bd9Sstevel@tonic-gate 	f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
7027c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint sock;\n");
7037c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint proto;\n");
7047c478bd9Sstevel@tonic-gate 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
7057c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint asize = sizeof (saddr);\n");
7067c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
7077c478bd9Sstevel@tonic-gate 	f_print(fout,
7087c478bd9Sstevel@tonic-gate 	"\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
7097c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
7107c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
7117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
7137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
7147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7157c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = 0;\n");
7167c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
7177c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tproto = 0;\n");
7187c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
7197c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else {\n");
7207c478bd9Sstevel@tonic-gate 	write_rpc_svc_fg(infile, "\t\t");
7217c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
7227c478bd9Sstevel@tonic-gate 	print_pmapunset("\t\t");
7237c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}\n");
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
726*61961e0fSrobinson static void
727*61961e0fSrobinson print_return(char *space)
7287c478bd9Sstevel@tonic-gate {
729*61961e0fSrobinson 	if (exitnow) {
7307c478bd9Sstevel@tonic-gate 		f_print(fout, "%sexit(0);\n", space);
731*61961e0fSrobinson 		return;
732*61961e0fSrobinson 	}
7337c478bd9Sstevel@tonic-gate 	if (timerflag) {
7347c478bd9Sstevel@tonic-gate 		if (mtflag)
735*61961e0fSrobinson 			f_print(fout, "%smutex_lock(&_svcstate_lock);\n",
7367c478bd9Sstevel@tonic-gate 					space);
7377c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_rpcsvccount--;\n", space);
7387c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
7397c478bd9Sstevel@tonic-gate 		if (mtflag)
740*61961e0fSrobinson 			f_print(fout, "%smutex_unlock(&_svcstate_lock);\n",
7417c478bd9Sstevel@tonic-gate 					space);
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	f_print(fout, "%sreturn;\n", space);
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
746*61961e0fSrobinson static void
747*61961e0fSrobinson print_pmapunset(char *space)
7487c478bd9Sstevel@tonic-gate {
7497c478bd9Sstevel@tonic-gate 	list *l;
7507c478bd9Sstevel@tonic-gate 	definition *def;
7517c478bd9Sstevel@tonic-gate 	version_list *vp;
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
7547c478bd9Sstevel@tonic-gate 		def = (definition *)l->val;
7557c478bd9Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
7567c478bd9Sstevel@tonic-gate 			for (vp = def->def.pr.versions; vp != NULL;
7577c478bd9Sstevel@tonic-gate 					vp = vp->next) {
7587c478bd9Sstevel@tonic-gate 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
7597c478bd9Sstevel@tonic-gate 					space, def->def_name, vp->vers_name);
7607c478bd9Sstevel@tonic-gate 			}
7617c478bd9Sstevel@tonic-gate 		}
7627c478bd9Sstevel@tonic-gate 	}
7637c478bd9Sstevel@tonic-gate }
7647c478bd9Sstevel@tonic-gate 
765*61961e0fSrobinson static void
766*61961e0fSrobinson print_err_message(char *space)
7677c478bd9Sstevel@tonic-gate {
7687c478bd9Sstevel@tonic-gate 	if (logflag)
7697c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssyslog(LOG_ERR, \"%%s\", \"%s\");\n", space,
7707c478bd9Sstevel@tonic-gate 		    _errbuf);
7717c478bd9Sstevel@tonic-gate 	else if (inetdflag || pmflag)
7727c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
7737c478bd9Sstevel@tonic-gate 	else
7747c478bd9Sstevel@tonic-gate 		f_print(fout, "%sfprintf(stderr, \"%%s\", \"%s\");\n", space,
7757c478bd9Sstevel@tonic-gate 		    _errbuf);
7767c478bd9Sstevel@tonic-gate }
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate /*
7797c478bd9Sstevel@tonic-gate  * Write the server auxiliary function (_msgout, timeout)
7807c478bd9Sstevel@tonic-gate  */
781*61961e0fSrobinson static void
782*61961e0fSrobinson write_svc_aux(int nomain)
7837c478bd9Sstevel@tonic-gate {
7847c478bd9Sstevel@tonic-gate 	if (!logflag)
7857c478bd9Sstevel@tonic-gate 		write_msg_out();
7867c478bd9Sstevel@tonic-gate 	if (!nomain)
7877c478bd9Sstevel@tonic-gate 		write_timeout_func();
7887c478bd9Sstevel@tonic-gate }
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * Write the _msgout function
7927c478bd9Sstevel@tonic-gate  */
793*61961e0fSrobinson static void
794*61961e0fSrobinson write_msg_out(void)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
797*61961e0fSrobinson 	f_print(fout, "static ");
7987c478bd9Sstevel@tonic-gate 	if (!Cflag) {
799*61961e0fSrobinson 		f_print(fout, "void\n_msgout(msg)\n");
8007c478bd9Sstevel@tonic-gate 		f_print(fout, "\tchar *msg;\n");
8017c478bd9Sstevel@tonic-gate 	} else {
802*61961e0fSrobinson 		f_print(fout, "void\n_msgout(char *msg)\n");
8037c478bd9Sstevel@tonic-gate 	}
8047c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8057c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifdef RPC_SVC_FG\n");
8067c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag)
8077c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (_rpcpmstart)\n");
8087c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
8097c478bd9Sstevel@tonic-gate 	f_print(fout, "\telse\n");
8107c478bd9Sstevel@tonic-gate 	f_print(fout,
8117c478bd9Sstevel@tonic-gate 		"\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
8127c478bd9Sstevel@tonic-gate 	f_print(fout, "#else\n");
8137c478bd9Sstevel@tonic-gate 	f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
8147c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
8157c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
8167c478bd9Sstevel@tonic-gate }
8177c478bd9Sstevel@tonic-gate 
8187c478bd9Sstevel@tonic-gate /*
8197c478bd9Sstevel@tonic-gate  * Write the timeout function
8207c478bd9Sstevel@tonic-gate  */
821*61961e0fSrobinson static void
822*61961e0fSrobinson write_timeout_func(void)
8237c478bd9Sstevel@tonic-gate {
8247c478bd9Sstevel@tonic-gate 	if (!timerflag)
8257c478bd9Sstevel@tonic-gate 		return;
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
8287c478bd9Sstevel@tonic-gate 	if (mtflag) {
8297c478bd9Sstevel@tonic-gate 		f_print(fout, "/*ARGSUSED*/\n");
8307c478bd9Sstevel@tonic-gate 		f_print(fout, "static void *\n");
8317c478bd9Sstevel@tonic-gate 		if (!Cflag) {
8327c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(arg)\n");
8337c478bd9Sstevel@tonic-gate 			f_print(fout, "\tvoid *arg;\n");
8347c478bd9Sstevel@tonic-gate 		} else
8357c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(void *arg)\n");
8367c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
8377c478bd9Sstevel@tonic-gate 		f_print(fout, "\t/*CONSTCOND*/\n");
8387c478bd9Sstevel@tonic-gate 		f_print(fout, "\twhile (1) {\n");
8397c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
8407c478bd9Sstevel@tonic-gate 		f_print(fout,
8417c478bd9Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
8427c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tcontinue;\n\n");
8437c478bd9Sstevel@tonic-gate 		f_print(fout,
8447c478bd9Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8457c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8467c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tint size;\n");
8477c478bd9Sstevel@tonic-gate 		} else {
8487c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
8497c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tstatic int size;\n");
8507c478bd9Sstevel@tonic-gate 		}
8517c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
8527c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8537c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
8547c478bd9Sstevel@tonic-gate 		} else {
8557c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tif (size == 0) {\n");
8567c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
8577c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 		f_print(fout,
8607c478bd9Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
8617c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8627c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
8637c478bd9Sstevel@tonic-gate 		} else {
8647c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
8657c478bd9Sstevel@tonic-gate 		}
8667c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\t\topenfd++;\n");
8677c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (openfd <= 1)\n");
8687c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\texit(0);\n");
8697c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t} else\n");
8707c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
8717c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tmutex_unlock(&_svcstate_lock);\n");
8727c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
8737c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
8747c478bd9Sstevel@tonic-gate 		return;
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate 	f_print(fout, "static void\n");
8787c478bd9Sstevel@tonic-gate 	if (!Cflag) {
8797c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(sig)\n");
8807c478bd9Sstevel@tonic-gate 		f_print(fout, "\tint sig;\n");
8817c478bd9Sstevel@tonic-gate 	} else
8827c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(int sig)\n");
8837c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8847c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8857c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
8867c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint size;\n");
8877c478bd9Sstevel@tonic-gate 	} else {
8887c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\textern fd_set svc_fdset;\n");
8897c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstatic int size;\n");
8907c478bd9Sstevel@tonic-gate 	}
8917c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint i, openfd = 0;\n\n");
8927c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
8937c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tsize = svc_max_pollfd;\n");
8947c478bd9Sstevel@tonic-gate 	} else {
8957c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (size == 0) {\n");
8967c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
8977c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
8987c478bd9Sstevel@tonic-gate 	}
8997c478bd9Sstevel@tonic-gate 	f_print(fout,
9007c478bd9Sstevel@tonic-gate 		"\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
9017c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9027c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
9037c478bd9Sstevel@tonic-gate 	} else {
9047c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\topenfd++;\n");
9077c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (openfd <= 1)\n");
9087c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(0);\n");
9097c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else\n");
9107c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
9137c478bd9Sstevel@tonic-gate 				Cflag? "(SIG_PF)" : "(void(*)())");
9147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
9157c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate /*
9197c478bd9Sstevel@tonic-gate  * Write the most of port monitor support
9207c478bd9Sstevel@tonic-gate  */
921*61961e0fSrobinson static void
922*61961e0fSrobinson write_pm_most(char *infile, int netflag)
9237c478bd9Sstevel@tonic-gate {
9247c478bd9Sstevel@tonic-gate 	list *l;
9257c478bd9Sstevel@tonic-gate 	definition *def;
9267c478bd9Sstevel@tonic-gate 	version_list *vp;
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
9297c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/*\n");
9307c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
9317c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * that we were started by a port monitor. If\n");
9327c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
9337c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * TLI endpoint.\n");
9347c478bd9Sstevel@tonic-gate 	f_print(fout, "\t */\n");
9357c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
9367c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tchar *netid;\n");
9377c478bd9Sstevel@tonic-gate 	if (!netflag) {	/* Not included by -n option */
9387c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
9397c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
9407c478bd9Sstevel@tonic-gate 	}
9417c478bd9Sstevel@tonic-gate 	if (timerflag)
9427c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint pmclose;\n");
9437c478bd9Sstevel@tonic-gate /*
9447c478bd9Sstevel@tonic-gate  *  Not necessary, defined in /usr/include/stdlib
9457c478bd9Sstevel@tonic-gate  *  f_print(fout, "\t\textern char *getenv();\n");
9467c478bd9Sstevel@tonic-gate  */
9477c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
9487c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
9497c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
9507c478bd9Sstevel@tonic-gate 	f_print(fout,
9517c478bd9Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	if (timerflag) {
9547c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* started from inetd */\n");
9557c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tpmclose = 1;\n");
9567c478bd9Sstevel@tonic-gate 	}
957*61961e0fSrobinson 	f_print(fout, "\t\t} else {\n");
9587c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
959*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot get transport info");
9607c478bd9Sstevel@tonic-gate 	print_err_message("\t\t\t\t");
9617c478bd9Sstevel@tonic-gate 	if (timerflag)
9627c478bd9Sstevel@tonic-gate 		f_print(fout,
9637c478bd9Sstevel@tonic-gate 			"\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
9647c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9657c478bd9Sstevel@tonic-gate 	f_print(fout,
9667c478bd9Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
9677c478bd9Sstevel@tonic-gate 		TRANSP);
968*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create server handle");
9697c478bd9Sstevel@tonic-gate 	print_err_message("\t\t\t");
9707c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
9717c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9727c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (nconf)\n");
9737c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
9747c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
9757c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
9767c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
9777c478bd9Sstevel@tonic-gate 			continue;
9787c478bd9Sstevel@tonic-gate 		}
9797c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
9807c478bd9Sstevel@tonic-gate 			f_print(fout,
9817c478bd9Sstevel@tonic-gate 				"\t\tif (!svc_reg(%s, %s, %s, ",
9827c478bd9Sstevel@tonic-gate 				TRANSP, def->def_name, vp->vers_name);
9837c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
9847c478bd9Sstevel@tonic-gate 			f_print(fout, ", 0)) {\n");
985*61961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
986*61961e0fSrobinson 					"unable to register (%s, %s).",
9877c478bd9Sstevel@tonic-gate 					def->def_name, vp->vers_name);
9887c478bd9Sstevel@tonic-gate 			print_err_message("\t\t\t");
9897c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\texit(1);\n");
9907c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t}\n");
9917c478bd9Sstevel@tonic-gate 		}
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 	if (timerflag) {
9947c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (pmclose) {\n");
9957c478bd9Sstevel@tonic-gate 		if (mtflag) {
9967c478bd9Sstevel@tonic-gate 			f_print(fout,
9977c478bd9Sstevel@tonic-gate "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t\t\t0, NULL) != 0) {\n");
9987c478bd9Sstevel@tonic-gate 			f_print(fout,
9997c478bd9Sstevel@tonic-gate "\t\t\t\t_msgout(\"cannot create closedown thread\");\n");
10007c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\texit(1);\n");
10017c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
10027c478bd9Sstevel@tonic-gate 		} else {
10037c478bd9Sstevel@tonic-gate 			f_print(fout,
10047c478bd9Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
10057c478bd9Sstevel@tonic-gate 					Cflag? "(SIG_PF)" : "(void(*)())");
10067c478bd9Sstevel@tonic-gate 			f_print(fout,
10077c478bd9Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
10087c478bd9Sstevel@tonic-gate 		}
10097c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
10107c478bd9Sstevel@tonic-gate 	}
10117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvc_run();\n");
10127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n");
10137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t/* NOTREACHED */\n");
10147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}");
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*
10187c478bd9Sstevel@tonic-gate  * Support for backgrounding the server if self started.
10197c478bd9Sstevel@tonic-gate  */
1020*61961e0fSrobinson static void
1021*61961e0fSrobinson write_rpc_svc_fg(char *infile, char *sp)
10227c478bd9Sstevel@tonic-gate {
10237c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef RPC_SVC_FG\n");
10247c478bd9Sstevel@tonic-gate 	f_print(fout, "#pragma weak closefrom\n");
10257c478bd9Sstevel@tonic-gate 	f_print(fout, "%sextern void closefrom();\n", sp);
10267c478bd9Sstevel@tonic-gate 	f_print(fout, "%sint size;\n", sp);
10277c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10287c478bd9Sstevel@tonic-gate 		f_print(fout, "%sstruct rlimit rl;\n", sp);
10297c478bd9Sstevel@tonic-gate 	if (inetdflag)
10307c478bd9Sstevel@tonic-gate 		f_print(fout, "%sint pid, i;\n\n", sp);
10317c478bd9Sstevel@tonic-gate 	f_print(fout, "%spid = fork();\n", sp);
10327c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid < 0) {\n", sp);
10337c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
10347c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(1);\n", sp);
10357c478bd9Sstevel@tonic-gate 	f_print(fout, "%s}\n", sp);
10367c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid)\n", sp);
10377c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(0);\n", sp);
10387c478bd9Sstevel@tonic-gate 	/* close all file descriptors */
10397c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
10407c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (closefrom != NULL)\n", sp);
10417c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tclosefrom(0);\n", sp);
10427c478bd9Sstevel@tonic-gate 		f_print(fout, "%selse {\n", sp);
10437c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
10447c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
10457c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
10467c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\texit(1);\n", sp);
10477c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10487c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10497c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10507c478bd9Sstevel@tonic-gate 	} else {
10517c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tsize = getdtablesize();\n", sp);
10527c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10537c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 	/* Redirect stderr and stdout to /dev/null */
10567c478bd9Sstevel@tonic-gate 	f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
10577c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
10587c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
10597c478bd9Sstevel@tonic-gate 	/* This removes control of the controlling terminal */
10607c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10617c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssetsid();\n", sp);
10627c478bd9Sstevel@tonic-gate 	else {
10637c478bd9Sstevel@tonic-gate 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
10647c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (i >= 0) {\n", sp);
10657c478bd9Sstevel@tonic-gate 		f_print(fout,
10667c478bd9Sstevel@tonic-gate 			"%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
10677c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t(void) close(i);\n", sp);
10687c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10697c478bd9Sstevel@tonic-gate 	}
10707c478bd9Sstevel@tonic-gate 	if (!logflag)
10717c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10727c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
10737c478bd9Sstevel@tonic-gate 	if (logflag)
10747c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate 
1077*61961e0fSrobinson static void
1078*61961e0fSrobinson open_log_file(char *infile, char *sp)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	char *s;
10817c478bd9Sstevel@tonic-gate 
10827c478bd9Sstevel@tonic-gate 	s = strrchr(infile, '.');
10837c478bd9Sstevel@tonic-gate 	if (s)
10847c478bd9Sstevel@tonic-gate 		*s = '\0';
10857c478bd9Sstevel@tonic-gate 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
10867c478bd9Sstevel@tonic-gate 	if (s)
10877c478bd9Sstevel@tonic-gate 		*s = '.';
10887c478bd9Sstevel@tonic-gate }
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate /*
10917c478bd9Sstevel@tonic-gate  * write a registration for the given transport for Inetd
10927c478bd9Sstevel@tonic-gate  */
10937c478bd9Sstevel@tonic-gate void
1094*61961e0fSrobinson write_inetd_register(char *transp)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	list *l;
10977c478bd9Sstevel@tonic-gate 	definition *def;
10987c478bd9Sstevel@tonic-gate 	version_list *vp;
10997c478bd9Sstevel@tonic-gate 	char *sp;
11007c478bd9Sstevel@tonic-gate 	int isudp;
11017c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	if (inetdflag)
11047c478bd9Sstevel@tonic-gate 		sp = "\t";
11057c478bd9Sstevel@tonic-gate 	else
11067c478bd9Sstevel@tonic-gate 		sp = "";
11077c478bd9Sstevel@tonic-gate 	if (streq(transp, "udp"))
11087c478bd9Sstevel@tonic-gate 		isudp = 1;
11097c478bd9Sstevel@tonic-gate 	else
11107c478bd9Sstevel@tonic-gate 		isudp = 0;
11117c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
11127c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11137c478bd9Sstevel@tonic-gate 		f_print(fout,
11147c478bd9Sstevel@tonic-gate 			"\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
11157c478bd9Sstevel@tonic-gate 			isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
11167c478bd9Sstevel@tonic-gate 	}
11177c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc%s_create(%s",
11187c478bd9Sstevel@tonic-gate 		sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
11197c478bd9Sstevel@tonic-gate 	if (!isudp)
11207c478bd9Sstevel@tonic-gate 		f_print(fout, ", 0, 0");
11217c478bd9Sstevel@tonic-gate 	f_print(fout, ");\n");
11227c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1123*61961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create %s service.",
1124*61961e0fSrobinson 						transp);
1125*61961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
11267c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
11277c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
11287c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11317c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
11327c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
11337c478bd9Sstevel@tonic-gate 				sp, isudp ? "UDP": "TCP");
11347c478bd9Sstevel@tonic-gate 	}
11357c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
11367c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
1137*61961e0fSrobinson 		if (def->def_kind != DEF_PROGRAM)
11387c478bd9Sstevel@tonic-gate 			continue;
11397c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
11407c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
11417c478bd9Sstevel@tonic-gate 				sp, TRANSP, def->def_name, vp->vers_name);
11427c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
11437c478bd9Sstevel@tonic-gate 			if (inetdflag)
11447c478bd9Sstevel@tonic-gate 				f_print(fout, ", proto)) {\n");
11457c478bd9Sstevel@tonic-gate 			else
11467c478bd9Sstevel@tonic-gate 				f_print(fout, ", IPPROTO_%s)) {\n",
11477c478bd9Sstevel@tonic-gate 					isudp ? "UDP": "TCP");
1148*61961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
11497c478bd9Sstevel@tonic-gate 				"unable to register (%s, %s, %s).",
11507c478bd9Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
11517c478bd9Sstevel@tonic-gate 			print_err_message(tmpbuf);
11527c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
11537c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
11547c478bd9Sstevel@tonic-gate 		}
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 	if (inetdflag)
11577c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
11587c478bd9Sstevel@tonic-gate }
1159