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