1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
26*0Sstevel@tonic-gate /* All Rights Reserved */
27*0Sstevel@tonic-gate /*
28*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
29*0Sstevel@tonic-gate  * The Regents of the University of California
30*0Sstevel@tonic-gate  * All Rights Reserved
31*0Sstevel@tonic-gate  *
32*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
33*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
34*0Sstevel@tonic-gate  * contributors.
35*0Sstevel@tonic-gate  */
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate /*
40*0Sstevel@tonic-gate  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
41*0Sstevel@tonic-gate  */
42*0Sstevel@tonic-gate #include <stdio.h>
43*0Sstevel@tonic-gate #include <string.h>
44*0Sstevel@tonic-gate #include "rpc_parse.h"
45*0Sstevel@tonic-gate #include "rpc_util.h"
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate static char RQSTP[] = "rqstp";
48*0Sstevel@tonic-gate static char TRANSP[] = "transp";
49*0Sstevel@tonic-gate static char ARG[] = "argument";
50*0Sstevel@tonic-gate static char RESULT[] = "result";
51*0Sstevel@tonic-gate static char ROUTINE[] = "local";
52*0Sstevel@tonic-gate static char ONEWAY_ROUTINE[] = "oneway_local";
53*0Sstevel@tonic-gate static char RETVAL[] = "retval";
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate char _errbuf[256];	/* For all messages */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate void internal_proctype();
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate static
60*0Sstevel@tonic-gate p_xdrfunc(rname, typename)
61*0Sstevel@tonic-gate char *rname;
62*0Sstevel@tonic-gate char *typename;
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	if (Cflag)
65*0Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n",
66*0Sstevel@tonic-gate 			rname, stringfix(typename));
67*0Sstevel@tonic-gate 	else
68*0Sstevel@tonic-gate 		f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
69*0Sstevel@tonic-gate 			rname, stringfix(typename));
70*0Sstevel@tonic-gate }
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate void
73*0Sstevel@tonic-gate internal_proctype(plist)
74*0Sstevel@tonic-gate 	proc_list *plist;
75*0Sstevel@tonic-gate {
76*0Sstevel@tonic-gate 	f_print(fout, "static ");
77*0Sstevel@tonic-gate 	ptype(plist->res_prefix, plist->res_type, 1);
78*0Sstevel@tonic-gate 	f_print(fout, "*");
79*0Sstevel@tonic-gate }
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 
82*0Sstevel@tonic-gate static void
83*0Sstevel@tonic-gate write_mtauto()
84*0Sstevel@tonic-gate {
85*0Sstevel@tonic-gate 	f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
86*0Sstevel@tonic-gate 	f_print(fout, "\t\t_msgout(\"%s\");\n",
87*0Sstevel@tonic-gate 				"unable to set automatic MT mode.");
88*0Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n\t}\n");
89*0Sstevel@tonic-gate }
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * write most of the service, that is, everything but the registrations.
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate void
95*0Sstevel@tonic-gate write_most(infile, netflag, nomain)
96*0Sstevel@tonic-gate 	char *infile;		/* our name */
97*0Sstevel@tonic-gate 	int netflag;
98*0Sstevel@tonic-gate 	int nomain;
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate 	if (inetdflag || pmflag) {
101*0Sstevel@tonic-gate 		char *var_type;
102*0Sstevel@tonic-gate 		var_type = (nomain? "extern" : "static");
103*0Sstevel@tonic-gate 		f_print(fout, "%s int _rpcpmstart;", var_type);
104*0Sstevel@tonic-gate 		f_print(fout, "\t\t/* Started by a port monitor ? */\n");
105*0Sstevel@tonic-gate 		if (!tirpcflag) {
106*0Sstevel@tonic-gate 			f_print(fout, "%s int _rpcfdtype;", var_type);
107*0Sstevel@tonic-gate 			f_print(fout,
108*0Sstevel@tonic-gate "\n\t\t /* Whether Stream or Datagram ? */\n");
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 		if (timerflag) {
112*0Sstevel@tonic-gate 			f_print(fout,
113*0Sstevel@tonic-gate "\n/* States a server can be in wrt request */\n\n");
114*0Sstevel@tonic-gate 			f_print(fout, "#define\t_IDLE 0\n");
115*0Sstevel@tonic-gate 			f_print(fout, "#define\t_SERVED 1\n\n");
116*0Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvcstate = _IDLE;");
117*0Sstevel@tonic-gate 			f_print(fout,
118*0Sstevel@tonic-gate "\t/* Set when a request is serviced */\n");
119*0Sstevel@tonic-gate 			f_print(fout, "static int _rpcsvccount = 0;");
120*0Sstevel@tonic-gate 			f_print(fout,
121*0Sstevel@tonic-gate "\t\t/* Number of requests being serviced */\n");
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 			if (mtflag) {
124*0Sstevel@tonic-gate 				f_print(fout, "mutex_t _svcstate_lock;");
125*0Sstevel@tonic-gate 				f_print(fout,
126*0Sstevel@tonic-gate "\t\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
127*0Sstevel@tonic-gate 
128*0Sstevel@tonic-gate 			}
129*0Sstevel@tonic-gate 		}
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 		write_svc_aux(nomain);
132*0Sstevel@tonic-gate 	}
133*0Sstevel@tonic-gate 	/* write out dispatcher and stubs */
134*0Sstevel@tonic-gate 	write_programs(nomain? (char *)NULL : "static");
135*0Sstevel@tonic-gate 
136*0Sstevel@tonic-gate 	if (nomain)
137*0Sstevel@tonic-gate 		return;
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	f_print(fout, "\nmain()\n");
140*0Sstevel@tonic-gate 	f_print(fout, "{\n");
141*0Sstevel@tonic-gate 	if (inetdflag) {
142*0Sstevel@tonic-gate 		write_inetmost(infile);
143*0Sstevel@tonic-gate 		/* Includes call to write_rpc_svc_fg() */
144*0Sstevel@tonic-gate 	} else {
145*0Sstevel@tonic-gate 		if (tirpcflag) {
146*0Sstevel@tonic-gate 			if (netflag) {
147*0Sstevel@tonic-gate 				f_print(fout,
148*0Sstevel@tonic-gate 					"\tregister SVCXPRT *%s;\n", TRANSP);
149*0Sstevel@tonic-gate 				f_print(fout,
150*0Sstevel@tonic-gate 					"\tstruct netconfig *nconf = NULL;\n");
151*0Sstevel@tonic-gate 			}
152*0Sstevel@tonic-gate 			f_print(fout, "\tpid_t pid;\n");
153*0Sstevel@tonic-gate 			f_print(fout, "\tint i;\n");
154*0Sstevel@tonic-gate 			if (mtauto) {
155*0Sstevel@tonic-gate 				f_print(fout,
156*0Sstevel@tonic-gate 					"\tint mode = RPC_SVC_MT_AUTO;\n\n");
157*0Sstevel@tonic-gate 				write_mtauto();
158*0Sstevel@tonic-gate 			} else
159*0Sstevel@tonic-gate 				f_print(fout, "\n");
160*0Sstevel@tonic-gate 
161*0Sstevel@tonic-gate 			if (mtflag & timerflag)
162*0Sstevel@tonic-gate 				f_print(fout,
163*0Sstevel@tonic-gate "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 			write_pm_most(infile, netflag);
166*0Sstevel@tonic-gate 			f_print(fout, "\telse {\n");
167*0Sstevel@tonic-gate 			write_rpc_svc_fg(infile, "\t\t");
168*0Sstevel@tonic-gate 			f_print(fout, "\t}\n");
169*0Sstevel@tonic-gate 		} else {
170*0Sstevel@tonic-gate 			f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
171*0Sstevel@tonic-gate 			f_print(fout, "\n");
172*0Sstevel@tonic-gate 			print_pmapunset("\t");
173*0Sstevel@tonic-gate 		}
174*0Sstevel@tonic-gate 	}
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	if (logflag && !inetdflag) {
177*0Sstevel@tonic-gate 		open_log_file(infile, "\t");
178*0Sstevel@tonic-gate 	}
179*0Sstevel@tonic-gate }
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate /*
182*0Sstevel@tonic-gate  * write a registration for the given transport
183*0Sstevel@tonic-gate  */
184*0Sstevel@tonic-gate void
185*0Sstevel@tonic-gate write_netid_register(transp)
186*0Sstevel@tonic-gate 	char *transp;
187*0Sstevel@tonic-gate {
188*0Sstevel@tonic-gate 	list *l;
189*0Sstevel@tonic-gate 	definition *def;
190*0Sstevel@tonic-gate 	version_list *vp;
191*0Sstevel@tonic-gate 	char *sp;
192*0Sstevel@tonic-gate 	char tmpbuf[32];
193*0Sstevel@tonic-gate 
194*0Sstevel@tonic-gate 	sp = "";
195*0Sstevel@tonic-gate 	f_print(fout, "\n");
196*0Sstevel@tonic-gate 	f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
197*0Sstevel@tonic-gate 	f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
198*0Sstevel@tonic-gate 	(void) sprintf(_errbuf, "cannot find %s netid.", transp);
199*0Sstevel@tonic-gate 	sprintf(tmpbuf, "%s\t\t", sp);
200*0Sstevel@tonic-gate 	print_err_message(tmpbuf);
201*0Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
202*0Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
203*0Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
204*0Sstevel@tonic-gate 			sp, TRANSP, transp);
205*0Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
206*0Sstevel@tonic-gate 	(void) sprintf(_errbuf, "cannot create %s service.", transp);
207*0Sstevel@tonic-gate 	print_err_message(tmpbuf);
208*0Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
209*0Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
210*0Sstevel@tonic-gate 
211*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
212*0Sstevel@tonic-gate 		def = (definition *) l->val;
213*0Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
214*0Sstevel@tonic-gate 			continue;
215*0Sstevel@tonic-gate 		}
216*0Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
217*0Sstevel@tonic-gate 			f_print(fout,
218*0Sstevel@tonic-gate 				"%s\t(void) rpcb_unset(%s, %s, nconf);\n",
219*0Sstevel@tonic-gate 				sp, def->def_name, vp->vers_name);
220*0Sstevel@tonic-gate 			f_print(fout,
221*0Sstevel@tonic-gate 				"%s\tif (!svc_reg(%s, %s, %s, ",
222*0Sstevel@tonic-gate 				sp, TRANSP, def->def_name, vp->vers_name);
223*0Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
224*0Sstevel@tonic-gate 			f_print(fout, ", nconf)) {\n");
225*0Sstevel@tonic-gate 			(void) sprintf(_errbuf,
226*0Sstevel@tonic-gate 				"unable to register (%s, %s, %s).",
227*0Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
228*0Sstevel@tonic-gate 			print_err_message(tmpbuf);
229*0Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
230*0Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
231*0Sstevel@tonic-gate 		}
232*0Sstevel@tonic-gate 	}
233*0Sstevel@tonic-gate 	f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate /*
237*0Sstevel@tonic-gate  * write a registration for the given transport for TLI
238*0Sstevel@tonic-gate  */
239*0Sstevel@tonic-gate void
240*0Sstevel@tonic-gate write_nettype_register(transp)
241*0Sstevel@tonic-gate 	char *transp;
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	list *l;
244*0Sstevel@tonic-gate 	definition *def;
245*0Sstevel@tonic-gate 	version_list *vp;
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
248*0Sstevel@tonic-gate 		def = (definition *) l->val;
249*0Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
250*0Sstevel@tonic-gate 			continue;
251*0Sstevel@tonic-gate 		}
252*0Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
253*0Sstevel@tonic-gate 			f_print(fout, "\tif (!svc_create(");
254*0Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
255*0Sstevel@tonic-gate 			f_print(fout, ", %s, %s, \"%s\")) {\n",
256*0Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
257*0Sstevel@tonic-gate 			(void) sprintf(_errbuf,
258*0Sstevel@tonic-gate 				"unable to create (%s, %s) for %s.",
259*0Sstevel@tonic-gate 					def->def_name, vp->vers_name, transp);
260*0Sstevel@tonic-gate 			print_err_message("\t\t");
261*0Sstevel@tonic-gate 			f_print(fout, "\t\texit(1);\n");
262*0Sstevel@tonic-gate 			f_print(fout, "\t}\n");
263*0Sstevel@tonic-gate 		}
264*0Sstevel@tonic-gate 	}
265*0Sstevel@tonic-gate }
266*0Sstevel@tonic-gate 
267*0Sstevel@tonic-gate /*
268*0Sstevel@tonic-gate  * write the rest of the service
269*0Sstevel@tonic-gate  */
270*0Sstevel@tonic-gate void
271*0Sstevel@tonic-gate write_rest()
272*0Sstevel@tonic-gate {
273*0Sstevel@tonic-gate 	f_print(fout, "\n");
274*0Sstevel@tonic-gate 	if (inetdflag) {
275*0Sstevel@tonic-gate 		f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
276*0Sstevel@tonic-gate 		(void) sprintf(_errbuf, "could not create a handle");
277*0Sstevel@tonic-gate 		print_err_message("\t\t");
278*0Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
279*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
280*0Sstevel@tonic-gate 		if (timerflag) {
281*0Sstevel@tonic-gate 			f_print(fout, "\tif (_rpcpmstart) {\n");
282*0Sstevel@tonic-gate 			if (mtflag) {
283*0Sstevel@tonic-gate 				f_print(fout,
284*0Sstevel@tonic-gate "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
285*0Sstevel@tonic-gate 				f_print(fout,
286*0Sstevel@tonic-gate "\t\t\t_msgout(\"cannot create closedown thread\");\n");
287*0Sstevel@tonic-gate 				f_print(fout, "\t\t\texit(1);\n");
288*0Sstevel@tonic-gate 				f_print(fout, "\t\t}\n");
289*0Sstevel@tonic-gate 				f_print(fout, "\t}\n");
290*0Sstevel@tonic-gate 			} else {
291*0Sstevel@tonic-gate 				f_print(fout,
292*0Sstevel@tonic-gate 				"\t\t(void) signal(SIGALRM, %s closedown);\n",
293*0Sstevel@tonic-gate 					Cflag? "(SIG_PF)":"(void(*)())");
294*0Sstevel@tonic-gate 				f_print(fout,
295*0Sstevel@tonic-gate "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
296*0Sstevel@tonic-gate 				f_print(fout, "\t}\n");
297*0Sstevel@tonic-gate 			}
298*0Sstevel@tonic-gate 		}
299*0Sstevel@tonic-gate 	}
300*0Sstevel@tonic-gate 	f_print(fout, "\tsvc_run();\n");
301*0Sstevel@tonic-gate 	(void) sprintf(_errbuf, "svc_run returned");
302*0Sstevel@tonic-gate 	print_err_message("\t");
303*0Sstevel@tonic-gate 	f_print(fout, "\texit(1);\n");
304*0Sstevel@tonic-gate 	f_print(fout, "\t/* NOTREACHED */\n");
305*0Sstevel@tonic-gate 	f_print(fout, "}\n");
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate void
309*0Sstevel@tonic-gate write_programs(storage)
310*0Sstevel@tonic-gate 	char *storage;
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	list *l;
313*0Sstevel@tonic-gate 	definition *def;
314*0Sstevel@tonic-gate 
315*0Sstevel@tonic-gate 	/* write out stubs for procedure  definitions */
316*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
317*0Sstevel@tonic-gate 		def = (definition *) l->val;
318*0Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
319*0Sstevel@tonic-gate 			write_real_program(def);
320*0Sstevel@tonic-gate 		}
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	/* write out dispatcher for each program */
324*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
325*0Sstevel@tonic-gate 		def = (definition *) l->val;
326*0Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
327*0Sstevel@tonic-gate 			write_program(def, storage);
328*0Sstevel@tonic-gate 		}
329*0Sstevel@tonic-gate 	}
330*0Sstevel@tonic-gate 
331*0Sstevel@tonic-gate 
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate /*
335*0Sstevel@tonic-gate  * write out definition of internal function (e.g. _printmsg_1(...))
336*0Sstevel@tonic-gate  *  which calls server's defintion of actual function (e.g. printmsg_1(...)).
337*0Sstevel@tonic-gate  *  Unpacks single user argument of printmsg_1 to call-by-value format
338*0Sstevel@tonic-gate  *  expected by printmsg_1.
339*0Sstevel@tonic-gate  */
340*0Sstevel@tonic-gate static
341*0Sstevel@tonic-gate write_real_program(def)
342*0Sstevel@tonic-gate 	definition *def;
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate 	version_list *vp;
345*0Sstevel@tonic-gate 	proc_list *proc;
346*0Sstevel@tonic-gate 	decl_list *l;
347*0Sstevel@tonic-gate 
348*0Sstevel@tonic-gate 	if (!newstyle)
349*0Sstevel@tonic-gate 		return;  /* not needed for old style */
350*0Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
351*0Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
352*0Sstevel@tonic-gate 			int oneway = streq(proc->res_type, "oneway");
353*0Sstevel@tonic-gate 
354*0Sstevel@tonic-gate 			f_print(fout, "\n");
355*0Sstevel@tonic-gate 			if (!mtflag)
356*0Sstevel@tonic-gate 				internal_proctype(proc);
357*0Sstevel@tonic-gate 			else
358*0Sstevel@tonic-gate 				f_print(fout, "int");
359*0Sstevel@tonic-gate 			f_print(fout, "\n_");
360*0Sstevel@tonic-gate 			pvname(proc->proc_name, vp->vers_num);
361*0Sstevel@tonic-gate 			if (Cflag) {
362*0Sstevel@tonic-gate 				f_print(fout, "(");
363*0Sstevel@tonic-gate 				/* arg name */
364*0Sstevel@tonic-gate 				if (proc->arg_num > 1)
365*0Sstevel@tonic-gate 					f_print(fout, proc->args.argname);
366*0Sstevel@tonic-gate 				else
367*0Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
368*0Sstevel@tonic-gate 						proc->args.decls->decl.type, 0);
369*0Sstevel@tonic-gate 				if (mtflag) {
370*0Sstevel@tonic-gate 					f_print(fout, " *argp, ");
371*0Sstevel@tonic-gate 					ptype(proc->res_prefix,
372*0Sstevel@tonic-gate 							proc->res_type, 1);
373*0Sstevel@tonic-gate 					f_print(fout,
374*0Sstevel@tonic-gate 						"*%s, struct svc_req *%s)\n",
375*0Sstevel@tonic-gate 						RESULT, RQSTP);
376*0Sstevel@tonic-gate 				}
377*0Sstevel@tonic-gate 				else
378*0Sstevel@tonic-gate 					f_print(fout,
379*0Sstevel@tonic-gate 						" *argp, struct svc_req *%s)\n",
380*0Sstevel@tonic-gate 						RQSTP);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 			} else {
383*0Sstevel@tonic-gate 				if (mtflag)
384*0Sstevel@tonic-gate 					f_print(fout, "(argp, %s, %s)\n",
385*0Sstevel@tonic-gate 							RESULT, RQSTP);
386*0Sstevel@tonic-gate 				else
387*0Sstevel@tonic-gate 					f_print(fout, "(argp, %s)\n", RQSTP);
388*0Sstevel@tonic-gate 				/* arg name */
389*0Sstevel@tonic-gate 				if (proc->arg_num > 1)
390*0Sstevel@tonic-gate 					f_print(fout, "\t%s *argp;\n",
391*0Sstevel@tonic-gate 						proc->args.argname);
392*0Sstevel@tonic-gate 				else {
393*0Sstevel@tonic-gate 					f_print(fout, "\t");
394*0Sstevel@tonic-gate 					ptype(proc->args.decls->decl.prefix,
395*0Sstevel@tonic-gate 						proc->args.decls->decl.type, 0);
396*0Sstevel@tonic-gate 					f_print(fout, " *argp;\n");
397*0Sstevel@tonic-gate 				}
398*0Sstevel@tonic-gate 				if (mtflag)
399*0Sstevel@tonic-gate 					f_print(fout, "\tvoid *%s;\n", RESULT);
400*0Sstevel@tonic-gate 				f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
401*0Sstevel@tonic-gate 			}
402*0Sstevel@tonic-gate 
403*0Sstevel@tonic-gate 			f_print(fout, "{\n");
404*0Sstevel@tonic-gate 			f_print(fout, "\treturn (");
405*0Sstevel@tonic-gate 			/* for mtflag, arguments are different */
406*0Sstevel@tonic-gate 			if (Cflag || mtflag)
407*0Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
408*0Sstevel@tonic-gate 			else
409*0Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
410*0Sstevel@tonic-gate 			f_print(fout, "(");
411*0Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
412*0Sstevel@tonic-gate 				if (!streq(proc->args.decls->decl.type, "void"))
413*0Sstevel@tonic-gate 					f_print(fout, "*argp, "); /* non-void */
414*0Sstevel@tonic-gate 			} else {
415*0Sstevel@tonic-gate 				for (l = proc->args.decls;  l != NULL;
416*0Sstevel@tonic-gate 								l = l->next)
417*0Sstevel@tonic-gate 					f_print(fout, "argp->%s, ",
418*0Sstevel@tonic-gate 						l->decl.name);
419*0Sstevel@tonic-gate 			}
420*0Sstevel@tonic-gate 			if (mtflag && !oneway)
421*0Sstevel@tonic-gate 				f_print(fout, "%s, ", RESULT);
422*0Sstevel@tonic-gate 			f_print(fout, "%s));\n}\n", RQSTP);
423*0Sstevel@tonic-gate 		}
424*0Sstevel@tonic-gate 	}
425*0Sstevel@tonic-gate }
426*0Sstevel@tonic-gate 
427*0Sstevel@tonic-gate static
428*0Sstevel@tonic-gate write_program(def, storage)
429*0Sstevel@tonic-gate 	definition *def;
430*0Sstevel@tonic-gate 	char *storage;
431*0Sstevel@tonic-gate {
432*0Sstevel@tonic-gate 	version_list *vp;
433*0Sstevel@tonic-gate 	proc_list *proc;
434*0Sstevel@tonic-gate 	int filled;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 	for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
437*0Sstevel@tonic-gate 		f_print(fout, "\n");
438*0Sstevel@tonic-gate 		if (storage != NULL) {
439*0Sstevel@tonic-gate 			f_print(fout, "%s ", storage);
440*0Sstevel@tonic-gate 		}
441*0Sstevel@tonic-gate 		f_print(fout, "void\n");
442*0Sstevel@tonic-gate 		pvname(def->def_name, vp->vers_num);
443*0Sstevel@tonic-gate 
444*0Sstevel@tonic-gate 		if (Cflag) {
445*0Sstevel@tonic-gate 			f_print(fout, "(struct svc_req *%s, ", RQSTP);
446*0Sstevel@tonic-gate 			f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
447*0Sstevel@tonic-gate 		} else {
448*0Sstevel@tonic-gate 			f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
449*0Sstevel@tonic-gate 			f_print(fout, "	struct svc_req *%s;\n", RQSTP);
450*0Sstevel@tonic-gate 			f_print(fout, "	register SVCXPRT *%s;\n", TRANSP);
451*0Sstevel@tonic-gate 		}
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate 		f_print(fout, "{\n");
454*0Sstevel@tonic-gate 
455*0Sstevel@tonic-gate 		filled = 0;
456*0Sstevel@tonic-gate 		f_print(fout, "\tunion {\n");
457*0Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
458*0Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
459*0Sstevel@tonic-gate 				if (streq(proc->args.decls->decl.type,
460*0Sstevel@tonic-gate 								"void")) {
461*0Sstevel@tonic-gate 					continue;
462*0Sstevel@tonic-gate 				}
463*0Sstevel@tonic-gate 				filled = 1;
464*0Sstevel@tonic-gate 				f_print(fout, "\t\t");
465*0Sstevel@tonic-gate 				ptype(proc->args.decls->decl.prefix,
466*0Sstevel@tonic-gate 					proc->args.decls->decl.type, 0);
467*0Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
468*0Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
469*0Sstevel@tonic-gate 
470*0Sstevel@tonic-gate 			} else {
471*0Sstevel@tonic-gate 				filled = 1;
472*0Sstevel@tonic-gate 				f_print(fout, "\t\t%s", proc->args.argname);
473*0Sstevel@tonic-gate 				f_print(fout, " ");
474*0Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
475*0Sstevel@tonic-gate 				f_print(fout, "_arg;\n");
476*0Sstevel@tonic-gate 			}
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 		if (!filled) {
479*0Sstevel@tonic-gate 			f_print(fout, "\t\tint fill;\n");
480*0Sstevel@tonic-gate 		}
481*0Sstevel@tonic-gate 		f_print(fout, "\t} %s;\n", ARG);
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 		if (mtflag) {
484*0Sstevel@tonic-gate 			filled = 0;
485*0Sstevel@tonic-gate 			f_print(fout, "\tunion {\n");
486*0Sstevel@tonic-gate 			for (proc = vp->procs; proc != NULL;
487*0Sstevel@tonic-gate 							proc = proc->next) {
488*0Sstevel@tonic-gate 				if (streq(proc->res_type, "void") ||
489*0Sstevel@tonic-gate 				    streq(proc->res_type, "oneway"))
490*0Sstevel@tonic-gate 					continue;
491*0Sstevel@tonic-gate 				filled = 1;
492*0Sstevel@tonic-gate 				f_print(fout, "\t\t");
493*0Sstevel@tonic-gate 				ptype(proc->res_prefix, proc->res_type, 0);
494*0Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
495*0Sstevel@tonic-gate 				f_print(fout, "_res;\n");
496*0Sstevel@tonic-gate 			}
497*0Sstevel@tonic-gate 			if (!filled)
498*0Sstevel@tonic-gate 				f_print(fout, "\t\tint fill;\n");
499*0Sstevel@tonic-gate 			f_print(fout, "\t} %s;\n", RESULT);
500*0Sstevel@tonic-gate 			f_print(fout, "\tbool_t %s;\n", RETVAL);
501*0Sstevel@tonic-gate 
502*0Sstevel@tonic-gate 		} else
503*0Sstevel@tonic-gate 			f_print(fout, "\tchar *%s;\n", RESULT);
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 		if (Cflag) {
506*0Sstevel@tonic-gate 			f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
507*0Sstevel@tonic-gate 				ARG, RESULT);
508*0Sstevel@tonic-gate 			if (mtflag)
509*0Sstevel@tonic-gate 				f_print(fout,
510*0Sstevel@tonic-gate "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
511*0Sstevel@tonic-gate 					ROUTINE);
512*0Sstevel@tonic-gate 			else
513*0Sstevel@tonic-gate 				f_print(fout,
514*0Sstevel@tonic-gate "\tchar *(*%s)(char *, struct svc_req *);\n",
515*0Sstevel@tonic-gate 					ROUTINE);
516*0Sstevel@tonic-gate 		} else {
517*0Sstevel@tonic-gate 			f_print(fout,
518*0Sstevel@tonic-gate 				"\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
519*0Sstevel@tonic-gate 				ARG, RESULT);
520*0Sstevel@tonic-gate 			if (mtflag)
521*0Sstevel@tonic-gate 				f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
522*0Sstevel@tonic-gate 			else
523*0Sstevel@tonic-gate 				f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
524*0Sstevel@tonic-gate 		}
525*0Sstevel@tonic-gate 		f_print(fout, "\n");
526*0Sstevel@tonic-gate 
527*0Sstevel@tonic-gate 		if (timerflag) {
528*0Sstevel@tonic-gate 			if (mtflag)
529*0Sstevel@tonic-gate 				f_print(fout,
530*0Sstevel@tonic-gate "\tmutex_lock(&_svcstate_lock);\n");
531*0Sstevel@tonic-gate 
532*0Sstevel@tonic-gate 			f_print(fout, "\t_rpcsvccount++;\n");
533*0Sstevel@tonic-gate 			if (mtflag)
534*0Sstevel@tonic-gate 				f_print(fout,
535*0Sstevel@tonic-gate "\tmutex_unlock(&_svcstate_lock);\n");
536*0Sstevel@tonic-gate 		}
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 		f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
539*0Sstevel@tonic-gate 		if (!nullproc(vp->procs)) {
540*0Sstevel@tonic-gate 			f_print(fout, "\tcase NULLPROC:\n");
541*0Sstevel@tonic-gate 			f_print(fout,
542*0Sstevel@tonic-gate 			    Cflag ?
543*0Sstevel@tonic-gate "\t\t(void) svc_sendreply(%s,\n\t\t\t(xdrproc_t) xdr_void, (char *)NULL);\n" :
544*0Sstevel@tonic-gate "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\t(char *)NULL);\n",
545*0Sstevel@tonic-gate 				TRANSP);
546*0Sstevel@tonic-gate 			print_return("\t\t");
547*0Sstevel@tonic-gate 			f_print(fout, "\n");
548*0Sstevel@tonic-gate 		}
549*0Sstevel@tonic-gate 		for (proc = vp->procs; proc != NULL; proc = proc->next) {
550*0Sstevel@tonic-gate 			f_print(fout, "\tcase %s:\n", proc->proc_name);
551*0Sstevel@tonic-gate 			if (proc->arg_num < 2) { /* single argument */
552*0Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.decls->decl.type);
553*0Sstevel@tonic-gate 			} else {
554*0Sstevel@tonic-gate 				p_xdrfunc(ARG, proc->args.argname);
555*0Sstevel@tonic-gate 			}
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 			if (streq(proc->res_type, "oneway")) {
558*0Sstevel@tonic-gate 				/* One-way call */
559*0Sstevel@tonic-gate 				f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
560*0Sstevel@tonic-gate 			} else {
561*0Sstevel@tonic-gate 				p_xdrfunc(RESULT, proc->res_type);
562*0Sstevel@tonic-gate 			}
563*0Sstevel@tonic-gate 			if (Cflag) {
564*0Sstevel@tonic-gate 				if (mtflag) {
565*0Sstevel@tonic-gate 					f_print(fout,
566*0Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*) (char *,  "
567*0Sstevel@tonic-gate 					    "void *,  struct svc_req *))",
568*0Sstevel@tonic-gate 					    ROUTINE);
569*0Sstevel@tonic-gate 				} else {
570*0Sstevel@tonic-gate 					f_print(fout,
571*0Sstevel@tonic-gate 					    "\t\t%s = (char *(*)(char *, "
572*0Sstevel@tonic-gate 					    "struct svc_req *)) ",
573*0Sstevel@tonic-gate 					    ROUTINE);
574*0Sstevel@tonic-gate 				}
575*0Sstevel@tonic-gate 			} else {
576*0Sstevel@tonic-gate 				if (mtflag) {
577*0Sstevel@tonic-gate 					f_print(fout,
578*0Sstevel@tonic-gate 					    "\t\t%s = (bool_t (*)()) ",
579*0Sstevel@tonic-gate 					    ROUTINE);
580*0Sstevel@tonic-gate 				} else {
581*0Sstevel@tonic-gate 					f_print(fout, "\t\t%s = (char *(*)()) ",
582*0Sstevel@tonic-gate 					    ROUTINE);
583*0Sstevel@tonic-gate 				}
584*0Sstevel@tonic-gate 			}
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 			if (newstyle) { /* new style: calls internal routine */
587*0Sstevel@tonic-gate 				f_print(fout, "_");
588*0Sstevel@tonic-gate 			}
589*0Sstevel@tonic-gate 			if ((Cflag || mtflag) && !newstyle)
590*0Sstevel@tonic-gate 				pvname_svc(proc->proc_name, vp->vers_num);
591*0Sstevel@tonic-gate 			else
592*0Sstevel@tonic-gate 				pvname(proc->proc_name, vp->vers_num);
593*0Sstevel@tonic-gate 			f_print(fout, ";\n");
594*0Sstevel@tonic-gate 			f_print(fout, "\t\tbreak;\n\n");
595*0Sstevel@tonic-gate 		}
596*0Sstevel@tonic-gate 		f_print(fout, "\tdefault:\n");
597*0Sstevel@tonic-gate 		printerr("noproc", TRANSP);
598*0Sstevel@tonic-gate 		print_return("\t\t");
599*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 		f_print(fout,
602*0Sstevel@tonic-gate 			"\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
603*0Sstevel@tonic-gate 			ARG, ARG);
604*0Sstevel@tonic-gate 		printif("getargs", TRANSP, "(caddr_t) &", ARG);
605*0Sstevel@tonic-gate 		printerr("decode", TRANSP);
606*0Sstevel@tonic-gate 		print_return("\t\t");
607*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 		if (!mtflag)
610*0Sstevel@tonic-gate 			if (Cflag)
611*0Sstevel@tonic-gate 				f_print(fout,
612*0Sstevel@tonic-gate 					"\t%s = (*%s)((char *)&%s, %s);\n",
613*0Sstevel@tonic-gate 					RESULT, ROUTINE, ARG, RQSTP);
614*0Sstevel@tonic-gate 			else
615*0Sstevel@tonic-gate 				f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
616*0Sstevel@tonic-gate 					RESULT, ROUTINE, ARG, RQSTP);
617*0Sstevel@tonic-gate 		else
618*0Sstevel@tonic-gate 			if (Cflag)
619*0Sstevel@tonic-gate 				f_print(fout,
620*0Sstevel@tonic-gate "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
621*0Sstevel@tonic-gate 					RETVAL, ROUTINE, ARG, RESULT, RQSTP);
622*0Sstevel@tonic-gate 			else
623*0Sstevel@tonic-gate 				f_print(fout,
624*0Sstevel@tonic-gate "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
625*0Sstevel@tonic-gate 					RETVAL, ROUTINE, ARG, RESULT, RQSTP);
626*0Sstevel@tonic-gate 
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 		if (mtflag)
631*0Sstevel@tonic-gate 			f_print(fout,
632*0Sstevel@tonic-gate "\tif (_xdr_%s && %s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
633*0Sstevel@tonic-gate 				RESULT, RETVAL, TRANSP, RESULT, RESULT);
634*0Sstevel@tonic-gate 		else
635*0Sstevel@tonic-gate 			f_print(fout,
636*0Sstevel@tonic-gate "\tif (_xdr_%s && %s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n",
637*0Sstevel@tonic-gate 				RESULT, RESULT, TRANSP, RESULT, RESULT);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		printerr("systemerr", TRANSP);
640*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
641*0Sstevel@tonic-gate 
642*0Sstevel@tonic-gate 		printif("freeargs", TRANSP, "(caddr_t) &", ARG);
643*0Sstevel@tonic-gate 		(void) sprintf(_errbuf, "unable to free arguments");
644*0Sstevel@tonic-gate 		print_err_message("\t\t");
645*0Sstevel@tonic-gate 		f_print(fout, "\t\texit(1);\n");
646*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
647*0Sstevel@tonic-gate 		/* print out free routine */
648*0Sstevel@tonic-gate 		if (mtflag) {
649*0Sstevel@tonic-gate 			f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
650*0Sstevel@tonic-gate 			f_print(fout, "\t\tif (!");
651*0Sstevel@tonic-gate 
652*0Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
653*0Sstevel@tonic-gate 			f_print(fout,
654*0Sstevel@tonic-gate "_freeresult(%s, _xdr_%s, (caddr_t) &%s))\n",
655*0Sstevel@tonic-gate 				TRANSP, RESULT, RESULT);
656*0Sstevel@tonic-gate 			(void) sprintf(_errbuf, "unable to free results");
657*0Sstevel@tonic-gate 			print_err_message("\t\t\t");
658*0Sstevel@tonic-gate 			f_print(fout, "\n");
659*0Sstevel@tonic-gate 			f_print(fout, "\t}\n");
660*0Sstevel@tonic-gate 		};
661*0Sstevel@tonic-gate 		print_return("\t");
662*0Sstevel@tonic-gate 		f_print(fout, "}\n");
663*0Sstevel@tonic-gate 	}
664*0Sstevel@tonic-gate }
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate static
667*0Sstevel@tonic-gate printerr(err, transp)
668*0Sstevel@tonic-gate 	char *err;
669*0Sstevel@tonic-gate 	char *transp;
670*0Sstevel@tonic-gate {
671*0Sstevel@tonic-gate 	f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
672*0Sstevel@tonic-gate }
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate static
675*0Sstevel@tonic-gate printif(proc, transp, prefix, arg)
676*0Sstevel@tonic-gate 	char *proc;
677*0Sstevel@tonic-gate 	char *transp;
678*0Sstevel@tonic-gate 	char *prefix;
679*0Sstevel@tonic-gate 	char *arg;
680*0Sstevel@tonic-gate {
681*0Sstevel@tonic-gate 	f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
682*0Sstevel@tonic-gate 		proc, transp, arg, prefix, arg);
683*0Sstevel@tonic-gate }
684*0Sstevel@tonic-gate 
685*0Sstevel@tonic-gate nullproc(proc)
686*0Sstevel@tonic-gate 	proc_list *proc;
687*0Sstevel@tonic-gate {
688*0Sstevel@tonic-gate 	for (; proc != NULL; proc = proc->next) {
689*0Sstevel@tonic-gate 		if (streq(proc->proc_num, "0")) {
690*0Sstevel@tonic-gate 			return (1);
691*0Sstevel@tonic-gate 		}
692*0Sstevel@tonic-gate 	}
693*0Sstevel@tonic-gate 	return (0);
694*0Sstevel@tonic-gate }
695*0Sstevel@tonic-gate 
696*0Sstevel@tonic-gate static
697*0Sstevel@tonic-gate write_inetmost(infile)
698*0Sstevel@tonic-gate 	char *infile;
699*0Sstevel@tonic-gate {
700*0Sstevel@tonic-gate 	f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
701*0Sstevel@tonic-gate 	f_print(fout, "\tint sock;\n");
702*0Sstevel@tonic-gate 	f_print(fout, "\tint proto;\n");
703*0Sstevel@tonic-gate 	f_print(fout, "\tstruct sockaddr_in saddr;\n");
704*0Sstevel@tonic-gate 	f_print(fout, "\tint asize = sizeof (saddr);\n");
705*0Sstevel@tonic-gate 	f_print(fout, "\n");
706*0Sstevel@tonic-gate 	f_print(fout,
707*0Sstevel@tonic-gate 	"\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
708*0Sstevel@tonic-gate 	f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
709*0Sstevel@tonic-gate 	f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
710*0Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
711*0Sstevel@tonic-gate 	f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
712*0Sstevel@tonic-gate 	f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
713*0Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
714*0Sstevel@tonic-gate 	f_print(fout, "\t\tsock = 0;\n");
715*0Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
716*0Sstevel@tonic-gate 	f_print(fout, "\t\tproto = 0;\n");
717*0Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
718*0Sstevel@tonic-gate 	f_print(fout, "\t} else {\n");
719*0Sstevel@tonic-gate 	write_rpc_svc_fg(infile, "\t\t");
720*0Sstevel@tonic-gate 	f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
721*0Sstevel@tonic-gate 	print_pmapunset("\t\t");
722*0Sstevel@tonic-gate 	f_print(fout, "\t}\n");
723*0Sstevel@tonic-gate }
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate static
726*0Sstevel@tonic-gate print_return(space)
727*0Sstevel@tonic-gate 	char *space;
728*0Sstevel@tonic-gate {
729*0Sstevel@tonic-gate 	if (exitnow)
730*0Sstevel@tonic-gate 		f_print(fout, "%sexit(0);\n", space);
731*0Sstevel@tonic-gate 	else {
732*0Sstevel@tonic-gate 		if (timerflag) {
733*0Sstevel@tonic-gate 			if (mtflag)
734*0Sstevel@tonic-gate 				f_print(fout,
735*0Sstevel@tonic-gate 					"%smutex_lock(&_svcstate_lock);\n",
736*0Sstevel@tonic-gate 					space);
737*0Sstevel@tonic-gate 			f_print(fout, "%s_rpcsvccount--;\n", space);
738*0Sstevel@tonic-gate 			f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
739*0Sstevel@tonic-gate 			if (mtflag)
740*0Sstevel@tonic-gate 				f_print(fout,
741*0Sstevel@tonic-gate 					"%smutex_unlock(&_svcstate_lock);\n",
742*0Sstevel@tonic-gate 					space);
743*0Sstevel@tonic-gate 		}
744*0Sstevel@tonic-gate 		f_print(fout, "%sreturn;\n", space);
745*0Sstevel@tonic-gate 	}
746*0Sstevel@tonic-gate }
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate static
749*0Sstevel@tonic-gate print_pmapunset(space)
750*0Sstevel@tonic-gate 	char *space;
751*0Sstevel@tonic-gate {
752*0Sstevel@tonic-gate 	list *l;
753*0Sstevel@tonic-gate 	definition *def;
754*0Sstevel@tonic-gate 	version_list *vp;
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
757*0Sstevel@tonic-gate 		def = (definition *) l->val;
758*0Sstevel@tonic-gate 		if (def->def_kind == DEF_PROGRAM) {
759*0Sstevel@tonic-gate 			for (vp = def->def.pr.versions; vp != NULL;
760*0Sstevel@tonic-gate 					vp = vp->next) {
761*0Sstevel@tonic-gate 				f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
762*0Sstevel@tonic-gate 					space, def->def_name, vp->vers_name);
763*0Sstevel@tonic-gate 			}
764*0Sstevel@tonic-gate 		}
765*0Sstevel@tonic-gate 	}
766*0Sstevel@tonic-gate }
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate static
769*0Sstevel@tonic-gate print_err_message(space)
770*0Sstevel@tonic-gate 	char *space;
771*0Sstevel@tonic-gate {
772*0Sstevel@tonic-gate 	if (logflag)
773*0Sstevel@tonic-gate 		f_print(fout, "%ssyslog(LOG_ERR, \"%%s\", \"%s\");\n", space,
774*0Sstevel@tonic-gate 		    _errbuf);
775*0Sstevel@tonic-gate 	else if (inetdflag || pmflag)
776*0Sstevel@tonic-gate 		f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
777*0Sstevel@tonic-gate 	else
778*0Sstevel@tonic-gate 		f_print(fout, "%sfprintf(stderr, \"%%s\", \"%s\");\n", space,
779*0Sstevel@tonic-gate 		    _errbuf);
780*0Sstevel@tonic-gate }
781*0Sstevel@tonic-gate 
782*0Sstevel@tonic-gate /*
783*0Sstevel@tonic-gate  * Write the server auxiliary function (_msgout, timeout)
784*0Sstevel@tonic-gate  */
785*0Sstevel@tonic-gate void
786*0Sstevel@tonic-gate write_svc_aux(nomain)
787*0Sstevel@tonic-gate 	int nomain;
788*0Sstevel@tonic-gate {
789*0Sstevel@tonic-gate 	if (!logflag)
790*0Sstevel@tonic-gate 		write_msg_out();
791*0Sstevel@tonic-gate 	if (!nomain)
792*0Sstevel@tonic-gate 		write_timeout_func();
793*0Sstevel@tonic-gate }
794*0Sstevel@tonic-gate 
795*0Sstevel@tonic-gate /*
796*0Sstevel@tonic-gate  * Write the _msgout function
797*0Sstevel@tonic-gate  */
798*0Sstevel@tonic-gate 
799*0Sstevel@tonic-gate write_msg_out()
800*0Sstevel@tonic-gate {
801*0Sstevel@tonic-gate 	f_print(fout, "\n");
802*0Sstevel@tonic-gate 	f_print(fout, "static\n");
803*0Sstevel@tonic-gate 	if (!Cflag) {
804*0Sstevel@tonic-gate 		f_print(fout, "void _msgout(msg)\n");
805*0Sstevel@tonic-gate 		f_print(fout, "\tchar *msg;\n");
806*0Sstevel@tonic-gate 	} else {
807*0Sstevel@tonic-gate 		f_print(fout, "void _msgout(char* msg)\n");
808*0Sstevel@tonic-gate 	}
809*0Sstevel@tonic-gate 	f_print(fout, "{\n");
810*0Sstevel@tonic-gate 	f_print(fout, "#ifdef RPC_SVC_FG\n");
811*0Sstevel@tonic-gate 	if (inetdflag || pmflag)
812*0Sstevel@tonic-gate 		f_print(fout, "\tif (_rpcpmstart)\n");
813*0Sstevel@tonic-gate 	f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
814*0Sstevel@tonic-gate 	f_print(fout, "\telse\n");
815*0Sstevel@tonic-gate 	f_print(fout,
816*0Sstevel@tonic-gate 		"\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
817*0Sstevel@tonic-gate 	f_print(fout, "#else\n");
818*0Sstevel@tonic-gate 	f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
819*0Sstevel@tonic-gate 	f_print(fout, "#endif\n");
820*0Sstevel@tonic-gate 	f_print(fout, "}\n");
821*0Sstevel@tonic-gate }
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate /*
824*0Sstevel@tonic-gate  * Write the timeout function
825*0Sstevel@tonic-gate  */
826*0Sstevel@tonic-gate static
827*0Sstevel@tonic-gate write_timeout_func()
828*0Sstevel@tonic-gate {
829*0Sstevel@tonic-gate 	if (!timerflag)
830*0Sstevel@tonic-gate 		return;
831*0Sstevel@tonic-gate 
832*0Sstevel@tonic-gate 	f_print(fout, "\n");
833*0Sstevel@tonic-gate 	if (mtflag) {
834*0Sstevel@tonic-gate 		f_print(fout, "/*ARGSUSED*/\n");
835*0Sstevel@tonic-gate 		f_print(fout, "static void *\n");
836*0Sstevel@tonic-gate 		if (!Cflag) {
837*0Sstevel@tonic-gate 			f_print(fout, "closedown(arg)\n");
838*0Sstevel@tonic-gate 			f_print(fout, "\tvoid *arg;\n");
839*0Sstevel@tonic-gate 		} else
840*0Sstevel@tonic-gate 			f_print(fout, "closedown(void *arg)\n");
841*0Sstevel@tonic-gate 		f_print(fout, "{\n");
842*0Sstevel@tonic-gate 		f_print(fout, "\t/*CONSTCOND*/\n");
843*0Sstevel@tonic-gate 		f_print(fout, "\twhile (1) {\n");
844*0Sstevel@tonic-gate 		f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
845*0Sstevel@tonic-gate 		f_print(fout,
846*0Sstevel@tonic-gate "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
847*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tcontinue;\n\n");
848*0Sstevel@tonic-gate 		f_print(fout,
849*0Sstevel@tonic-gate "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
850*0Sstevel@tonic-gate 		if (tirpcflag) {
851*0Sstevel@tonic-gate 			f_print(fout, "\t\t\tint size;\n");
852*0Sstevel@tonic-gate 		} else {
853*0Sstevel@tonic-gate 			f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
854*0Sstevel@tonic-gate 			f_print(fout, "\t\t\tstatic int size;\n");
855*0Sstevel@tonic-gate 		}
856*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
857*0Sstevel@tonic-gate 		if (tirpcflag) {
858*0Sstevel@tonic-gate 			f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
859*0Sstevel@tonic-gate 		} else {
860*0Sstevel@tonic-gate 			f_print(fout, "\t\t\tif (size == 0) {\n");
861*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
862*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
863*0Sstevel@tonic-gate 		}
864*0Sstevel@tonic-gate 		f_print(fout,
865*0Sstevel@tonic-gate "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
866*0Sstevel@tonic-gate 		if (tirpcflag) {
867*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
868*0Sstevel@tonic-gate 		} else {
869*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
870*0Sstevel@tonic-gate 		}
871*0Sstevel@tonic-gate 		f_print(fout, "\t\t\t\t\topenfd++;\n");
872*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (openfd <= 1)\n");
873*0Sstevel@tonic-gate 		f_print(fout, "\t\t\t\texit(0);\n");
874*0Sstevel@tonic-gate 		f_print(fout, "\t\t} else\n");
875*0Sstevel@tonic-gate 		f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
876*0Sstevel@tonic-gate 		f_print(fout, "\t\tmutex_unlock(&_svcstate_lock);\n");
877*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
878*0Sstevel@tonic-gate 		f_print(fout, "}\n");
879*0Sstevel@tonic-gate 		return;
880*0Sstevel@tonic-gate 	}
881*0Sstevel@tonic-gate 
882*0Sstevel@tonic-gate 	f_print(fout, "static void\n");
883*0Sstevel@tonic-gate 	if (!Cflag) {
884*0Sstevel@tonic-gate 		f_print(fout, "closedown(sig)\n");
885*0Sstevel@tonic-gate 		f_print(fout, "\tint sig;\n");
886*0Sstevel@tonic-gate 	} else
887*0Sstevel@tonic-gate 		f_print(fout, "closedown(int sig)\n");
888*0Sstevel@tonic-gate 	f_print(fout, "{\n");
889*0Sstevel@tonic-gate 	f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
890*0Sstevel@tonic-gate 	if (tirpcflag) {
891*0Sstevel@tonic-gate 		f_print(fout, "\t\tint size;\n");
892*0Sstevel@tonic-gate 	} else {
893*0Sstevel@tonic-gate 		f_print(fout, "\t\textern fd_set svc_fdset;\n");
894*0Sstevel@tonic-gate 		f_print(fout, "\t\tstatic int size;\n");
895*0Sstevel@tonic-gate 	}
896*0Sstevel@tonic-gate 	f_print(fout, "\t\tint i, openfd = 0;\n\n");
897*0Sstevel@tonic-gate 	if (tirpcflag) {
898*0Sstevel@tonic-gate 		f_print(fout, "\t\tsize = svc_max_pollfd;\n");
899*0Sstevel@tonic-gate 	} else {
900*0Sstevel@tonic-gate 		f_print(fout, "\t\tif (size == 0) {\n");
901*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tsize = getdtablesize();\n");
902*0Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
903*0Sstevel@tonic-gate 	}
904*0Sstevel@tonic-gate 	f_print(fout,
905*0Sstevel@tonic-gate 		"\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
906*0Sstevel@tonic-gate 	if (tirpcflag) {
907*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
908*0Sstevel@tonic-gate 	} else {
909*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
910*0Sstevel@tonic-gate 	}
911*0Sstevel@tonic-gate 	f_print(fout, "\t\t\t\topenfd++;\n");
912*0Sstevel@tonic-gate 	f_print(fout, "\t\tif (openfd <= 1)\n");
913*0Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(0);\n");
914*0Sstevel@tonic-gate 	f_print(fout, "\t} else\n");
915*0Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate 	f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
918*0Sstevel@tonic-gate 				Cflag? "(SIG_PF)" : "(void(*)())");
919*0Sstevel@tonic-gate 	f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
920*0Sstevel@tonic-gate 	f_print(fout, "}\n");
921*0Sstevel@tonic-gate }
922*0Sstevel@tonic-gate 
923*0Sstevel@tonic-gate /*
924*0Sstevel@tonic-gate  * Write the most of port monitor support
925*0Sstevel@tonic-gate  */
926*0Sstevel@tonic-gate static
927*0Sstevel@tonic-gate write_pm_most(infile, netflag)
928*0Sstevel@tonic-gate 	char *infile;
929*0Sstevel@tonic-gate 	int netflag;
930*0Sstevel@tonic-gate {
931*0Sstevel@tonic-gate 	list *l;
932*0Sstevel@tonic-gate 	definition *def;
933*0Sstevel@tonic-gate 	version_list *vp;
934*0Sstevel@tonic-gate 
935*0Sstevel@tonic-gate 	f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
936*0Sstevel@tonic-gate 	f_print(fout, "\t/*\n");
937*0Sstevel@tonic-gate 	f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
938*0Sstevel@tonic-gate 	f_print(fout, "\t * that we were started by a port monitor. If\n");
939*0Sstevel@tonic-gate 	f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
940*0Sstevel@tonic-gate 	f_print(fout, "\t * TLI endpoint.\n");
941*0Sstevel@tonic-gate 	f_print(fout, "\t */\n");
942*0Sstevel@tonic-gate 	f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
943*0Sstevel@tonic-gate 	f_print(fout, "\t\tchar *netid;\n");
944*0Sstevel@tonic-gate 	if (!netflag) {	/* Not included by -n option */
945*0Sstevel@tonic-gate 		f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
946*0Sstevel@tonic-gate 		f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
947*0Sstevel@tonic-gate 	}
948*0Sstevel@tonic-gate 	if (timerflag)
949*0Sstevel@tonic-gate 		f_print(fout, "\t\tint pmclose;\n");
950*0Sstevel@tonic-gate /*
951*0Sstevel@tonic-gate  *  Not necessary, defined in /usr/include/stdlib
952*0Sstevel@tonic-gate  *  f_print(fout, "\t\textern char *getenv();\n");
953*0Sstevel@tonic-gate  */
954*0Sstevel@tonic-gate 	f_print(fout, "\n");
955*0Sstevel@tonic-gate 	f_print(fout, "\t\t_rpcpmstart = 1;\n");
956*0Sstevel@tonic-gate 	open_log_file(infile, "\t\t");
957*0Sstevel@tonic-gate 	f_print(fout,
958*0Sstevel@tonic-gate "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
959*0Sstevel@tonic-gate 
960*0Sstevel@tonic-gate 	if (timerflag) {
961*0Sstevel@tonic-gate 		f_print(fout, "\t\t/* started from inetd */\n");
962*0Sstevel@tonic-gate 		f_print(fout, "\t\t\tpmclose = 1;\n");
963*0Sstevel@tonic-gate 	}
964*0Sstevel@tonic-gate 	f_print(fout,
965*0Sstevel@tonic-gate 		"\t\t} else {\n");
966*0Sstevel@tonic-gate 	f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
967*0Sstevel@tonic-gate 	sprintf(_errbuf, "cannot get transport info");
968*0Sstevel@tonic-gate 	print_err_message("\t\t\t\t");
969*0Sstevel@tonic-gate 	if (timerflag)
970*0Sstevel@tonic-gate 		f_print(fout,
971*0Sstevel@tonic-gate 			"\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
972*0Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
973*0Sstevel@tonic-gate 	f_print(fout,
974*0Sstevel@tonic-gate "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
975*0Sstevel@tonic-gate 		TRANSP);
976*0Sstevel@tonic-gate 	sprintf(_errbuf, "cannot create server handle");
977*0Sstevel@tonic-gate 	print_err_message("\t\t\t");
978*0Sstevel@tonic-gate 	f_print(fout, "\t\t\texit(1);\n");
979*0Sstevel@tonic-gate 	f_print(fout, "\t\t}\n");
980*0Sstevel@tonic-gate 	f_print(fout, "\t\tif (nconf)\n");
981*0Sstevel@tonic-gate 	f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
982*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
983*0Sstevel@tonic-gate 		def = (definition *) l->val;
984*0Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
985*0Sstevel@tonic-gate 			continue;
986*0Sstevel@tonic-gate 		}
987*0Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
988*0Sstevel@tonic-gate 			f_print(fout,
989*0Sstevel@tonic-gate 				"\t\tif (!svc_reg(%s, %s, %s, ",
990*0Sstevel@tonic-gate 				TRANSP, def->def_name, vp->vers_name);
991*0Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
992*0Sstevel@tonic-gate 			f_print(fout, ", 0)) {\n");
993*0Sstevel@tonic-gate 			(void) sprintf(_errbuf, "unable to register (%s, %s).",
994*0Sstevel@tonic-gate 					def->def_name, vp->vers_name);
995*0Sstevel@tonic-gate 			print_err_message("\t\t\t");
996*0Sstevel@tonic-gate 			f_print(fout, "\t\t\texit(1);\n");
997*0Sstevel@tonic-gate 			f_print(fout, "\t\t}\n");
998*0Sstevel@tonic-gate 		}
999*0Sstevel@tonic-gate 	}
1000*0Sstevel@tonic-gate 	if (timerflag) {
1001*0Sstevel@tonic-gate 		f_print(fout, "\t\tif (pmclose) {\n");
1002*0Sstevel@tonic-gate 		if (mtflag) {
1003*0Sstevel@tonic-gate 			f_print(fout,
1004*0Sstevel@tonic-gate "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t\t\t0, NULL) != 0) {\n");
1005*0Sstevel@tonic-gate 			f_print(fout,
1006*0Sstevel@tonic-gate "\t\t\t\t_msgout(\"cannot create closedown thread\");\n");
1007*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t\texit(1);\n");
1008*0Sstevel@tonic-gate 			f_print(fout, "\t\t\t}\n");
1009*0Sstevel@tonic-gate 		} else {
1010*0Sstevel@tonic-gate 			f_print(fout,
1011*0Sstevel@tonic-gate "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1012*0Sstevel@tonic-gate 					Cflag? "(SIG_PF)" : "(void(*)())");
1013*0Sstevel@tonic-gate 			f_print(fout,
1014*0Sstevel@tonic-gate "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
1015*0Sstevel@tonic-gate 		}
1016*0Sstevel@tonic-gate 		f_print(fout, "\t\t}\n");
1017*0Sstevel@tonic-gate 	}
1018*0Sstevel@tonic-gate 	f_print(fout, "\t\tsvc_run();\n");
1019*0Sstevel@tonic-gate 	f_print(fout, "\t\texit(1);\n");
1020*0Sstevel@tonic-gate 	f_print(fout, "\t\t/* NOTREACHED */\n");
1021*0Sstevel@tonic-gate 	f_print(fout, "\t}");
1022*0Sstevel@tonic-gate }
1023*0Sstevel@tonic-gate 
1024*0Sstevel@tonic-gate /*
1025*0Sstevel@tonic-gate  * Support for backgrounding the server if self started.
1026*0Sstevel@tonic-gate  */
1027*0Sstevel@tonic-gate static
1028*0Sstevel@tonic-gate write_rpc_svc_fg(infile, sp)
1029*0Sstevel@tonic-gate 	char *infile;
1030*0Sstevel@tonic-gate 	char *sp;
1031*0Sstevel@tonic-gate {
1032*0Sstevel@tonic-gate 	f_print(fout, "#ifndef RPC_SVC_FG\n");
1033*0Sstevel@tonic-gate 	f_print(fout, "#pragma weak closefrom\n");
1034*0Sstevel@tonic-gate 	f_print(fout, "%sextern void closefrom();\n", sp);
1035*0Sstevel@tonic-gate 	f_print(fout, "%sint size;\n", sp);
1036*0Sstevel@tonic-gate 	if (tirpcflag)
1037*0Sstevel@tonic-gate 		f_print(fout, "%sstruct rlimit rl;\n", sp);
1038*0Sstevel@tonic-gate 	if (inetdflag)
1039*0Sstevel@tonic-gate 		f_print(fout, "%sint pid, i;\n\n", sp);
1040*0Sstevel@tonic-gate 	f_print(fout, "%spid = fork();\n", sp);
1041*0Sstevel@tonic-gate 	f_print(fout, "%sif (pid < 0) {\n", sp);
1042*0Sstevel@tonic-gate 	f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
1043*0Sstevel@tonic-gate 	f_print(fout, "%s\texit(1);\n", sp);
1044*0Sstevel@tonic-gate 	f_print(fout, "%s}\n", sp);
1045*0Sstevel@tonic-gate 	f_print(fout, "%sif (pid)\n", sp);
1046*0Sstevel@tonic-gate 	f_print(fout, "%s\texit(0);\n", sp);
1047*0Sstevel@tonic-gate 	/* close all file descriptors */
1048*0Sstevel@tonic-gate 	if (tirpcflag) {
1049*0Sstevel@tonic-gate 		f_print(fout, "%sif (closefrom != NULL)\n", sp);
1050*0Sstevel@tonic-gate 		f_print(fout, "%s\tclosefrom(0);\n", sp);
1051*0Sstevel@tonic-gate 		f_print(fout, "%selse {\n", sp);
1052*0Sstevel@tonic-gate 		f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
1053*0Sstevel@tonic-gate 		f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
1054*0Sstevel@tonic-gate 		f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
1055*0Sstevel@tonic-gate 		f_print(fout, "%s\t\texit(1);\n", sp);
1056*0Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
1057*0Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
1058*0Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
1059*0Sstevel@tonic-gate 	} else {
1060*0Sstevel@tonic-gate 		f_print(fout, "%s\tsize = getdtablesize();\n", sp);
1061*0Sstevel@tonic-gate 		f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
1062*0Sstevel@tonic-gate 		f_print(fout, "%s\t\t(void) close(i);\n", sp);
1063*0Sstevel@tonic-gate 	}
1064*0Sstevel@tonic-gate 	/* Redirect stderr and stdout to /dev/null */
1065*0Sstevel@tonic-gate 	f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
1066*0Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 1);\n", sp);
1067*0Sstevel@tonic-gate 	f_print(fout, "%s(void) dup2(i, 2);\n", sp);
1068*0Sstevel@tonic-gate 	/* This removes control of the controlling terminal */
1069*0Sstevel@tonic-gate 	if (tirpcflag)
1070*0Sstevel@tonic-gate 		f_print(fout, "%ssetsid();\n", sp);
1071*0Sstevel@tonic-gate 	else {
1072*0Sstevel@tonic-gate 		f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1073*0Sstevel@tonic-gate 		f_print(fout, "%sif (i >= 0) {\n", sp);
1074*0Sstevel@tonic-gate 		f_print(fout,
1075*0Sstevel@tonic-gate 			"%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
1076*0Sstevel@tonic-gate 		f_print(fout, "%s\t(void) close(i);\n", sp);
1077*0Sstevel@tonic-gate 		f_print(fout, "%s}\n", sp);
1078*0Sstevel@tonic-gate 	}
1079*0Sstevel@tonic-gate 	if (!logflag)
1080*0Sstevel@tonic-gate 		open_log_file(infile, sp);
1081*0Sstevel@tonic-gate 	f_print(fout, "#endif\n");
1082*0Sstevel@tonic-gate 	if (logflag)
1083*0Sstevel@tonic-gate 		open_log_file(infile, sp);
1084*0Sstevel@tonic-gate }
1085*0Sstevel@tonic-gate 
1086*0Sstevel@tonic-gate static
1087*0Sstevel@tonic-gate open_log_file(infile, sp)
1088*0Sstevel@tonic-gate 	char *infile;
1089*0Sstevel@tonic-gate 	char *sp;
1090*0Sstevel@tonic-gate {
1091*0Sstevel@tonic-gate 	char *s;
1092*0Sstevel@tonic-gate 
1093*0Sstevel@tonic-gate 	s = strrchr(infile, '.');
1094*0Sstevel@tonic-gate 	if (s)
1095*0Sstevel@tonic-gate 		*s = '\0';
1096*0Sstevel@tonic-gate 	f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1097*0Sstevel@tonic-gate 	if (s)
1098*0Sstevel@tonic-gate 		*s = '.';
1099*0Sstevel@tonic-gate }
1100*0Sstevel@tonic-gate 
1101*0Sstevel@tonic-gate 
1102*0Sstevel@tonic-gate 
1103*0Sstevel@tonic-gate 
1104*0Sstevel@tonic-gate /*
1105*0Sstevel@tonic-gate  * write a registration for the given transport for Inetd
1106*0Sstevel@tonic-gate  */
1107*0Sstevel@tonic-gate void
1108*0Sstevel@tonic-gate write_inetd_register(transp)
1109*0Sstevel@tonic-gate 	char *transp;
1110*0Sstevel@tonic-gate {
1111*0Sstevel@tonic-gate 	list *l;
1112*0Sstevel@tonic-gate 	definition *def;
1113*0Sstevel@tonic-gate 	version_list *vp;
1114*0Sstevel@tonic-gate 	char *sp;
1115*0Sstevel@tonic-gate 	int isudp;
1116*0Sstevel@tonic-gate 	char tmpbuf[32];
1117*0Sstevel@tonic-gate 
1118*0Sstevel@tonic-gate 	if (inetdflag)
1119*0Sstevel@tonic-gate 		sp = "\t";
1120*0Sstevel@tonic-gate 	else
1121*0Sstevel@tonic-gate 		sp = "";
1122*0Sstevel@tonic-gate 	if (streq(transp, "udp"))
1123*0Sstevel@tonic-gate 		isudp = 1;
1124*0Sstevel@tonic-gate 	else
1125*0Sstevel@tonic-gate 		isudp = 0;
1126*0Sstevel@tonic-gate 	f_print(fout, "\n");
1127*0Sstevel@tonic-gate 	if (inetdflag) {
1128*0Sstevel@tonic-gate 		f_print(fout,
1129*0Sstevel@tonic-gate 			"\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1130*0Sstevel@tonic-gate 			isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1131*0Sstevel@tonic-gate 	}
1132*0Sstevel@tonic-gate 	f_print(fout, "%s\t%s = svc%s_create(%s",
1133*0Sstevel@tonic-gate 		sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
1134*0Sstevel@tonic-gate 	if (!isudp)
1135*0Sstevel@tonic-gate 		f_print(fout, ", 0, 0");
1136*0Sstevel@tonic-gate 	f_print(fout, ");\n");
1137*0Sstevel@tonic-gate 	f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1138*0Sstevel@tonic-gate 	(void) sprintf(_errbuf, "cannot create %s service.", transp);
1139*0Sstevel@tonic-gate 	(void) sprintf(tmpbuf, "%s\t\t", sp);
1140*0Sstevel@tonic-gate 	print_err_message(tmpbuf);
1141*0Sstevel@tonic-gate 	f_print(fout, "%s\t\texit(1);\n", sp);
1142*0Sstevel@tonic-gate 	f_print(fout, "%s\t}\n", sp);
1143*0Sstevel@tonic-gate 
1144*0Sstevel@tonic-gate 	if (inetdflag) {
1145*0Sstevel@tonic-gate 		f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1146*0Sstevel@tonic-gate 		f_print(fout, "%s\tproto = IPPROTO_%s;\n",
1147*0Sstevel@tonic-gate 				sp, isudp ? "UDP": "TCP");
1148*0Sstevel@tonic-gate 	}
1149*0Sstevel@tonic-gate 	for (l = defined; l != NULL; l = l->next) {
1150*0Sstevel@tonic-gate 		def = (definition *) l->val;
1151*0Sstevel@tonic-gate 		if (def->def_kind != DEF_PROGRAM) {
1152*0Sstevel@tonic-gate 			continue;
1153*0Sstevel@tonic-gate 		}
1154*0Sstevel@tonic-gate 		for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1155*0Sstevel@tonic-gate 			f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
1156*0Sstevel@tonic-gate 				sp, TRANSP, def->def_name, vp->vers_name);
1157*0Sstevel@tonic-gate 			pvname(def->def_name, vp->vers_num);
1158*0Sstevel@tonic-gate 			if (inetdflag)
1159*0Sstevel@tonic-gate 				f_print(fout, ", proto)) {\n");
1160*0Sstevel@tonic-gate 			else
1161*0Sstevel@tonic-gate 				f_print(fout, ", IPPROTO_%s)) {\n",
1162*0Sstevel@tonic-gate 					isudp ? "UDP": "TCP");
1163*0Sstevel@tonic-gate 			(void) sprintf(_errbuf,
1164*0Sstevel@tonic-gate 				"unable to register (%s, %s, %s).",
1165*0Sstevel@tonic-gate 				def->def_name, vp->vers_name, transp);
1166*0Sstevel@tonic-gate 			print_err_message(tmpbuf);
1167*0Sstevel@tonic-gate 			f_print(fout, "%s\t\texit(1);\n", sp);
1168*0Sstevel@tonic-gate 			f_print(fout, "%s\t}\n", sp);
1169*0Sstevel@tonic-gate 		}
1170*0Sstevel@tonic-gate 	}
1171*0Sstevel@tonic-gate 	if (inetdflag)
1172*0Sstevel@tonic-gate 		f_print(fout, "\t}\n");
1173*0Sstevel@tonic-gate }
1174