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
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static const char svcout_rcsid
[] =
40 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
44 #include "rpc_parse.h"
48 static const char RQSTP
[] = "rqstp";
49 static const char TRANSP
[] = "transp";
50 static const char ARG
[] = "argument";
51 static const char RESULT
[] = "result";
52 static const char ROUTINE
[] = "local";
53 static char RETVAL
[] = "retval";
55 char _errbuf
[256]; /* For all messages */
57 static void internal_proctype (const proc_list
* plist
);
58 static void p_xdrfunc (const char *rname
, const char *typename
);
59 static void write_real_program (const definition
* def
);
60 static void write_program (const definition
* def
, const char *storage
);
61 static void printerr (const char *err
, const char *transp
);
62 static void printif (const char *proc
, const char *transp
, const char *arg
);
63 static void write_inetmost (const char *infile
);
64 static void print_return (const char *space
);
65 static void print_pmapunset (const char *space
);
66 static void print_err_message (const char *space
);
67 static void write_timeout_func (void);
68 static void write_pm_most (const char *infile
, int netflag
);
69 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
70 static void open_log_file (const char *infile
, const char *sp
);
73 p_xdrfunc (const char *rname
, const char *typename
)
76 f_print (fout
, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
77 stringfix (typename
));
79 f_print (fout
, "\t\t_xdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
83 internal_proctype (const proc_list
* plist
)
85 f_print (fout
, "static ");
86 ptype (plist
->res_prefix
, plist
->res_type
, 1);
92 * write most of the service, that is, everything but the registrations.
95 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
97 if (inetdflag
|| pmflag
)
100 #ifdef __GNU_LIBRARY__
102 var_type
= (nomain
? "extern" : "");
104 var_type
= (nomain
? "extern" : "static");
106 f_print (fout
, "%s int _rpcpmstart;", var_type
);
107 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
110 f_print (fout
, "%s int _rpcfdtype;", var_type
);
111 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
116 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
117 f_print (fout
, "\t/* Still serving ? */\n");
119 f_print(fout
, " /* States a server can be in wrt request */\n\n");
120 f_print(fout
, "#define\t_IDLE 0\n");
121 f_print(fout
, "#define\t_SERVED 1\n");
122 f_print(fout
, "#define\t_SERVING 2\n\n");
123 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
124 f_print(fout
, "\t /* Set when a request is serviced */\n");
128 f_print (fout
, "mutex_t _svcstate_lock;");
130 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
134 write_svc_aux (nomain
);
136 /* write out dispatcher and stubs */
137 write_programs (nomain
? NULL
: "static");
142 #ifdef __GNU_LIBRARY__
144 f_print (fout
, "\nint\nmain (int argc, char **argv)\n");
147 f_print (fout
, "\nint\nmain (argc, argv)\n");
148 f_print (fout
, "\tint argc;\n");
149 f_print (fout
, "\tchar **argv;\n");
152 f_print (fout
, "\nmain()\n");
154 f_print (fout
, "{\n");
157 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
165 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
166 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
168 f_print (fout
, "\tpid_t pid;\n");
169 f_print (fout
, "\tint i;\n");
170 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
172 if (mtflag
& timerflag
)
174 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
176 write_pm_most (infile
, netflag
);
177 f_print (fout
, "\telse {\n");
178 write_rpc_svc_fg (infile
, "\t\t");
179 f_print (fout
, "\t}\n");
183 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
184 f_print (fout
, "\n");
185 print_pmapunset ("\t");
189 if (logflag
&& !inetdflag
)
191 open_log_file (infile
, "\t");
196 * write a registration for the given transport
199 write_netid_register (const char *transp
)
208 f_print (fout
, "\n");
209 f_print (fout
, "%s\tnconf = getnetconfigent (\"%s\");\n", sp
, transp
);
210 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
211 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
212 sprintf (tmpbuf
, "%s\t\t", sp
);
213 print_err_message (tmpbuf
);
214 f_print (fout
, "%s\t\texit (1);\n", sp
);
215 f_print (fout
, "%s\t}\n", sp
);
216 f_print (fout
, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
217 sp
, TRANSP
/*, transp *//* ?!?... */ );
218 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
219 sprintf (_errbuf
, "cannot create %s service.", transp
);
220 print_err_message (tmpbuf
);
221 f_print (fout
, "%s\t\texit (1);\n", sp
);
222 f_print (fout
, "%s\t}\n", sp
);
224 for (l
= defined
; l
!= NULL
; l
= l
->next
)
226 def
= (definition
*) l
->val
;
227 if (def
->def_kind
!= DEF_PROGRAM
)
231 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
233 f_print (fout
, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
234 sp
, def
->def_name
, vp
->vers_name
);
235 f_print (fout
, "%s\tif (!svc_reg (%s, %s, %s, ",
236 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
237 pvname (def
->def_name
, vp
->vers_num
);
238 f_print (fout
, ", nconf)) {\n");
239 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
240 def
->def_name
, vp
->vers_name
, transp
);
241 print_err_message (tmpbuf
);
242 f_print (fout
, "%s\t\texit (1);\n", sp
);
243 f_print (fout
, "%s\t}\n", sp
);
246 f_print (fout
, "%s\tfreenetconfigent (nconf);\n", sp
);
250 * write a registration for the given transport for TLI
253 write_nettype_register (const char *transp
)
259 for (l
= defined
; l
!= NULL
; l
= l
->next
)
261 def
= (definition
*) l
->val
;
262 if (def
->def_kind
!= DEF_PROGRAM
)
266 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
268 f_print (fout
, "\tif (!svc_create (");
269 pvname (def
->def_name
, vp
->vers_num
);
270 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
271 def
->def_name
, vp
->vers_name
, transp
);
272 (void) sprintf (_errbuf
,
273 "unable to create (%s, %s) for %s.",
274 def
->def_name
, vp
->vers_name
, transp
);
275 print_err_message ("\t\t");
276 f_print (fout
, "\t\texit (1);\n");
277 f_print (fout
, "\t}\n");
283 * write the rest of the service
288 f_print (fout
, "\n");
291 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
292 (void) sprintf (_errbuf
, "could not create a handle");
293 print_err_message ("\t\t");
294 f_print (fout
, "\t\texit (1);\n");
295 f_print (fout
, "\t}\n");
298 f_print (fout
, "\tif (_rpcpmstart) {\n");
300 "\t\t(void) signal (SIGALRM, %s closedown);\n",
301 Cflag
? "(SIG_PF)" : "(void(*)())");
302 f_print (fout
, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
303 f_print (fout
, "\t}\n");
306 f_print (fout
, "\tsvc_run ();\n");
307 (void) sprintf (_errbuf
, "svc_run returned");
308 print_err_message ("\t");
309 f_print (fout
, "\texit (1);\n");
310 f_print (fout
, "\t/* NOTREACHED */\n");
311 f_print (fout
, "}\n");
315 write_programs (const char *storage
)
320 /* write out stubs for procedure definitions */
321 for (l
= defined
; l
!= NULL
; l
= l
->next
)
323 def
= (definition
*) l
->val
;
324 if (def
->def_kind
== DEF_PROGRAM
)
326 write_real_program (def
);
330 /* write out dispatcher for each program */
331 for (l
= defined
; l
!= NULL
; l
= l
->next
)
333 def
= (definition
*) l
->val
;
334 if (def
->def_kind
== DEF_PROGRAM
)
336 write_program (def
, storage
);
341 /* write out definition of internal function (e.g. _printmsg_1(...))
342 which calls server's defintion of actual function (e.g. printmsg_1(...)).
343 Unpacks single user argument of printmsg_1 to call-by-value format
344 expected by printmsg_1. */
346 write_real_program (const definition
* def
)
353 return; /* not needed for old style */
354 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
356 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
358 fprintf (fout
, "\n");
360 internal_proctype (proc
);
362 f_print (fout
, "int");
363 f_print (fout
, "\n_");
364 pvname (proc
->proc_name
, vp
->vers_num
);
367 f_print (fout
, " (");
369 if (proc
->arg_num
> 1)
370 f_print (fout
, proc
->args
.argname
);
372 ptype (proc
->args
.decls
->decl
.prefix
,
373 proc
->args
.decls
->decl
.type
, 0);
376 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
380 f_print (fout
, " *argp, struct svc_req *%s)\n",
386 f_print(fout
, " (argp, %s, %s)\n", RESULT
, RQSTP
);
388 f_print (fout
, " (argp, %s)\n", RQSTP
);
390 if (proc
->arg_num
> 1)
391 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
394 f_print (fout
, "\t");
395 ptype (proc
->args
.decls
->decl
.prefix
,
396 proc
->args
.decls
->decl
.type
, 0);
397 f_print (fout
, " *argp;\n");
399 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
402 f_print (fout
, "{\n");
403 f_print (fout
, "\treturn (");
405 pvname_svc (proc
->proc_name
, vp
->vers_num
);
407 pvname (proc
->proc_name
, vp
->vers_num
);
409 if (proc
->arg_num
< 2)
410 { /* single argument */
411 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
412 f_print (fout
, "*argp, "); /* non-void */
416 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
417 f_print (fout
, "argp->%s, ", l
->decl
.name
);
420 f_print (fout
, "%s, ", RESULT
);
421 f_print (fout
, "%s));\n}\n", RQSTP
);
427 write_program (const definition
* def
, const char *storage
)
433 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
435 f_print (fout
, "\n");
438 f_print (fout
, "%s ", storage
);
440 f_print (fout
, "void\n");
441 pvname (def
->def_name
, vp
->vers_num
);
445 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
446 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
450 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
451 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
452 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
455 f_print (fout
, "{\n");
458 f_print (fout
, "\tunion {\n");
459 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
461 if (proc
->arg_num
< 2)
462 { /* single argument */
463 if (streq (proc
->args
.decls
->decl
.type
,
469 f_print (fout
, "\t\t");
470 ptype (proc
->args
.decls
->decl
.prefix
,
471 proc
->args
.decls
->decl
.type
, 0);
472 pvname (proc
->proc_name
, vp
->vers_num
);
473 f_print (fout
, "_arg;\n");
479 f_print (fout
, "\t\t%s", proc
->args
.argname
);
481 pvname (proc
->proc_name
, vp
->vers_num
);
482 f_print (fout
, "_arg;\n");
487 f_print (fout
, "\t\tint fill;\n");
489 f_print (fout
, "\t} %s;\n", ARG
);
492 f_print(fout
, "\tunion {\n");
493 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
494 if (!streq (proc
->res_type
, "void"))
496 f_print(fout
, "\t\t");
497 ptype(proc
->res_prefix
, proc
->res_type
, 0);
498 pvname(proc
->proc_name
, vp
->vers_num
);
499 f_print(fout
, "_res;\n");
501 f_print(fout
, "\t} %s;\n", RESULT
);
502 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
505 f_print (fout
, "\tchar *%s;\n", RESULT
);
509 f_print (fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG
, RESULT
);
512 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
515 f_print (fout
, "\tchar *(*%s)(char *, struct svc_req *);\n",
520 f_print (fout
, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG
, RESULT
);
522 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
524 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
526 f_print (fout
, "\n");
530 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
534 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
535 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
537 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
541 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
542 if (!nullproc (vp
->procs
))
544 f_print (fout
, "\tcase NULLPROC:\n");
546 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
548 print_return ("\t\t");
549 f_print (fout
, "\n");
551 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
553 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
554 if (proc
->arg_num
< 2)
555 { /* single argument */
556 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
560 p_xdrfunc (ARG
, proc
->args
.argname
);
562 p_xdrfunc (RESULT
, proc
->res_type
);
567 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
571 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
576 f_print(fout
, "\t\t%s = (bool_t (*)()) ", ROUTINE
);
578 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
581 { /* new style: calls internal routine */
584 if ((Cflag
|| mtflag
) && !newstyle
)
585 pvname_svc (proc
->proc_name
, vp
->vers_num
);
587 pvname (proc
->proc_name
, vp
->vers_num
);
588 f_print (fout
, ";\n");
589 f_print (fout
, "\t\tbreak;\n\n");
591 f_print (fout
, "\tdefault:\n");
592 printerr ("noproc", TRANSP
);
593 print_return ("\t\t");
594 f_print (fout
, "\t}\n");
596 f_print (fout
, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
597 printif ("getargs", TRANSP
, ARG
);
598 printerr ("decode", TRANSP
);
599 print_return ("\t\t");
600 f_print (fout
, "\t}\n");
605 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
606 RESULT
, ROUTINE
, ARG
, RQSTP
);
608 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
609 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, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
621 RETVAL
, TRANSP
, RESULT
, RESULT
);
624 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
625 RESULT
, TRANSP
, RESULT
, RESULT
);
627 printerr ("systemerr", TRANSP
);
628 f_print (fout
, "\t}\n");
630 printif ("freeargs", TRANSP
, ARG
);
632 sprintf (_errbuf
, "unable to free arguments");
633 print_err_message ("\t\t");
634 f_print (fout
, "\t\texit (1);\n");
635 f_print (fout
, "\t}\n");
636 /* print out free routine */
639 f_print(fout
,"\tif (!");
640 pvname(def
->def_name
, vp
->vers_num
);
641 f_print(fout
,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
642 TRANSP
, RESULT
, RESULT
);
643 (void) sprintf(_errbuf
, "unable to free results");
644 print_err_message("\t\t");
648 f_print (fout
, "}\n");
653 printerr (const char *err
, const char *transp
)
655 f_print (fout
, "\t\tsvcerr_%s (%s);\n", err
, transp
);
659 printif (const char *proc
, const char *transp
, const char *arg
)
661 f_print (fout
, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
662 proc
, transp
, arg
, arg
);
666 nullproc (const proc_list
* proc
)
668 for (; proc
!= NULL
; proc
= proc
->next
)
670 if (streq (proc
->proc_num
, "0"))
679 write_inetmost (const char *infile
)
681 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
682 f_print (fout
, "\tint sock;\n");
683 f_print (fout
, "\tint proto;\n");
684 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
685 f_print (fout
, "\tint asize = sizeof (saddr);\n");
686 f_print (fout
, "\n");
688 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
689 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
690 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
691 f_print (fout
, "\t\t\texit (1);\n");
692 f_print (fout
, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
693 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
694 f_print (fout
, "\t\t\texit (1);\n");
695 f_print (fout
, "\t\tsock = 0;\n");
696 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
697 f_print (fout
, "\t\tproto = 0;\n");
698 open_log_file (infile
, "\t\t");
699 f_print (fout
, "\t} else {\n");
700 write_rpc_svc_fg (infile
, "\t\t");
701 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
702 print_pmapunset ("\t\t");
703 f_print (fout
, "\t}\n");
707 print_return (const char *space
)
710 f_print (fout
, "%sexit (0);\n", space
);
716 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
719 f_print(fout
, "%smutex_lock(&_svcstate_lock);\n", space
);
720 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
722 f_print(fout
, "%smutex_unlock(&_svcstate_lock);\n", space
);
725 f_print (fout
, "%sreturn;\n", space
);
730 print_pmapunset (const char *space
)
736 for (l
= defined
; l
!= NULL
; l
= l
->next
)
738 def
= (definition
*) l
->val
;
739 if (def
->def_kind
== DEF_PROGRAM
)
741 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
744 f_print (fout
, "%spmap_unset (%s, %s);\n",
745 space
, def
->def_name
, vp
->vers_name
);
752 print_err_message (const char *space
)
755 f_print (fout
, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space
, _errbuf
);
756 else if (inetdflag
|| pmflag
)
757 f_print (fout
, "%s_msgout (\"%s\");\n", space
, _errbuf
);
759 f_print (fout
, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space
, _errbuf
);
763 * Write the server auxiliary function ( _msgout, timeout)
766 write_svc_aux (int nomain
)
771 write_timeout_func ();
775 * Write the _msgout function
781 f_print (fout
, "\n");
782 f_print (fout
, "static\n");
785 f_print (fout
, "void _msgout (msg)\n");
786 f_print (fout
, "\tchar *msg;\n");
790 f_print (fout
, "void _msgout (char* msg)\n");
792 f_print (fout
, "{\n");
793 f_print (fout
, "#ifdef RPC_SVC_FG\n");
794 if (inetdflag
|| pmflag
)
795 f_print (fout
, "\tif (_rpcpmstart)\n");
796 f_print (fout
, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
797 f_print (fout
, "\telse\n");
798 f_print (fout
, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
799 f_print (fout
, "#else\n");
800 f_print (fout
, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
801 f_print (fout
, "#endif\n");
802 f_print (fout
, "}\n");
806 * Write the timeout function
809 write_timeout_func (void)
813 f_print (fout
, "\n");
814 f_print (fout
, "static void\n");
816 f_print (fout
, "closedown (int sig)\n");
818 f_print (fout
, "closedown (sig)\n\tint sig;\n");
819 f_print (fout
, "{\n");
821 #if defined (__GNU_LIBRARY__) && 0
822 f_print (fout
, "\t(void) signal (sig, %s closedown);\n",
823 Cflag
? "(SIG_PF)" : "(void(*)())");
826 f_print(fout
, "\tmutex_lock(&_svcstate_lock);\n");
828 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
830 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
832 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
833 f_print (fout
, "\t\tstatic int size;\n");
834 f_print (fout
, "\t\tint i, openfd;\n");
835 if (tirpcflag
&& pmflag
)
837 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
838 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
842 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
844 f_print (fout
, "\t\t\texit (0);\n");
845 f_print (fout
, "\t\tif (size == 0) {\n");
848 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
849 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
850 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
851 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
853 f_print(fout
, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
854 f_print (fout
, "\t\t\t\treturn;\n\t\t\t}\n");
858 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
860 f_print (fout
, "\t\t}\n");
861 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
862 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
863 f_print (fout
, "\t\t\t\topenfd++;\n");
864 f_print (fout
, "\t\tif (openfd <= 1)\n");
865 f_print (fout
, "\t\t\texit (0);\n");
866 f_print (fout
, "\t}\n");
867 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
868 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
870 f_print(fout
, "\tmutex_unlock(&_svcstate_lock);\n");
871 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
872 Cflag
? "(SIG_PF)" : "(void(*)())");
873 f_print (fout
, "\talarm (_RPCSVC_CLOSEDOWN);\n");
874 f_print (fout
, "}\n");
878 * Write the most of port monitor support
881 write_pm_most (const char *infile
, int netflag
)
887 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
888 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
889 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
890 f_print (fout
, "\t\tchar *netid;\n");
892 { /* Not included by -n option */
893 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
894 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
897 f_print (fout
, "\t\tint pmclose;\n");
898 /* not necessary, defined in /usr/include/stdlib */
899 /* f_print(fout, "\t\textern char *getenv();\n"); */
900 f_print (fout
, "\n");
901 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
903 open_log_file (infile
, "\t\t");
904 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
905 sprintf (_errbuf
, "cannot get transport name");
906 print_err_message ("\t\t\t");
907 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
908 sprintf (_errbuf
, "cannot get transport info");
909 print_err_message ("\t\t\t");
910 f_print (fout
, "\t\t}\n");
912 * A kludgy support for inetd services. Inetd only works with
913 * sockmod, and RPC works only with timod, hence all this jugglery
915 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
916 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
917 sprintf (_errbuf
, "could not get the right module");
918 print_err_message ("\t\t\t\t");
919 f_print (fout
, "\t\t\t\texit(1);\n");
920 f_print (fout
, "\t\t\t}\n");
921 f_print (fout
, "\t\t}\n");
923 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
924 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
926 sprintf (_errbuf
, "cannot create server handle");
927 print_err_message ("\t\t\t");
928 f_print (fout
, "\t\t\texit(1);\n");
929 f_print (fout
, "\t\t}\n");
930 f_print (fout
, "\t\tif (nconf)\n");
931 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
932 for (l
= defined
; l
!= NULL
; l
= l
->next
)
934 def
= (definition
*) l
->val
;
935 if (def
->def_kind
!= DEF_PROGRAM
)
939 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
942 "\t\tif (!svc_reg(%s, %s, %s, ",
943 TRANSP
, def
->def_name
, vp
->vers_name
);
944 pvname (def
->def_name
, vp
->vers_num
);
945 f_print (fout
, ", 0)) {\n");
946 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
947 def
->def_name
, vp
->vers_name
);
948 print_err_message ("\t\t\t");
949 f_print (fout
, "\t\t\texit(1);\n");
950 f_print (fout
, "\t\t}\n");
955 f_print (fout
, "\t\tif (pmclose) {\n");
956 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
957 Cflag
? "(SIG_PF)" : "(void(*)())");
958 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
959 f_print (fout
, "\t\t}\n");
961 f_print (fout
, "\t\tsvc_run();\n");
962 f_print (fout
, "\t\texit(1);\n");
963 f_print (fout
, "\t\t/* NOTREACHED */\n");
964 f_print (fout
, "\t}\n");
968 * Support for backgrounding the server if self started.
971 write_rpc_svc_fg (const char *infile
, const char *sp
)
973 f_print (fout
, "#ifndef RPC_SVC_FG\n");
974 f_print (fout
, "%sint size;\n", sp
);
976 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
978 f_print (fout
, "%sint pid, i;\n\n", sp
);
979 f_print (fout
, "%spid = fork();\n", sp
);
980 f_print (fout
, "%sif (pid < 0) {\n", sp
);
981 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
982 f_print (fout
, "%s\texit(1);\n", sp
);
983 f_print (fout
, "%s}\n", sp
);
984 f_print (fout
, "%sif (pid)\n", sp
);
985 f_print (fout
, "%s\texit(0);\n", sp
);
986 /* get number of file descriptors */
989 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
990 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
991 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
992 f_print (fout
, "%s\texit(1);\n", sp
);
996 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
999 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
1000 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1001 /* Redirect stderr and stdout to console */
1002 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
1003 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
1004 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
1005 /* This removes control of the controlling terminal */
1007 f_print (fout
, "%ssetsid();\n", sp
);
1010 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1011 f_print (fout
, "%sif (i >= 0) {\n", sp
);
1012 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
1013 f_print (fout
, "%s\t(void) close(i);\n", sp
);
1014 f_print (fout
, "%s}\n", sp
);
1017 open_log_file (infile
, sp
);
1018 f_print (fout
, "#endif\n");
1020 open_log_file (infile
, sp
);
1024 open_log_file (const char *infile
, const char *sp
)
1028 s
= strrchr (infile
, '.');
1031 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1037 * write a registration for the given transport for Inetd
1040 write_inetd_register (const char *transp
)
1053 if (streq (transp
, "udp") || streq (transp
, "udp6"))
1057 f_print (fout
, "\n");
1060 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1061 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1063 f_print (fout
, "%s\t%s = svc%s_create(%s",
1064 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
1066 f_print (fout
, ", 0, 0");
1067 f_print (fout
, ");\n");
1068 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1069 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
1070 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
1071 print_err_message (tmpbuf
);
1072 f_print (fout
, "%s\t\texit(1);\n", sp
);
1073 f_print (fout
, "%s\t}\n", sp
);
1077 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1078 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
1079 sp
, isudp
? "UDP" : "TCP");
1081 for (l
= defined
; l
!= NULL
; l
= l
->next
)
1083 def
= (definition
*) l
->val
;
1084 if (def
->def_kind
!= DEF_PROGRAM
)
1088 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
1090 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
1091 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1092 pvname (def
->def_name
, vp
->vers_num
);
1094 f_print (fout
, ", proto)) {\n");
1096 f_print (fout
, ", IPPROTO_%s)) {\n",
1097 isudp
? "UDP" : "TCP");
1098 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
1099 def
->def_name
, vp
->vers_name
, transp
);
1100 print_err_message (tmpbuf
);
1101 f_print (fout
, "%s\t\texit(1);\n", sp
);
1102 f_print (fout
, "%s\t}\n", sp
);
1106 f_print (fout
, "\t}\n");