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