4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
26 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
29 * University Copyright- Copyright (c) 1982, 1986, 1988
30 * The Regents of the University of California
33 * University Acknowledgment- Portions of this document are derived from
34 * software developed by the University of California, Berkeley, and its
39 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
44 #include "rpc_parse.h"
47 extern int nullproc(proc_list
*);
49 static char RQSTP
[] = "rqstp";
50 static char TRANSP
[] = "transp";
51 static char ARG
[] = "argument";
52 static char RESULT
[] = "result";
53 static char ROUTINE
[] = "local";
54 static char RETVAL
[] = "retval";
58 static void internal_proctype(proc_list
*);
59 static void write_real_program(definition
*);
60 static void write_programs(char *);
61 static void write_program(definition
*, char *);
62 static void printerr(char *, char *);
63 static void write_svc_aux(int);
64 static void printif(char *, char *, char *, char *);
65 static void write_inetmost(char *);
66 static void print_return(char *);
67 static void print_pmapunset(char *);
68 static void print_err_message(const char *, const char *, ...);
69 static void write_msg_out(void);
70 static void write_timeout_func(void);
71 static void write_pm_most(char *, int);
72 static void write_rpc_svc_fg(char *, char *);
73 static void open_log_file(char *, char *);
76 p_xdrfunc(char *rname
, char *typename
)
79 f_print(fout
, "\t\t_xdr_%s = (xdrproc_t)\n", rname
);
80 f_print(fout
, "\t\t xdr_%s;\n", stringfix(typename
));
82 f_print(fout
, "\t\t_xdr_%s = xdr_%s;\n",
83 rname
, stringfix(typename
));
88 internal_proctype(proc_list
*plist
)
90 f_print(fout
, "static ");
91 ptype(plist
->res_prefix
, plist
->res_type
, 1);
99 f_print(fout
, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
100 print_err_message("\t\t", "unable to set automatic MT mode.");
101 f_print(fout
, "\t\texit(1);\n\t}\n");
105 * write most of the service, that is, everything but the registrations.
108 write_most(char *infile
, int netflag
, int nomain
)
110 if (inetdflag
|| pmflag
) {
112 var_type
= (nomain
? "extern" : "static");
113 f_print(fout
, "%s int _rpcpmstart;", var_type
);
114 f_print(fout
, "\t\t/* Started by a port monitor ? */\n");
116 f_print(fout
, "%s int _rpcfdtype;", var_type
);
118 "\t\t /* Whether Stream or Datagram ? */\n");
123 "\n/* States a server can be in wrt request */\n\n");
124 f_print(fout
, "#define\t_IDLE 0\n");
125 f_print(fout
, "#define\t_SERVED 1\n\n");
128 "/* LINTED static unused if no main */\n");
131 "static int _rpcsvcstate = _IDLE;");
133 "\t/* Set when a request is serviced */\n");
134 f_print(fout
, "static int _rpcsvccount = 0;");
136 "\t\t/* Number of requests being serviced */\n");
139 f_print(fout
, "mutex_t _svcstate_lock;");
141 "\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
146 write_svc_aux(nomain
);
148 /* write out dispatcher and stubs */
149 write_programs(nomain
? NULL
: "static");
154 f_print(fout
, "\nint\nmain()\n");
155 f_print(fout
, "{\n");
157 write_inetmost(infile
);
158 /* Includes call to write_rpc_svc_fg() */
163 "\tregister SVCXPRT *%s;\n", TRANSP
);
165 "\tstruct netconfig *nconf = NULL;\n");
167 f_print(fout
, "\tpid_t pid;\n");
168 f_print(fout
, "\tint i;\n");
171 "\tint mode = RPC_SVC_MT_AUTO;\n\n");
176 if (mtflag
& timerflag
)
178 "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
180 write_pm_most(infile
, netflag
);
181 f_print(fout
, "\telse {\n");
182 write_rpc_svc_fg(infile
, "\t\t");
183 f_print(fout
, "\t}\n");
185 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
187 print_pmapunset("\t");
191 if (logflag
&& !inetdflag
) {
192 open_log_file(infile
, "\t");
197 * write a registration for the given transport
200 write_netid_register(char *transp
)
210 f_print(fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
211 f_print(fout
, "%s\tif (nconf == NULL) {\n", sp
);
212 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s\t\t", sp
);
213 print_err_message(tmpbuf
, "cannot find %s netid.", transp
);
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",
218 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
219 print_err_message(tmpbuf
, "cannot create %s service.", transp
);
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
) {
224 def
= (definition
*) l
->val
;
225 if (def
->def_kind
!= DEF_PROGRAM
) {
228 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
230 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
231 sp
, def
->def_name
, vp
->vers_name
);
233 "%s\tif (!svc_reg(%s, %s, %s, ",
234 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
235 pvname(def
->def_name
, vp
->vers_num
);
236 f_print(fout
, ", nconf)) {\n");
237 print_err_message(tmpbuf
,
238 "unable to register (%s, %s, %s).",
239 def
->def_name
, vp
->vers_name
, transp
);
240 f_print(fout
, "%s\t\texit(1);\n", sp
);
241 f_print(fout
, "%s\t}\n", sp
);
244 f_print(fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
248 * write a registration for the given transport for TLI
251 write_nettype_register(char *transp
)
257 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
258 def
= (definition
*) l
->val
;
259 if (def
->def_kind
!= DEF_PROGRAM
) {
262 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
263 f_print(fout
, "\tif (!svc_create(\n");
264 f_print(fout
, "\t ");
265 pvname(def
->def_name
, vp
->vers_num
);
266 f_print(fout
, ", %s, %s,\n",
267 def
->def_name
, vp
->vers_name
);
268 f_print(fout
, "\t \"%s\")) {\n", transp
);
269 print_err_message("\t\t",
270 "unable to create (%s, %s) for %s.",
271 def
->def_name
, vp
->vers_name
, transp
);
272 f_print(fout
, "\t\texit(1);\n");
273 f_print(fout
, "\t}\n");
279 * write the rest of the service
286 f_print(fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
287 print_err_message("\t\t", "could not create a handle");
288 f_print(fout
, "\t\texit(1);\n");
289 f_print(fout
, "\t}\n");
291 f_print(fout
, "\tif (_rpcpmstart) {\n");
294 "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
295 print_err_message("\t\t\t",
296 "cannot create closedown thread");
297 f_print(fout
, "\t\t\texit(1);\n");
298 f_print(fout
, "\t\t}\n");
299 f_print(fout
, "\t}\n");
302 "\t\t(void) signal(SIGALRM, %s closedown);\n",
303 Cflag
? "(SIG_PF)":"(void(*)())");
305 "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
306 f_print(fout
, "\t}\n");
310 f_print(fout
, "\tsvc_run();\n");
311 print_err_message("\t", "svc_run returned");
312 f_print(fout
, "\texit(1);\n");
313 f_print(fout
, "\t/* NOTREACHED */\n");
314 f_print(fout
, "}\n");
318 write_programs(char *storage
)
323 /* write out stubs for procedure definitions */
324 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
325 def
= (definition
*) l
->val
;
326 if (def
->def_kind
== DEF_PROGRAM
)
327 write_real_program(def
);
330 /* write out dispatcher for each program */
331 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
332 def
= (definition
*) l
->val
;
333 if (def
->def_kind
== DEF_PROGRAM
)
334 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.
347 write_real_program(definition
*def
)
354 return; /* not needed for old style */
355 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
356 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
357 int oneway
= streq(proc
->res_type
, "oneway");
360 if (proc
->arg_num
< 2 &&
361 streq(proc
->args
.decls
->decl
.type
, "void")) {
362 f_print(fout
, "/* ARGSUSED */\n");
365 internal_proctype(proc
);
367 f_print(fout
, "int");
368 f_print(fout
, "\n_");
369 pvname(proc
->proc_name
, vp
->vers_num
);
371 f_print(fout
, "(\n");
374 if (proc
->arg_num
> 1)
375 /* LINTED variable format */
376 f_print(fout
, proc
->args
.argname
);
378 ptype(proc
->args
.decls
->decl
.prefix
,
379 proc
->args
.decls
->decl
.type
, 0);
380 f_print(fout
, " *argp,\n");
383 ptype(proc
->res_prefix
,
385 f_print(fout
, "*%s,\n", RESULT
);
387 f_print(fout
, " struct svc_req *%s)\n",
392 f_print(fout
, "(argp, %s, %s)\n",
395 f_print(fout
, "(argp, %s)\n", RQSTP
);
397 if (proc
->arg_num
> 1)
398 f_print(fout
, "\t%s *argp;\n",
402 ptype(proc
->args
.decls
->decl
.prefix
,
403 proc
->args
.decls
->decl
.type
, 0);
404 f_print(fout
, " *argp;\n");
407 f_print(fout
, "\tvoid *%s;\n", RESULT
);
408 f_print(fout
, "\tstruct svc_req *%s;\n", RQSTP
);
411 f_print(fout
, "{\n");
412 f_print(fout
, "\treturn (");
413 /* for mtflag, arguments are different */
415 pvname_svc(proc
->proc_name
, vp
->vers_num
);
417 pvname(proc
->proc_name
, vp
->vers_num
);
419 if (proc
->arg_num
< 2) { /* single argument */
420 /* only print if non-void */
421 if (!streq(proc
->args
.decls
->decl
.type
, "void"))
422 f_print(fout
, "*argp, ");
425 for (l
= proc
->args
.decls
; l
!= NULL
;
427 f_print(fout
, "\t argp->%s,\n",
429 f_print(fout
, "\t ");
431 if (mtflag
&& !oneway
)
432 f_print(fout
, "%s, ", RESULT
);
433 f_print(fout
, "%s));\n}\n", RQSTP
);
439 write_program(definition
*def
, char *storage
)
445 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
447 if (storage
!= NULL
) {
448 f_print(fout
, "%s ", storage
);
450 f_print(fout
, "void\n");
451 pvname(def
->def_name
, vp
->vers_num
);
454 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
455 f_print(fout
, "register SVCXPRT *%s)\n", TRANSP
);
457 f_print(fout
, "(%s, %s)\n", RQSTP
, TRANSP
);
458 f_print(fout
, " struct svc_req *%s;\n", RQSTP
);
459 f_print(fout
, " register SVCXPRT *%s;\n", TRANSP
);
462 f_print(fout
, "{\n");
465 f_print(fout
, "\tunion {\n");
466 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
467 if (proc
->arg_num
< 2) { /* single argument */
468 if (streq(proc
->args
.decls
->decl
.type
,
473 f_print(fout
, "\t\t");
474 ptype(proc
->args
.decls
->decl
.prefix
,
475 proc
->args
.decls
->decl
.type
, 0);
476 pvname(proc
->proc_name
, vp
->vers_num
);
477 f_print(fout
, "_arg;\n");
481 f_print(fout
, "\t\t%s", proc
->args
.argname
);
483 pvname(proc
->proc_name
, vp
->vers_num
);
484 f_print(fout
, "_arg;\n");
488 f_print(fout
, "\t\tint fill;\n");
490 f_print(fout
, "\t} %s;\n", ARG
);
494 f_print(fout
, "\tunion {\n");
495 for (proc
= vp
->procs
; proc
!= NULL
;
497 if (streq(proc
->res_type
, "void") ||
498 streq(proc
->res_type
, "oneway"))
501 f_print(fout
, "\t\t");
502 ptype(proc
->res_prefix
, proc
->res_type
, 0);
503 pvname(proc
->proc_name
, vp
->vers_num
);
504 f_print(fout
, "_res;\n");
507 f_print(fout
, "\t\tint fill;\n");
508 f_print(fout
, "\t} %s;\n", RESULT
);
509 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
512 f_print(fout
, "\tchar *%s;\n", RESULT
);
515 f_print(fout
, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
519 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
523 "\tchar *(*%s)(char *, struct svc_req *);\n",
527 "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
530 f_print(fout
, "\tbool_t (*%s)();\n", ROUTINE
);
532 f_print(fout
, "\tchar *(*%s)();\n", ROUTINE
);
539 "\t(void) mutex_lock(&_svcstate_lock);\n");
541 f_print(fout
, "\t_rpcsvccount++;\n");
544 "\t(void) mutex_unlock(&_svcstate_lock);\n");
547 f_print(fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
548 if (!nullproc(vp
->procs
)) {
549 f_print(fout
, "\tcase NULLPROC:\n");
552 "\t\t(void) svc_sendreply(%s,\n\t\t (xdrproc_t)xdr_void, NULL);\n" :
553 "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
555 print_return("\t\t");
558 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
559 f_print(fout
, "\tcase %s:\n", proc
->proc_name
);
560 if (proc
->arg_num
< 2) { /* single argument */
561 p_xdrfunc(ARG
, proc
->args
.decls
->decl
.type
);
563 p_xdrfunc(ARG
, proc
->args
.argname
);
566 if (streq(proc
->res_type
, "oneway")) {
568 f_print(fout
, "\t\t_xdr_%s = NULL;\n", RESULT
);
570 p_xdrfunc(RESULT
, proc
->res_type
);
575 "\t\t%s = (bool_t (*) (char *, "
576 "void *, struct svc_req *))",
580 "\t\t%s = (char *(*)(char *, "
581 "struct svc_req *))",
587 "\t\t%s = (bool_t (*)())",
590 f_print(fout
, "\t\t%s = (char *(*)())",
595 f_print(fout
, "\n\t\t ");
596 if (newstyle
) { /* new style: calls internal routine */
599 if ((Cflag
|| mtflag
) && !newstyle
)
600 pvname_svc(proc
->proc_name
, vp
->vers_num
);
602 pvname(proc
->proc_name
, vp
->vers_num
);
603 f_print(fout
, ";\n");
604 f_print(fout
, "\t\tbreak;\n\n");
606 f_print(fout
, "\tdefault:\n");
607 printerr("noproc", TRANSP
);
608 print_return("\t\t");
609 f_print(fout
, "\t}\n");
612 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
614 printif("getargs", TRANSP
, "(caddr_t)&", ARG
);
615 printerr("decode", TRANSP
);
616 print_return("\t\t");
617 f_print(fout
, "\t}\n");
622 "\t%s = (*%s)((char *)&%s, %s);\n",
623 RESULT
, ROUTINE
, ARG
, RQSTP
);
625 f_print(fout
, "\t%s = (*%s)(&%s, %s);\n",
626 RESULT
, ROUTINE
, ARG
, RQSTP
);
630 "\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
631 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
634 "\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
635 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
642 "\tif (_xdr_%s && %s > 0 &&\n"
643 "\t !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
644 RESULT
, RETVAL
, TRANSP
, RESULT
, RESULT
);
647 "\tif (_xdr_%s && %s != NULL &&\n"
648 "\t !svc_sendreply(%s, _xdr_%s, %s)) {\n",
649 RESULT
, RESULT
, TRANSP
, RESULT
, RESULT
);
651 printerr("systemerr", TRANSP
);
652 f_print(fout
, "\t}\n");
654 printif("freeargs", TRANSP
, "(caddr_t)&", ARG
);
655 print_err_message("\t\t", "unable to free arguments");
656 f_print(fout
, "\t\texit(1);\n");
657 f_print(fout
, "\t}\n");
658 /* print out free routine */
660 f_print(fout
, "\tif (_xdr_%s != NULL) {\n", RESULT
);
661 f_print(fout
, "\t\tif (!");
663 pvname(def
->def_name
, vp
->vers_num
);
664 f_print(fout
, "_freeresult(%s, _xdr_%s,\n",
666 f_print(fout
, "\t\t (caddr_t)&%s))\n",
668 print_err_message("\t\t\t", "unable to free results");
670 f_print(fout
, "\t}\n");
673 f_print(fout
, "}\n");
678 printerr(char *err
, char *transp
)
680 f_print(fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
684 printif(char *proc
, char *transp
, char *prefix
, char *arg
)
686 f_print(fout
, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
687 proc
, transp
, arg
, prefix
, arg
);
691 nullproc(proc_list
*proc
)
693 for (; proc
!= NULL
; proc
= proc
->next
) {
694 if (streq(proc
->proc_num
, "0"))
701 write_inetmost(char *infile
)
703 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
704 f_print(fout
, "\tint sock;\n");
705 f_print(fout
, "\tint proto;\n");
706 f_print(fout
, "\tstruct sockaddr_in saddr;\n");
707 f_print(fout
, "\tint asize = sizeof (saddr);\n");
710 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
711 f_print(fout
, "\t\tint ssize = sizeof (int);\n\n");
712 f_print(fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
713 f_print(fout
, "\t\t\texit(1);\n");
714 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
715 f_print(fout
, "\t\t (char *)&_rpcfdtype, &ssize) == -1)\n");
716 f_print(fout
, "\t\t\texit(1);\n");
717 f_print(fout
, "\t\tsock = 0;\n");
718 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
719 f_print(fout
, "\t\tproto = 0;\n");
720 open_log_file(infile
, "\t\t");
721 f_print(fout
, "\t} else {\n");
722 write_rpc_svc_fg(infile
, "\t\t");
723 f_print(fout
, "\t\tsock = RPC_ANYSOCK;\n");
724 print_pmapunset("\t\t");
725 f_print(fout
, "\t}\n");
729 print_return(char *space
)
732 f_print(fout
, "%sexit(0);\n", space
);
738 "%s(void) mutex_lock(&_svcstate_lock);\n",
741 f_print(fout
, "%s_rpcsvccount--;\n", space
);
742 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
745 "%s(void) mutex_unlock(&_svcstate_lock);\n",
749 f_print(fout
, "%sreturn; /* CSTYLED */\n", space
);
753 print_pmapunset(char *space
)
759 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
760 def
= (definition
*)l
->val
;
761 if (def
->def_kind
== DEF_PROGRAM
) {
762 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
764 f_print(fout
, "%s(void) pmap_unset(%s, %s);\n",
765 space
, def
->def_name
, vp
->vers_name
);
772 print_err_message(const char *space
, const char *fmt
, ...)
774 char errbuf
[ERRBUFLEN
];
778 (void) vsnprintf(errbuf
, sizeof (errbuf
), fmt
, va
);
782 f_print(fout
, "%ssyslog(LOG_ERR, \"%%s\",\n", space
);
783 else if (inetdflag
|| pmflag
)
784 f_print(fout
, "%sRPC_MSGOUT(\"%%s\",\n", space
);
786 f_print(fout
, "%sfprintf(stderr, \"%%s\",\n", space
);
787 f_print(fout
, "%s \"%s\");\n", space
, errbuf
);
791 * Write the server auxiliary function (RPC_MSGOUT, timeout)
794 write_svc_aux(int nomain
)
799 write_timeout_func();
803 * Write the RPC_MSGOUT function
805 * Note that while we define RPC_MSGOUT to be printf-like, all existing
806 * calls are of the form "%s","<msg>" and this implementation assumes that
807 * trivial case. If in the future it's desirable to generate richer calls
808 * this implementation can change to match. This way we don't (yet) have
809 * to introduce varargs into the generated code.
815 f_print(fout
, "#if\tdefined(RPC_MSGOUT)\n");
817 f_print(fout
, "extern void RPC_MSGOUT();\n");
819 f_print(fout
, "extern void RPC_MSGOUT(const char *, ...);\n");
821 f_print(fout
, "#else\t/* defined(RPC_MSGOUT) */\n");
822 f_print(fout
, "static ");
824 f_print(fout
, "void\nRPC_MSGOUT(fmt, msg)\n");
825 f_print(fout
, "\tchar *fmt;\n");
826 f_print(fout
, "\tchar *msg;\n");
828 f_print(fout
, "void\nRPC_MSGOUT(const char *fmt, char *msg)\n");
830 f_print(fout
, "{\n");
831 f_print(fout
, "#ifdef RPC_SVC_FG\n");
832 if (inetdflag
|| pmflag
)
833 f_print(fout
, "\tif (_rpcpmstart)\n");
834 f_print(fout
, "\t\tsyslog(LOG_ERR, fmt, msg);\n");
835 f_print(fout
, "\telse {\n");
836 f_print(fout
, "\t\t(void) fprintf(stderr, fmt, msg);\n");
837 f_print(fout
, "\t\t(void) putc('\\n', stderr);\n");
838 f_print(fout
, "\t}\n");
839 f_print(fout
, "#else\n");
840 f_print(fout
, "\tsyslog(LOG_ERR, fmt, msg);\n");
841 f_print(fout
, "#endif\n");
842 f_print(fout
, "}\n");
843 f_print(fout
, "#endif\t/* defined(RPC_MSGOUT) */\n");
847 * Write the timeout function
850 write_timeout_func(void)
857 f_print(fout
, "/*ARGSUSED*/\n");
858 f_print(fout
, "static void *\n");
860 f_print(fout
, "closedown(arg)\n");
861 f_print(fout
, "\tvoid *arg;\n");
863 f_print(fout
, "closedown(void *arg)\n");
864 f_print(fout
, "{\n");
865 f_print(fout
, "\t/*CONSTCOND*/\n");
866 f_print(fout
, "\twhile (1) {\n");
867 f_print(fout
, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
869 "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
870 f_print(fout
, "\t\t\tcontinue;\n\n");
872 "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
874 f_print(fout
, "\t\t\tint size;\n");
876 f_print(fout
, "\t\t\textern fd_set svc_fdset;\n");
877 f_print(fout
, "\t\t\tstatic int size;\n");
879 f_print(fout
, "\t\t\tint i, openfd = 0;\n\n");
881 f_print(fout
, "\t\t\tsize = svc_max_pollfd;\n");
883 f_print(fout
, "\t\t\tif (size == 0) {\n");
884 f_print(fout
, "\t\t\t\tsize = getdtablesize();\n");
885 f_print(fout
, "\t\t\t}\n");
888 "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
890 f_print(fout
, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
892 f_print(fout
, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
894 f_print(fout
, "\t\t\t\t\topenfd++;\n");
895 f_print(fout
, "\t\t\tif (openfd <= 1)\n");
896 f_print(fout
, "\t\t\t\texit(0);\n");
897 f_print(fout
, "\t\t} else\n");
898 f_print(fout
, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
899 f_print(fout
, "\t\t(void) mutex_unlock(&_svcstate_lock);\n");
900 f_print(fout
, "\t}\n");
901 f_print(fout
, "}\n");
905 f_print(fout
, "static void\n");
907 f_print(fout
, "closedown(sig)\n");
908 f_print(fout
, "\tint sig;\n");
910 f_print(fout
, "closedown(int sig)\n");
911 f_print(fout
, "{\n");
912 f_print(fout
, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
914 f_print(fout
, "\t\tint size;\n");
916 f_print(fout
, "\t\textern fd_set svc_fdset;\n");
917 f_print(fout
, "\t\tstatic int size;\n");
919 f_print(fout
, "\t\tint i, openfd = 0;\n\n");
921 f_print(fout
, "\t\tsize = svc_max_pollfd;\n");
923 f_print(fout
, "\t\tif (size == 0) {\n");
924 f_print(fout
, "\t\t\tsize = getdtablesize();\n");
925 f_print(fout
, "\t\t}\n");
928 "\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
930 f_print(fout
, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
932 f_print(fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
934 f_print(fout
, "\t\t\t\topenfd++;\n");
935 f_print(fout
, "\t\tif (openfd <= 1)\n");
936 f_print(fout
, "\t\t\texit(0);\n");
937 f_print(fout
, "\t} else\n");
938 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
940 f_print(fout
, "\t(void) signal(SIGALRM, %s closedown);\n",
941 Cflag
? "(SIG_PF)" : "(void(*)())");
942 f_print(fout
, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
943 f_print(fout
, "}\n");
947 * Write the most of port monitor support
950 write_pm_most(char *infile
, int netflag
)
956 f_print(fout
, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
957 f_print(fout
, "\t/*\n");
958 f_print(fout
, "\t * If stdin looks like a TLI endpoint, we assume\n");
959 f_print(fout
, "\t * that we were started by a port monitor. If\n");
960 f_print(fout
, "\t * t_getstate fails with TBADF, this is not a\n");
961 f_print(fout
, "\t * TLI endpoint.\n");
962 f_print(fout
, "\t */\n");
963 f_print(fout
, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
964 f_print(fout
, "\t\tchar *netid;\n");
965 if (!netflag
) { /* Not included by -n option */
966 f_print(fout
, "\t\tstruct netconfig *nconf = NULL;\n");
967 f_print(fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
970 f_print(fout
, "\t\tint pmclose;\n");
972 * Not necessary, defined in /usr/include/stdlib
973 * f_print(fout, "\t\textern char *getenv();\n");
976 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
977 open_log_file(infile
, "\t\t");
979 "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
982 f_print(fout
, "\t\t/* started from inetd */\n");
983 f_print(fout
, "\t\t\tpmclose = 1;\n");
985 f_print(fout
, "\t\t} else {\n");
986 f_print(fout
, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
987 print_err_message("\t\t\t\t", "cannot get transport info");
990 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
991 f_print(fout
, "\t\t}\n");
993 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
995 print_err_message("\t\t\t", "cannot create server handle");
996 f_print(fout
, "\t\t\texit(1);\n");
997 f_print(fout
, "\t\t}\n");
998 f_print(fout
, "\t\tif (nconf)\n");
999 f_print(fout
, "\t\t\tfreenetconfigent(nconf);\n");
1000 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
1001 def
= (definition
*) l
->val
;
1002 if (def
->def_kind
!= DEF_PROGRAM
) {
1005 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
1007 "\t\tif (!svc_reg(%s, %s, %s,\n",
1008 TRANSP
, def
->def_name
, vp
->vers_name
);
1009 f_print(fout
, "\t\t ");
1010 pvname(def
->def_name
, vp
->vers_num
);
1011 f_print(fout
, ", 0)) {\n");
1012 print_err_message("\t\t\t",
1013 "unable to register (%s, %s).",
1014 def
->def_name
, vp
->vers_name
);
1015 f_print(fout
, "\t\t\texit(1);\n");
1016 f_print(fout
, "\t\t}\n");
1020 f_print(fout
, "\t\tif (pmclose) {\n");
1023 "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t 0, NULL) != 0) {\n");
1024 print_err_message("\t\t\t\t",
1025 "cannot create closedown thread");
1026 f_print(fout
, "\t\t\t\texit(1);\n");
1027 f_print(fout
, "\t\t\t}\n");
1030 "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1031 Cflag
? "(SIG_PF)" : "(void(*)())");
1033 "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
1035 f_print(fout
, "\t\t}\n");
1037 f_print(fout
, "\t\tsvc_run();\n");
1038 f_print(fout
, "\t\texit(1);\n");
1039 f_print(fout
, "\t\t/* NOTREACHED */\n");
1040 f_print(fout
, "\t}");
1044 * Support for backgrounding the server if self started.
1047 write_rpc_svc_fg(char *infile
, char *sp
)
1049 f_print(fout
, "#ifndef RPC_SVC_FG\n");
1050 f_print(fout
, "#pragma weak closefrom\n");
1051 f_print(fout
, "%sextern void closefrom();\n", sp
);
1052 f_print(fout
, "%sint size;\n", sp
);
1054 f_print(fout
, "%sstruct rlimit rl;\n", sp
);
1056 f_print(fout
, "%sint pid, i;\n\n", sp
);
1057 f_print(fout
, "%spid = fork();\n", sp
);
1058 f_print(fout
, "%sif (pid < 0) {\n", sp
);
1059 f_print(fout
, "%s\tperror(\"cannot fork\");\n", sp
);
1060 f_print(fout
, "%s\texit(1);\n", sp
);
1061 f_print(fout
, "%s}\n", sp
);
1062 f_print(fout
, "%sif (pid)\n", sp
);
1063 f_print(fout
, "%s\texit(0);\n", sp
);
1064 /* close all file descriptors */
1066 f_print(fout
, "%sif (closefrom != NULL)\n", sp
);
1067 f_print(fout
, "%s\tclosefrom(0);\n", sp
);
1068 f_print(fout
, "%selse {\n", sp
);
1069 f_print(fout
, "%s\trl.rlim_max = 0;\n", sp
);
1070 f_print(fout
, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
1071 f_print(fout
, "%s\tif ((size = rl.rlim_max) == 0)\n", sp
);
1072 f_print(fout
, "%s\t\texit(1);\n", sp
);
1073 f_print(fout
, "%s\tfor (i = 0; i < size; i++)\n", sp
);
1074 f_print(fout
, "%s\t\t(void) close(i);\n", sp
);
1075 f_print(fout
, "%s}\n", sp
);
1077 f_print(fout
, "%s\tsize = getdtablesize();\n", sp
);
1078 f_print(fout
, "%s\tfor (i = 0; i < size; i++)\n", sp
);
1079 f_print(fout
, "%s\t\t(void) close(i);\n", sp
);
1081 /* Redirect stderr and stdout to /dev/null */
1082 f_print(fout
, "%si = open(\"/dev/null\", 2);\n", sp
);
1083 f_print(fout
, "%s(void) dup2(i, 1);\n", sp
);
1084 f_print(fout
, "%s(void) dup2(i, 2);\n", sp
);
1085 /* This removes control of the controlling terminal */
1087 f_print(fout
, "%ssetsid();\n", sp
);
1089 f_print(fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
1090 f_print(fout
, "%sif (i >= 0) {\n", sp
);
1092 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);
1093 f_print(fout
, "%s\t(void) close(i);\n", sp
);
1094 f_print(fout
, "%s}\n", sp
);
1097 open_log_file(infile
, sp
);
1098 f_print(fout
, "#endif\n");
1100 open_log_file(infile
, sp
);
1104 open_log_file(char *infile
, char *sp
)
1108 s
= strrchr(infile
, '.');
1111 f_print(fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
1117 * write a registration for the given transport for Inetd
1120 write_inetd_register(char *transp
)
1133 if (streq(transp
, "udp"))
1137 f_print(fout
, "\n");
1140 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1141 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
1143 f_print(fout
, "%s\t%s = svc%s_create(%s",
1144 sp
, TRANSP
, transp
, inetdflag
? "sock": "RPC_ANYSOCK");
1146 f_print(fout
, ", 0, 0");
1147 f_print(fout
, ");\n");
1148 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
1149 (void) snprintf(tmpbuf
, sizeof (tmpbuf
), "%s\t\t", sp
);
1150 print_err_message(tmpbuf
, "cannot create %s service.", transp
);
1151 f_print(fout
, "%s\t\texit(1);\n", sp
);
1152 f_print(fout
, "%s\t}\n", sp
);
1155 f_print(fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
1156 f_print(fout
, "%s\tproto = IPPROTO_%s;\n",
1157 sp
, isudp
? "UDP": "TCP");
1159 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
1160 def
= (definition
*) l
->val
;
1161 if (def
->def_kind
!= DEF_PROGRAM
)
1163 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
1164 f_print(fout
, "%s\tif (!svc_register(%s, %s, %s,\n",
1165 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
1166 f_print(fout
, "%s\t ", sp
);
1167 pvname(def
->def_name
, vp
->vers_num
);
1169 f_print(fout
, ", proto)) {\n");
1171 f_print(fout
, ", IPPROTO_%s)) {\n",
1172 isudp
? "UDP": "TCP");
1173 print_err_message(tmpbuf
,
1174 "unable to register (%s, %s, %s).",
1175 def
->def_name
, vp
->vers_name
, transp
);
1176 f_print(fout
, "%s\t\texit(1);\n", sp
);
1177 f_print(fout
, "%s\t}\n", sp
);
1181 f_print(fout
, "\t}\n");