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
38 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
42 #include "rpc_parse.h"
46 static const char RQSTP
[] = "rqstp";
47 static const char TRANSP
[] = "transp";
48 static const char ARG
[] = "argument";
49 static const char RESULT
[] = "result";
50 static const char ROUTINE
[] = "local";
51 static char RETVAL
[] = "retval";
53 char _errbuf
[256]; /* For all messages */
55 static void internal_proctype (const proc_list
* plist
);
56 static void p_xdrfunc (const char *rname
, const char *typename
);
57 static void write_real_program (const definition
* def
);
58 static void write_program (const definition
* def
, const char *storage
);
59 static void printerr (const char *err
, const char *transp
);
60 static void printif (const char *proc
, const char *transp
, const char *arg
);
61 static void write_inetmost (const char *infile
);
62 static void print_return (const char *space
);
63 static void print_pmapunset (const char *space
);
64 static void print_err_message (const char *space
);
65 static void write_timeout_func (void);
66 static void write_pm_most (const char *infile
, int netflag
);
67 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
68 static void open_log_file (const char *infile
, const char *sp
);
71 p_xdrfunc (const char *rname
, const char *typename
)
74 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
75 stringfix (typename
));
77 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
81 internal_proctype (const proc_list
* plist
)
83 f_print (fout
, "static ");
84 ptype (plist
->res_prefix
, plist
->res_type
, 1);
90 * write most of the service, that is, everything but the registrations.
93 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
95 if (inetdflag
|| pmflag
)
98 #ifdef __GNU_LIBRARY__
100 var_type
= (nomain
? "extern" : "");
102 var_type
= (nomain
? "extern" : "static");
104 f_print (fout
, "%s int _rpcpmstart;", var_type
);
105 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
108 f_print (fout
, "%s int _rpcfdtype;", var_type
);
109 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
114 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
115 f_print (fout
, "\t/* Still serving ? */\n");
117 f_print(fout
, " /* States a server can be in wrt request */\n\n");
118 f_print(fout
, "#define\t_IDLE 0\n");
119 f_print(fout
, "#define\t_SERVED 1\n");
120 f_print(fout
, "#define\t_SERVING 2\n\n");
121 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
122 f_print(fout
, "\t /* Set when a request is serviced */\n");
126 f_print (fout
, "mutex_t _svcstate_lock;");
128 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
132 write_svc_aux (nomain
);
134 /* write out dispatcher and stubs */
135 write_programs (nomain
? NULL
: "static");
140 #ifdef __GNU_LIBRARY__
142 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
145 f_print (fout
, "\nint\nmain (argc, argv)\n");
146 f_print (fout
, "\tint argc;\n");
147 f_print (fout
, "\tchar **argv;\n");
150 f_print (fout
, "\nmain()\n");
152 f_print (fout
, "{\n");
155 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
163 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
164 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
166 f_print (fout
, "\tpid_t pid;\n");
167 f_print (fout
, "\tint i;\n");
168 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
170 if (mtflag
& timerflag
)
172 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
174 write_pm_most (infile
, netflag
);
175 f_print (fout
, "\telse {\n");
176 write_rpc_svc_fg (infile
, "\t\t");
177 f_print (fout
, "\t}\n");
181 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
182 f_print (fout
, "\n");
183 print_pmapunset ("\t");
187 if (logflag
&& !inetdflag
)
189 open_log_file (infile
, "\t");
194 * write a registration for the given transport
197 write_netid_register (const char *transp
)
206 f_print (fout
, "\n");
207 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
208 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
209 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
210 sprintf (tmpbuf
, "%s\t\t", sp
);
211 print_err_message (tmpbuf
);
212 f_print (fout
, "%s\t\texit (1);\n", sp
);
213 f_print (fout
, "%s\t}\n", sp
);
214 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
215 sp
, TRANSP
/*, transp *//* ?!?... */ );
216 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
217 sprintf (_errbuf
, "cannot create %s service.", transp
);
218 print_err_message (tmpbuf
);
219 f_print (fout
, "%s\t\texit (1);\n", sp
);
220 f_print (fout
, "%s\t}\n", sp
);
222 for (l
= defined
; l
!= NULL
; l
= l
->next
)
224 def
= (definition
*) l
->val
;
225 if (def
->def_kind
!= DEF_PROGRAM
)
229 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
231 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
232 sp
, def
->def_name
, vp
->vers_name
);
233 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
234 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
235 pvname (def
->def_name
, vp
->vers_num
);
236 f_print (fout
, ", nconf)) {\n");
237 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
238 def
->def_name
, vp
->vers_name
, transp
);
239 print_err_message (tmpbuf
);
240 f_print (fout
, "%s\t\texit (1);\n", sp
);
241 f_print (fout
, "%s\t}\n", sp
);
244 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
248 * write a registration for the given transport for TLI
251 write_nettype_register (const char *transp
)
257 for (l
= defined
; l
!= NULL
; l
= l
->next
)
259 def
= (definition
*) l
->val
;
260 if (def
->def_kind
!= DEF_PROGRAM
)
264 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
266 f_print (fout
, "\tif (!svc_create (");
267 pvname (def
->def_name
, vp
->vers_num
);
268 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
269 def
->def_name
, vp
->vers_name
, transp
);
270 (void) sprintf (_errbuf
,
271 "unable to create (%s, %s) for %s.",
272 def
->def_name
, vp
->vers_name
, transp
);
273 print_err_message ("\t\t");
274 f_print (fout
, "\t\texit (1);\n");
275 f_print (fout
, "\t}\n");
281 * write the rest of the service
286 f_print (fout
, "\n");
289 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
290 (void) sprintf (_errbuf
, "could not create a handle");
291 print_err_message ("\t\t");
292 f_print (fout
, "\t\texit (1);\n");
293 f_print (fout
, "\t}\n");
296 f_print (fout
, "\tif (_rpcpmstart) {\n");
298 "\t\t(void) signal (SIGALRM, %s closedown);\n",
299 Cflag
? "(SIG_PF)" : "(void(*)())");
300 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
301 f_print (fout
, "\t}\n");
304 f_print (fout
, "\tsvc_run ();\n");
305 (void) sprintf (_errbuf
, "svc_run returned");
306 print_err_message ("\t");
307 f_print (fout
, "\texit (1);\n");
308 f_print (fout
, "\t/* NOTREACHED */\n");
309 f_print (fout
, "}\n");
313 write_programs (const char *storage
)
318 /* write out stubs for procedure definitions */
319 for (l
= defined
; l
!= NULL
; l
= l
->next
)
321 def
= (definition
*) l
->val
;
322 if (def
->def_kind
== DEF_PROGRAM
)
324 write_real_program (def
);
328 /* write out dispatcher for each program */
329 for (l
= defined
; l
!= NULL
; l
= l
->next
)
331 def
= (definition
*) l
->val
;
332 if (def
->def_kind
== DEF_PROGRAM
)
334 write_program (def
, storage
);
339 /* write out definition of internal function (e.g. _printmsg_1(...))
340 which calls server's defintion of actual function (e.g. printmsg_1(...)).
341 Unpacks single user argument of printmsg_1 to call-by-value format
342 expected by printmsg_1. */
344 write_real_program (const definition
* def
)
351 return; /* not needed for old style */
352 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
354 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
356 fprintf (fout
, "\n");
358 internal_proctype (proc
);
360 f_print (fout
, "int");
361 f_print (fout
, "\n_");
362 pvname (proc
->proc_name
, vp
->vers_num
);
365 f_print (fout
, " (");
367 if (proc
->arg_num
> 1)
368 f_print (fout
, proc
->args
.argname
);
370 ptype (proc
->args
.decls
->decl
.prefix
,
371 proc
->args
.decls
->decl
.type
, 0);
374 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
378 f_print (fout
, " *argp, struct svc_req *%s)\n",
384 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
386 f_print (fout
, " (argp, %s)\n", RQSTP
);
388 if (proc
->arg_num
> 1)
389 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
392 f_print (fout
, "\t");
393 ptype (proc
->args
.decls
->decl
.prefix
,
394 proc
->args
.decls
->decl
.type
, 0);
395 f_print (fout
, " *argp;\n");
397 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
400 f_print (fout
, "{\n");
401 f_print (fout
, "\treturn (");
403 pvname_svc (proc
->proc_name
, vp
->vers_num
);
405 pvname (proc
->proc_name
, vp
->vers_num
);
407 if (proc
->arg_num
< 2)
408 { /* single argument */
409 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
410 f_print (fout
, "*argp, "); /* non-void */
414 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
415 f_print (fout
, "argp->%s, ", l
->decl
.name
);
418 f_print (fout
, "%s, ", RESULT
);
419 f_print (fout
, "%s));\n}\n", RQSTP
);
425 write_program (const definition
* def
, const char *storage
)
431 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
433 f_print (fout
, "\n");
436 f_print (fout
, "%s ", storage
);
438 f_print (fout
, "void\n");
439 pvname (def
->def_name
, vp
->vers_num
);
443 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
444 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
448 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
449 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
450 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
453 f_print (fout
, "{\n");
456 f_print (fout
, "\tunion {\n");
457 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
459 if (proc
->arg_num
< 2)
460 { /* single argument */
461 if (streq (proc
->args
.decls
->decl
.type
,
467 f_print (fout
, "\t\t");
468 ptype (proc
->args
.decls
->decl
.prefix
,
469 proc
->args
.decls
->decl
.type
, 0);
470 pvname (proc
->proc_name
, vp
->vers_num
);
471 f_print (fout
, "_arg;\n");
477 f_print (fout
, "\t\t%s", proc
->args
.argname
);
479 pvname (proc
->proc_name
, vp
->vers_num
);
480 f_print (fout
, "_arg;\n");
485 f_print (fout
, "\t\tint fill;\n");
487 f_print (fout
, "\t} %s;\n", ARG
);
490 f_print(fout
, "\tunion {\n");
491 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
492 if (!streq (proc
->res_type
, "void"))
494 f_print(fout
, "\t\t");
495 ptype(proc
->res_prefix
, proc
->res_type
, 0);
496 pvname(proc
->proc_name
, vp
->vers_num
);
497 f_print(fout
, "_res;\n");
499 f_print(fout
, "\t} %s;\n", RESULT
);
500 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
503 f_print (fout
, "\tchar *%s;\n", RESULT
);
507 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
510 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
513 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
518 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
520 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
522 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
524 f_print (fout
, "\n");
528 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
532 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
533 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
535 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
539 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
540 if (!nullproc (vp
->procs
))
542 f_print (fout
, "\tcase NULLPROC:\n");
544 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
546 print_return ("\t\t");
547 f_print (fout
, "\n");
549 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
551 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
552 if (proc
->arg_num
< 2)
553 { /* single argument */
554 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
558 p_xdrfunc (ARG
, proc
->args
.argname
);
560 p_xdrfunc (RESULT
, proc
->res_type
);
565 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
569 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
574 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
576 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
579 { /* new style: calls internal routine */
582 if ((Cflag
|| mtflag
) && !newstyle
)
583 pvname_svc (proc
->proc_name
, vp
->vers_num
);
585 pvname (proc
->proc_name
, vp
->vers_num
);
586 f_print (fout
, ";\n");
587 f_print (fout
, "\t\tbreak;\n\n");
589 f_print (fout
, "\tdefault:\n");
590 printerr ("noproc", TRANSP
);
591 print_return ("\t\t");
592 f_print (fout
, "\t}\n");
594 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
595 printif ("getargs", TRANSP
, ARG
);
596 printerr ("decode", TRANSP
);
597 print_return ("\t\t");
598 f_print (fout
, "\t}\n");
603 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
604 RESULT
, ROUTINE
, ARG
, RQSTP
);
606 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
607 RESULT
, ROUTINE
, ARG
, RQSTP
);
611 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
612 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
614 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
615 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
618 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
619 RETVAL
, TRANSP
, RESULT
, RESULT
);
622 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
623 RESULT
, TRANSP
, RESULT
, RESULT
);
625 printerr ("systemerr", TRANSP
);
626 f_print (fout
, "\t}\n");
628 printif ("freeargs", TRANSP
, ARG
);
630 sprintf (_errbuf
, "unable to free arguments");
631 print_err_message ("\t\t");
632 f_print (fout
, "\t\texit (1);\n");
633 f_print (fout
, "\t}\n");
634 /* print out free routine */
637 f_print(fout
,"\tif (!");
638 pvname(def
->def_name
, vp
->vers_num
);
639 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
640 TRANSP
, RESULT
, RESULT
);
641 (void) sprintf(_errbuf
, "unable to free results");
642 print_err_message("\t\t");
646 f_print (fout
, "}\n");
651 printerr (const char *err
, const char *transp
)
653 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
657 printif (const char *proc
, const char *transp
, const char *arg
)
659 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
660 proc
, transp
, arg
, arg
);
664 nullproc (const proc_list
* proc
)
666 for (; proc
!= NULL
; proc
= proc
->next
)
668 if (streq (proc
->proc_num
, "0"))
677 write_inetmost (const char *infile
)
679 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
680 f_print (fout
, "\tint sock;\n");
681 f_print (fout
, "\tint proto;\n");
682 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
683 f_print (fout
, "\tint asize = sizeof (saddr);\n");
684 f_print (fout
, "\n");
686 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
687 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
688 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
689 f_print (fout
, "\t\t\texit (1);\n");
690 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
691 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
692 f_print (fout
, "\t\t\texit (1);\n");
693 f_print (fout
, "\t\tsock = 0;\n");
694 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
695 f_print (fout
, "\t\tproto = 0;\n");
696 open_log_file (infile
, "\t\t");
697 f_print (fout
, "\t} else {\n");
698 write_rpc_svc_fg (infile
, "\t\t");
699 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
700 print_pmapunset ("\t\t");
701 f_print (fout
, "\t}\n");
705 print_return (const char *space
)
708 f_print (fout
, "%sexit (0);\n", space
);
714 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
717 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
718 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
720 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
723 f_print (fout
, "%sreturn;\n", space
);
728 print_pmapunset (const char *space
)
734 for (l
= defined
; l
!= NULL
; l
= l
->next
)
736 def
= (definition
*) l
->val
;
737 if (def
->def_kind
== DEF_PROGRAM
)
739 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
742 f_print (fout
, "%spmap_unset (%s, %s);\n",
743 space
, def
->def_name
, vp
->vers_name
);
750 print_err_message (const char *space
)
753 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
754 else if (inetdflag
|| pmflag
)
755 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
757 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
761 * Write the server auxiliary function ( _msgout, timeout)
764 write_svc_aux (int nomain
)
769 write_timeout_func ();
773 * Write the _msgout function
779 f_print (fout
, "\n");
780 f_print (fout
, "static\n");
783 f_print (fout
, "void _msgout (msg)\n");
784 f_print (fout
, "\tchar *msg;\n");
788 f_print (fout
, "void _msgout (char* msg)\n");
790 f_print (fout
, "{\n");
791 f_print (fout
, "#ifdef RPC_SVC_FG\n");
792 if (inetdflag
|| pmflag
)
793 f_print (fout
, "\tif (_rpcpmstart)\n");
794 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
795 f_print (fout
, "\telse\n");
796 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
797 f_print (fout
, "#else\n");
798 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
799 f_print (fout
, "#endif\n");
800 f_print (fout
, "}\n");
804 * Write the timeout function
807 write_timeout_func (void)
811 f_print (fout
, "\n");
812 f_print (fout
, "static void\n");
814 f_print (fout
, "closedown (int sig)\n");
816 f_print (fout
, "closedown (sig)\n\tint sig;\n");
817 f_print (fout
, "{\n");
819 #if defined (__GNU_LIBRARY__) && 0
820 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
821 Cflag
? "(SIG_PF)" : "(void(*)())");
824 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
826 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
828 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
830 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
831 f_print (fout
, "\t\tstatic int size;\n");
832 f_print (fout
, "\t\tint i, openfd;\n");
833 if (tirpcflag
&& pmflag
)
835 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
836 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
840 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
842 f_print (fout
, "\t\t\texit (0);\n");
843 f_print (fout
, "\t\tif (size == 0) {\n");
846 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
847 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
848 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
849 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
851 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
852 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
856 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
858 f_print (fout
, "\t\t}\n");
859 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
860 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
861 f_print (fout
, "\t\t\t\topenfd++;\n");
862 f_print (fout
, "\t\tif (openfd <= 1)\n");
863 f_print (fout
, "\t\t\texit (0);\n");
864 f_print (fout
, "\t}\n");
865 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
866 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
868 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
869 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
870 Cflag
? "(SIG_PF)" : "(void(*)())");
871 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
872 f_print (fout
, "}\n");
876 * Write the most of port monitor support
879 write_pm_most (const char *infile
, int netflag
)
885 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
886 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
887 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
888 f_print (fout
, "\t\tchar *netid;\n");
890 { /* Not included by -n option */
891 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
892 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
895 f_print (fout
, "\t\tint pmclose;\n");
896 /* not necessary, defined in /usr/include/stdlib */
897 /* f_print(fout, "\t\textern char *getenv();\n"); */
898 f_print (fout
, "\n");
899 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
901 open_log_file (infile
, "\t\t");
902 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
903 sprintf (_errbuf
, "cannot get transport name");
904 print_err_message ("\t\t\t");
905 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
906 sprintf (_errbuf
, "cannot get transport info");
907 print_err_message ("\t\t\t");
908 f_print (fout
, "\t\t}\n");
910 * A kludgy support for inetd services. Inetd only works with
911 * sockmod, and RPC works only with timod, hence all this jugglery
913 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
914 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
915 sprintf (_errbuf
, "could not get the right module");
916 print_err_message ("\t\t\t\t");
917 f_print (fout
, "\t\t\t\texit(1);\n");
918 f_print (fout
, "\t\t\t}\n");
919 f_print (fout
, "\t\t}\n");
921 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
922 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
924 sprintf (_errbuf
, "cannot create server handle");
925 print_err_message ("\t\t\t");
926 f_print (fout
, "\t\t\texit(1);\n");
927 f_print (fout
, "\t\t}\n");
928 f_print (fout
, "\t\tif (nconf)\n");
929 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
930 for (l
= defined
; l
!= NULL
; l
= l
->next
)
932 def
= (definition
*) l
->val
;
933 if (def
->def_kind
!= DEF_PROGRAM
)
937 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
940 "\t\tif (!svc_reg(%s, %s, %s, ",
941 TRANSP
, def
->def_name
, vp
->vers_name
);
942 pvname (def
->def_name
, vp
->vers_num
);
943 f_print (fout
, ", 0)) {\n");
944 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
945 def
->def_name
, vp
->vers_name
);
946 print_err_message ("\t\t\t");
947 f_print (fout
, "\t\t\texit(1);\n");
948 f_print (fout
, "\t\t}\n");
953 f_print (fout
, "\t\tif (pmclose) {\n");
954 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
955 Cflag
? "(SIG_PF)" : "(void(*)())");
956 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
957 f_print (fout
, "\t\t}\n");
959 f_print (fout
, "\t\tsvc_run();\n");
960 f_print (fout
, "\t\texit(1);\n");
961 f_print (fout
, "\t\t/* NOTREACHED */\n");
962 f_print (fout
, "\t}\n");
966 * Support for backgrounding the server if self started.
969 write_rpc_svc_fg (const char *infile
, const char *sp
)
971 f_print (fout
, "#ifndef RPC_SVC_FG\n");
972 f_print (fout
, "%sint size;\n", sp
);
974 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
976 f_print (fout
, "%sint pid, i;\n\n", sp
);
977 f_print (fout
, "%spid = fork();\n", sp
);
978 f_print (fout
, "%sif (pid < 0) {\n", sp
);
979 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
980 f_print (fout
, "%s\texit(1);\n", sp
);
981 f_print (fout
, "%s}\n", sp
);
982 f_print (fout
, "%sif (pid)\n", sp
);
983 f_print (fout
, "%s\texit(0);\n", sp
);
984 /* get number of file descriptors */
987 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
988 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
989 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
990 f_print (fout
, "%s\texit(1);\n", sp
);
994 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
997 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
998 f_print (fout
, "%s\t(void) close(i);\n", sp
);
999 /* Redirect stderr and stdout to console */
1000 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
1001 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
1002 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
1003 /* This removes control of the controlling terminal */
1005 f_print (fout
, "%ssetsid();\n", sp
);
1008 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1009 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1010 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1011 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1012 f_print (fout
, "%s}\n", sp
);
1015 open_log_file (infile
, sp
);
1016 f_print (fout
, "#endif\n");
1018 open_log_file (infile
, sp
);
1022 open_log_file (const char *infile
, const char *sp
)
1026 s
= strrchr (infile
, '.');
1029 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1035 * write a registration for the given transport for Inetd
1038 write_inetd_register (const char *transp
)
1051 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1055 f_print (fout
, "\n");
1058 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1059 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1061 f_print (fout
, "%s\t%s = svc%s_create(%s",
1062 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1064 f_print (fout
, ", 0, 0");
1065 f_print (fout
, ");\n");
1066 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1067 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1068 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1069 print_err_message (tmpbuf
);
1070 f_print (fout
, "%s\t\texit(1);\n", sp
);
1071 f_print (fout
, "%s\t}\n", sp
);
1075 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1076 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1077 sp
, isudp
? "UDP" : "TCP");
1079 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1081 def
= (definition
*) l
->val
;
1082 if (def
->def_kind
!= DEF_PROGRAM
)
1086 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1088 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1089 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1090 pvname (def
->def_name
, vp
->vers_num
);
1092 f_print (fout
, ", proto)) {\n");
1094 f_print (fout
, ", IPPROTO_%s)) {\n",
1095 isudp
? "UDP" : "TCP");
1096 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1097 def
->def_name
, vp
->vers_name
, transp
);
1098 print_err_message (tmpbuf
);
1099 f_print (fout
, "%s\t\texit(1);\n", sp
);
1100 f_print (fout
, "%s\t}\n", sp
);
1104 f_print (fout
, "\t}\n");