2 * From: @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
37 #include "rpc_parse.h"
41 static const char RQSTP
[] = "rqstp";
42 static const char TRANSP
[] = "transp";
43 static const char ARG
[] = "argument";
44 static const char RESULT
[] = "result";
45 static const char ROUTINE
[] = "local";
46 static char RETVAL
[] = "retval";
48 char _errbuf
[256]; /* For all messages */
50 static void internal_proctype (const proc_list
* plist
);
51 static void p_xdrfunc (const char *rname
, const char *typename
);
52 static void write_real_program (const definition
* def
);
53 static void write_program (const definition
* def
, const char *storage
);
54 static void printerr (const char *err
, const char *transp
);
55 static void printif (const char *proc
, const char *transp
, const char *arg
);
56 static void write_inetmost (const char *infile
);
57 static void print_return (const char *space
);
58 static void print_pmapunset (const char *space
);
59 static void print_err_message (const char *space
);
60 static void write_timeout_func (void);
61 static void write_pm_most (const char *infile
, int netflag
);
62 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
63 static void open_log_file (const char *infile
, const char *sp
);
66 p_xdrfunc (const char *rname
, const char *typename
)
69 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
70 stringfix (typename
));
72 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
76 internal_proctype (const proc_list
* plist
)
78 f_print (fout
, "static ");
79 ptype (plist
->res_prefix
, plist
->res_type
, 1);
85 * write most of the service, that is, everything but the registrations.
88 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
90 if (inetdflag
|| pmflag
)
93 #ifdef __GNU_LIBRARY__
95 var_type
= (nomain
? "extern" : "");
97 var_type
= (nomain
? "extern" : "static");
99 f_print (fout
, "%s int _rpcpmstart;", var_type
);
100 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
103 f_print (fout
, "%s int _rpcfdtype;", var_type
);
104 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
109 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
110 f_print (fout
, "\t/* Still serving ? */\n");
112 f_print(fout
, " /* States a server can be in wrt request */\n\n");
113 f_print(fout
, "#define\t_IDLE 0\n");
114 f_print(fout
, "#define\t_SERVED 1\n");
115 f_print(fout
, "#define\t_SERVING 2\n\n");
116 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
117 f_print(fout
, "\t /* Set when a request is serviced */\n");
121 f_print (fout
, "mutex_t _svcstate_lock;");
123 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
127 write_svc_aux (nomain
);
129 /* write out dispatcher and stubs */
130 write_programs (nomain
? NULL
: "static");
135 #ifdef __GNU_LIBRARY__
137 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
140 f_print (fout
, "\nint\nmain (argc, argv)\n");
141 f_print (fout
, "\tint argc;\n");
142 f_print (fout
, "\tchar **argv;\n");
145 f_print (fout
, "\nmain()\n");
147 f_print (fout
, "{\n");
150 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
158 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
159 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
161 f_print (fout
, "\tpid_t pid;\n");
162 f_print (fout
, "\tint i;\n");
163 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
165 if (mtflag
& timerflag
)
167 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
169 write_pm_most (infile
, netflag
);
170 f_print (fout
, "\telse {\n");
171 write_rpc_svc_fg (infile
, "\t\t");
172 f_print (fout
, "\t}\n");
176 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
177 f_print (fout
, "\n");
178 print_pmapunset ("\t");
182 if (logflag
&& !inetdflag
)
184 open_log_file (infile
, "\t");
189 * write a registration for the given transport
192 write_netid_register (const char *transp
)
201 f_print (fout
, "\n");
202 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
203 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
204 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
205 sprintf (tmpbuf
, "%s\t\t", sp
);
206 print_err_message (tmpbuf
);
207 f_print (fout
, "%s\t\texit (1);\n", sp
);
208 f_print (fout
, "%s\t}\n", sp
);
209 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
210 sp
, TRANSP
/*, transp *//* ?!?... */ );
211 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
212 sprintf (_errbuf
, "cannot create %s service.", transp
);
213 print_err_message (tmpbuf
);
214 f_print (fout
, "%s\t\texit (1);\n", sp
);
215 f_print (fout
, "%s\t}\n", sp
);
217 for (l
= defined
; l
!= NULL
; l
= l
->next
)
219 def
= (definition
*) l
->val
;
220 if (def
->def_kind
!= DEF_PROGRAM
)
224 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
226 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
227 sp
, def
->def_name
, vp
->vers_name
);
228 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
229 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
230 pvname (def
->def_name
, vp
->vers_num
);
231 f_print (fout
, ", nconf)) {\n");
232 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
233 def
->def_name
, vp
->vers_name
, transp
);
234 print_err_message (tmpbuf
);
235 f_print (fout
, "%s\t\texit (1);\n", sp
);
236 f_print (fout
, "%s\t}\n", sp
);
239 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
243 * write a registration for the given transport for TLI
246 write_nettype_register (const char *transp
)
252 for (l
= defined
; l
!= NULL
; l
= l
->next
)
254 def
= (definition
*) l
->val
;
255 if (def
->def_kind
!= DEF_PROGRAM
)
259 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
261 f_print (fout
, "\tif (!svc_create (");
262 pvname (def
->def_name
, vp
->vers_num
);
263 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
264 def
->def_name
, vp
->vers_name
, transp
);
265 (void) sprintf (_errbuf
,
266 "unable to create (%s, %s) for %s.",
267 def
->def_name
, vp
->vers_name
, transp
);
268 print_err_message ("\t\t");
269 f_print (fout
, "\t\texit (1);\n");
270 f_print (fout
, "\t}\n");
276 * write the rest of the service
281 f_print (fout
, "\n");
284 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
285 (void) sprintf (_errbuf
, "could not create a handle");
286 print_err_message ("\t\t");
287 f_print (fout
, "\t\texit (1);\n");
288 f_print (fout
, "\t}\n");
291 f_print (fout
, "\tif (_rpcpmstart) {\n");
293 "\t\t(void) signal (SIGALRM, %s closedown);\n",
294 Cflag
? "(SIG_PF)" : "(void(*)())");
295 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
296 f_print (fout
, "\t}\n");
299 f_print (fout
, "\tsvc_run ();\n");
300 (void) sprintf (_errbuf
, "svc_run returned");
301 print_err_message ("\t");
302 f_print (fout
, "\texit (1);\n");
303 f_print (fout
, "\t/* NOTREACHED */\n");
304 f_print (fout
, "}\n");
308 write_programs (const char *storage
)
313 /* write out stubs for procedure definitions */
314 for (l
= defined
; l
!= NULL
; l
= l
->next
)
316 def
= (definition
*) l
->val
;
317 if (def
->def_kind
== DEF_PROGRAM
)
319 write_real_program (def
);
323 /* write out dispatcher for each program */
324 for (l
= defined
; l
!= NULL
; l
= l
->next
)
326 def
= (definition
*) l
->val
;
327 if (def
->def_kind
== DEF_PROGRAM
)
329 write_program (def
, storage
);
334 /* write out definition of internal function (e.g. _printmsg_1(...))
335 which calls server's defintion of actual function (e.g. printmsg_1(...)).
336 Unpacks single user argument of printmsg_1 to call-by-value format
337 expected by printmsg_1. */
339 write_real_program (const definition
* def
)
346 return; /* not needed for old style */
347 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
349 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
351 fprintf (fout
, "\n");
353 internal_proctype (proc
);
355 f_print (fout
, "int");
356 f_print (fout
, "\n_");
357 pvname (proc
->proc_name
, vp
->vers_num
);
360 f_print (fout
, " (");
362 if (proc
->arg_num
> 1)
363 f_print (fout
, proc
->args
.argname
);
365 ptype (proc
->args
.decls
->decl
.prefix
,
366 proc
->args
.decls
->decl
.type
, 0);
369 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
373 f_print (fout
, " *argp, struct svc_req *%s)\n",
379 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
381 f_print (fout
, " (argp, %s)\n", RQSTP
);
383 if (proc
->arg_num
> 1)
384 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
387 f_print (fout
, "\t");
388 ptype (proc
->args
.decls
->decl
.prefix
,
389 proc
->args
.decls
->decl
.type
, 0);
390 f_print (fout
, " *argp;\n");
392 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
395 f_print (fout
, "{\n");
396 f_print (fout
, "\treturn (");
398 pvname_svc (proc
->proc_name
, vp
->vers_num
);
400 pvname (proc
->proc_name
, vp
->vers_num
);
402 if (proc
->arg_num
< 2)
403 { /* single argument */
404 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
405 f_print (fout
, "*argp, "); /* non-void */
409 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
410 f_print (fout
, "argp->%s, ", l
->decl
.name
);
413 f_print (fout
, "%s, ", RESULT
);
414 f_print (fout
, "%s));\n}\n", RQSTP
);
420 write_program (const definition
* def
, const char *storage
)
426 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
428 f_print (fout
, "\n");
431 f_print (fout
, "%s ", storage
);
433 f_print (fout
, "void\n");
434 pvname (def
->def_name
, vp
->vers_num
);
438 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
439 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
443 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
444 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
445 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
448 f_print (fout
, "{\n");
451 f_print (fout
, "\tunion {\n");
452 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
454 if (proc
->arg_num
< 2)
455 { /* single argument */
456 if (streq (proc
->args
.decls
->decl
.type
,
462 f_print (fout
, "\t\t");
463 ptype (proc
->args
.decls
->decl
.prefix
,
464 proc
->args
.decls
->decl
.type
, 0);
465 pvname (proc
->proc_name
, vp
->vers_num
);
466 f_print (fout
, "_arg;\n");
472 f_print (fout
, "\t\t%s", proc
->args
.argname
);
474 pvname (proc
->proc_name
, vp
->vers_num
);
475 f_print (fout
, "_arg;\n");
480 f_print (fout
, "\t\tint fill;\n");
482 f_print (fout
, "\t} %s;\n", ARG
);
485 f_print(fout
, "\tunion {\n");
486 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
487 if (!streq (proc
->res_type
, "void"))
489 f_print(fout
, "\t\t");
490 ptype(proc
->res_prefix
, proc
->res_type
, 0);
491 pvname(proc
->proc_name
, vp
->vers_num
);
492 f_print(fout
, "_res;\n");
494 f_print(fout
, "\t} %s;\n", RESULT
);
495 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
498 f_print (fout
, "\tchar *%s;\n", RESULT
);
502 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
505 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
508 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
513 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
515 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
517 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
519 f_print (fout
, "\n");
523 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
527 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
528 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
530 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
534 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
535 if (!nullproc (vp
->procs
))
537 f_print (fout
, "\tcase NULLPROC:\n");
539 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
541 print_return ("\t\t");
542 f_print (fout
, "\n");
544 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
546 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
547 if (proc
->arg_num
< 2)
548 { /* single argument */
549 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
553 p_xdrfunc (ARG
, proc
->args
.argname
);
555 p_xdrfunc (RESULT
, proc
->res_type
);
560 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
564 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
569 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
571 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
574 { /* new style: calls internal routine */
577 if ((Cflag
|| mtflag
) && !newstyle
)
578 pvname_svc (proc
->proc_name
, vp
->vers_num
);
580 pvname (proc
->proc_name
, vp
->vers_num
);
581 f_print (fout
, ";\n");
582 f_print (fout
, "\t\tbreak;\n\n");
584 f_print (fout
, "\tdefault:\n");
585 printerr ("noproc", TRANSP
);
586 print_return ("\t\t");
587 f_print (fout
, "\t}\n");
589 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
590 printif ("getargs", TRANSP
, ARG
);
591 printerr ("decode", TRANSP
);
592 print_return ("\t\t");
593 f_print (fout
, "\t}\n");
598 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
599 RESULT
, ROUTINE
, ARG
, RQSTP
);
601 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
602 RESULT
, ROUTINE
, ARG
, RQSTP
);
606 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
607 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
609 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
610 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
613 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
614 RETVAL
, TRANSP
, RESULT
, RESULT
);
617 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
618 RESULT
, TRANSP
, RESULT
, RESULT
);
620 printerr ("systemerr", TRANSP
);
621 f_print (fout
, "\t}\n");
623 printif ("freeargs", TRANSP
, ARG
);
625 sprintf (_errbuf
, "unable to free arguments");
626 print_err_message ("\t\t");
627 f_print (fout
, "\t\texit (1);\n");
628 f_print (fout
, "\t}\n");
629 /* print out free routine */
632 f_print(fout
,"\tif (!");
633 pvname(def
->def_name
, vp
->vers_num
);
634 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
635 TRANSP
, RESULT
, RESULT
);
636 (void) sprintf(_errbuf
, "unable to free results");
637 print_err_message("\t\t");
641 f_print (fout
, "}\n");
646 printerr (const char *err
, const char *transp
)
648 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
652 printif (const char *proc
, const char *transp
, const char *arg
)
654 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
655 proc
, transp
, arg
, arg
);
659 nullproc (const proc_list
* proc
)
661 for (; proc
!= NULL
; proc
= proc
->next
)
663 if (streq (proc
->proc_num
, "0"))
672 write_inetmost (const char *infile
)
674 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
675 f_print (fout
, "\tint sock;\n");
676 f_print (fout
, "\tint proto;\n");
677 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
678 f_print (fout
, "\tint asize = sizeof (saddr);\n");
679 f_print (fout
, "\n");
681 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
682 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
683 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
684 f_print (fout
, "\t\t\texit (1);\n");
685 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
686 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
687 f_print (fout
, "\t\t\texit (1);\n");
688 f_print (fout
, "\t\tsock = 0;\n");
689 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
690 f_print (fout
, "\t\tproto = 0;\n");
691 open_log_file (infile
, "\t\t");
692 f_print (fout
, "\t} else {\n");
693 write_rpc_svc_fg (infile
, "\t\t");
694 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
695 print_pmapunset ("\t\t");
696 f_print (fout
, "\t}\n");
700 print_return (const char *space
)
703 f_print (fout
, "%sexit (0);\n", space
);
709 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
712 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
713 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
715 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
718 f_print (fout
, "%sreturn;\n", space
);
723 print_pmapunset (const char *space
)
729 for (l
= defined
; l
!= NULL
; l
= l
->next
)
731 def
= (definition
*) l
->val
;
732 if (def
->def_kind
== DEF_PROGRAM
)
734 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
737 f_print (fout
, "%spmap_unset (%s, %s);\n",
738 space
, def
->def_name
, vp
->vers_name
);
745 print_err_message (const char *space
)
748 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
749 else if (inetdflag
|| pmflag
)
750 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
752 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
756 * Write the server auxiliary function ( _msgout, timeout)
759 write_svc_aux (int nomain
)
764 write_timeout_func ();
768 * Write the _msgout function
774 f_print (fout
, "\n");
775 f_print (fout
, "static\n");
778 f_print (fout
, "void _msgout (msg)\n");
779 f_print (fout
, "\tchar *msg;\n");
783 f_print (fout
, "void _msgout (char* msg)\n");
785 f_print (fout
, "{\n");
786 f_print (fout
, "#ifdef RPC_SVC_FG\n");
787 if (inetdflag
|| pmflag
)
788 f_print (fout
, "\tif (_rpcpmstart)\n");
789 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
790 f_print (fout
, "\telse\n");
791 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
792 f_print (fout
, "#else\n");
793 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
794 f_print (fout
, "#endif\n");
795 f_print (fout
, "}\n");
799 * Write the timeout function
802 write_timeout_func (void)
806 f_print (fout
, "\n");
807 f_print (fout
, "static void\n");
809 f_print (fout
, "closedown (int sig)\n");
811 f_print (fout
, "closedown (sig)\n\tint sig;\n");
812 f_print (fout
, "{\n");
814 #if defined (__GNU_LIBRARY__) && 0
815 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
816 Cflag
? "(SIG_PF)" : "(void(*)())");
819 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
821 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
823 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
825 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
826 f_print (fout
, "\t\tstatic int size;\n");
827 f_print (fout
, "\t\tint i, openfd;\n");
828 if (tirpcflag
&& pmflag
)
830 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
831 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
835 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
837 f_print (fout
, "\t\t\texit (0);\n");
838 f_print (fout
, "\t\tif (size == 0) {\n");
841 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
842 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
843 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
844 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
846 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
847 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
851 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
853 f_print (fout
, "\t\t}\n");
854 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
855 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
856 f_print (fout
, "\t\t\t\topenfd++;\n");
857 f_print (fout
, "\t\tif (openfd <= 1)\n");
858 f_print (fout
, "\t\t\texit (0);\n");
859 f_print (fout
, "\t}\n");
860 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
861 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
863 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
864 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
865 Cflag
? "(SIG_PF)" : "(void(*)())");
866 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
867 f_print (fout
, "}\n");
871 * Write the most of port monitor support
874 write_pm_most (const char *infile
, int netflag
)
880 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
881 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
882 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
883 f_print (fout
, "\t\tchar *netid;\n");
885 { /* Not included by -n option */
886 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
887 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
890 f_print (fout
, "\t\tint pmclose;\n");
891 /* not necessary, defined in /usr/include/stdlib */
892 /* f_print(fout, "\t\textern char *getenv();\n"); */
893 f_print (fout
, "\n");
894 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
896 open_log_file (infile
, "\t\t");
897 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
898 sprintf (_errbuf
, "cannot get transport name");
899 print_err_message ("\t\t\t");
900 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
901 sprintf (_errbuf
, "cannot get transport info");
902 print_err_message ("\t\t\t");
903 f_print (fout
, "\t\t}\n");
905 * A kludgy support for inetd services. Inetd only works with
906 * sockmod, and RPC works only with timod, hence all this jugglery
908 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
909 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
910 sprintf (_errbuf
, "could not get the right module");
911 print_err_message ("\t\t\t\t");
912 f_print (fout
, "\t\t\t\texit(1);\n");
913 f_print (fout
, "\t\t\t}\n");
914 f_print (fout
, "\t\t}\n");
916 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
917 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
919 sprintf (_errbuf
, "cannot create server handle");
920 print_err_message ("\t\t\t");
921 f_print (fout
, "\t\t\texit(1);\n");
922 f_print (fout
, "\t\t}\n");
923 f_print (fout
, "\t\tif (nconf)\n");
924 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
925 for (l
= defined
; l
!= NULL
; l
= l
->next
)
927 def
= (definition
*) l
->val
;
928 if (def
->def_kind
!= DEF_PROGRAM
)
932 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
935 "\t\tif (!svc_reg(%s, %s, %s, ",
936 TRANSP
, def
->def_name
, vp
->vers_name
);
937 pvname (def
->def_name
, vp
->vers_num
);
938 f_print (fout
, ", 0)) {\n");
939 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
940 def
->def_name
, vp
->vers_name
);
941 print_err_message ("\t\t\t");
942 f_print (fout
, "\t\t\texit(1);\n");
943 f_print (fout
, "\t\t}\n");
948 f_print (fout
, "\t\tif (pmclose) {\n");
949 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
950 Cflag
? "(SIG_PF)" : "(void(*)())");
951 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
952 f_print (fout
, "\t\t}\n");
954 f_print (fout
, "\t\tsvc_run();\n");
955 f_print (fout
, "\t\texit(1);\n");
956 f_print (fout
, "\t\t/* NOTREACHED */\n");
957 f_print (fout
, "\t}\n");
961 * Support for backgrounding the server if self started.
964 write_rpc_svc_fg (const char *infile
, const char *sp
)
966 f_print (fout
, "#ifndef RPC_SVC_FG\n");
967 f_print (fout
, "%sint size;\n", sp
);
969 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
971 f_print (fout
, "%sint pid, i;\n\n", sp
);
972 f_print (fout
, "%spid = fork();\n", sp
);
973 f_print (fout
, "%sif (pid < 0) {\n", sp
);
974 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
975 f_print (fout
, "%s\texit(1);\n", sp
);
976 f_print (fout
, "%s}\n", sp
);
977 f_print (fout
, "%sif (pid)\n", sp
);
978 f_print (fout
, "%s\texit(0);\n", sp
);
979 /* get number of file descriptors */
982 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
983 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
984 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
985 f_print (fout
, "%s\texit(1);\n", sp
);
989 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
992 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
993 f_print (fout
, "%s\t(void) close(i);\n", sp
);
994 /* Redirect stderr and stdout to console */
995 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
996 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
997 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
998 /* This removes control of the controlling terminal */
1000 f_print (fout
, "%ssetsid();\n", sp
);
1003 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1004 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1005 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1006 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1007 f_print (fout
, "%s}\n", sp
);
1010 open_log_file (infile
, sp
);
1011 f_print (fout
, "#endif\n");
1013 open_log_file (infile
, sp
);
1017 open_log_file (const char *infile
, const char *sp
)
1021 s
= strrchr (infile
, '.');
1024 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1030 * write a registration for the given transport for Inetd
1033 write_inetd_register (const char *transp
)
1046 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1050 f_print (fout
, "\n");
1053 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1054 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1056 f_print (fout
, "%s\t%s = svc%s_create(%s",
1057 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1059 f_print (fout
, ", 0, 0");
1060 f_print (fout
, ");\n");
1061 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1062 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1063 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1064 print_err_message (tmpbuf
);
1065 f_print (fout
, "%s\t\texit(1);\n", sp
);
1066 f_print (fout
, "%s\t}\n", sp
);
1070 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1071 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1072 sp
, isudp
? "UDP" : "TCP");
1074 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1076 def
= (definition
*) l
->val
;
1077 if (def
->def_kind
!= DEF_PROGRAM
)
1081 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1083 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1084 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1085 pvname (def
->def_name
, vp
->vers_num
);
1087 f_print (fout
, ", proto)) {\n");
1089 f_print (fout
, ", IPPROTO_%s)) {\n",
1090 isudp
? "UDP" : "TCP");
1091 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1092 def
->def_name
, vp
->vers_name
, transp
);
1093 print_err_message (tmpbuf
);
1094 f_print (fout
, "%s\t\texit(1);\n", sp
);
1095 f_print (fout
, "%s\t}\n", sp
);
1099 f_print (fout
, "\t}\n");