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