2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
28 * Mountain View, California 94043
32 * From: @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
36 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
40 #include "rpc_parse.h"
44 static const char RQSTP
[] = "rqstp";
45 static const char TRANSP
[] = "transp";
46 static const char ARG
[] = "argument";
47 static const char RESULT
[] = "result";
48 static const char ROUTINE
[] = "local";
49 static char RETVAL
[] = "retval";
51 char _errbuf
[256]; /* For all messages */
53 static void internal_proctype (const proc_list
* plist
);
54 static void p_xdrfunc (const char *rname
, const char *typename
);
55 static void write_real_program (const definition
* def
);
56 static void write_program (const definition
* def
, const char *storage
);
57 static void printerr (const char *err
, const char *transp
);
58 static void printif (const char *proc
, const char *transp
, const char *arg
);
59 static void write_inetmost (const char *infile
);
60 static void print_return (const char *space
);
61 static void print_pmapunset (const char *space
);
62 static void print_err_message (const char *space
);
63 static void write_timeout_func (void);
64 static void write_pm_most (const char *infile
, int netflag
);
65 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
66 static void open_log_file (const char *infile
, const char *sp
);
69 p_xdrfunc (const char *rname
, const char *typename
)
72 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
73 stringfix (typename
));
75 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
79 internal_proctype (const proc_list
* plist
)
81 f_print (fout
, "static ");
82 ptype (plist
->res_prefix
, plist
->res_type
, 1);
88 * write most of the service, that is, everything but the registrations.
91 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
93 if (inetdflag
|| pmflag
)
96 #ifdef __GNU_LIBRARY__
98 var_type
= (nomain
? "extern" : "");
100 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");
106 f_print (fout
, "%s int _rpcfdtype;", var_type
);
107 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
112 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
113 f_print (fout
, "\t/* Still serving ? */\n");
115 f_print(fout
, " /* States a server can be in wrt request */\n\n");
116 f_print(fout
, "#define\t_IDLE 0\n");
117 f_print(fout
, "#define\t_SERVED 1\n");
118 f_print(fout
, "#define\t_SERVING 2\n\n");
119 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
120 f_print(fout
, "\t /* Set when a request is serviced */\n");
124 f_print (fout
, "mutex_t _svcstate_lock;");
126 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
130 write_svc_aux (nomain
);
132 /* write out dispatcher and stubs */
133 write_programs (nomain
? NULL
: "static");
138 #ifdef __GNU_LIBRARY__
140 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
143 f_print (fout
, "\nint\nmain (argc, argv)\n");
144 f_print (fout
, "\tint argc;\n");
145 f_print (fout
, "\tchar **argv;\n");
148 f_print (fout
, "\nmain()\n");
150 f_print (fout
, "{\n");
153 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
161 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
162 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
164 f_print (fout
, "\tpid_t pid;\n");
165 f_print (fout
, "\tint i;\n");
166 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
168 if (mtflag
& timerflag
)
170 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
172 write_pm_most (infile
, netflag
);
173 f_print (fout
, "\telse {\n");
174 write_rpc_svc_fg (infile
, "\t\t");
175 f_print (fout
, "\t}\n");
179 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
180 f_print (fout
, "\n");
181 print_pmapunset ("\t");
185 if (logflag
&& !inetdflag
)
187 open_log_file (infile
, "\t");
192 * write a registration for the given transport
195 write_netid_register (const char *transp
)
204 f_print (fout
, "\n");
205 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
206 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
207 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
208 sprintf (tmpbuf
, "%s\t\t", sp
);
209 print_err_message (tmpbuf
);
210 f_print (fout
, "%s\t\texit (1);\n", sp
);
211 f_print (fout
, "%s\t}\n", sp
);
212 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
213 sp
, TRANSP
/*, transp *//* ?!?... */ );
214 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
215 sprintf (_errbuf
, "cannot create %s service.", transp
);
216 print_err_message (tmpbuf
);
217 f_print (fout
, "%s\t\texit (1);\n", sp
);
218 f_print (fout
, "%s\t}\n", sp
);
220 for (l
= defined
; l
!= NULL
; l
= l
->next
)
222 def
= (definition
*) l
->val
;
223 if (def
->def_kind
!= DEF_PROGRAM
)
227 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
229 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
230 sp
, def
->def_name
, vp
->vers_name
);
231 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
232 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
233 pvname (def
->def_name
, vp
->vers_num
);
234 f_print (fout
, ", nconf)) {\n");
235 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
236 def
->def_name
, vp
->vers_name
, transp
);
237 print_err_message (tmpbuf
);
238 f_print (fout
, "%s\t\texit (1);\n", sp
);
239 f_print (fout
, "%s\t}\n", sp
);
242 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
246 * write a registration for the given transport for TLI
249 write_nettype_register (const char *transp
)
255 for (l
= defined
; l
!= NULL
; l
= l
->next
)
257 def
= (definition
*) l
->val
;
258 if (def
->def_kind
!= DEF_PROGRAM
)
262 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
264 f_print (fout
, "\tif (!svc_create (");
265 pvname (def
->def_name
, vp
->vers_num
);
266 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
267 def
->def_name
, vp
->vers_name
, transp
);
268 (void) sprintf (_errbuf
,
269 "unable to create (%s, %s) for %s.",
270 def
->def_name
, vp
->vers_name
, transp
);
271 print_err_message ("\t\t");
272 f_print (fout
, "\t\texit (1);\n");
273 f_print (fout
, "\t}\n");
279 * write the rest of the service
284 f_print (fout
, "\n");
287 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
288 (void) sprintf (_errbuf
, "could not create a handle");
289 print_err_message ("\t\t");
290 f_print (fout
, "\t\texit (1);\n");
291 f_print (fout
, "\t}\n");
294 f_print (fout
, "\tif (_rpcpmstart) {\n");
296 "\t\t(void) signal (SIGALRM, %s closedown);\n",
297 Cflag
? "(SIG_PF)" : "(void(*)())");
298 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
299 f_print (fout
, "\t}\n");
302 f_print (fout
, "\tsvc_run ();\n");
303 (void) sprintf (_errbuf
, "svc_run returned");
304 print_err_message ("\t");
305 f_print (fout
, "\texit (1);\n");
306 f_print (fout
, "\t/* NOTREACHED */\n");
307 f_print (fout
, "}\n");
311 write_programs (const char *storage
)
316 /* write out stubs for procedure definitions */
317 for (l
= defined
; l
!= NULL
; l
= l
->next
)
319 def
= (definition
*) l
->val
;
320 if (def
->def_kind
== DEF_PROGRAM
)
322 write_real_program (def
);
326 /* write out dispatcher for each program */
327 for (l
= defined
; l
!= NULL
; l
= l
->next
)
329 def
= (definition
*) l
->val
;
330 if (def
->def_kind
== DEF_PROGRAM
)
332 write_program (def
, storage
);
337 /* write out definition of internal function (e.g. _printmsg_1(...))
338 which calls server's defintion of actual function (e.g. printmsg_1(...)).
339 Unpacks single user argument of printmsg_1 to call-by-value format
340 expected by printmsg_1. */
342 write_real_program (const definition
* def
)
349 return; /* not needed for old style */
350 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
352 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
354 fprintf (fout
, "\n");
356 internal_proctype (proc
);
358 f_print (fout
, "int");
359 f_print (fout
, "\n_");
360 pvname (proc
->proc_name
, vp
->vers_num
);
363 f_print (fout
, " (");
365 if (proc
->arg_num
> 1)
366 f_print (fout
, proc
->args
.argname
);
368 ptype (proc
->args
.decls
->decl
.prefix
,
369 proc
->args
.decls
->decl
.type
, 0);
372 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
376 f_print (fout
, " *argp, struct svc_req *%s)\n",
382 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
384 f_print (fout
, " (argp, %s)\n", RQSTP
);
386 if (proc
->arg_num
> 1)
387 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
390 f_print (fout
, "\t");
391 ptype (proc
->args
.decls
->decl
.prefix
,
392 proc
->args
.decls
->decl
.type
, 0);
393 f_print (fout
, " *argp;\n");
395 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
398 f_print (fout
, "{\n");
399 f_print (fout
, "\treturn (");
401 pvname_svc (proc
->proc_name
, vp
->vers_num
);
403 pvname (proc
->proc_name
, vp
->vers_num
);
405 if (proc
->arg_num
< 2)
406 { /* single argument */
407 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
408 f_print (fout
, "*argp, "); /* non-void */
412 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
413 f_print (fout
, "argp->%s, ", l
->decl
.name
);
416 f_print (fout
, "%s, ", RESULT
);
417 f_print (fout
, "%s));\n}\n", RQSTP
);
423 write_program (const definition
* def
, const char *storage
)
429 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
431 f_print (fout
, "\n");
434 f_print (fout
, "%s ", storage
);
436 f_print (fout
, "void\n");
437 pvname (def
->def_name
, vp
->vers_num
);
441 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
442 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
446 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
447 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
448 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
451 f_print (fout
, "{\n");
454 f_print (fout
, "\tunion {\n");
455 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
457 if (proc
->arg_num
< 2)
458 { /* single argument */
459 if (streq (proc
->args
.decls
->decl
.type
,
465 f_print (fout
, "\t\t");
466 ptype (proc
->args
.decls
->decl
.prefix
,
467 proc
->args
.decls
->decl
.type
, 0);
468 pvname (proc
->proc_name
, vp
->vers_num
);
469 f_print (fout
, "_arg;\n");
475 f_print (fout
, "\t\t%s", proc
->args
.argname
);
477 pvname (proc
->proc_name
, vp
->vers_num
);
478 f_print (fout
, "_arg;\n");
483 f_print (fout
, "\t\tint fill;\n");
485 f_print (fout
, "\t} %s;\n", ARG
);
488 f_print(fout
, "\tunion {\n");
489 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
490 if (!streq (proc
->res_type
, "void"))
492 f_print(fout
, "\t\t");
493 ptype(proc
->res_prefix
, proc
->res_type
, 0);
494 pvname(proc
->proc_name
, vp
->vers_num
);
495 f_print(fout
, "_res;\n");
497 f_print(fout
, "\t} %s;\n", RESULT
);
498 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
501 f_print (fout
, "\tchar *%s;\n", RESULT
);
505 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
508 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
511 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
516 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
518 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
520 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
522 f_print (fout
, "\n");
526 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
530 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
531 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
533 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
537 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
538 if (!nullproc (vp
->procs
))
540 f_print (fout
, "\tcase NULLPROC:\n");
542 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
544 print_return ("\t\t");
545 f_print (fout
, "\n");
547 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
549 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
550 if (proc
->arg_num
< 2)
551 { /* single argument */
552 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
556 p_xdrfunc (ARG
, proc
->args
.argname
);
558 p_xdrfunc (RESULT
, proc
->res_type
);
563 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
567 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
572 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
574 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
577 { /* new style: calls internal routine */
580 if ((Cflag
|| mtflag
) && !newstyle
)
581 pvname_svc (proc
->proc_name
, vp
->vers_num
);
583 pvname (proc
->proc_name
, vp
->vers_num
);
584 f_print (fout
, ";\n");
585 f_print (fout
, "\t\tbreak;\n\n");
587 f_print (fout
, "\tdefault:\n");
588 printerr ("noproc", TRANSP
);
589 print_return ("\t\t");
590 f_print (fout
, "\t}\n");
592 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
593 printif ("getargs", TRANSP
, ARG
);
594 printerr ("decode", TRANSP
);
595 print_return ("\t\t");
596 f_print (fout
, "\t}\n");
601 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
602 RESULT
, ROUTINE
, ARG
, RQSTP
);
604 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
605 RESULT
, ROUTINE
, ARG
, RQSTP
);
609 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
610 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
612 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
613 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
616 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
617 RETVAL
, TRANSP
, RESULT
, RESULT
);
620 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
621 RESULT
, TRANSP
, RESULT
, RESULT
);
623 printerr ("systemerr", TRANSP
);
624 f_print (fout
, "\t}\n");
626 printif ("freeargs", TRANSP
, ARG
);
628 sprintf (_errbuf
, "unable to free arguments");
629 print_err_message ("\t\t");
630 f_print (fout
, "\t\texit (1);\n");
631 f_print (fout
, "\t}\n");
632 /* print out free routine */
635 f_print(fout
,"\tif (!");
636 pvname(def
->def_name
, vp
->vers_num
);
637 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
638 TRANSP
, RESULT
, RESULT
);
639 (void) sprintf(_errbuf
, "unable to free results");
640 print_err_message("\t\t");
644 f_print (fout
, "}\n");
649 printerr (const char *err
, const char *transp
)
651 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
655 printif (const char *proc
, const char *transp
, const char *arg
)
657 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
658 proc
, transp
, arg
, arg
);
662 nullproc (const proc_list
* proc
)
664 for (; proc
!= NULL
; proc
= proc
->next
)
666 if (streq (proc
->proc_num
, "0"))
675 write_inetmost (const char *infile
)
677 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
678 f_print (fout
, "\tint sock;\n");
679 f_print (fout
, "\tint proto;\n");
680 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
681 f_print (fout
, "\tint asize = sizeof (saddr);\n");
682 f_print (fout
, "\n");
684 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
685 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
686 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
687 f_print (fout
, "\t\t\texit (1);\n");
688 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
689 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
690 f_print (fout
, "\t\t\texit (1);\n");
691 f_print (fout
, "\t\tsock = 0;\n");
692 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
693 f_print (fout
, "\t\tproto = 0;\n");
694 open_log_file (infile
, "\t\t");
695 f_print (fout
, "\t} else {\n");
696 write_rpc_svc_fg (infile
, "\t\t");
697 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
698 print_pmapunset ("\t\t");
699 f_print (fout
, "\t}\n");
703 print_return (const char *space
)
706 f_print (fout
, "%sexit (0);\n", space
);
712 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
715 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
716 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
718 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
721 f_print (fout
, "%sreturn;\n", space
);
726 print_pmapunset (const char *space
)
732 for (l
= defined
; l
!= NULL
; l
= l
->next
)
734 def
= (definition
*) l
->val
;
735 if (def
->def_kind
== DEF_PROGRAM
)
737 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
740 f_print (fout
, "%spmap_unset (%s, %s);\n",
741 space
, def
->def_name
, vp
->vers_name
);
748 print_err_message (const char *space
)
751 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
752 else if (inetdflag
|| pmflag
)
753 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
755 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
759 * Write the server auxiliary function ( _msgout, timeout)
762 write_svc_aux (int nomain
)
767 write_timeout_func ();
771 * Write the _msgout function
777 f_print (fout
, "\n");
778 f_print (fout
, "static\n");
781 f_print (fout
, "void _msgout (msg)\n");
782 f_print (fout
, "\tchar *msg;\n");
786 f_print (fout
, "void _msgout (char* msg)\n");
788 f_print (fout
, "{\n");
789 f_print (fout
, "#ifdef RPC_SVC_FG\n");
790 if (inetdflag
|| pmflag
)
791 f_print (fout
, "\tif (_rpcpmstart)\n");
792 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
793 f_print (fout
, "\telse\n");
794 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
795 f_print (fout
, "#else\n");
796 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
797 f_print (fout
, "#endif\n");
798 f_print (fout
, "}\n");
802 * Write the timeout function
805 write_timeout_func (void)
809 f_print (fout
, "\n");
810 f_print (fout
, "static void\n");
812 f_print (fout
, "closedown (int sig)\n");
814 f_print (fout
, "closedown (sig)\n\tint sig;\n");
815 f_print (fout
, "{\n");
817 #if defined (__GNU_LIBRARY__) && 0
818 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
819 Cflag
? "(SIG_PF)" : "(void(*)())");
822 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
824 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
826 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
828 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
829 f_print (fout
, "\t\tstatic int size;\n");
830 f_print (fout
, "\t\tint i, openfd;\n");
831 if (tirpcflag
&& pmflag
)
833 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
834 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
838 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
840 f_print (fout
, "\t\t\texit (0);\n");
841 f_print (fout
, "\t\tif (size == 0) {\n");
844 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
845 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
846 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
847 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
849 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
850 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
854 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
856 f_print (fout
, "\t\t}\n");
857 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
858 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
859 f_print (fout
, "\t\t\t\topenfd++;\n");
860 f_print (fout
, "\t\tif (openfd <= 1)\n");
861 f_print (fout
, "\t\t\texit (0);\n");
862 f_print (fout
, "\t}\n");
863 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
864 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
866 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
867 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
868 Cflag
? "(SIG_PF)" : "(void(*)())");
869 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
870 f_print (fout
, "}\n");
874 * Write the most of port monitor support
877 write_pm_most (const char *infile
, int netflag
)
883 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
884 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
885 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
886 f_print (fout
, "\t\tchar *netid;\n");
888 { /* Not included by -n option */
889 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
890 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
893 f_print (fout
, "\t\tint pmclose;\n");
894 /* not necessary, defined in /usr/include/stdlib */
895 /* f_print(fout, "\t\textern char *getenv();\n"); */
896 f_print (fout
, "\n");
897 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
899 open_log_file (infile
, "\t\t");
900 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
901 sprintf (_errbuf
, "cannot get transport name");
902 print_err_message ("\t\t\t");
903 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
904 sprintf (_errbuf
, "cannot get transport info");
905 print_err_message ("\t\t\t");
906 f_print (fout
, "\t\t}\n");
908 * A kludgy support for inetd services. Inetd only works with
909 * sockmod, and RPC works only with timod, hence all this jugglery
911 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
912 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
913 sprintf (_errbuf
, "could not get the right module");
914 print_err_message ("\t\t\t\t");
915 f_print (fout
, "\t\t\t\texit(1);\n");
916 f_print (fout
, "\t\t\t}\n");
917 f_print (fout
, "\t\t}\n");
919 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
920 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
922 sprintf (_errbuf
, "cannot create server handle");
923 print_err_message ("\t\t\t");
924 f_print (fout
, "\t\t\texit(1);\n");
925 f_print (fout
, "\t\t}\n");
926 f_print (fout
, "\t\tif (nconf)\n");
927 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
928 for (l
= defined
; l
!= NULL
; l
= l
->next
)
930 def
= (definition
*) l
->val
;
931 if (def
->def_kind
!= DEF_PROGRAM
)
935 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
938 "\t\tif (!svc_reg(%s, %s, %s, ",
939 TRANSP
, def
->def_name
, vp
->vers_name
);
940 pvname (def
->def_name
, vp
->vers_num
);
941 f_print (fout
, ", 0)) {\n");
942 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
943 def
->def_name
, vp
->vers_name
);
944 print_err_message ("\t\t\t");
945 f_print (fout
, "\t\t\texit(1);\n");
946 f_print (fout
, "\t\t}\n");
951 f_print (fout
, "\t\tif (pmclose) {\n");
952 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
953 Cflag
? "(SIG_PF)" : "(void(*)())");
954 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
955 f_print (fout
, "\t\t}\n");
957 f_print (fout
, "\t\tsvc_run();\n");
958 f_print (fout
, "\t\texit(1);\n");
959 f_print (fout
, "\t\t/* NOTREACHED */\n");
960 f_print (fout
, "\t}\n");
964 * Support for backgrounding the server if self started.
967 write_rpc_svc_fg (const char *infile
, const char *sp
)
969 f_print (fout
, "#ifndef RPC_SVC_FG\n");
970 f_print (fout
, "%sint size;\n", sp
);
972 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
974 f_print (fout
, "%sint pid, i;\n\n", sp
);
975 f_print (fout
, "%spid = fork();\n", sp
);
976 f_print (fout
, "%sif (pid < 0) {\n", sp
);
977 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
978 f_print (fout
, "%s\texit(1);\n", sp
);
979 f_print (fout
, "%s}\n", sp
);
980 f_print (fout
, "%sif (pid)\n", sp
);
981 f_print (fout
, "%s\texit(0);\n", sp
);
982 /* get number of file descriptors */
985 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
986 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
987 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
988 f_print (fout
, "%s\texit(1);\n", sp
);
992 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
995 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
996 f_print (fout
, "%s\t(void) close(i);\n", sp
);
997 /* Redirect stderr and stdout to console */
998 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
999 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
1000 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
1001 /* This removes control of the controlling terminal */
1003 f_print (fout
, "%ssetsid();\n", sp
);
1006 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1007 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1008 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1009 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1010 f_print (fout
, "%s}\n", sp
);
1013 open_log_file (infile
, sp
);
1014 f_print (fout
, "#endif\n");
1016 open_log_file (infile
, sp
);
1020 open_log_file (const char *infile
, const char *sp
)
1024 s
= strrchr (infile
, '.');
1027 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1033 * write a registration for the given transport for Inetd
1036 write_inetd_register (const char *transp
)
1049 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1053 f_print (fout
, "\n");
1056 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1057 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1059 f_print (fout
, "%s\t%s = svc%s_create(%s",
1060 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1062 f_print (fout
, ", 0, 0");
1063 f_print (fout
, ");\n");
1064 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1065 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1066 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1067 print_err_message (tmpbuf
);
1068 f_print (fout
, "%s\t\texit(1);\n", sp
);
1069 f_print (fout
, "%s\t}\n", sp
);
1073 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1074 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1075 sp
, isudp
? "UDP" : "TCP");
1077 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1079 def
= (definition
*) l
->val
;
1080 if (def
->def_kind
!= DEF_PROGRAM
)
1084 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1086 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1087 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1088 pvname (def
->def_name
, vp
->vers_num
);
1090 f_print (fout
, ", proto)) {\n");
1092 f_print (fout
, ", IPPROTO_%s)) {\n",
1093 isudp
? "UDP" : "TCP");
1094 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1095 def
->def_name
, vp
->vers_name
, transp
);
1096 print_err_message (tmpbuf
);
1097 f_print (fout
, "%s\t\texit(1);\n", sp
);
1098 f_print (fout
, "%s\t}\n", sp
);
1102 f_print (fout
, "\t}\n");