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 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
36 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
40 #include "rpc_parse.h"
42 #include "rpc_output.h"
44 static void write_real_program(definition *def);
45 static void write_program(definition *def, char *storage);
46 static void printerr(char *err, char *transp);
47 static void printif(char *proc, char *transp, char *prefix, char *arg);
48 static void write_inetmost(char *infile);
49 static void print_return(char *space);
50 static void print_pmapunset(char *space);
51 static void print_err_message(char *space);
52 static void write_timeout_func(void);
53 static void write_pm_most(char *infile, int netflag);
54 static void write_rpc_svc_fg(char *infile, char *sp);
55 static void open_log_file(char *infile, char *sp);
57 static char RQSTP[] = "rqstp";
58 static char TRANSP[] = "transp";
59 static char ARG[] = "argument";
60 static char RESULT[] = "result";
61 static char ROUTINE[] = "local";
63 char _errbuf[256]; /* For all messages */
66 p_xdrfunc(char *rname, char *typename)
69 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname,
72 f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix(typename));
76 internal_proctype(proc_list *plist)
78 f_print(fout, "static ");
79 ptype( plist->res_prefix, plist->res_type, 1 );
85 * write most of the service, that is, everything but the registrations.
88 write_most(char *infile, int netflag, int nomain)
90 if (inetdflag || pmflag) {
92 var_type = (nomain? "extern" : "static");
93 f_print(fout, "%s int _rpcpmstart;", var_type );
94 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
95 f_print(fout, "%s int _rpcfdtype;", var_type );
96 f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
98 f_print(fout, "%s int _rpcsvcdirty;", var_type );
99 f_print(fout, "\t/* Still serving ? */\n");
101 write_svc_aux( nomain );
103 /* write out dispatcher and stubs */
104 write_programs( nomain? (char *)NULL : "static" );
109 f_print(fout, "\nmain()\n");
110 f_print(fout, "{\n");
112 write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */
116 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
117 f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
119 f_print(fout, "\tpid_t pid;\n");
120 f_print(fout, "\tint i;\n");
121 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
122 write_pm_most(infile, netflag);
123 f_print(fout, "\telse {\n");
124 write_rpc_svc_fg(infile, "\t\t");
125 f_print(fout, "\t}\n");
127 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
129 print_pmapunset("\t");
133 if (logflag && !inetdflag) {
134 open_log_file(infile, "\t");
139 * write a registration for the given transport
142 write_netid_register(char *transp)
152 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
153 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
154 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
155 sprintf(tmpbuf, "%s\t\t", sp);
156 print_err_message(tmpbuf);
157 f_print(fout, "%s\t\texit(1);\n", sp);
158 f_print(fout, "%s\t}\n", sp);
159 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
161 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
162 (void) sprintf(_errbuf, "cannot create %s service.", transp);
163 print_err_message(tmpbuf);
164 f_print(fout, "%s\t\texit(1);\n", sp);
165 f_print(fout, "%s\t}\n", sp);
167 for (l = defined; l != NULL; l = l->next) {
168 def = (definition *) l->val;
169 if (def->def_kind != DEF_PROGRAM) {
172 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
174 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
175 sp, def->def_name, vp->vers_name);
177 "%s\tif (!svc_reg(%s, %s, %s, ",
178 sp, TRANSP, def->def_name, vp->vers_name);
179 pvname(def->def_name, vp->vers_num);
180 f_print(fout, ", nconf)) {\n");
181 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
182 def->def_name, vp->vers_name, transp);
183 print_err_message(tmpbuf);
184 f_print(fout, "%s\t\texit(1);\n", sp);
185 f_print(fout, "%s\t}\n", sp);
188 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
192 * write a registration for the given transport for TLI
195 write_nettype_register(char *transp)
201 for (l = defined; l != NULL; l = l->next) {
202 def = (definition *) l->val;
203 if (def->def_kind != DEF_PROGRAM) {
206 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
207 f_print(fout, "\tif (!svc_create(");
208 pvname(def->def_name, vp->vers_num);
209 f_print(fout, ", %s, %s, \"%s\")) {\n ",
210 def->def_name, vp->vers_name, transp);
211 (void) sprintf(_errbuf,
212 "unable to create (%s, %s) for %s.",
213 def->def_name, vp->vers_name, transp);
214 print_err_message("\t\t");
215 f_print(fout, "\t\texit(1);\n");
216 f_print(fout, "\t}\n");
222 * write the rest of the service
229 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
230 (void) sprintf(_errbuf, "could not create a handle");
231 print_err_message("\t\t");
232 f_print(fout, "\t\texit(1);\n");
233 f_print(fout, "\t}\n");
235 f_print(fout, "\tif (_rpcpmstart) {\n");
237 "\t\t(void) signal(SIGALRM, %s closedown);\n",
238 Cflag? "(SIG_PF)" : "(void(*)())" );
239 f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
240 f_print(fout, "\t}\n");
243 f_print(fout, "\tsvc_run();\n");
244 (void) sprintf(_errbuf, "svc_run returned");
245 print_err_message("\t");
246 f_print(fout, "\texit(1);\n");
247 f_print(fout, "\t/* NOTREACHED */\n");
248 f_print(fout, "}\n");
252 write_programs(char *storage)
257 /* write out stubs for procedure definitions */
258 for (l = defined; l != NULL; l = l->next) {
259 def = (definition *) l->val;
260 if (def->def_kind == DEF_PROGRAM) {
261 write_real_program(def);
265 /* write out dispatcher for each program */
266 for (l = defined; l != NULL; l = l->next) {
267 def = (definition *) l->val;
268 if (def->def_kind == DEF_PROGRAM) {
269 write_program(def, storage);
276 /* write out definition of internal function (e.g. _printmsg_1(...))
277 which calls server's defintion of actual function (e.g. printmsg_1(...)).
278 Unpacks single user argument of printmsg_1 to call-by-value format
279 expected by printmsg_1. */
281 write_real_program(definition *def)
287 if( !newstyle ) return; /* not needed for old style */
288 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
289 for (proc = vp->procs; proc != NULL; proc = proc->next) {
291 internal_proctype(proc);
292 f_print(fout, "\n_");
293 pvname(proc->proc_name, vp->vers_num);
297 if (proc->arg_num > 1)
298 f_print(fout, proc->args.argname);
300 ptype(proc->args.decls->decl.prefix,
301 proc->args.decls->decl.type, 0);
302 f_print(fout, " *argp, struct svc_req *%s)\n",
305 f_print(fout, "(argp, %s)\n", RQSTP );
307 if (proc->arg_num > 1)
308 f_print(fout, "\t%s *argp;\n", proc->args.argname);
311 ptype(proc->args.decls->decl.prefix,
312 proc->args.decls->decl.type, 0);
313 f_print(fout, " *argp;\n");
315 f_print(fout, " struct svc_req *%s;\n", RQSTP);
318 f_print(fout, "{\n");
319 f_print(fout, "\treturn(");
321 pvname_svc(proc->proc_name, vp->vers_num);
323 pvname(proc->proc_name, vp->vers_num);
325 if (proc->arg_num < 2) { /* single argument */
326 if (!streq( proc->args.decls->decl.type, "void"))
327 f_print(fout, "*argp, "); /* non-void */
329 for (l = proc->args.decls; l != NULL; l = l->next)
330 f_print(fout, "argp->%s, ", l->decl.name);
332 f_print(fout, "%s));\n}\n", RQSTP);
338 write_program(definition *def, char *storage)
344 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
346 if (storage != NULL) {
347 f_print(fout, "%s ", storage);
349 f_print(fout, "void\n");
350 pvname(def->def_name, vp->vers_num);
353 f_print(fout, "(struct svc_req *%s, ", RQSTP);
354 f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
356 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
357 f_print(fout, " struct svc_req *%s;\n", RQSTP);
358 f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
361 f_print(fout, "{\n");
364 f_print(fout, "\tunion {\n");
365 for (proc = vp->procs; proc != NULL; proc = proc->next) {
366 if (proc->arg_num < 2) { /* single argument */
367 if (streq(proc->args.decls->decl.type,
372 f_print(fout, "\t\t");
373 ptype(proc->args.decls->decl.prefix,
374 proc->args.decls->decl.type, 0);
375 pvname(proc->proc_name, vp->vers_num);
376 f_print(fout, "_arg;\n");
381 f_print(fout, "\t\t%s", proc->args.argname);
383 pvname(proc->proc_name, vp->vers_num);
384 f_print(fout, "_arg;\n");
388 f_print(fout, "\t\tint fill;\n");
390 f_print(fout, "\t} %s;\n", ARG);
391 f_print(fout, "\tchar *%s;\n", RESULT);
394 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
396 "\tchar *(*%s)(char *, struct svc_req *);\n",
399 f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
400 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
406 f_print(fout, "\t_rpcsvcdirty = 1;\n");
407 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
408 if (!nullproc(vp->procs)) {
409 f_print(fout, "\tcase NULLPROC:\n");
411 "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
413 print_return("\t\t");
416 for (proc = vp->procs; proc != NULL; proc = proc->next) {
417 f_print(fout, "\tcase %s:\n", proc->proc_name);
418 if (proc->arg_num < 2) { /* single argument */
419 p_xdrfunc( ARG, proc->args.decls->decl.type);
421 p_xdrfunc( ARG, proc->args.argname);
423 p_xdrfunc( RESULT, proc->res_type);
426 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
429 f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
431 if (newstyle) { /* new style: calls internal routine */
434 /* Not sure about the following...
435 * rpc_hout always generates foobar_1_svc for
436 * the service procedure, so why should we use
437 * foobar_1 here?! --okir */
439 if( Cflag && !newstyle )
440 pvname_svc(proc->proc_name, vp->vers_num);
442 pvname(proc->proc_name, vp->vers_num);
444 pvname_svc(proc->proc_name, vp->vers_num);
446 f_print(fout, ";\n");
447 f_print(fout, "\t\tbreak;\n\n");
449 f_print(fout, "\tdefault:\n");
450 printerr("noproc", TRANSP);
451 print_return("\t\t");
452 f_print(fout, "\t}\n");
454 f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
456 printif("getargs", TRANSP, "(caddr_t) &", ARG);
458 printif("getargs", TRANSP, "&", ARG);
459 printerr("decode", TRANSP);
460 print_return("\t\t");
461 f_print(fout, "\t}\n");
464 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
465 RESULT, ROUTINE, ARG, RQSTP);
467 f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
468 RESULT, ROUTINE, ARG, RQSTP);
470 "\tif (%s != NULL && !svc_sendreply(%s, "
471 "(xdrproc_t) xdr_%s, %s)) {\n",
472 RESULT, TRANSP, RESULT, RESULT);
473 printerr("systemerr", TRANSP);
474 f_print(fout, "\t}\n");
477 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
479 printif("freeargs", TRANSP, "&", ARG);
480 (void) sprintf(_errbuf, "unable to free arguments");
481 print_err_message("\t\t");
482 f_print(fout, "\t\texit(1);\n");
483 f_print(fout, "\t}\n");
485 f_print(fout, "}\n");
490 printerr(char *err, char *transp)
492 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
496 printif(char *proc, char *transp, char *prefix, char *arg)
498 f_print(fout, "\tif (!svc_%s(%s, (xdrproc_t) xdr_%s, (caddr_t) %s%s)) {\n",
499 proc, transp, arg, prefix, arg);
503 nullproc(proc_list *proc)
505 for (; proc != NULL; proc = proc->next) {
506 if (streq(proc->proc_num, "0")) {
514 write_inetmost(char *infile)
516 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
517 f_print(fout, "\tint sock;\n");
518 f_print(fout, "\tint proto;\n");
519 f_print(fout, "\tstruct sockaddr_in saddr;\n");
520 f_print(fout, "\tint asize = sizeof (saddr);\n");
523 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
524 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
525 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
526 f_print(fout, "\t\t\texit(1);\n");
527 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
528 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
529 f_print(fout, "\t\t\texit(1);\n");
530 f_print(fout, "\t\tsock = 0;\n");
531 f_print(fout, "\t\t_rpcpmstart = 1;\n");
532 f_print(fout, "\t\tproto = 0;\n");
533 open_log_file(infile, "\t\t");
534 f_print(fout, "\t} else {\n");
535 write_rpc_svc_fg(infile, "\t\t");
536 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
537 print_pmapunset("\t\t");
538 f_print(fout, "\t}\n");
542 print_return(char *space)
545 f_print(fout, "%sexit(0);\n", space);
548 f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
549 f_print(fout, "%sreturn;\n", space);
554 print_pmapunset(char *space)
560 for (l = defined; l != NULL; l = l->next) {
561 def = (definition *) l->val;
562 if (def->def_kind == DEF_PROGRAM) {
563 for (vp = def->def.pr.versions; vp != NULL;
565 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
566 space, def->def_name, vp->vers_name);
573 print_err_message(char *space)
576 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
577 else if (inetdflag || pmflag)
578 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
580 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
584 * Write the server auxiliary function ( _msgout, timeout)
587 write_svc_aux(int nomain)
592 write_timeout_func();
596 * Write the _msgout function
602 f_print(fout, "static\n");
604 f_print(fout, "void _msgout(msg)\n");
605 f_print(fout, "\tchar *msg;\n");
607 f_print(fout, "void _msgout(char* msg)\n");
609 f_print(fout, "{\n");
610 f_print(fout, "#ifdef RPC_SVC_FG\n");
611 if (inetdflag || pmflag)
612 f_print(fout, "\tif (_rpcpmstart)\n");
613 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
614 f_print(fout, "\telse\n");
615 f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
616 f_print(fout, "#else\n");
617 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
618 f_print(fout, "#endif\n");
619 f_print(fout, "}\n");
623 * Write the timeout function
626 write_timeout_func(void)
631 f_print(fout, "static void\n");
632 f_print(fout, "closedown()\n");
633 f_print(fout, "{\n");
634 f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
635 f_print(fout, "\t\tstatic int size;\n");
636 f_print(fout, "\t\tint i, openfd;\n");
637 if (tirpcflag && pmflag) {
638 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
639 f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
641 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
643 f_print(fout, "\t\t\texit(0);\n");
644 f_print(fout, "\t\tif (size == 0) {\n");
646 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
647 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
648 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
649 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
650 f_print(fout, "\t\t\t\treturn;\n");
652 f_print(fout, "\t\t\tsize = getdtablesize();\n");
654 f_print(fout, "\t\t}\n");
655 f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
656 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
657 f_print(fout, "\t\t\t\topenfd++;\n");
658 f_print(fout, "\t\tif (openfd <= 1)\n");
659 f_print(fout, "\t\t\texit(0);\n");
660 f_print(fout, "\t}\n");
661 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
662 f_print(fout, "}\n");
666 * Write the most of port monitor support
669 write_pm_most(char *infile, int netflag)
675 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
676 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
677 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
678 f_print(fout, "\t\tchar *netid;\n");
679 if (!netflag) { /* Not included by -n option */
680 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
681 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
684 f_print(fout, "\t\tint pmclose;\n");
685 /* not necessary, defined in /usr/include/stdlib */
686 /* f_print(fout, "\t\textern char *getenv();\n");*/
688 f_print(fout, "\t\t_rpcpmstart = 1;\n");
690 open_log_file(infile, "\t\t");
691 f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
692 sprintf(_errbuf, "cannot get transport name");
693 print_err_message("\t\t\t");
694 f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
695 sprintf(_errbuf, "cannot get transport info");
696 print_err_message("\t\t\t");
697 f_print(fout, "\t\t}\n");
699 * A kludgy support for inetd services. Inetd only works with
700 * sockmod, and RPC works only with timod, hence all this jugglery
702 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
703 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
704 sprintf(_errbuf, "could not get the right module");
705 print_err_message("\t\t\t\t");
706 f_print(fout, "\t\t\t\texit(1);\n");
707 f_print(fout, "\t\t\t}\n");
708 f_print(fout, "\t\t}\n");
710 f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
711 f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
713 sprintf(_errbuf, "cannot create server handle");
714 print_err_message("\t\t\t");
715 f_print(fout, "\t\t\texit(1);\n");
716 f_print(fout, "\t\t}\n");
717 f_print(fout, "\t\tif (nconf)\n");
718 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
719 for (l = defined; l != NULL; l = l->next) {
720 def = (definition *) l->val;
721 if (def->def_kind != DEF_PROGRAM) {
724 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
726 "\t\tif (!svc_reg(%s, %s, %s, ",
727 TRANSP, def->def_name, vp->vers_name);
728 pvname(def->def_name, vp->vers_num);
729 f_print(fout, ", 0)) {\n");
730 (void) sprintf(_errbuf, "unable to register (%s, %s).",
731 def->def_name, vp->vers_name);
732 print_err_message("\t\t\t");
733 f_print(fout, "\t\t\texit(1);\n");
734 f_print(fout, "\t\t}\n");
738 f_print(fout, "\t\tif (pmclose) {\n");
739 f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
740 Cflag? "(SIG_PF)" : "(void(*)())" );
741 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
742 f_print(fout, "\t\t}\n");
744 f_print(fout, "\t\tsvc_run();\n");
745 f_print(fout, "\t\texit(1);\n");
746 f_print(fout, "\t\t/* NOTREACHED */\n");
747 f_print(fout, "\t}\n");
751 * Support for backgrounding the server if self started.
754 write_rpc_svc_fg(char *infile, char *sp)
756 f_print(fout, "#ifndef RPC_SVC_FG\n");
757 f_print(fout, "%sint size;\n", sp);
759 f_print(fout, "%sstruct rlimit rl;\n", sp);
761 f_print(fout, "%sint pid, i;\n\n", sp);
762 f_print(fout, "%spid = fork();\n", sp);
763 f_print(fout, "%sif (pid < 0) {\n", sp);
764 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
765 f_print(fout, "%s\texit(1);\n", sp);
766 f_print(fout, "%s}\n", sp);
767 f_print(fout, "%sif (pid)\n", sp);
768 f_print(fout, "%s\texit(0);\n", sp);
769 /* get number of file descriptors */
771 f_print(fout, "%srl.rlim_max = 0;\n", sp);
772 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
773 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
774 f_print(fout, "%s\texit(1);\n", sp);
776 f_print(fout, "%ssize = getdtablesize();\n", sp);
779 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
780 f_print(fout, "%s\t(void) close(i);\n", sp);
781 /* Redirect stderr and stdout to console */
782 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
783 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
784 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
785 /* This removes control of the controlling terminal */
787 f_print(fout, "%ssetsid();\n", sp);
789 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
790 f_print(fout, "%sif (i >= 0) {\n", sp);
791 f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
792 f_print(fout, "%s\t(void) close(i);\n", sp);
793 f_print(fout, "%s}\n", sp);
796 open_log_file(infile, sp);
797 f_print(fout, "#endif\n");
799 open_log_file(infile, sp);
803 open_log_file(char *infile, char *sp)
807 s = strrchr(infile, '.');
810 f_print(fout,"%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
819 * write a registration for the given transport for Inetd
822 write_inetd_register(char *transp)
835 if (streq(transp, "udp"))
841 f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
842 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
844 f_print(fout, "%s\t%s = svc%s_create(%s",
845 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
847 f_print(fout, ", 0, 0");
848 f_print(fout, ");\n");
849 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
850 (void) sprintf(_errbuf, "cannot create %s service.", transp);
851 (void) sprintf(tmpbuf, "%s\t\t", sp);
852 print_err_message(tmpbuf);
853 f_print(fout, "%s\t\texit(1);\n", sp);
854 f_print(fout, "%s\t}\n", sp);
857 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
858 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
859 sp, isudp ? "UDP": "TCP");
861 for (l = defined; l != NULL; l = l->next) {
862 def = (definition *) l->val;
863 if (def->def_kind != DEF_PROGRAM) {
866 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
867 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
868 sp, TRANSP, def->def_name, vp->vers_name);
869 pvname(def->def_name, vp->vers_num);
871 f_print(fout, ", proto)) {\n");
873 f_print(fout, ", IPPROTO_%s)) {\n",
874 isudp ? "UDP": "TCP");
875 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
876 def->def_name, vp->vers_name, transp);
877 print_err_message(tmpbuf);
878 f_print(fout, "%s\t\texit(1);\n", sp);
879 f_print(fout, "%s\t}\n", sp);
883 f_print(fout, "\t}\n");