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