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.
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
27 * Mountain View, California 94043
29 * @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_svcout.c,v 1.6.6.1 2001/07/19 01:38:22 kris Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_svcout.c,v 1.3 2003/11/03 19:31:32 eirikn Exp $
34 #ident "@(#)rpc_svcout.c 1.4 90/04/13 SMI"
37 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
42 #include "rpc_parse.h"
45 static char RQSTP[] = "rqstp";
46 static char TRANSP[] = "transp";
47 static char ARG[] = "argument";
48 static char RESULT[] = "result";
49 static char ROUTINE[] = "local";
50 static char RETVAL[] = "retval";
52 char _errbuf[256]; /* For all messages */
54 void internal_proctype( proc_list * );
55 static void write_real_program( definition * );
56 static void write_program( definition *, char * );
57 static void printerr( char *, char * );
58 static void printif( char *, char *, char *, char * );
59 static void write_inetmost( char * );
60 static void print_return( char * );
61 static void print_pmapunset( char * );
62 static void print_err_message( char * );
63 static void write_timeout_func( void );
64 static void write_pm_most( char *, int );
65 static void write_rpc_svc_fg( char *, char * );
66 static void open_log_file( char *, char * );
67 static void write_msg_out( void );
68 int nullproc( proc_list * );
72 p_xdrfunc(rname, typename)
77 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
78 rname, stringfix(typename));
80 f_print(fout, "\t\txdr_%s = xdr_%s;\n",
81 rname, stringfix(typename));
85 internal_proctype(plist)
88 f_print(fout, "static ");
89 ptype(plist->res_prefix, plist->res_type, 1);
95 * write most of the service, that is, everything but the registrations.
98 write_most(infile, netflag, nomain)
99 char *infile; /* our name */
103 if (inetdflag || pmflag) {
105 var_type = (nomain? "extern" : "static");
106 f_print(fout, "%s int _rpcpmstart;", var_type);
107 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
109 f_print(fout, "%s int _rpcfdtype;", var_type);
110 f_print(fout, "\n\t\t /* Whether Stream or \
115 f_print(fout, " /* States a server can be in \
116 wrt request */\n\n");
117 f_print(fout, "#define\t_IDLE 0\n");
118 f_print(fout, "#define\t_SERVED 1\n");
119 f_print(fout, "#define\t_SERVING 2\n\n");
120 f_print(fout, "static int _rpcsvcstate = _IDLE;");
121 f_print(fout, "\t /* Set when a request is \
125 f_print(fout, "mutex_t _svcstate_lock;");
126 f_print(fout, "\t\t\t/* Mutex lock for variable _rpcsvcstate */\n");
132 write_svc_aux(nomain);
134 /* write out dispatcher and stubs */
135 write_programs(nomain? (char *)NULL : "static");
140 f_print(fout, "\nmain()\n");
141 f_print(fout, "{\n");
143 write_inetmost(infile);
144 /* Includes call to write_rpc_svc_fg() */
149 "\tregister SVCXPRT *%s;\n", TRANSP);
151 "\tstruct netconfig *nconf = NULL;\n");
153 f_print(fout, "\tpid_t pid;\n");
154 f_print(fout, "\tint i;\n");
155 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
157 if (mtflag & timerflag)
158 f_print(fout, "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
160 write_pm_most(infile, netflag);
161 f_print(fout, "\telse {\n");
162 write_rpc_svc_fg(infile, "\t\t");
163 f_print(fout, "\t}\n");
165 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
167 print_pmapunset("\t");
171 if (logflag && !inetdflag) {
172 open_log_file(infile, "\t");
177 * write a registration for the given transport
180 write_netid_register(transp)
191 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
192 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
193 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
194 sprintf(tmpbuf, "%s\t\t", sp);
195 print_err_message(tmpbuf);
196 f_print(fout, "%s\t\texit(1);\n", sp);
197 f_print(fout, "%s\t}\n", sp);
198 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
200 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
201 (void) sprintf(_errbuf, "cannot create %s service.", transp);
202 print_err_message(tmpbuf);
203 f_print(fout, "%s\t\texit(1);\n", sp);
204 f_print(fout, "%s\t}\n", sp);
206 for (l = defined; l != NULL; l = l->next) {
207 def = (definition *) l->val;
208 if (def->def_kind != DEF_PROGRAM) {
211 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
213 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
214 sp, def->def_name, vp->vers_name);
216 "%s\tif (!svc_reg(%s, %s, %s, ",
217 sp, TRANSP, def->def_name, vp->vers_name);
218 pvname(def->def_name, vp->vers_num);
219 f_print(fout, ", nconf)) {\n");
220 (void) sprintf(_errbuf,
221 "unable to register (%s, %s, %s).",
222 def->def_name, vp->vers_name, transp);
223 print_err_message(tmpbuf);
224 f_print(fout, "%s\t\texit(1);\n", sp);
225 f_print(fout, "%s\t}\n", sp);
228 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
232 * write a registration for the given transport for TLI
235 write_nettype_register(transp)
242 for (l = defined; l != NULL; l = l->next) {
243 def = (definition *) l->val;
244 if (def->def_kind != DEF_PROGRAM) {
247 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
248 f_print(fout, "\tif (!svc_create(");
249 pvname(def->def_name, vp->vers_num);
250 f_print(fout, ", %s, %s, \"%s\")) {\n",
251 def->def_name, vp->vers_name, transp);
252 (void) sprintf(_errbuf,
253 "unable to create (%s, %s) for %s.",
254 def->def_name, vp->vers_name, transp);
255 print_err_message("\t\t");
256 f_print(fout, "\t\texit(1);\n");
257 f_print(fout, "\t}\n");
263 * write the rest of the service
270 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
271 (void) sprintf(_errbuf, "could not create a handle");
272 print_err_message("\t\t");
273 f_print(fout, "\t\texit(1);\n");
274 f_print(fout, "\t}\n");
276 f_print(fout, "\tif (_rpcpmstart) {\n");
278 "\t\t(void) signal(SIGALRM, %s closedown);\n",
279 Cflag? "(SIG_PF)":"(void(*)())");
280 f_print(fout, "\t\t(void) \
281 alarm(_RPCSVC_CLOSEDOWN/2);\n");
282 f_print(fout, "\t}\n");
285 f_print(fout, "\tsvc_run();\n");
286 (void) sprintf(_errbuf, "svc_run returned");
287 print_err_message("\t");
288 f_print(fout, "\texit(1);\n");
289 f_print(fout, "\t/* NOTREACHED */\n");
290 f_print(fout, "}\n");
294 write_programs(storage)
300 /* write out stubs for procedure definitions */
301 for (l = defined; l != NULL; l = l->next) {
302 def = (definition *) l->val;
303 if (def->def_kind == DEF_PROGRAM) {
304 write_real_program(def);
308 /* write out dispatcher for each program */
309 for (l = defined; l != NULL; l = l->next) {
310 def = (definition *) l->val;
311 if (def->def_kind == DEF_PROGRAM) {
312 write_program(def, storage);
320 * write out definition of internal function (e.g. _printmsg_1(...))
321 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
322 * Unpacks single user argument of printmsg_1 to call-by-value format
323 * expected by printmsg_1.
326 write_real_program(def)
333 if (!newstyle) return; /* not needed for old style */
334 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
335 for (proc = vp->procs; proc != NULL; proc = proc->next) {
338 internal_proctype(proc);
340 f_print(fout, "int");
341 f_print(fout, "\n_");
342 pvname(proc->proc_name, vp->vers_num);
346 if (proc->arg_num > 1)
347 f_print(fout, proc->args.argname);
349 ptype(proc->args.decls->decl.prefix,
350 proc->args.decls->decl.type, 0);
352 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
358 f_print(fout, " *argp, struct svc_req *%s)\n",
363 f_print(fout, "(argp, %s, %s)\n", RESULT, RQSTP);
365 f_print(fout, "(argp, %s)\n", RQSTP);
367 if (proc->arg_num > 1)
368 f_print(fout, "\t%s *argp;\n",
372 ptype(proc->args.decls->decl.prefix,
373 proc->args.decls->decl.type, 0);
374 f_print(fout, " *argp;\n");
377 f_print(fout, "\tvoid *%s;\n", RESULT);
378 f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
381 f_print(fout, "{\n");
382 f_print(fout, "\treturn (");
383 if (Cflag || mtflag) /* for mtflag, arguments are different */
384 pvname_svc(proc->proc_name, vp->vers_num);
386 pvname(proc->proc_name, vp->vers_num);
388 if (proc->arg_num < 2) { /* single argument */
389 if (!streq(proc->args.decls->decl.type, "void"))
390 f_print(fout, "*argp, "); /* non-void */
392 for (l = proc->args.decls; l != NULL;
394 f_print(fout, "argp->%s, ",
398 f_print(fout, "%s, ",RESULT);
399 f_print(fout, "%s));\n}\n", RQSTP);
405 write_program(def, storage)
413 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
415 if (storage != NULL) {
416 f_print(fout, "%s ", storage);
418 f_print(fout, "void\n");
419 pvname(def->def_name, vp->vers_num);
422 f_print(fout, "(struct svc_req *%s, ", RQSTP);
423 f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
425 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
426 f_print(fout, " struct svc_req *%s;\n", RQSTP);
427 f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
430 f_print(fout, "{\n");
433 f_print(fout, "\tunion {\n");
434 for (proc = vp->procs; proc != NULL; proc = proc->next) {
435 if (proc->arg_num < 2) { /* single argument */
436 if (streq(proc->args.decls->decl.type,
441 f_print(fout, "\t\t");
442 ptype(proc->args.decls->decl.prefix,
443 proc->args.decls->decl.type, 0);
444 pvname(proc->proc_name, vp->vers_num);
445 f_print(fout, "_arg;\n");
449 f_print(fout, "\t\t%s", proc->args.argname);
451 pvname(proc->proc_name, vp->vers_num);
452 f_print(fout, "_arg;\n");
456 f_print(fout, "\t\tint fill;\n");
458 f_print(fout, "\t} %s;\n", ARG);
461 f_print(fout, "\tunion {\n");
462 for (proc = vp->procs; proc != NULL; proc = proc->next) {
463 f_print(fout, "\t\t");
464 ptype(proc->res_prefix, proc->res_type, 0);
465 pvname(proc->proc_name, vp->vers_num);
466 f_print(fout, "_res;\n");
468 f_print(fout, "\t} %s;\n", RESULT);
469 f_print(fout, "\tbool_t %s;\n", RETVAL);
472 f_print(fout, "\tchar *%s;\n", RESULT);
475 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n",
479 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
483 "\tchar *(*%s)(char *, struct svc_req *);\n",
487 "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n",
490 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
492 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
498 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
500 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
502 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
505 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
506 if (!nullproc(vp->procs)) {
507 f_print(fout, "\tcase NULLPROC:\n");
510 ? "\t\t(void) svc_sendreply(%s,\n\t\t\t\
511 (xdrproc_t) xdr_void, (char *)NULL);\n"
512 : "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\t\
515 print_return("\t\t");
518 for (proc = vp->procs; proc != NULL; proc = proc->next) {
519 f_print(fout, "\tcase %s:\n", proc->proc_name);
520 if (proc->arg_num < 2) { /* single argument */
521 p_xdrfunc(ARG, proc->args.decls->decl.type);
523 p_xdrfunc(ARG, proc->args.argname);
525 p_xdrfunc(RESULT, proc->res_type);
530 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
534 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
538 f_print(fout, "\t\t%s = (bool_t (*)()) ",
542 f_print(fout, "\t\t%s = (char *(*)()) ",
544 if (newstyle) { /* new style: calls internal routine */
547 if ((Cflag || mtflag) && !newstyle)
548 pvname_svc(proc->proc_name, vp->vers_num);
550 pvname(proc->proc_name, vp->vers_num);
551 f_print(fout, ";\n");
552 f_print(fout, "\t\tbreak;\n\n");
554 f_print(fout, "\tdefault:\n");
555 printerr("noproc", TRANSP);
556 print_return("\t\t");
557 f_print(fout, "\t}\n");
560 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
563 printif("getargs", TRANSP, "(caddr_t) &", ARG);
565 printif("getargs", TRANSP, "&", ARG);
566 printerr("decode", TRANSP);
567 print_return("\t\t");
568 f_print(fout, "\t}\n");
572 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
573 RESULT, ROUTINE, ARG, RQSTP);
575 f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
576 RESULT, ROUTINE, ARG, RQSTP);
579 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
580 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
582 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
583 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
590 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
591 RETVAL, TRANSP, RESULT, RESULT);
594 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
595 RESULT, TRANSP, RESULT, RESULT);
597 printerr("systemerr", TRANSP);
598 f_print(fout, "\t}\n");
601 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
603 printif("freeargs", TRANSP, "&", ARG);
604 (void) sprintf(_errbuf, "unable to free arguments");
605 print_err_message("\t\t");
606 f_print(fout, "\t\texit(1);\n");
607 f_print(fout, "\t}\n");
608 /* print out free routine */
610 f_print(fout,"\tif (!");
611 pvname(def->def_name, vp->vers_num);
612 f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
613 TRANSP, RESULT, RESULT);
614 (void) sprintf(_errbuf, "unable to free results");
615 print_err_message("\t\t");
619 f_print(fout, "}\n");
624 printerr(err, transp)
628 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
632 printif(proc, transp, prefix, arg)
638 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
639 proc, transp, arg, prefix, arg);
646 for (; proc != NULL; proc = proc->next) {
647 if (streq(proc->proc_num, "0")) {
655 write_inetmost(infile)
658 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
659 f_print(fout, "\tint sock;\n");
660 f_print(fout, "\tint proto;\n");
661 f_print(fout, "\tstruct sockaddr_in saddr;\n");
662 f_print(fout, "\tint asize = sizeof (saddr);\n");
665 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
666 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
667 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
668 f_print(fout, "\t\t\texit(1);\n");
669 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
670 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
671 f_print(fout, "\t\t\texit(1);\n");
672 f_print(fout, "\t\tsock = 0;\n");
673 f_print(fout, "\t\t_rpcpmstart = 1;\n");
674 f_print(fout, "\t\tproto = 0;\n");
675 open_log_file(infile, "\t\t");
676 f_print(fout, "\t} else {\n");
677 write_rpc_svc_fg(infile, "\t\t");
678 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
679 print_pmapunset("\t\t");
680 f_print(fout, "\t}\n");
688 f_print(fout, "%sexit(0);\n", space);
692 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
693 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
695 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
697 f_print(fout, "%sreturn;\n", space);
702 print_pmapunset(space)
709 for (l = defined; l != NULL; l = l->next) {
710 def = (definition *) l->val;
711 if (def->def_kind == DEF_PROGRAM) {
712 for (vp = def->def.pr.versions; vp != NULL;
714 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
715 space, def->def_name, vp->vers_name);
722 print_err_message(space)
726 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
727 else if (inetdflag || pmflag)
728 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
730 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
734 * Write the server auxiliary function (_msgout, timeout)
737 write_svc_aux(nomain)
743 write_timeout_func();
747 * Write the _msgout function
755 * Avoid making _msgout() static -- it's useful to have it visible
756 * in the toplevel RPC server code.
758 f_print(fout, "static\n");
761 f_print(fout, "void _msgout(msg)\n");
762 f_print(fout, "\tchar *msg;\n");
764 f_print(fout, "void _msgout(char* msg)\n");
766 f_print(fout, "{\n");
767 f_print(fout, "#ifdef RPC_SVC_FG\n");
768 if (inetdflag || pmflag)
769 f_print(fout, "\tif (_rpcpmstart)\n");
770 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
771 f_print(fout, "\telse\n");
773 "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
774 f_print(fout, "#else\n");
775 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
776 f_print(fout, "#endif\n");
777 f_print(fout, "}\n");
781 * Write the timeout function
784 write_timeout_func(void)
790 f_print(fout, "static void\n");
792 f_print(fout, "closedown(sig)\n");
793 f_print(fout, "\tint sig;\n");
795 f_print(fout, "closedown(int sig)\n");
796 f_print(fout, "{\n");
798 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
799 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
800 f_print(fout, "\t\textern fd_set svc_fdset;\n");
801 f_print(fout, "\t\tstatic int size;\n");
802 f_print(fout, "\t\tint i, openfd;\n");
803 if (tirpcflag && pmflag) {
804 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
806 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
808 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
810 f_print(fout, "\t\t\texit(0);\n");
811 f_print(fout, "\t\tif (size == 0) {\n");
813 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
814 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
815 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
816 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
819 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
821 f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n");
823 f_print(fout, "\t\t\tsize = getdtablesize();\n");
825 f_print(fout, "\t\t}\n");
827 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
828 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
829 f_print(fout, "\t\t\t\topenfd++;\n");
830 f_print(fout, "\t\tif (openfd <= 1)\n");
831 f_print(fout, "\t\t\texit(0);\n");
832 f_print(fout, "\t}\n");
833 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
834 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
836 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
838 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
839 Cflag? "(SIG_PF)" : "(void(*)())");
840 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
841 f_print(fout, "}\n");
846 * Write the most of port monitor support
849 write_pm_most(infile, netflag)
857 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
858 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
859 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
860 f_print(fout, "\t\tchar *netid;\n");
861 if (!netflag) { /* Not included by -n option */
862 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
863 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
866 f_print(fout, "\t\tint pmclose;\n");
868 * Not necessary, defined in /usr/include/stdlib
869 * f_print(fout, "\t\textern char *getenv();\n");
872 f_print(fout, "\t\t_rpcpmstart = 1;\n");
873 open_log_file(infile, "\t\t");
874 f_print(fout, "\n\t\tif ((netid = \
875 getenv(\"NLSPROVIDER\")) == NULL) {\n");
878 f_print(fout, "\t\t/* started from inetd */\n");
879 f_print(fout, "\t\t\tpmclose = 1;\n");
883 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
884 sprintf(_errbuf, "cannot get transport info");
885 print_err_message("\t\t\t\t");
887 f_print(fout, "\n\t\t\tpmclose = \
888 (t_getstate(0) != T_DATAXFER);\n");
889 f_print(fout, "\t\t}\n");
891 * A kludgy support for inetd services. Inetd only works with
892 * sockmod, and RPC works only with timod, hence all this jugglery
894 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
896 "\t\t\tif (ioctl(0, I_POP, 0) || \
897 ioctl(0, I_PUSH, \"timod\")) {\n");
898 sprintf(_errbuf, "could not get the right module");
899 print_err_message("\t\t\t\t");
900 f_print(fout, "\t\t\t\texit(1);\n");
901 f_print(fout, "\t\t\t}\n");
902 f_print(fout, "\t\t}\n");
904 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
907 sprintf(_errbuf, "cannot create server handle");
908 print_err_message("\t\t\t");
909 f_print(fout, "\t\t\texit(1);\n");
910 f_print(fout, "\t\t}\n");
911 f_print(fout, "\t\tif (nconf)\n");
912 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
913 for (l = defined; l != NULL; l = l->next) {
914 def = (definition *) l->val;
915 if (def->def_kind != DEF_PROGRAM) {
918 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
920 "\t\tif (!svc_reg(%s, %s, %s, ",
921 TRANSP, def->def_name, vp->vers_name);
922 pvname(def->def_name, vp->vers_num);
923 f_print(fout, ", 0)) {\n");
924 (void) sprintf(_errbuf, "unable to register (%s, %s).",
925 def->def_name, vp->vers_name);
926 print_err_message("\t\t\t");
927 f_print(fout, "\t\t\texit(1);\n");
928 f_print(fout, "\t\t}\n");
932 f_print(fout, "\t\tif (pmclose) {\n");
933 f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
934 Cflag? "(SIG_PF)" : "(void(*)())");
935 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
936 f_print(fout, "\t\t}\n");
938 f_print(fout, "\t\tsvc_run();\n");
939 f_print(fout, "\t\texit(1);\n");
940 f_print(fout, "\t\t/* NOTREACHED */\n");
941 f_print(fout, "\t}");
945 * Support for backgrounding the server if self started.
948 write_rpc_svc_fg(infile, sp)
952 f_print(fout, "#ifndef RPC_SVC_FG\n");
953 f_print(fout, "%sint size;\n", sp);
955 f_print(fout, "%sstruct rlimit rl;\n", sp);
957 f_print(fout, "%sint pid, i;\n\n", sp);
958 f_print(fout, "%spid = fork();\n", sp);
959 f_print(fout, "%sif (pid < 0) {\n", sp);
960 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
961 f_print(fout, "%s\texit(1);\n", sp);
962 f_print(fout, "%s}\n", sp);
963 f_print(fout, "%sif (pid)\n", sp);
964 f_print(fout, "%s\texit(0);\n", sp);
965 /* get number of file descriptors */
967 f_print(fout, "%srl.rlim_max = 0;\n", sp);
968 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
969 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
970 f_print(fout, "%s\texit(1);\n", sp);
972 f_print(fout, "%ssize = getdtablesize();\n", sp);
975 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
976 f_print(fout, "%s\t(void) close(i);\n", sp);
977 /* Redirect stderr and stdout to console */
978 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
979 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
980 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
981 /* This removes control of the controlling terminal */
983 f_print(fout, "%ssetsid();\n", sp);
985 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
986 f_print(fout, "%sif (i >= 0) {\n", sp);
988 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
989 f_print(fout, "%s\t(void) close(i);\n", sp);
990 f_print(fout, "%s}\n", sp);
993 open_log_file(infile, sp);
994 f_print(fout, "#endif\n");
996 open_log_file(infile, sp);
1000 open_log_file(infile, sp)
1006 s = strrchr(infile, '.');
1009 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1018 * write a registration for the given transport for Inetd
1021 write_inetd_register(transp)
1035 if (streq(transp, "udp"))
1039 f_print(fout, "\n");
1042 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1043 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1045 f_print(fout, "%s\t%s = svc%s_create(%s",
1046 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
1048 f_print(fout, ", 0, 0");
1049 f_print(fout, ");\n");
1050 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1051 (void) sprintf(_errbuf, "cannot create %s service.", transp);
1052 (void) sprintf(tmpbuf, "%s\t\t", sp);
1053 print_err_message(tmpbuf);
1054 f_print(fout, "%s\t\texit(1);\n", sp);
1055 f_print(fout, "%s\t}\n", sp);
1058 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1059 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
1060 sp, isudp ? "UDP": "TCP");
1062 for (l = defined; l != NULL; l = l->next) {
1063 def = (definition *) l->val;
1064 if (def->def_kind != DEF_PROGRAM) {
1067 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1068 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
1069 sp, TRANSP, def->def_name, vp->vers_name);
1070 pvname(def->def_name, vp->vers_num);
1072 f_print(fout, ", proto)) {\n");
1074 f_print(fout, ", IPPROTO_%s)) {\n",
1075 isudp ? "UDP": "TCP");
1076 (void) sprintf(_errbuf,
1077 "unable to register (%s, %s, %s).",
1078 def->def_name, vp->vers_name, transp);
1079 print_err_message(tmpbuf);
1080 f_print(fout, "%s\t\texit(1);\n", sp);
1081 f_print(fout, "%s\t}\n", sp);
1085 f_print(fout, "\t}\n");