home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp / src / devtools / rpcgen / rpc_svcout.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-09  |  25.8 KB  |  932 lines

  1. /*
  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.
  9.  *
  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.
  13.  *
  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.
  17.  *
  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.
  21.  *
  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.
  25.  *
  26.  * Sun Microsystems, Inc.
  27.  * 2550 Garcia Avenue
  28.  * Mountain View, California  94043
  29.  */
  30.  
  31. #ifndef lint
  32.  static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
  33. #endif
  34.  
  35. /*
  36.  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
  37.  */
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include "rpc_parse.h"
  41. #include "rpc_util.h"
  42.  
  43. #ifdef __STDC__
  44. #include "rpc_scan.h"
  45. #include "protos.h"
  46.  
  47. static void p_xdrfunc ( char *rname , char *typename );
  48. void internal_proctype ( proc_list *plist );
  49. void write_most ( char *infile , int netflag , int nomain );
  50. void write_netid_register ( char *transp );
  51. void write_nettype_register ( char *transp );
  52. void write_rest ( void );
  53. void write_programs ( char *storage );
  54. static void write_real_program ( definition *def );
  55. static void write_program ( definition *def , char *storage );
  56. static void printerr ( char *err , char *transp );
  57. static void printif ( char *proc , char *transp , char *prefix , char *arg );
  58. int nullproc ( proc_list *proc );
  59. static void write_inetmost ( char *infile );
  60. static void print_return ( char *space );
  61. static void print_pmapunset ( char *space );
  62. static void print_err_message ( char *space );
  63. void write_svc_aux ( int nomain );
  64. void write_msg_out ( void );
  65. static void write_timeout_func ( void );
  66. static void write_pm_most ( char *infile , int netflag );
  67. static void write_rpc_svc_fg ( char *infile , char *sp );
  68. static void open_log_file ( char *infile , char *sp );
  69. void write_inetd_register ( char *transp );
  70. #endif
  71.  
  72. static char RQSTP[] = "rqstp";
  73. static char TRANSP[] = "transp";
  74. static char ARG[] = "argument";
  75. static char RESULT[] = "result";
  76. static char ROUTINE[] = "local";
  77.  
  78. char _errbuf[256];    /* For all messages */
  79.  
  80. void internal_proctype(proc_list *plist);
  81.  
  82. static void
  83. p_xdrfunc(char *rname, char *typename)
  84. {
  85.   if( Cflag )
  86.     f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname, 
  87.         stringfix(typename) );
  88.   else
  89.     f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix(typename) );
  90. }    
  91.  
  92. void
  93. internal_proctype(proc_list *plist)
  94. {
  95.     f_print(fout, "static ");
  96.     ptype( plist->res_prefix, plist->res_type, 1 );
  97.     f_print( fout, "*" );
  98. }
  99.  
  100.  
  101. /*
  102.  * write most of the service, that is, everything but the registrations. 
  103.  */
  104. void
  105. write_most(char *infile, int netflag, int nomain)
  106.                          /* our name */
  107.                 
  108.                    
  109. {
  110.     if (inetdflag || pmflag) {
  111.             char* var_type;
  112. #ifdef linux /* FIXME: amigados ? */
  113.         var_type = (nomain? "extern" : "");
  114. #else
  115.         var_type = (nomain? "extern" : "static");
  116. #endif
  117.         f_print(fout, "%s int _rpcpmstart;", var_type );
  118.         f_print(fout, "\t\t/* Started by a port monitor ? */\n"); 
  119.         f_print(fout, "%s int _rpcfdtype;", var_type );
  120.         f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
  121.         if (timerflag) {
  122.             f_print(fout, "%s int _rpcsvcdirty;", var_type );
  123.             f_print(fout, "\t/* Still serving ? */\n");
  124.         }
  125.         write_svc_aux( nomain );
  126.     }
  127.     /* write out dispatcher and stubs */
  128.     write_programs( nomain? (char *)NULL : "static" );
  129.  
  130.         if( nomain ) 
  131.       return;
  132.  
  133. #if defined(linux) || defined(amigados)
  134.     if (Cflag)
  135.         f_print(fout, "\nint\nmain(int argc, char **argv)\n");
  136.     else {
  137.         f_print(fout, "\nint\nmain(argc, argv)\n");
  138.         f_print(fout, "\tint argc;\n");
  139.         f_print(fout, "\tchar **argv;\n");
  140.     }
  141. #else
  142.     f_print(fout, "\nmain()\n");
  143. #endif
  144.     f_print(fout, "{\n");
  145.     if (inetdflag) {
  146.         write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */
  147.     } else {
  148.       if( tirpcflag ) {
  149.         if (netflag) {
  150.             f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
  151.             f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
  152.         }
  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");
  156.         write_pm_most(infile, netflag);
  157.         f_print(fout, "\telse {\n");
  158.         write_rpc_svc_fg(infile, "\t\t");
  159.         f_print(fout, "\t}\n");
  160.           } else {
  161.         f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
  162.         f_print(fout, "\n");
  163.         print_pmapunset("\t");
  164.           }
  165.     }
  166.  
  167.     if (logflag && !inetdflag) {
  168.         open_log_file(infile, "\t");
  169.     }
  170. }
  171.  
  172. /*
  173.  * write a registration for the given transport 
  174.  */
  175. void
  176. write_netid_register(char *transp)
  177. {
  178.     list *l;
  179.     definition *def;
  180.     version_list *vp;
  181.     char *sp;
  182.     char tmpbuf[32];
  183.  
  184.     sp = "";
  185.     f_print(fout, "\n");
  186.     f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
  187.     f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
  188.     (void) sprintf(_errbuf, "cannot find %s netid.", transp);
  189.     sprintf(tmpbuf, "%s\t\t", sp);
  190.     print_err_message(tmpbuf);
  191.     f_print(fout, "%s\t\texit(1);\n", sp);
  192.     f_print(fout, "%s\t}\n", sp);
  193.     f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
  194.             sp, TRANSP, transp);
  195.     f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
  196.     (void) sprintf(_errbuf, "cannot create %s service.", transp);
  197.     print_err_message(tmpbuf);
  198.     f_print(fout, "%s\t\texit(1);\n", sp);
  199.     f_print(fout, "%s\t}\n", sp);
  200.  
  201.     for (l = defined; l != NULL; l = l->next) {
  202.         def = (definition *) l->val;
  203.         if (def->def_kind != DEF_PROGRAM) {
  204.             continue;
  205.         }
  206.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  207.             f_print(fout,
  208.                 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
  209.                 sp, def->def_name, vp->vers_name);
  210.             f_print(fout,
  211.                 "%s\tif (!svc_reg(%s, %s, %s, ",
  212.                 sp, TRANSP, def->def_name, vp->vers_name);
  213.             pvname(def->def_name, vp->vers_num);
  214.             f_print(fout, ", nconf)) {\n");
  215.             (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
  216.                     def->def_name, vp->vers_name, transp);
  217.             print_err_message(tmpbuf);
  218.             f_print(fout, "%s\t\texit(1);\n", sp);
  219.             f_print(fout, "%s\t}\n", sp);
  220.         }
  221.     }
  222.     f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
  223. }
  224.  
  225. /*
  226.  * write a registration for the given transport for TLI
  227.  */
  228. void
  229. write_nettype_register(char *transp)
  230. {
  231.     list *l;
  232.     definition *def;
  233.     version_list *vp;
  234.  
  235.     for (l = defined; l != NULL; l = l->next) {
  236.         def = (definition *) l->val;
  237.         if (def->def_kind != DEF_PROGRAM) {
  238.             continue;
  239.         }
  240.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  241.             f_print(fout, "\tif (!svc_create(");
  242.             pvname(def->def_name, vp->vers_num);
  243.             f_print(fout, ", %s, %s, \"%s\")) {\n ",
  244.                 def->def_name, vp->vers_name, transp);
  245.             (void) sprintf(_errbuf,
  246.                 "unable to create (%s, %s) for %s.",
  247.                     def->def_name, vp->vers_name, transp);
  248.             print_err_message("\t\t");
  249.             f_print(fout, "\t\texit(1);\n");
  250.             f_print(fout, "\t}\n");
  251.         }
  252.     }
  253. }
  254.  
  255. /*
  256.  * write the rest of the service 
  257.  */
  258. void
  259. write_rest(void)
  260. {
  261.     f_print(fout, "\n");
  262.     if (inetdflag) {
  263.         f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
  264.         (void) sprintf(_errbuf, "could not create a handle");
  265.         print_err_message("\t\t");
  266.         f_print(fout, "\t\texit(1);\n");
  267.         f_print(fout, "\t}\n");
  268.         if (timerflag) {
  269.             f_print(fout, "\tif (_rpcpmstart) {\n");
  270.             f_print(fout, 
  271.                 "\t\t(void) signal(SIGALRM, %s closedown);\n",
  272.                 Cflag? "(SIG_PF)" : "(void(*)())" );
  273.             f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
  274.             f_print(fout, "\t}\n");
  275.         }
  276.     }
  277.     f_print(fout, "\tsvc_run();\n");
  278.     (void) sprintf(_errbuf, "svc_run returned");
  279.     print_err_message("\t");
  280. #ifdef amigados
  281.     print_pmapunset("\t"); /* unset on service exit, too */
  282. #endif
  283.     f_print(fout, "\texit(1);\n");
  284.     f_print(fout, "\t/* NOTREACHED */\n");
  285.     f_print(fout, "}\n");
  286. }
  287.  
  288. void
  289. write_programs(char *storage)
  290. {
  291.     list *l;
  292.     definition *def;
  293.  
  294.     /* write out stubs for procedure  definitions */
  295.     for (l = defined; l != NULL; l = l->next) {
  296.         def = (definition *) l->val;
  297.         if (def->def_kind == DEF_PROGRAM) {
  298.             write_real_program(def);
  299.         }
  300.     }
  301.  
  302.     /* write out dispatcher for each program */
  303.     for (l = defined; l != NULL; l = l->next) {
  304.         def = (definition *) l->val;
  305.         if (def->def_kind == DEF_PROGRAM) {
  306.             write_program(def, storage);
  307.         }
  308.     }
  309.  
  310.  
  311. }
  312.  
  313. /* write out definition of internal function (e.g. _printmsg_1(...))
  314.    which calls server's defintion of actual function (e.g. printmsg_1(...)).
  315.    Unpacks single user argument of printmsg_1 to call-by-value format
  316.    expected by printmsg_1. */
  317. static void
  318. write_real_program(definition *def)
  319. {
  320.     version_list *vp;
  321.     proc_list *proc;
  322.     decl_list *l;
  323.  
  324.     if( !newstyle ) return;  /* not needed for old style */
  325.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  326.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  327.             f_print(fout, "\n");
  328.             internal_proctype(proc);
  329.             f_print(fout, "\n_");
  330.             pvname(proc->proc_name, vp->vers_num);
  331.             if( Cflag ) {
  332.               f_print(fout, "(" );
  333.               /* arg name */
  334.               if (proc->arg_num > 1)
  335.                 f_print(fout, proc->args.argname);
  336.               else
  337.                 ptype(proc->args.decls->decl.prefix, 
  338.                   proc->args.decls->decl.type, 0);
  339.               f_print(fout, " *argp, struct svc_req *%s)\n", 
  340.                   RQSTP);
  341.             } else {
  342.               f_print(fout, "(argp, %s)\n", RQSTP );
  343.               /* arg name */
  344.               if (proc->arg_num > 1)
  345.                 f_print(fout, "\t%s *argp;\n", proc->args.argname);
  346.               else {
  347.                 f_print(fout, "\t");
  348.                 ptype(proc->args.decls->decl.prefix, 
  349.                   proc->args.decls->decl.type, 0);
  350.                 f_print(fout, " *argp;\n");
  351.               }
  352.               f_print(fout, "    struct svc_req *%s;\n", RQSTP);
  353.             }
  354.  
  355.             f_print(fout, "{\n");
  356.             f_print(fout, "\treturn(");
  357.             if( Cflag )
  358.               pvname_svc(proc->proc_name, vp->vers_num);
  359.             else
  360.               pvname(proc->proc_name, vp->vers_num);
  361.             f_print(fout, "(");
  362.             if (proc->arg_num < 2) { /* single argument */
  363.               if (!streq( proc->args.decls->decl.type, "void"))
  364.                 f_print(fout, "*argp, ");  /* non-void */
  365.             } else {
  366.               for (l = proc->args.decls;  l != NULL; l = l->next) 
  367.                 f_print(fout, "argp->%s, ", l->decl.name);
  368.             }
  369.             f_print(fout, "%s));\n}\n", RQSTP);
  370.         }         
  371.     }
  372. }
  373.  
  374. static void
  375. write_program(definition *def, char *storage)
  376. {
  377.     version_list *vp;
  378.     proc_list *proc;
  379.     int filled;
  380.  
  381.     for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  382.         f_print(fout, "\n");
  383.         if (storage != NULL) {
  384.             f_print(fout, "%s ", storage);
  385.         }
  386.         f_print(fout, "void\n");
  387.         pvname(def->def_name, vp->vers_num);
  388.  
  389.         if (Cflag) {
  390.            f_print(fout, "(struct svc_req *%s, ", RQSTP);
  391.            f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
  392.         } else {
  393.            f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
  394.            f_print(fout, "    struct svc_req *%s;\n", RQSTP);
  395.            f_print(fout, "    register SVCXPRT *%s;\n", TRANSP);
  396.         }
  397.  
  398.         f_print(fout, "{\n");
  399.  
  400.         filled = 0;
  401.         f_print(fout, "\tunion {\n");
  402.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  403.             if (proc->arg_num < 2) { /* single argument */
  404.                 if (streq(proc->args.decls->decl.type, 
  405.                       "void")) {
  406.                     continue;
  407.                 }
  408.                 filled = 1;
  409.                 f_print(fout, "\t\t");
  410.                 ptype(proc->args.decls->decl.prefix, 
  411.                       proc->args.decls->decl.type, 0);
  412.                 pvname(proc->proc_name, vp->vers_num);
  413.                 f_print(fout, "_arg;\n");
  414.  
  415.             }
  416.             else {
  417.                 filled = 1;
  418.                 f_print(fout, "\t\t%s", proc->args.argname);
  419.                 f_print(fout, " ");
  420.                 pvname(proc->proc_name, vp->vers_num);
  421.                 f_print(fout, "_arg;\n");
  422.             }
  423.         }
  424.         if (!filled) {
  425.             f_print(fout, "\t\tint fill;\n");
  426.         }
  427.         f_print(fout, "\t} %s;\n", ARG);
  428.         f_print(fout, "\tchar *%s;\n", RESULT);
  429.  
  430.         if (Cflag) {
  431.             f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
  432.             f_print(fout,
  433.                 "\tchar *(*%s)(char *, struct svc_req *);\n",
  434.                 ROUTINE);
  435.         } else {
  436.             f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
  437.             f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
  438.         }
  439.  
  440.         f_print(fout, "\n");
  441.  
  442.         if (timerflag)
  443.             f_print(fout, "\t_rpcsvcdirty = 1;\n");
  444.         f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
  445.         if (!nullproc(vp->procs)) {
  446.             f_print(fout, "\tcase NULLPROC:\n");
  447.             f_print(fout,
  448.             Cflag
  449.             ? "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
  450.             : "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
  451.                     TRANSP);
  452.             print_return("\t\t");
  453.             f_print(fout, "\n");
  454.         }
  455.         for (proc = vp->procs; proc != NULL; proc = proc->next) {
  456.             f_print(fout, "\tcase %s:\n", proc->proc_name);
  457.             if (proc->arg_num < 2) { /* single argument */
  458.               p_xdrfunc( ARG, proc->args.decls->decl.type);
  459.             } else {
  460.               p_xdrfunc( ARG, proc->args.argname);
  461.             }
  462.             p_xdrfunc( RESULT, proc->res_type);
  463.             if( Cflag )
  464.                 f_print(fout,
  465.                     "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
  466.                     ROUTINE);
  467.             else
  468.                 f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
  469.  
  470.             if (newstyle) { /* new style: calls internal routine */
  471.                 f_print(fout,"_");
  472.             }
  473.             if( Cflag && !newstyle )
  474.               pvname_svc(proc->proc_name, vp->vers_num);
  475.             else
  476.               pvname(proc->proc_name, vp->vers_num);
  477.             f_print(fout, ";\n");
  478.             f_print(fout, "\t\tbreak;\n\n");
  479.         }
  480.         f_print(fout, "\tdefault:\n");
  481.         printerr("noproc", TRANSP);
  482.         print_return("\t\t");
  483.         f_print(fout, "\t}\n");
  484.  
  485.         f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
  486.         if (Cflag)
  487.             printif("getargs", TRANSP, "(caddr_t) &", ARG);
  488.         else
  489.             printif("getargs", TRANSP, "&", ARG);
  490.         printerr("decode", TRANSP);
  491.         print_return("\t\t");
  492.         f_print(fout, "\t}\n");
  493.  
  494.         if (Cflag)
  495.             f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
  496.                 RESULT, ROUTINE, ARG, RQSTP);
  497.         else
  498.             f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
  499.                 RESULT, ROUTINE, ARG, RQSTP);
  500.         f_print(fout, 
  501.             "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
  502.             RESULT, TRANSP, RESULT, RESULT);
  503.         printerr("systemerr", TRANSP);
  504.         f_print(fout, "\t}\n");
  505.  
  506.         if (Cflag)
  507.             printif("freeargs", TRANSP, "(caddr_t) &", ARG);
  508.         else
  509.             printif("freeargs", TRANSP, "&", ARG);
  510.         (void) sprintf(_errbuf, "unable to free arguments");
  511.         print_err_message("\t\t");
  512.         f_print(fout, "\t\texit(1);\n");
  513.         f_print(fout, "\t}\n");
  514.         print_return("\t");
  515.         f_print(fout, "}\n");
  516.     }
  517. }
  518.  
  519. static void
  520. printerr(char *err, char *transp)
  521. {
  522.     f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
  523. }
  524.  
  525. static void
  526. printif(char *proc, char *transp, char *prefix, char *arg)
  527. {
  528.     f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
  529.         proc, transp, arg, prefix, arg);
  530. }
  531.  
  532. int
  533. nullproc(proc_list *proc)
  534. {
  535.     for (; proc != NULL; proc = proc->next) {
  536.         if (streq(proc->proc_num, "0")) {
  537.             return (1);
  538.         }
  539.     }
  540.     return (0);
  541. }
  542.  
  543. static void
  544. write_inetmost(char *infile)
  545. {
  546.     f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
  547.     f_print(fout, "\tint sock;\n");
  548.     f_print(fout, "\tint proto;\n");
  549.     f_print(fout, "\tstruct sockaddr_in saddr;\n");
  550.     f_print(fout, "\tint asize = sizeof (saddr);\n");
  551.     f_print(fout, "\n");
  552.     f_print(fout, 
  553.     "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
  554.     f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
  555.     f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
  556.     f_print(fout, "\t\t\texit(1);\n");
  557.     f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
  558.     f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
  559.     f_print(fout, "\t\t\texit(1);\n");
  560.     f_print(fout, "\t\tsock = 0;\n");
  561.     f_print(fout, "\t\t_rpcpmstart = 1;\n");
  562.     f_print(fout, "\t\tproto = 0;\n");
  563.     open_log_file(infile, "\t\t");
  564.     f_print(fout, "\t} else {\n");
  565.     write_rpc_svc_fg(infile, "\t\t");
  566.     f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
  567.     print_pmapunset("\t\t");
  568.     f_print(fout, "\t}\n");
  569. }
  570.  
  571. static void
  572. print_return(char *space)
  573. {
  574.     if (exitnow)
  575.         f_print(fout, "%sexit(0);\n", space);
  576.     else {
  577.         if (timerflag)
  578.             f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
  579.         f_print(fout, "%sreturn;\n", space);
  580.     }
  581. }
  582.  
  583. static void
  584. print_pmapunset(char *space)
  585. {
  586.     list *l;
  587.     definition *def;
  588.     version_list *vp;
  589.  
  590.     for (l = defined; l != NULL; l = l->next) {
  591.         def = (definition *) l->val;
  592.         if (def->def_kind == DEF_PROGRAM) {
  593.             for (vp = def->def.pr.versions; vp != NULL;
  594.                     vp = vp->next) {
  595.                 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
  596.                     space, def->def_name, vp->vers_name);
  597.             }
  598.         }
  599.     }
  600. }
  601.  
  602. static void
  603. print_err_message(char *space)
  604. {
  605.     if (logflag)
  606.         f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
  607.     else if (inetdflag || pmflag)
  608.         f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
  609.     else
  610.         f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
  611. }
  612.  
  613. /*
  614.  * Write the server auxiliary function ( _msgout, timeout)
  615.  */
  616. void
  617. write_svc_aux(int nomain)
  618. {
  619.     if (!logflag)
  620.         write_msg_out();
  621.     if( !nomain )
  622.       write_timeout_func();
  623. }
  624.  
  625. /*
  626.  * Write the _msgout function
  627.  */
  628.  
  629. void
  630. write_msg_out(void)
  631. {
  632.     f_print(fout, "\n");
  633.     f_print(fout, "static\n");
  634.     if( !Cflag ) {
  635.       f_print(fout, "void _msgout(msg)\n");
  636.       f_print(fout, "\tchar *msg;\n");
  637.     } else {
  638.       f_print(fout, "void _msgout(char* msg)\n");
  639.     }
  640.     f_print(fout, "{\n");
  641.     f_print(fout, "#ifdef RPC_SVC_FG\n");
  642.     if (inetdflag || pmflag)
  643.         f_print(fout, "\tif (_rpcpmstart)\n");
  644.     f_print(fout, "\t\tsyslog(LOG_ERR, msg);\n");
  645.     f_print(fout, "\telse\n");
  646.     f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
  647.     f_print(fout, "#else\n");
  648.     f_print(fout, "\tsyslog(LOG_ERR, msg);\n");
  649.     f_print(fout, "#endif\n");
  650.     f_print(fout, "}\n");
  651. }
  652.  
  653. /*
  654.  * Write the timeout function
  655.  */
  656. static void
  657. write_timeout_func(void)
  658. {
  659.     if (!timerflag)
  660.         return;
  661.     f_print(fout, "\n");
  662.     f_print(fout, "static void\n");
  663. #if defined(linux) || defined(amigados)
  664.     if (Cflag)
  665.         f_print(fout, "closedown(int sig)\n");
  666.     else
  667.         f_print(fout, "closedown(sig)\n\tint sig;\n");
  668. #else
  669.     f_print(fout, "closedown()\n");
  670. #endif
  671.     f_print(fout, "{\n");
  672. #ifdef amigados
  673.     f_print(fout, "/* FIXME in rpcgen !!!! */\n");
  674. #else
  675. #ifdef linux /* FIXME: amigados ? */
  676.     f_print(fout, "\t(void) signal(sig, %s closedown);\n",
  677.         Cflag? "(SIG_PF)" : "(void(*)())" );
  678. #endif
  679.     f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
  680.     f_print(fout, "\t\textern fd_set svc_fdset;\n");
  681.     f_print(fout, "\t\tstatic int size;\n");
  682.     f_print(fout, "\t\tint i, openfd;\n");
  683.     if (tirpcflag && pmflag) {
  684.         f_print(fout, "\t\tstruct t_info tinfo;\n\n");
  685.         f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
  686.     } else {
  687.         f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
  688.     }
  689.     f_print(fout, "\t\t\texit(0);\n");
  690.     f_print(fout, "\t\tif (size == 0) {\n");
  691.     if( tirpcflag ) {
  692.       f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
  693.       f_print(fout, "\t\t\trl.rlim_max = 0;\n");
  694.       f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
  695.       f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
  696.       f_print(fout, "\t\t\t\treturn;\n");
  697.     } else {
  698.       f_print(fout, "\t\t\tsize = getdtablesize();\n");
  699.     }
  700.     f_print(fout, "\t\t}\n");
  701.     f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
  702.     f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
  703.     f_print(fout, "\t\t\t\topenfd++;\n");
  704.     f_print(fout, "\t\tif (openfd <= 1)\n");
  705.     f_print(fout, "\t\t\texit(0);\n");
  706.     f_print(fout, "\t}\n");
  707.     f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
  708. #endif
  709.     f_print(fout, "}\n");
  710. }
  711.  
  712. /*
  713.  * Write the most of port monitor support
  714.  */
  715. static void
  716. write_pm_most(char *infile, int netflag)
  717. {
  718.     list *l;
  719.     definition *def;
  720.     version_list *vp;
  721.  
  722.     f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
  723.     f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
  724.     f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
  725.     f_print(fout, "\t\tchar *netid;\n");
  726.     if (!netflag) {    /* Not included by -n option */
  727.         f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
  728.         f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
  729.     }
  730.     if( timerflag )
  731.       f_print(fout, "\t\tint pmclose;\n");
  732. /* not necessary, defined in /usr/include/stdlib */
  733. /*    f_print(fout, "\t\textern char *getenv();\n");*/
  734.     f_print(fout, "\n");
  735.     f_print(fout, "\t\t_rpcpmstart = 1;\n");
  736.     if (logflag)
  737.         open_log_file(infile, "\t\t");
  738.     f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
  739.     sprintf(_errbuf, "cannot get transport name");
  740.     print_err_message("\t\t\t");
  741.     f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
  742.     sprintf(_errbuf, "cannot get transport info");
  743.     print_err_message("\t\t\t");
  744.     f_print(fout, "\t\t}\n");
  745.     /*
  746.      * A kludgy support for inetd services. Inetd only works with
  747.      * sockmod, and RPC works only with timod, hence all this jugglery
  748.      */
  749.     f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
  750.     f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
  751.     sprintf(_errbuf, "could not get the right module");
  752.     print_err_message("\t\t\t\t");
  753.     f_print(fout, "\t\t\t\texit(1);\n");
  754.     f_print(fout, "\t\t\t}\n");
  755.     f_print(fout, "\t\t}\n");
  756.     if( timerflag )
  757.       f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
  758.     f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
  759.             TRANSP);
  760.     sprintf(_errbuf, "cannot create server handle");
  761.     print_err_message("\t\t\t");
  762.     f_print(fout, "\t\t\texit(1);\n");
  763.     f_print(fout, "\t\t}\n");
  764.     f_print(fout, "\t\tif (nconf)\n");
  765.     f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
  766.     for (l = defined; l != NULL; l = l->next) {
  767.         def = (definition *) l->val;
  768.         if (def->def_kind != DEF_PROGRAM) {
  769.             continue;
  770.         }
  771.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  772.             f_print(fout,
  773.                 "\t\tif (!svc_reg(%s, %s, %s, ",
  774.                 TRANSP, def->def_name, vp->vers_name);
  775.             pvname(def->def_name, vp->vers_num);
  776.             f_print(fout, ", 0)) {\n");
  777.             (void) sprintf(_errbuf, "unable to register (%s, %s).",
  778.                     def->def_name, vp->vers_name);
  779.             print_err_message("\t\t\t");
  780.             f_print(fout, "\t\t\texit(1);\n");
  781.             f_print(fout, "\t\t}\n");
  782.         }
  783.     }
  784.     if (timerflag) {
  785.         f_print(fout, "\t\tif (pmclose) {\n");
  786.         f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
  787.                 Cflag? "(SIG_PF)" : "(void(*)())" );
  788.         f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
  789.         f_print(fout, "\t\t}\n");
  790.     }
  791.     f_print(fout, "\t\tsvc_run();\n");
  792.     f_print(fout, "\t\texit(1);\n");
  793.     f_print(fout, "\t\t/* NOTREACHED */\n");
  794.     f_print(fout, "\t}\n");
  795. }
  796.  
  797. /*
  798.  * Support for backgrounding the server if self started.
  799.  */
  800. static void
  801. write_rpc_svc_fg(char *infile, char *sp)
  802. {
  803.     f_print(fout, "#ifndef RPC_SVC_FG\n");
  804.     f_print(fout, "%sint size;\n", sp);
  805.     if( tirpcflag )
  806.             f_print(fout, "%sstruct rlimit rl;\n", sp);
  807.     if (inetdflag)
  808.         f_print(fout, "%sint pid, i;\n\n", sp);
  809.     f_print(fout, "%spid = fork();\n", sp);
  810.     f_print(fout, "%sif (pid < 0) {\n", sp);
  811.     f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
  812.     f_print(fout, "%s\texit(1);\n", sp);
  813.     f_print(fout, "%s}\n", sp);
  814.     f_print(fout, "%sif (pid)\n", sp);
  815.     f_print(fout, "%s\texit(0);\n", sp);
  816.     /* get number of file descriptors */
  817.     if( tirpcflag ) {
  818.       f_print(fout, "%srl.rlim_max = 0;\n", sp);
  819.       f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
  820.       f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
  821.       f_print(fout, "%s\texit(1);\n", sp);
  822.     } else {
  823.       f_print(fout, "%ssize = getdtablesize();\n", sp);
  824.     }
  825.  
  826.     f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
  827.     f_print(fout, "%s\t(void) close(i);\n", sp);
  828.     /* Redirect stderr and stdout to console */
  829.     f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
  830.     f_print(fout, "%s(void) dup2(i, 1);\n", sp);
  831.     f_print(fout, "%s(void) dup2(i, 2);\n", sp);
  832.     /* This removes control of the controlling terminal */
  833.     if( tirpcflag )
  834.       f_print(fout, "%ssetsid();\n", sp);
  835.     else {
  836.       f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
  837.       f_print(fout, "%sif (i >= 0) {\n", sp);
  838.       f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
  839.       f_print(fout, "%s\t(void) close(i);\n", sp);
  840.       f_print(fout, "%s}\n", sp);
  841.     }
  842.     if (!logflag)
  843.         open_log_file(infile, sp);
  844.     f_print(fout, "#endif\n");
  845.     if (logflag)
  846.         open_log_file(infile, sp);
  847. }
  848.  
  849. static void
  850. open_log_file(char *infile, char *sp)
  851. {
  852.     char *s;
  853.  
  854.     s = strrchr(infile, '.');
  855.     if (s) 
  856.         *s = '\0';
  857.     f_print(fout,"%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
  858.     if (s)
  859.         *s = '.';
  860. }
  861.  
  862.  
  863.  
  864.  
  865. /*
  866.  * write a registration for the given transport for Inetd
  867.  */
  868. void
  869. write_inetd_register(char *transp)
  870. {
  871.     list *l;
  872.     definition *def;
  873.     version_list *vp;
  874.     char *sp;
  875.     int isudp;
  876.     char tmpbuf[32];
  877.  
  878.     if (inetdflag)
  879.         sp = "\t";
  880.     else
  881.         sp = "";
  882.     if (streq(transp, "udp"))
  883.         isudp = 1;
  884.     else
  885.         isudp = 0;
  886.     f_print(fout, "\n");
  887.     if (inetdflag) {
  888.         f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
  889.                 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
  890.     }
  891.     f_print(fout, "%s\t%s = svc%s_create(%s",
  892.         sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
  893.     if (!isudp)
  894.         f_print(fout, ", 0, 0");
  895.     f_print(fout, ");\n");
  896.     f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
  897.     (void) sprintf(_errbuf, "cannot create %s service.", transp);
  898.     (void) sprintf(tmpbuf, "%s\t\t", sp);
  899.     print_err_message(tmpbuf);
  900.     f_print(fout, "%s\t\texit(1);\n", sp);
  901.     f_print(fout, "%s\t}\n", sp);
  902.  
  903.     if (inetdflag) {
  904.         f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
  905.         f_print(fout, "%s\tproto = IPPROTO_%s;\n",
  906.                 sp, isudp ? "UDP": "TCP");
  907.     }
  908.     for (l = defined; l != NULL; l = l->next) {
  909.         def = (definition *) l->val;
  910.         if (def->def_kind != DEF_PROGRAM) {
  911.             continue;
  912.         }
  913.         for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
  914.             f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
  915.                 sp, TRANSP, def->def_name, vp->vers_name);
  916.             pvname(def->def_name, vp->vers_num);
  917.             if (inetdflag)
  918.                 f_print(fout, ", proto)) {\n");
  919.             else 
  920.                 f_print(fout, ", IPPROTO_%s)) {\n",
  921.                     isudp ? "UDP": "TCP");
  922.             (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
  923.                     def->def_name, vp->vers_name, transp);
  924.             print_err_message(tmpbuf);
  925.             f_print(fout, "%s\t\texit(1);\n", sp);
  926.             f_print(fout, "%s\t}\n", sp);
  927.         }
  928.     }
  929.     if (inetdflag)
  930.         f_print(fout, "\t}\n");
  931. }
  932.