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