Update.
[glibc.git] / sunrpc / rpc_svcout.c
blob9b5f1dd0f56625ddd9e4dea53d3082b347395f17
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\txdr_%s = (xdrproc_t) xdr_%s;\n", rname,
76 stringfix (typename));
77 else
78 f_print (fout, "\t\txdr_%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)
605 if (Cflag)
606 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
607 RESULT, ROUTINE, ARG, RQSTP);
608 else
609 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
610 RESULT, ROUTINE, ARG, RQSTP);
611 else
612 if (Cflag)
613 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
614 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
615 else
616 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
617 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
618 if (mtflag)
619 f_print(fout,
620 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
621 RETVAL, TRANSP, RESULT, RESULT);
622 else
623 f_print(fout,
624 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
625 RESULT, TRANSP, RESULT, RESULT);
627 printerr ("systemerr", TRANSP);
628 f_print (fout, "\t}\n");
630 if (Cflag)
631 printif ("freeargs", TRANSP, "(caddr_t) &", ARG);
632 else
633 printif ("freeargs", TRANSP, "&", ARG);
635 sprintf (_errbuf, "unable to free arguments");
636 print_err_message ("\t\t");
637 f_print (fout, "\t\texit (1);\n");
638 f_print (fout, "\t}\n");
639 /* print out free routine */
640 if (mtflag)
642 f_print(fout,"\tif (!");
643 pvname(def->def_name, vp->vers_num);
644 f_print(fout,"_freeresult (%s, xdr_%s, (caddr_t) &%s))\n",
645 TRANSP, RESULT, RESULT);
646 (void) sprintf(_errbuf, "unable to free results");
647 print_err_message("\t\t");
648 f_print(fout, "\n");
650 print_return ("\t");
651 f_print (fout, "}\n");
655 static void
656 printerr (const char *err, const char *transp)
658 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
661 static void
662 printif (const char *proc, const char *transp, const char *prefix,
663 const char *arg)
665 f_print (fout, "\tif (!svc_%s (%s, xdr_%s, %s%s)) {\n",
666 proc, transp, arg, prefix, arg);
670 nullproc (const proc_list * proc)
672 for (; proc != NULL; proc = proc->next)
674 if (streq (proc->proc_num, "0"))
676 return 1;
679 return 0;
682 static void
683 write_inetmost (const char *infile)
685 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
686 f_print (fout, "\tint sock;\n");
687 f_print (fout, "\tint proto;\n");
688 f_print (fout, "\tstruct sockaddr_in saddr;\n");
689 f_print (fout, "\tint asize = sizeof (saddr);\n");
690 f_print (fout, "\n");
691 f_print (fout,
692 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
693 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
694 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
695 f_print (fout, "\t\t\texit (1);\n");
696 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
697 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
698 f_print (fout, "\t\t\texit (1);\n");
699 f_print (fout, "\t\tsock = 0;\n");
700 f_print (fout, "\t\t_rpcpmstart = 1;\n");
701 f_print (fout, "\t\tproto = 0;\n");
702 open_log_file (infile, "\t\t");
703 f_print (fout, "\t} else {\n");
704 write_rpc_svc_fg (infile, "\t\t");
705 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
706 print_pmapunset ("\t\t");
707 f_print (fout, "\t}\n");
710 static void
711 print_return (const char *space)
713 if (exitnow)
714 f_print (fout, "%sexit (0);\n", space);
715 else
717 if (timerflag)
719 #if 0
720 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
721 #else
722 if (mtflag)
723 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
724 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
725 if (mtflag)
726 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
727 #endif
729 f_print (fout, "%sreturn;\n", space);
733 static void
734 print_pmapunset (const char *space)
736 list *l;
737 definition *def;
738 version_list *vp;
740 for (l = defined; l != NULL; l = l->next)
742 def = (definition *) l->val;
743 if (def->def_kind == DEF_PROGRAM)
745 for (vp = def->def.pr.versions; vp != NULL;
746 vp = vp->next)
748 f_print (fout, "%spmap_unset (%s, %s);\n",
749 space, def->def_name, vp->vers_name);
755 static void
756 print_err_message (const char *space)
758 if (logflag)
759 f_print (fout, "%ssyslog (LOG_ERR, \"%s\");\n", space, _errbuf);
760 else if (inetdflag || pmflag)
761 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
762 else
763 f_print (fout, "%sfprintf (stderr, \"%s\");\n", space, _errbuf);
767 * Write the server auxiliary function ( _msgout, timeout)
769 void
770 write_svc_aux (int nomain)
772 if (!logflag)
773 write_msg_out ();
774 if (!nomain)
775 write_timeout_func ();
779 * Write the _msgout function
782 void
783 write_msg_out (void)
785 f_print (fout, "\n");
786 f_print (fout, "static\n");
787 if (!Cflag)
789 f_print (fout, "void _msgout (msg)\n");
790 f_print (fout, "\tchar *msg;\n");
792 else
794 f_print (fout, "void _msgout (char* msg)\n");
796 f_print (fout, "{\n");
797 f_print (fout, "#ifdef RPC_SVC_FG\n");
798 if (inetdflag || pmflag)
799 f_print (fout, "\tif (_rpcpmstart)\n");
800 f_print (fout, "\t\tsyslog (LOG_ERR, msg);\n");
801 f_print (fout, "\telse\n");
802 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
803 f_print (fout, "#else\n");
804 f_print (fout, "\tsyslog (LOG_ERR, msg);\n");
805 f_print (fout, "#endif\n");
806 f_print (fout, "}\n");
810 * Write the timeout function
812 static void
813 write_timeout_func (void)
815 if (!timerflag)
816 return;
817 f_print (fout, "\n");
818 f_print (fout, "static void\n");
819 if (Cflag)
820 f_print (fout, "closedown (int sig)\n");
821 else
822 f_print (fout, "closedown (sig)\n\tint sig;\n");
823 f_print (fout, "{\n");
825 #if defined (__GNU_LIBRARY__) && 0
826 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
827 Cflag ? "(SIG_PF)" : "(void(*)())");
828 #endif
829 if (mtflag)
830 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
831 #if 0
832 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
833 #else
834 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
835 #endif
836 f_print (fout, "\t\textern fd_set svc_fdset;\n");
837 f_print (fout, "\t\tstatic int size;\n");
838 f_print (fout, "\t\tint i, openfd;\n");
839 if (tirpcflag && pmflag)
841 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
842 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
844 else
846 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
848 f_print (fout, "\t\t\texit (0);\n");
849 f_print (fout, "\t\tif (size == 0) {\n");
850 if (tirpcflag)
852 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
853 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
854 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
855 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
856 if (mtflag)
857 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
858 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
860 else
862 f_print (fout, "\t\t\tsize = getdtablesize();\n");
864 f_print (fout, "\t\t}\n");
865 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
866 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
867 f_print (fout, "\t\t\t\topenfd++;\n");
868 f_print (fout, "\t\tif (openfd <= 1)\n");
869 f_print (fout, "\t\t\texit (0);\n");
870 f_print (fout, "\t}\n");
871 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
872 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
873 if (mtflag)
874 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
875 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
876 Cflag? "(SIG_PF)" : "(void(*)())");
877 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
878 f_print (fout, "}\n");
882 * Write the most of port monitor support
884 static void
885 write_pm_most (const char *infile, int netflag)
887 list *l;
888 definition *def;
889 version_list *vp;
891 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
892 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
893 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
894 f_print (fout, "\t\tchar *netid;\n");
895 if (!netflag)
896 { /* Not included by -n option */
897 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
898 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
900 if (timerflag)
901 f_print (fout, "\t\tint pmclose;\n");
902 /* not necessary, defined in /usr/include/stdlib */
903 /* f_print(fout, "\t\textern char *getenv();\n"); */
904 f_print (fout, "\n");
905 f_print (fout, "\t\t_rpcpmstart = 1;\n");
906 if (logflag)
907 open_log_file (infile, "\t\t");
908 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
909 sprintf (_errbuf, "cannot get transport name");
910 print_err_message ("\t\t\t");
911 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
912 sprintf (_errbuf, "cannot get transport info");
913 print_err_message ("\t\t\t");
914 f_print (fout, "\t\t}\n");
916 * A kludgy support for inetd services. Inetd only works with
917 * sockmod, and RPC works only with timod, hence all this jugglery
919 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
920 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
921 sprintf (_errbuf, "could not get the right module");
922 print_err_message ("\t\t\t\t");
923 f_print (fout, "\t\t\t\texit(1);\n");
924 f_print (fout, "\t\t\t}\n");
925 f_print (fout, "\t\t}\n");
926 if (timerflag)
927 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
928 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
929 TRANSP);
930 sprintf (_errbuf, "cannot create server handle");
931 print_err_message ("\t\t\t");
932 f_print (fout, "\t\t\texit(1);\n");
933 f_print (fout, "\t\t}\n");
934 f_print (fout, "\t\tif (nconf)\n");
935 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
936 for (l = defined; l != NULL; l = l->next)
938 def = (definition *) l->val;
939 if (def->def_kind != DEF_PROGRAM)
941 continue;
943 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
945 f_print (fout,
946 "\t\tif (!svc_reg(%s, %s, %s, ",
947 TRANSP, def->def_name, vp->vers_name);
948 pvname (def->def_name, vp->vers_num);
949 f_print (fout, ", 0)) {\n");
950 (void) sprintf (_errbuf, "unable to register (%s, %s).",
951 def->def_name, vp->vers_name);
952 print_err_message ("\t\t\t");
953 f_print (fout, "\t\t\texit(1);\n");
954 f_print (fout, "\t\t}\n");
957 if (timerflag)
959 f_print (fout, "\t\tif (pmclose) {\n");
960 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
961 Cflag ? "(SIG_PF)" : "(void(*)())");
962 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
963 f_print (fout, "\t\t}\n");
965 f_print (fout, "\t\tsvc_run();\n");
966 f_print (fout, "\t\texit(1);\n");
967 f_print (fout, "\t\t/* NOTREACHED */\n");
968 f_print (fout, "\t}\n");
972 * Support for backgrounding the server if self started.
974 static void
975 write_rpc_svc_fg (const char *infile, const char *sp)
977 f_print (fout, "#ifndef RPC_SVC_FG\n");
978 f_print (fout, "%sint size;\n", sp);
979 if (tirpcflag)
980 f_print (fout, "%sstruct rlimit rl;\n", sp);
981 if (inetdflag)
982 f_print (fout, "%sint pid, i;\n\n", sp);
983 f_print (fout, "%spid = fork();\n", sp);
984 f_print (fout, "%sif (pid < 0) {\n", sp);
985 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
986 f_print (fout, "%s\texit(1);\n", sp);
987 f_print (fout, "%s}\n", sp);
988 f_print (fout, "%sif (pid)\n", sp);
989 f_print (fout, "%s\texit(0);\n", sp);
990 /* get number of file descriptors */
991 if (tirpcflag)
993 f_print (fout, "%srl.rlim_max = 0;\n", sp);
994 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
995 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
996 f_print (fout, "%s\texit(1);\n", sp);
998 else
1000 f_print (fout, "%ssize = getdtablesize();\n", sp);
1003 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
1004 f_print (fout, "%s\t(void) close(i);\n", sp);
1005 /* Redirect stderr and stdout to console */
1006 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
1007 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
1008 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
1009 /* This removes control of the controlling terminal */
1010 if (tirpcflag)
1011 f_print (fout, "%ssetsid();\n", sp);
1012 else
1014 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1015 f_print (fout, "%sif (i >= 0) {\n", sp);
1016 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
1017 f_print (fout, "%s\t(void) close(i);\n", sp);
1018 f_print (fout, "%s}\n", sp);
1020 if (!logflag)
1021 open_log_file (infile, sp);
1022 f_print (fout, "#endif\n");
1023 if (logflag)
1024 open_log_file (infile, sp);
1027 static void
1028 open_log_file (const char *infile, const char *sp)
1030 char *s;
1032 s = strrchr (infile, '.');
1033 if (s)
1034 *s = '\0';
1035 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1036 if (s)
1037 *s = '.';
1041 * write a registration for the given transport for Inetd
1043 void
1044 write_inetd_register (const char *transp)
1046 list *l;
1047 definition *def;
1048 version_list *vp;
1049 const char *sp;
1050 int isudp;
1051 char tmpbuf[32];
1053 if (inetdflag)
1054 sp = "\t";
1055 else
1056 sp = "";
1057 if (streq (transp, "udp"))
1058 isudp = 1;
1059 else
1060 isudp = 0;
1061 f_print (fout, "\n");
1062 if (inetdflag)
1064 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1065 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1067 f_print (fout, "%s\t%s = svc%s_create(%s",
1068 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1069 if (!isudp)
1070 f_print (fout, ", 0, 0");
1071 f_print (fout, ");\n");
1072 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1073 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1074 (void) sprintf (tmpbuf, "%s\t\t", sp);
1075 print_err_message (tmpbuf);
1076 f_print (fout, "%s\t\texit(1);\n", sp);
1077 f_print (fout, "%s\t}\n", sp);
1079 if (inetdflag)
1081 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1082 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1083 sp, isudp ? "UDP" : "TCP");
1085 for (l = defined; l != NULL; l = l->next)
1087 def = (definition *) l->val;
1088 if (def->def_kind != DEF_PROGRAM)
1090 continue;
1092 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1094 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1095 sp, TRANSP, def->def_name, vp->vers_name);
1096 pvname (def->def_name, vp->vers_num);
1097 if (inetdflag)
1098 f_print (fout, ", proto)) {\n");
1099 else
1100 f_print (fout, ", IPPROTO_%s)) {\n",
1101 isudp ? "UDP" : "TCP");
1102 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1103 def->def_name, vp->vers_name, transp);
1104 print_err_message (tmpbuf);
1105 f_print (fout, "%s\t\texit(1);\n", sp);
1106 f_print (fout, "%s\t}\n", sp);
1109 if (inetdflag)
1110 f_print (fout, "\t}\n");