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