2 * From: @(#)rpc_svcout.c 1.29 89/03/30
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
38 #include "rpc_parse.h"
42 static const char RQSTP
[] = "rqstp";
43 static const char TRANSP
[] = "transp";
44 static const char ARG
[] = "argument";
45 static const char RESULT
[] = "result";
46 static const char ROUTINE
[] = "local";
47 static char RETVAL
[] = "retval";
49 char _errbuf
[256]; /* For all messages */
51 static void internal_proctype (const proc_list
* plist
);
52 static void p_xdrfunc (const char *rname
, const char *typename
);
53 static void write_real_program (const definition
* def
);
54 static void write_program (const definition
* def
, const char *storage
);
55 static void printerr (const char *err
, const char *transp
);
56 static void printif (const char *proc
, const char *transp
, const char *arg
);
57 static void write_inetmost (const char *infile
);
58 static void print_return (const char *space
);
59 static void print_pmapunset (const char *space
);
60 static void print_err_message (const char *space
);
61 static void write_timeout_func (void);
62 static void write_pm_most (const char *infile
, int netflag
);
63 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
64 static void open_log_file (const char *infile
, const char *sp
);
67 p_xdrfunc (const char *rname
, const char *typename
)
70 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
71 stringfix (typename
));
73 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
77 internal_proctype (const proc_list
* plist
)
79 f_print (fout
, "static ");
80 ptype (plist
->res_prefix
, plist
->res_type
, 1);
86 * write most of the service, that is, everything but the registrations.
89 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
91 if (inetdflag
|| pmflag
)
94 #ifdef __GNU_LIBRARY__
96 var_type
= (nomain
? "extern" : "");
98 var_type
= (nomain
? "extern" : "static");
100 f_print (fout
, "%s int _rpcpmstart;", var_type
);
101 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
104 f_print (fout
, "%s int _rpcfdtype;", var_type
);
105 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
110 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
111 f_print (fout
, "\t/* Still serving ? */\n");
113 f_print(fout
, " /* States a server can be in wrt request */\n\n");
114 f_print(fout
, "#define\t_IDLE 0\n");
115 f_print(fout
, "#define\t_SERVED 1\n");
116 f_print(fout
, "#define\t_SERVING 2\n\n");
117 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
118 f_print(fout
, "\t /* Set when a request is serviced */\n");
122 f_print (fout
, "mutex_t _svcstate_lock;");
124 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
128 write_svc_aux (nomain
);
130 /* write out dispatcher and stubs */
131 write_programs (nomain
? NULL
: "static");
136 #ifdef __GNU_LIBRARY__
138 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
141 f_print (fout
, "\nint\nmain (argc, argv)\n");
142 f_print (fout
, "\tint argc;\n");
143 f_print (fout
, "\tchar **argv;\n");
146 f_print (fout
, "\nmain()\n");
148 f_print (fout
, "{\n");
151 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
159 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
160 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
162 f_print (fout
, "\tpid_t pid;\n");
163 f_print (fout
, "\tint i;\n");
164 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
166 if (mtflag
& timerflag
)
168 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
170 write_pm_most (infile
, netflag
);
171 f_print (fout
, "\telse {\n");
172 write_rpc_svc_fg (infile
, "\t\t");
173 f_print (fout
, "\t}\n");
177 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
178 f_print (fout
, "\n");
179 print_pmapunset ("\t");
183 if (logflag
&& !inetdflag
)
185 open_log_file (infile
, "\t");
190 * write a registration for the given transport
193 write_netid_register (const char *transp
)
202 f_print (fout
, "\n");
203 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
204 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
205 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
206 sprintf (tmpbuf
, "%s\t\t", sp
);
207 print_err_message (tmpbuf
);
208 f_print (fout
, "%s\t\texit (1);\n", sp
);
209 f_print (fout
, "%s\t}\n", sp
);
210 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
211 sp
, TRANSP
/*, transp *//* ?!?... */ );
212 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
213 sprintf (_errbuf
, "cannot create %s service.", transp
);
214 print_err_message (tmpbuf
);
215 f_print (fout
, "%s\t\texit (1);\n", sp
);
216 f_print (fout
, "%s\t}\n", sp
);
218 for (l
= defined
; l
!= NULL
; l
= l
->next
)
220 def
= (definition
*) l
->val
;
221 if (def
->def_kind
!= DEF_PROGRAM
)
225 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
227 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
228 sp
, def
->def_name
, vp
->vers_name
);
229 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
230 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
231 pvname (def
->def_name
, vp
->vers_num
);
232 f_print (fout
, ", nconf)) {\n");
233 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
234 def
->def_name
, vp
->vers_name
, transp
);
235 print_err_message (tmpbuf
);
236 f_print (fout
, "%s\t\texit (1);\n", sp
);
237 f_print (fout
, "%s\t}\n", sp
);
240 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
244 * write a registration for the given transport for TLI
247 write_nettype_register (const char *transp
)
253 for (l
= defined
; l
!= NULL
; l
= l
->next
)
255 def
= (definition
*) l
->val
;
256 if (def
->def_kind
!= DEF_PROGRAM
)
260 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
262 f_print (fout
, "\tif (!svc_create (");
263 pvname (def
->def_name
, vp
->vers_num
);
264 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
265 def
->def_name
, vp
->vers_name
, transp
);
266 (void) sprintf (_errbuf
,
267 "unable to create (%s, %s) for %s.",
268 def
->def_name
, vp
->vers_name
, transp
);
269 print_err_message ("\t\t");
270 f_print (fout
, "\t\texit (1);\n");
271 f_print (fout
, "\t}\n");
277 * write the rest of the service
282 f_print (fout
, "\n");
285 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
286 (void) sprintf (_errbuf
, "could not create a handle");
287 print_err_message ("\t\t");
288 f_print (fout
, "\t\texit (1);\n");
289 f_print (fout
, "\t}\n");
292 f_print (fout
, "\tif (_rpcpmstart) {\n");
294 "\t\t(void) signal (SIGALRM, %s closedown);\n",
295 Cflag
? "(SIG_PF)" : "(void(*)())");
296 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
297 f_print (fout
, "\t}\n");
300 f_print (fout
, "\tsvc_run ();\n");
301 (void) sprintf (_errbuf
, "svc_run returned");
302 print_err_message ("\t");
303 f_print (fout
, "\texit (1);\n");
304 f_print (fout
, "\t/* NOTREACHED */\n");
305 f_print (fout
, "}\n");
309 write_programs (const char *storage
)
314 /* write out stubs for procedure definitions */
315 for (l
= defined
; l
!= NULL
; l
= l
->next
)
317 def
= (definition
*) l
->val
;
318 if (def
->def_kind
== DEF_PROGRAM
)
320 write_real_program (def
);
324 /* write out dispatcher for each program */
325 for (l
= defined
; l
!= NULL
; l
= l
->next
)
327 def
= (definition
*) l
->val
;
328 if (def
->def_kind
== DEF_PROGRAM
)
330 write_program (def
, storage
);
335 /* write out definition of internal function (e.g. _printmsg_1(...))
336 which calls server's defintion of actual function (e.g. printmsg_1(...)).
337 Unpacks single user argument of printmsg_1 to call-by-value format
338 expected by printmsg_1. */
340 write_real_program (const definition
* def
)
347 return; /* not needed for old style */
348 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
350 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
352 fprintf (fout
, "\n");
354 internal_proctype (proc
);
356 f_print (fout
, "int");
357 f_print (fout
, "\n_");
358 pvname (proc
->proc_name
, vp
->vers_num
);
361 f_print (fout
, " (");
363 if (proc
->arg_num
> 1)
364 f_print (fout
, proc
->args
.argname
);
366 ptype (proc
->args
.decls
->decl
.prefix
,
367 proc
->args
.decls
->decl
.type
, 0);
370 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
374 f_print (fout
, " *argp, struct svc_req *%s)\n",
380 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
382 f_print (fout
, " (argp, %s)\n", RQSTP
);
384 if (proc
->arg_num
> 1)
385 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
388 f_print (fout
, "\t");
389 ptype (proc
->args
.decls
->decl
.prefix
,
390 proc
->args
.decls
->decl
.type
, 0);
391 f_print (fout
, " *argp;\n");
393 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
396 f_print (fout
, "{\n");
397 f_print (fout
, "\treturn (");
399 pvname_svc (proc
->proc_name
, vp
->vers_num
);
401 pvname (proc
->proc_name
, vp
->vers_num
);
403 if (proc
->arg_num
< 2)
404 { /* single argument */
405 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
406 f_print (fout
, "*argp, "); /* non-void */
410 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
411 f_print (fout
, "argp->%s, ", l
->decl
.name
);
414 f_print (fout
, "%s, ", RESULT
);
415 f_print (fout
, "%s));\n}\n", RQSTP
);
421 write_program (const definition
* def
, const char *storage
)
427 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
429 f_print (fout
, "\n");
432 f_print (fout
, "%s ", storage
);
434 f_print (fout
, "void\n");
435 pvname (def
->def_name
, vp
->vers_num
);
439 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
440 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
444 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
445 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
446 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
449 f_print (fout
, "{\n");
452 f_print (fout
, "\tunion {\n");
453 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
455 if (proc
->arg_num
< 2)
456 { /* single argument */
457 if (streq (proc
->args
.decls
->decl
.type
,
463 f_print (fout
, "\t\t");
464 ptype (proc
->args
.decls
->decl
.prefix
,
465 proc
->args
.decls
->decl
.type
, 0);
466 pvname (proc
->proc_name
, vp
->vers_num
);
467 f_print (fout
, "_arg;\n");
473 f_print (fout
, "\t\t%s", proc
->args
.argname
);
475 pvname (proc
->proc_name
, vp
->vers_num
);
476 f_print (fout
, "_arg;\n");
481 f_print (fout
, "\t\tint fill;\n");
483 f_print (fout
, "\t} %s;\n", ARG
);
486 f_print(fout
, "\tunion {\n");
487 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
488 if (!streq (proc
->res_type
, "void"))
490 f_print(fout
, "\t\t");
491 ptype(proc
->res_prefix
, proc
->res_type
, 0);
492 pvname(proc
->proc_name
, vp
->vers_num
);
493 f_print(fout
, "_res;\n");
495 f_print(fout
, "\t} %s;\n", RESULT
);
496 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
499 f_print (fout
, "\tchar *%s;\n", RESULT
);
503 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
506 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
509 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
514 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
516 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
518 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
520 f_print (fout
, "\n");
524 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
528 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
529 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
531 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
535 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
536 if (!nullproc (vp
->procs
))
538 f_print (fout
, "\tcase NULLPROC:\n");
540 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
542 print_return ("\t\t");
543 f_print (fout
, "\n");
545 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
547 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
548 if (proc
->arg_num
< 2)
549 { /* single argument */
550 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
554 p_xdrfunc (ARG
, proc
->args
.argname
);
556 p_xdrfunc (RESULT
, proc
->res_type
);
561 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
565 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
570 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
572 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
575 { /* new style: calls internal routine */
578 if ((Cflag
|| mtflag
) && !newstyle
)
579 pvname_svc (proc
->proc_name
, vp
->vers_num
);
581 pvname (proc
->proc_name
, vp
->vers_num
);
582 f_print (fout
, ";\n");
583 f_print (fout
, "\t\tbreak;\n\n");
585 f_print (fout
, "\tdefault:\n");
586 printerr ("noproc", TRANSP
);
587 print_return ("\t\t");
588 f_print (fout
, "\t}\n");
590 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
591 printif ("getargs", TRANSP
, ARG
);
592 printerr ("decode", TRANSP
);
593 print_return ("\t\t");
594 f_print (fout
, "\t}\n");
599 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
600 RESULT
, ROUTINE
, ARG
, RQSTP
);
602 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
603 RESULT
, ROUTINE
, ARG
, RQSTP
);
607 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
608 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
610 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
611 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
614 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
615 RETVAL
, TRANSP
, RESULT
, RESULT
);
618 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
619 RESULT
, TRANSP
, RESULT
, RESULT
);
621 printerr ("systemerr", TRANSP
);
622 f_print (fout
, "\t}\n");
624 printif ("freeargs", TRANSP
, ARG
);
626 sprintf (_errbuf
, "unable to free arguments");
627 print_err_message ("\t\t");
628 f_print (fout
, "\t\texit (1);\n");
629 f_print (fout
, "\t}\n");
630 /* print out free routine */
633 f_print(fout
,"\tif (!");
634 pvname(def
->def_name
, vp
->vers_num
);
635 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
636 TRANSP
, RESULT
, RESULT
);
637 (void) sprintf(_errbuf
, "unable to free results");
638 print_err_message("\t\t");
642 f_print (fout
, "}\n");
647 printerr (const char *err
, const char *transp
)
649 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
653 printif (const char *proc
, const char *transp
, const char *arg
)
655 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
656 proc
, transp
, arg
, arg
);
660 nullproc (const proc_list
* proc
)
662 for (; proc
!= NULL
; proc
= proc
->next
)
664 if (streq (proc
->proc_num
, "0"))
673 write_inetmost (const char *infile
)
675 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
676 f_print (fout
, "\tint sock;\n");
677 f_print (fout
, "\tint proto;\n");
678 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
679 f_print (fout
, "\tint asize = sizeof (saddr);\n");
680 f_print (fout
, "\n");
682 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
683 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
684 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
685 f_print (fout
, "\t\t\texit (1);\n");
686 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
687 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
688 f_print (fout
, "\t\t\texit (1);\n");
689 f_print (fout
, "\t\tsock = 0;\n");
690 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
691 f_print (fout
, "\t\tproto = 0;\n");
692 open_log_file (infile
, "\t\t");
693 f_print (fout
, "\t} else {\n");
694 write_rpc_svc_fg (infile
, "\t\t");
695 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
696 print_pmapunset ("\t\t");
697 f_print (fout
, "\t}\n");
701 print_return (const char *space
)
704 f_print (fout
, "%sexit (0);\n", space
);
710 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
713 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
714 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
716 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
719 f_print (fout
, "%sreturn;\n", space
);
724 print_pmapunset (const char *space
)
730 for (l
= defined
; l
!= NULL
; l
= l
->next
)
732 def
= (definition
*) l
->val
;
733 if (def
->def_kind
== DEF_PROGRAM
)
735 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
738 f_print (fout
, "%spmap_unset (%s, %s);\n",
739 space
, def
->def_name
, vp
->vers_name
);
746 print_err_message (const char *space
)
749 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
750 else if (inetdflag
|| pmflag
)
751 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
753 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
757 * Write the server auxiliary function ( _msgout, timeout)
760 write_svc_aux (int nomain
)
765 write_timeout_func ();
769 * Write the _msgout function
775 f_print (fout
, "\n");
776 f_print (fout
, "static\n");
779 f_print (fout
, "void _msgout (msg)\n");
780 f_print (fout
, "\tchar *msg;\n");
784 f_print (fout
, "void _msgout (char* msg)\n");
786 f_print (fout
, "{\n");
787 f_print (fout
, "#ifdef RPC_SVC_FG\n");
788 if (inetdflag
|| pmflag
)
789 f_print (fout
, "\tif (_rpcpmstart)\n");
790 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
791 f_print (fout
, "\telse\n");
792 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
793 f_print (fout
, "#else\n");
794 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
795 f_print (fout
, "#endif\n");
796 f_print (fout
, "}\n");
800 * Write the timeout function
803 write_timeout_func (void)
807 f_print (fout
, "\n");
808 f_print (fout
, "static void\n");
810 f_print (fout
, "closedown (int sig)\n");
812 f_print (fout
, "closedown (sig)\n\tint sig;\n");
813 f_print (fout
, "{\n");
815 #if defined (__GNU_LIBRARY__) && 0
816 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
817 Cflag
? "(SIG_PF)" : "(void(*)())");
820 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
822 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
824 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
826 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
827 f_print (fout
, "\t\tstatic int size;\n");
828 f_print (fout
, "\t\tint i, openfd;\n");
829 if (tirpcflag
&& pmflag
)
831 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
832 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
836 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
838 f_print (fout
, "\t\t\texit (0);\n");
839 f_print (fout
, "\t\tif (size == 0) {\n");
842 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
843 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
844 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
845 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
847 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
848 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
852 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
854 f_print (fout
, "\t\t}\n");
855 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
856 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
857 f_print (fout
, "\t\t\t\topenfd++;\n");
858 f_print (fout
, "\t\tif (openfd <= 1)\n");
859 f_print (fout
, "\t\t\texit (0);\n");
860 f_print (fout
, "\t}\n");
861 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
862 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
864 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
865 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
866 Cflag
? "(SIG_PF)" : "(void(*)())");
867 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
868 f_print (fout
, "}\n");
872 * Write the most of port monitor support
875 write_pm_most (const char *infile
, int netflag
)
881 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
882 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
883 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
884 f_print (fout
, "\t\tchar *netid;\n");
886 { /* Not included by -n option */
887 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
888 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
891 f_print (fout
, "\t\tint pmclose;\n");
892 /* not necessary, defined in /usr/include/stdlib */
893 /* f_print(fout, "\t\textern char *getenv();\n"); */
894 f_print (fout
, "\n");
895 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
897 open_log_file (infile
, "\t\t");
898 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
899 sprintf (_errbuf
, "cannot get transport name");
900 print_err_message ("\t\t\t");
901 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
902 sprintf (_errbuf
, "cannot get transport info");
903 print_err_message ("\t\t\t");
904 f_print (fout
, "\t\t}\n");
906 * A kludgy support for inetd services. Inetd only works with
907 * sockmod, and RPC works only with timod, hence all this jugglery
909 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
910 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
911 sprintf (_errbuf
, "could not get the right module");
912 print_err_message ("\t\t\t\t");
913 f_print (fout
, "\t\t\t\texit(1);\n");
914 f_print (fout
, "\t\t\t}\n");
915 f_print (fout
, "\t\t}\n");
917 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
918 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
920 sprintf (_errbuf
, "cannot create server handle");
921 print_err_message ("\t\t\t");
922 f_print (fout
, "\t\t\texit(1);\n");
923 f_print (fout
, "\t\t}\n");
924 f_print (fout
, "\t\tif (nconf)\n");
925 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
926 for (l
= defined
; l
!= NULL
; l
= l
->next
)
928 def
= (definition
*) l
->val
;
929 if (def
->def_kind
!= DEF_PROGRAM
)
933 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
936 "\t\tif (!svc_reg(%s, %s, %s, ",
937 TRANSP
, def
->def_name
, vp
->vers_name
);
938 pvname (def
->def_name
, vp
->vers_num
);
939 f_print (fout
, ", 0)) {\n");
940 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
941 def
->def_name
, vp
->vers_name
);
942 print_err_message ("\t\t\t");
943 f_print (fout
, "\t\t\texit(1);\n");
944 f_print (fout
, "\t\t}\n");
949 f_print (fout
, "\t\tif (pmclose) {\n");
950 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
951 Cflag
? "(SIG_PF)" : "(void(*)())");
952 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
953 f_print (fout
, "\t\t}\n");
955 f_print (fout
, "\t\tsvc_run();\n");
956 f_print (fout
, "\t\texit(1);\n");
957 f_print (fout
, "\t\t/* NOTREACHED */\n");
958 f_print (fout
, "\t}\n");
962 * Support for backgrounding the server if self started.
965 write_rpc_svc_fg (const char *infile
, const char *sp
)
967 f_print (fout
, "#ifndef RPC_SVC_FG\n");
968 f_print (fout
, "%sint size;\n", sp
);
970 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
972 f_print (fout
, "%sint pid, i;\n\n", sp
);
973 f_print (fout
, "%spid = fork();\n", sp
);
974 f_print (fout
, "%sif (pid < 0) {\n", sp
);
975 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
976 f_print (fout
, "%s\texit(1);\n", sp
);
977 f_print (fout
, "%s}\n", sp
);
978 f_print (fout
, "%sif (pid)\n", sp
);
979 f_print (fout
, "%s\texit(0);\n", sp
);
980 /* get number of file descriptors */
983 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
984 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
985 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
986 f_print (fout
, "%s\texit(1);\n", sp
);
990 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
993 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
994 f_print (fout
, "%s\t(void) close(i);\n", sp
);
995 /* Redirect stderr and stdout to console */
996 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
997 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
998 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
999 /* This removes control of the controlling terminal */
1001 f_print (fout
, "%ssetsid();\n", sp
);
1004 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1005 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1006 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1007 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1008 f_print (fout
, "%s}\n", sp
);
1011 open_log_file (infile
, sp
);
1012 f_print (fout
, "#endif\n");
1014 open_log_file (infile
, sp
);
1018 open_log_file (const char *infile
, const char *sp
)
1022 s
= strrchr (infile
, '.');
1025 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1031 * write a registration for the given transport for Inetd
1034 write_inetd_register (const char *transp
)
1047 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1051 f_print (fout
, "\n");
1054 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1055 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1057 f_print (fout
, "%s\t%s = svc%s_create(%s",
1058 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1060 f_print (fout
, ", 0, 0");
1061 f_print (fout
, ");\n");
1062 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1063 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1064 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1065 print_err_message (tmpbuf
);
1066 f_print (fout
, "%s\t\texit(1);\n", sp
);
1067 f_print (fout
, "%s\t}\n", sp
);
1071 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1072 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1073 sp
, isudp
? "UDP" : "TCP");
1075 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1077 def
= (definition
*) l
->val
;
1078 if (def
->def_kind
!= DEF_PROGRAM
)
1082 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1084 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1085 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1086 pvname (def
->def_name
, vp
->vers_num
);
1088 f_print (fout
, ", proto)) {\n");
1090 f_print (fout
, ", IPPROTO_%s)) {\n",
1091 isudp
? "UDP" : "TCP");
1092 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1093 def
->def_name
, vp
->vers_name
, transp
);
1094 print_err_message (tmpbuf
);
1095 f_print (fout
, "%s\t\texit(1);\n", sp
);
1096 f_print (fout
, "%s\t}\n", sp
);
1100 f_print (fout
, "\t}\n");