xref: /titanic_44/usr/src/cmd/rpcgen/rpc_svcout.c (revision a2f144d1eca25f33af1e19affd8eda208f547108)
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
5*a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6*a2f144d1SJordan 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 /*
23*a2f144d1SJordan 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>
437c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
447c478bd9Sstevel@tonic-gate #include "rpc_util.h"
457c478bd9Sstevel@tonic-gate 
4661961e0fSrobinson extern int nullproc(proc_list *);
4761961e0fSrobinson 
487c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp";
497c478bd9Sstevel@tonic-gate static char TRANSP[] = "transp";
507c478bd9Sstevel@tonic-gate static char ARG[] = "argument";
517c478bd9Sstevel@tonic-gate static char RESULT[] = "result";
527c478bd9Sstevel@tonic-gate static char ROUTINE[] = "local";
537c478bd9Sstevel@tonic-gate static char RETVAL[] = "retval";
547c478bd9Sstevel@tonic-gate 
5561961e0fSrobinson #define	ERRBUFLEN	256
5661961e0fSrobinson char _errbuf[ERRBUFLEN];	/* For all messages */
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 *);
6861961e0fSrobinson static void print_err_message(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
7661961e0fSrobinson p_xdrfunc(char *rname, char *typename)
777c478bd9Sstevel@tonic-gate {
787c478bd9Sstevel@tonic-gate 	if (Cflag)
797c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = (xdrproc_t)xdr_%s;\n",
807c478bd9Sstevel@tonic-gate 		    rname, stringfix(typename));
817c478bd9Sstevel@tonic-gate 	else
827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
837c478bd9Sstevel@tonic-gate 		    rname, stringfix(typename));
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
8661961e0fSrobinson static void
8761961e0fSrobinson internal_proctype(proc_list *plist)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 	f_print(fout, "static ");
907c478bd9Sstevel@tonic-gate 	ptype(plist->res_prefix, plist->res_type, 1);
917c478bd9Sstevel@tonic-gate 	f_print(fout, "*");
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static void
9661961e0fSrobinson write_mtauto(void)
977c478bd9Sstevel@tonic-gate {
987c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
997c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_msgout(\"%s\");\n",
1007c478bd9Sstevel@tonic-gate 	    "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
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,
1187c478bd9Sstevel@tonic-gate "\n\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");
1267c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvcstate = _IDLE;");
1277c478bd9Sstevel@tonic-gate 			f_print(fout,
1287c478bd9Sstevel@tonic-gate "\t/* Set when a request is serviced */\n");
1297c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvccount = 0;");
1307c478bd9Sstevel@tonic-gate 			f_print(fout,
1317c478bd9Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n");
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate 			if (mtflag) {
1347c478bd9Sstevel@tonic-gate 				f_print(fout, "mutex_t _svcstate_lock;");
1357c478bd9Sstevel@tonic-gate 				f_print(fout,
1367c478bd9Sstevel@tonic-gate "\t\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 			}
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 		write_svc_aux(nomain);
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	/* write out dispatcher and stubs */
14461961e0fSrobinson 	write_programs(nomain ? NULL : "static");
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	if (nomain)
1477c478bd9Sstevel@tonic-gate 		return;
1487c478bd9Sstevel@tonic-gate 
14961961e0fSrobinson 	f_print(fout, "\nint\nmain()\n");
1507c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
1517c478bd9Sstevel@tonic-gate 	if (inetdflag) {
1527c478bd9Sstevel@tonic-gate 		write_inetmost(infile);
1537c478bd9Sstevel@tonic-gate 		/* Includes call to write_rpc_svc_fg() */
1547c478bd9Sstevel@tonic-gate 	} else {
1557c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
1567c478bd9Sstevel@tonic-gate 			if (netflag) {
1577c478bd9Sstevel@tonic-gate 				f_print(fout,
1587c478bd9Sstevel@tonic-gate 				    "\tregister SVCXPRT *%s;\n", TRANSP);
1597c478bd9Sstevel@tonic-gate 				f_print(fout,
1607c478bd9Sstevel@tonic-gate 				    "\tstruct netconfig *nconf = NULL;\n");
1617c478bd9Sstevel@tonic-gate 			}
1627c478bd9Sstevel@tonic-gate 			f_print(fout, "\tpid_t pid;\n");
1637c478bd9Sstevel@tonic-gate 			f_print(fout, "\tint i;\n");
1647c478bd9Sstevel@tonic-gate 			if (mtauto) {
1657c478bd9Sstevel@tonic-gate 				f_print(fout,
1667c478bd9Sstevel@tonic-gate 				    "\tint mode = RPC_SVC_MT_AUTO;\n\n");
1677c478bd9Sstevel@tonic-gate 				write_mtauto();
1687c478bd9Sstevel@tonic-gate 			} else
1697c478bd9Sstevel@tonic-gate 				f_print(fout, "\n");
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 			if (mtflag & timerflag)
1727c478bd9Sstevel@tonic-gate 				f_print(fout,
1737c478bd9Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 			write_pm_most(infile, netflag);
1767c478bd9Sstevel@tonic-gate 			f_print(fout, "\telse {\n");
1777c478bd9Sstevel@tonic-gate 			write_rpc_svc_fg(infile, "\t\t");
1787c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
1797c478bd9Sstevel@tonic-gate 		} else {
1807c478bd9Sstevel@tonic-gate 			f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
1817c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
1827c478bd9Sstevel@tonic-gate 			print_pmapunset("\t");
1837c478bd9Sstevel@tonic-gate 		}
1847c478bd9Sstevel@tonic-gate 	}
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (logflag && !inetdflag) {
1877c478bd9Sstevel@tonic-gate 		open_log_file(infile, "\t");
1887c478bd9Sstevel@tonic-gate 	}
1897c478bd9Sstevel@tonic-gate }
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * write a registration for the given transport
1937c478bd9Sstevel@tonic-gate  */
1947c478bd9Sstevel@tonic-gate void
19561961e0fSrobinson write_netid_register(char *transp)
1967c478bd9Sstevel@tonic-gate {
1977c478bd9Sstevel@tonic-gate 	list *l;
1987c478bd9Sstevel@tonic-gate 	definition *def;
1997c478bd9Sstevel@tonic-gate 	version_list *vp;
2007c478bd9Sstevel@tonic-gate 	char *sp;
2017c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	sp = "";
2047c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2057c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
2067c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
20761961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot find %s netid.", transp);
20861961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
2097c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
2107c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2117c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2127c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
21361961e0fSrobinson 	    sp, TRANSP);
2147c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
21561961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create %s service.",
21661961e0fSrobinson 	    transp);
2177c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
2187c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2197c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2207c478bd9Sstevel@tonic-gate 
2217c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2227c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2237c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2247c478bd9Sstevel@tonic-gate 			continue;
2257c478bd9Sstevel@tonic-gate 		}
2267c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2277c478bd9Sstevel@tonic-gate 			f_print(fout,
2287c478bd9Sstevel@tonic-gate 			    "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
2297c478bd9Sstevel@tonic-gate 			    sp, def->def_name, vp->vers_name);
2307c478bd9Sstevel@tonic-gate 			f_print(fout,
2317c478bd9Sstevel@tonic-gate 			    "%s\tif (!svc_reg(%s, %s, %s, ",
2327c478bd9Sstevel@tonic-gate 			    sp, TRANSP, def->def_name, vp->vers_name);
2337c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
2347c478bd9Sstevel@tonic-gate 			f_print(fout, ", nconf)) {\n");
23561961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
2367c478bd9Sstevel@tonic-gate 			    "unable to register (%s, %s, %s).",
2377c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
2387c478bd9Sstevel@tonic-gate 			print_err_message(tmpbuf);
2397c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
2407c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
2417c478bd9Sstevel@tonic-gate 		}
2427c478bd9Sstevel@tonic-gate 	}
2437c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate /*
2477c478bd9Sstevel@tonic-gate  * write a registration for the given transport for TLI
2487c478bd9Sstevel@tonic-gate  */
2497c478bd9Sstevel@tonic-gate void
25061961e0fSrobinson write_nettype_register(char *transp)
2517c478bd9Sstevel@tonic-gate {
2527c478bd9Sstevel@tonic-gate 	list *l;
2537c478bd9Sstevel@tonic-gate 	definition *def;
2547c478bd9Sstevel@tonic-gate 	version_list *vp;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2577c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2587c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2597c478bd9Sstevel@tonic-gate 			continue;
2607c478bd9Sstevel@tonic-gate 		}
2617c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
2627c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (!svc_create(");
2637c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
2647c478bd9Sstevel@tonic-gate 			f_print(fout, ", %s, %s, \"%s\")) {\n",
2657c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
26661961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
2677c478bd9Sstevel@tonic-gate 			    "unable to create (%s, %s) for %s.",
2687c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
2697c478bd9Sstevel@tonic-gate 			print_err_message("\t\t");
2707c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\texit(1);\n");
2717c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
2727c478bd9Sstevel@tonic-gate 		}
2737c478bd9Sstevel@tonic-gate 	}
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate /*
2777c478bd9Sstevel@tonic-gate  * write the rest of the service
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate void
28061961e0fSrobinson write_rest(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2837c478bd9Sstevel@tonic-gate 	if (inetdflag) {
2847c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
28561961e0fSrobinson 		(void) snprintf(_errbuf, ERRBUFLEN,
28661961e0fSrobinson 		    "could not create a handle");
2877c478bd9Sstevel@tonic-gate 		print_err_message("\t\t");
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");
2957c478bd9Sstevel@tonic-gate 				f_print(fout,
2967c478bd9Sstevel@tonic-gate "\t\t\t_msgout(\"cannot create closedown thread\");\n");
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");
31161961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "svc_run returned");
3127c478bd9Sstevel@tonic-gate 	print_err_message("\t");
3137c478bd9Sstevel@tonic-gate 	f_print(fout, "\texit(1);\n");
3147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/* NOTREACHED */\n");
3157c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
31861961e0fSrobinson static void
31961961e0fSrobinson write_programs(char *storage)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 	list *l;
3227c478bd9Sstevel@tonic-gate 	definition *def;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	/* write out stubs for procedure  definitions */
3257c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3267c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
32761961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3287c478bd9Sstevel@tonic-gate 			write_real_program(def);
3297c478bd9Sstevel@tonic-gate 	}
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 	/* write out dispatcher for each program */
3327c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
3337c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
33461961e0fSrobinson 		if (def->def_kind == DEF_PROGRAM)
3357c478bd9Sstevel@tonic-gate 			write_program(def, storage);
3367c478bd9Sstevel@tonic-gate 	}
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * write out definition of internal function (e.g. _printmsg_1(...))
3437c478bd9Sstevel@tonic-gate  *  which calls server's defintion of actual function (e.g. printmsg_1(...)).
3447c478bd9Sstevel@tonic-gate  *  Unpacks single user argument of printmsg_1 to call-by-value format
3457c478bd9Sstevel@tonic-gate  *  expected by printmsg_1.
3467c478bd9Sstevel@tonic-gate  */
34761961e0fSrobinson static void
34861961e0fSrobinson write_real_program(definition *def)
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	version_list *vp;
3517c478bd9Sstevel@tonic-gate 	proc_list *proc;
3527c478bd9Sstevel@tonic-gate 	decl_list *l;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (!newstyle)
3557c478bd9Sstevel@tonic-gate 		return;  /* not needed for old style */
3567c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
3577c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
3587c478bd9Sstevel@tonic-gate 			int oneway = streq(proc->res_type, "oneway");
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
3617c478bd9Sstevel@tonic-gate 			if (!mtflag)
3627c478bd9Sstevel@tonic-gate 				internal_proctype(proc);
3637c478bd9Sstevel@tonic-gate 			else
3647c478bd9Sstevel@tonic-gate 				f_print(fout, "int");
3657c478bd9Sstevel@tonic-gate 			f_print(fout, "\n_");
3667c478bd9Sstevel@tonic-gate 			pvname(proc->proc_name, vp->vers_num);
3677c478bd9Sstevel@tonic-gate 			if (Cflag) {
3687c478bd9Sstevel@tonic-gate 				f_print(fout, "(");
3697c478bd9Sstevel@tonic-gate 				/* arg name */
3707c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
37161961e0fSrobinson 					/* LINTED variable format */
3727c478bd9Sstevel@tonic-gate 					f_print(fout, proc->args.argname);
3737c478bd9Sstevel@tonic-gate 				else
3747c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
3757c478bd9Sstevel@tonic-gate 					    proc->args.decls->decl.type, 0);
3767c478bd9Sstevel@tonic-gate 				if (mtflag) {
3777c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp, ");
3787c478bd9Sstevel@tonic-gate 					ptype(proc->res_prefix,
3797c478bd9Sstevel@tonic-gate 					    proc->res_type, 1);
3807c478bd9Sstevel@tonic-gate 					f_print(fout,
3817c478bd9Sstevel@tonic-gate 					    "*%s, struct svc_req *%s)\n",
3827c478bd9Sstevel@tonic-gate 					    RESULT, RQSTP);
3837c478bd9Sstevel@tonic-gate 				}
3847c478bd9Sstevel@tonic-gate 				else
3857c478bd9Sstevel@tonic-gate 					f_print(fout,
3867c478bd9Sstevel@tonic-gate 					    " *argp, struct svc_req *%s)\n",
3877c478bd9Sstevel@tonic-gate 					    RQSTP);
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 			} else {
3907c478bd9Sstevel@tonic-gate 				if (mtflag)
3917c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s, %s)\n",
3927c478bd9Sstevel@tonic-gate 					    RESULT, RQSTP);
3937c478bd9Sstevel@tonic-gate 				else
3947c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s)\n", RQSTP);
3957c478bd9Sstevel@tonic-gate 				/* arg name */
3967c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
3977c478bd9Sstevel@tonic-gate 					f_print(fout, "\t%s *argp;\n",
3987c478bd9Sstevel@tonic-gate 					    proc->args.argname);
3997c478bd9Sstevel@tonic-gate 				else {
4007c478bd9Sstevel@tonic-gate 					f_print(fout, "\t");
4017c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
4027c478bd9Sstevel@tonic-gate 					    proc->args.decls->decl.type, 0);
4037c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp;\n");
4047c478bd9Sstevel@tonic-gate 				}
4057c478bd9Sstevel@tonic-gate 				if (mtflag)
4067c478bd9Sstevel@tonic-gate 					f_print(fout, "\tvoid *%s;\n", RESULT);
4077c478bd9Sstevel@tonic-gate 				f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
4087c478bd9Sstevel@tonic-gate 			}
4097c478bd9Sstevel@tonic-gate 
4107c478bd9Sstevel@tonic-gate 			f_print(fout, "{\n");
4117c478bd9Sstevel@tonic-gate 			f_print(fout, "\treturn (");
4127c478bd9Sstevel@tonic-gate 			/* for mtflag, arguments are different */
4137c478bd9Sstevel@tonic-gate 			if (Cflag || mtflag)
4147c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
4157c478bd9Sstevel@tonic-gate 			else
4167c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4177c478bd9Sstevel@tonic-gate 			f_print(fout, "(");
4187c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4197c478bd9Sstevel@tonic-gate 				if (!streq(proc->args.decls->decl.type, "void"))
4207c478bd9Sstevel@tonic-gate 					f_print(fout, "*argp, "); /* non-void */
4217c478bd9Sstevel@tonic-gate 			} else {
4227c478bd9Sstevel@tonic-gate 				for (l = proc->args.decls;  l != NULL;
4237c478bd9Sstevel@tonic-gate 				    l = l->next)
4247c478bd9Sstevel@tonic-gate 					f_print(fout, "argp->%s, ",
4257c478bd9Sstevel@tonic-gate 					    l->decl.name);
4267c478bd9Sstevel@tonic-gate 			}
4277c478bd9Sstevel@tonic-gate 			if (mtflag && !oneway)
4287c478bd9Sstevel@tonic-gate 				f_print(fout, "%s, ", RESULT);
4297c478bd9Sstevel@tonic-gate 			f_print(fout, "%s));\n}\n", RQSTP);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 	}
4327c478bd9Sstevel@tonic-gate }
4337c478bd9Sstevel@tonic-gate 
43461961e0fSrobinson static void
43561961e0fSrobinson write_program(definition *def, char *storage)
4367c478bd9Sstevel@tonic-gate {
4377c478bd9Sstevel@tonic-gate 	version_list *vp;
4387c478bd9Sstevel@tonic-gate 	proc_list *proc;
4397c478bd9Sstevel@tonic-gate 	int filled;
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
4427c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
4437c478bd9Sstevel@tonic-gate 		if (storage != NULL) {
4447c478bd9Sstevel@tonic-gate 			f_print(fout, "%s ", storage);
4457c478bd9Sstevel@tonic-gate 		}
4467c478bd9Sstevel@tonic-gate 		f_print(fout, "void\n");
4477c478bd9Sstevel@tonic-gate 		pvname(def->def_name, vp->vers_num);
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 		if (Cflag) {
4507c478bd9Sstevel@tonic-gate 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
4517c478bd9Sstevel@tonic-gate 			f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
4527c478bd9Sstevel@tonic-gate 		} else {
4537c478bd9Sstevel@tonic-gate 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
4547c478bd9Sstevel@tonic-gate 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
4557c478bd9Sstevel@tonic-gate 			f_print(fout, "	register SVCXPRT *%s;\n", TRANSP);
4567c478bd9Sstevel@tonic-gate 		}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		filled = 0;
4617c478bd9Sstevel@tonic-gate 		f_print(fout, "\tunion {\n");
4627c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
4637c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4647c478bd9Sstevel@tonic-gate 				if (streq(proc->args.decls->decl.type,
4657c478bd9Sstevel@tonic-gate 				    "void")) {
4667c478bd9Sstevel@tonic-gate 					continue;
4677c478bd9Sstevel@tonic-gate 				}
4687c478bd9Sstevel@tonic-gate 				filled = 1;
4697c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
4707c478bd9Sstevel@tonic-gate 				ptype(proc->args.decls->decl.prefix,
4717c478bd9Sstevel@tonic-gate 				    proc->args.decls->decl.type, 0);
4727c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4737c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 			} else {
4767c478bd9Sstevel@tonic-gate 				filled = 1;
4777c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t%s", proc->args.argname);
4787c478bd9Sstevel@tonic-gate 				f_print(fout, " ");
4797c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4807c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4817c478bd9Sstevel@tonic-gate 			}
4827c478bd9Sstevel@tonic-gate 		}
4837c478bd9Sstevel@tonic-gate 		if (!filled) {
4847c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tint fill;\n");
4857c478bd9Sstevel@tonic-gate 		}
4867c478bd9Sstevel@tonic-gate 		f_print(fout, "\t} %s;\n", ARG);
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 		if (mtflag) {
4897c478bd9Sstevel@tonic-gate 			filled = 0;
4907c478bd9Sstevel@tonic-gate 			f_print(fout, "\tunion {\n");
4917c478bd9Sstevel@tonic-gate 			for (proc = vp->procs; proc != NULL;
4927c478bd9Sstevel@tonic-gate 			    proc = proc->next) {
4937c478bd9Sstevel@tonic-gate 				if (streq(proc->res_type, "void") ||
4947c478bd9Sstevel@tonic-gate 				    streq(proc->res_type, "oneway"))
4957c478bd9Sstevel@tonic-gate 					continue;
4967c478bd9Sstevel@tonic-gate 				filled = 1;
4977c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
4987c478bd9Sstevel@tonic-gate 				ptype(proc->res_prefix, proc->res_type, 0);
4997c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
5007c478bd9Sstevel@tonic-gate 				f_print(fout, "_res;\n");
5017c478bd9Sstevel@tonic-gate 			}
5027c478bd9Sstevel@tonic-gate 			if (!filled)
5037c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\tint fill;\n");
5047c478bd9Sstevel@tonic-gate 			f_print(fout, "\t} %s;\n", RESULT);
5057c478bd9Sstevel@tonic-gate 			f_print(fout, "\tbool_t %s;\n", RETVAL);
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 		} else
5087c478bd9Sstevel@tonic-gate 			f_print(fout, "\tchar *%s;\n", RESULT);
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 		if (Cflag) {
5117c478bd9Sstevel@tonic-gate 			f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
5127c478bd9Sstevel@tonic-gate 			    ARG, RESULT);
5137c478bd9Sstevel@tonic-gate 			if (mtflag)
5147c478bd9Sstevel@tonic-gate 				f_print(fout,
5157c478bd9Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
5167c478bd9Sstevel@tonic-gate 				    ROUTINE);
5177c478bd9Sstevel@tonic-gate 			else
5187c478bd9Sstevel@tonic-gate 				f_print(fout,
5197c478bd9Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n",
5207c478bd9Sstevel@tonic-gate 				    ROUTINE);
5217c478bd9Sstevel@tonic-gate 		} else {
5227c478bd9Sstevel@tonic-gate 			f_print(fout,
5237c478bd9Sstevel@tonic-gate 			    "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
5247c478bd9Sstevel@tonic-gate 			    ARG, RESULT);
5257c478bd9Sstevel@tonic-gate 			if (mtflag)
5267c478bd9Sstevel@tonic-gate 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
5277c478bd9Sstevel@tonic-gate 			else
5287c478bd9Sstevel@tonic-gate 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		if (timerflag) {
5337c478bd9Sstevel@tonic-gate 			if (mtflag)
5347c478bd9Sstevel@tonic-gate 				f_print(fout,
5357c478bd9Sstevel@tonic-gate "\tmutex_lock(&_svcstate_lock);\n");
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 			f_print(fout, "\t_rpcsvccount++;\n");
5387c478bd9Sstevel@tonic-gate 			if (mtflag)
5397c478bd9Sstevel@tonic-gate 				f_print(fout,
5407c478bd9Sstevel@tonic-gate "\tmutex_unlock(&_svcstate_lock);\n");
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
5447c478bd9Sstevel@tonic-gate 		if (!nullproc(vp->procs)) {
5457c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase NULLPROC:\n");
5467c478bd9Sstevel@tonic-gate 			f_print(fout,
5477c478bd9Sstevel@tonic-gate 			    Cflag ?
54861961e0fSrobinson "\t\t(void) svc_sendreply(%s,\n\t\t\t(xdrproc_t)xdr_void, NULL);\n" :
54961961e0fSrobinson "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
5507c478bd9Sstevel@tonic-gate 			    TRANSP);
5517c478bd9Sstevel@tonic-gate 			print_return("\t\t");
5527c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
5537c478bd9Sstevel@tonic-gate 		}
5547c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
5557c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase %s:\n", proc->proc_name);
5567c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
5577c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.decls->decl.type);
5587c478bd9Sstevel@tonic-gate 			} else {
5597c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.argname);
5607c478bd9Sstevel@tonic-gate 			}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 			if (streq(proc->res_type, "oneway")) {
5637c478bd9Sstevel@tonic-gate 				/* One-way call */
5647c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
5657c478bd9Sstevel@tonic-gate 			} else {
5667c478bd9Sstevel@tonic-gate 				p_xdrfunc(RESULT, proc->res_type);
5677c478bd9Sstevel@tonic-gate 			}
5687c478bd9Sstevel@tonic-gate 			if (Cflag) {
5697c478bd9Sstevel@tonic-gate 				if (mtflag) {
5707c478bd9Sstevel@tonic-gate 					f_print(fout,
5717c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*) (char *,  "
5727c478bd9Sstevel@tonic-gate 					    "void *,  struct svc_req *))",
5737c478bd9Sstevel@tonic-gate 					    ROUTINE);
5747c478bd9Sstevel@tonic-gate 				} else {
5757c478bd9Sstevel@tonic-gate 					f_print(fout,
5767c478bd9Sstevel@tonic-gate 					    "\t\t%s = (char *(*)(char *, "
5777c478bd9Sstevel@tonic-gate 					    "struct svc_req *)) ",
5787c478bd9Sstevel@tonic-gate 					    ROUTINE);
5797c478bd9Sstevel@tonic-gate 				}
5807c478bd9Sstevel@tonic-gate 			} else {
5817c478bd9Sstevel@tonic-gate 				if (mtflag) {
5827c478bd9Sstevel@tonic-gate 					f_print(fout,
5837c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*)()) ",
5847c478bd9Sstevel@tonic-gate 					    ROUTINE);
5857c478bd9Sstevel@tonic-gate 				} else {
5867c478bd9Sstevel@tonic-gate 					f_print(fout, "\t\t%s = (char *(*)()) ",
5877c478bd9Sstevel@tonic-gate 					    ROUTINE);
5887c478bd9Sstevel@tonic-gate 				}
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 			if (newstyle) { /* new style: calls internal routine */
5927c478bd9Sstevel@tonic-gate 				f_print(fout, "_");
5937c478bd9Sstevel@tonic-gate 			}
5947c478bd9Sstevel@tonic-gate 			if ((Cflag || mtflag) && !newstyle)
5957c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
5967c478bd9Sstevel@tonic-gate 			else
5977c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
5987c478bd9Sstevel@tonic-gate 			f_print(fout, ";\n");
5997c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n\n");
6007c478bd9Sstevel@tonic-gate 		}
6017c478bd9Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
6027c478bd9Sstevel@tonic-gate 		printerr("noproc", TRANSP);
6037c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6047c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		f_print(fout,
6077c478bd9Sstevel@tonic-gate 		    "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
6087c478bd9Sstevel@tonic-gate 		    ARG, ARG);
6097c478bd9Sstevel@tonic-gate 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
6107c478bd9Sstevel@tonic-gate 		printerr("decode", TRANSP);
6117c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6127c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 		if (!mtflag)
6157c478bd9Sstevel@tonic-gate 			if (Cflag)
6167c478bd9Sstevel@tonic-gate 				f_print(fout,
6177c478bd9Sstevel@tonic-gate 				    "\t%s = (*%s)((char *)&%s, %s);\n",
6187c478bd9Sstevel@tonic-gate 				    RESULT, ROUTINE, ARG, RQSTP);
6197c478bd9Sstevel@tonic-gate 			else
6207c478bd9Sstevel@tonic-gate 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
6217c478bd9Sstevel@tonic-gate 				    RESULT, ROUTINE, ARG, RQSTP);
6227c478bd9Sstevel@tonic-gate 		else
6237c478bd9Sstevel@tonic-gate 			if (Cflag)
6247c478bd9Sstevel@tonic-gate 				f_print(fout,
6257c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
6267c478bd9Sstevel@tonic-gate 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6277c478bd9Sstevel@tonic-gate 			else
6287c478bd9Sstevel@tonic-gate 				f_print(fout,
6297c478bd9Sstevel@tonic-gate "\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
6307c478bd9Sstevel@tonic-gate 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 		if (mtflag)
6367c478bd9Sstevel@tonic-gate 			f_print(fout,
6377c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
6387c478bd9Sstevel@tonic-gate 			    RESULT, RETVAL, TRANSP, RESULT, RESULT);
6397c478bd9Sstevel@tonic-gate 		else
6407c478bd9Sstevel@tonic-gate 			f_print(fout,
6417c478bd9Sstevel@tonic-gate "\tif (_xdr_%s && %s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n",
6427c478bd9Sstevel@tonic-gate 			    RESULT, RESULT, TRANSP, RESULT, RESULT);
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 		printerr("systemerr", TRANSP);
6457c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
64861961e0fSrobinson 		(void) snprintf(_errbuf, ERRBUFLEN, "unable to free arguments");
6497c478bd9Sstevel@tonic-gate 		print_err_message("\t\t");
6507c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
6517c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6527c478bd9Sstevel@tonic-gate 		/* print out free routine */
6537c478bd9Sstevel@tonic-gate 		if (mtflag) {
6547c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
6557c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tif (!");
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
6587c478bd9Sstevel@tonic-gate 			f_print(fout,
6597c478bd9Sstevel@tonic-gate "_freeresult(%s, _xdr_%s, (caddr_t)&%s))\n",
6607c478bd9Sstevel@tonic-gate 			    TRANSP, RESULT, RESULT);
66161961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
66261961e0fSrobinson 			    "unable to free results");
6637c478bd9Sstevel@tonic-gate 			print_err_message("\t\t\t");
6647c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
6657c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
6667c478bd9Sstevel@tonic-gate 		};
6677c478bd9Sstevel@tonic-gate 		print_return("\t");
6687c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
6697c478bd9Sstevel@tonic-gate 	}
6707c478bd9Sstevel@tonic-gate }
6717c478bd9Sstevel@tonic-gate 
67261961e0fSrobinson static void
67361961e0fSrobinson printerr(char *err, char *transp)
6747c478bd9Sstevel@tonic-gate {
6757c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
67861961e0fSrobinson static void
67961961e0fSrobinson printif(char *proc, char *transp, char *prefix, char *arg)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
6827c478bd9Sstevel@tonic-gate 	    proc, transp, arg, prefix, arg);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
68561961e0fSrobinson int
68661961e0fSrobinson nullproc(proc_list *proc)
6877c478bd9Sstevel@tonic-gate {
6887c478bd9Sstevel@tonic-gate 	for (; proc != NULL; proc = proc->next) {
68961961e0fSrobinson 		if (streq(proc->proc_num, "0"))
6907c478bd9Sstevel@tonic-gate 			return (1);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 	return (0);
6937c478bd9Sstevel@tonic-gate }
6947c478bd9Sstevel@tonic-gate 
69561961e0fSrobinson static void
69661961e0fSrobinson write_inetmost(char *infile)
6977c478bd9Sstevel@tonic-gate {
6987c478bd9Sstevel@tonic-gate 	f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
6997c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint sock;\n");
7007c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint proto;\n");
7017c478bd9Sstevel@tonic-gate 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
7027c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint asize = sizeof (saddr);\n");
7037c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
7047c478bd9Sstevel@tonic-gate 	f_print(fout,
7057c478bd9Sstevel@tonic-gate 	"\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
7067c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
7077c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
7087c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7097c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
7107c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
7117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = 0;\n");
7137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
7147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tproto = 0;\n");
7157c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
7167c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else {\n");
7177c478bd9Sstevel@tonic-gate 	write_rpc_svc_fg(infile, "\t\t");
7187c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
7197c478bd9Sstevel@tonic-gate 	print_pmapunset("\t\t");
7207c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}\n");
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
72361961e0fSrobinson static void
72461961e0fSrobinson print_return(char *space)
7257c478bd9Sstevel@tonic-gate {
72661961e0fSrobinson 	if (exitnow) {
7277c478bd9Sstevel@tonic-gate 		f_print(fout, "%sexit(0);\n", space);
72861961e0fSrobinson 		return;
72961961e0fSrobinson 	}
7307c478bd9Sstevel@tonic-gate 	if (timerflag) {
7317c478bd9Sstevel@tonic-gate 		if (mtflag)
73261961e0fSrobinson 			f_print(fout, "%smutex_lock(&_svcstate_lock);\n",
7337c478bd9Sstevel@tonic-gate 			    space);
7347c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_rpcsvccount--;\n", space);
7357c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
7367c478bd9Sstevel@tonic-gate 		if (mtflag)
73761961e0fSrobinson 			f_print(fout, "%smutex_unlock(&_svcstate_lock);\n",
7387c478bd9Sstevel@tonic-gate 			    space);
7397c478bd9Sstevel@tonic-gate 	}
7407c478bd9Sstevel@tonic-gate 	f_print(fout, "%sreturn;\n", space);
7417c478bd9Sstevel@tonic-gate }
7427c478bd9Sstevel@tonic-gate 
74361961e0fSrobinson static void
74461961e0fSrobinson print_pmapunset(char *space)
7457c478bd9Sstevel@tonic-gate {
7467c478bd9Sstevel@tonic-gate 	list *l;
7477c478bd9Sstevel@tonic-gate 	definition *def;
7487c478bd9Sstevel@tonic-gate 	version_list *vp;
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
7517c478bd9Sstevel@tonic-gate 		def = (definition *)l->val;
7527c478bd9Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
7537c478bd9Sstevel@tonic-gate 			for (vp = def->def.pr.versions; vp != NULL;
7547c478bd9Sstevel@tonic-gate 			    vp = vp->next) {
7557c478bd9Sstevel@tonic-gate 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
7567c478bd9Sstevel@tonic-gate 				    space, def->def_name, vp->vers_name);
7577c478bd9Sstevel@tonic-gate 			}
7587c478bd9Sstevel@tonic-gate 		}
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate }
7617c478bd9Sstevel@tonic-gate 
76261961e0fSrobinson static void
76361961e0fSrobinson print_err_message(char *space)
7647c478bd9Sstevel@tonic-gate {
7657c478bd9Sstevel@tonic-gate 	if (logflag)
7667c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssyslog(LOG_ERR, \"%%s\", \"%s\");\n", space,
7677c478bd9Sstevel@tonic-gate 		    _errbuf);
7687c478bd9Sstevel@tonic-gate 	else if (inetdflag || pmflag)
7697c478bd9Sstevel@tonic-gate 		f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
7707c478bd9Sstevel@tonic-gate 	else
7717c478bd9Sstevel@tonic-gate 		f_print(fout, "%sfprintf(stderr, \"%%s\", \"%s\");\n", space,
7727c478bd9Sstevel@tonic-gate 		    _errbuf);
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate /*
7767c478bd9Sstevel@tonic-gate  * Write the server auxiliary function (_msgout, timeout)
7777c478bd9Sstevel@tonic-gate  */
77861961e0fSrobinson static void
77961961e0fSrobinson write_svc_aux(int nomain)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 	if (!logflag)
7827c478bd9Sstevel@tonic-gate 		write_msg_out();
7837c478bd9Sstevel@tonic-gate 	if (!nomain)
7847c478bd9Sstevel@tonic-gate 		write_timeout_func();
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Write the _msgout function
7897c478bd9Sstevel@tonic-gate  */
79061961e0fSrobinson static void
79161961e0fSrobinson write_msg_out(void)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
79461961e0fSrobinson 	f_print(fout, "static ");
7957c478bd9Sstevel@tonic-gate 	if (!Cflag) {
79661961e0fSrobinson 		f_print(fout, "void\n_msgout(msg)\n");
7977c478bd9Sstevel@tonic-gate 		f_print(fout, "\tchar *msg;\n");
7987c478bd9Sstevel@tonic-gate 	} else {
79961961e0fSrobinson 		f_print(fout, "void\n_msgout(char *msg)\n");
8007c478bd9Sstevel@tonic-gate 	}
8017c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8027c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifdef RPC_SVC_FG\n");
8037c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag)
8047c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (_rpcpmstart)\n");
8057c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
8067c478bd9Sstevel@tonic-gate 	f_print(fout, "\telse\n");
8077c478bd9Sstevel@tonic-gate 	f_print(fout,
8087c478bd9Sstevel@tonic-gate 	    "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
8097c478bd9Sstevel@tonic-gate 	f_print(fout, "#else\n");
8107c478bd9Sstevel@tonic-gate 	f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
8117c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
8127c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
8137c478bd9Sstevel@tonic-gate }
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate /*
8167c478bd9Sstevel@tonic-gate  * Write the timeout function
8177c478bd9Sstevel@tonic-gate  */
81861961e0fSrobinson static void
81961961e0fSrobinson write_timeout_func(void)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	if (!timerflag)
8227c478bd9Sstevel@tonic-gate 		return;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
8257c478bd9Sstevel@tonic-gate 	if (mtflag) {
8267c478bd9Sstevel@tonic-gate 		f_print(fout, "/*ARGSUSED*/\n");
8277c478bd9Sstevel@tonic-gate 		f_print(fout, "static void *\n");
8287c478bd9Sstevel@tonic-gate 		if (!Cflag) {
8297c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(arg)\n");
8307c478bd9Sstevel@tonic-gate 			f_print(fout, "\tvoid *arg;\n");
8317c478bd9Sstevel@tonic-gate 		} else
8327c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(void *arg)\n");
8337c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
8347c478bd9Sstevel@tonic-gate 		f_print(fout, "\t/*CONSTCOND*/\n");
8357c478bd9Sstevel@tonic-gate 		f_print(fout, "\twhile (1) {\n");
8367c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
8377c478bd9Sstevel@tonic-gate 		f_print(fout,
8387c478bd9Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
8397c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tcontinue;\n\n");
8407c478bd9Sstevel@tonic-gate 		f_print(fout,
8417c478bd9Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8427c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8437c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tint size;\n");
8447c478bd9Sstevel@tonic-gate 		} else {
8457c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
8467c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tstatic int size;\n");
8477c478bd9Sstevel@tonic-gate 		}
8487c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
8497c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8507c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
8517c478bd9Sstevel@tonic-gate 		} else {
8527c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tif (size == 0) {\n");
8537c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
8547c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
8557c478bd9Sstevel@tonic-gate 		}
8567c478bd9Sstevel@tonic-gate 		f_print(fout,
8577c478bd9Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
8587c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8597c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
8607c478bd9Sstevel@tonic-gate 		} else {
8617c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\t\topenfd++;\n");
8647c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (openfd <= 1)\n");
8657c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\texit(0);\n");
8667c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t} else\n");
8677c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
8687c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tmutex_unlock(&_svcstate_lock);\n");
8697c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
8707c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
8717c478bd9Sstevel@tonic-gate 		return;
8727c478bd9Sstevel@tonic-gate 	}
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	f_print(fout, "static void\n");
8757c478bd9Sstevel@tonic-gate 	if (!Cflag) {
8767c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(sig)\n");
8777c478bd9Sstevel@tonic-gate 		f_print(fout, "\tint sig;\n");
8787c478bd9Sstevel@tonic-gate 	} else
8797c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(int sig)\n");
8807c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8817c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8827c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
8837c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint size;\n");
8847c478bd9Sstevel@tonic-gate 	} else {
8857c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\textern fd_set svc_fdset;\n");
8867c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstatic int size;\n");
8877c478bd9Sstevel@tonic-gate 	}
8887c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint i, openfd = 0;\n\n");
8897c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
8907c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tsize = svc_max_pollfd;\n");
8917c478bd9Sstevel@tonic-gate 	} else {
8927c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (size == 0) {\n");
8937c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
8947c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate 	f_print(fout,
8977c478bd9Sstevel@tonic-gate 	    "\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
8987c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
8997c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
9007c478bd9Sstevel@tonic-gate 	} else {
9017c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\topenfd++;\n");
9047c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (openfd <= 1)\n");
9057c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(0);\n");
9067c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else\n");
9077c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
9107c478bd9Sstevel@tonic-gate 	    Cflag? "(SIG_PF)" : "(void(*)())");
9117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
9127c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
9137c478bd9Sstevel@tonic-gate }
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate /*
9167c478bd9Sstevel@tonic-gate  * Write the most of port monitor support
9177c478bd9Sstevel@tonic-gate  */
91861961e0fSrobinson static void
91961961e0fSrobinson write_pm_most(char *infile, int netflag)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 	list *l;
9227c478bd9Sstevel@tonic-gate 	definition *def;
9237c478bd9Sstevel@tonic-gate 	version_list *vp;
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
9267c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/*\n");
9277c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
9287c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * that we were started by a port monitor. If\n");
9297c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
9307c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * TLI endpoint.\n");
9317c478bd9Sstevel@tonic-gate 	f_print(fout, "\t */\n");
9327c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
9337c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tchar *netid;\n");
9347c478bd9Sstevel@tonic-gate 	if (!netflag) {	/* Not included by -n option */
9357c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
9367c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 	if (timerflag)
9397c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint pmclose;\n");
9407c478bd9Sstevel@tonic-gate /*
9417c478bd9Sstevel@tonic-gate  *  Not necessary, defined in /usr/include/stdlib
9427c478bd9Sstevel@tonic-gate  *  f_print(fout, "\t\textern char *getenv();\n");
9437c478bd9Sstevel@tonic-gate  */
9447c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
9457c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
9467c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
9477c478bd9Sstevel@tonic-gate 	f_print(fout,
9487c478bd9Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if (timerflag) {
9517c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* started from inetd */\n");
9527c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tpmclose = 1;\n");
9537c478bd9Sstevel@tonic-gate 	}
95461961e0fSrobinson 	f_print(fout, "\t\t} else {\n");
9557c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
95661961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot get transport info");
9577c478bd9Sstevel@tonic-gate 	print_err_message("\t\t\t\t");
9587c478bd9Sstevel@tonic-gate 	if (timerflag)
9597c478bd9Sstevel@tonic-gate 		f_print(fout,
9607c478bd9Sstevel@tonic-gate 		    "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
9617c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9627c478bd9Sstevel@tonic-gate 	f_print(fout,
9637c478bd9Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
9647c478bd9Sstevel@tonic-gate 	    TRANSP);
96561961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create server handle");
9667c478bd9Sstevel@tonic-gate 	print_err_message("\t\t\t");
9677c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
9687c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9697c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (nconf)\n");
9707c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
9717c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
9727c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
9737c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
9747c478bd9Sstevel@tonic-gate 			continue;
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
9777c478bd9Sstevel@tonic-gate 			f_print(fout,
9787c478bd9Sstevel@tonic-gate 			    "\t\tif (!svc_reg(%s, %s, %s, ",
9797c478bd9Sstevel@tonic-gate 			    TRANSP, def->def_name, vp->vers_name);
9807c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
9817c478bd9Sstevel@tonic-gate 			f_print(fout, ", 0)) {\n");
98261961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
98361961e0fSrobinson 			    "unable to register (%s, %s).",
9847c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name);
9857c478bd9Sstevel@tonic-gate 			print_err_message("\t\t\t");
9867c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\texit(1);\n");
9877c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t}\n");
9887c478bd9Sstevel@tonic-gate 		}
9897c478bd9Sstevel@tonic-gate 	}
9907c478bd9Sstevel@tonic-gate 	if (timerflag) {
9917c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (pmclose) {\n");
9927c478bd9Sstevel@tonic-gate 		if (mtflag) {
9937c478bd9Sstevel@tonic-gate 			f_print(fout,
9947c478bd9Sstevel@tonic-gate "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t\t\t0, NULL) != 0) {\n");
9957c478bd9Sstevel@tonic-gate 			f_print(fout,
9967c478bd9Sstevel@tonic-gate "\t\t\t\t_msgout(\"cannot create closedown thread\");\n");
9977c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\texit(1);\n");
9987c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
9997c478bd9Sstevel@tonic-gate 		} else {
10007c478bd9Sstevel@tonic-gate 			f_print(fout,
10017c478bd9Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
10027c478bd9Sstevel@tonic-gate 			    Cflag? "(SIG_PF)" : "(void(*)())");
10037c478bd9Sstevel@tonic-gate 			f_print(fout,
10047c478bd9Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
10077c478bd9Sstevel@tonic-gate 	}
10087c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvc_run();\n");
10097c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n");
10107c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t/* NOTREACHED */\n");
10117c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}");
10127c478bd9Sstevel@tonic-gate }
10137c478bd9Sstevel@tonic-gate 
10147c478bd9Sstevel@tonic-gate /*
10157c478bd9Sstevel@tonic-gate  * Support for backgrounding the server if self started.
10167c478bd9Sstevel@tonic-gate  */
101761961e0fSrobinson static void
101861961e0fSrobinson write_rpc_svc_fg(char *infile, char *sp)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef RPC_SVC_FG\n");
10217c478bd9Sstevel@tonic-gate 	f_print(fout, "#pragma weak closefrom\n");
10227c478bd9Sstevel@tonic-gate 	f_print(fout, "%sextern void closefrom();\n", sp);
10237c478bd9Sstevel@tonic-gate 	f_print(fout, "%sint size;\n", sp);
10247c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10257c478bd9Sstevel@tonic-gate 		f_print(fout, "%sstruct rlimit rl;\n", sp);
10267c478bd9Sstevel@tonic-gate 	if (inetdflag)
10277c478bd9Sstevel@tonic-gate 		f_print(fout, "%sint pid, i;\n\n", sp);
10287c478bd9Sstevel@tonic-gate 	f_print(fout, "%spid = fork();\n", sp);
10297c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid < 0) {\n", sp);
10307c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
10317c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(1);\n", sp);
10327c478bd9Sstevel@tonic-gate 	f_print(fout, "%s}\n", sp);
10337c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid)\n", sp);
10347c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(0);\n", sp);
10357c478bd9Sstevel@tonic-gate 	/* close all file descriptors */
10367c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
10377c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (closefrom != NULL)\n", sp);
10387c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tclosefrom(0);\n", sp);
10397c478bd9Sstevel@tonic-gate 		f_print(fout, "%selse {\n", sp);
10407c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
10417c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
10427c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
10437c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\texit(1);\n", sp);
10447c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10457c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10467c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10477c478bd9Sstevel@tonic-gate 	} else {
10487c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tsize = getdtablesize();\n", sp);
10497c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10507c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 	/* Redirect stderr and stdout to /dev/null */
10537c478bd9Sstevel@tonic-gate 	f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
10547c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
10557c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
10567c478bd9Sstevel@tonic-gate 	/* This removes control of the controlling terminal */
10577c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10587c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssetsid();\n", sp);
10597c478bd9Sstevel@tonic-gate 	else {
10607c478bd9Sstevel@tonic-gate 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
10617c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (i >= 0) {\n", sp);
10627c478bd9Sstevel@tonic-gate 		f_print(fout,
10637c478bd9Sstevel@tonic-gate 		    "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
10647c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t(void) close(i);\n", sp);
10657c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10667c478bd9Sstevel@tonic-gate 	}
10677c478bd9Sstevel@tonic-gate 	if (!logflag)
10687c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10697c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
10707c478bd9Sstevel@tonic-gate 	if (logflag)
10717c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
107461961e0fSrobinson static void
107561961e0fSrobinson open_log_file(char *infile, char *sp)
10767c478bd9Sstevel@tonic-gate {
10777c478bd9Sstevel@tonic-gate 	char *s;
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 	s = strrchr(infile, '.');
10807c478bd9Sstevel@tonic-gate 	if (s)
10817c478bd9Sstevel@tonic-gate 		*s = '\0';
10827c478bd9Sstevel@tonic-gate 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
10837c478bd9Sstevel@tonic-gate 	if (s)
10847c478bd9Sstevel@tonic-gate 		*s = '.';
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate /*
10887c478bd9Sstevel@tonic-gate  * write a registration for the given transport for Inetd
10897c478bd9Sstevel@tonic-gate  */
10907c478bd9Sstevel@tonic-gate void
109161961e0fSrobinson write_inetd_register(char *transp)
10927c478bd9Sstevel@tonic-gate {
10937c478bd9Sstevel@tonic-gate 	list *l;
10947c478bd9Sstevel@tonic-gate 	definition *def;
10957c478bd9Sstevel@tonic-gate 	version_list *vp;
10967c478bd9Sstevel@tonic-gate 	char *sp;
10977c478bd9Sstevel@tonic-gate 	int isudp;
10987c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if (inetdflag)
11017c478bd9Sstevel@tonic-gate 		sp = "\t";
11027c478bd9Sstevel@tonic-gate 	else
11037c478bd9Sstevel@tonic-gate 		sp = "";
11047c478bd9Sstevel@tonic-gate 	if (streq(transp, "udp"))
11057c478bd9Sstevel@tonic-gate 		isudp = 1;
11067c478bd9Sstevel@tonic-gate 	else
11077c478bd9Sstevel@tonic-gate 		isudp = 0;
11087c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
11097c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11107c478bd9Sstevel@tonic-gate 		f_print(fout,
11117c478bd9Sstevel@tonic-gate 		    "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
11127c478bd9Sstevel@tonic-gate 		    isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc%s_create(%s",
11157c478bd9Sstevel@tonic-gate 	    sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
11167c478bd9Sstevel@tonic-gate 	if (!isudp)
11177c478bd9Sstevel@tonic-gate 		f_print(fout, ", 0, 0");
11187c478bd9Sstevel@tonic-gate 	f_print(fout, ");\n");
11197c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
112061961e0fSrobinson 	(void) snprintf(_errbuf, ERRBUFLEN, "cannot create %s service.",
112161961e0fSrobinson 	    transp);
112261961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
11237c478bd9Sstevel@tonic-gate 	print_err_message(tmpbuf);
11247c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
11257c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11287c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
11297c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
11307c478bd9Sstevel@tonic-gate 		    sp, isudp ? "UDP": "TCP");
11317c478bd9Sstevel@tonic-gate 	}
11327c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
11337c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
113461961e0fSrobinson 		if (def->def_kind != DEF_PROGRAM)
11357c478bd9Sstevel@tonic-gate 			continue;
11367c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
11377c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
11387c478bd9Sstevel@tonic-gate 			    sp, TRANSP, def->def_name, vp->vers_name);
11397c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
11407c478bd9Sstevel@tonic-gate 			if (inetdflag)
11417c478bd9Sstevel@tonic-gate 				f_print(fout, ", proto)) {\n");
11427c478bd9Sstevel@tonic-gate 			else
11437c478bd9Sstevel@tonic-gate 				f_print(fout, ", IPPROTO_%s)) {\n",
11447c478bd9Sstevel@tonic-gate 				    isudp ? "UDP": "TCP");
114561961e0fSrobinson 			(void) snprintf(_errbuf, ERRBUFLEN,
11467c478bd9Sstevel@tonic-gate 			    "unable to register (%s, %s, %s).",
11477c478bd9Sstevel@tonic-gate 			    def->def_name, vp->vers_name, transp);
11487c478bd9Sstevel@tonic-gate 			print_err_message(tmpbuf);
11497c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
11507c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
11517c478bd9Sstevel@tonic-gate 		}
11527c478bd9Sstevel@tonic-gate 	}
11537c478bd9Sstevel@tonic-gate 	if (inetdflag)
11547c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
11557c478bd9Sstevel@tonic-gate }
1156