1 /* $NetBSD: rpc_svcout.c,v 1.21 2003/09/26 22:25:21 wiz Exp $ */
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.
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.
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.
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.
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.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
39 static char sccsid
[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
41 __RCSID("$NetBSD: rpc_svcout.c,v 1.21 2003/09/26 22:25:21 wiz Exp $");
46 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
51 #include "rpc_parse.h"
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";
60 static void p_xdrfunc
__P((char *, char *));
61 static void internal_proctype
__P((proc_list
*));
62 static void write_real_program
__P((definition
*));
63 static void write_program
__P((definition
*, char *));
64 static void printerr
__P((char *, char *));
65 static void printif
__P((char *, char *, char *, char *));
66 static void write_inetmost
__P((char *));
67 static void print_return
__P((char *));
68 static void print_pmapunset
__P((char *));
69 static void print_err_message
__P((char *));
70 static void write_timeout_func
__P((void));
71 static void write_caller_func
__P((void));
72 static void write_pm_most
__P((char *, int));
73 static void write_rpc_svc_fg
__P((char *, char *));
74 static void open_log_file
__P((char *, char *));
75 static char *aster
__P((char *));
77 char _errbuf
[256]; /* For all messages */
80 p_xdrfunc(rname
, typename
)
85 f_print(fout
, "\t\txdr_%s = (xdrproc_t)xdr_%s;\n", rname
,
88 f_print(fout
, "\t\txdr_%s = xdr_%s;\n", rname
, stringfix(typename
));
92 internal_proctype(plist
)
95 f_print(fout
, "static ");
96 ptype(plist
->res_prefix
, plist
->res_type
, 1);
103 * write most of the service, that is, everything but the registrations.
106 write_most(infile
, netflag
, nomain
)
107 char *infile
; /* our name */
111 if (inetdflag
|| pmflag
) {
113 var_type
= (nomain
? "" : "static ");
114 f_print(fout
, "%sint _rpcpmstart;", var_type
);
115 f_print(fout
, "\t\t/* Started by a port monitor ? */\n");
116 f_print(fout
, "%sint _rpcfdtype;", var_type
);
117 f_print(fout
, "\t\t/* Whether Stream or Datagram ? */\n");
119 f_print(fout
, "%sint _rpcsvcdirty;", var_type
);
120 f_print(fout
, "\t/* Still serving ? */\n");
122 write_svc_aux(nomain
);
124 /* write out dispatcher and stubs */
125 write_programs(nomain
? (char *) NULL
: "static");
130 f_print(fout
, "\n\n");
132 f_print(fout
, "int main(int argc, char *argv[]);\n");
133 f_print(fout
, "\nint\n");
135 f_print(fout
, "main(int argc, char *argv[])\n");
137 f_print(fout
, "main(argc, argv)\nint argc;\nchar *argv[];\n");
138 f_print(fout
, "{\n");
140 write_inetmost(infile
); /* Includes call to write_rpc_svc_fg() */
144 f_print(fout
, "\tSVCXPRT *%s;\n", TRANSP
);
145 f_print(fout
, "\tstruct netconfig *nconf = NULL;\n");
147 f_print(fout
, "\tpid_t pid;\n");
148 f_print(fout
, "\tint i;\n");
149 f_print(fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
150 write_pm_most(infile
, netflag
);
151 f_print(fout
, "\telse {\n");
152 write_rpc_svc_fg(infile
, "\t\t");
153 f_print(fout
, "\t}\n");
155 f_print(fout
, "\tSVCXPRT *%s;\n", TRANSP
);
157 print_pmapunset("\t");
161 if (logflag
&& !inetdflag
) {
162 open_log_file(infile
, "\t");
166 * write a registration for the given transport
169 write_netid_register(transp
)
180 f_print(fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
181 f_print(fout
, "%s\tif (nconf == NULL) {\n", sp
);
182 (void) sprintf(_errbuf
, "cannot find %s netid.", transp
);
183 sprintf(tmpbuf
, "%s\t\t", sp
);
184 print_err_message(tmpbuf
);
185 f_print(fout
, "%s\t\texit(1);\n", sp
);
186 f_print(fout
, "%s\t}\n", sp
);
187 f_print(fout
, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
189 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
190 (void) sprintf(_errbuf
, "cannot create %s service.", transp
);
191 print_err_message(tmpbuf
);
192 f_print(fout
, "%s\t\texit(1);\n", sp
);
193 f_print(fout
, "%s\t}\n", sp
);
195 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
196 def
= (definition
*) l
->val
;
197 if (def
->def_kind
!= DEF_PROGRAM
) {
200 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
202 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
203 sp
, def
->def_name
, vp
->vers_name
);
205 "%s\tif (!svc_reg(%s, %s, %s, ",
206 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
207 pvname(def
->def_name
, vp
->vers_num
);
208 f_print(fout
, ", nconf)) {\n");
209 (void) sprintf(_errbuf
, "unable to register (%s, %s, %s).",
210 def
->def_name
, vp
->vers_name
, transp
);
211 print_err_message(tmpbuf
);
212 f_print(fout
, "%s\t\texit(1);\n", sp
);
213 f_print(fout
, "%s\t}\n", sp
);
216 f_print(fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
219 * write a registration for the given transport for TLI
222 write_nettype_register(transp
)
229 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
230 def
= (definition
*) l
->val
;
231 if (def
->def_kind
!= DEF_PROGRAM
) {
234 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
235 f_print(fout
, "\tif (!svc_create(");
236 pvname(def
->def_name
, vp
->vers_num
);
237 f_print(fout
, ", %s, %s, \"%s\")) {\n ",
238 def
->def_name
, vp
->vers_name
, transp
);
239 (void) sprintf(_errbuf
,
240 "unable to create (%s, %s) for %s.",
241 def
->def_name
, vp
->vers_name
, transp
);
242 print_err_message("\t\t");
243 f_print(fout
, "\t\texit(1);\n");
244 f_print(fout
, "\t}\n");
249 * write the rest of the service
256 f_print(fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
257 (void) sprintf(_errbuf
, "could not create a handle");
258 print_err_message("\t\t");
259 f_print(fout
, "\t\texit(1);\n");
260 f_print(fout
, "\t}\n");
262 f_print(fout
, "\tif (_rpcpmstart) {\n");
264 "\t\t(void) signal(SIGALRM, %s closedown);\n",
265 Cflag
? "(SIG_PF)" : "(void(*)())");
266 f_print(fout
, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
267 f_print(fout
, "\t}\n");
270 f_print(fout
, "\tsvc_run();\n");
271 (void) sprintf(_errbuf
, "svc_run returned");
272 print_err_message("\t");
273 f_print(fout
, "\texit(1);\n");
274 f_print(fout
, "\t/* NOTREACHED */\n");
275 f_print(fout
, "}\n");
279 write_programs(storage
)
285 /* write out stubs for procedure definitions */
286 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
287 def
= (definition
*) l
->val
;
288 if (def
->def_kind
== DEF_PROGRAM
) {
289 write_real_program(def
);
293 /* write out dispatcher for each program */
294 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
295 def
= (definition
*) l
->val
;
296 if (def
->def_kind
== DEF_PROGRAM
) {
297 write_program(def
, storage
);
303 /* write out definition of internal function (e.g. _printmsg_1(...))
304 which calls server's definition of actual function (e.g. printmsg_1(...)).
305 Unpacks single user argument of printmsg_1 to call-by-value format
306 expected by printmsg_1. */
308 write_real_program(def
)
316 return; /* not needed for old style */
317 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
318 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
320 internal_proctype(proc
);
321 f_print(fout
, "\n_");
322 pvname(proc
->proc_name
, vp
->vers_num
);
326 if (proc
->arg_num
> 1)
330 ptype(proc
->args
.decls
->decl
.prefix
,
331 proc
->args
.decls
->decl
.type
, 0);
332 f_print(fout
, "*argp, ");
334 if (streq(proc
->res_type
, "void"))
335 f_print(fout
, "char ");
337 ptype(proc
->res_prefix
,
339 f_print(fout
, "%sresult, ",
340 aster(proc
->res_type
));
342 f_print(fout
, "struct svc_req *%s)\n", RQSTP
);
344 f_print(fout
, "(argp, ");
346 f_print(fout
, "result, ");
347 f_print(fout
, "%s)\n", RQSTP
);
349 if (proc
->arg_num
> 1)
353 ptype(proc
->args
.decls
->decl
.prefix
,
354 proc
->args
.decls
->decl
.type
, 0);
355 f_print(fout
, "*argp;\n");
358 if (streq(proc
->res_type
, "void"))
359 f_print(fout
, "char ");
361 ptype(proc
->res_prefix
,
363 f_print(fout
, "%sresult;\n",
364 aster(proc
->res_type
));
366 f_print(fout
, "\tstruct svc_req *%s;\n", RQSTP
);
369 f_print(fout
, "{\n");
370 f_print(fout
, "\treturn (");
371 pvname_svc(proc
->proc_name
, vp
->vers_num
);
373 if (proc
->arg_num
< 2) { /* single argument */
374 if (!streq(proc
->args
.decls
->decl
.type
, "void"))
375 f_print(fout
, "*argp, "); /* non-void */
377 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
378 f_print(fout
, "argp->%s, ", l
->decl
.name
);
380 f_print(fout
, "%s));\n}\n", RQSTP
);
386 write_program(def
, storage
)
394 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
397 if (storage
!= NULL
) {
398 f_print(fout
, "%s ", storage
);
400 f_print(fout
, "void ");
401 pvname(def
->def_name
, vp
->vers_num
);
402 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
403 f_print(fout
, "SVCXPRT *%s);\n", TRANSP
);
406 if (storage
!= NULL
) {
407 f_print(fout
, "%s ", storage
);
409 f_print(fout
, "void\n");
410 pvname(def
->def_name
, vp
->vers_num
);
413 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
414 f_print(fout
, "SVCXPRT *%s)\n", TRANSP
);
416 f_print(fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
417 f_print(fout
, " struct svc_req *%s;\n", RQSTP
);
418 f_print(fout
, " SVCXPRT *%s;\n", TRANSP
);
421 f_print(fout
, "{\n");
424 f_print(fout
, "\tunion {\n");
425 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
426 if (proc
->arg_num
< 2) { /* single argument */
427 if (streq(proc
->args
.decls
->decl
.type
,
432 f_print(fout
, "\t\t");
433 ptype(proc
->args
.decls
->decl
.prefix
,
434 proc
->args
.decls
->decl
.type
, 0);
435 pvname(proc
->proc_name
, vp
->vers_num
);
436 f_print(fout
, "_arg;\n");
440 f_print(fout
, "\t\t%s", proc
->args
.argname
);
442 pvname(proc
->proc_name
, vp
->vers_num
);
443 f_print(fout
, "_arg;\n");
447 f_print(fout
, "\t\tint fill;\n");
449 f_print(fout
, "\t} %s;\n", ARG
);
451 f_print(fout
, "\tunion {\n");
452 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
453 f_print(fout
, "\t\t");
454 if (streq(proc
->res_type
, "void"))
455 f_print(fout
, "char ");
457 ptype(proc
->res_prefix
, proc
->res_type
,
459 pvname(proc
->proc_name
, vp
->vers_num
);
460 f_print(fout
, "_res;\n");
462 f_print(fout
, "\t} %s;\n", RESULT
);
463 f_print(fout
, "\tbool_t retval;\n");
465 f_print(fout
, "\tchar *%s;\n", RESULT
);
468 f_print(fout
, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG
, RESULT
);
471 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
475 "\tchar *(*%s)(char *, struct svc_req *);\n",
478 f_print(fout
, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG
, RESULT
);
480 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
482 f_print(fout
, "\tchar *(*%s)();\n", ROUTINE
);
488 f_print(fout
, "\tcaller = transp;\n"); /* EVAS */
490 f_print(fout
, "\t_rpcsvcdirty = 1;\n");
491 f_print(fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
492 if (!nullproc(vp
->procs
)) {
493 f_print(fout
, "\tcase NULLPROC:\n");
496 "\t\t(void) svc_sendreply(%s, (xdrproc_t)xdr_void, (char *)NULL);\n", TRANSP
);
499 "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
502 print_return("\t\t");
505 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
506 f_print(fout
, "\tcase %s:\n", proc
->proc_name
);
507 if (proc
->arg_num
< 2) { /* single argument */
508 p_xdrfunc(ARG
, proc
->args
.decls
->decl
.type
);
510 p_xdrfunc(ARG
, proc
->args
.argname
);
512 p_xdrfunc(RESULT
, proc
->res_type
);
516 "\t\t%s = (bool_t (*)(char *, void *, struct svc_req *))",
520 "\t\t%s = (char *(*)(char *, struct svc_req *))",
524 f_print(fout
, "\t\t%s = (bool_t (*)())", ROUTINE
);
526 f_print(fout
, "\t\t%s = (char *(*)())", ROUTINE
);
529 if (newstyle
) /* new style: calls internal routine */
532 pvname_svc(proc
->proc_name
, vp
->vers_num
);
534 pvname(proc
->proc_name
, vp
->vers_num
);
535 f_print(fout
, ";\n");
536 f_print(fout
, "\t\tbreak;\n\n");
538 f_print(fout
, "\tdefault:\n");
539 printerr("noproc", TRANSP
);
540 print_return("\t\t");
541 f_print(fout
, "\t}\n");
543 f_print(fout
, "\t(void) memset(&%s, 0, sizeof(%s));\n", ARG
, ARG
);
544 printif("getargs", TRANSP
, "(caddr_t)&", ARG
);
545 printerr("decode", TRANSP
);
546 print_return("\t\t");
547 f_print(fout
, "\t}\n");
551 f_print(fout
, "\tretval = (*%s)((char *)&%s, (void *)&%s, %s);\n",
552 ROUTINE
, ARG
, RESULT
, RQSTP
);
554 f_print(fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
555 RESULT
, ROUTINE
, ARG
, RQSTP
);
558 f_print(fout
, "\tretval = (*%s)(&%s, &%s, %s);\n",
559 ROUTINE
, ARG
, RESULT
, RQSTP
);
561 f_print(fout
, "\t%s = (*%s)(&%s, %s);\n",
562 RESULT
, ROUTINE
, ARG
, RQSTP
);
566 "\tif (retval > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
567 TRANSP
, RESULT
, RESULT
);
570 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
571 RESULT
, TRANSP
, RESULT
, RESULT
);
572 printerr("systemerr", TRANSP
);
573 f_print(fout
, "\t}\n");
575 printif("freeargs", TRANSP
, "(caddr_t)&", ARG
);
576 (void) sprintf(_errbuf
, "unable to free arguments");
577 print_err_message("\t\t");
578 f_print(fout
, "\t\texit(1);\n");
579 f_print(fout
, "\t}\n");
582 f_print(fout
, "\tif (!");
583 pvname(def
->def_name
, vp
->vers_num
);
584 f_print(fout
, "_freeresult");
585 f_print(fout
, "(%s, xdr_%s, (caddr_t)&%s)) {\n",
586 TRANSP
, RESULT
, RESULT
);
587 (void) sprintf(_errbuf
, "unable to free results");
588 print_err_message("\t\t");
589 f_print(fout
, "\t\texit(1);\n");
590 f_print(fout
, "\t}\n");
594 f_print(fout
, "}\n");
599 printerr(err
, transp
)
603 f_print(fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
607 printif(proc
, transp
, prefix
, arg
)
613 f_print(fout
, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
614 proc
, transp
, arg
, prefix
, arg
);
621 for (; proc
!= NULL
; proc
= proc
->next
) {
622 if (streq(proc
->proc_num
, "0")) {
630 write_inetmost(infile
)
633 f_print(fout
, "\tSVCXPRT *%s = NULL;\n", TRANSP
);
634 f_print(fout
, "\tint sock;\n");
635 f_print(fout
, "\tint proto = 0;\n");
636 f_print(fout
, "\tstruct sockaddr_in saddr;\n");
637 f_print(fout
, "\tsocklen_t asize = (socklen_t)sizeof(saddr);\n");
640 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
641 f_print(fout
, "\t\tsocklen_t ssize = (socklen_t)sizeof(int);\n\n");
642 f_print(fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
643 f_print(fout
, "\t\t\texit(1);\n");
644 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
645 f_print(fout
, "\t\t\t\t(void *)&_rpcfdtype, &ssize) == -1)\n");
646 f_print(fout
, "\t\t\texit(1);\n");
647 f_print(fout
, "\t\tsock = 0;\n");
648 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
649 f_print(fout
, "\t\tproto = 0;\n");
650 open_log_file(infile
, "\t\t");
651 f_print(fout
, "\t} else {\n");
652 write_rpc_svc_fg(infile
, "\t\t");
653 f_print(fout
, "\t\tsock = RPC_ANYSOCK;\n");
654 print_pmapunset("\t\t");
655 f_print(fout
, "\t}\n");
663 f_print(fout
, "%sexit(0);\n", space
);
666 f_print(fout
, "%s_rpcsvcdirty = 0;\n", space
);
667 f_print(fout
, "%sreturn;\n", space
);
672 print_pmapunset(space
)
679 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
680 def
= (definition
*) l
->val
;
681 if (def
->def_kind
== DEF_PROGRAM
) {
682 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
684 f_print(fout
, "%s(void) pmap_unset(%s, %s);\n",
685 space
, def
->def_name
, vp
->vers_name
);
692 print_err_message(space
)
696 f_print(fout
, "%ssyslog(LOG_ERR, \"%s\");\n", space
, _errbuf
);
698 if (inetdflag
|| pmflag
)
699 f_print(fout
, "%s_msgout(\"%s\");\n", space
, _errbuf
);
701 f_print(fout
, "%sfprintf(stderr, \"%s\");\n", space
, _errbuf
);
704 * Write the server auxiliary function ( _msgout, timeout)
707 write_svc_aux(nomain
)
713 write_timeout_func();
714 if (callerflag
) /* EVAS */
715 write_caller_func(); /* EVAS */
718 * Write the _msgout function
724 f_print(fout
, "static\n");
726 f_print(fout
, "void _msgout(msg)\n");
727 f_print(fout
, "\tchar *msg;\n");
729 f_print(fout
, "void _msgout(const char *msg)\n");
731 f_print(fout
, "{\n");
732 f_print(fout
, "#ifdef RPC_SVC_FG\n");
733 if (inetdflag
|| pmflag
)
734 f_print(fout
, "\tif (_rpcpmstart)\n");
735 f_print(fout
, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
736 f_print(fout
, "\telse\n");
737 f_print(fout
, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
738 f_print(fout
, "#else\n");
739 f_print(fout
, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
740 f_print(fout
, "#endif\n");
741 f_print(fout
, "}\n");
744 * Write the timeout function
753 f_print(fout
, "static void closedown(void);\n");
756 f_print(fout
, "static void\n");
757 f_print(fout
, "closedown()\n");
758 f_print(fout
, "{\n");
759 f_print(fout
, "\tif (_rpcsvcdirty == 0) {\n");
760 f_print(fout
, "\t\textern fd_set svc_fdset;\n");
761 f_print(fout
, "\t\tstatic int size;\n");
762 f_print(fout
, "\t\tint i, openfd;\n");
763 if (tirpcflag
&& pmflag
) {
764 f_print(fout
, "\t\tstruct t_info tinfo;\n\n");
765 f_print(fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
767 f_print(fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
769 f_print(fout
, "\t\t\texit(0);\n");
770 f_print(fout
, "\t\tif (size == 0) {\n");
772 f_print(fout
, "\t\t\tstruct rlimit rl;\n\n");
773 f_print(fout
, "\t\t\trl.rlim_max = 0;\n");
774 f_print(fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
775 f_print(fout
, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
776 f_print(fout
, "\t\t\t\treturn;\n");
778 f_print(fout
, "\t\t\tsize = getdtablesize();\n");
780 f_print(fout
, "\t\t}\n");
781 f_print(fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
782 f_print(fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
783 f_print(fout
, "\t\t\t\topenfd++;\n");
784 f_print(fout
, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
785 f_print(fout
, "\t\t\texit(0);\n");
786 f_print(fout
, "\t}\n");
787 f_print(fout
, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
788 f_print(fout
, "}\n");
794 #define P(s) f_print(fout, s);
797 P("char *svc_caller()\n");
799 P(" struct sockaddr_in actual;\n");
800 P(" struct hostent *hp;\n");
801 P(" static struct in_addr prev;\n");
802 P(" static char cname[128];\n\n");
804 P(" actual = *svc_getcaller(caller);\n\n");
806 P(" if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
807 P(" sizeof(struct in_addr)) == 0)\n");
808 P(" return (cname);\n\n");
810 P(" prev = actual.sin_addr;\n\n");
812 P(" hp = gethostbyaddr((char *)&actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
813 P(" if (hp == NULL) { /* dummy one up */\n");
814 P(" extern char *inet_ntoa();\n");
815 P(" strlcpy(cname, inet_ntoa(actual.sin_addr), sizeof(cname));\n");
817 P(" strlcpy(cname, hp->h_name, sizeof(cname));\n");
820 P(" return (cname);\n");
826 * Write the most of port monitor support
829 write_pm_most(infile
, netflag
)
837 f_print(fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
838 f_print(fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
839 f_print(fout
, " !strcmp(mname, \"timod\"))) {\n");
840 f_print(fout
, "\t\tchar *netid;\n");
841 if (!netflag
) { /* Not included by -n option */
842 f_print(fout
, "\t\tstruct netconfig *nconf = NULL;\n");
843 f_print(fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
846 f_print(fout
, "\t\tint pmclose;\n");
847 /* not necessary, defined in /usr/include/stdlib */
848 /* f_print(fout, "\t\textern char *getenv();\n");*/
850 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
852 open_log_file(infile
, "\t\t");
853 f_print(fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
854 sprintf(_errbuf
, "cannot get transport name");
855 print_err_message("\t\t\t");
856 f_print(fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
857 sprintf(_errbuf
, "cannot get transport info");
858 print_err_message("\t\t\t");
859 f_print(fout
, "\t\t}\n");
861 * A kludgy support for inetd services. Inetd only works with
862 * sockmod, and RPC works only with timod, hence all this jugglery
864 f_print(fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
865 f_print(fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
866 sprintf(_errbuf
, "could not get the right module");
867 print_err_message("\t\t\t\t");
868 f_print(fout
, "\t\t\t\texit(1);\n");
869 f_print(fout
, "\t\t\t}\n");
870 f_print(fout
, "\t\t}\n");
872 f_print(fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
873 f_print(fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
875 sprintf(_errbuf
, "cannot create server handle");
876 print_err_message("\t\t\t");
877 f_print(fout
, "\t\t\texit(1);\n");
878 f_print(fout
, "\t\t}\n");
879 f_print(fout
, "\t\tif (nconf)\n");
880 f_print(fout
, "\t\t\tfreenetconfigent(nconf);\n");
881 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
882 def
= (definition
*) l
->val
;
883 if (def
->def_kind
!= DEF_PROGRAM
) {
886 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
888 "\t\tif (!svc_reg(%s, %s, %s, ",
889 TRANSP
, def
->def_name
, vp
->vers_name
);
890 pvname(def
->def_name
, vp
->vers_num
);
891 f_print(fout
, ", 0)) {\n");
892 (void) sprintf(_errbuf
, "unable to register (%s, %s).",
893 def
->def_name
, vp
->vers_name
);
894 print_err_message("\t\t\t");
895 f_print(fout
, "\t\t\texit(1);\n");
896 f_print(fout
, "\t\t}\n");
900 f_print(fout
, "\t\tif (pmclose) {\n");
901 f_print(fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
902 Cflag
? "(SIG_PF)" : "(void(*)())");
903 f_print(fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
904 f_print(fout
, "\t\t}\n");
906 f_print(fout
, "\t\tsvc_run();\n");
907 f_print(fout
, "\t\texit(1);\n");
908 f_print(fout
, "\t\t/* NOTREACHED */\n");
909 f_print(fout
, "\t}\n");
912 * Support for backgrounding the server if self started.
915 write_rpc_svc_fg(infile
, sp
)
919 f_print(fout
, "#ifndef RPC_SVC_FG\n");
920 f_print(fout
, "%sint size;\n", sp
);
922 f_print(fout
, "%sstruct rlimit rl;\n", sp
);
924 f_print(fout
, "%sint pid, i;\n\n", sp
);
925 f_print(fout
, "%spid = fork();\n", sp
);
926 f_print(fout
, "%sif (pid < 0) {\n", sp
);
927 f_print(fout
, "%s\tperror(\"cannot fork\");\n", sp
);
928 f_print(fout
, "%s\texit(1);\n", sp
);
929 f_print(fout
, "%s}\n", sp
);
930 f_print(fout
, "%sif (pid)\n", sp
);
931 f_print(fout
, "%s\texit(0);\n", sp
);
932 /* get number of file descriptors */
934 f_print(fout
, "%srl.rlim_max = 0;\n", sp
);
935 f_print(fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
936 f_print(fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
937 f_print(fout
, "%s\texit(1);\n", sp
);
939 f_print(fout
, "%ssize = getdtablesize();\n", sp
);
942 f_print(fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
943 f_print(fout
, "%s\t(void) close(i);\n", sp
);
944 /* Redirect stderr and stdout to console */
945 f_print(fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
946 f_print(fout
, "%s(void) dup2(i, 1);\n", sp
);
947 f_print(fout
, "%s(void) dup2(i, 2);\n", sp
);
948 /* This removes control of the controlling terminal */
950 f_print(fout
, "%ssetsid();\n", sp
);
952 f_print(fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
953 f_print(fout
, "%sif (i >= 0) {\n", sp
);
954 f_print(fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);
955 f_print(fout
, "%s\t(void) close(i);\n", sp
);
956 f_print(fout
, "%s}\n", sp
);
959 open_log_file(infile
, sp
);
960 f_print(fout
, "#endif\n");
962 open_log_file(infile
, sp
);
966 open_log_file(infile
, sp
)
972 s
= strrchr(infile
, '.');
975 p
= strrchr(infile
, '/');
980 f_print(fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, p
);
986 * write a registration for the given transport for Inetd
989 write_inetd_register(transp
)
1003 if (streq(transp
, "udp"))
1007 f_print(fout
, "\n");
1009 f_print(fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1010 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1012 if (inetdflag
&& streq(transp
, "tcp")) {
1013 f_print(fout
, "%s\tif (_rpcpmstart)\n", sp
);
1015 f_print(fout
, "%s\t\t%s = svc%s_create(%s",
1016 sp
, TRANSP
, "fd", inetdflag
? "sock" : "RPC_ANYSOCK");
1018 f_print(fout
, ", 0, 0");
1019 f_print(fout
, ");\n");
1021 f_print(fout
, "%s\telse\n", sp
);
1023 f_print(fout
, "%s\t\t%s = svc%s_create(%s",
1024 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1026 f_print(fout
, ", 0, 0");
1027 f_print(fout
, ");\n");
1030 f_print(fout
, "%s\t%s = svc%s_create(%s",
1031 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1033 f_print(fout
, ", 0, 0");
1034 f_print(fout
, ");\n");
1036 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1037 (void) sprintf(_errbuf
, "cannot create %s service.", transp
);
1038 (void) sprintf(tmpbuf
, "%s\t\t", sp
);
1039 print_err_message(tmpbuf
);
1040 f_print(fout
, "%s\t\texit(1);\n", sp
);
1041 f_print(fout
, "%s\t}\n", sp
);
1044 f_print(fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1045 f_print(fout
, "%s\tproto = IPPROTO_%s;\n",
1046 sp
, isudp
? "UDP" : "TCP");
1048 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
1049 def
= (definition
*) l
->val
;
1050 if (def
->def_kind
!= DEF_PROGRAM
) {
1053 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
1054 f_print(fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1055 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1056 pvname(def
->def_name
, vp
->vers_num
);
1058 f_print(fout
, ", proto)) {\n");
1060 f_print(fout
, ", IPPROTO_%s)) {\n",
1061 isudp
? "UDP" : "TCP");
1062 (void) sprintf(_errbuf
, "unable to register (%s, %s, %s).",
1063 def
->def_name
, vp
->vers_name
, transp
);
1064 print_err_message(tmpbuf
);
1065 f_print(fout
, "%s\t\texit(1);\n", sp
);
1066 f_print(fout
, "%s\t}\n", sp
);
1070 f_print(fout
, "\t}\n");
1077 if (isvectordef(type
, REL_ALIAS
)) {