4 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
5 * unrestricted use provided that this legend is included on all tape
6 * media and as a part of the software program in whole or part. Users
7 * may copy or modify Sun RPC without charge, but are not authorized
8 * to license or distribute it to anyone else except as part of a product or
9 * program developed by the user.
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
27 * Sun Microsystems, Inc.
29 * Mountain View, California 94043
32 #ident "@(#)rpc_svcout.c 1.4 90/04/13 SMI"
35 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
39 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
40 * Copyright (C) 1987, Sun Microsystems, Inc.
44 #include "rpc_parse.h"
47 static char RQSTP[] = "rqstp";
48 static char TRANSP[] = "transp";
49 static char ARG[] = "argument";
50 static char RESULT[] = "result";
51 static char ROUTINE[] = "local";
52 static char RETVAL[] = "retval";
54 char _errbuf[256]; /* For all messages */
56 void internal_proctype __P(( proc_list * ));
57 static void write_real_program __P(( definition * ));
58 static void write_program __P(( definition *, char * ));
59 static void printerr __P(( char *, char * ));
60 static void printif __P(( char *, char *, char *, char * ));
61 static void write_inetmost __P(( char * ));
62 static void print_return __P(( char * ));
63 static void print_pmapunset __P(( char * ));
64 static void print_err_message __P(( char * ));
65 static void write_timeout_func __P(( void ));
66 static void write_pm_most __P(( char *, int ));
67 static void write_rpc_svc_fg __P(( char *, char * ));
68 static void open_log_file __P(( char *, char * ));
69 static void write_msg_out __P(( void ));
70 int nullproc __P(( proc_list * ));
74 p_xdrfunc(rname, typename)
79 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
80 rname, stringfix(typename));
82 f_print(fout, "\t\txdr_%s = xdr_%s;\n",
83 rname, stringfix(typename));
87 internal_proctype(plist)
90 f_print(fout, "static ");
91 ptype(plist->res_prefix, plist->res_type, 1);
97 * write most of the service, that is, everything but the registrations.
100 write_most(infile, netflag, nomain)
101 char *infile; /* our name */
105 if (inetdflag || pmflag) {
107 var_type = (nomain? "extern" : "static");
108 f_print(fout, "%s int _rpcpmstart;", var_type);
109 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
111 f_print(fout, "%s int _rpcfdtype;", var_type);
112 f_print(fout, "\n\t\t /* Whether Stream or \
117 f_print(fout, " /* States a server can be in \
118 wrt request */\n\n");
119 f_print(fout, "#define\t_IDLE 0\n");
120 f_print(fout, "#define\t_SERVED 1\n");
121 f_print(fout, "#define\t_SERVING 2\n\n");
122 f_print(fout, "static int _rpcsvcstate = _IDLE;");
123 f_print(fout, "\t /* Set when a request is \
127 f_print(fout, "mutex_t _svcstate_lock;");
128 f_print(fout, "\t\t\t/* Mutex lock for variable _rpcsvcstate */\n");
134 write_svc_aux(nomain);
136 /* write out dispatcher and stubs */
137 write_programs(nomain? (char *)NULL : "static");
142 f_print(fout, "\nmain()\n");
143 f_print(fout, "{\n");
145 write_inetmost(infile);
146 /* Includes call to write_rpc_svc_fg() */
151 "\tregister SVCXPRT *%s;\n", TRANSP);
153 "\tstruct netconfig *nconf = NULL;\n");
155 f_print(fout, "\tpid_t pid;\n");
156 f_print(fout, "\tint i;\n");
157 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
159 if (mtflag & timerflag)
160 f_print(fout, "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
162 write_pm_most(infile, netflag);
163 f_print(fout, "\telse {\n");
164 write_rpc_svc_fg(infile, "\t\t");
165 f_print(fout, "\t}\n");
167 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
169 print_pmapunset("\t");
173 if (logflag && !inetdflag) {
174 open_log_file(infile, "\t");
179 * write a registration for the given transport
182 write_netid_register(transp)
193 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
194 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
195 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
196 sprintf(tmpbuf, "%s\t\t", sp);
197 print_err_message(tmpbuf);
198 f_print(fout, "%s\t\texit(1);\n", sp);
199 f_print(fout, "%s\t}\n", sp);
200 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
202 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
203 (void) sprintf(_errbuf, "cannot create %s service.", transp);
204 print_err_message(tmpbuf);
205 f_print(fout, "%s\t\texit(1);\n", sp);
206 f_print(fout, "%s\t}\n", sp);
208 for (l = defined; l != NULL; l = l->next) {
209 def = (definition *) l->val;
210 if (def->def_kind != DEF_PROGRAM) {
213 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
215 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
216 sp, def->def_name, vp->vers_name);
218 "%s\tif (!svc_reg(%s, %s, %s, ",
219 sp, TRANSP, def->def_name, vp->vers_name);
220 pvname(def->def_name, vp->vers_num);
221 f_print(fout, ", nconf)) {\n");
222 (void) sprintf(_errbuf,
223 "unable to register (%s, %s, %s).",
224 def->def_name, vp->vers_name, transp);
225 print_err_message(tmpbuf);
226 f_print(fout, "%s\t\texit(1);\n", sp);
227 f_print(fout, "%s\t}\n", sp);
230 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
234 * write a registration for the given transport for TLI
237 write_nettype_register(transp)
244 for (l = defined; l != NULL; l = l->next) {
245 def = (definition *) l->val;
246 if (def->def_kind != DEF_PROGRAM) {
249 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
250 f_print(fout, "\tif (!svc_create(");
251 pvname(def->def_name, vp->vers_num);
252 f_print(fout, ", %s, %s, \"%s\")) {\n",
253 def->def_name, vp->vers_name, transp);
254 (void) sprintf(_errbuf,
255 "unable to create (%s, %s) for %s.",
256 def->def_name, vp->vers_name, transp);
257 print_err_message("\t\t");
258 f_print(fout, "\t\texit(1);\n");
259 f_print(fout, "\t}\n");
265 * write the rest of the service
272 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
273 (void) sprintf(_errbuf, "could not create a handle");
274 print_err_message("\t\t");
275 f_print(fout, "\t\texit(1);\n");
276 f_print(fout, "\t}\n");
278 f_print(fout, "\tif (_rpcpmstart) {\n");
280 "\t\t(void) signal(SIGALRM, %s closedown);\n",
281 Cflag? "(SIG_PF)":"(void(*)())");
282 f_print(fout, "\t\t(void) \
283 alarm(_RPCSVC_CLOSEDOWN/2);\n");
284 f_print(fout, "\t}\n");
287 f_print(fout, "\tsvc_run();\n");
288 (void) sprintf(_errbuf, "svc_run returned");
289 print_err_message("\t");
290 f_print(fout, "\texit(1);\n");
291 f_print(fout, "\t/* NOTREACHED */\n");
292 f_print(fout, "}\n");
296 write_programs(storage)
302 /* write out stubs for procedure definitions */
303 for (l = defined; l != NULL; l = l->next) {
304 def = (definition *) l->val;
305 if (def->def_kind == DEF_PROGRAM) {
306 write_real_program(def);
310 /* write out dispatcher for each program */
311 for (l = defined; l != NULL; l = l->next) {
312 def = (definition *) l->val;
313 if (def->def_kind == DEF_PROGRAM) {
314 write_program(def, storage);
322 * write out definition of internal function (e.g. _printmsg_1(...))
323 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
324 * Unpacks single user argument of printmsg_1 to call-by-value format
325 * expected by printmsg_1.
328 write_real_program(def)
335 if (!newstyle) return; /* not needed for old style */
336 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
337 for (proc = vp->procs; proc != NULL; proc = proc->next) {
340 internal_proctype(proc);
342 f_print(fout, "int");
343 f_print(fout, "\n_");
344 pvname(proc->proc_name, vp->vers_num);
348 if (proc->arg_num > 1)
349 f_print(fout, proc->args.argname);
351 ptype(proc->args.decls->decl.prefix,
352 proc->args.decls->decl.type, 0);
354 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
360 f_print(fout, " *argp, struct svc_req *%s)\n",
365 f_print(fout, "(argp, %s, %s)\n", RESULT, RQSTP);
367 f_print(fout, "(argp, %s)\n", RQSTP);
369 if (proc->arg_num > 1)
370 f_print(fout, "\t%s *argp;\n",
374 ptype(proc->args.decls->decl.prefix,
375 proc->args.decls->decl.type, 0);
376 f_print(fout, " *argp;\n");
379 f_print(fout, "\tvoid *%s;\n", RESULT);
380 f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
383 f_print(fout, "{\n");
384 f_print(fout, "\treturn (");
385 if (Cflag || mtflag) /* for mtflag, arguments are different */
386 pvname_svc(proc->proc_name, vp->vers_num);
388 pvname(proc->proc_name, vp->vers_num);
390 if (proc->arg_num < 2) { /* single argument */
391 if (!streq(proc->args.decls->decl.type, "void"))
392 f_print(fout, "*argp, "); /* non-void */
394 for (l = proc->args.decls; l != NULL;
396 f_print(fout, "argp->%s, ",
400 f_print(fout, "%s, ",RESULT);
401 f_print(fout, "%s));\n}\n", RQSTP);
407 write_program(def, storage)
415 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
417 if (storage != NULL) {
418 f_print(fout, "%s ", storage);
420 f_print(fout, "void\n");
421 pvname(def->def_name, vp->vers_num);
424 f_print(fout, "(struct svc_req *%s, ", RQSTP);
425 f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
427 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
428 f_print(fout, " struct svc_req *%s;\n", RQSTP);
429 f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
432 f_print(fout, "{\n");
435 f_print(fout, "\tunion {\n");
436 for (proc = vp->procs; proc != NULL; proc = proc->next) {
437 if (proc->arg_num < 2) { /* single argument */
438 if (streq(proc->args.decls->decl.type,
443 f_print(fout, "\t\t");
444 ptype(proc->args.decls->decl.prefix,
445 proc->args.decls->decl.type, 0);
446 pvname(proc->proc_name, vp->vers_num);
447 f_print(fout, "_arg;\n");
451 f_print(fout, "\t\t%s", proc->args.argname);
453 pvname(proc->proc_name, vp->vers_num);
454 f_print(fout, "_arg;\n");
458 f_print(fout, "\t\tint fill;\n");
460 f_print(fout, "\t} %s;\n", ARG);
463 f_print(fout, "\tunion {\n");
464 for (proc = vp->procs; proc != NULL; proc = proc->next) {
465 f_print(fout, "\t\t");
466 ptype(proc->res_prefix, proc->res_type, 0);
467 pvname(proc->proc_name, vp->vers_num);
468 f_print(fout, "_res;\n");
470 f_print(fout, "\t} %s;\n", RESULT);
471 f_print(fout, "\tbool_t %s;\n", RETVAL);
474 f_print(fout, "\tchar *%s;\n", RESULT);
477 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n",
481 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
485 "\tchar *(*%s)(char *, struct svc_req *);\n",
489 "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n",
492 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
494 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
500 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
502 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
504 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
507 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
508 if (!nullproc(vp->procs)) {
509 f_print(fout, "\tcase NULLPROC:\n");
512 ? "\t\t(void) svc_sendreply(%s,\n\t\t\t\
513 (xdrproc_t) xdr_void, (char *)NULL);\n"
514 : "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\t\
517 print_return("\t\t");
520 for (proc = vp->procs; proc != NULL; proc = proc->next) {
521 f_print(fout, "\tcase %s:\n", proc->proc_name);
522 if (proc->arg_num < 2) { /* single argument */
523 p_xdrfunc(ARG, proc->args.decls->decl.type);
525 p_xdrfunc(ARG, proc->args.argname);
527 p_xdrfunc(RESULT, proc->res_type);
532 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
536 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
540 f_print(fout, "\t\t%s = (bool_t (*)()) ",
544 f_print(fout, "\t\t%s = (char *(*)()) ",
546 if (newstyle) { /* new style: calls internal routine */
549 if ((Cflag || mtflag) && !newstyle)
550 pvname_svc(proc->proc_name, vp->vers_num);
552 pvname(proc->proc_name, vp->vers_num);
553 f_print(fout, ";\n");
554 f_print(fout, "\t\tbreak;\n\n");
556 f_print(fout, "\tdefault:\n");
557 printerr("noproc", TRANSP);
558 print_return("\t\t");
559 f_print(fout, "\t}\n");
562 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
565 printif("getargs", TRANSP, "(caddr_t) &", ARG);
567 printif("getargs", TRANSP, "&", ARG);
568 printerr("decode", TRANSP);
569 print_return("\t\t");
570 f_print(fout, "\t}\n");
574 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
575 RESULT, ROUTINE, ARG, RQSTP);
577 f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
578 RESULT, ROUTINE, ARG, RQSTP);
581 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
582 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
584 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
585 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
592 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
593 RETVAL, TRANSP, RESULT, RESULT);
596 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
597 RESULT, TRANSP, RESULT, RESULT);
599 printerr("systemerr", TRANSP);
600 f_print(fout, "\t}\n");
603 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
605 printif("freeargs", TRANSP, "&", ARG);
606 (void) sprintf(_errbuf, "unable to free arguments");
607 print_err_message("\t\t");
608 f_print(fout, "\t\texit(1);\n");
609 f_print(fout, "\t}\n");
610 /* print out free routine */
612 f_print(fout,"\tif (!");
613 pvname(def->def_name, vp->vers_num);
614 f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
615 TRANSP, RESULT, RESULT);
616 (void) sprintf(_errbuf, "unable to free results");
617 print_err_message("\t\t");
621 f_print(fout, "}\n");
626 printerr(err, transp)
630 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
634 printif(proc, transp, prefix, arg)
640 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n",
641 proc, transp, arg, prefix, arg);
648 for (; proc != NULL; proc = proc->next) {
649 if (streq(proc->proc_num, "0")) {
657 write_inetmost(infile)
660 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
661 f_print(fout, "\tint sock;\n");
662 f_print(fout, "\tint proto;\n");
663 f_print(fout, "\tstruct sockaddr_in saddr;\n");
664 f_print(fout, "\tint asize = sizeof (saddr);\n");
667 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
668 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
669 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
670 f_print(fout, "\t\t\texit(1);\n");
671 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
672 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
673 f_print(fout, "\t\t\texit(1);\n");
674 f_print(fout, "\t\tsock = 0;\n");
675 f_print(fout, "\t\t_rpcpmstart = 1;\n");
676 f_print(fout, "\t\tproto = 0;\n");
677 open_log_file(infile, "\t\t");
678 f_print(fout, "\t} else {\n");
679 write_rpc_svc_fg(infile, "\t\t");
680 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
681 print_pmapunset("\t\t");
682 f_print(fout, "\t}\n");
690 f_print(fout, "%sexit(0);\n", space);
694 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
695 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
697 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
699 f_print(fout, "%sreturn;\n", space);
704 print_pmapunset(space)
711 for (l = defined; l != NULL; l = l->next) {
712 def = (definition *) l->val;
713 if (def->def_kind == DEF_PROGRAM) {
714 for (vp = def->def.pr.versions; vp != NULL;
716 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
717 space, def->def_name, vp->vers_name);
724 print_err_message(space)
728 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
729 else if (inetdflag || pmflag)
730 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
732 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
736 * Write the server auxiliary function (_msgout, timeout)
739 write_svc_aux(nomain)
745 write_timeout_func();
749 * Write the _msgout function
757 * Avoid making _msgout() static -- it's useful to have it visible
758 * in the toplevel RPC server code.
760 f_print(fout, "static\n");
763 f_print(fout, "void _msgout(msg)\n");
764 f_print(fout, "\tchar *msg;\n");
766 f_print(fout, "void _msgout(char* msg)\n");
768 f_print(fout, "{\n");
769 f_print(fout, "#ifdef RPC_SVC_FG\n");
770 if (inetdflag || pmflag)
771 f_print(fout, "\tif (_rpcpmstart)\n");
772 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
773 f_print(fout, "\telse\n");
775 "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
776 f_print(fout, "#else\n");
777 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
778 f_print(fout, "#endif\n");
779 f_print(fout, "}\n");
783 * Write the timeout function
786 write_timeout_func(void)
792 f_print(fout, "static void\n");
794 f_print(fout, "closedown(sig)\n");
795 f_print(fout, "\tint sig;\n");
797 f_print(fout, "closedown(int sig)\n");
798 f_print(fout, "{\n");
800 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
801 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
802 f_print(fout, "\t\textern fd_set svc_fdset;\n");
803 f_print(fout, "\t\tstatic int size;\n");
804 f_print(fout, "\t\tint i, openfd;\n");
805 if (tirpcflag && pmflag) {
806 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
808 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
810 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
812 f_print(fout, "\t\t\texit(0);\n");
813 f_print(fout, "\t\tif (size == 0) {\n");
815 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
816 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
817 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
818 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
821 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
823 f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n");
825 f_print(fout, "\t\t\tsize = getdtablesize();\n");
827 f_print(fout, "\t\t}\n");
829 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
830 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
831 f_print(fout, "\t\t\t\topenfd++;\n");
832 f_print(fout, "\t\tif (openfd <= 1)\n");
833 f_print(fout, "\t\t\texit(0);\n");
834 f_print(fout, "\t}\n");
835 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
836 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
838 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
840 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
841 Cflag? "(SIG_PF)" : "(void(*)())");
842 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
843 f_print(fout, "}\n");
848 * Write the most of port monitor support
851 write_pm_most(infile, netflag)
859 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
860 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
861 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
862 f_print(fout, "\t\tchar *netid;\n");
863 if (!netflag) { /* Not included by -n option */
864 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
865 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
868 f_print(fout, "\t\tint pmclose;\n");
870 * Not necessary, defined in /usr/include/stdlib
871 * f_print(fout, "\t\textern char *getenv();\n");
874 f_print(fout, "\t\t_rpcpmstart = 1;\n");
875 open_log_file(infile, "\t\t");
876 f_print(fout, "\n\t\tif ((netid = \
877 getenv(\"NLSPROVIDER\")) == NULL) {\n");
880 f_print(fout, "\t\t/* started from inetd */\n");
881 f_print(fout, "\t\t\tpmclose = 1;\n");
885 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
886 sprintf(_errbuf, "cannot get transport info");
887 print_err_message("\t\t\t\t");
889 f_print(fout, "\n\t\t\tpmclose = \
890 (t_getstate(0) != T_DATAXFER);\n");
891 f_print(fout, "\t\t}\n");
893 * A kludgy support for inetd services. Inetd only works with
894 * sockmod, and RPC works only with timod, hence all this jugglery
896 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
898 "\t\t\tif (ioctl(0, I_POP, 0) || \
899 ioctl(0, I_PUSH, \"timod\")) {\n");
900 sprintf(_errbuf, "could not get the right module");
901 print_err_message("\t\t\t\t");
902 f_print(fout, "\t\t\t\texit(1);\n");
903 f_print(fout, "\t\t\t}\n");
904 f_print(fout, "\t\t}\n");
906 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
909 sprintf(_errbuf, "cannot create server handle");
910 print_err_message("\t\t\t");
911 f_print(fout, "\t\t\texit(1);\n");
912 f_print(fout, "\t\t}\n");
913 f_print(fout, "\t\tif (nconf)\n");
914 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
915 for (l = defined; l != NULL; l = l->next) {
916 def = (definition *) l->val;
917 if (def->def_kind != DEF_PROGRAM) {
920 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
922 "\t\tif (!svc_reg(%s, %s, %s, ",
923 TRANSP, def->def_name, vp->vers_name);
924 pvname(def->def_name, vp->vers_num);
925 f_print(fout, ", 0)) {\n");
926 (void) sprintf(_errbuf, "unable to register (%s, %s).",
927 def->def_name, vp->vers_name);
928 print_err_message("\t\t\t");
929 f_print(fout, "\t\t\texit(1);\n");
930 f_print(fout, "\t\t}\n");
934 f_print(fout, "\t\tif (pmclose) {\n");
935 f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
936 Cflag? "(SIG_PF)" : "(void(*)())");
937 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
938 f_print(fout, "\t\t}\n");
940 f_print(fout, "\t\tsvc_run();\n");
941 f_print(fout, "\t\texit(1);\n");
942 f_print(fout, "\t\t/* NOTREACHED */\n");
943 f_print(fout, "\t}");
947 * Support for backgrounding the server if self started.
950 write_rpc_svc_fg(infile, sp)
954 f_print(fout, "#ifndef RPC_SVC_FG\n");
955 f_print(fout, "%sint size;\n", sp);
957 f_print(fout, "%sstruct rlimit rl;\n", sp);
959 f_print(fout, "%sint pid, i;\n\n", sp);
960 f_print(fout, "%spid = fork();\n", sp);
961 f_print(fout, "%sif (pid < 0) {\n", sp);
962 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
963 f_print(fout, "%s\texit(1);\n", sp);
964 f_print(fout, "%s}\n", sp);
965 f_print(fout, "%sif (pid)\n", sp);
966 f_print(fout, "%s\texit(0);\n", sp);
967 /* get number of file descriptors */
969 f_print(fout, "%srl.rlim_max = 0;\n", sp);
970 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
971 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
972 f_print(fout, "%s\texit(1);\n", sp);
974 f_print(fout, "%ssize = getdtablesize();\n", sp);
977 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
978 f_print(fout, "%s\t(void) close(i);\n", sp);
979 /* Redirect stderr and stdout to console */
980 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
981 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
982 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
983 /* This removes control of the controlling terminal */
985 f_print(fout, "%ssetsid();\n", sp);
987 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
988 f_print(fout, "%sif (i >= 0) {\n", sp);
990 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
991 f_print(fout, "%s\t(void) close(i);\n", sp);
992 f_print(fout, "%s}\n", sp);
995 open_log_file(infile, sp);
996 f_print(fout, "#endif\n");
998 open_log_file(infile, sp);
1002 open_log_file(infile, sp)
1008 s = strrchr(infile, '.');
1011 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1020 * write a registration for the given transport for Inetd
1023 write_inetd_register(transp)
1037 if (streq(transp, "udp"))
1041 f_print(fout, "\n");
1044 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1045 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1047 f_print(fout, "%s\t%s = svc%s_create(%s",
1048 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
1050 f_print(fout, ", 0, 0");
1051 f_print(fout, ");\n");
1052 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1053 (void) sprintf(_errbuf, "cannot create %s service.", transp);
1054 (void) sprintf(tmpbuf, "%s\t\t", sp);
1055 print_err_message(tmpbuf);
1056 f_print(fout, "%s\t\texit(1);\n", sp);
1057 f_print(fout, "%s\t}\n", sp);
1060 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1061 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
1062 sp, isudp ? "UDP": "TCP");
1064 for (l = defined; l != NULL; l = l->next) {
1065 def = (definition *) l->val;
1066 if (def->def_kind != DEF_PROGRAM) {
1069 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1070 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
1071 sp, TRANSP, def->def_name, vp->vers_name);
1072 pvname(def->def_name, vp->vers_num);
1074 f_print(fout, ", proto)) {\n");
1076 f_print(fout, ", IPPROTO_%s)) {\n",
1077 isudp ? "UDP": "TCP");
1078 (void) sprintf(_errbuf,
1079 "unable to register (%s, %s, %s).",
1080 def->def_name, vp->vers_name, transp);
1081 print_err_message(tmpbuf);
1082 f_print(fout, "%s\t\texit(1);\n", sp);
1083 f_print(fout, "%s\t}\n", sp);
1087 f_print(fout, "\t}\n");