Back to index

glibc  2.9
rpc_svcout.c
Go to the documentation of this file.
00001 /*
00002  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00003  * unrestricted use provided that this legend is included on all tape
00004  * media and as a part of the software program in whole or part.  Users
00005  * may copy or modify Sun RPC without charge, but are not authorized
00006  * to license or distribute it to anyone else except as part of a product or
00007  * program developed by the user or with the express written consent of
00008  * Sun Microsystems, Inc.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 
00031 /*
00032  * From: @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
00033  */
00034 
00035 /*
00036  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
00037  */
00038 #include <stdio.h>
00039 #include <string.h>
00040 #include "rpc_parse.h"
00041 #include "rpc_util.h"
00042 #include "proto.h"
00043 
00044 static const char RQSTP[] = "rqstp";
00045 static const char TRANSP[] = "transp";
00046 static const char ARG[] = "argument";
00047 static const char RESULT[] = "result";
00048 static const char ROUTINE[] = "local";
00049 static char RETVAL[] = "retval";
00050 
00051 char _errbuf[256];          /* For all messages */
00052 
00053 static void internal_proctype (const proc_list * plist);
00054 static void p_xdrfunc (const char *rname, const char *typename);
00055 static void write_real_program (const definition * def);
00056 static void write_program (const definition * def, const char *storage);
00057 static void printerr (const char *err, const char *transp);
00058 static void printif (const char *proc, const char *transp, const char *arg);
00059 static void write_inetmost (const char *infile);
00060 static void print_return (const char *space);
00061 static void print_pmapunset (const char *space);
00062 static void print_err_message (const char *space);
00063 static void write_timeout_func (void);
00064 static void write_pm_most (const char *infile, int netflag);
00065 static void write_rpc_svc_fg (const char *infile, const char *sp);
00066 static void open_log_file (const char *infile, const char *sp);
00067 
00068 static void
00069 p_xdrfunc (const char *rname, const char *typename)
00070 {
00071   if (Cflag)
00072     f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
00073             stringfix (typename));
00074   else
00075     f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
00076 }
00077 
00078 void
00079 internal_proctype (const proc_list * plist)
00080 {
00081   f_print (fout, "static ");
00082   ptype (plist->res_prefix, plist->res_type, 1);
00083   f_print (fout, "*");
00084 }
00085 
00086 
00087 /*
00088  * write most of the service, that is, everything but the registrations.
00089  */
00090 void
00091 write_most (const char *infile /* our name */ , int netflag, int nomain)
00092 {
00093   if (inetdflag || pmflag)
00094     {
00095       const char *var_type;
00096 #ifdef __GNU_LIBRARY__
00097       /* WHY? */
00098       var_type = (nomain ? "extern" : "");
00099 #else
00100       var_type = (nomain ? "extern" : "static");
00101 #endif
00102       f_print (fout, "%s int _rpcpmstart;", var_type);
00103       f_print (fout, "\t\t/* Started by a port monitor ? */\n");
00104       if (!tirpcflag)
00105        {
00106          f_print (fout, "%s int _rpcfdtype;", var_type);
00107          f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
00108        }
00109       if (timerflag)
00110        {
00111 #if 0
00112          f_print (fout, "%s int _rpcsvcdirty;", var_type);
00113          f_print (fout, "\t/* Still serving ? */\n");
00114 #else
00115          f_print(fout, " /* States a server can be in wrt request */\n\n");
00116          f_print(fout, "#define\t_IDLE 0\n");
00117          f_print(fout, "#define\t_SERVED 1\n");
00118          f_print(fout, "#define\t_SERVING 2\n\n");
00119          f_print(fout, "static int _rpcsvcstate = _IDLE;");
00120          f_print(fout, "\t /* Set when a request is serviced */\n");
00121 
00122          if (mtflag)
00123            {
00124              f_print (fout, "mutex_t _svcstate_lock;");
00125              f_print (fout,
00126                      "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
00127            }
00128 #endif
00129        }
00130       write_svc_aux (nomain);
00131     }
00132   /* write out dispatcher and stubs */
00133   write_programs (nomain ? NULL : "static");
00134 
00135   if (nomain)
00136     return;
00137 
00138 #ifdef __GNU_LIBRARY__
00139   if (Cflag)
00140     f_print (fout, "\nint\nmain (int argc, char **argv)\n");
00141   else
00142     {
00143       f_print (fout, "\nint\nmain (argc, argv)\n");
00144       f_print (fout, "\tint argc;\n");
00145       f_print (fout, "\tchar **argv;\n");
00146     }
00147 #else
00148   f_print (fout, "\nmain()\n");
00149 #endif
00150   f_print (fout, "{\n");
00151   if (inetdflag)
00152     {
00153       write_inetmost (infile);     /* Includes call to write_rpc_svc_fg() */
00154     }
00155   else
00156     {
00157       if (tirpcflag)
00158        {
00159          if (netflag)
00160            {
00161              f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
00162              f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
00163            }
00164          f_print (fout, "\tpid_t pid;\n");
00165          f_print (fout, "\tint i;\n");
00166          f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
00167 
00168          if (mtflag & timerflag)
00169            f_print (fout,
00170                    "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
00171 
00172          write_pm_most (infile, netflag);
00173          f_print (fout, "\telse {\n");
00174          write_rpc_svc_fg (infile, "\t\t");
00175          f_print (fout, "\t}\n");
00176        }
00177       else
00178        {
00179          f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
00180          f_print (fout, "\n");
00181          print_pmapunset ("\t");
00182        }
00183     }
00184 
00185   if (logflag && !inetdflag)
00186     {
00187       open_log_file (infile, "\t");
00188     }
00189 }
00190 
00191 /*
00192  * write a registration for the given transport
00193  */
00194 void
00195 write_netid_register (const char *transp)
00196 {
00197   list *l;
00198   definition *def;
00199   version_list *vp;
00200   const char *sp;
00201   char tmpbuf[32];
00202 
00203   sp = "";
00204   f_print (fout, "\n");
00205   f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
00206   f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
00207   (void) sprintf (_errbuf, "cannot find %s netid.", transp);
00208   sprintf (tmpbuf, "%s\t\t", sp);
00209   print_err_message (tmpbuf);
00210   f_print (fout, "%s\t\texit (1);\n", sp);
00211   f_print (fout, "%s\t}\n", sp);
00212   f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
00213           sp, TRANSP /*, transp *//* ?!?... */ );
00214   f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
00215   sprintf (_errbuf, "cannot create %s service.", transp);
00216   print_err_message (tmpbuf);
00217   f_print (fout, "%s\t\texit (1);\n", sp);
00218   f_print (fout, "%s\t}\n", sp);
00219 
00220   for (l = defined; l != NULL; l = l->next)
00221     {
00222       def = (definition *) l->val;
00223       if (def->def_kind != DEF_PROGRAM)
00224        {
00225          continue;
00226        }
00227       for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00228        {
00229          f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
00230                  sp, def->def_name, vp->vers_name);
00231          f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
00232                  sp, TRANSP, def->def_name, vp->vers_name);
00233          pvname (def->def_name, vp->vers_num);
00234          f_print (fout, ", nconf)) {\n");
00235          (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
00236                        def->def_name, vp->vers_name, transp);
00237          print_err_message (tmpbuf);
00238          f_print (fout, "%s\t\texit (1);\n", sp);
00239          f_print (fout, "%s\t}\n", sp);
00240        }
00241     }
00242   f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
00243 }
00244 
00245 /*
00246  * write a registration for the given transport for TLI
00247  */
00248 void
00249 write_nettype_register (const char *transp)
00250 {
00251   list *l;
00252   definition *def;
00253   version_list *vp;
00254 
00255   for (l = defined; l != NULL; l = l->next)
00256     {
00257       def = (definition *) l->val;
00258       if (def->def_kind != DEF_PROGRAM)
00259        {
00260          continue;
00261        }
00262       for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00263        {
00264          f_print (fout, "\tif (!svc_create (");
00265          pvname (def->def_name, vp->vers_num);
00266          f_print (fout, ", %s, %s, \"%s\")) {\n ",
00267                  def->def_name, vp->vers_name, transp);
00268          (void) sprintf (_errbuf,
00269                        "unable to create (%s, %s) for %s.",
00270                        def->def_name, vp->vers_name, transp);
00271          print_err_message ("\t\t");
00272          f_print (fout, "\t\texit (1);\n");
00273          f_print (fout, "\t}\n");
00274        }
00275     }
00276 }
00277 
00278 /*
00279  * write the rest of the service
00280  */
00281 void
00282 write_rest (void)
00283 {
00284   f_print (fout, "\n");
00285   if (inetdflag)
00286     {
00287       f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
00288       (void) sprintf (_errbuf, "could not create a handle");
00289       print_err_message ("\t\t");
00290       f_print (fout, "\t\texit (1);\n");
00291       f_print (fout, "\t}\n");
00292       if (timerflag)
00293        {
00294          f_print (fout, "\tif (_rpcpmstart) {\n");
00295          f_print (fout,
00296                  "\t\t(void) signal (SIGALRM, %s closedown);\n",
00297                  Cflag ? "(SIG_PF)" : "(void(*)())");
00298          f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
00299          f_print (fout, "\t}\n");
00300        }
00301     }
00302   f_print (fout, "\tsvc_run ();\n");
00303   (void) sprintf (_errbuf, "svc_run returned");
00304   print_err_message ("\t");
00305   f_print (fout, "\texit (1);\n");
00306   f_print (fout, "\t/* NOTREACHED */\n");
00307   f_print (fout, "}\n");
00308 }
00309 
00310 void
00311 write_programs (const char *storage)
00312 {
00313   list *l;
00314   definition *def;
00315 
00316   /* write out stubs for procedure  definitions */
00317   for (l = defined; l != NULL; l = l->next)
00318     {
00319       def = (definition *) l->val;
00320       if (def->def_kind == DEF_PROGRAM)
00321        {
00322          write_real_program (def);
00323        }
00324     }
00325 
00326   /* write out dispatcher for each program */
00327   for (l = defined; l != NULL; l = l->next)
00328     {
00329       def = (definition *) l->val;
00330       if (def->def_kind == DEF_PROGRAM)
00331        {
00332          write_program (def, storage);
00333        }
00334     }
00335 }
00336 
00337 /* write out definition of internal function (e.g. _printmsg_1(...))
00338    which calls server's defintion of actual function (e.g. printmsg_1(...)).
00339    Unpacks single user argument of printmsg_1 to call-by-value format
00340    expected by printmsg_1. */
00341 static void
00342 write_real_program (const definition * def)
00343 {
00344   version_list *vp;
00345   proc_list *proc;
00346   decl_list *l;
00347 
00348   if (!newstyle)
00349     return;                 /* not needed for old style */
00350   for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00351     {
00352       for (proc = vp->procs; proc != NULL; proc = proc->next)
00353        {
00354          fprintf (fout, "\n");
00355          if (!mtflag)
00356            internal_proctype (proc);
00357          else
00358            f_print (fout, "int");
00359          f_print (fout, "\n_");
00360          pvname (proc->proc_name, vp->vers_num);
00361          if (Cflag)
00362            {
00363              f_print (fout, " (");
00364              /* arg name */
00365              if (proc->arg_num > 1)
00366               f_print (fout, proc->args.argname);
00367              else
00368               ptype (proc->args.decls->decl.prefix,
00369                      proc->args.decls->decl.type, 0);
00370              if (mtflag)
00371               {
00372                 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
00373                        RESULT, RQSTP);
00374               }
00375              else
00376               f_print (fout, " *argp, struct svc_req *%s)\n",
00377                       RQSTP);
00378            }
00379          else
00380            {
00381              if (mtflag)
00382               f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
00383              else
00384               f_print (fout, " (argp, %s)\n", RQSTP);
00385              /* arg name */
00386              if (proc->arg_num > 1)
00387               f_print (fout, "\t%s *argp;\n", proc->args.argname);
00388              else
00389               {
00390                 f_print (fout, "\t");
00391                 ptype (proc->args.decls->decl.prefix,
00392                       proc->args.decls->decl.type, 0);
00393                 f_print (fout, " *argp;\n");
00394               }
00395              f_print (fout, "      struct svc_req *%s;\n", RQSTP);
00396            }
00397 
00398          f_print (fout, "{\n");
00399          f_print (fout, "\treturn (");
00400          if (Cflag || mtflag)
00401            pvname_svc (proc->proc_name, vp->vers_num);
00402          else
00403            pvname (proc->proc_name, vp->vers_num);
00404          f_print (fout, "(");
00405          if (proc->arg_num < 2)
00406            {                /* single argument */
00407              if (!streq (proc->args.decls->decl.type, "void"))
00408               f_print (fout, "*argp, ");  /* non-void */
00409            }
00410          else
00411            {
00412              for (l = proc->args.decls; l != NULL; l = l->next)
00413               f_print (fout, "argp->%s, ", l->decl.name);
00414            }
00415          if (mtflag)
00416            f_print (fout, "%s, ", RESULT);
00417          f_print (fout, "%s));\n}\n", RQSTP);
00418        }
00419     }
00420 }
00421 
00422 static void
00423 write_program (const definition * def, const char *storage)
00424 {
00425   version_list *vp;
00426   proc_list *proc;
00427   int filled;
00428 
00429   for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00430     {
00431       f_print (fout, "\n");
00432       if (storage != NULL)
00433        {
00434          f_print (fout, "%s ", storage);
00435        }
00436       f_print (fout, "void\n");
00437       pvname (def->def_name, vp->vers_num);
00438 
00439       if (Cflag)
00440        {
00441          f_print (fout, "(struct svc_req *%s, ", RQSTP);
00442          f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
00443        }
00444       else
00445        {
00446          f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
00447          f_print (fout, "   struct svc_req *%s;\n", RQSTP);
00448          f_print (fout, "   register SVCXPRT *%s;\n", TRANSP);
00449        }
00450 
00451       f_print (fout, "{\n");
00452 
00453       filled = 0;
00454       f_print (fout, "\tunion {\n");
00455       for (proc = vp->procs; proc != NULL; proc = proc->next)
00456        {
00457          if (proc->arg_num < 2)
00458            {                /* single argument */
00459              if (streq (proc->args.decls->decl.type,
00460                       "void"))
00461               {
00462                 continue;
00463               }
00464              filled = 1;
00465              f_print (fout, "\t\t");
00466              ptype (proc->args.decls->decl.prefix,
00467                    proc->args.decls->decl.type, 0);
00468              pvname (proc->proc_name, vp->vers_num);
00469              f_print (fout, "_arg;\n");
00470 
00471            }
00472          else
00473            {
00474              filled = 1;
00475              f_print (fout, "\t\t%s", proc->args.argname);
00476              f_print (fout, " ");
00477              pvname (proc->proc_name, vp->vers_num);
00478              f_print (fout, "_arg;\n");
00479            }
00480        }
00481       if (!filled)
00482        {
00483          f_print (fout, "\t\tint fill;\n");
00484        }
00485       f_print (fout, "\t} %s;\n", ARG);
00486       if (mtflag)
00487        {
00488          f_print(fout, "\tunion {\n");
00489          for (proc = vp->procs; proc != NULL; proc = proc->next)
00490            if (!streq (proc->res_type, "void"))
00491              {
00492               f_print(fout, "\t\t");
00493               ptype(proc->res_prefix, proc->res_type, 0);
00494               pvname(proc->proc_name, vp->vers_num);
00495               f_print(fout, "_res;\n");
00496              }
00497          f_print(fout, "\t} %s;\n", RESULT);
00498          f_print(fout, "\tbool_t %s;\n", RETVAL);
00499 
00500        } else
00501          f_print (fout, "\tchar *%s;\n", RESULT);
00502 
00503       if (Cflag)
00504        {
00505          f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
00506          if (mtflag)
00507            f_print(fout,
00508                   "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
00509                   ROUTINE);
00510          else
00511            f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
00512                    ROUTINE);
00513        }
00514       else
00515        {
00516          f_print (fout, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG, RESULT);
00517          if (mtflag)
00518            f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
00519          else
00520            f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
00521        }
00522       f_print (fout, "\n");
00523 
00524       if (timerflag)
00525 #if 0
00526        f_print (fout, "\t_rpcsvcdirty = 1;\n");
00527 #else
00528       {
00529        if (mtflag)
00530          f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
00531        f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
00532        if (mtflag)
00533          f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
00534       }
00535 #endif
00536 
00537       f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
00538       if (!nullproc (vp->procs))
00539        {
00540          f_print (fout, "\tcase NULLPROC:\n");
00541          f_print (fout,
00542                  "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
00543                  TRANSP);
00544          print_return ("\t\t");
00545          f_print (fout, "\n");
00546        }
00547       for (proc = vp->procs; proc != NULL; proc = proc->next)
00548        {
00549          f_print (fout, "\tcase %s:\n", proc->proc_name);
00550          if (proc->arg_num < 2)
00551            {                /* single argument */
00552              p_xdrfunc (ARG, proc->args.decls->decl.type);
00553            }
00554          else
00555            {
00556              p_xdrfunc (ARG, proc->args.argname);
00557            }
00558          p_xdrfunc (RESULT, proc->res_type);
00559          if (Cflag)
00560            {
00561              if (mtflag)
00562               f_print(fout,
00563                      "\t\t%s = (bool_t (*) (char *, void *,  struct svc_req *))",
00564                      ROUTINE);
00565              else
00566               f_print (fout,
00567                       "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
00568                       ROUTINE);
00569            }
00570          else
00571            if (mtflag)
00572              f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
00573            else
00574              f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
00575 
00576          if (newstyle)
00577            {                /* new style: calls internal routine */
00578              f_print (fout, "_");
00579            }
00580          if ((Cflag || mtflag) && !newstyle)
00581            pvname_svc (proc->proc_name, vp->vers_num);
00582          else
00583            pvname (proc->proc_name, vp->vers_num);
00584          f_print (fout, ";\n");
00585          f_print (fout, "\t\tbreak;\n\n");
00586        }
00587       f_print (fout, "\tdefault:\n");
00588       printerr ("noproc", TRANSP);
00589       print_return ("\t\t");
00590       f_print (fout, "\t}\n");
00591 
00592       f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
00593       printif ("getargs", TRANSP, ARG);
00594       printerr ("decode", TRANSP);
00595       print_return ("\t\t");
00596       f_print (fout, "\t}\n");
00597 
00598       if (!mtflag)
00599        {
00600          if (Cflag)
00601            f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
00602                    RESULT, ROUTINE, ARG, RQSTP);
00603          else
00604            f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
00605                    RESULT, ROUTINE, ARG, RQSTP);
00606        }
00607       else
00608        if (Cflag)
00609          f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
00610                 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
00611        else
00612          f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
00613                 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
00614       if (mtflag)
00615        f_print(fout,
00616               "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
00617               RETVAL, TRANSP, RESULT, RESULT);
00618       else
00619        f_print(fout,
00620               "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
00621               RESULT, TRANSP, RESULT, RESULT);
00622 
00623       printerr ("systemerr", TRANSP);
00624       f_print (fout, "\t}\n");
00625 
00626       printif ("freeargs", TRANSP, ARG);
00627 
00628       sprintf (_errbuf, "unable to free arguments");
00629       print_err_message ("\t\t");
00630       f_print (fout, "\t\texit (1);\n");
00631       f_print (fout, "\t}\n");
00632       /* print out free routine */
00633       if (mtflag)
00634        {
00635          f_print(fout,"\tif (!");
00636          pvname(def->def_name, vp->vers_num);
00637          f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
00638                 TRANSP, RESULT, RESULT);
00639          (void) sprintf(_errbuf, "unable to free results");
00640          print_err_message("\t\t");
00641          f_print(fout, "\n");
00642        }
00643       print_return ("\t");
00644       f_print (fout, "}\n");
00645     }
00646 }
00647 
00648 static void
00649 printerr (const char *err, const char *transp)
00650 {
00651   f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
00652 }
00653 
00654 static void
00655 printif (const char *proc, const char *transp, const char *arg)
00656 {
00657   f_print (fout, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
00658           proc, transp, arg, arg);
00659 }
00660 
00661 int
00662 nullproc (const proc_list * proc)
00663 {
00664   for (; proc != NULL; proc = proc->next)
00665     {
00666       if (streq (proc->proc_num, "0"))
00667        {
00668          return 1;
00669        }
00670     }
00671   return 0;
00672 }
00673 
00674 static void
00675 write_inetmost (const char *infile)
00676 {
00677   f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
00678   f_print (fout, "\tint sock;\n");
00679   f_print (fout, "\tint proto;\n");
00680   f_print (fout, "\tstruct sockaddr_in saddr;\n");
00681   f_print (fout, "\tint asize = sizeof (saddr);\n");
00682   f_print (fout, "\n");
00683   f_print (fout,
00684        "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
00685   f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
00686   f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
00687   f_print (fout, "\t\t\texit (1);\n");
00688   f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
00689   f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
00690   f_print (fout, "\t\t\texit (1);\n");
00691   f_print (fout, "\t\tsock = 0;\n");
00692   f_print (fout, "\t\t_rpcpmstart = 1;\n");
00693   f_print (fout, "\t\tproto = 0;\n");
00694   open_log_file (infile, "\t\t");
00695   f_print (fout, "\t} else {\n");
00696   write_rpc_svc_fg (infile, "\t\t");
00697   f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
00698   print_pmapunset ("\t\t");
00699   f_print (fout, "\t}\n");
00700 }
00701 
00702 static void
00703 print_return (const char *space)
00704 {
00705   if (exitnow)
00706     f_print (fout, "%sexit (0);\n", space);
00707   else
00708     {
00709       if (timerflag)
00710        {
00711 #if 0
00712          f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
00713 #else
00714          if (mtflag)
00715            f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
00716          f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
00717          if (mtflag)
00718            f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
00719 #endif
00720        }
00721       f_print (fout, "%sreturn;\n", space);
00722     }
00723 }
00724 
00725 static void
00726 print_pmapunset (const char *space)
00727 {
00728   list *l;
00729   definition *def;
00730   version_list *vp;
00731 
00732   for (l = defined; l != NULL; l = l->next)
00733     {
00734       def = (definition *) l->val;
00735       if (def->def_kind == DEF_PROGRAM)
00736        {
00737          for (vp = def->def.pr.versions; vp != NULL;
00738               vp = vp->next)
00739            {
00740              f_print (fout, "%spmap_unset (%s, %s);\n",
00741                      space, def->def_name, vp->vers_name);
00742            }
00743        }
00744     }
00745 }
00746 
00747 static void
00748 print_err_message (const char *space)
00749 {
00750   if (logflag)
00751     f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
00752   else if (inetdflag || pmflag)
00753     f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
00754   else
00755     f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
00756 }
00757 
00758 /*
00759  * Write the server auxiliary function ( _msgout, timeout)
00760  */
00761 void
00762 write_svc_aux (int nomain)
00763 {
00764   if (!logflag)
00765     write_msg_out ();
00766   if (!nomain)
00767     write_timeout_func ();
00768 }
00769 
00770 /*
00771  * Write the _msgout function
00772  */
00773 
00774 void
00775 write_msg_out (void)
00776 {
00777   f_print (fout, "\n");
00778   f_print (fout, "static\n");
00779   if (!Cflag)
00780     {
00781       f_print (fout, "void _msgout (msg)\n");
00782       f_print (fout, "\tchar *msg;\n");
00783     }
00784   else
00785     {
00786       f_print (fout, "void _msgout (char* msg)\n");
00787     }
00788   f_print (fout, "{\n");
00789   f_print (fout, "#ifdef RPC_SVC_FG\n");
00790   if (inetdflag || pmflag)
00791     f_print (fout, "\tif (_rpcpmstart)\n");
00792   f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
00793   f_print (fout, "\telse\n");
00794   f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
00795   f_print (fout, "#else\n");
00796   f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
00797   f_print (fout, "#endif\n");
00798   f_print (fout, "}\n");
00799 }
00800 
00801 /*
00802  * Write the timeout function
00803  */
00804 static void
00805 write_timeout_func (void)
00806 {
00807   if (!timerflag)
00808     return;
00809   f_print (fout, "\n");
00810   f_print (fout, "static void\n");
00811   if (Cflag)
00812     f_print (fout, "closedown (int sig)\n");
00813   else
00814     f_print (fout, "closedown (sig)\n\tint sig;\n");
00815   f_print (fout, "{\n");
00816 
00817 #if defined (__GNU_LIBRARY__) && 0
00818   f_print (fout, "\t(void) signal (sig, %s closedown);\n",
00819           Cflag ? "(SIG_PF)" : "(void(*)())");
00820 #endif
00821   if (mtflag)
00822     f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
00823 #if 0
00824   f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
00825 #else
00826   f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
00827 #endif
00828   f_print (fout, "\t\textern fd_set svc_fdset;\n");
00829   f_print (fout, "\t\tstatic int size;\n");
00830   f_print (fout, "\t\tint i, openfd;\n");
00831   if (tirpcflag && pmflag)
00832     {
00833       f_print (fout, "\t\tstruct t_info tinfo;\n\n");
00834       f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
00835     }
00836   else
00837     {
00838       f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
00839     }
00840   f_print (fout, "\t\t\texit (0);\n");
00841   f_print (fout, "\t\tif (size == 0) {\n");
00842   if (tirpcflag)
00843     {
00844       f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
00845       f_print (fout, "\t\t\trl.rlim_max = 0;\n");
00846       f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
00847       f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
00848       if (mtflag)
00849        f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
00850        f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
00851     }
00852   else
00853     {
00854       f_print (fout, "\t\t\tsize = getdtablesize();\n");
00855     }
00856   f_print (fout, "\t\t}\n");
00857   f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
00858   f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
00859   f_print (fout, "\t\t\t\topenfd++;\n");
00860   f_print (fout, "\t\tif (openfd <= 1)\n");
00861   f_print (fout, "\t\t\texit (0);\n");
00862   f_print (fout, "\t}\n");
00863   f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
00864   f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
00865   if (mtflag)
00866     f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
00867   f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
00868          Cflag? "(SIG_PF)" : "(void(*)())");
00869   f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
00870   f_print (fout, "}\n");
00871 }
00872 
00873 /*
00874  * Write the most of port monitor support
00875  */
00876 static void
00877 write_pm_most (const char *infile, int netflag)
00878 {
00879   list *l;
00880   definition *def;
00881   version_list *vp;
00882 
00883   f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
00884   f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
00885   f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
00886   f_print (fout, "\t\tchar *netid;\n");
00887   if (!netflag)
00888     {                       /* Not included by -n option */
00889       f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
00890       f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
00891     }
00892   if (timerflag)
00893     f_print (fout, "\t\tint pmclose;\n");
00894 /* not necessary, defined in /usr/include/stdlib */
00895 /*      f_print(fout, "\t\textern char *getenv();\n"); */
00896   f_print (fout, "\n");
00897   f_print (fout, "\t\t_rpcpmstart = 1;\n");
00898   if (logflag)
00899     open_log_file (infile, "\t\t");
00900   f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
00901   sprintf (_errbuf, "cannot get transport name");
00902   print_err_message ("\t\t\t");
00903   f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
00904   sprintf (_errbuf, "cannot get transport info");
00905   print_err_message ("\t\t\t");
00906   f_print (fout, "\t\t}\n");
00907   /*
00908    * A kludgy support for inetd services. Inetd only works with
00909    * sockmod, and RPC works only with timod, hence all this jugglery
00910    */
00911   f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
00912   f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
00913   sprintf (_errbuf, "could not get the right module");
00914   print_err_message ("\t\t\t\t");
00915   f_print (fout, "\t\t\t\texit(1);\n");
00916   f_print (fout, "\t\t\t}\n");
00917   f_print (fout, "\t\t}\n");
00918   if (timerflag)
00919     f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
00920   f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
00921           TRANSP);
00922   sprintf (_errbuf, "cannot create server handle");
00923   print_err_message ("\t\t\t");
00924   f_print (fout, "\t\t\texit(1);\n");
00925   f_print (fout, "\t\t}\n");
00926   f_print (fout, "\t\tif (nconf)\n");
00927   f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
00928   for (l = defined; l != NULL; l = l->next)
00929     {
00930       def = (definition *) l->val;
00931       if (def->def_kind != DEF_PROGRAM)
00932        {
00933          continue;
00934        }
00935       for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
00936        {
00937          f_print (fout,
00938                  "\t\tif (!svc_reg(%s, %s, %s, ",
00939                  TRANSP, def->def_name, vp->vers_name);
00940          pvname (def->def_name, vp->vers_num);
00941          f_print (fout, ", 0)) {\n");
00942          (void) sprintf (_errbuf, "unable to register (%s, %s).",
00943                        def->def_name, vp->vers_name);
00944          print_err_message ("\t\t\t");
00945          f_print (fout, "\t\t\texit(1);\n");
00946          f_print (fout, "\t\t}\n");
00947        }
00948     }
00949   if (timerflag)
00950     {
00951       f_print (fout, "\t\tif (pmclose) {\n");
00952       f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
00953               Cflag ? "(SIG_PF)" : "(void(*)())");
00954       f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
00955       f_print (fout, "\t\t}\n");
00956     }
00957   f_print (fout, "\t\tsvc_run();\n");
00958   f_print (fout, "\t\texit(1);\n");
00959   f_print (fout, "\t\t/* NOTREACHED */\n");
00960   f_print (fout, "\t}\n");
00961 }
00962 
00963 /*
00964  * Support for backgrounding the server if self started.
00965  */
00966 static void
00967 write_rpc_svc_fg (const char *infile, const char *sp)
00968 {
00969   f_print (fout, "#ifndef RPC_SVC_FG\n");
00970   f_print (fout, "%sint size;\n", sp);
00971   if (tirpcflag)
00972     f_print (fout, "%sstruct rlimit rl;\n", sp);
00973   if (inetdflag)
00974     f_print (fout, "%sint pid, i;\n\n", sp);
00975   f_print (fout, "%spid = fork();\n", sp);
00976   f_print (fout, "%sif (pid < 0) {\n", sp);
00977   f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
00978   f_print (fout, "%s\texit(1);\n", sp);
00979   f_print (fout, "%s}\n", sp);
00980   f_print (fout, "%sif (pid)\n", sp);
00981   f_print (fout, "%s\texit(0);\n", sp);
00982   /* get number of file descriptors */
00983   if (tirpcflag)
00984     {
00985       f_print (fout, "%srl.rlim_max = 0;\n", sp);
00986       f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
00987       f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
00988       f_print (fout, "%s\texit(1);\n", sp);
00989     }
00990   else
00991     {
00992       f_print (fout, "%ssize = getdtablesize();\n", sp);
00993     }
00994 
00995   f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
00996   f_print (fout, "%s\t(void) close(i);\n", sp);
00997   /* Redirect stderr and stdout to console */
00998   f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
00999   f_print (fout, "%s(void) dup2(i, 1);\n", sp);
01000   f_print (fout, "%s(void) dup2(i, 2);\n", sp);
01001   /* This removes control of the controlling terminal */
01002   if (tirpcflag)
01003     f_print (fout, "%ssetsid();\n", sp);
01004   else
01005     {
01006       f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
01007       f_print (fout, "%sif (i >= 0) {\n", sp);
01008       f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
01009       f_print (fout, "%s\t(void) close(i);\n", sp);
01010       f_print (fout, "%s}\n", sp);
01011     }
01012   if (!logflag)
01013     open_log_file (infile, sp);
01014   f_print (fout, "#endif\n");
01015   if (logflag)
01016     open_log_file (infile, sp);
01017 }
01018 
01019 static void
01020 open_log_file (const char *infile, const char *sp)
01021 {
01022   char *s;
01023 
01024   s = strrchr (infile, '.');
01025   if (s)
01026     *s = '\0';
01027   f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
01028   if (s)
01029     *s = '.';
01030 }
01031 
01032 /*
01033  * write a registration for the given transport for Inetd
01034  */
01035 void
01036 write_inetd_register (const char *transp)
01037 {
01038   list *l;
01039   definition *def;
01040   version_list *vp;
01041   const char *sp;
01042   int isudp;
01043   char tmpbuf[32];
01044 
01045   if (inetdflag)
01046     sp = "\t";
01047   else
01048     sp = "";
01049   if (streq (transp, "udp") || streq (transp, "udp6"))
01050     isudp = 1;
01051   else
01052     isudp = 0;
01053   f_print (fout, "\n");
01054   if (inetdflag)
01055     {
01056       f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
01057               isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
01058     }
01059   f_print (fout, "%s\t%s = svc%s_create(%s",
01060           sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
01061   if (!isudp)
01062     f_print (fout, ", 0, 0");
01063   f_print (fout, ");\n");
01064   f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
01065   (void) sprintf (_errbuf, "cannot create %s service.", transp);
01066   (void) sprintf (tmpbuf, "%s\t\t", sp);
01067   print_err_message (tmpbuf);
01068   f_print (fout, "%s\t\texit(1);\n", sp);
01069   f_print (fout, "%s\t}\n", sp);
01070 
01071   if (inetdflag)
01072     {
01073       f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
01074       f_print (fout, "%s\tproto = IPPROTO_%s;\n",
01075               sp, isudp ? "UDP" : "TCP");
01076     }
01077   for (l = defined; l != NULL; l = l->next)
01078     {
01079       def = (definition *) l->val;
01080       if (def->def_kind != DEF_PROGRAM)
01081        {
01082          continue;
01083        }
01084       for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
01085        {
01086          f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
01087                  sp, TRANSP, def->def_name, vp->vers_name);
01088          pvname (def->def_name, vp->vers_num);
01089          if (inetdflag)
01090            f_print (fout, ", proto)) {\n");
01091          else
01092            f_print (fout, ", IPPROTO_%s)) {\n",
01093                    isudp ? "UDP" : "TCP");
01094          (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
01095                        def->def_name, vp->vers_name, transp);
01096          print_err_message (tmpbuf);
01097          f_print (fout, "%s\t\texit(1);\n", sp);
01098          f_print (fout, "%s\t}\n", sp);
01099        }
01100     }
01101   if (inetdflag)
01102     f_print (fout, "\t}\n");
01103 }