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
)
95 var_type
= (nomain
? "extern" : "");
96 f_print (fout
, "%s int _rpcpmstart;", var_type
);
97 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
100 f_print (fout
, "%s int _rpcfdtype;", var_type
);
101 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
106 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
107 f_print (fout
, "\t/* Still serving ? */\n");
109 f_print(fout
, " /* States a server can be in wrt request */\n\n");
110 f_print(fout
, "#define\t_IDLE 0\n");
111 f_print(fout
, "#define\t_SERVED 1\n");
112 f_print(fout
, "#define\t_SERVING 2\n\n");
113 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
114 f_print(fout
, "\t /* Set when a request is serviced */\n");
118 f_print (fout
, "mutex_t _svcstate_lock;");
120 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
124 write_svc_aux (nomain
);
126 /* write out dispatcher and stubs */
127 write_programs (nomain
? NULL
: "static");
133 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
136 f_print (fout
, "\nint\nmain (argc, argv)\n");
137 f_print (fout
, "\tint argc;\n");
138 f_print (fout
, "\tchar **argv;\n");
140 f_print (fout
, "{\n");
143 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
151 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
152 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
154 f_print (fout
, "\tpid_t pid;\n");
155 f_print (fout
, "\tint i;\n");
156 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
158 if (mtflag
& timerflag
)
160 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
162 write_pm_most (infile
, netflag
);
163 f_print (fout
, "\telse {\n");
164 write_rpc_svc_fg (infile
, "\t\t");
165 f_print (fout
, "\t}\n");
169 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
170 f_print (fout
, "\n");
171 print_pmapunset ("\t");
175 if (logflag
&& !inetdflag
)
177 open_log_file (infile
, "\t");
182 * write a registration for the given transport
185 write_netid_register (const char *transp
)
194 f_print (fout
, "\n");
195 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
196 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
197 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
198 sprintf (tmpbuf
, "%s\t\t", sp
);
199 print_err_message (tmpbuf
);
200 f_print (fout
, "%s\t\texit (1);\n", sp
);
201 f_print (fout
, "%s\t}\n", sp
);
202 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
203 sp
, TRANSP
/*, transp *//* ?!?... */ );
204 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
205 sprintf (_errbuf
, "cannot create %s service.", transp
);
206 print_err_message (tmpbuf
);
207 f_print (fout
, "%s\t\texit (1);\n", sp
);
208 f_print (fout
, "%s\t}\n", sp
);
210 for (l
= defined
; l
!= NULL
; l
= l
->next
)
212 def
= (definition
*) l
->val
;
213 if (def
->def_kind
!= DEF_PROGRAM
)
217 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
219 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
220 sp
, def
->def_name
, vp
->vers_name
);
221 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
222 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
223 pvname (def
->def_name
, vp
->vers_num
);
224 f_print (fout
, ", nconf)) {\n");
225 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
226 def
->def_name
, vp
->vers_name
, transp
);
227 print_err_message (tmpbuf
);
228 f_print (fout
, "%s\t\texit (1);\n", sp
);
229 f_print (fout
, "%s\t}\n", sp
);
232 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
236 * write a registration for the given transport for TLI
239 write_nettype_register (const char *transp
)
245 for (l
= defined
; l
!= NULL
; l
= l
->next
)
247 def
= (definition
*) l
->val
;
248 if (def
->def_kind
!= DEF_PROGRAM
)
252 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
254 f_print (fout
, "\tif (!svc_create (");
255 pvname (def
->def_name
, vp
->vers_num
);
256 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
257 def
->def_name
, vp
->vers_name
, transp
);
258 (void) sprintf (_errbuf
,
259 "unable to create (%s, %s) for %s.",
260 def
->def_name
, vp
->vers_name
, transp
);
261 print_err_message ("\t\t");
262 f_print (fout
, "\t\texit (1);\n");
263 f_print (fout
, "\t}\n");
269 * write the rest of the service
274 f_print (fout
, "\n");
277 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
278 (void) sprintf (_errbuf
, "could not create a handle");
279 print_err_message ("\t\t");
280 f_print (fout
, "\t\texit (1);\n");
281 f_print (fout
, "\t}\n");
284 f_print (fout
, "\tif (_rpcpmstart) {\n");
286 "\t\t(void) signal (SIGALRM, %s closedown);\n",
287 Cflag
? "(SIG_PF)" : "(void(*)())");
288 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
289 f_print (fout
, "\t}\n");
292 f_print (fout
, "\tsvc_run ();\n");
293 (void) sprintf (_errbuf
, "svc_run returned");
294 print_err_message ("\t");
295 f_print (fout
, "\texit (1);\n");
296 f_print (fout
, "\t/* NOTREACHED */\n");
297 f_print (fout
, "}\n");
301 write_programs (const char *storage
)
306 /* write out stubs for procedure definitions */
307 for (l
= defined
; l
!= NULL
; l
= l
->next
)
309 def
= (definition
*) l
->val
;
310 if (def
->def_kind
== DEF_PROGRAM
)
312 write_real_program (def
);
316 /* write out dispatcher for each program */
317 for (l
= defined
; l
!= NULL
; l
= l
->next
)
319 def
= (definition
*) l
->val
;
320 if (def
->def_kind
== DEF_PROGRAM
)
322 write_program (def
, storage
);
327 /* write out definition of internal function (e.g. _printmsg_1(...))
328 which calls server's defintion of actual function (e.g. printmsg_1(...)).
329 Unpacks single user argument of printmsg_1 to call-by-value format
330 expected by printmsg_1. */
332 write_real_program (const definition
* def
)
339 return; /* not needed for old style */
340 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
342 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
344 fprintf (fout
, "\n");
346 internal_proctype (proc
);
348 f_print (fout
, "int");
349 f_print (fout
, "\n_");
350 pvname (proc
->proc_name
, vp
->vers_num
);
353 f_print (fout
, " (");
355 if (proc
->arg_num
> 1)
356 f_print (fout
, "%s", proc
->args
.argname
);
358 ptype (proc
->args
.decls
->decl
.prefix
,
359 proc
->args
.decls
->decl
.type
, 0);
362 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
366 f_print (fout
, " *argp, struct svc_req *%s)\n",
372 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
374 f_print (fout
, " (argp, %s)\n", RQSTP
);
376 if (proc
->arg_num
> 1)
377 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
380 f_print (fout
, "\t");
381 ptype (proc
->args
.decls
->decl
.prefix
,
382 proc
->args
.decls
->decl
.type
, 0);
383 f_print (fout
, " *argp;\n");
385 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
388 f_print (fout
, "{\n");
389 f_print (fout
, "\treturn (");
391 pvname_svc (proc
->proc_name
, vp
->vers_num
);
393 pvname (proc
->proc_name
, vp
->vers_num
);
395 if (proc
->arg_num
< 2)
396 { /* single argument */
397 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
398 f_print (fout
, "*argp, "); /* non-void */
402 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
403 f_print (fout
, "argp->%s, ", l
->decl
.name
);
406 f_print (fout
, "%s, ", RESULT
);
407 f_print (fout
, "%s));\n}\n", RQSTP
);
413 write_program (const definition
* def
, const char *storage
)
419 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
421 f_print (fout
, "\n");
424 f_print (fout
, "%s ", storage
);
426 f_print (fout
, "void\n");
427 pvname (def
->def_name
, vp
->vers_num
);
431 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
432 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
436 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
437 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
438 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
441 f_print (fout
, "{\n");
444 f_print (fout
, "\tunion {\n");
445 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
447 if (proc
->arg_num
< 2)
448 { /* single argument */
449 if (streq (proc
->args
.decls
->decl
.type
,
455 f_print (fout
, "\t\t");
456 ptype (proc
->args
.decls
->decl
.prefix
,
457 proc
->args
.decls
->decl
.type
, 0);
458 pvname (proc
->proc_name
, vp
->vers_num
);
459 f_print (fout
, "_arg;\n");
465 f_print (fout
, "\t\t%s", proc
->args
.argname
);
467 pvname (proc
->proc_name
, vp
->vers_num
);
468 f_print (fout
, "_arg;\n");
473 f_print (fout
, "\t\tint fill;\n");
475 f_print (fout
, "\t} %s;\n", ARG
);
478 f_print(fout
, "\tunion {\n");
479 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
480 if (!streq (proc
->res_type
, "void"))
482 f_print(fout
, "\t\t");
483 ptype(proc
->res_prefix
, proc
->res_type
, 0);
484 pvname(proc
->proc_name
, vp
->vers_num
);
485 f_print(fout
, "_res;\n");
487 f_print(fout
, "\t} %s;\n", RESULT
);
488 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
491 f_print (fout
, "\tchar *%s;\n", RESULT
);
495 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
498 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
501 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
506 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
508 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
510 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
512 f_print (fout
, "\n");
516 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
520 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
521 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
523 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
527 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
528 if (!nullproc (vp
->procs
))
530 f_print (fout
, "\tcase NULLPROC:\n");
532 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
534 print_return ("\t\t");
535 f_print (fout
, "\n");
537 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
539 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
540 if (proc
->arg_num
< 2)
541 { /* single argument */
542 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
546 p_xdrfunc (ARG
, proc
->args
.argname
);
548 p_xdrfunc (RESULT
, proc
->res_type
);
553 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
557 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
562 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
564 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
567 { /* new style: calls internal routine */
570 if ((Cflag
|| mtflag
) && !newstyle
)
571 pvname_svc (proc
->proc_name
, vp
->vers_num
);
573 pvname (proc
->proc_name
, vp
->vers_num
);
574 f_print (fout
, ";\n");
575 f_print (fout
, "\t\tbreak;\n\n");
577 f_print (fout
, "\tdefault:\n");
578 printerr ("noproc", TRANSP
);
579 print_return ("\t\t");
580 f_print (fout
, "\t}\n");
582 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
583 printif ("getargs", TRANSP
, ARG
);
584 printerr ("decode", TRANSP
);
585 print_return ("\t\t");
586 f_print (fout
, "\t}\n");
591 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
592 RESULT
, ROUTINE
, ARG
, RQSTP
);
594 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
595 RESULT
, ROUTINE
, ARG
, RQSTP
);
599 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
600 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
602 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
603 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
606 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
607 RETVAL
, TRANSP
, RESULT
, RESULT
);
610 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
611 RESULT
, TRANSP
, RESULT
, RESULT
);
613 printerr ("systemerr", TRANSP
);
614 f_print (fout
, "\t}\n");
616 printif ("freeargs", TRANSP
, ARG
);
618 sprintf (_errbuf
, "unable to free arguments");
619 print_err_message ("\t\t");
620 f_print (fout
, "\t\texit (1);\n");
621 f_print (fout
, "\t}\n");
622 /* print out free routine */
625 f_print(fout
,"\tif (!");
626 pvname(def
->def_name
, vp
->vers_num
);
627 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
628 TRANSP
, RESULT
, RESULT
);
629 (void) sprintf(_errbuf
, "unable to free results");
630 print_err_message("\t\t");
634 f_print (fout
, "}\n");
639 printerr (const char *err
, const char *transp
)
641 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
645 printif (const char *proc
, const char *transp
, const char *arg
)
647 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
648 proc
, transp
, arg
, arg
);
652 nullproc (const proc_list
* proc
)
654 for (; proc
!= NULL
; proc
= proc
->next
)
656 if (streq (proc
->proc_num
, "0"))
665 write_inetmost (const char *infile
)
667 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
668 f_print (fout
, "\tint sock;\n");
669 f_print (fout
, "\tint proto;\n");
670 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
671 f_print (fout
, "\tint asize = sizeof (saddr);\n");
672 f_print (fout
, "\n");
674 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
675 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
676 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
677 f_print (fout
, "\t\t\texit (1);\n");
678 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
679 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
680 f_print (fout
, "\t\t\texit (1);\n");
681 f_print (fout
, "\t\tsock = 0;\n");
682 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
683 f_print (fout
, "\t\tproto = 0;\n");
684 open_log_file (infile
, "\t\t");
685 f_print (fout
, "\t} else {\n");
686 write_rpc_svc_fg (infile
, "\t\t");
687 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
688 print_pmapunset ("\t\t");
689 f_print (fout
, "\t}\n");
693 print_return (const char *space
)
696 f_print (fout
, "%sexit (0);\n", space
);
702 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
705 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
706 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
708 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
711 f_print (fout
, "%sreturn;\n", space
);
716 print_pmapunset (const char *space
)
722 for (l
= defined
; l
!= NULL
; l
= l
->next
)
724 def
= (definition
*) l
->val
;
725 if (def
->def_kind
== DEF_PROGRAM
)
727 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
730 f_print (fout
, "%spmap_unset (%s, %s);\n",
731 space
, def
->def_name
, vp
->vers_name
);
738 print_err_message (const char *space
)
741 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
742 else if (inetdflag
|| pmflag
)
743 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
745 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
749 * Write the server auxiliary function ( _msgout, timeout)
752 write_svc_aux (int nomain
)
757 write_timeout_func ();
761 * Write the _msgout function
767 f_print (fout
, "\n");
768 f_print (fout
, "static\n");
771 f_print (fout
, "void _msgout (msg)\n");
772 f_print (fout
, "\tchar *msg;\n");
776 f_print (fout
, "void _msgout (char* msg)\n");
778 f_print (fout
, "{\n");
779 f_print (fout
, "#ifdef RPC_SVC_FG\n");
780 if (inetdflag
|| pmflag
)
781 f_print (fout
, "\tif (_rpcpmstart)\n");
782 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
783 f_print (fout
, "\telse\n");
784 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
785 f_print (fout
, "#else\n");
786 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
787 f_print (fout
, "#endif\n");
788 f_print (fout
, "}\n");
792 * Write the timeout function
795 write_timeout_func (void)
799 f_print (fout
, "\n");
800 f_print (fout
, "static void\n");
802 f_print (fout
, "closedown (int sig)\n");
804 f_print (fout
, "closedown (sig)\n\tint sig;\n");
805 f_print (fout
, "{\n");
808 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
809 Cflag
? "(SIG_PF)" : "(void(*)())");
812 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
814 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
816 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
818 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
819 f_print (fout
, "\t\tstatic int size;\n");
820 f_print (fout
, "\t\tint i, openfd;\n");
821 if (tirpcflag
&& pmflag
)
823 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
824 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
828 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
830 f_print (fout
, "\t\t\texit (0);\n");
831 f_print (fout
, "\t\tif (size == 0) {\n");
834 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
835 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
836 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
837 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
839 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
840 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
844 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
846 f_print (fout
, "\t\t}\n");
847 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
848 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
849 f_print (fout
, "\t\t\t\topenfd++;\n");
850 f_print (fout
, "\t\tif (openfd <= 1)\n");
851 f_print (fout
, "\t\t\texit (0);\n");
852 f_print (fout
, "\t}\n");
853 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
854 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
856 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
857 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
858 Cflag
? "(SIG_PF)" : "(void(*)())");
859 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
860 f_print (fout
, "}\n");
864 * Write the most of port monitor support
867 write_pm_most (const char *infile
, int netflag
)
873 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
874 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
875 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
876 f_print (fout
, "\t\tchar *netid;\n");
878 { /* Not included by -n option */
879 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
880 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
883 f_print (fout
, "\t\tint pmclose;\n");
884 /* not necessary, defined in /usr/include/stdlib */
885 /* f_print(fout, "\t\textern char *getenv();\n"); */
886 f_print (fout
, "\n");
887 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
889 open_log_file (infile
, "\t\t");
890 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
891 sprintf (_errbuf
, "cannot get transport name");
892 print_err_message ("\t\t\t");
893 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
894 sprintf (_errbuf
, "cannot get transport info");
895 print_err_message ("\t\t\t");
896 f_print (fout
, "\t\t}\n");
898 * A kludgy support for inetd services. Inetd only works with
899 * sockmod, and RPC works only with timod, hence all this jugglery
901 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
902 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
903 sprintf (_errbuf
, "could not get the right module");
904 print_err_message ("\t\t\t\t");
905 f_print (fout
, "\t\t\t\texit(1);\n");
906 f_print (fout
, "\t\t\t}\n");
907 f_print (fout
, "\t\t}\n");
909 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
910 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
912 sprintf (_errbuf
, "cannot create server handle");
913 print_err_message ("\t\t\t");
914 f_print (fout
, "\t\t\texit(1);\n");
915 f_print (fout
, "\t\t}\n");
916 f_print (fout
, "\t\tif (nconf)\n");
917 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
918 for (l
= defined
; l
!= NULL
; l
= l
->next
)
920 def
= (definition
*) l
->val
;
921 if (def
->def_kind
!= DEF_PROGRAM
)
925 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
928 "\t\tif (!svc_reg(%s, %s, %s, ",
929 TRANSP
, def
->def_name
, vp
->vers_name
);
930 pvname (def
->def_name
, vp
->vers_num
);
931 f_print (fout
, ", 0)) {\n");
932 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
933 def
->def_name
, vp
->vers_name
);
934 print_err_message ("\t\t\t");
935 f_print (fout
, "\t\t\texit(1);\n");
936 f_print (fout
, "\t\t}\n");
941 f_print (fout
, "\t\tif (pmclose) {\n");
942 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
943 Cflag
? "(SIG_PF)" : "(void(*)())");
944 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
945 f_print (fout
, "\t\t}\n");
947 f_print (fout
, "\t\tsvc_run();\n");
948 f_print (fout
, "\t\texit(1);\n");
949 f_print (fout
, "\t\t/* NOTREACHED */\n");
950 f_print (fout
, "\t}\n");
954 * Support for backgrounding the server if self started.
957 write_rpc_svc_fg (const char *infile
, const char *sp
)
959 f_print (fout
, "#ifndef RPC_SVC_FG\n");
960 f_print (fout
, "%sint size;\n", sp
);
962 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
964 f_print (fout
, "%sint pid, i;\n\n", sp
);
965 f_print (fout
, "%spid = fork();\n", sp
);
966 f_print (fout
, "%sif (pid < 0) {\n", sp
);
967 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
968 f_print (fout
, "%s\texit(1);\n", sp
);
969 f_print (fout
, "%s}\n", sp
);
970 f_print (fout
, "%sif (pid)\n", sp
);
971 f_print (fout
, "%s\texit(0);\n", sp
);
972 /* get number of file descriptors */
975 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
976 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
977 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
978 f_print (fout
, "%s\texit(1);\n", sp
);
982 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
985 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
986 f_print (fout
, "%s\t(void) close(i);\n", sp
);
987 /* Redirect stderr and stdout to console */
988 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
989 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
990 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
991 /* This removes control of the controlling terminal */
993 f_print (fout
, "%ssetsid();\n", sp
);
996 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
997 f_print (fout
, "%sif (i >= 0) {\n", sp
);
998 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
999 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1000 f_print (fout
, "%s}\n", sp
);
1003 open_log_file (infile
, sp
);
1004 f_print (fout
, "#endif\n");
1006 open_log_file (infile
, sp
);
1010 open_log_file (const char *infile
, const char *sp
)
1014 s
= strrchr (infile
, '.');
1017 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1023 * write a registration for the given transport for Inetd
1026 write_inetd_register (const char *transp
)
1039 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1043 f_print (fout
, "\n");
1046 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1047 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1049 f_print (fout
, "%s\t%s = svc%s_create(%s",
1050 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1052 f_print (fout
, ", 0, 0");
1053 f_print (fout
, ");\n");
1054 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1055 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1056 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1057 print_err_message (tmpbuf
);
1058 f_print (fout
, "%s\t\texit(1);\n", sp
);
1059 f_print (fout
, "%s\t}\n", sp
);
1063 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1064 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1065 sp
, isudp
? "UDP" : "TCP");
1067 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1069 def
= (definition
*) l
->val
;
1070 if (def
->def_kind
!= DEF_PROGRAM
)
1074 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1076 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1077 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1078 pvname (def
->def_name
, vp
->vers_num
);
1080 f_print (fout
, ", proto)) {\n");
1082 f_print (fout
, ", IPPROTO_%s)) {\n",
1083 isudp
? "UDP" : "TCP");
1084 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1085 def
->def_name
, vp
->vers_name
, transp
);
1086 print_err_message (tmpbuf
);
1087 f_print (fout
, "%s\t\texit(1);\n", sp
);
1088 f_print (fout
, "%s\t}\n", sp
);
1092 f_print (fout
, "\t}\n");