xref: /illumos-gate/usr/src/cmd/rpcgen/rpc_svcout.c (revision 60414d47)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5a2f144d1SJordan Brown  * Common Development and Distribution License (the "License").
6a2f144d1SJordan Brown  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
2061961e0fSrobinson  */
2161961e0fSrobinson 
2261961e0fSrobinson /*
23a2f144d1SJordan Brown  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
277c478bd9Sstevel@tonic-gate /* All Rights Reserved */
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
307c478bd9Sstevel@tonic-gate  * The Regents of the University of California
317c478bd9Sstevel@tonic-gate  * All Rights Reserved
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
347c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
357c478bd9Sstevel@tonic-gate  * contributors.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
407c478bd9Sstevel@tonic-gate  */
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <string.h>
43a465d40cSJordan Brown #include <stdarg.h>
447c478bd9Sstevel@tonic-gate #include "rpc_parse.h"
457c478bd9Sstevel@tonic-gate #include "rpc_util.h"
467c478bd9Sstevel@tonic-gate 
4761961e0fSrobinson extern int nullproc(proc_list *);
4861961e0fSrobinson 
497c478bd9Sstevel@tonic-gate static char RQSTP[] = "rqstp";
507c478bd9Sstevel@tonic-gate static char TRANSP[] = "transp";
517c478bd9Sstevel@tonic-gate static char ARG[] = "argument";
527c478bd9Sstevel@tonic-gate static char RESULT[] = "result";
537c478bd9Sstevel@tonic-gate static char ROUTINE[] = "local";
547c478bd9Sstevel@tonic-gate static char RETVAL[] = "retval";
557c478bd9Sstevel@tonic-gate 
5661961e0fSrobinson #define	ERRBUFLEN	256
5761961e0fSrobinson 
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 *);
68a465d40cSJordan Brown static void print_err_message(const char *, const char *, ...);
6961961e0fSrobinson static void write_msg_out(void);
7061961e0fSrobinson static void write_timeout_func(void);
7161961e0fSrobinson static void write_pm_most(char *, int);
7261961e0fSrobinson static void write_rpc_svc_fg(char *, char *);
7361961e0fSrobinson static void open_log_file(char *, char *);
747c478bd9Sstevel@tonic-gate 
7561961e0fSrobinson static void
p_xdrfunc(char * rname,char * typename)7661961e0fSrobinson p_xdrfunc(char *rname, char *typename)
777c478bd9Sstevel@tonic-gate {
78a465d40cSJordan Brown 	if (Cflag) {
79a465d40cSJordan Brown 		f_print(fout, "\t\t_xdr_%s = (xdrproc_t)\n", rname);
80a465d40cSJordan Brown 		f_print(fout, "\t\t    xdr_%s;\n", stringfix(typename));
81a465d40cSJordan Brown 	} else {
827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
83a2f144d1SJordan Brown 		    rname, stringfix(typename));
84a465d40cSJordan Brown 	}
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
8761961e0fSrobinson static void
internal_proctype(proc_list * plist)8861961e0fSrobinson internal_proctype(proc_list *plist)
897c478bd9Sstevel@tonic-gate {
907c478bd9Sstevel@tonic-gate 	f_print(fout, "static ");
917c478bd9Sstevel@tonic-gate 	ptype(plist->res_prefix, plist->res_type, 1);
927c478bd9Sstevel@tonic-gate 	f_print(fout, "*");
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate static void
write_mtauto(void)9761961e0fSrobinson write_mtauto(void)
987c478bd9Sstevel@tonic-gate {
997c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
100a465d40cSJordan Brown 	print_err_message("\t\t", "unable to set automatic MT mode.");
1017c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n\t}\n");
1027c478bd9Sstevel@tonic-gate }
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * write most of the service, that is, everything but the registrations.
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate void
write_most(char * infile,int netflag,int nomain)10861961e0fSrobinson write_most(char *infile, int netflag, int nomain)
1097c478bd9Sstevel@tonic-gate {
1107c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag) {
1117c478bd9Sstevel@tonic-gate 		char *var_type;
1127c478bd9Sstevel@tonic-gate 		var_type = (nomain? "extern" : "static");
1137c478bd9Sstevel@tonic-gate 		f_print(fout, "%s int _rpcpmstart;", var_type);
1147c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* Started by a port monitor ? */\n");
1157c478bd9Sstevel@tonic-gate 		if (!tirpcflag) {
1167c478bd9Sstevel@tonic-gate 			f_print(fout, "%s int _rpcfdtype;", var_type);
1177c478bd9Sstevel@tonic-gate 			f_print(fout,
118a465d40cSJordan Brown 			    "\t\t /* Whether Stream or Datagram ? */\n");
1197c478bd9Sstevel@tonic-gate 		}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 		if (timerflag) {
1227c478bd9Sstevel@tonic-gate 			f_print(fout,
1237c478bd9Sstevel@tonic-gate "\n/* States a server can be in wrt request */\n\n");
1247c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_IDLE 0\n");
1257c478bd9Sstevel@tonic-gate 			f_print(fout, "#define\t_SERVED 1\n\n");
126a465d40cSJordan Brown 			if (nomain) {
127a465d40cSJordan Brown 				f_print(fout,
128a465d40cSJordan Brown 				    "/* LINTED static unused if no main */\n");
129a465d40cSJordan Brown 			}
130a465d40cSJordan Brown 			f_print(fout,
131a465d40cSJordan Brown 			    "static int _rpcsvcstate = _IDLE;");
1327c478bd9Sstevel@tonic-gate 			f_print(fout,
1337c478bd9Sstevel@tonic-gate "\t/* Set when a request is serviced */\n");
1347c478bd9Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvccount = 0;");
1357c478bd9Sstevel@tonic-gate 			f_print(fout,
1367c478bd9Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n");
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 			if (mtflag) {
139*60414d47SToomas Soome 				f_print(fout, "%smutex_t _svcstate_lock;",
140*60414d47SToomas Soome 				    nomain? "extern " : "");
1417c478bd9Sstevel@tonic-gate 				f_print(fout,
142a465d40cSJordan Brown "\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 		write_svc_aux(nomain);
1487c478bd9Sstevel@tonic-gate 	}
1497c478bd9Sstevel@tonic-gate 	/* write out dispatcher and stubs */
15061961e0fSrobinson 	write_programs(nomain ? NULL : "static");
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (nomain)
1537c478bd9Sstevel@tonic-gate 		return;
1547c478bd9Sstevel@tonic-gate 
15561961e0fSrobinson 	f_print(fout, "\nint\nmain()\n");
1567c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
1577c478bd9Sstevel@tonic-gate 	if (inetdflag) {
1587c478bd9Sstevel@tonic-gate 		write_inetmost(infile);
1597c478bd9Sstevel@tonic-gate 		/* Includes call to write_rpc_svc_fg() */
1607c478bd9Sstevel@tonic-gate 	} else {
1617c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
1627c478bd9Sstevel@tonic-gate 			if (netflag) {
1637c478bd9Sstevel@tonic-gate 				f_print(fout,
164a2f144d1SJordan Brown 				    "\tregister SVCXPRT *%s;\n", TRANSP);
1657c478bd9Sstevel@tonic-gate 				f_print(fout,
166a2f144d1SJordan Brown 				    "\tstruct netconfig *nconf = NULL;\n");
1677c478bd9Sstevel@tonic-gate 			}
1687c478bd9Sstevel@tonic-gate 			f_print(fout, "\tpid_t pid;\n");
1697c478bd9Sstevel@tonic-gate 			f_print(fout, "\tint i;\n");
1707c478bd9Sstevel@tonic-gate 			if (mtauto) {
1717c478bd9Sstevel@tonic-gate 				f_print(fout,
172a2f144d1SJordan Brown 				    "\tint mode = RPC_SVC_MT_AUTO;\n\n");
1737c478bd9Sstevel@tonic-gate 				write_mtauto();
1747c478bd9Sstevel@tonic-gate 			} else
1757c478bd9Sstevel@tonic-gate 				f_print(fout, "\n");
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 			if (mtflag & timerflag)
1787c478bd9Sstevel@tonic-gate 				f_print(fout,
1797c478bd9Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 			write_pm_most(infile, netflag);
1827c478bd9Sstevel@tonic-gate 			f_print(fout, "\telse {\n");
1837c478bd9Sstevel@tonic-gate 			write_rpc_svc_fg(infile, "\t\t");
1847c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
1857c478bd9Sstevel@tonic-gate 		} else {
1867c478bd9Sstevel@tonic-gate 			f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
1877c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
1887c478bd9Sstevel@tonic-gate 			print_pmapunset("\t");
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	if (logflag && !inetdflag) {
1937c478bd9Sstevel@tonic-gate 		open_log_file(infile, "\t");
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate /*
1987c478bd9Sstevel@tonic-gate  * write a registration for the given transport
1997c478bd9Sstevel@tonic-gate  */
2007c478bd9Sstevel@tonic-gate void
write_netid_register(char * transp)20161961e0fSrobinson write_netid_register(char *transp)
2027c478bd9Sstevel@tonic-gate {
2037c478bd9Sstevel@tonic-gate 	list *l;
2047c478bd9Sstevel@tonic-gate 	definition *def;
2057c478bd9Sstevel@tonic-gate 	version_list *vp;
2067c478bd9Sstevel@tonic-gate 	char *sp;
2077c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	sp = "";
2107c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
2117c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
2127c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
21361961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
214a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot find %s netid.", transp);
2157c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
2167c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
2177c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
218a2f144d1SJordan Brown 	    sp, TRANSP);
2197c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
220a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot create %s service.", transp);
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,
231a2f144d1SJordan Brown 			    "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
232a2f144d1SJordan Brown 			    sp, def->def_name, vp->vers_name);
2337c478bd9Sstevel@tonic-gate 			f_print(fout,
234a2f144d1SJordan Brown 			    "%s\tif (!svc_reg(%s, %s, %s, ",
235a2f144d1SJordan Brown 			    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");
238a465d40cSJordan Brown 			print_err_message(tmpbuf,
239a2f144d1SJordan Brown 			    "unable to register (%s, %s, %s).",
240a2f144d1SJordan Brown 			    def->def_name, vp->vers_name, transp);
2417c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
2427c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
2437c478bd9Sstevel@tonic-gate 		}
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate 
2487c478bd9Sstevel@tonic-gate /*
2497c478bd9Sstevel@tonic-gate  * write a registration for the given transport for TLI
2507c478bd9Sstevel@tonic-gate  */
2517c478bd9Sstevel@tonic-gate void
write_nettype_register(char * transp)25261961e0fSrobinson write_nettype_register(char *transp)
2537c478bd9Sstevel@tonic-gate {
2547c478bd9Sstevel@tonic-gate 	list *l;
2557c478bd9Sstevel@tonic-gate 	definition *def;
2567c478bd9Sstevel@tonic-gate 	version_list *vp;
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
2597c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
2607c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
2617c478bd9Sstevel@tonic-gate 			continue;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
264a465d40cSJordan Brown 			f_print(fout, "\tif (!svc_create(\n");
265a465d40cSJordan Brown 			f_print(fout, "\t    ");
2667c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
267a465d40cSJordan Brown 			f_print(fout, ", %s, %s,\n",
268a465d40cSJordan Brown 			    def->def_name, vp->vers_name);
269a465d40cSJordan Brown 			f_print(fout, "\t    \"%s\")) {\n", transp);
270a465d40cSJordan Brown 			print_err_message("\t\t",
271a2f144d1SJordan Brown 			    "unable to create (%s, %s) for %s.",
272a2f144d1SJordan Brown 			    def->def_name, vp->vers_name, transp);
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
write_rest(void)28361961e0fSrobinson 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);
288a465d40cSJordan Brown 		print_err_message("\t\t", "could not create a handle");
2897c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
2907c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
2917c478bd9Sstevel@tonic-gate 		if (timerflag) {
2927c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_rpcpmstart) {\n");
2937c478bd9Sstevel@tonic-gate 			if (mtflag) {
2947c478bd9Sstevel@tonic-gate 				f_print(fout,
2957c478bd9Sstevel@tonic-gate "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
296a465d40cSJordan Brown 				print_err_message("\t\t\t",
297a465d40cSJordan Brown 				    "cannot create closedown thread");
2987c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t\texit(1);\n");
2997c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t}\n");
3007c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3017c478bd9Sstevel@tonic-gate 			} else {
3027c478bd9Sstevel@tonic-gate 				f_print(fout,
3037c478bd9Sstevel@tonic-gate 				"\t\t(void) signal(SIGALRM, %s closedown);\n",
304a2f144d1SJordan Brown 				    Cflag? "(SIG_PF)":"(void(*)())");
3057c478bd9Sstevel@tonic-gate 				f_print(fout,
3067c478bd9Sstevel@tonic-gate "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
3077c478bd9Sstevel@tonic-gate 				f_print(fout, "\t}\n");
3087c478bd9Sstevel@tonic-gate 			}
3097c478bd9Sstevel@tonic-gate 		}
3107c478bd9Sstevel@tonic-gate 	}
3117c478bd9Sstevel@tonic-gate 	f_print(fout, "\tsvc_run();\n");
312a465d40cSJordan Brown 	print_err_message("\t", "svc_run returned");
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
write_programs(char * storage)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
write_real_program(definition * def)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");
361a465d40cSJordan Brown 			if (proc->arg_num < 2 &&
362a465d40cSJordan Brown 			    streq(proc->args.decls->decl.type, "void")) {
363a465d40cSJordan Brown 				f_print(fout, "/* ARGSUSED */\n");
364a465d40cSJordan Brown 			}
3657c478bd9Sstevel@tonic-gate 			if (!mtflag)
3667c478bd9Sstevel@tonic-gate 				internal_proctype(proc);
3677c478bd9Sstevel@tonic-gate 			else
3687c478bd9Sstevel@tonic-gate 				f_print(fout, "int");
3697c478bd9Sstevel@tonic-gate 			f_print(fout, "\n_");
3707c478bd9Sstevel@tonic-gate 			pvname(proc->proc_name, vp->vers_num);
3717c478bd9Sstevel@tonic-gate 			if (Cflag) {
372a465d40cSJordan Brown 				f_print(fout, "(\n");
373a465d40cSJordan Brown 				f_print(fout, "    ");
3747c478bd9Sstevel@tonic-gate 				/* arg name */
3757c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
37661961e0fSrobinson 					/* LINTED variable format */
3777c478bd9Sstevel@tonic-gate 					f_print(fout, proc->args.argname);
3787c478bd9Sstevel@tonic-gate 				else
3797c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
380a2f144d1SJordan Brown 					    proc->args.decls->decl.type, 0);
381a465d40cSJordan Brown 				f_print(fout, " *argp,\n");
3827c478bd9Sstevel@tonic-gate 				if (mtflag) {
383a465d40cSJordan Brown 					f_print(fout, "    ");
3847c478bd9Sstevel@tonic-gate 					ptype(proc->res_prefix,
385a2f144d1SJordan Brown 					    proc->res_type, 1);
386a465d40cSJordan Brown 					f_print(fout, "*%s,\n", RESULT);
3877c478bd9Sstevel@tonic-gate 				}
388a465d40cSJordan Brown 				f_print(fout, "    struct svc_req *%s)\n",
389a465d40cSJordan Brown 				    RQSTP);
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate 			} else {
3927c478bd9Sstevel@tonic-gate 				if (mtflag)
3937c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s, %s)\n",
394a2f144d1SJordan Brown 					    RESULT, RQSTP);
3957c478bd9Sstevel@tonic-gate 				else
3967c478bd9Sstevel@tonic-gate 					f_print(fout, "(argp, %s)\n", RQSTP);
3977c478bd9Sstevel@tonic-gate 				/* arg name */
3987c478bd9Sstevel@tonic-gate 				if (proc->arg_num > 1)
3997c478bd9Sstevel@tonic-gate 					f_print(fout, "\t%s *argp;\n",
400a2f144d1SJordan Brown 					    proc->args.argname);
4017c478bd9Sstevel@tonic-gate 				else {
4027c478bd9Sstevel@tonic-gate 					f_print(fout, "\t");
4037c478bd9Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
404a2f144d1SJordan Brown 					    proc->args.decls->decl.type, 0);
4057c478bd9Sstevel@tonic-gate 					f_print(fout, " *argp;\n");
4067c478bd9Sstevel@tonic-gate 				}
4077c478bd9Sstevel@tonic-gate 				if (mtflag)
4087c478bd9Sstevel@tonic-gate 					f_print(fout, "\tvoid *%s;\n", RESULT);
4097c478bd9Sstevel@tonic-gate 				f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate 			f_print(fout, "{\n");
4137c478bd9Sstevel@tonic-gate 			f_print(fout, "\treturn (");
4147c478bd9Sstevel@tonic-gate 			/* for mtflag, arguments are different */
4157c478bd9Sstevel@tonic-gate 			if (Cflag || mtflag)
4167c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
4177c478bd9Sstevel@tonic-gate 			else
4187c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4197c478bd9Sstevel@tonic-gate 			f_print(fout, "(");
4207c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
421a465d40cSJordan Brown 				/* only print if non-void */
4227c478bd9Sstevel@tonic-gate 				if (!streq(proc->args.decls->decl.type, "void"))
423a465d40cSJordan Brown 					f_print(fout, "*argp, ");
4247c478bd9Sstevel@tonic-gate 			} else {
425a465d40cSJordan Brown 				f_print(fout, "\n");
4267c478bd9Sstevel@tonic-gate 				for (l = proc->args.decls;  l != NULL;
427a2f144d1SJordan Brown 				    l = l->next)
428a465d40cSJordan Brown 					f_print(fout, "\t    argp->%s,\n",
429a2f144d1SJordan Brown 					    l->decl.name);
430a465d40cSJordan Brown 				f_print(fout, "\t    ");
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 			if (mtflag && !oneway)
4337c478bd9Sstevel@tonic-gate 				f_print(fout, "%s, ", RESULT);
4347c478bd9Sstevel@tonic-gate 			f_print(fout, "%s));\n}\n", RQSTP);
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 	}
4377c478bd9Sstevel@tonic-gate }
4387c478bd9Sstevel@tonic-gate 
43961961e0fSrobinson static void
write_program(definition * def,char * storage)44061961e0fSrobinson write_program(definition *def, char *storage)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	version_list *vp;
4437c478bd9Sstevel@tonic-gate 	proc_list *proc;
4447c478bd9Sstevel@tonic-gate 	int filled;
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
4477c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
4487c478bd9Sstevel@tonic-gate 		if (storage != NULL) {
4497c478bd9Sstevel@tonic-gate 			f_print(fout, "%s ", storage);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 		f_print(fout, "void\n");
4527c478bd9Sstevel@tonic-gate 		pvname(def->def_name, vp->vers_num);
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate 		if (Cflag) {
4557c478bd9Sstevel@tonic-gate 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
4567c478bd9Sstevel@tonic-gate 			f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
4577c478bd9Sstevel@tonic-gate 		} else {
4587c478bd9Sstevel@tonic-gate 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
4597c478bd9Sstevel@tonic-gate 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
4607c478bd9Sstevel@tonic-gate 			f_print(fout, "	register SVCXPRT *%s;\n", TRANSP);
4617c478bd9Sstevel@tonic-gate 		}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 		filled = 0;
4667c478bd9Sstevel@tonic-gate 		f_print(fout, "\tunion {\n");
4677c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
4687c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
4697c478bd9Sstevel@tonic-gate 				if (streq(proc->args.decls->decl.type,
470a2f144d1SJordan Brown 				    "void")) {
4717c478bd9Sstevel@tonic-gate 					continue;
4727c478bd9Sstevel@tonic-gate 				}
4737c478bd9Sstevel@tonic-gate 				filled = 1;
4747c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
4757c478bd9Sstevel@tonic-gate 				ptype(proc->args.decls->decl.prefix,
476a2f144d1SJordan Brown 				    proc->args.decls->decl.type, 0);
4777c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4787c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 			} else {
4817c478bd9Sstevel@tonic-gate 				filled = 1;
4827c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t%s", proc->args.argname);
4837c478bd9Sstevel@tonic-gate 				f_print(fout, " ");
4847c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
4857c478bd9Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
4867c478bd9Sstevel@tonic-gate 			}
4877c478bd9Sstevel@tonic-gate 		}
4887c478bd9Sstevel@tonic-gate 		if (!filled) {
4897c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tint fill;\n");
4907c478bd9Sstevel@tonic-gate 		}
4917c478bd9Sstevel@tonic-gate 		f_print(fout, "\t} %s;\n", ARG);
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (mtflag) {
4947c478bd9Sstevel@tonic-gate 			filled = 0;
4957c478bd9Sstevel@tonic-gate 			f_print(fout, "\tunion {\n");
4967c478bd9Sstevel@tonic-gate 			for (proc = vp->procs; proc != NULL;
497a2f144d1SJordan Brown 			    proc = proc->next) {
4987c478bd9Sstevel@tonic-gate 				if (streq(proc->res_type, "void") ||
4997c478bd9Sstevel@tonic-gate 				    streq(proc->res_type, "oneway"))
5007c478bd9Sstevel@tonic-gate 					continue;
5017c478bd9Sstevel@tonic-gate 				filled = 1;
5027c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t");
5037c478bd9Sstevel@tonic-gate 				ptype(proc->res_prefix, proc->res_type, 0);
5047c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
5057c478bd9Sstevel@tonic-gate 				f_print(fout, "_res;\n");
5067c478bd9Sstevel@tonic-gate 			}
5077c478bd9Sstevel@tonic-gate 			if (!filled)
5087c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\tint fill;\n");
5097c478bd9Sstevel@tonic-gate 			f_print(fout, "\t} %s;\n", RESULT);
5107c478bd9Sstevel@tonic-gate 			f_print(fout, "\tbool_t %s;\n", RETVAL);
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 		} else
5137c478bd9Sstevel@tonic-gate 			f_print(fout, "\tchar *%s;\n", RESULT);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 		if (Cflag) {
5167c478bd9Sstevel@tonic-gate 			f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
517a2f144d1SJordan Brown 			    ARG, RESULT);
5187c478bd9Sstevel@tonic-gate 			if (mtflag)
5197c478bd9Sstevel@tonic-gate 				f_print(fout,
5207c478bd9Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
521a2f144d1SJordan Brown 				    ROUTINE);
5227c478bd9Sstevel@tonic-gate 			else
5237c478bd9Sstevel@tonic-gate 				f_print(fout,
5247c478bd9Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n",
525a2f144d1SJordan Brown 				    ROUTINE);
5267c478bd9Sstevel@tonic-gate 		} else {
5277c478bd9Sstevel@tonic-gate 			f_print(fout,
528a2f144d1SJordan Brown 			    "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
529a2f144d1SJordan Brown 			    ARG, RESULT);
5307c478bd9Sstevel@tonic-gate 			if (mtflag)
5317c478bd9Sstevel@tonic-gate 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
5327c478bd9Sstevel@tonic-gate 			else
5337c478bd9Sstevel@tonic-gate 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 		f_print(fout, "\n");
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		if (timerflag) {
5387c478bd9Sstevel@tonic-gate 			if (mtflag)
5397c478bd9Sstevel@tonic-gate 				f_print(fout,
540a465d40cSJordan Brown "\t(void) mutex_lock(&_svcstate_lock);\n");
5417c478bd9Sstevel@tonic-gate 
5427c478bd9Sstevel@tonic-gate 			f_print(fout, "\t_rpcsvccount++;\n");
5437c478bd9Sstevel@tonic-gate 			if (mtflag)
5447c478bd9Sstevel@tonic-gate 				f_print(fout,
545a465d40cSJordan Brown "\t(void) mutex_unlock(&_svcstate_lock);\n");
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
5497c478bd9Sstevel@tonic-gate 		if (!nullproc(vp->procs)) {
5507c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase NULLPROC:\n");
5517c478bd9Sstevel@tonic-gate 			f_print(fout,
5527c478bd9Sstevel@tonic-gate 			    Cflag ?
553a465d40cSJordan Brown "\t\t(void) svc_sendreply(%s,\n\t\t    (xdrproc_t)xdr_void, NULL);\n" :
55461961e0fSrobinson "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
555a2f144d1SJordan Brown 			    TRANSP);
5567c478bd9Sstevel@tonic-gate 			print_return("\t\t");
5577c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
5607c478bd9Sstevel@tonic-gate 			f_print(fout, "\tcase %s:\n", proc->proc_name);
5617c478bd9Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
5627c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.decls->decl.type);
5637c478bd9Sstevel@tonic-gate 			} else {
5647c478bd9Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.argname);
5657c478bd9Sstevel@tonic-gate 			}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 			if (streq(proc->res_type, "oneway")) {
5687c478bd9Sstevel@tonic-gate 				/* One-way call */
5697c478bd9Sstevel@tonic-gate 				f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
5707c478bd9Sstevel@tonic-gate 			} else {
5717c478bd9Sstevel@tonic-gate 				p_xdrfunc(RESULT, proc->res_type);
5727c478bd9Sstevel@tonic-gate 			}
5737c478bd9Sstevel@tonic-gate 			if (Cflag) {
5747c478bd9Sstevel@tonic-gate 				if (mtflag) {
5757c478bd9Sstevel@tonic-gate 					f_print(fout,
5767c478bd9Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*) (char *,  "
5777c478bd9Sstevel@tonic-gate 					    "void *,  struct svc_req *))",
5787c478bd9Sstevel@tonic-gate 					    ROUTINE);
5797c478bd9Sstevel@tonic-gate 				} else {
5807c478bd9Sstevel@tonic-gate 					f_print(fout,
5817c478bd9Sstevel@tonic-gate 					    "\t\t%s = (char *(*)(char *, "
582a465d40cSJordan Brown 					    "struct svc_req *))",
5837c478bd9Sstevel@tonic-gate 					    ROUTINE);
5847c478bd9Sstevel@tonic-gate 				}
5857c478bd9Sstevel@tonic-gate 			} else {
5867c478bd9Sstevel@tonic-gate 				if (mtflag) {
5877c478bd9Sstevel@tonic-gate 					f_print(fout,
588a465d40cSJordan Brown 					    "\t\t%s = (bool_t (*)())",
5897c478bd9Sstevel@tonic-gate 					    ROUTINE);
5907c478bd9Sstevel@tonic-gate 				} else {
591a465d40cSJordan Brown 					f_print(fout, "\t\t%s = (char *(*)())",
5927c478bd9Sstevel@tonic-gate 					    ROUTINE);
5937c478bd9Sstevel@tonic-gate 				}
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 
596a465d40cSJordan Brown 			f_print(fout, "\n\t\t    ");
5977c478bd9Sstevel@tonic-gate 			if (newstyle) { /* new style: calls internal routine */
5987c478bd9Sstevel@tonic-gate 				f_print(fout, "_");
5997c478bd9Sstevel@tonic-gate 			}
6007c478bd9Sstevel@tonic-gate 			if ((Cflag || mtflag) && !newstyle)
6017c478bd9Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
6027c478bd9Sstevel@tonic-gate 			else
6037c478bd9Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
6047c478bd9Sstevel@tonic-gate 			f_print(fout, ";\n");
6057c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n\n");
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
6087c478bd9Sstevel@tonic-gate 		printerr("noproc", TRANSP);
6097c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6107c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 		f_print(fout,
613a2f144d1SJordan Brown 		    "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
614a2f144d1SJordan Brown 		    ARG, ARG);
61561961e0fSrobinson 		printif("getargs", TRANSP, "(caddr_t)&", ARG);
6167c478bd9Sstevel@tonic-gate 		printerr("decode", TRANSP);
6177c478bd9Sstevel@tonic-gate 		print_return("\t\t");
6187c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		if (!mtflag)
6217c478bd9Sstevel@tonic-gate 			if (Cflag)
6227c478bd9Sstevel@tonic-gate 				f_print(fout,
623a2f144d1SJordan Brown 				    "\t%s = (*%s)((char *)&%s, %s);\n",
624a2f144d1SJordan Brown 				    RESULT, ROUTINE, ARG, RQSTP);
6257c478bd9Sstevel@tonic-gate 			else
6267c478bd9Sstevel@tonic-gate 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
627a2f144d1SJordan Brown 				    RESULT, ROUTINE, ARG, RQSTP);
6287c478bd9Sstevel@tonic-gate 		else
6297c478bd9Sstevel@tonic-gate 			if (Cflag)
6307c478bd9Sstevel@tonic-gate 				f_print(fout,
63161961e0fSrobinson "\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
632a2f144d1SJordan Brown 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6337c478bd9Sstevel@tonic-gate 			else
6347c478bd9Sstevel@tonic-gate 				f_print(fout,
63561961e0fSrobinson "\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
636a2f144d1SJordan Brown 				    RETVAL, ROUTINE, ARG, RESULT, RQSTP);
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		if (mtflag)
6427c478bd9Sstevel@tonic-gate 			f_print(fout,
643a465d40cSJordan Brown "\tif (_xdr_%s && %s > 0 &&\n"
644a465d40cSJordan Brown "\t    !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
645a2f144d1SJordan Brown 			    RESULT, RETVAL, TRANSP, RESULT, RESULT);
6467c478bd9Sstevel@tonic-gate 		else
6477c478bd9Sstevel@tonic-gate 			f_print(fout,
648a465d40cSJordan Brown "\tif (_xdr_%s && %s != NULL &&\n"
649a465d40cSJordan Brown "\t    !svc_sendreply(%s, _xdr_%s, %s)) {\n",
650a2f144d1SJordan Brown 			    RESULT, RESULT, TRANSP, RESULT, RESULT);
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 		printerr("systemerr", TRANSP);
6537c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6547c478bd9Sstevel@tonic-gate 
65561961e0fSrobinson 		printif("freeargs", TRANSP, "(caddr_t)&", ARG);
656a465d40cSJordan Brown 		print_err_message("\t\t", "unable to free arguments");
6577c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
6587c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
6597c478bd9Sstevel@tonic-gate 		/* print out free routine */
6607c478bd9Sstevel@tonic-gate 		if (mtflag) {
6617c478bd9Sstevel@tonic-gate 			f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
6627c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\tif (!");
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
665a465d40cSJordan Brown 			f_print(fout, "_freeresult(%s, _xdr_%s,\n",
666a465d40cSJordan Brown 			    TRANSP, RESULT);
667a465d40cSJordan Brown 			f_print(fout, "\t\t    (caddr_t)&%s))\n",
668a465d40cSJordan Brown 			    RESULT);
669a465d40cSJordan Brown 			print_err_message("\t\t\t", "unable to free results");
6707c478bd9Sstevel@tonic-gate 			f_print(fout, "\n");
6717c478bd9Sstevel@tonic-gate 			f_print(fout, "\t}\n");
6727c478bd9Sstevel@tonic-gate 		};
6737c478bd9Sstevel@tonic-gate 		print_return("\t");
6747c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
67861961e0fSrobinson static void
printerr(char * err,char * transp)67961961e0fSrobinson printerr(char *err, char *transp)
6807c478bd9Sstevel@tonic-gate {
6817c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
68461961e0fSrobinson static void
printif(char * proc,char * transp,char * prefix,char * arg)68561961e0fSrobinson printif(char *proc, char *transp, char *prefix, char *arg)
6867c478bd9Sstevel@tonic-gate {
6877c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
688a2f144d1SJordan Brown 	    proc, transp, arg, prefix, arg);
6897c478bd9Sstevel@tonic-gate }
6907c478bd9Sstevel@tonic-gate 
69161961e0fSrobinson int
nullproc(proc_list * proc)69261961e0fSrobinson nullproc(proc_list *proc)
6937c478bd9Sstevel@tonic-gate {
6947c478bd9Sstevel@tonic-gate 	for (; proc != NULL; proc = proc->next) {
69561961e0fSrobinson 		if (streq(proc->proc_num, "0"))
6967c478bd9Sstevel@tonic-gate 			return (1);
6977c478bd9Sstevel@tonic-gate 	}
6987c478bd9Sstevel@tonic-gate 	return (0);
6997c478bd9Sstevel@tonic-gate }
7007c478bd9Sstevel@tonic-gate 
70161961e0fSrobinson static void
write_inetmost(char * infile)70261961e0fSrobinson write_inetmost(char *infile)
7037c478bd9Sstevel@tonic-gate {
7047c478bd9Sstevel@tonic-gate 	f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
7057c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint sock;\n");
7067c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint proto;\n");
7077c478bd9Sstevel@tonic-gate 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
7087c478bd9Sstevel@tonic-gate 	f_print(fout, "\tint asize = sizeof (saddr);\n");
7097c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
7107c478bd9Sstevel@tonic-gate 	f_print(fout,
7117c478bd9Sstevel@tonic-gate 	"\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
7127c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
7137c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
7147c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7157c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
716a465d40cSJordan Brown 	f_print(fout, "\t\t    (char *)&_rpcfdtype, &ssize) == -1)\n");
7177c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
7187c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = 0;\n");
7197c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
7207c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tproto = 0;\n");
7217c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
7227c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else {\n");
7237c478bd9Sstevel@tonic-gate 	write_rpc_svc_fg(infile, "\t\t");
7247c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
7257c478bd9Sstevel@tonic-gate 	print_pmapunset("\t\t");
7267c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}\n");
7277c478bd9Sstevel@tonic-gate }
7287c478bd9Sstevel@tonic-gate 
72961961e0fSrobinson static void
print_return(char * space)73061961e0fSrobinson print_return(char *space)
7317c478bd9Sstevel@tonic-gate {
73261961e0fSrobinson 	if (exitnow) {
7337c478bd9Sstevel@tonic-gate 		f_print(fout, "%sexit(0);\n", space);
73461961e0fSrobinson 		return;
73561961e0fSrobinson 	}
73661961e0fSrobinson 	if (timerflag) {
737a465d40cSJordan Brown 		if (mtflag) {
738a465d40cSJordan Brown 			f_print(fout,
739a465d40cSJordan Brown 			    "%s(void) mutex_lock(&_svcstate_lock);\n",
740a2f144d1SJordan Brown 			    space);
741a465d40cSJordan Brown 		}
74261961e0fSrobinson 		f_print(fout, "%s_rpcsvccount--;\n", space);
74361961e0fSrobinson 		f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
744a465d40cSJordan Brown 		if (mtflag) {
745a465d40cSJordan Brown 			f_print(fout,
746a465d40cSJordan Brown 			    "%s(void) mutex_unlock(&_svcstate_lock);\n",
747a2f144d1SJordan Brown 			    space);
748a465d40cSJordan Brown 		}
7497c478bd9Sstevel@tonic-gate 	}
750a465d40cSJordan Brown 	f_print(fout, "%sreturn; /* CSTYLED */\n", space);
7517c478bd9Sstevel@tonic-gate }
7527c478bd9Sstevel@tonic-gate 
75361961e0fSrobinson static void
print_pmapunset(char * space)75461961e0fSrobinson print_pmapunset(char *space)
7557c478bd9Sstevel@tonic-gate {
7567c478bd9Sstevel@tonic-gate 	list *l;
7577c478bd9Sstevel@tonic-gate 	definition *def;
7587c478bd9Sstevel@tonic-gate 	version_list *vp;
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
76161961e0fSrobinson 		def = (definition *)l->val;
7627c478bd9Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
7637c478bd9Sstevel@tonic-gate 			for (vp = def->def.pr.versions; vp != NULL;
764a2f144d1SJordan Brown 			    vp = vp->next) {
7657c478bd9Sstevel@tonic-gate 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
766a2f144d1SJordan Brown 				    space, def->def_name, vp->vers_name);
7677c478bd9Sstevel@tonic-gate 			}
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate }
7717c478bd9Sstevel@tonic-gate 
77261961e0fSrobinson static void
print_err_message(const char * space,const char * fmt,...)773a465d40cSJordan Brown print_err_message(const char *space, const char *fmt, ...)
7747c478bd9Sstevel@tonic-gate {
775a465d40cSJordan Brown 	char errbuf[ERRBUFLEN];
776a465d40cSJordan Brown 	va_list va;
777a465d40cSJordan Brown 
778a465d40cSJordan Brown 	va_start(va, fmt);
779a465d40cSJordan Brown 	(void) vsnprintf(errbuf, sizeof (errbuf), fmt, va);
780a465d40cSJordan Brown 	va_end(va);
781a465d40cSJordan Brown 
7827c478bd9Sstevel@tonic-gate 	if (logflag)
783a465d40cSJordan Brown 		f_print(fout, "%ssyslog(LOG_ERR, \"%%s\",\n", space);
7847c478bd9Sstevel@tonic-gate 	else if (inetdflag || pmflag)
785a465d40cSJordan Brown 		f_print(fout, "%sRPC_MSGOUT(\"%%s\",\n", space);
7867c478bd9Sstevel@tonic-gate 	else
787a465d40cSJordan Brown 		f_print(fout, "%sfprintf(stderr, \"%%s\",\n", space);
788a465d40cSJordan Brown 	f_print(fout, "%s    \"%s\");\n", space, errbuf);
7897c478bd9Sstevel@tonic-gate }
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate /*
792a465d40cSJordan Brown  * Write the server auxiliary function (RPC_MSGOUT, timeout)
7937c478bd9Sstevel@tonic-gate  */
79461961e0fSrobinson static void
write_svc_aux(int nomain)79561961e0fSrobinson write_svc_aux(int nomain)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	if (!logflag)
7987c478bd9Sstevel@tonic-gate 		write_msg_out();
7997c478bd9Sstevel@tonic-gate 	if (!nomain)
8007c478bd9Sstevel@tonic-gate 		write_timeout_func();
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
804a465d40cSJordan Brown  * Write the RPC_MSGOUT function
805a465d40cSJordan Brown  *
806a465d40cSJordan Brown  * Note that while we define RPC_MSGOUT to be printf-like, all existing
807a465d40cSJordan Brown  * calls are of the form "%s","<msg>" and this implementation assumes that
808a465d40cSJordan Brown  * trivial case.  If in the future it's desirable to generate richer calls
809a465d40cSJordan Brown  * this implementation can change to match.  This way we don't (yet) have
810a465d40cSJordan Brown  * to introduce varargs into the generated code.
8117c478bd9Sstevel@tonic-gate  */
81261961e0fSrobinson static void
write_msg_out(void)81361961e0fSrobinson write_msg_out(void)
8147c478bd9Sstevel@tonic-gate {
8157c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
816a465d40cSJordan Brown 	f_print(fout, "#if\tdefined(RPC_MSGOUT)\n");
817a465d40cSJordan Brown 	if (!Cflag) {
818a465d40cSJordan Brown 		f_print(fout, "extern void RPC_MSGOUT();\n");
819a465d40cSJordan Brown 	} else {
820a465d40cSJordan Brown 		f_print(fout, "extern void RPC_MSGOUT(const char *, ...);\n");
821a465d40cSJordan Brown 	}
822a465d40cSJordan Brown 	f_print(fout, "#else\t/* defined(RPC_MSGOUT) */\n");
82361961e0fSrobinson 	f_print(fout, "static ");
8247c478bd9Sstevel@tonic-gate 	if (!Cflag) {
825a465d40cSJordan Brown 		f_print(fout, "void\nRPC_MSGOUT(fmt, msg)\n");
826a465d40cSJordan Brown 		f_print(fout, "\tchar *fmt;\n");
8277c478bd9Sstevel@tonic-gate 		f_print(fout, "\tchar *msg;\n");
8287c478bd9Sstevel@tonic-gate 	} else {
829a465d40cSJordan Brown 		f_print(fout, "void\nRPC_MSGOUT(const char *fmt, char *msg)\n");
8307c478bd9Sstevel@tonic-gate 	}
8317c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
8327c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifdef RPC_SVC_FG\n");
8337c478bd9Sstevel@tonic-gate 	if (inetdflag || pmflag)
8347c478bd9Sstevel@tonic-gate 		f_print(fout, "\tif (_rpcpmstart)\n");
835a465d40cSJordan Brown 	f_print(fout, "\t\tsyslog(LOG_ERR, fmt, msg);\n");
836a465d40cSJordan Brown 	f_print(fout, "\telse {\n");
837a465d40cSJordan Brown 	f_print(fout, "\t\t(void) fprintf(stderr, fmt, msg);\n");
838a465d40cSJordan Brown 	f_print(fout, "\t\t(void) putc('\\n', stderr);\n");
839a465d40cSJordan Brown 	f_print(fout, "\t}\n");
8407c478bd9Sstevel@tonic-gate 	f_print(fout, "#else\n");
841a465d40cSJordan Brown 	f_print(fout, "\tsyslog(LOG_ERR, fmt, msg);\n");
8427c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
8437c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
844a465d40cSJordan Brown 	f_print(fout, "#endif\t/* defined(RPC_MSGOUT) */\n");
8457c478bd9Sstevel@tonic-gate }
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate /*
8487c478bd9Sstevel@tonic-gate  * Write the timeout function
8497c478bd9Sstevel@tonic-gate  */
85061961e0fSrobinson static void
write_timeout_func(void)85161961e0fSrobinson write_timeout_func(void)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	if (!timerflag)
8547c478bd9Sstevel@tonic-gate 		return;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
8577c478bd9Sstevel@tonic-gate 	if (mtflag) {
8587c478bd9Sstevel@tonic-gate 		f_print(fout, "/*ARGSUSED*/\n");
8597c478bd9Sstevel@tonic-gate 		f_print(fout, "static void *\n");
8607c478bd9Sstevel@tonic-gate 		if (!Cflag) {
8617c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(arg)\n");
8627c478bd9Sstevel@tonic-gate 			f_print(fout, "\tvoid *arg;\n");
8637c478bd9Sstevel@tonic-gate 		} else
8647c478bd9Sstevel@tonic-gate 			f_print(fout, "closedown(void *arg)\n");
8657c478bd9Sstevel@tonic-gate 		f_print(fout, "{\n");
8667c478bd9Sstevel@tonic-gate 		f_print(fout, "\t/*CONSTCOND*/\n");
8677c478bd9Sstevel@tonic-gate 		f_print(fout, "\twhile (1) {\n");
8687c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
8697c478bd9Sstevel@tonic-gate 		f_print(fout,
8707c478bd9Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
8717c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tcontinue;\n\n");
8727c478bd9Sstevel@tonic-gate 		f_print(fout,
8737c478bd9Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
8747c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8757c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tint size;\n");
8767c478bd9Sstevel@tonic-gate 		} else {
8777c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
8787c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tstatic int size;\n");
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
8817c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8827c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
8837c478bd9Sstevel@tonic-gate 		} else {
8847c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\tif (size == 0) {\n");
8857c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
8867c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 		f_print(fout,
8897c478bd9Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
8907c478bd9Sstevel@tonic-gate 		if (tirpcflag) {
8917c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
8927c478bd9Sstevel@tonic-gate 		} else {
8937c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
8947c478bd9Sstevel@tonic-gate 		}
8957c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\t\topenfd++;\n");
8967c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (openfd <= 1)\n");
8977c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t\texit(0);\n");
8987c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t} else\n");
8997c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
900a465d40cSJordan Brown 		f_print(fout, "\t\t(void) mutex_unlock(&_svcstate_lock);\n");
9017c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
9027c478bd9Sstevel@tonic-gate 		f_print(fout, "}\n");
9037c478bd9Sstevel@tonic-gate 		return;
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	f_print(fout, "static void\n");
9077c478bd9Sstevel@tonic-gate 	if (!Cflag) {
9087c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(sig)\n");
9097c478bd9Sstevel@tonic-gate 		f_print(fout, "\tint sig;\n");
9107c478bd9Sstevel@tonic-gate 	} else
9117c478bd9Sstevel@tonic-gate 		f_print(fout, "closedown(int sig)\n");
9127c478bd9Sstevel@tonic-gate 	f_print(fout, "{\n");
9137c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
9147c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9157c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint size;\n");
9167c478bd9Sstevel@tonic-gate 	} else {
9177c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\textern fd_set svc_fdset;\n");
9187c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstatic int size;\n");
9197c478bd9Sstevel@tonic-gate 	}
9207c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tint i, openfd = 0;\n\n");
9217c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9227c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tsize = svc_max_pollfd;\n");
9237c478bd9Sstevel@tonic-gate 	} else {
9247c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (size == 0) {\n");
9257c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
9267c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
9277c478bd9Sstevel@tonic-gate 	}
9287c478bd9Sstevel@tonic-gate 	f_print(fout,
929a2f144d1SJordan Brown 	    "\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
9307c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
9317c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
9327c478bd9Sstevel@tonic-gate 	} else {
9337c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\t\topenfd++;\n");
9367c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (openfd <= 1)\n");
9377c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(0);\n");
9387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t} else\n");
9397c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
942a2f144d1SJordan Brown 	    Cflag? "(SIG_PF)" : "(void(*)())");
9437c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
9447c478bd9Sstevel@tonic-gate 	f_print(fout, "}\n");
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate /*
9487c478bd9Sstevel@tonic-gate  * Write the most of port monitor support
9497c478bd9Sstevel@tonic-gate  */
95061961e0fSrobinson static void
write_pm_most(char * infile,int netflag)95161961e0fSrobinson write_pm_most(char *infile, int netflag)
9527c478bd9Sstevel@tonic-gate {
9537c478bd9Sstevel@tonic-gate 	list *l;
9547c478bd9Sstevel@tonic-gate 	definition *def;
9557c478bd9Sstevel@tonic-gate 	version_list *vp;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
9587c478bd9Sstevel@tonic-gate 	f_print(fout, "\t/*\n");
9597c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
9607c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * that we were started by a port monitor. If\n");
9617c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
9627c478bd9Sstevel@tonic-gate 	f_print(fout, "\t * TLI endpoint.\n");
9637c478bd9Sstevel@tonic-gate 	f_print(fout, "\t */\n");
9647c478bd9Sstevel@tonic-gate 	f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
9657c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tchar *netid;\n");
9667c478bd9Sstevel@tonic-gate 	if (!netflag) {	/* Not included by -n option */
9677c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
9687c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
9697c478bd9Sstevel@tonic-gate 	}
9707c478bd9Sstevel@tonic-gate 	if (timerflag)
9717c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tint pmclose;\n");
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  *  Not necessary, defined in /usr/include/stdlib
9747c478bd9Sstevel@tonic-gate  *  f_print(fout, "\t\textern char *getenv();\n");
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
9777c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
9787c478bd9Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
9797c478bd9Sstevel@tonic-gate 	f_print(fout,
9807c478bd9Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (timerflag) {
9837c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t/* started from inetd */\n");
9847c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t\tpmclose = 1;\n");
9857c478bd9Sstevel@tonic-gate 	}
98661961e0fSrobinson 	f_print(fout, "\t\t} else {\n");
9877c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
988a465d40cSJordan Brown 	print_err_message("\t\t\t\t", "cannot get transport info");
9897c478bd9Sstevel@tonic-gate 	if (timerflag)
9907c478bd9Sstevel@tonic-gate 		f_print(fout,
991a2f144d1SJordan Brown 		    "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
9927c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9937c478bd9Sstevel@tonic-gate 	f_print(fout,
9947c478bd9Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
995a2f144d1SJordan Brown 	    TRANSP);
996a465d40cSJordan Brown 	print_err_message("\t\t\t", "cannot create server handle");
9977c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
9987c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
9997c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tif (nconf)\n");
10007c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
10017c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
10027c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
10037c478bd9Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
10047c478bd9Sstevel@tonic-gate 			continue;
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
10077c478bd9Sstevel@tonic-gate 			f_print(fout,
1008a465d40cSJordan Brown 			    "\t\tif (!svc_reg(%s, %s, %s,\n",
1009a2f144d1SJordan Brown 			    TRANSP, def->def_name, vp->vers_name);
1010a465d40cSJordan Brown 			f_print(fout, "\t\t    ");
10117c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
10127c478bd9Sstevel@tonic-gate 			f_print(fout, ", 0)) {\n");
1013a465d40cSJordan Brown 			print_err_message("\t\t\t",
1014a2f144d1SJordan Brown 			    "unable to register (%s, %s).",
1015a2f144d1SJordan Brown 			    def->def_name, vp->vers_name);
10167c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\texit(1);\n");
10177c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t}\n");
10187c478bd9Sstevel@tonic-gate 		}
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 	if (timerflag) {
10217c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\tif (pmclose) {\n");
10227c478bd9Sstevel@tonic-gate 		if (mtflag) {
10237c478bd9Sstevel@tonic-gate 			f_print(fout,
1024a465d40cSJordan Brown "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t    0, NULL) != 0) {\n");
1025a465d40cSJordan Brown 			print_err_message("\t\t\t\t",
1026a465d40cSJordan Brown 			    "cannot create closedown thread");
10277c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t\texit(1);\n");
10287c478bd9Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
10297c478bd9Sstevel@tonic-gate 		} else {
10307c478bd9Sstevel@tonic-gate 			f_print(fout,
10317c478bd9Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1032a2f144d1SJordan Brown 			    Cflag? "(SIG_PF)" : "(void(*)())");
10337c478bd9Sstevel@tonic-gate 			f_print(fout,
10347c478bd9Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\tsvc_run();\n");
10397c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n");
10407c478bd9Sstevel@tonic-gate 	f_print(fout, "\t\t/* NOTREACHED */\n");
10417c478bd9Sstevel@tonic-gate 	f_print(fout, "\t}");
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
10447c478bd9Sstevel@tonic-gate /*
10457c478bd9Sstevel@tonic-gate  * Support for backgrounding the server if self started.
10467c478bd9Sstevel@tonic-gate  */
104761961e0fSrobinson static void
write_rpc_svc_fg(char * infile,char * sp)104861961e0fSrobinson write_rpc_svc_fg(char *infile, char *sp)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	f_print(fout, "#ifndef RPC_SVC_FG\n");
10517c478bd9Sstevel@tonic-gate 	f_print(fout, "#pragma weak closefrom\n");
10527c478bd9Sstevel@tonic-gate 	f_print(fout, "%sextern void closefrom();\n", sp);
10537c478bd9Sstevel@tonic-gate 	f_print(fout, "%sint size;\n", sp);
10547c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10557c478bd9Sstevel@tonic-gate 		f_print(fout, "%sstruct rlimit rl;\n", sp);
10567c478bd9Sstevel@tonic-gate 	if (inetdflag)
10577c478bd9Sstevel@tonic-gate 		f_print(fout, "%sint pid, i;\n\n", sp);
10587c478bd9Sstevel@tonic-gate 	f_print(fout, "%spid = fork();\n", sp);
10597c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid < 0) {\n", sp);
10607c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
10617c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(1);\n", sp);
10627c478bd9Sstevel@tonic-gate 	f_print(fout, "%s}\n", sp);
10637c478bd9Sstevel@tonic-gate 	f_print(fout, "%sif (pid)\n", sp);
10647c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\texit(0);\n", sp);
10657c478bd9Sstevel@tonic-gate 	/* close all file descriptors */
10667c478bd9Sstevel@tonic-gate 	if (tirpcflag) {
10677c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (closefrom != NULL)\n", sp);
10687c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tclosefrom(0);\n", sp);
10697c478bd9Sstevel@tonic-gate 		f_print(fout, "%selse {\n", sp);
10707c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
10717c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
10727c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
10737c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\texit(1);\n", sp);
10747c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10757c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10767c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10777c478bd9Sstevel@tonic-gate 	} else {
10787c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tsize = getdtablesize();\n", sp);
10797c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
10807c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 	/* Redirect stderr and stdout to /dev/null */
10837c478bd9Sstevel@tonic-gate 	f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
10847c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
10857c478bd9Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
10867c478bd9Sstevel@tonic-gate 	/* This removes control of the controlling terminal */
10877c478bd9Sstevel@tonic-gate 	if (tirpcflag)
10887c478bd9Sstevel@tonic-gate 		f_print(fout, "%ssetsid();\n", sp);
10897c478bd9Sstevel@tonic-gate 	else {
10907c478bd9Sstevel@tonic-gate 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
10917c478bd9Sstevel@tonic-gate 		f_print(fout, "%sif (i >= 0) {\n", sp);
10927c478bd9Sstevel@tonic-gate 		f_print(fout,
1093a2f144d1SJordan Brown 		    "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
10947c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\t(void) close(i);\n", sp);
10957c478bd9Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 	if (!logflag)
10987c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
10997c478bd9Sstevel@tonic-gate 	f_print(fout, "#endif\n");
11007c478bd9Sstevel@tonic-gate 	if (logflag)
11017c478bd9Sstevel@tonic-gate 		open_log_file(infile, sp);
11027c478bd9Sstevel@tonic-gate }
11037c478bd9Sstevel@tonic-gate 
110461961e0fSrobinson static void
open_log_file(char * infile,char * sp)110561961e0fSrobinson open_log_file(char *infile, char *sp)
11067c478bd9Sstevel@tonic-gate {
11077c478bd9Sstevel@tonic-gate 	char *s;
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	s = strrchr(infile, '.');
11107c478bd9Sstevel@tonic-gate 	if (s)
11117c478bd9Sstevel@tonic-gate 		*s = '\0';
11127c478bd9Sstevel@tonic-gate 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
11137c478bd9Sstevel@tonic-gate 	if (s)
11147c478bd9Sstevel@tonic-gate 		*s = '.';
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * write a registration for the given transport for Inetd
11197c478bd9Sstevel@tonic-gate  */
11207c478bd9Sstevel@tonic-gate void
write_inetd_register(char * transp)112161961e0fSrobinson write_inetd_register(char *transp)
11227c478bd9Sstevel@tonic-gate {
11237c478bd9Sstevel@tonic-gate 	list *l;
11247c478bd9Sstevel@tonic-gate 	definition *def;
11257c478bd9Sstevel@tonic-gate 	version_list *vp;
11267c478bd9Sstevel@tonic-gate 	char *sp;
11277c478bd9Sstevel@tonic-gate 	int isudp;
11287c478bd9Sstevel@tonic-gate 	char tmpbuf[32];
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate 	if (inetdflag)
11317c478bd9Sstevel@tonic-gate 		sp = "\t";
11327c478bd9Sstevel@tonic-gate 	else
11337c478bd9Sstevel@tonic-gate 		sp = "";
11347c478bd9Sstevel@tonic-gate 	if (streq(transp, "udp"))
11357c478bd9Sstevel@tonic-gate 		isudp = 1;
11367c478bd9Sstevel@tonic-gate 	else
11377c478bd9Sstevel@tonic-gate 		isudp = 0;
11387c478bd9Sstevel@tonic-gate 	f_print(fout, "\n");
11397c478bd9Sstevel@tonic-gate 	if (inetdflag) {
11407c478bd9Sstevel@tonic-gate 		f_print(fout,
1141a2f144d1SJordan Brown 		    "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1142a2f144d1SJordan Brown 		    isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc%s_create(%s",
1145a2f144d1SJordan Brown 	    sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
11467c478bd9Sstevel@tonic-gate 	if (!isudp)
11477c478bd9Sstevel@tonic-gate 		f_print(fout, ", 0, 0");
11487c478bd9Sstevel@tonic-gate 	f_print(fout, ");\n");
11497c478bd9Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
115061961e0fSrobinson 	(void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
1151a465d40cSJordan Brown 	print_err_message(tmpbuf, "cannot create %s service.", transp);
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 	if (inetdflag) {
11567c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
11577c478bd9Sstevel@tonic-gate 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
1158a2f144d1SJordan Brown 		    sp, isudp ? "UDP": "TCP");
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
11617c478bd9Sstevel@tonic-gate 		def = (definition *) l->val;
116261961e0fSrobinson 		if (def->def_kind != DEF_PROGRAM)
11637c478bd9Sstevel@tonic-gate 			continue;
11647c478bd9Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1165a465d40cSJordan Brown 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s,\n",
1166a2f144d1SJordan Brown 			    sp, TRANSP, def->def_name, vp->vers_name);
1167a465d40cSJordan Brown 			f_print(fout, "%s\t    ", sp);
11687c478bd9Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
11697c478bd9Sstevel@tonic-gate 			if (inetdflag)
11707c478bd9Sstevel@tonic-gate 				f_print(fout, ", proto)) {\n");
11717c478bd9Sstevel@tonic-gate 			else
11727c478bd9Sstevel@tonic-gate 				f_print(fout, ", IPPROTO_%s)) {\n",
1173a2f144d1SJordan Brown 				    isudp ? "UDP": "TCP");
1174a465d40cSJordan Brown 			print_err_message(tmpbuf,
1175a2f144d1SJordan Brown 			    "unable to register (%s, %s, %s).",
1176a2f144d1SJordan Brown 			    def->def_name, vp->vers_name, transp);
11777c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
11787c478bd9Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
11797c478bd9Sstevel@tonic-gate 		}
11807c478bd9Sstevel@tonic-gate 	}
11817c478bd9Sstevel@tonic-gate 	if (inetdflag)
11827c478bd9Sstevel@tonic-gate 		f_print(fout, "\t}\n");
11837c478bd9Sstevel@tonic-gate }
1184