]>
git.cameronkatri.com Git - apple_cmds.git/blob - developer_cmds/rpcgen/rpc_svcout.c
1 /* $NetBSD: rpc_svcout.c,v 1.10 1997/10/18 10:54:07 lukem 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 #include <sys/cdefs.h>
35 static char sccsid
[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
37 __RCSID("$NetBSD: rpc_svcout.c,v 1.10 1997/10/18 10:54:07 lukem Exp $");
42 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
47 #include "rpc_parse.h"
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";
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 *));
72 char _errbuf
[256]; /* For all messages */
75 p_xdrfunc(rname
, typename
)
80 f_print(fout
, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
83 f_print(fout
, "\t\txdr_%s = xdr_%s;\n", rname
, stringfix(typename
));
87 internal_proctype(plist
)
90 f_print(fout
, "static ");
91 ptype(plist
->res_prefix
, plist
->res_type
, 1);
97 * write most of the service, that is, everything but the registrations.
100 write_most(infile
, netflag
, nomain
)
101 char *infile
; /* our name */
105 if (inetdflag
|| pmflag
) {
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");
113 f_print(fout
, "%s int _rpcsvcdirty;", var_type
);
114 f_print(fout
, "\t/* Still serving ? */\n");
116 write_svc_aux(nomain
);
118 /* write out dispatcher and stubs */
119 write_programs(nomain
? (char *) NULL
: "static");
124 f_print(fout
, "\n\n");
126 f_print(fout
, "int main( int argc, char* argv[] );\n");
127 f_print(fout
, "\nint\n");
129 f_print(fout
, "main( int argc, char* argv[] )\n");
131 f_print(fout
, "main(argc, argv)\nint argc;\nchar *argv[];\n");
132 f_print(fout
, "{\n");
134 write_inetmost(infile
); /* Includes call to write_rpc_svc_fg() */
138 f_print(fout
, "\tSVCXPRT *%s;\n", TRANSP
);
139 f_print(fout
, "\tstruct netconfig *nconf = NULL;\n");
141 f_print(fout
, "\tpid_t pid;\n");
142 f_print(fout
, "\tint i;\n");
143 f_print(fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
144 write_pm_most(infile
, netflag
);
145 f_print(fout
, "\telse {\n");
146 write_rpc_svc_fg(infile
, "\t\t");
147 f_print(fout
, "\t}\n");
149 f_print(fout
, "\tSVCXPRT *%s;\n", TRANSP
);
151 print_pmapunset("\t");
155 if (logflag
&& !inetdflag
) {
156 open_log_file(infile
, "\t");
160 * write a registration for the given transport
163 write_netid_register(transp
)
174 f_print(fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
175 f_print(fout
, "%s\tif (nconf == NULL) {\n", sp
);
176 (void) sprintf(_errbuf
, "cannot find %s netid.", transp
);
177 sprintf(tmpbuf
, "%s\t\t", sp
);
178 print_err_message(tmpbuf
);
179 f_print(fout
, "%s\t\texit(1);\n", sp
);
180 f_print(fout
, "%s\t}\n", sp
);
181 f_print(fout
, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
183 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
184 (void) sprintf(_errbuf
, "cannot create %s service.", transp
);
185 print_err_message(tmpbuf
);
186 f_print(fout
, "%s\t\texit(1);\n", sp
);
187 f_print(fout
, "%s\t}\n", sp
);
189 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
190 def
= (definition
*) l
->val
;
191 if (def
->def_kind
!= DEF_PROGRAM
) {
194 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
196 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
197 sp
, def
->def_name
, vp
->vers_name
);
199 "%s\tif (!svc_reg(%s, %s, %s, ",
200 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
201 pvname(def
->def_name
, vp
->vers_num
);
202 f_print(fout
, ", nconf)) {\n");
203 (void) sprintf(_errbuf
, "unable to register (%s, %s, %s).",
204 def
->def_name
, vp
->vers_name
, transp
);
205 print_err_message(tmpbuf
);
206 f_print(fout
, "%s\t\texit(1);\n", sp
);
207 f_print(fout
, "%s\t}\n", sp
);
210 f_print(fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
213 * write a registration for the given transport for TLI
216 write_nettype_register(transp
)
223 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
224 def
= (definition
*) l
->val
;
225 if (def
->def_kind
!= DEF_PROGRAM
) {
228 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
229 f_print(fout
, "\tif (!svc_create(");
230 pvname(def
->def_name
, vp
->vers_num
);
231 f_print(fout
, ", %s, %s, \"%s\")) {\n ",
232 def
->def_name
, vp
->vers_name
, transp
);
233 (void) sprintf(_errbuf
,
234 "unable to create (%s, %s) for %s.",
235 def
->def_name
, vp
->vers_name
, transp
);
236 print_err_message("\t\t");
237 f_print(fout
, "\t\texit(1);\n");
238 f_print(fout
, "\t}\n");
243 * write the rest of the service
250 f_print(fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
251 (void) sprintf(_errbuf
, "could not create a handle");
252 print_err_message("\t\t");
253 f_print(fout
, "\t\texit(1);\n");
254 f_print(fout
, "\t}\n");
256 f_print(fout
, "\tif (_rpcpmstart) {\n");
258 "\t\t(void) signal(SIGALRM, %s closedown);\n",
259 Cflag
? "(SIG_PF)" : "(void(*)())");
260 f_print(fout
, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
261 f_print(fout
, "\t}\n");
264 f_print(fout
, "\tsvc_run();\n");
265 (void) sprintf(_errbuf
, "svc_run returned");
266 print_err_message("\t");
267 f_print(fout
, "\texit(1);\n");
268 f_print(fout
, "\t/* NOTREACHED */\n");
269 f_print(fout
, "}\n");
273 write_programs(storage
)
279 /* write out stubs for procedure definitions */
280 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
281 def
= (definition
*) l
->val
;
282 if (def
->def_kind
== DEF_PROGRAM
) {
283 write_real_program(def
);
287 /* write out dispatcher for each program */
288 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
289 def
= (definition
*) l
->val
;
290 if (def
->def_kind
== DEF_PROGRAM
) {
291 write_program(def
, storage
);
297 /* write out definition of internal function (e.g. _printmsg_1(...))
298 which calls server's defintion of actual function (e.g. printmsg_1(...)).
299 Unpacks single user argument of printmsg_1 to call-by-value format
300 expected by printmsg_1. */
302 write_real_program(def
)
310 return; /* not needed for old style */
311 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
312 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
314 internal_proctype(proc
);
315 f_print(fout
, "\n_");
316 pvname(proc
->proc_name
, vp
->vers_num
);
320 if (proc
->arg_num
> 1)
321 f_print(fout
, "%s", proc
->args
.argname
);
323 ptype(proc
->args
.decls
->decl
.prefix
,
324 proc
->args
.decls
->decl
.type
, 0);
325 f_print(fout
, " *argp, struct svc_req *%s)\n",
328 f_print(fout
, "(argp, %s)\n", RQSTP
);
330 if (proc
->arg_num
> 1)
331 f_print(fout
, "\t%s *argp;\n", proc
->args
.argname
);
334 ptype(proc
->args
.decls
->decl
.prefix
,
335 proc
->args
.decls
->decl
.type
, 0);
336 f_print(fout
, " *argp;\n");
338 f_print(fout
, " struct svc_req *%s;\n", RQSTP
);
341 f_print(fout
, "{\n");
342 f_print(fout
, "\treturn(");
343 pvname_svc(proc
->proc_name
, vp
->vers_num
);
345 if (proc
->arg_num
< 2) { /* single argument */
346 if (!streq(proc
->args
.decls
->decl
.type
, "void"))
347 f_print(fout
, "*argp, "); /* non-void */
349 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
350 f_print(fout
, "argp->%s, ", l
->decl
.name
);
352 f_print(fout
, "%s));\n}\n", RQSTP
);
358 write_program(def
, storage
)
366 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
369 if (storage
!= NULL
) {
370 f_print(fout
, "%s ", storage
);
372 f_print(fout
, "void ");
373 pvname(def
->def_name
, vp
->vers_num
);
374 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
375 f_print(fout
, "SVCXPRT *%s);\n", TRANSP
);
378 if (storage
!= NULL
) {
379 f_print(fout
, "%s ", storage
);
381 f_print(fout
, "void\n");
382 pvname(def
->def_name
, vp
->vers_num
);
385 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
386 f_print(fout
, "SVCXPRT *%s)\n", TRANSP
);
388 f_print(fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
389 f_print(fout
, " struct svc_req *%s;\n", RQSTP
);
390 f_print(fout
, " SVCXPRT *%s;\n", TRANSP
);
393 f_print(fout
, "{\n");
396 f_print(fout
, "\tunion {\n");
397 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
398 if (proc
->arg_num
< 2) { /* single argument */
399 if (streq(proc
->args
.decls
->decl
.type
,
404 f_print(fout
, "\t\t");
405 ptype(proc
->args
.decls
->decl
.prefix
,
406 proc
->args
.decls
->decl
.type
, 0);
407 pvname(proc
->proc_name
, vp
->vers_num
);
408 f_print(fout
, "_arg;\n");
412 f_print(fout
, "\t\t%s", proc
->args
.argname
);
414 pvname(proc
->proc_name
, vp
->vers_num
);
415 f_print(fout
, "_arg;\n");
419 f_print(fout
, "\t\tint fill;\n");
421 f_print(fout
, "\t} %s;\n", ARG
);
422 f_print(fout
, "\tchar *%s;\n", RESULT
);
425 f_print(fout
, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG
, RESULT
);
427 "\tchar *(*%s)(char *, struct svc_req *);\n",
430 f_print(fout
, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG
, RESULT
);
431 f_print(fout
, "\tchar *(*%s)();\n", ROUTINE
);
437 f_print(fout
, "\tcaller = transp;\n"); /* EVAS */
439 f_print(fout
, "\t_rpcsvcdirty = 1;\n");
440 f_print(fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
441 if (!nullproc(vp
->procs
)) {
442 f_print(fout
, "\tcase NULLPROC:\n");
444 "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
446 print_return("\t\t");
449 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
450 f_print(fout
, "\tcase %s:\n", proc
->proc_name
);
451 if (proc
->arg_num
< 2) { /* single argument */
452 p_xdrfunc(ARG
, proc
->args
.decls
->decl
.type
);
454 p_xdrfunc(ARG
, proc
->args
.argname
);
456 p_xdrfunc(RESULT
, proc
->res_type
);
459 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
462 f_print(fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
464 if (newstyle
) { /* new style: calls internal routine */
468 pvname_svc(proc
->proc_name
, vp
->vers_num
);
470 pvname(proc
->proc_name
, vp
->vers_num
);
471 f_print(fout
, ";\n");
472 f_print(fout
, "\t\tbreak;\n\n");
474 f_print(fout
, "\tdefault:\n");
475 printerr("noproc", TRANSP
);
476 print_return("\t\t");
477 f_print(fout
, "\t}\n");
479 f_print(fout
, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
480 printif("getargs", TRANSP
, "(caddr_t) &", ARG
);
481 printerr("decode", TRANSP
);
482 print_return("\t\t");
483 f_print(fout
, "\t}\n");
486 f_print(fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
487 RESULT
, ROUTINE
, ARG
, RQSTP
);
489 f_print(fout
, "\t%s = (*%s)(&%s, %s);\n",
490 RESULT
, ROUTINE
, ARG
, RQSTP
);
492 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) xdr_%s, %s)) {\n",
493 RESULT
, TRANSP
, RESULT
, RESULT
);
494 printerr("systemerr", TRANSP
);
495 f_print(fout
, "\t}\n");
497 printif("freeargs", TRANSP
, "(caddr_t) &", ARG
);
498 (void) sprintf(_errbuf
, "unable to free arguments");
499 print_err_message("\t\t");
500 f_print(fout
, "\t\texit(1);\n");
501 f_print(fout
, "\t}\n");
503 f_print(fout
, "}\n");
508 printerr(err
, transp
)
512 f_print(fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
516 printif(proc
, transp
, prefix
, arg
)
522 f_print(fout
, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
523 proc
, transp
, arg
, prefix
, arg
);
530 for (; proc
!= NULL
; proc
= proc
->next
) {
531 if (streq(proc
->proc_num
, "0")) {
539 write_inetmost(infile
)
542 f_print(fout
, "\tSVCXPRT *%s = NULL;\n", TRANSP
);
543 f_print(fout
, "\tint sock;\n");
544 f_print(fout
, "\tint proto = 0;\n");
545 f_print(fout
, "\tstruct sockaddr_in saddr;\n");
546 f_print(fout
, "\tint asize = sizeof (saddr);\n");
549 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
550 f_print(fout
, "\t\tint ssize = sizeof (int);\n\n");
551 f_print(fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
552 f_print(fout
, "\t\t\texit(1);\n");
553 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
554 f_print(fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
555 f_print(fout
, "\t\t\texit(1);\n");
556 f_print(fout
, "\t\tsock = 0;\n");
557 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
558 f_print(fout
, "\t\tproto = 0;\n");
559 open_log_file(infile
, "\t\t");
560 f_print(fout
, "\t} else {\n");
561 write_rpc_svc_fg(infile
, "\t\t");
562 f_print(fout
, "\t\tsock = RPC_ANYSOCK;\n");
563 print_pmapunset("\t\t");
564 f_print(fout
, "\t}\n");
572 f_print(fout
, "%sexit(0);\n", space
);
575 f_print(fout
, "%s_rpcsvcdirty = 0;\n", space
);
576 f_print(fout
, "%sreturn;\n", space
);
581 print_pmapunset(space
)
588 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
589 def
= (definition
*) l
->val
;
590 if (def
->def_kind
== DEF_PROGRAM
) {
591 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
593 f_print(fout
, "%s(void) pmap_unset(%s, %s);\n",
594 space
, def
->def_name
, vp
->vers_name
);
601 print_err_message(space
)
605 f_print(fout
, "%ssyslog(LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
607 if (inetdflag
|| pmflag
)
608 f_print(fout
, "%s_msgout(\"%s\");\n", space
, _errbuf
);
610 f_print(fout
, "%sfprintf(stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
613 * Write the server auxiliary function ( _msgout, timeout)
616 write_svc_aux(nomain
)
622 write_timeout_func();
623 if (callerflag
) /* EVAS */
624 write_caller_func(); /* EVAS */
627 * Write the _msgout function
633 f_print(fout
, "static\n");
635 f_print(fout
, "void _msgout(msg)\n");
636 f_print(fout
, "\tchar *msg;\n");
638 f_print(fout
, "void _msgout(char* msg)\n");
640 f_print(fout
, "{\n");
641 f_print(fout
, "#ifdef RPC_SVC_FG\n");
642 if (inetdflag
|| pmflag
)
643 f_print(fout
, "\tif (_rpcpmstart)\n");
644 f_print(fout
, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
645 f_print(fout
, "\telse\n");
646 f_print(fout
, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
647 f_print(fout
, "#else\n");
648 f_print(fout
, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
649 f_print(fout
, "#endif\n");
650 f_print(fout
, "}\n");
653 * Write the timeout function
662 f_print(fout
, "static void closedown(void);\n");
665 f_print(fout
, "static void\n");
666 f_print(fout
, "closedown()\n");
667 f_print(fout
, "{\n");
668 f_print(fout
, "\tif (_rpcsvcdirty == 0) {\n");
669 f_print(fout
, "\t\textern fd_set svc_fdset;\n");
670 f_print(fout
, "\t\tstatic int size;\n");
671 f_print(fout
, "\t\tint i, openfd;\n");
672 if (tirpcflag
&& pmflag
) {
673 f_print(fout
, "\t\tstruct t_info tinfo;\n\n");
674 f_print(fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
676 f_print(fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
678 f_print(fout
, "\t\t\texit(0);\n");
679 f_print(fout
, "\t\tif (size == 0) {\n");
681 f_print(fout
, "\t\t\tstruct rlimit rl;\n\n");
682 f_print(fout
, "\t\t\trl.rlim_max = 0;\n");
683 f_print(fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
684 f_print(fout
, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
685 f_print(fout
, "\t\t\t\treturn;\n");
687 f_print(fout
, "\t\t\tsize = getdtablesize();\n");
689 f_print(fout
, "\t\t}\n");
690 f_print(fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
691 f_print(fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
692 f_print(fout
, "\t\t\t\topenfd++;\n");
693 f_print(fout
, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
694 f_print(fout
, "\t\t\texit(0);\n");
695 f_print(fout
, "\t}\n");
696 f_print(fout
, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
697 f_print(fout
, "}\n");
703 #define P(s) f_print(fout, s);
706 P("char *svc_caller()\n");
708 P(" struct sockaddr_in actual;\n");
709 P(" struct hostent *hp;\n");
710 P(" static struct in_addr prev;\n");
711 P(" static char cname[128];\n\n");
713 P(" actual = *svc_getcaller(caller);\n\n");
715 P(" if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
716 P(" sizeof(struct in_addr)) == 0)\n");
717 P(" return (cname);\n\n");
719 P(" prev = actual.sin_addr;\n\n");
721 P(" hp = gethostbyaddr((char *) &actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
722 P(" if (hp == NULL) { /* dummy one up */\n");
723 P(" extern char *inet_ntoa();\n");
724 P(" strcpy(cname, inet_ntoa(actual.sin_addr));\n");
726 P(" strcpy(cname, hp->h_name);\n");
729 P(" return (cname);\n");
735 * Write the most of port monitor support
738 write_pm_most(infile
, netflag
)
746 f_print(fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
747 f_print(fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
748 f_print(fout
, " !strcmp(mname, \"timod\"))) {\n");
749 f_print(fout
, "\t\tchar *netid;\n");
750 if (!netflag
) { /* Not included by -n option */
751 f_print(fout
, "\t\tstruct netconfig *nconf = NULL;\n");
752 f_print(fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
755 f_print(fout
, "\t\tint pmclose;\n");
756 /* not necessary, defined in /usr/include/stdlib */
757 /* f_print(fout, "\t\textern char *getenv();\n");*/
759 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
761 open_log_file(infile
, "\t\t");
762 f_print(fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
763 sprintf(_errbuf
, "cannot get transport name");
764 print_err_message("\t\t\t");
765 f_print(fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
766 sprintf(_errbuf
, "cannot get transport info");
767 print_err_message("\t\t\t");
768 f_print(fout
, "\t\t}\n");
770 * A kludgy support for inetd services. Inetd only works with
771 * sockmod, and RPC works only with timod, hence all this jugglery
773 f_print(fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
774 f_print(fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
775 sprintf(_errbuf
, "could not get the right module");
776 print_err_message("\t\t\t\t");
777 f_print(fout
, "\t\t\t\texit(1);\n");
778 f_print(fout
, "\t\t\t}\n");
779 f_print(fout
, "\t\t}\n");
781 f_print(fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
782 f_print(fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
784 sprintf(_errbuf
, "cannot create server handle");
785 print_err_message("\t\t\t");
786 f_print(fout
, "\t\t\texit(1);\n");
787 f_print(fout
, "\t\t}\n");
788 f_print(fout
, "\t\tif (nconf)\n");
789 f_print(fout
, "\t\t\tfreenetconfigent(nconf);\n");
790 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
791 def
= (definition
*) l
->val
;
792 if (def
->def_kind
!= DEF_PROGRAM
) {
795 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
797 "\t\tif (!svc_reg(%s, %s, %s, ",
798 TRANSP
, def
->def_name
, vp
->vers_name
);
799 pvname(def
->def_name
, vp
->vers_num
);
800 f_print(fout
, ", 0)) {\n");
801 (void) sprintf(_errbuf
, "unable to register (%s, %s).",
802 def
->def_name
, vp
->vers_name
);
803 print_err_message("\t\t\t");
804 f_print(fout
, "\t\t\texit(1);\n");
805 f_print(fout
, "\t\t}\n");
809 f_print(fout
, "\t\tif (pmclose) {\n");
810 f_print(fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
811 Cflag
? "(SIG_PF)" : "(void(*)())");
812 f_print(fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
813 f_print(fout
, "\t\t}\n");
815 f_print(fout
, "\t\tsvc_run();\n");
816 f_print(fout
, "\t\texit(1);\n");
817 f_print(fout
, "\t\t/* NOTREACHED */\n");
818 f_print(fout
, "\t}\n");
821 * Support for backgrounding the server if self started.
824 write_rpc_svc_fg(infile
, sp
)
828 f_print(fout
, "#ifndef RPC_SVC_FG\n");
829 f_print(fout
, "%sint size;\n", sp
);
831 f_print(fout
, "%sstruct rlimit rl;\n", sp
);
833 f_print(fout
, "%sint pid, i;\n\n", sp
);
834 f_print(fout
, "%spid = fork();\n", sp
);
835 f_print(fout
, "%sif (pid < 0) {\n", sp
);
836 f_print(fout
, "%s\tperror(\"cannot fork\");\n", sp
);
837 f_print(fout
, "%s\texit(1);\n", sp
);
838 f_print(fout
, "%s}\n", sp
);
839 f_print(fout
, "%sif (pid)\n", sp
);
840 f_print(fout
, "%s\texit(0);\n", sp
);
841 /* get number of file descriptors */
843 f_print(fout
, "%srl.rlim_max = 0;\n", sp
);
844 f_print(fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
845 f_print(fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
846 f_print(fout
, "%s\texit(1);\n", sp
);
848 f_print(fout
, "%ssize = getdtablesize();\n", sp
);
851 f_print(fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
852 f_print(fout
, "%s\t(void) close(i);\n", sp
);
853 /* Redirect stderr and stdout to console */
854 f_print(fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
855 f_print(fout
, "%s(void) dup2(i, 1);\n", sp
);
856 f_print(fout
, "%s(void) dup2(i, 2);\n", sp
);
857 /* This removes control of the controlling terminal */
859 f_print(fout
, "%ssetsid();\n", sp
);
861 f_print(fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
862 f_print(fout
, "%sif (i >= 0) {\n", sp
);
863 f_print(fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
864 f_print(fout
, "%s\t(void) close(i);\n", sp
);
865 f_print(fout
, "%s}\n", sp
);
868 open_log_file(infile
, sp
);
869 f_print(fout
, "#endif\n");
871 open_log_file(infile
, sp
);
875 open_log_file(infile
, sp
)
881 s
= strrchr(infile
, '.');
884 p
= strrchr(infile
, '/');
889 f_print(fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, p
);
895 * write a registration for the given transport for Inetd
898 write_inetd_register(transp
)
912 if (streq(transp
, "udp"))
918 f_print(fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
919 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
921 if (inetdflag
&& streq(transp
, "tcp")) {
922 f_print(fout
, "%s\tif (_rpcpmstart)\n", sp
);
924 f_print(fout
, "%s\t\t%s = svc%s_create(%s",
925 sp
, TRANSP
, "fd", inetdflag
? "sock" : "RPC_ANYSOCK");
927 f_print(fout
, ", 0, 0");
928 f_print(fout
, ");\n");
930 f_print(fout
, "%s\telse\n", sp
);
932 f_print(fout
, "%s\t\t%s = svc%s_create(%s",
933 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
935 f_print(fout
, ", 0, 0");
936 f_print(fout
, ");\n");
939 f_print(fout
, "%s\t%s = svc%s_create(%s",
940 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
942 f_print(fout
, ", 0, 0");
943 f_print(fout
, ");\n");
945 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
946 (void) sprintf(_errbuf
, "cannot create %s service.", transp
);
947 (void) sprintf(tmpbuf
, "%s\t\t", sp
);
948 print_err_message(tmpbuf
);
949 f_print(fout
, "%s\t\texit(1);\n", sp
);
950 f_print(fout
, "%s\t}\n", sp
);
953 f_print(fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
954 f_print(fout
, "%s\tproto = IPPROTO_%s;\n",
955 sp
, isudp
? "UDP" : "TCP");
957 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
958 def
= (definition
*) l
->val
;
959 if (def
->def_kind
!= DEF_PROGRAM
) {
962 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
963 f_print(fout
, "%s\tif (!svc_register(%s, %s, %s, ",
964 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
965 pvname(def
->def_name
, vp
->vers_num
);
967 f_print(fout
, ", proto)) {\n");
969 f_print(fout
, ", IPPROTO_%s)) {\n",
970 isudp
? "UDP" : "TCP");
971 (void) sprintf(_errbuf
, "unable to register (%s, %s, %s).",
972 def
->def_name
, vp
->vers_name
, transp
);
973 print_err_message(tmpbuf
);
974 f_print(fout
, "%s\t\texit(1);\n", sp
);
975 f_print(fout
, "%s\t}\n", sp
);
979 f_print(fout
, "\t}\n");