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";
52 char _errbuf
[256]; /* For all messages */
54 static void internal_proctype (const proc_list
* plist
);
55 static void p_xdrfunc (const char *rname
, const char *typename
);
56 static void write_real_program (const definition
* def
);
57 static void write_program (const definition
* def
, const char *storage
);
58 static void printerr (const char *err
, const char *transp
);
59 static void printif (const char *proc
, const char *transp
,
60 const char *prefix
, const char *arg
);
61 static void write_inetmost (const char *infile
);
62 static void print_return (const char *space
);
63 static void print_pmapunset (const char *space
);
64 static void print_err_message (const char *space
);
65 static void write_timeout_func (void);
66 static void write_pm_most (const char *infile
, int netflag
);
67 static void write_rpc_svc_fg (const char *infile
, const char *sp
);
68 static void open_log_file (const char *infile
, const char *sp
);
71 p_xdrfunc (const char *rname
, const char *typename
)
74 f_print (fout
, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname
,
75 stringfix (typename
));
77 f_print (fout
, "\t\txdr_%s = xdr_%s;\n", rname
, stringfix (typename
));
81 internal_proctype (const proc_list
* plist
)
83 f_print (fout
, "static ");
84 ptype (plist
->res_prefix
, plist
->res_type
, 1);
90 * write most of the service, that is, everything but the registrations.
93 write_most (const char *infile
/* our name */ , int netflag
, int nomain
)
95 if (inetdflag
|| pmflag
)
98 #ifdef __GNU_LIBRARY__
100 var_type
= (nomain
? "extern" : "");
102 var_type
= (nomain
? "extern" : "static");
104 f_print (fout
, "%s int _rpcpmstart;", var_type
);
105 f_print (fout
, "\t\t/* Started by a port monitor ? */\n");
106 f_print (fout
, "%s int _rpcfdtype;", var_type
);
107 f_print (fout
, "\t\t/* Whether Stream or Datagram ? */\n");
110 f_print (fout
, "%s int _rpcsvcdirty;", var_type
);
111 f_print (fout
, "\t/* Still serving ? */\n");
113 write_svc_aux (nomain
);
115 /* write out dispatcher and stubs */
116 write_programs (nomain
? NULL
: "static");
121 #ifdef __GNU_LIBRARY__
123 f_print (fout
, "\nint\nmain(int argc, char **argv)\n");
126 f_print (fout
, "\nint\nmain(argc, argv)\n");
127 f_print (fout
, "\tint argc;\n");
128 f_print (fout
, "\tchar **argv;\n");
131 f_print (fout
, "\nmain()\n");
133 f_print (fout
, "{\n");
136 write_inetmost (infile
); /* Includes call to write_rpc_svc_fg() */
144 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
145 f_print (fout
, "\tstruct netconfig *nconf = NULL;\n");
147 f_print (fout
, "\tpid_t pid;\n");
148 f_print (fout
, "\tint i;\n");
149 f_print (fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
150 write_pm_most (infile
, netflag
);
151 f_print (fout
, "\telse {\n");
152 write_rpc_svc_fg (infile
, "\t\t");
153 f_print (fout
, "\t}\n");
157 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
158 f_print (fout
, "\n");
159 print_pmapunset ("\t");
163 if (logflag
&& !inetdflag
)
165 open_log_file (infile
, "\t");
170 * write a registration for the given transport
173 write_netid_register (const char *transp
)
182 f_print (fout
, "\n");
183 f_print (fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
184 f_print (fout
, "%s\tif (nconf == NULL) {\n", sp
);
185 (void) sprintf (_errbuf
, "cannot find %s netid.", transp
);
186 sprintf (tmpbuf
, "%s\t\t", sp
);
187 print_err_message (tmpbuf
);
188 f_print (fout
, "%s\t\texit(1);\n", sp
);
189 f_print (fout
, "%s\t}\n", sp
);
190 f_print (fout
, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
191 sp
, TRANSP
/*, transp *//* ?!?... */ );
192 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
193 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
194 print_err_message (tmpbuf
);
195 f_print (fout
, "%s\t\texit(1);\n", sp
);
196 f_print (fout
, "%s\t}\n", sp
);
198 for (l
= defined
; l
!= NULL
; l
= l
->next
)
200 def
= (definition
*) l
->val
;
201 if (def
->def_kind
!= DEF_PROGRAM
)
205 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
208 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
209 sp
, def
->def_name
, vp
->vers_name
);
211 "%s\tif (!svc_reg(%s, %s, %s, ",
212 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
213 pvname (def
->def_name
, vp
->vers_num
);
214 f_print (fout
, ", nconf)) {\n");
215 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
216 def
->def_name
, vp
->vers_name
, transp
);
217 print_err_message (tmpbuf
);
218 f_print (fout
, "%s\t\texit(1);\n", sp
);
219 f_print (fout
, "%s\t}\n", sp
);
222 f_print (fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
226 * write a registration for the given transport for TLI
229 write_nettype_register (const char *transp
)
235 for (l
= defined
; l
!= NULL
; l
= l
->next
)
237 def
= (definition
*) l
->val
;
238 if (def
->def_kind
!= DEF_PROGRAM
)
242 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
244 f_print (fout
, "\tif (!svc_create(");
245 pvname (def
->def_name
, vp
->vers_num
);
246 f_print (fout
, ", %s, %s, \"%s\")) {\n ",
247 def
->def_name
, vp
->vers_name
, transp
);
248 (void) sprintf (_errbuf
,
249 "unable to create (%s, %s) for %s.",
250 def
->def_name
, vp
->vers_name
, transp
);
251 print_err_message ("\t\t");
252 f_print (fout
, "\t\texit(1);\n");
253 f_print (fout
, "\t}\n");
259 * write the rest of the service
264 f_print (fout
, "\n");
267 f_print (fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
268 (void) sprintf (_errbuf
, "could not create a handle");
269 print_err_message ("\t\t");
270 f_print (fout
, "\t\texit(1);\n");
271 f_print (fout
, "\t}\n");
274 f_print (fout
, "\tif (_rpcpmstart) {\n");
276 "\t\t(void) signal(SIGALRM, %s closedown);\n",
277 Cflag
? "(SIG_PF)" : "(void(*)())");
278 f_print (fout
, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
279 f_print (fout
, "\t}\n");
282 f_print (fout
, "\tsvc_run();\n");
283 (void) sprintf (_errbuf
, "svc_run returned");
284 print_err_message ("\t");
285 f_print (fout
, "\texit(1);\n");
286 f_print (fout
, "\t/* NOTREACHED */\n");
287 f_print (fout
, "}\n");
291 write_programs (const char *storage
)
296 /* write out stubs for procedure definitions */
297 for (l
= defined
; l
!= NULL
; l
= l
->next
)
299 def
= (definition
*) l
->val
;
300 if (def
->def_kind
== DEF_PROGRAM
)
302 write_real_program (def
);
306 /* write out dispatcher for each program */
307 for (l
= defined
; l
!= NULL
; l
= l
->next
)
309 def
= (definition
*) l
->val
;
310 if (def
->def_kind
== DEF_PROGRAM
)
312 write_program (def
, storage
);
319 /* write out definition of internal function (e.g. _printmsg_1(...))
320 which calls server's defintion of actual function (e.g. printmsg_1(...)).
321 Unpacks single user argument of printmsg_1 to call-by-value format
322 expected by printmsg_1. */
324 write_real_program (const definition
* def
)
331 return; /* not needed for old style */
332 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
334 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
336 f_print (fout
, "\n");
337 internal_proctype (proc
);
338 f_print (fout
, "\n_");
339 pvname (proc
->proc_name
, vp
->vers_num
);
344 if (proc
->arg_num
> 1)
345 f_print (fout
, proc
->args
.argname
);
347 ptype (proc
->args
.decls
->decl
.prefix
,
348 proc
->args
.decls
->decl
.type
, 0);
349 f_print (fout
, " *argp, struct svc_req *%s)\n",
354 f_print (fout
, "(argp, %s)\n", RQSTP
);
356 if (proc
->arg_num
> 1)
357 f_print (fout
, "\t%s *argp;\n", proc
->args
.argname
);
360 f_print (fout
, "\t");
361 ptype (proc
->args
.decls
->decl
.prefix
,
362 proc
->args
.decls
->decl
.type
, 0);
363 f_print (fout
, " *argp;\n");
365 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
368 f_print (fout
, "{\n");
369 f_print (fout
, "\treturn(");
371 pvname_svc (proc
->proc_name
, vp
->vers_num
);
373 pvname (proc
->proc_name
, vp
->vers_num
);
375 if (proc
->arg_num
< 2)
376 { /* single argument */
377 if (!streq (proc
->args
.decls
->decl
.type
, "void"))
378 f_print (fout
, "*argp, "); /* non-void */
382 for (l
= proc
->args
.decls
; l
!= NULL
; l
= l
->next
)
383 f_print (fout
, "argp->%s, ", l
->decl
.name
);
385 f_print (fout
, "%s));\n}\n", RQSTP
);
391 write_program (const definition
* def
, const char *storage
)
397 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
399 f_print (fout
, "\n");
402 f_print (fout
, "%s ", storage
);
404 f_print (fout
, "void\n");
405 pvname (def
->def_name
, vp
->vers_num
);
409 f_print (fout
, "(struct svc_req *%s, ", RQSTP
);
410 f_print (fout
, "register SVCXPRT *%s)\n", TRANSP
);
414 f_print (fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
415 f_print (fout
, " struct svc_req *%s;\n", RQSTP
);
416 f_print (fout
, " register SVCXPRT *%s;\n", TRANSP
);
419 f_print (fout
, "{\n");
422 f_print (fout
, "\tunion {\n");
423 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
425 if (proc
->arg_num
< 2)
426 { /* single argument */
427 if (streq (proc
->args
.decls
->decl
.type
,
433 f_print (fout
, "\t\t");
434 ptype (proc
->args
.decls
->decl
.prefix
,
435 proc
->args
.decls
->decl
.type
, 0);
436 pvname (proc
->proc_name
, vp
->vers_num
);
437 f_print (fout
, "_arg;\n");
443 f_print (fout
, "\t\t%s", proc
->args
.argname
);
445 pvname (proc
->proc_name
, vp
->vers_num
);
446 f_print (fout
, "_arg;\n");
451 f_print (fout
, "\t\tint fill;\n");
453 f_print (fout
, "\t} %s;\n", ARG
);
454 f_print (fout
, "\tchar *%s;\n", RESULT
);
458 f_print (fout
, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG
, RESULT
);
460 "\tchar *(*%s)(char *, struct svc_req *);\n",
465 f_print (fout
, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG
, RESULT
);
466 f_print (fout
, "\tchar *(*%s)();\n", ROUTINE
);
469 f_print (fout
, "\n");
472 f_print (fout
, "\t_rpcsvcdirty = 1;\n");
473 f_print (fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
474 if (!nullproc (vp
->procs
))
476 f_print (fout
, "\tcase NULLPROC:\n");
479 ? "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
480 : "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
482 print_return ("\t\t");
483 f_print (fout
, "\n");
485 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
)
487 f_print (fout
, "\tcase %s:\n", proc
->proc_name
);
488 if (proc
->arg_num
< 2)
489 { /* single argument */
490 p_xdrfunc (ARG
, proc
->args
.decls
->decl
.type
);
494 p_xdrfunc (ARG
, proc
->args
.argname
);
496 p_xdrfunc (RESULT
, proc
->res_type
);
499 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
502 f_print (fout
, "\t\t%s = (char *(*)()) ", ROUTINE
);
505 { /* new style: calls internal routine */
508 if (Cflag
&& !newstyle
)
509 pvname_svc (proc
->proc_name
, vp
->vers_num
);
511 pvname (proc
->proc_name
, vp
->vers_num
);
512 f_print (fout
, ";\n");
513 f_print (fout
, "\t\tbreak;\n\n");
515 f_print (fout
, "\tdefault:\n");
516 printerr ("noproc", TRANSP
);
517 print_return ("\t\t");
518 f_print (fout
, "\t}\n");
520 f_print (fout
, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG
, ARG
);
522 printif ("getargs", TRANSP
, "(caddr_t) &", ARG
);
524 printif ("getargs", TRANSP
, "&", ARG
);
525 printerr ("decode", TRANSP
);
526 print_return ("\t\t");
527 f_print (fout
, "\t}\n");
530 f_print (fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
531 RESULT
, ROUTINE
, ARG
, RQSTP
);
533 f_print (fout
, "\t%s = (*%s)(&%s, %s);\n",
534 RESULT
, ROUTINE
, ARG
, RQSTP
);
536 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
537 RESULT
, TRANSP
, RESULT
, RESULT
);
538 printerr ("systemerr", TRANSP
);
539 f_print (fout
, "\t}\n");
542 printif ("freeargs", TRANSP
, "(caddr_t) &", ARG
);
544 printif ("freeargs", TRANSP
, "&", ARG
);
545 (void) sprintf (_errbuf
, "unable to free arguments");
546 print_err_message ("\t\t");
547 f_print (fout
, "\t\texit(1);\n");
548 f_print (fout
, "\t}\n");
550 f_print (fout
, "}\n");
555 printerr (const char *err
, const char *transp
)
557 f_print (fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
561 printif (const char *proc
, const char *transp
, const char *prefix
,
564 f_print (fout
, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
565 proc
, transp
, arg
, prefix
, arg
);
569 nullproc (const proc_list
* proc
)
571 for (; proc
!= NULL
; proc
= proc
->next
)
573 if (streq (proc
->proc_num
, "0"))
582 write_inetmost (const char *infile
)
584 f_print (fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
585 f_print (fout
, "\tint sock;\n");
586 f_print (fout
, "\tint proto;\n");
587 f_print (fout
, "\tstruct sockaddr_in saddr;\n");
588 f_print (fout
, "\tint asize = sizeof (saddr);\n");
589 f_print (fout
, "\n");
591 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
592 f_print (fout
, "\t\tint ssize = sizeof (int);\n\n");
593 f_print (fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
594 f_print (fout
, "\t\t\texit(1);\n");
595 f_print (fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
596 f_print (fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
597 f_print (fout
, "\t\t\texit(1);\n");
598 f_print (fout
, "\t\tsock = 0;\n");
599 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
600 f_print (fout
, "\t\tproto = 0;\n");
601 open_log_file (infile
, "\t\t");
602 f_print (fout
, "\t} else {\n");
603 write_rpc_svc_fg (infile
, "\t\t");
604 f_print (fout
, "\t\tsock = RPC_ANYSOCK;\n");
605 print_pmapunset ("\t\t");
606 f_print (fout
, "\t}\n");
610 print_return (const char *space
)
613 f_print (fout
, "%sexit(0);\n", space
);
617 f_print (fout
, "%s_rpcsvcdirty = 0;\n", space
);
618 f_print (fout
, "%sreturn;\n", space
);
623 print_pmapunset (const char *space
)
629 for (l
= defined
; l
!= NULL
; l
= l
->next
)
631 def
= (definition
*) l
->val
;
632 if (def
->def_kind
== DEF_PROGRAM
)
634 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
637 f_print (fout
, "%s(void) pmap_unset(%s, %s);\n",
638 space
, def
->def_name
, vp
->vers_name
);
645 print_err_message (const char *space
)
648 f_print (fout
, "%ssyslog(LOG_ERR, \"%s\");\n", space
, _errbuf
);
649 else if (inetdflag
|| pmflag
)
650 f_print (fout
, "%s_msgout(\"%s\");\n", space
, _errbuf
);
652 f_print (fout
, "%sfprintf(stderr, \"%s\");\n", space
, _errbuf
);
656 * Write the server auxiliary function ( _msgout, timeout)
659 write_svc_aux (int nomain
)
664 write_timeout_func ();
668 * Write the _msgout function
674 f_print (fout
, "\n");
675 f_print (fout
, "static\n");
678 f_print (fout
, "void _msgout(msg)\n");
679 f_print (fout
, "\tchar *msg;\n");
683 f_print (fout
, "void _msgout(char* msg)\n");
685 f_print (fout
, "{\n");
686 f_print (fout
, "#ifdef RPC_SVC_FG\n");
687 if (inetdflag
|| pmflag
)
688 f_print (fout
, "\tif (_rpcpmstart)\n");
689 f_print (fout
, "\t\tsyslog(LOG_ERR, msg);\n");
690 f_print (fout
, "\telse\n");
691 f_print (fout
, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
692 f_print (fout
, "#else\n");
693 f_print (fout
, "\tsyslog(LOG_ERR, msg);\n");
694 f_print (fout
, "#endif\n");
695 f_print (fout
, "}\n");
699 * Write the timeout function
702 write_timeout_func (void)
706 f_print (fout
, "\n");
707 f_print (fout
, "static void\n");
708 #ifdef __GNU_LIBRARY__
710 f_print (fout
, "closedown(int sig)\n");
712 f_print (fout
, "closedown(sig)\n\tint sig;\n");
714 f_print (fout
, "closedown()\n");
716 f_print (fout
, "{\n");
717 #ifdef __GNU_LIBRARY__
718 f_print (fout
, "\t(void) signal(sig, %s closedown);\n",
719 Cflag
? "(SIG_PF)" : "(void(*)())");
721 f_print (fout
, "\tif (_rpcsvcdirty == 0) {\n");
722 f_print (fout
, "\t\textern fd_set svc_fdset;\n");
723 f_print (fout
, "\t\tstatic int size;\n");
724 f_print (fout
, "\t\tint i, openfd;\n");
725 if (tirpcflag
&& pmflag
)
727 f_print (fout
, "\t\tstruct t_info tinfo;\n\n");
728 f_print (fout
, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
732 f_print (fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
734 f_print (fout
, "\t\t\texit(0);\n");
735 f_print (fout
, "\t\tif (size == 0) {\n");
738 f_print (fout
, "\t\t\tstruct rlimit rl;\n\n");
739 f_print (fout
, "\t\t\trl.rlim_max = 0;\n");
740 f_print (fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
741 f_print (fout
, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
742 f_print (fout
, "\t\t\t\treturn;\n");
746 f_print (fout
, "\t\t\tsize = getdtablesize();\n");
748 f_print (fout
, "\t\t}\n");
749 f_print (fout
, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
750 f_print (fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
751 f_print (fout
, "\t\t\t\topenfd++;\n");
752 f_print (fout
, "\t\tif (openfd <= 1)\n");
753 f_print (fout
, "\t\t\texit(0);\n");
754 f_print (fout
, "\t}\n");
755 f_print (fout
, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
756 f_print (fout
, "}\n");
760 * Write the most of port monitor support
763 write_pm_most (const char *infile
, int netflag
)
769 f_print (fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
770 f_print (fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
771 f_print (fout
, " !strcmp(mname, \"timod\"))) {\n");
772 f_print (fout
, "\t\tchar *netid;\n");
774 { /* Not included by -n option */
775 f_print (fout
, "\t\tstruct netconfig *nconf = NULL;\n");
776 f_print (fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
779 f_print (fout
, "\t\tint pmclose;\n");
780 /* not necessary, defined in /usr/include/stdlib */
781 /* f_print(fout, "\t\textern char *getenv();\n"); */
782 f_print (fout
, "\n");
783 f_print (fout
, "\t\t_rpcpmstart = 1;\n");
785 open_log_file (infile
, "\t\t");
786 f_print (fout
, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
787 sprintf (_errbuf
, "cannot get transport name");
788 print_err_message ("\t\t\t");
789 f_print (fout
, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
790 sprintf (_errbuf
, "cannot get transport info");
791 print_err_message ("\t\t\t");
792 f_print (fout
, "\t\t}\n");
794 * A kludgy support for inetd services. Inetd only works with
795 * sockmod, and RPC works only with timod, hence all this jugglery
797 f_print (fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
798 f_print (fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
799 sprintf (_errbuf
, "could not get the right module");
800 print_err_message ("\t\t\t\t");
801 f_print (fout
, "\t\t\t\texit(1);\n");
802 f_print (fout
, "\t\t\t}\n");
803 f_print (fout
, "\t\t}\n");
805 f_print (fout
, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
806 f_print (fout
, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
808 sprintf (_errbuf
, "cannot create server handle");
809 print_err_message ("\t\t\t");
810 f_print (fout
, "\t\t\texit(1);\n");
811 f_print (fout
, "\t\t}\n");
812 f_print (fout
, "\t\tif (nconf)\n");
813 f_print (fout
, "\t\t\tfreenetconfigent(nconf);\n");
814 for (l
= defined
; l
!= NULL
; l
= l
->next
)
816 def
= (definition
*) l
->val
;
817 if (def
->def_kind
!= DEF_PROGRAM
)
821 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
824 "\t\tif (!svc_reg(%s, %s, %s, ",
825 TRANSP
, def
->def_name
, vp
->vers_name
);
826 pvname (def
->def_name
, vp
->vers_num
);
827 f_print (fout
, ", 0)) {\n");
828 (void) sprintf (_errbuf
, "unable to register (%s, %s).",
829 def
->def_name
, vp
->vers_name
);
830 print_err_message ("\t\t\t");
831 f_print (fout
, "\t\t\texit(1);\n");
832 f_print (fout
, "\t\t}\n");
837 f_print (fout
, "\t\tif (pmclose) {\n");
838 f_print (fout
, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
839 Cflag
? "(SIG_PF)" : "(void(*)())");
840 f_print (fout
, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
841 f_print (fout
, "\t\t}\n");
843 f_print (fout
, "\t\tsvc_run();\n");
844 f_print (fout
, "\t\texit(1);\n");
845 f_print (fout
, "\t\t/* NOTREACHED */\n");
846 f_print (fout
, "\t}\n");
850 * Support for backgrounding the server if self started.
853 write_rpc_svc_fg (const char *infile
, const char *sp
)
855 f_print (fout
, "#ifndef RPC_SVC_FG\n");
856 f_print (fout
, "%sint size;\n", sp
);
858 f_print (fout
, "%sstruct rlimit rl;\n", sp
);
860 f_print (fout
, "%sint pid, i;\n\n", sp
);
861 f_print (fout
, "%spid = fork();\n", sp
);
862 f_print (fout
, "%sif (pid < 0) {\n", sp
);
863 f_print (fout
, "%s\tperror(\"cannot fork\");\n", sp
);
864 f_print (fout
, "%s\texit(1);\n", sp
);
865 f_print (fout
, "%s}\n", sp
);
866 f_print (fout
, "%sif (pid)\n", sp
);
867 f_print (fout
, "%s\texit(0);\n", sp
);
868 /* get number of file descriptors */
871 f_print (fout
, "%srl.rlim_max = 0;\n", sp
);
872 f_print (fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
873 f_print (fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
874 f_print (fout
, "%s\texit(1);\n", sp
);
878 f_print (fout
, "%ssize = getdtablesize();\n", sp
);
881 f_print (fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
882 f_print (fout
, "%s\t(void) close(i);\n", sp
);
883 /* Redirect stderr and stdout to console */
884 f_print (fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
885 f_print (fout
, "%s(void) dup2(i, 1);\n", sp
);
886 f_print (fout
, "%s(void) dup2(i, 2);\n", sp
);
887 /* This removes control of the controlling terminal */
889 f_print (fout
, "%ssetsid();\n", sp
);
892 f_print (fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
893 f_print (fout
, "%sif (i >= 0) {\n", sp
);
894 f_print (fout
, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);;
895 f_print (fout
, "%s\t(void) close(i);\n", sp
);
896 f_print (fout
, "%s}\n", sp
);
899 open_log_file (infile
, sp
);
900 f_print (fout
, "#endif\n");
902 open_log_file (infile
, sp
);
906 open_log_file (const char *infile
, const char *sp
)
910 s
= strrchr (infile
, '.');
913 f_print (fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
922 * write a registration for the given transport for Inetd
925 write_inetd_register (const char *transp
)
938 if (streq (transp
, "udp"))
942 f_print (fout
, "\n");
945 f_print (fout
, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
946 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
948 f_print (fout
, "%s\t%s = svc%s_create(%s",
949 sp
, TRANSP
, transp
, inetdflag
? "sock" : "RPC_ANYSOCK");
951 f_print (fout
, ", 0, 0");
952 f_print (fout
, ");\n");
953 f_print (fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
954 (void) sprintf (_errbuf
, "cannot create %s service.", transp
);
955 (void) sprintf (tmpbuf
, "%s\t\t", sp
);
956 print_err_message (tmpbuf
);
957 f_print (fout
, "%s\t\texit(1);\n", sp
);
958 f_print (fout
, "%s\t}\n", sp
);
962 f_print (fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
963 f_print (fout
, "%s\tproto = IPPROTO_%s;\n",
964 sp
, isudp
? "UDP" : "TCP");
966 for (l
= defined
; l
!= NULL
; l
= l
->next
)
968 def
= (definition
*) l
->val
;
969 if (def
->def_kind
!= DEF_PROGRAM
)
973 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
)
975 f_print (fout
, "%s\tif (!svc_register(%s, %s, %s, ",
976 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
977 pvname (def
->def_name
, vp
->vers_num
);
979 f_print (fout
, ", proto)) {\n");
981 f_print (fout
, ", IPPROTO_%s)) {\n",
982 isudp
? "UDP" : "TCP");
983 (void) sprintf (_errbuf
, "unable to register (%s, %s, %s).",
984 def
->def_name
, vp
->vers_name
, transp
);
985 print_err_message (tmpbuf
);
986 f_print (fout
, "%s\t\texit(1);\n", sp
);
987 f_print (fout
, "%s\t}\n", sp
);
991 f_print (fout
, "\t}\n");