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
,
61 const char *prefix
, const char *arg
);
62 static void write_inetmost (const char *infile
);
63 static void print_return (const char *space
);
64 static void print_pmapunset (const char *space
);
65 static void print_err_message (const char *space
);
66 static void write_timeout_func (void);
67 static void write_pm_most (const char *infile
, int netflag
);
68 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
69 static void open_log_file (const char *infile
, const char *sp
);
72 p_xdrfunc (const char *rname
, const char *typename
)
75 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
76 stringfix (typename
));
78 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
82 internal_proctype (const proc_list
* plist
)
84 f_print (fout
, "static ");
85 ptype (plist
->res_prefix
, plist
->res_type
, 1);
91 * write most of the service, that is, everything but the registrations.
94 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
96 if (inetdflag
|| pmflag
)
99 #ifdef __GNU_LIBRARY__
101 var_type
= (nomain
? "extern" : "");
103 var_type
= (nomain
? "extern" : "static");
105 f_print (fout
, "%s int _rpcpmstart;", var_type
);
106 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
109 f_print (fout
, "%s int _rpcfdtype;", var_type
);
110 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
115 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
116 f_print (fout
, "\t/* Still serving ? */\n");
118 f_print(fout
, " /* States a server can be in wrt request */\n\n");
119 f_print(fout
, "#define\t_IDLE 0\n");
120 f_print(fout
, "#define\t_SERVED 1\n");
121 f_print(fout
, "#define\t_SERVING 2\n\n");
122 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
123 f_print(fout
, "\t /* Set when a request is serviced */\n");
127 f_print (fout
, "mutex_t _svcstate_lock;");
129 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
133 write_svc_aux (nomain
);
135 /* write out dispatcher and stubs */
136 write_programs (nomain
? NULL
: "static");
141 #ifdef __GNU_LIBRARY__
143 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
146 f_print (fout
, "\nint\nmain (argc, argv)\n");
147 f_print (fout
, "\tint argc;\n");
148 f_print (fout
, "\tchar **argv;\n");
151 f_print (fout
, "\nmain()\n");
153 f_print (fout
, "{\n");
156 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
164 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
165 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
167 f_print (fout
, "\tpid_t pid;\n");
168 f_print (fout
, "\tint i;\n");
169 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
171 if (mtflag
& timerflag
)
173 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
175 write_pm_most (infile
, netflag
);
176 f_print (fout
, "\telse {\n");
177 write_rpc_svc_fg (infile
, "\t\t");
178 f_print (fout
, "\t}\n");
182 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
183 f_print (fout
, "\n");
184 print_pmapunset ("\t");
188 if (logflag
&& !inetdflag
)
190 open_log_file (infile
, "\t");
195 * write a registration for the given transport
198 write_netid_register (const char *transp
)
207 f_print (fout
, "\n");
208 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
209 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
210 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
211 sprintf (tmpbuf
, "%s\t\t", sp
);
212 print_err_message (tmpbuf
);
213 f_print (fout
, "%s\t\texit (1);\n", sp
);
214 f_print (fout
, "%s\t}\n", sp
);
215 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
216 sp
, TRANSP
/*, transp *//* ?!?... */ );
217 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
218 sprintf (_errbuf
, "cannot create %s service.", transp
);
219 print_err_message (tmpbuf
);
220 f_print (fout
, "%s\t\texit (1);\n", sp
);
221 f_print (fout
, "%s\t}\n", sp
);
223 for (l
= defined
; l
!= NULL
; l
= l
->next
)
225 def
= (definition
*) l
->val
;
226 if (def
->def_kind
!= DEF_PROGRAM
)
230 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
232 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
233 sp
, def
->def_name
, vp
->vers_name
);
234 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
235 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
236 pvname (def
->def_name
, vp
->vers_num
);
237 f_print (fout
, ", nconf)) {\n");
238 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
239 def
->def_name
, vp
->vers_name
, transp
);
240 print_err_message (tmpbuf
);
241 f_print (fout
, "%s\t\texit (1);\n", sp
);
242 f_print (fout
, "%s\t}\n", sp
);
245 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
249 * write a registration for the given transport for TLI
252 write_nettype_register (const char *transp
)
258 for (l
= defined
; l
!= NULL
; l
= l
->next
)
260 def
= (definition
*) l
->val
;
261 if (def
->def_kind
!= DEF_PROGRAM
)
265 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
267 f_print (fout
, "\tif (!svc_create (");
268 pvname (def
->def_name
, vp
->vers_num
);
269 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
270 def
->def_name
, vp
->vers_name
, transp
);
271 (void) sprintf (_errbuf
,
272 "unable to create (%s, %s) for %s.",
273 def
->def_name
, vp
->vers_name
, transp
);
274 print_err_message ("\t\t");
275 f_print (fout
, "\t\texit (1);\n");
276 f_print (fout
, "\t}\n");
282 * write the rest of the service
287 f_print (fout
, "\n");
290 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
291 (void) sprintf (_errbuf
, "could not create a handle");
292 print_err_message ("\t\t");
293 f_print (fout
, "\t\texit (1);\n");
294 f_print (fout
, "\t}\n");
297 f_print (fout
, "\tif (_rpcpmstart) {\n");
299 "\t\t(void) signal (SIGALRM, %s closedown);\n",
300 Cflag
? "(SIG_PF)" : "(void(*)())");
301 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
302 f_print (fout
, "\t}\n");
305 f_print (fout
, "\tsvc_run ();\n");
306 (void) sprintf (_errbuf
, "svc_run returned");
307 print_err_message ("\t");
308 f_print (fout
, "\texit (1);\n");
309 f_print (fout
, "\t/* NOTREACHED */\n");
310 f_print (fout
, "}\n");
314 write_programs (const char *storage
)
319 /* write out stubs for procedure definitions */
320 for (l
= defined
; l
!= NULL
; l
= l
->next
)
322 def
= (definition
*) l
->val
;
323 if (def
->def_kind
== DEF_PROGRAM
)
325 write_real_program (def
);
329 /* write out dispatcher for each program */
330 for (l
= defined
; l
!= NULL
; l
= l
->next
)
332 def
= (definition
*) l
->val
;
333 if (def
->def_kind
== DEF_PROGRAM
)
335 write_program (def
, storage
);
340 /* write out definition of internal function (e.g. _printmsg_1(...))
341 which calls server's defintion of actual function (e.g. printmsg_1(...)).
342 Unpacks single user argument of printmsg_1 to call-by-value format
343 expected by printmsg_1. */
345 write_real_program (const definition
* def
)
352 return; /* not needed for old style */
353 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
355 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
357 fprintf (fout
, "\n");
359 internal_proctype (proc
);
361 f_print (fout
, "int");
362 f_print (fout
, "\n_");
363 pvname (proc
->proc_name
, vp
->vers_num
);
366 f_print (fout
, " (");
368 if (proc
->arg_num
> 1)
369 f_print (fout
, proc
->args
.argname
);
371 ptype (proc
->args
.decls
->decl
.prefix
,
372 proc
->args
.decls
->decl
.type
, 0);
375 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
379 f_print (fout
, " *argp, struct svc_req *%s)\n",
385 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
387 f_print (fout
, " (argp, %s)\n", RQSTP
);
389 if (proc
->arg_num
> 1)
390 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
393 f_print (fout
, "\t");
394 ptype (proc
->args
.decls
->decl
.prefix
,
395 proc
->args
.decls
->decl
.type
, 0);
396 f_print (fout
, " *argp;\n");
398 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
401 f_print (fout
, "{\n");
402 f_print (fout
, "\treturn (");
404 pvname_svc (proc
->proc_name
, vp
->vers_num
);
406 pvname (proc
->proc_name
, vp
->vers_num
);
408 if (proc
->arg_num
< 2)
409 { /* single argument */
410 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
411 f_print (fout
, "*argp, "); /* non-void */
415 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
416 f_print (fout
, "argp->%s, ", l
->decl
.name
);
418 f_print (fout
, "%s));\n}\n", RQSTP
);
424 write_program (const definition
* def
, const char *storage
)
430 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
432 f_print (fout
, "\n");
435 f_print (fout
, "%s ", storage
);
437 f_print (fout
, "void\n");
438 pvname (def
->def_name
, vp
->vers_num
);
442 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
443 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
447 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
448 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
449 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
452 f_print (fout
, "{\n");
455 f_print (fout
, "\tunion {\n");
456 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
458 if (proc
->arg_num
< 2)
459 { /* single argument */
460 if (streq (proc
->args
.decls
->decl
.type
,
466 f_print (fout
, "\t\t");
467 ptype (proc
->args
.decls
->decl
.prefix
,
468 proc
->args
.decls
->decl
.type
, 0);
469 pvname (proc
->proc_name
, vp
->vers_num
);
470 f_print (fout
, "_arg;\n");
476 f_print (fout
, "\t\t%s", proc
->args
.argname
);
478 pvname (proc
->proc_name
, vp
->vers_num
);
479 f_print (fout
, "_arg;\n");
484 f_print (fout
, "\t\tint fill;\n");
486 f_print (fout
, "\t} %s;\n", ARG
);
489 f_print(fout
, "\tunion {\n");
490 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
491 if (!streq (proc
->res_type
, "void"))
493 f_print(fout
, "\t\t");
494 ptype(proc
->res_prefix
, proc
->res_type
, 0);
495 pvname(proc
->proc_name
, vp
->vers_num
);
496 f_print(fout
, "_res;\n");
498 f_print(fout
, "\t} %s;\n", RESULT
);
499 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
502 f_print (fout
, "\tchar *%s;\n", RESULT
);
506 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
509 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
512 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
517 f_print (fout
, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG
, RESULT
);
519 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
521 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
523 f_print (fout
, "\n");
527 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
531 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
532 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
534 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
538 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
539 if (!nullproc (vp
->procs
))
541 f_print (fout
, "\tcase NULLPROC:\n");
544 ? "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
545 : "\t\t(void) svc_sendreply (%s, xdr_void, (char *)NULL);\n",
547 print_return ("\t\t");
548 f_print (fout
, "\n");
550 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
552 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
553 if (proc
->arg_num
< 2)
554 { /* single argument */
555 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
559 p_xdrfunc (ARG
, proc
->args
.argname
);
561 p_xdrfunc (RESULT
, proc
->res_type
);
566 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
570 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
575 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
577 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
580 { /* new style: calls internal routine */
583 if ((Cflag
|| mtflag
) && !newstyle
)
584 pvname_svc (proc
->proc_name
, vp
->vers_num
);
586 pvname (proc
->proc_name
, vp
->vers_num
);
587 f_print (fout
, ";\n");
588 f_print (fout
, "\t\tbreak;\n\n");
590 f_print (fout
, "\tdefault:\n");
591 printerr ("noproc", TRANSP
);
592 print_return ("\t\t");
593 f_print (fout
, "\t}\n");
595 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
597 printif ("getargs", TRANSP
, "(caddr_t) &", ARG
);
599 printif ("getargs", TRANSP
, "&", ARG
);
600 printerr ("decode", TRANSP
);
601 print_return ("\t\t");
602 f_print (fout
, "\t}\n");
607 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
608 RESULT
, ROUTINE
, ARG
, RQSTP
);
610 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
611 RESULT
, ROUTINE
, ARG
, RQSTP
);
615 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
616 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
618 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
619 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
622 "\tif (%s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
623 RETVAL
, TRANSP
, RESULT
, RESULT
);
626 "\tif (%s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n",
627 RESULT
, TRANSP
, RESULT
, RESULT
);
629 printerr ("systemerr", TRANSP
);
630 f_print (fout
, "\t}\n");
633 printif ("freeargs", TRANSP
, "(caddr_t) &", ARG
);
635 printif ("freeargs", TRANSP
, "&", ARG
);
637 sprintf (_errbuf
, "unable to free arguments");
638 print_err_message ("\t\t");
639 f_print (fout
, "\t\texit (1);\n");
640 f_print (fout
, "\t}\n");
641 /* print out free routine */
644 f_print(fout
,"\tif (!");
645 pvname(def
->def_name
, vp
->vers_num
);
646 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
647 TRANSP
, RESULT
, RESULT
);
648 (void) sprintf(_errbuf
, "unable to free results");
649 print_err_message("\t\t");
653 f_print (fout
, "}\n");
658 printerr (const char *err
, const char *transp
)
660 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
664 printif (const char *proc
, const char *transp
, const char *prefix
,
667 f_print (fout
, "\tif (!svc_%s (%s, _xdr_%s, %s%s)) {\n",
668 proc
, transp
, arg
, prefix
, arg
);
672 nullproc (const proc_list
* proc
)
674 for (; proc
!= NULL
; proc
= proc
->next
)
676 if (streq (proc
->proc_num
, "0"))
685 write_inetmost (const char *infile
)
687 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
688 f_print (fout
, "\tint sock;\n");
689 f_print (fout
, "\tint proto;\n");
690 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
691 f_print (fout
, "\tint asize = sizeof (saddr);\n");
692 f_print (fout
, "\n");
694 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
695 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
696 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
697 f_print (fout
, "\t\t\texit (1);\n");
698 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
699 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
700 f_print (fout
, "\t\t\texit (1);\n");
701 f_print (fout
, "\t\tsock = 0;\n");
702 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
703 f_print (fout
, "\t\tproto = 0;\n");
704 open_log_file (infile
, "\t\t");
705 f_print (fout
, "\t} else {\n");
706 write_rpc_svc_fg (infile
, "\t\t");
707 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
708 print_pmapunset ("\t\t");
709 f_print (fout
, "\t}\n");
713 print_return (const char *space
)
716 f_print (fout
, "%sexit (0);\n", space
);
722 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
725 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
726 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
728 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
731 f_print (fout
, "%sreturn;\n", space
);
736 print_pmapunset (const char *space
)
742 for (l
= defined
; l
!= NULL
; l
= l
->next
)
744 def
= (definition
*) l
->val
;
745 if (def
->def_kind
== DEF_PROGRAM
)
747 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
750 f_print (fout
, "%spmap_unset (%s, %s);\n",
751 space
, def
->def_name
, vp
->vers_name
);
758 print_err_message (const char *space
)
761 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
762 else if (inetdflag
|| pmflag
)
763 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
765 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
769 * Write the server auxiliary function ( _msgout, timeout)
772 write_svc_aux (int nomain
)
777 write_timeout_func ();
781 * Write the _msgout function
787 f_print (fout
, "\n");
788 f_print (fout
, "static\n");
791 f_print (fout
, "void _msgout (msg)\n");
792 f_print (fout
, "\tchar *msg;\n");
796 f_print (fout
, "void _msgout (char* msg)\n");
798 f_print (fout
, "{\n");
799 f_print (fout
, "#ifdef RPC_SVC_FG\n");
800 if (inetdflag
|| pmflag
)
801 f_print (fout
, "\tif (_rpcpmstart)\n");
802 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
803 f_print (fout
, "\telse\n");
804 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
805 f_print (fout
, "#else\n");
806 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
807 f_print (fout
, "#endif\n");
808 f_print (fout
, "}\n");
812 * Write the timeout function
815 write_timeout_func (void)
819 f_print (fout
, "\n");
820 f_print (fout
, "static void\n");
822 f_print (fout
, "closedown (int sig)\n");
824 f_print (fout
, "closedown (sig)\n\tint sig;\n");
825 f_print (fout
, "{\n");
827 #if defined (__GNU_LIBRARY__) && 0
828 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
829 Cflag
? "(SIG_PF)" : "(void(*)())");
832 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
834 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
836 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
838 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
839 f_print (fout
, "\t\tstatic int size;\n");
840 f_print (fout
, "\t\tint i, openfd;\n");
841 if (tirpcflag
&& pmflag
)
843 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
844 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
848 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
850 f_print (fout
, "\t\t\texit (0);\n");
851 f_print (fout
, "\t\tif (size == 0) {\n");
854 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
855 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
856 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
857 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
859 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
860 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
864 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
866 f_print (fout
, "\t\t}\n");
867 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
868 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
869 f_print (fout
, "\t\t\t\topenfd++;\n");
870 f_print (fout
, "\t\tif (openfd <= 1)\n");
871 f_print (fout
, "\t\t\texit (0);\n");
872 f_print (fout
, "\t}\n");
873 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
874 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
876 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
877 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
878 Cflag
? "(SIG_PF)" : "(void(*)())");
879 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
880 f_print (fout
, "}\n");
884 * Write the most of port monitor support
887 write_pm_most (const char *infile
, int netflag
)
893 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
894 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
895 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
896 f_print (fout
, "\t\tchar *netid;\n");
898 { /* Not included by -n option */
899 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
900 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
903 f_print (fout
, "\t\tint pmclose;\n");
904 /* not necessary, defined in /usr/include/stdlib */
905 /* f_print(fout, "\t\textern char *getenv();\n"); */
906 f_print (fout
, "\n");
907 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
909 open_log_file (infile
, "\t\t");
910 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
911 sprintf (_errbuf
, "cannot get transport name");
912 print_err_message ("\t\t\t");
913 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
914 sprintf (_errbuf
, "cannot get transport info");
915 print_err_message ("\t\t\t");
916 f_print (fout
, "\t\t}\n");
918 * A kludgy support for inetd services. Inetd only works with
919 * sockmod, and RPC works only with timod, hence all this jugglery
921 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
922 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
923 sprintf (_errbuf
, "could not get the right module");
924 print_err_message ("\t\t\t\t");
925 f_print (fout
, "\t\t\t\texit(1);\n");
926 f_print (fout
, "\t\t\t}\n");
927 f_print (fout
, "\t\t}\n");
929 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
930 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
932 sprintf (_errbuf
, "cannot create server handle");
933 print_err_message ("\t\t\t");
934 f_print (fout
, "\t\t\texit(1);\n");
935 f_print (fout
, "\t\t}\n");
936 f_print (fout
, "\t\tif (nconf)\n");
937 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
938 for (l
= defined
; l
!= NULL
; l
= l
->next
)
940 def
= (definition
*) l
->val
;
941 if (def
->def_kind
!= DEF_PROGRAM
)
945 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
948 "\t\tif (!svc_reg(%s, %s, %s, ",
949 TRANSP
, def
->def_name
, vp
->vers_name
);
950 pvname (def
->def_name
, vp
->vers_num
);
951 f_print (fout
, ", 0)) {\n");
952 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
953 def
->def_name
, vp
->vers_name
);
954 print_err_message ("\t\t\t");
955 f_print (fout
, "\t\t\texit(1);\n");
956 f_print (fout
, "\t\t}\n");
961 f_print (fout
, "\t\tif (pmclose) {\n");
962 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
963 Cflag
? "(SIG_PF)" : "(void(*)())");
964 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
965 f_print (fout
, "\t\t}\n");
967 f_print (fout
, "\t\tsvc_run();\n");
968 f_print (fout
, "\t\texit(1);\n");
969 f_print (fout
, "\t\t/* NOTREACHED */\n");
970 f_print (fout
, "\t}\n");
974 * Support for backgrounding the server if self started.
977 write_rpc_svc_fg (const char *infile
, const char *sp
)
979 f_print (fout
, "#ifndef RPC_SVC_FG\n");
980 f_print (fout
, "%sint size;\n", sp
);
982 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
984 f_print (fout
, "%sint pid, i;\n\n", sp
);
985 f_print (fout
, "%spid = fork();\n", sp
);
986 f_print (fout
, "%sif (pid < 0) {\n", sp
);
987 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
988 f_print (fout
, "%s\texit(1);\n", sp
);
989 f_print (fout
, "%s}\n", sp
);
990 f_print (fout
, "%sif (pid)\n", sp
);
991 f_print (fout
, "%s\texit(0);\n", sp
);
992 /* get number of file descriptors */
995 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
996 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
997 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
998 f_print (fout
, "%s\texit(1);\n", sp
);
1002 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
1005 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
1006 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1007 /* Redirect stderr and stdout to console */
1008 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
1009 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
1010 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
1011 /* This removes control of the controlling terminal */
1013 f_print (fout
, "%ssetsid();\n", sp
);
1016 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1017 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1018 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1019 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1020 f_print (fout
, "%s}\n", sp
);
1023 open_log_file (infile
, sp
);
1024 f_print (fout
, "#endif\n");
1026 open_log_file (infile
, sp
);
1030 open_log_file (const char *infile
, const char *sp
)
1034 s
= strrchr (infile
, '.');
1037 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1043 * write a registration for the given transport for Inetd
1046 write_inetd_register (const char *transp
)
1059 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1063 f_print (fout
, "\n");
1066 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1067 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1069 f_print (fout
, "%s\t%s = svc%s_create(%s",
1070 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1072 f_print (fout
, ", 0, 0");
1073 f_print (fout
, ");\n");
1074 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1075 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1076 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1077 print_err_message (tmpbuf
);
1078 f_print (fout
, "%s\t\texit(1);\n", sp
);
1079 f_print (fout
, "%s\t}\n", sp
);
1083 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1084 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1085 sp
, isudp
? "UDP" : "TCP");
1087 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1089 def
= (definition
*) l
->val
;
1090 if (def
->def_kind
!= DEF_PROGRAM
)
1094 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1096 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1097 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1098 pvname (def
->def_name
, vp
->vers_num
);
1100 f_print (fout
, ", proto)) {\n");
1102 f_print (fout
, ", IPPROTO_%s)) {\n",
1103 isudp
? "UDP" : "TCP");
1104 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1105 def
->def_name
, vp
->vers_name
, transp
);
1106 print_err_message (tmpbuf
);
1107 f_print (fout
, "%s\t\texit(1);\n", sp
);
1108 f_print (fout
, "%s\t}\n", sp
);
1112 f_print (fout
, "\t}\n");