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