2 * Copyright (c) 2009, Sun Microsystems, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * - Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * - Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12 * - Neither the name of Sun Microsystems, Inc. nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
30 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
34 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
38 #include "rpc_parse.h"
40 #include "rpc_output.h"
42 static void write_real_program(definition *def);
43 static void write_program(definition *def, char *storage);
44 static void printerr(char *err, char *transp);
45 static void printif(char *proc, char *transp, char *prefix, char *arg);
46 static void write_inetmost(char *infile);
47 static void print_return(char *space);
48 static void print_pmapunset(char *space);
49 static void print_err_message(char *space);
50 static void write_timeout_func(void);
51 static void write_pm_most(char *infile, int netflag);
52 static void write_rpc_svc_fg(char *infile, char *sp);
53 static void open_log_file(char *infile, char *sp);
55 static char RQSTP[] = "rqstp";
56 static char TRANSP[] = "transp";
57 static char ARG[] = "argument";
58 static char RESULT[] = "result";
59 static char ROUTINE[] = "local";
61 char _errbuf[256]; /* For all messages */
64 p_xdrfunc(char *rname, char *typename)
67 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname,
70 f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix(typename));
74 internal_proctype(proc_list *plist)
76 f_print(fout, "static ");
77 ptype( plist->res_prefix, plist->res_type, 1 );
83 * write most of the service, that is, everything but the registrations.
86 write_most(char *infile, int netflag, int nomain)
88 if (inetdflag || pmflag) {
90 var_type = (nomain? "extern" : "static");
91 f_print(fout, "%s int _rpcpmstart;", var_type );
92 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
93 f_print(fout, "%s int _rpcfdtype;", var_type );
94 f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
96 f_print(fout, "%s int _rpcsvcdirty;", var_type );
97 f_print(fout, "\t/* Still serving ? */\n");
99 write_svc_aux( nomain );
101 /* write out dispatcher and stubs */
102 write_programs( nomain? (char *)NULL : "static" );
107 f_print(fout, "\nmain()\n");
108 f_print(fout, "{\n");
110 write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */
114 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
115 f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
117 f_print(fout, "\tpid_t pid;\n");
118 f_print(fout, "\tint i;\n");
119 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
120 write_pm_most(infile, netflag);
121 f_print(fout, "\telse {\n");
122 write_rpc_svc_fg(infile, "\t\t");
123 f_print(fout, "\t}\n");
125 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
127 print_pmapunset("\t");
131 if (logflag && !inetdflag) {
132 open_log_file(infile, "\t");
137 * write a registration for the given transport
140 write_netid_register(char *transp)
150 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
151 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
152 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
153 sprintf(tmpbuf, "%s\t\t", sp);
154 print_err_message(tmpbuf);
155 f_print(fout, "%s\t\texit(1);\n", sp);
156 f_print(fout, "%s\t}\n", sp);
157 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
159 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
160 (void) sprintf(_errbuf, "cannot create %s service.", transp);
161 print_err_message(tmpbuf);
162 f_print(fout, "%s\t\texit(1);\n", sp);
163 f_print(fout, "%s\t}\n", sp);
165 for (l = defined; l != NULL; l = l->next) {
166 def = (definition *) l->val;
167 if (def->def_kind != DEF_PROGRAM) {
170 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
172 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
173 sp, def->def_name, vp->vers_name);
175 "%s\tif (!svc_reg(%s, %s, %s, ",
176 sp, TRANSP, def->def_name, vp->vers_name);
177 pvname(def->def_name, vp->vers_num);
178 f_print(fout, ", nconf)) {\n");
179 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
180 def->def_name, vp->vers_name, transp);
181 print_err_message(tmpbuf);
182 f_print(fout, "%s\t\texit(1);\n", sp);
183 f_print(fout, "%s\t}\n", sp);
186 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
190 * write a registration for the given transport for TLI
193 write_nettype_register(char *transp)
199 for (l = defined; l != NULL; l = l->next) {
200 def = (definition *) l->val;
201 if (def->def_kind != DEF_PROGRAM) {
204 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
205 f_print(fout, "\tif (!svc_create(");
206 pvname(def->def_name, vp->vers_num);
207 f_print(fout, ", %s, %s, \"%s\")) {\n ",
208 def->def_name, vp->vers_name, transp);
209 (void) sprintf(_errbuf,
210 "unable to create (%s, %s) for %s.",
211 def->def_name, vp->vers_name, transp);
212 print_err_message("\t\t");
213 f_print(fout, "\t\texit(1);\n");
214 f_print(fout, "\t}\n");
220 * write the rest of the service
227 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
228 (void) sprintf(_errbuf, "could not create a handle");
229 print_err_message("\t\t");
230 f_print(fout, "\t\texit(1);\n");
231 f_print(fout, "\t}\n");
233 f_print(fout, "\tif (_rpcpmstart) {\n");
235 "\t\t(void) signal(SIGALRM, %s closedown);\n",
236 Cflag? "(SIG_PF)" : "(void(*)())" );
237 f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
238 f_print(fout, "\t}\n");
241 f_print(fout, "\tsvc_run();\n");
242 (void) sprintf(_errbuf, "svc_run returned");
243 print_err_message("\t");
244 f_print(fout, "\texit(1);\n");
245 f_print(fout, "\t/* NOTREACHED */\n");
246 f_print(fout, "}\n");
250 write_programs(char *storage)
255 /* write out stubs for procedure definitions */
256 for (l = defined; l != NULL; l = l->next) {
257 def = (definition *) l->val;
258 if (def->def_kind == DEF_PROGRAM) {
259 write_real_program(def);
263 /* write out dispatcher for each program */
264 for (l = defined; l != NULL; l = l->next) {
265 def = (definition *) l->val;
266 if (def->def_kind == DEF_PROGRAM) {
267 write_program(def, storage);
274 /* write out definition of internal function (e.g. _printmsg_1(...))
275 which calls server's defintion of actual function (e.g. printmsg_1(...)).
276 Unpacks single user argument of printmsg_1 to call-by-value format
277 expected by printmsg_1. */
279 write_real_program(definition *def)
285 if( !newstyle ) return; /* not needed for old style */
286 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
287 for (proc = vp->procs; proc != NULL; proc = proc->next) {
289 internal_proctype(proc);
290 f_print(fout, "\n_");
291 pvname(proc->proc_name, vp->vers_num);
295 if (proc->arg_num > 1)
296 f_print(fout, proc->args.argname);
298 ptype(proc->args.decls->decl.prefix,
299 proc->args.decls->decl.type, 0);
300 f_print(fout, " *argp, struct svc_req *%s)\n",
303 f_print(fout, "(argp, %s)\n", RQSTP );
305 if (proc->arg_num > 1)
306 f_print(fout, "\t%s *argp;\n", proc->args.argname);
309 ptype(proc->args.decls->decl.prefix,
310 proc->args.decls->decl.type, 0);
311 f_print(fout, " *argp;\n");
313 f_print(fout, " struct svc_req *%s;\n", RQSTP);
316 f_print(fout, "{\n");
317 f_print(fout, "\treturn(");
319 pvname_svc(proc->proc_name, vp->vers_num);
321 pvname(proc->proc_name, vp->vers_num);
323 if (proc->arg_num < 2) { /* single argument */
324 if (!streq( proc->args.decls->decl.type, "void"))
325 f_print(fout, "*argp, "); /* non-void */
327 for (l = proc->args.decls; l != NULL; l = l->next)
328 f_print(fout, "argp->%s, ", l->decl.name);
330 f_print(fout, "%s));\n}\n", RQSTP);
336 write_program(definition *def, char *storage)
342 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
344 if (storage != NULL) {
345 f_print(fout, "%s ", storage);
347 f_print(fout, "void\n");
348 pvname(def->def_name, vp->vers_num);
351 f_print(fout, "(struct svc_req *%s, ", RQSTP);
352 f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
354 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
355 f_print(fout, " struct svc_req *%s;\n", RQSTP);
356 f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
359 f_print(fout, "{\n");
362 f_print(fout, "\tunion {\n");
363 for (proc = vp->procs; proc != NULL; proc = proc->next) {
364 if (proc->arg_num < 2) { /* single argument */
365 if (streq(proc->args.decls->decl.type,
370 f_print(fout, "\t\t");
371 ptype(proc->args.decls->decl.prefix,
372 proc->args.decls->decl.type, 0);
373 pvname(proc->proc_name, vp->vers_num);
374 f_print(fout, "_arg;\n");
379 f_print(fout, "\t\t%s", proc->args.argname);
381 pvname(proc->proc_name, vp->vers_num);
382 f_print(fout, "_arg;\n");
386 f_print(fout, "\t\tint fill;\n");
388 f_print(fout, "\t} %s;\n", ARG);
389 f_print(fout, "\tchar *%s;\n", RESULT);
392 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
394 "\tchar *(*%s)(char *, struct svc_req *);\n",
397 f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
398 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
404 f_print(fout, "\t_rpcsvcdirty = 1;\n");
405 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
406 if (!nullproc(vp->procs)) {
407 f_print(fout, "\tcase NULLPROC:\n");
409 "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
411 print_return("\t\t");
414 for (proc = vp->procs; proc != NULL; proc = proc->next) {
415 f_print(fout, "\tcase %s:\n", proc->proc_name);
416 if (proc->arg_num < 2) { /* single argument */
417 p_xdrfunc( ARG, proc->args.decls->decl.type);
419 p_xdrfunc( ARG, proc->args.argname);
421 p_xdrfunc( RESULT, proc->res_type);
424 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
427 f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
429 if (newstyle) { /* new style: calls internal routine */
432 /* Not sure about the following...
433 * rpc_hout always generates foobar_1_svc for
434 * the service procedure, so why should we use
435 * foobar_1 here?! --okir */
437 if( Cflag && !newstyle )
438 pvname_svc(proc->proc_name, vp->vers_num);
440 pvname(proc->proc_name, vp->vers_num);
442 pvname_svc(proc->proc_name, vp->vers_num);
444 f_print(fout, ";\n");
445 f_print(fout, "\t\tbreak;\n\n");
447 f_print(fout, "\tdefault:\n");
448 printerr("noproc", TRANSP);
449 print_return("\t\t");
450 f_print(fout, "\t}\n");
452 f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
454 printif("getargs", TRANSP, "(caddr_t) &", ARG);
456 printif("getargs", TRANSP, "&", ARG);
457 printerr("decode", TRANSP);
458 print_return("\t\t");
459 f_print(fout, "\t}\n");
462 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
463 RESULT, ROUTINE, ARG, RQSTP);
465 f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
466 RESULT, ROUTINE, ARG, RQSTP);
468 "\tif (%s != NULL && !svc_sendreply(%s, "
469 "(xdrproc_t) xdr_%s, %s)) {\n",
470 RESULT, TRANSP, RESULT, RESULT);
471 printerr("systemerr", TRANSP);
472 f_print(fout, "\t}\n");
475 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
477 printif("freeargs", TRANSP, "&", ARG);
478 (void) sprintf(_errbuf, "unable to free arguments");
479 print_err_message("\t\t");
480 f_print(fout, "\t\texit(1);\n");
481 f_print(fout, "\t}\n");
483 f_print(fout, "}\n");
488 printerr(char *err, char *transp)
490 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
494 printif(char *proc, char *transp, char *prefix, char *arg)
496 f_print(fout, "\tif (!svc_%s(%s, (xdrproc_t) xdr_%s, (caddr_t) %s%s)) {\n",
497 proc, transp, arg, prefix, arg);
501 nullproc(proc_list *proc)
503 for (; proc != NULL; proc = proc->next) {
504 if (streq(proc->proc_num, "0")) {
512 write_inetmost(char *infile)
514 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
515 f_print(fout, "\tint sock;\n");
516 f_print(fout, "\tint proto;\n");
517 f_print(fout, "\tstruct sockaddr_in saddr;\n");
518 f_print(fout, "\tint asize = sizeof (saddr);\n");
521 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
522 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
523 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
524 f_print(fout, "\t\t\texit(1);\n");
525 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
526 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
527 f_print(fout, "\t\t\texit(1);\n");
528 f_print(fout, "\t\tsock = 0;\n");
529 f_print(fout, "\t\t_rpcpmstart = 1;\n");
530 f_print(fout, "\t\tproto = 0;\n");
531 open_log_file(infile, "\t\t");
532 f_print(fout, "\t} else {\n");
533 write_rpc_svc_fg(infile, "\t\t");
534 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
535 print_pmapunset("\t\t");
536 f_print(fout, "\t}\n");
540 print_return(char *space)
543 f_print(fout, "%sexit(0);\n", space);
546 f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
547 f_print(fout, "%sreturn;\n", space);
552 print_pmapunset(char *space)
558 for (l = defined; l != NULL; l = l->next) {
559 def = (definition *) l->val;
560 if (def->def_kind == DEF_PROGRAM) {
561 for (vp = def->def.pr.versions; vp != NULL;
563 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
564 space, def->def_name, vp->vers_name);
571 print_err_message(char *space)
574 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
575 else if (inetdflag || pmflag)
576 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
578 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
582 * Write the server auxiliary function ( _msgout, timeout)
585 write_svc_aux(int nomain)
590 write_timeout_func();
594 * Write the _msgout function
600 f_print(fout, "static\n");
602 f_print(fout, "void _msgout(msg)\n");
603 f_print(fout, "\tchar *msg;\n");
605 f_print(fout, "void _msgout(char* msg)\n");
607 f_print(fout, "{\n");
608 f_print(fout, "#ifdef RPC_SVC_FG\n");
609 if (inetdflag || pmflag)
610 f_print(fout, "\tif (_rpcpmstart)\n");
611 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
612 f_print(fout, "\telse\n");
613 f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
614 f_print(fout, "#else\n");
615 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
616 f_print(fout, "#endif\n");
617 f_print(fout, "}\n");
621 * Write the timeout function
624 write_timeout_func(void)
629 f_print(fout, "static void\n");
630 f_print(fout, "closedown()\n");
631 f_print(fout, "{\n");
632 f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
633 f_print(fout, "\t\tstatic int size;\n");
634 f_print(fout, "\t\tint i, openfd;\n");
635 if (tirpcflag && pmflag) {
636 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
637 f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
639 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
641 f_print(fout, "\t\t\texit(0);\n");
642 f_print(fout, "\t\tif (size == 0) {\n");
644 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
645 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
646 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
647 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
648 f_print(fout, "\t\t\t\treturn;\n");
650 f_print(fout, "\t\t\tsize = getdtablesize();\n");
652 f_print(fout, "\t\t}\n");
653 f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
654 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
655 f_print(fout, "\t\t\t\topenfd++;\n");
656 f_print(fout, "\t\tif (openfd <= 1)\n");
657 f_print(fout, "\t\t\texit(0);\n");
658 f_print(fout, "\t}\n");
659 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
660 f_print(fout, "}\n");
664 * Write the most of port monitor support
667 write_pm_most(char *infile, int netflag)
673 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
674 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
675 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
676 f_print(fout, "\t\tchar *netid;\n");
677 if (!netflag) { /* Not included by -n option */
678 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
679 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
682 f_print(fout, "\t\tint pmclose;\n");
683 /* not necessary, defined in /usr/include/stdlib */
684 /* f_print(fout, "\t\textern char *getenv();\n");*/
686 f_print(fout, "\t\t_rpcpmstart = 1;\n");
688 open_log_file(infile, "\t\t");
689 f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
690 sprintf(_errbuf, "cannot get transport name");
691 print_err_message("\t\t\t");
692 f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
693 sprintf(_errbuf, "cannot get transport info");
694 print_err_message("\t\t\t");
695 f_print(fout, "\t\t}\n");
697 * A kludgy support for inetd services. Inetd only works with
698 * sockmod, and RPC works only with timod, hence all this jugglery
700 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
701 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
702 sprintf(_errbuf, "could not get the right module");
703 print_err_message("\t\t\t\t");
704 f_print(fout, "\t\t\t\texit(1);\n");
705 f_print(fout, "\t\t\t}\n");
706 f_print(fout, "\t\t}\n");
708 f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
709 f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
711 sprintf(_errbuf, "cannot create server handle");
712 print_err_message("\t\t\t");
713 f_print(fout, "\t\t\texit(1);\n");
714 f_print(fout, "\t\t}\n");
715 f_print(fout, "\t\tif (nconf)\n");
716 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
717 for (l = defined; l != NULL; l = l->next) {
718 def = (definition *) l->val;
719 if (def->def_kind != DEF_PROGRAM) {
722 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
724 "\t\tif (!svc_reg(%s, %s, %s, ",
725 TRANSP, def->def_name, vp->vers_name);
726 pvname(def->def_name, vp->vers_num);
727 f_print(fout, ", 0)) {\n");
728 (void) sprintf(_errbuf, "unable to register (%s, %s).",
729 def->def_name, vp->vers_name);
730 print_err_message("\t\t\t");
731 f_print(fout, "\t\t\texit(1);\n");
732 f_print(fout, "\t\t}\n");
736 f_print(fout, "\t\tif (pmclose) {\n");
737 f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
738 Cflag? "(SIG_PF)" : "(void(*)())" );
739 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
740 f_print(fout, "\t\t}\n");
742 f_print(fout, "\t\tsvc_run();\n");
743 f_print(fout, "\t\texit(1);\n");
744 f_print(fout, "\t\t/* NOTREACHED */\n");
745 f_print(fout, "\t}\n");
749 * Support for backgrounding the server if self started.
752 write_rpc_svc_fg(char *infile, char *sp)
754 f_print(fout, "#ifndef RPC_SVC_FG\n");
755 f_print(fout, "%sint size;\n", sp);
757 f_print(fout, "%sstruct rlimit rl;\n", sp);
759 f_print(fout, "%sint pid, i;\n\n", sp);
760 f_print(fout, "%spid = fork();\n", sp);
761 f_print(fout, "%sif (pid < 0) {\n", sp);
762 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
763 f_print(fout, "%s\texit(1);\n", sp);
764 f_print(fout, "%s}\n", sp);
765 f_print(fout, "%sif (pid)\n", sp);
766 f_print(fout, "%s\texit(0);\n", sp);
767 /* get number of file descriptors */
769 f_print(fout, "%srl.rlim_max = 0;\n", sp);
770 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
771 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
772 f_print(fout, "%s\texit(1);\n", sp);
774 f_print(fout, "%ssize = getdtablesize();\n", sp);
777 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
778 f_print(fout, "%s\t(void) close(i);\n", sp);
779 /* Redirect stderr and stdout to console */
780 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
781 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
782 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
783 /* This removes control of the controlling terminal */
785 f_print(fout, "%ssetsid();\n", sp);
787 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
788 f_print(fout, "%sif (i >= 0) {\n", sp);
789 f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
790 f_print(fout, "%s\t(void) close(i);\n", sp);
791 f_print(fout, "%s}\n", sp);
794 open_log_file(infile, sp);
795 f_print(fout, "#endif\n");
797 open_log_file(infile, sp);
801 open_log_file(char *infile, char *sp)
805 s = strrchr(infile, '.');
808 f_print(fout,"%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
817 * write a registration for the given transport for Inetd
820 write_inetd_register(char *transp)
833 if (streq(transp, "udp"))
839 f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
840 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
842 f_print(fout, "%s\t%s = svc%s_create(%s",
843 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
845 f_print(fout, ", 0, 0");
846 f_print(fout, ");\n");
847 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
848 (void) sprintf(_errbuf, "cannot create %s service.", transp);
849 (void) sprintf(tmpbuf, "%s\t\t", sp);
850 print_err_message(tmpbuf);
851 f_print(fout, "%s\t\texit(1);\n", sp);
852 f_print(fout, "%s\t}\n", sp);
855 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
856 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
857 sp, isudp ? "UDP": "TCP");
859 for (l = defined; l != NULL; l = l->next) {
860 def = (definition *) l->val;
861 if (def->def_kind != DEF_PROGRAM) {
864 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
865 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
866 sp, TRANSP, def->def_name, vp->vers_name);
867 pvname(def->def_name, vp->vers_num);
869 f_print(fout, ", proto)) {\n");
871 f_print(fout, ", IPPROTO_%s)) {\n",
872 isudp ? "UDP": "TCP");
873 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
874 def->def_name, vp->vers_name, transp);
875 print_err_message(tmpbuf);
876 f_print(fout, "%s\t\texit(1);\n", sp);
877 f_print(fout, "%s\t}\n", sp);
881 f_print(fout, "\t}\n");