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