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