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