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