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