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\txdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
76 stringfix (typename
));
78 f_print (fout
, "\t\txdr_%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");
606 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
607 RESULT
, ROUTINE
, ARG
, RQSTP
);
609 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
610 RESULT
, ROUTINE
, ARG
, RQSTP
);
613 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
614 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
616 f_print(fout
, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
617 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
620 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
621 RETVAL
, TRANSP
, RESULT
, RESULT
);
624 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
625 RESULT
, TRANSP
, RESULT
, RESULT
);
627 printerr ("systemerr", TRANSP
);
628 f_print (fout
, "\t}\n");
631 printif ("freeargs", TRANSP
, "(caddr_t) &", ARG
);
633 printif ("freeargs", TRANSP
, "&", ARG
);
635 sprintf (_errbuf
, "unable to free arguments");
636 print_err_message ("\t\t");
637 f_print (fout
, "\t\texit (1);\n");
638 f_print (fout
, "\t}\n");
639 /* print out free routine */
642 f_print(fout
,"\tif (!");
643 pvname(def
->def_name
, vp
->vers_num
);
644 f_print(fout
,"_freeresult (%s, xdr_%s, (caddr_t) &%s))\n",
645 TRANSP
, RESULT
, RESULT
);
646 (void) sprintf(_errbuf
, "unable to free results");
647 print_err_message("\t\t");
651 f_print (fout
, "}\n");
656 printerr (const char *err
, const char *transp
)
658 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
662 printif (const char *proc
, const char *transp
, const char *prefix
,
665 f_print (fout
, "\tif (!svc_%s (%s, xdr_%s, %s%s)) {\n",
666 proc
, transp
, arg
, prefix
, arg
);
670 nullproc (const proc_list
* proc
)
672 for (; proc
!= NULL
; proc
= proc
->next
)
674 if (streq (proc
->proc_num
, "0"))
683 write_inetmost (const char *infile
)
685 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
686 f_print (fout
, "\tint sock;\n");
687 f_print (fout
, "\tint proto;\n");
688 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
689 f_print (fout
, "\tint asize = sizeof (saddr);\n");
690 f_print (fout
, "\n");
692 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
693 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
694 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
695 f_print (fout
, "\t\t\texit (1);\n");
696 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
697 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
698 f_print (fout
, "\t\t\texit (1);\n");
699 f_print (fout
, "\t\tsock = 0;\n");
700 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
701 f_print (fout
, "\t\tproto = 0;\n");
702 open_log_file (infile
, "\t\t");
703 f_print (fout
, "\t} else {\n");
704 write_rpc_svc_fg (infile
, "\t\t");
705 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
706 print_pmapunset ("\t\t");
707 f_print (fout
, "\t}\n");
711 print_return (const char *space
)
714 f_print (fout
, "%sexit (0);\n", space
);
720 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
723 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
724 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
726 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
729 f_print (fout
, "%sreturn;\n", space
);
734 print_pmapunset (const char *space
)
740 for (l
= defined
; l
!= NULL
; l
= l
->next
)
742 def
= (definition
*) l
->val
;
743 if (def
->def_kind
== DEF_PROGRAM
)
745 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
748 f_print (fout
, "%spmap_unset (%s, %s);\n",
749 space
, def
->def_name
, vp
->vers_name
);
756 print_err_message (const char *space
)
759 f_print (fout
, "%ssyslog (LOG_ERR, \"%s\");\n", space
, _errbuf
);
760 else if (inetdflag
|| pmflag
)
761 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
763 f_print (fout
, "%sfprintf (stderr, \"%s\");\n", space
, _errbuf
);
767 * Write the server auxiliary function ( _msgout, timeout)
770 write_svc_aux (int nomain
)
775 write_timeout_func ();
779 * Write the _msgout function
785 f_print (fout
, "\n");
786 f_print (fout
, "static\n");
789 f_print (fout
, "void _msgout (msg)\n");
790 f_print (fout
, "\tchar *msg;\n");
794 f_print (fout
, "void _msgout (char* msg)\n");
796 f_print (fout
, "{\n");
797 f_print (fout
, "#ifdef RPC_SVC_FG\n");
798 if (inetdflag
|| pmflag
)
799 f_print (fout
, "\tif (_rpcpmstart)\n");
800 f_print (fout
, "\t\tsyslog (LOG_ERR, msg);\n");
801 f_print (fout
, "\telse\n");
802 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
803 f_print (fout
, "#else\n");
804 f_print (fout
, "\tsyslog (LOG_ERR, msg);\n");
805 f_print (fout
, "#endif\n");
806 f_print (fout
, "}\n");
810 * Write the timeout function
813 write_timeout_func (void)
817 f_print (fout
, "\n");
818 f_print (fout
, "static void\n");
820 f_print (fout
, "closedown (int sig)\n");
822 f_print (fout
, "closedown (sig)\n\tint sig;\n");
823 f_print (fout
, "{\n");
825 #if defined (__GNU_LIBRARY__) && 0
826 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
827 Cflag
? "(SIG_PF)" : "(void(*)())");
830 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
832 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
834 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
836 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
837 f_print (fout
, "\t\tstatic int size;\n");
838 f_print (fout
, "\t\tint i, openfd;\n");
839 if (tirpcflag
&& pmflag
)
841 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
842 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
846 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
848 f_print (fout
, "\t\t\texit (0);\n");
849 f_print (fout
, "\t\tif (size == 0) {\n");
852 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
853 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
854 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
855 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
857 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
858 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
862 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
864 f_print (fout
, "\t\t}\n");
865 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
866 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
867 f_print (fout
, "\t\t\t\topenfd++;\n");
868 f_print (fout
, "\t\tif (openfd <= 1)\n");
869 f_print (fout
, "\t\t\texit (0);\n");
870 f_print (fout
, "\t}\n");
871 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
872 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
874 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
875 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
876 Cflag
? "(SIG_PF)" : "(void(*)())");
877 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
878 f_print (fout
, "}\n");
882 * Write the most of port monitor support
885 write_pm_most (const char *infile
, int netflag
)
891 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
892 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
893 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
894 f_print (fout
, "\t\tchar *netid;\n");
896 { /* Not included by -n option */
897 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
898 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
901 f_print (fout
, "\t\tint pmclose;\n");
902 /* not necessary, defined in /usr/include/stdlib */
903 /* f_print(fout, "\t\textern char *getenv();\n"); */
904 f_print (fout
, "\n");
905 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
907 open_log_file (infile
, "\t\t");
908 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
909 sprintf (_errbuf
, "cannot get transport name");
910 print_err_message ("\t\t\t");
911 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
912 sprintf (_errbuf
, "cannot get transport info");
913 print_err_message ("\t\t\t");
914 f_print (fout
, "\t\t}\n");
916 * A kludgy support for inetd services. Inetd only works with
917 * sockmod, and RPC works only with timod, hence all this jugglery
919 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
920 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
921 sprintf (_errbuf
, "could not get the right module");
922 print_err_message ("\t\t\t\t");
923 f_print (fout
, "\t\t\t\texit(1);\n");
924 f_print (fout
, "\t\t\t}\n");
925 f_print (fout
, "\t\t}\n");
927 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
928 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
930 sprintf (_errbuf
, "cannot create server handle");
931 print_err_message ("\t\t\t");
932 f_print (fout
, "\t\t\texit(1);\n");
933 f_print (fout
, "\t\t}\n");
934 f_print (fout
, "\t\tif (nconf)\n");
935 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
936 for (l
= defined
; l
!= NULL
; l
= l
->next
)
938 def
= (definition
*) l
->val
;
939 if (def
->def_kind
!= DEF_PROGRAM
)
943 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
946 "\t\tif (!svc_reg(%s, %s, %s, ",
947 TRANSP
, def
->def_name
, vp
->vers_name
);
948 pvname (def
->def_name
, vp
->vers_num
);
949 f_print (fout
, ", 0)) {\n");
950 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
951 def
->def_name
, vp
->vers_name
);
952 print_err_message ("\t\t\t");
953 f_print (fout
, "\t\t\texit(1);\n");
954 f_print (fout
, "\t\t}\n");
959 f_print (fout
, "\t\tif (pmclose) {\n");
960 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
961 Cflag
? "(SIG_PF)" : "(void(*)())");
962 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
963 f_print (fout
, "\t\t}\n");
965 f_print (fout
, "\t\tsvc_run();\n");
966 f_print (fout
, "\t\texit(1);\n");
967 f_print (fout
, "\t\t/* NOTREACHED */\n");
968 f_print (fout
, "\t}\n");
972 * Support for backgrounding the server if self started.
975 write_rpc_svc_fg (const char *infile
, const char *sp
)
977 f_print (fout
, "#ifndef RPC_SVC_FG\n");
978 f_print (fout
, "%sint size;\n", sp
);
980 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
982 f_print (fout
, "%sint pid, i;\n\n", sp
);
983 f_print (fout
, "%spid = fork();\n", sp
);
984 f_print (fout
, "%sif (pid < 0) {\n", sp
);
985 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
986 f_print (fout
, "%s\texit(1);\n", sp
);
987 f_print (fout
, "%s}\n", sp
);
988 f_print (fout
, "%sif (pid)\n", sp
);
989 f_print (fout
, "%s\texit(0);\n", sp
);
990 /* get number of file descriptors */
993 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
994 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
995 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
996 f_print (fout
, "%s\texit(1);\n", sp
);
1000 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
1003 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
1004 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1005 /* Redirect stderr and stdout to console */
1006 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
1007 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
1008 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
1009 /* This removes control of the controlling terminal */
1011 f_print (fout
, "%ssetsid();\n", sp
);
1014 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1015 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1016 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1017 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1018 f_print (fout
, "%s}\n", sp
);
1021 open_log_file (infile
, sp
);
1022 f_print (fout
, "#endif\n");
1024 open_log_file (infile
, sp
);
1028 open_log_file (const char *infile
, const char *sp
)
1032 s
= strrchr (infile
, '.');
1035 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1041 * write a registration for the given transport for Inetd
1044 write_inetd_register (const char *transp
)
1057 if (streq (transp
, "udp"))
1061 f_print (fout
, "\n");
1064 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1065 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1067 f_print (fout
, "%s\t%s = svc%s_create(%s",
1068 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1070 f_print (fout
, ", 0, 0");
1071 f_print (fout
, ");\n");
1072 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1073 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1074 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1075 print_err_message (tmpbuf
);
1076 f_print (fout
, "%s\t\texit(1);\n", sp
);
1077 f_print (fout
, "%s\t}\n", sp
);
1081 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1082 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1083 sp
, isudp
? "UDP" : "TCP");
1085 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1087 def
= (definition
*) l
->val
;
1088 if (def
->def_kind
!= DEF_PROGRAM
)
1092 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1094 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1095 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1096 pvname (def
->def_name
, vp
->vers_num
);
1098 f_print (fout
, ", proto)) {\n");
1100 f_print (fout
, ", IPPROTO_%s)) {\n",
1101 isudp
? "UDP" : "TCP");
1102 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1103 def
->def_name
, vp
->vers_name
, transp
);
1104 print_err_message (tmpbuf
);
1105 f_print (fout
, "%s\t\texit(1);\n", sp
);
1106 f_print (fout
, "%s\t}\n", sp
);
1110 f_print (fout
, "\t}\n");