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