Correctly handle m68k long double format.
[glibc/pb-stable.git] / sunrpc / rpc_svcout.c
blob4cd7c6cad0be546aec7717fadfbce988d01229a2
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user or with the express written consent of
8 * Sun Microsystems, Inc.
10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 * Sun RPC is provided with no support and without any obligation on the
15 * part of Sun Microsystems, Inc. to assist in its use, correction,
16 * modification or enhancement.
18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20 * OR ANY PART THEREOF.
22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23 * or profits or other special, indirect and consequential damages, even if
24 * Sun has been advised of the possibility of such damages.
26 * Sun Microsystems, Inc.
27 * 2550 Garcia Avenue
28 * Mountain View, California 94043
32 * From: @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
34 char svcout_rcsid[] =
35 "$Id$";
38 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
40 #include <stdio.h>
41 #include <string.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
44 #include "proto.h"
46 static const char RQSTP[] = "rqstp";
47 static const char TRANSP[] = "transp";
48 static const char ARG[] = "argument";
49 static const char RESULT[] = "result";
50 static const char ROUTINE[] = "local";
51 static char RETVAL[] = "retval";
53 char _errbuf[256]; /* For all messages */
55 static void internal_proctype (const proc_list * plist);
56 static void p_xdrfunc (const char *rname, const char *typename);
57 static void write_real_program (const definition * def);
58 static void write_program (const definition * def, const char *storage);
59 static void printerr (const char *err, const char *transp);
60 static void printif (const char *proc, const char *transp,
61 const char *prefix, const char *arg);
62 static void write_inetmost (const char *infile);
63 static void print_return (const char *space);
64 static void print_pmapunset (const char *space);
65 static void print_err_message (const char *space);
66 static void write_timeout_func (void);
67 static void write_pm_most (const char *infile, int netflag);
68 static void write_rpc_svc_fg (const char *infile, const char *sp);
69 static void open_log_file (const char *infile, const char *sp);
71 static void
72 p_xdrfunc (const char *rname, const char *typename)
74 if (Cflag)
75 f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
76 stringfix (typename));
77 else
78 f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
81 void
82 internal_proctype (const proc_list * plist)
84 f_print (fout, "static ");
85 ptype (plist->res_prefix, plist->res_type, 1);
86 f_print (fout, "*");
91 * write most of the service, that is, everything but the registrations.
93 void
94 write_most (const char *infile /* our name */ , int netflag, int nomain)
96 if (inetdflag || pmflag)
98 const char *var_type;
99 #ifdef __GNU_LIBRARY__
100 /* WHY? */
101 var_type = (nomain ? "extern" : "");
102 #else
103 var_type = (nomain ? "extern" : "static");
104 #endif
105 f_print (fout, "%s int _rpcpmstart;", var_type);
106 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
107 if (!tirpcflag)
109 f_print (fout, "%s int _rpcfdtype;", var_type);
110 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
112 if (timerflag)
114 #if 0
115 f_print (fout, "%s int _rpcsvcdirty;", var_type);
116 f_print (fout, "\t/* Still serving ? */\n");
117 #else
118 f_print(fout, " /* States a server can be in wrt request */\n\n");
119 f_print(fout, "#define\t_IDLE 0\n");
120 f_print(fout, "#define\t_SERVED 1\n");
121 f_print(fout, "#define\t_SERVING 2\n\n");
122 f_print(fout, "static int _rpcsvcstate = _IDLE;");
123 f_print(fout, "\t /* Set when a request is serviced */\n");
125 if (mtflag)
127 f_print (fout, "mutex_t _svcstate_lock;");
128 f_print (fout,
129 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
131 #endif
133 write_svc_aux (nomain);
135 /* write out dispatcher and stubs */
136 write_programs (nomain ? NULL : "static");
138 if (nomain)
139 return;
141 #ifdef __GNU_LIBRARY__
142 if (Cflag)
143 f_print (fout, "\nint\nmain (int argc, char **argv)\n");
144 else
146 f_print (fout, "\nint\nmain (argc, argv)\n");
147 f_print (fout, "\tint argc;\n");
148 f_print (fout, "\tchar **argv;\n");
150 #else
151 f_print (fout, "\nmain()\n");
152 #endif
153 f_print (fout, "{\n");
154 if (inetdflag)
156 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
158 else
160 if (tirpcflag)
162 if (netflag)
164 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
165 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
167 f_print (fout, "\tpid_t pid;\n");
168 f_print (fout, "\tint i;\n");
169 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
171 if (mtflag & timerflag)
172 f_print (fout,
173 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
175 write_pm_most (infile, netflag);
176 f_print (fout, "\telse {\n");
177 write_rpc_svc_fg (infile, "\t\t");
178 f_print (fout, "\t}\n");
180 else
182 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
183 f_print (fout, "\n");
184 print_pmapunset ("\t");
188 if (logflag && !inetdflag)
190 open_log_file (infile, "\t");
195 * write a registration for the given transport
197 void
198 write_netid_register (const char *transp)
200 list *l;
201 definition *def;
202 version_list *vp;
203 const char *sp;
204 char tmpbuf[32];
206 sp = "";
207 f_print (fout, "\n");
208 f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
209 f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
210 (void) sprintf (_errbuf, "cannot find %s netid.", transp);
211 sprintf (tmpbuf, "%s\t\t", sp);
212 print_err_message (tmpbuf);
213 f_print (fout, "%s\t\texit (1);\n", sp);
214 f_print (fout, "%s\t}\n", sp);
215 f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
216 sp, TRANSP /*, transp *//* ?!?... */ );
217 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
218 sprintf (_errbuf, "cannot create %s service.", transp);
219 print_err_message (tmpbuf);
220 f_print (fout, "%s\t\texit (1);\n", sp);
221 f_print (fout, "%s\t}\n", sp);
223 for (l = defined; l != NULL; l = l->next)
225 def = (definition *) l->val;
226 if (def->def_kind != DEF_PROGRAM)
228 continue;
230 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
232 f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
233 sp, def->def_name, vp->vers_name);
234 f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
235 sp, TRANSP, def->def_name, vp->vers_name);
236 pvname (def->def_name, vp->vers_num);
237 f_print (fout, ", nconf)) {\n");
238 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
239 def->def_name, vp->vers_name, transp);
240 print_err_message (tmpbuf);
241 f_print (fout, "%s\t\texit (1);\n", sp);
242 f_print (fout, "%s\t}\n", sp);
245 f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
249 * write a registration for the given transport for TLI
251 void
252 write_nettype_register (const char *transp)
254 list *l;
255 definition *def;
256 version_list *vp;
258 for (l = defined; l != NULL; l = l->next)
260 def = (definition *) l->val;
261 if (def->def_kind != DEF_PROGRAM)
263 continue;
265 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
267 f_print (fout, "\tif (!svc_create (");
268 pvname (def->def_name, vp->vers_num);
269 f_print (fout, ", %s, %s, \"%s\")) {\n ",
270 def->def_name, vp->vers_name, transp);
271 (void) sprintf (_errbuf,
272 "unable to create (%s, %s) for %s.",
273 def->def_name, vp->vers_name, transp);
274 print_err_message ("\t\t");
275 f_print (fout, "\t\texit (1);\n");
276 f_print (fout, "\t}\n");
282 * write the rest of the service
284 void
285 write_rest (void)
287 f_print (fout, "\n");
288 if (inetdflag)
290 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
291 (void) sprintf (_errbuf, "could not create a handle");
292 print_err_message ("\t\t");
293 f_print (fout, "\t\texit (1);\n");
294 f_print (fout, "\t}\n");
295 if (timerflag)
297 f_print (fout, "\tif (_rpcpmstart) {\n");
298 f_print (fout,
299 "\t\t(void) signal (SIGALRM, %s closedown);\n",
300 Cflag ? "(SIG_PF)" : "(void(*)())");
301 f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
302 f_print (fout, "\t}\n");
305 f_print (fout, "\tsvc_run ();\n");
306 (void) sprintf (_errbuf, "svc_run returned");
307 print_err_message ("\t");
308 f_print (fout, "\texit (1);\n");
309 f_print (fout, "\t/* NOTREACHED */\n");
310 f_print (fout, "}\n");
313 void
314 write_programs (const char *storage)
316 list *l;
317 definition *def;
319 /* write out stubs for procedure definitions */
320 for (l = defined; l != NULL; l = l->next)
322 def = (definition *) l->val;
323 if (def->def_kind == DEF_PROGRAM)
325 write_real_program (def);
329 /* write out dispatcher for each program */
330 for (l = defined; l != NULL; l = l->next)
332 def = (definition *) l->val;
333 if (def->def_kind == DEF_PROGRAM)
335 write_program (def, storage);
340 /* write out definition of internal function (e.g. _printmsg_1(...))
341 which calls server's defintion of actual function (e.g. printmsg_1(...)).
342 Unpacks single user argument of printmsg_1 to call-by-value format
343 expected by printmsg_1. */
344 static void
345 write_real_program (const definition * def)
347 version_list *vp;
348 proc_list *proc;
349 decl_list *l;
351 if (!newstyle)
352 return; /* not needed for old style */
353 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
355 for (proc = vp->procs; proc != NULL; proc = proc->next)
357 fprintf (fout, "\n");
358 if (!mtflag)
359 internal_proctype (proc);
360 else
361 f_print (fout, "int");
362 f_print (fout, "\n_");
363 pvname (proc->proc_name, vp->vers_num);
364 if (Cflag)
366 f_print (fout, " (");
367 /* arg name */
368 if (proc->arg_num > 1)
369 f_print (fout, proc->args.argname);
370 else
371 ptype (proc->args.decls->decl.prefix,
372 proc->args.decls->decl.type, 0);
373 if (mtflag)
375 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
376 RESULT, RQSTP);
378 else
379 f_print (fout, " *argp, struct svc_req *%s)\n",
380 RQSTP);
382 else
384 if (mtflag)
385 f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
386 else
387 f_print (fout, " (argp, %s)\n", RQSTP);
388 /* arg name */
389 if (proc->arg_num > 1)
390 f_print (fout, "\t%s *argp;\n", proc->args.argname);
391 else
393 f_print (fout, "\t");
394 ptype (proc->args.decls->decl.prefix,
395 proc->args.decls->decl.type, 0);
396 f_print (fout, " *argp;\n");
398 f_print (fout, " struct svc_req *%s;\n", RQSTP);
401 f_print (fout, "{\n");
402 f_print (fout, "\treturn (");
403 if (Cflag || mtflag)
404 pvname_svc (proc->proc_name, vp->vers_num);
405 else
406 pvname (proc->proc_name, vp->vers_num);
407 f_print (fout, "(");
408 if (proc->arg_num < 2)
409 { /* single argument */
410 if (!streq (proc->args.decls->decl.type, "void"))
411 f_print (fout, "*argp, "); /* non-void */
413 else
415 for (l = proc->args.decls; l != NULL; l = l->next)
416 f_print (fout, "argp->%s, ", l->decl.name);
418 f_print (fout, "%s));\n}\n", RQSTP);
423 static void
424 write_program (const definition * def, const char *storage)
426 version_list *vp;
427 proc_list *proc;
428 int filled;
430 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
432 f_print (fout, "\n");
433 if (storage != NULL)
435 f_print (fout, "%s ", storage);
437 f_print (fout, "void\n");
438 pvname (def->def_name, vp->vers_num);
440 if (Cflag)
442 f_print (fout, "(struct svc_req *%s, ", RQSTP);
443 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
445 else
447 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
448 f_print (fout, " struct svc_req *%s;\n", RQSTP);
449 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
452 f_print (fout, "{\n");
454 filled = 0;
455 f_print (fout, "\tunion {\n");
456 for (proc = vp->procs; proc != NULL; proc = proc->next)
458 if (proc->arg_num < 2)
459 { /* single argument */
460 if (streq (proc->args.decls->decl.type,
461 "void"))
463 continue;
465 filled = 1;
466 f_print (fout, "\t\t");
467 ptype (proc->args.decls->decl.prefix,
468 proc->args.decls->decl.type, 0);
469 pvname (proc->proc_name, vp->vers_num);
470 f_print (fout, "_arg;\n");
473 else
475 filled = 1;
476 f_print (fout, "\t\t%s", proc->args.argname);
477 f_print (fout, " ");
478 pvname (proc->proc_name, vp->vers_num);
479 f_print (fout, "_arg;\n");
482 if (!filled)
484 f_print (fout, "\t\tint fill;\n");
486 f_print (fout, "\t} %s;\n", ARG);
487 if (mtflag)
489 f_print(fout, "\tunion {\n");
490 for (proc = vp->procs; proc != NULL; proc = proc->next)
491 if (!streq (proc->res_type, "void"))
493 f_print(fout, "\t\t");
494 ptype(proc->res_prefix, proc->res_type, 0);
495 pvname(proc->proc_name, vp->vers_num);
496 f_print(fout, "_res;\n");
498 f_print(fout, "\t} %s;\n", RESULT);
499 f_print(fout, "\tbool_t %s;\n", RETVAL);
501 } else
502 f_print (fout, "\tchar *%s;\n", RESULT);
504 if (Cflag)
506 f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
507 if (mtflag)
508 f_print(fout,
509 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
510 ROUTINE);
511 else
512 f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
513 ROUTINE);
515 else
517 f_print (fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
518 if (mtflag)
519 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
520 else
521 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
523 f_print (fout, "\n");
525 if (timerflag)
526 #if 0
527 f_print (fout, "\t_rpcsvcdirty = 1;\n");
528 #else
530 if (mtflag)
531 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
532 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
533 if (mtflag)
534 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
536 #endif
538 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
539 if (!nullproc (vp->procs))
541 f_print (fout, "\tcase NULLPROC:\n");
542 f_print (fout,
543 Cflag
544 ? "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
545 : "\t\t(void) svc_sendreply (%s, xdr_void, (char *)NULL);\n",
546 TRANSP);
547 print_return ("\t\t");
548 f_print (fout, "\n");
550 for (proc = vp->procs; proc != NULL; proc = proc->next)
552 f_print (fout, "\tcase %s:\n", proc->proc_name);
553 if (proc->arg_num < 2)
554 { /* single argument */
555 p_xdrfunc (ARG, proc->args.decls->decl.type);
557 else
559 p_xdrfunc (ARG, proc->args.argname);
561 p_xdrfunc (RESULT, proc->res_type);
562 if (Cflag)
564 if (mtflag)
565 f_print(fout,
566 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
567 ROUTINE);
568 else
569 f_print (fout,
570 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
571 ROUTINE);
573 else
574 if (mtflag)
575 f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
576 else
577 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
579 if (newstyle)
580 { /* new style: calls internal routine */
581 f_print (fout, "_");
583 if ((Cflag || mtflag) && !newstyle)
584 pvname_svc (proc->proc_name, vp->vers_num);
585 else
586 pvname (proc->proc_name, vp->vers_num);
587 f_print (fout, ";\n");
588 f_print (fout, "\t\tbreak;\n\n");
590 f_print (fout, "\tdefault:\n");
591 printerr ("noproc", TRANSP);
592 print_return ("\t\t");
593 f_print (fout, "\t}\n");
595 f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
596 if (Cflag)
597 printif ("getargs", TRANSP, "(caddr_t) &", ARG);
598 else
599 printif ("getargs", TRANSP, "&", ARG);
600 printerr ("decode", TRANSP);
601 print_return ("\t\t");
602 f_print (fout, "\t}\n");
604 if (!mtflag)
606 if (Cflag)
607 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
608 RESULT, ROUTINE, ARG, RQSTP);
609 else
610 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
611 RESULT, ROUTINE, ARG, RQSTP);
613 else
614 if (Cflag)
615 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
616 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
617 else
618 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
619 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
620 if (mtflag)
621 f_print(fout,
622 "\tif (%s > 0 && !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
623 RETVAL, TRANSP, RESULT, RESULT);
624 else
625 f_print(fout,
626 "\tif (%s != NULL && !svc_sendreply(%s, _xdr_%s, %s)) {\n",
627 RESULT, TRANSP, RESULT, RESULT);
629 printerr ("systemerr", TRANSP);
630 f_print (fout, "\t}\n");
632 if (Cflag)
633 printif ("freeargs", TRANSP, "(caddr_t) &", ARG);
634 else
635 printif ("freeargs", TRANSP, "&", ARG);
637 sprintf (_errbuf, "unable to free arguments");
638 print_err_message ("\t\t");
639 f_print (fout, "\t\texit (1);\n");
640 f_print (fout, "\t}\n");
641 /* print out free routine */
642 if (mtflag)
644 f_print(fout,"\tif (!");
645 pvname(def->def_name, vp->vers_num);
646 f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
647 TRANSP, RESULT, RESULT);
648 (void) sprintf(_errbuf, "unable to free results");
649 print_err_message("\t\t");
650 f_print(fout, "\n");
652 print_return ("\t");
653 f_print (fout, "}\n");
657 static void
658 printerr (const char *err, const char *transp)
660 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
663 static void
664 printif (const char *proc, const char *transp, const char *prefix,
665 const char *arg)
667 f_print (fout, "\tif (!svc_%s (%s, _xdr_%s, %s%s)) {\n",
668 proc, transp, arg, prefix, arg);
672 nullproc (const proc_list * proc)
674 for (; proc != NULL; proc = proc->next)
676 if (streq (proc->proc_num, "0"))
678 return 1;
681 return 0;
684 static void
685 write_inetmost (const char *infile)
687 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
688 f_print (fout, "\tint sock;\n");
689 f_print (fout, "\tint proto;\n");
690 f_print (fout, "\tstruct sockaddr_in saddr;\n");
691 f_print (fout, "\tint asize = sizeof (saddr);\n");
692 f_print (fout, "\n");
693 f_print (fout,
694 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
695 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
696 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
697 f_print (fout, "\t\t\texit (1);\n");
698 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
699 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
700 f_print (fout, "\t\t\texit (1);\n");
701 f_print (fout, "\t\tsock = 0;\n");
702 f_print (fout, "\t\t_rpcpmstart = 1;\n");
703 f_print (fout, "\t\tproto = 0;\n");
704 open_log_file (infile, "\t\t");
705 f_print (fout, "\t} else {\n");
706 write_rpc_svc_fg (infile, "\t\t");
707 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
708 print_pmapunset ("\t\t");
709 f_print (fout, "\t}\n");
712 static void
713 print_return (const char *space)
715 if (exitnow)
716 f_print (fout, "%sexit (0);\n", space);
717 else
719 if (timerflag)
721 #if 0
722 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
723 #else
724 if (mtflag)
725 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
726 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
727 if (mtflag)
728 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
729 #endif
731 f_print (fout, "%sreturn;\n", space);
735 static void
736 print_pmapunset (const char *space)
738 list *l;
739 definition *def;
740 version_list *vp;
742 for (l = defined; l != NULL; l = l->next)
744 def = (definition *) l->val;
745 if (def->def_kind == DEF_PROGRAM)
747 for (vp = def->def.pr.versions; vp != NULL;
748 vp = vp->next)
750 f_print (fout, "%spmap_unset (%s, %s);\n",
751 space, def->def_name, vp->vers_name);
757 static void
758 print_err_message (const char *space)
760 if (logflag)
761 f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
762 else if (inetdflag || pmflag)
763 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
764 else
765 f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
769 * Write the server auxiliary function ( _msgout, timeout)
771 void
772 write_svc_aux (int nomain)
774 if (!logflag)
775 write_msg_out ();
776 if (!nomain)
777 write_timeout_func ();
781 * Write the _msgout function
784 void
785 write_msg_out (void)
787 f_print (fout, "\n");
788 f_print (fout, "static\n");
789 if (!Cflag)
791 f_print (fout, "void _msgout (msg)\n");
792 f_print (fout, "\tchar *msg;\n");
794 else
796 f_print (fout, "void _msgout (char* msg)\n");
798 f_print (fout, "{\n");
799 f_print (fout, "#ifdef RPC_SVC_FG\n");
800 if (inetdflag || pmflag)
801 f_print (fout, "\tif (_rpcpmstart)\n");
802 f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
803 f_print (fout, "\telse\n");
804 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
805 f_print (fout, "#else\n");
806 f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
807 f_print (fout, "#endif\n");
808 f_print (fout, "}\n");
812 * Write the timeout function
814 static void
815 write_timeout_func (void)
817 if (!timerflag)
818 return;
819 f_print (fout, "\n");
820 f_print (fout, "static void\n");
821 if (Cflag)
822 f_print (fout, "closedown (int sig)\n");
823 else
824 f_print (fout, "closedown (sig)\n\tint sig;\n");
825 f_print (fout, "{\n");
827 #if defined (__GNU_LIBRARY__) && 0
828 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
829 Cflag ? "(SIG_PF)" : "(void(*)())");
830 #endif
831 if (mtflag)
832 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
833 #if 0
834 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
835 #else
836 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
837 #endif
838 f_print (fout, "\t\textern fd_set svc_fdset;\n");
839 f_print (fout, "\t\tstatic int size;\n");
840 f_print (fout, "\t\tint i, openfd;\n");
841 if (tirpcflag && pmflag)
843 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
844 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
846 else
848 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
850 f_print (fout, "\t\t\texit (0);\n");
851 f_print (fout, "\t\tif (size == 0) {\n");
852 if (tirpcflag)
854 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
855 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
856 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
857 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
858 if (mtflag)
859 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
860 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
862 else
864 f_print (fout, "\t\t\tsize = getdtablesize();\n");
866 f_print (fout, "\t\t}\n");
867 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
868 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
869 f_print (fout, "\t\t\t\topenfd++;\n");
870 f_print (fout, "\t\tif (openfd <= 1)\n");
871 f_print (fout, "\t\t\texit (0);\n");
872 f_print (fout, "\t}\n");
873 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
874 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
875 if (mtflag)
876 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
877 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
878 Cflag? "(SIG_PF)" : "(void(*)())");
879 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
880 f_print (fout, "}\n");
884 * Write the most of port monitor support
886 static void
887 write_pm_most (const char *infile, int netflag)
889 list *l;
890 definition *def;
891 version_list *vp;
893 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
894 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
895 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
896 f_print (fout, "\t\tchar *netid;\n");
897 if (!netflag)
898 { /* Not included by -n option */
899 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
900 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
902 if (timerflag)
903 f_print (fout, "\t\tint pmclose;\n");
904 /* not necessary, defined in /usr/include/stdlib */
905 /* f_print(fout, "\t\textern char *getenv();\n"); */
906 f_print (fout, "\n");
907 f_print (fout, "\t\t_rpcpmstart = 1;\n");
908 if (logflag)
909 open_log_file (infile, "\t\t");
910 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
911 sprintf (_errbuf, "cannot get transport name");
912 print_err_message ("\t\t\t");
913 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
914 sprintf (_errbuf, "cannot get transport info");
915 print_err_message ("\t\t\t");
916 f_print (fout, "\t\t}\n");
918 * A kludgy support for inetd services. Inetd only works with
919 * sockmod, and RPC works only with timod, hence all this jugglery
921 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
922 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
923 sprintf (_errbuf, "could not get the right module");
924 print_err_message ("\t\t\t\t");
925 f_print (fout, "\t\t\t\texit(1);\n");
926 f_print (fout, "\t\t\t}\n");
927 f_print (fout, "\t\t}\n");
928 if (timerflag)
929 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
930 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
931 TRANSP);
932 sprintf (_errbuf, "cannot create server handle");
933 print_err_message ("\t\t\t");
934 f_print (fout, "\t\t\texit(1);\n");
935 f_print (fout, "\t\t}\n");
936 f_print (fout, "\t\tif (nconf)\n");
937 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
938 for (l = defined; l != NULL; l = l->next)
940 def = (definition *) l->val;
941 if (def->def_kind != DEF_PROGRAM)
943 continue;
945 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
947 f_print (fout,
948 "\t\tif (!svc_reg(%s, %s, %s, ",
949 TRANSP, def->def_name, vp->vers_name);
950 pvname (def->def_name, vp->vers_num);
951 f_print (fout, ", 0)) {\n");
952 (void) sprintf (_errbuf, "unable to register (%s, %s).",
953 def->def_name, vp->vers_name);
954 print_err_message ("\t\t\t");
955 f_print (fout, "\t\t\texit(1);\n");
956 f_print (fout, "\t\t}\n");
959 if (timerflag)
961 f_print (fout, "\t\tif (pmclose) {\n");
962 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
963 Cflag ? "(SIG_PF)" : "(void(*)())");
964 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
965 f_print (fout, "\t\t}\n");
967 f_print (fout, "\t\tsvc_run();\n");
968 f_print (fout, "\t\texit(1);\n");
969 f_print (fout, "\t\t/* NOTREACHED */\n");
970 f_print (fout, "\t}\n");
974 * Support for backgrounding the server if self started.
976 static void
977 write_rpc_svc_fg (const char *infile, const char *sp)
979 f_print (fout, "#ifndef RPC_SVC_FG\n");
980 f_print (fout, "%sint size;\n", sp);
981 if (tirpcflag)
982 f_print (fout, "%sstruct rlimit rl;\n", sp);
983 if (inetdflag)
984 f_print (fout, "%sint pid, i;\n\n", sp);
985 f_print (fout, "%spid = fork();\n", sp);
986 f_print (fout, "%sif (pid < 0) {\n", sp);
987 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
988 f_print (fout, "%s\texit(1);\n", sp);
989 f_print (fout, "%s}\n", sp);
990 f_print (fout, "%sif (pid)\n", sp);
991 f_print (fout, "%s\texit(0);\n", sp);
992 /* get number of file descriptors */
993 if (tirpcflag)
995 f_print (fout, "%srl.rlim_max = 0;\n", sp);
996 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
997 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
998 f_print (fout, "%s\texit(1);\n", sp);
1000 else
1002 f_print (fout, "%ssize = getdtablesize();\n", sp);
1005 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
1006 f_print (fout, "%s\t(void) close(i);\n", sp);
1007 /* Redirect stderr and stdout to console */
1008 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
1009 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
1010 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
1011 /* This removes control of the controlling terminal */
1012 if (tirpcflag)
1013 f_print (fout, "%ssetsid();\n", sp);
1014 else
1016 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1017 f_print (fout, "%sif (i >= 0) {\n", sp);
1018 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
1019 f_print (fout, "%s\t(void) close(i);\n", sp);
1020 f_print (fout, "%s}\n", sp);
1022 if (!logflag)
1023 open_log_file (infile, sp);
1024 f_print (fout, "#endif\n");
1025 if (logflag)
1026 open_log_file (infile, sp);
1029 static void
1030 open_log_file (const char *infile, const char *sp)
1032 char *s;
1034 s = strrchr (infile, '.');
1035 if (s)
1036 *s = '\0';
1037 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1038 if (s)
1039 *s = '.';
1043 * write a registration for the given transport for Inetd
1045 void
1046 write_inetd_register (const char *transp)
1048 list *l;
1049 definition *def;
1050 version_list *vp;
1051 const char *sp;
1052 int isudp;
1053 char tmpbuf[32];
1055 if (inetdflag)
1056 sp = "\t";
1057 else
1058 sp = "";
1059 if (streq (transp, "udp") || streq (transp, "udp6"))
1060 isudp = 1;
1061 else
1062 isudp = 0;
1063 f_print (fout, "\n");
1064 if (inetdflag)
1066 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1067 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1069 f_print (fout, "%s\t%s = svc%s_create(%s",
1070 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1071 if (!isudp)
1072 f_print (fout, ", 0, 0");
1073 f_print (fout, ");\n");
1074 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1075 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1076 (void) sprintf (tmpbuf, "%s\t\t", sp);
1077 print_err_message (tmpbuf);
1078 f_print (fout, "%s\t\texit(1);\n", sp);
1079 f_print (fout, "%s\t}\n", sp);
1081 if (inetdflag)
1083 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1084 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1085 sp, isudp ? "UDP" : "TCP");
1087 for (l = defined; l != NULL; l = l->next)
1089 def = (definition *) l->val;
1090 if (def->def_kind != DEF_PROGRAM)
1092 continue;
1094 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1096 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1097 sp, TRANSP, def->def_name, vp->vers_name);
1098 pvname (def->def_name, vp->vers_num);
1099 if (inetdflag)
1100 f_print (fout, ", proto)) {\n");
1101 else
1102 f_print (fout, ", IPPROTO_%s)) {\n",
1103 isudp ? "UDP" : "TCP");
1104 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1105 def->def_name, vp->vers_name, transp);
1106 print_err_message (tmpbuf);
1107 f_print (fout, "%s\t\texit(1);\n", sp);
1108 f_print (fout, "%s\t}\n", sp);
1111 if (inetdflag)
1112 f_print (fout, "\t}\n");