Wrapper around wcstod.c for use as wcstod_l.
[glibc/history.git] / sunrpc / rpc_svcout.c
blob75eb695e87f352ef7aa99915a2a2e4caee2e0048
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";
52 char _errbuf[256]; /* For all messages */
54 static void internal_proctype (const proc_list * plist);
55 static void p_xdrfunc (const char *rname, const char *typename);
56 static void write_real_program (const definition * def);
57 static void write_program (const definition * def, const char *storage);
58 static void printerr (const char *err, const char *transp);
59 static void printif (const char *proc, const char *transp,
60 const char *prefix, const char *arg);
61 static void write_inetmost (const char *infile);
62 static void print_return (const char *space);
63 static void print_pmapunset (const char *space);
64 static void print_err_message (const char *space);
65 static void write_timeout_func (void);
66 static void write_pm_most (const char *infile, int netflag);
67 static void write_rpc_svc_fg (const char *infile, const char *sp);
68 static void open_log_file (const char *infile, const char *sp);
70 static void
71 p_xdrfunc (const char *rname, const char *typename)
73 if (Cflag)
74 f_print (fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname,
75 stringfix (typename));
76 else
77 f_print (fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix (typename));
80 void
81 internal_proctype (const proc_list * plist)
83 f_print (fout, "static ");
84 ptype (plist->res_prefix, plist->res_type, 1);
85 f_print (fout, "*");
90 * write most of the service, that is, everything but the registrations.
92 void
93 write_most (const char *infile /* our name */ , int netflag, int nomain)
95 if (inetdflag || pmflag)
97 const char *var_type;
98 #ifdef __GNU_LIBRARY__
99 /* WHY? */
100 var_type = (nomain ? "extern" : "");
101 #else
102 var_type = (nomain ? "extern" : "static");
103 #endif
104 f_print (fout, "%s int _rpcpmstart;", var_type);
105 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
106 f_print (fout, "%s int _rpcfdtype;", var_type);
107 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
108 if (timerflag)
110 f_print (fout, "%s int _rpcsvcdirty;", var_type);
111 f_print (fout, "\t/* Still serving ? */\n");
113 write_svc_aux (nomain);
115 /* write out dispatcher and stubs */
116 write_programs (nomain ? NULL : "static");
118 if (nomain)
119 return;
121 #ifdef __GNU_LIBRARY__
122 if (Cflag)
123 f_print (fout, "\nint\nmain(int argc, char **argv)\n");
124 else
126 f_print (fout, "\nint\nmain(argc, argv)\n");
127 f_print (fout, "\tint argc;\n");
128 f_print (fout, "\tchar **argv;\n");
130 #else
131 f_print (fout, "\nmain()\n");
132 #endif
133 f_print (fout, "{\n");
134 if (inetdflag)
136 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
138 else
140 if (tirpcflag)
142 if (netflag)
144 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
145 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
147 f_print (fout, "\tpid_t pid;\n");
148 f_print (fout, "\tint i;\n");
149 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
150 write_pm_most (infile, netflag);
151 f_print (fout, "\telse {\n");
152 write_rpc_svc_fg (infile, "\t\t");
153 f_print (fout, "\t}\n");
155 else
157 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
158 f_print (fout, "\n");
159 print_pmapunset ("\t");
163 if (logflag && !inetdflag)
165 open_log_file (infile, "\t");
170 * write a registration for the given transport
172 void
173 write_netid_register (const char *transp)
175 list *l;
176 definition *def;
177 version_list *vp;
178 const char *sp;
179 char tmpbuf[32];
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 (void) 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 /*, transp *//* ?!?... */ );
192 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
193 (void) 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);
198 for (l = defined; l != NULL; l = l->next)
200 def = (definition *) l->val;
201 if (def->def_kind != DEF_PROGRAM)
203 continue;
205 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
207 f_print (fout,
208 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
209 sp, def->def_name, vp->vers_name);
210 f_print (fout,
211 "%s\tif (!svc_reg(%s, %s, %s, ",
212 sp, TRANSP, def->def_name, vp->vers_name);
213 pvname (def->def_name, vp->vers_num);
214 f_print (fout, ", nconf)) {\n");
215 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
216 def->def_name, vp->vers_name, transp);
217 print_err_message (tmpbuf);
218 f_print (fout, "%s\t\texit(1);\n", sp);
219 f_print (fout, "%s\t}\n", sp);
222 f_print (fout, "%s\tfreenetconfigent(nconf);\n", sp);
226 * write a registration for the given transport for TLI
228 void
229 write_nettype_register (const char *transp)
231 list *l;
232 definition *def;
233 version_list *vp;
235 for (l = defined; l != NULL; l = l->next)
237 def = (definition *) l->val;
238 if (def->def_kind != DEF_PROGRAM)
240 continue;
242 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
244 f_print (fout, "\tif (!svc_create(");
245 pvname (def->def_name, vp->vers_num);
246 f_print (fout, ", %s, %s, \"%s\")) {\n ",
247 def->def_name, vp->vers_name, transp);
248 (void) sprintf (_errbuf,
249 "unable to create (%s, %s) for %s.",
250 def->def_name, vp->vers_name, transp);
251 print_err_message ("\t\t");
252 f_print (fout, "\t\texit(1);\n");
253 f_print (fout, "\t}\n");
259 * write the rest of the service
261 void
262 write_rest (void)
264 f_print (fout, "\n");
265 if (inetdflag)
267 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
268 (void) sprintf (_errbuf, "could not create a handle");
269 print_err_message ("\t\t");
270 f_print (fout, "\t\texit(1);\n");
271 f_print (fout, "\t}\n");
272 if (timerflag)
274 f_print (fout, "\tif (_rpcpmstart) {\n");
275 f_print (fout,
276 "\t\t(void) signal(SIGALRM, %s closedown);\n",
277 Cflag ? "(SIG_PF)" : "(void(*)())");
278 f_print (fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
279 f_print (fout, "\t}\n");
282 f_print (fout, "\tsvc_run();\n");
283 (void) sprintf (_errbuf, "svc_run returned");
284 print_err_message ("\t");
285 f_print (fout, "\texit(1);\n");
286 f_print (fout, "\t/* NOTREACHED */\n");
287 f_print (fout, "}\n");
290 void
291 write_programs (const char *storage)
293 list *l;
294 definition *def;
296 /* write out stubs for procedure definitions */
297 for (l = defined; l != NULL; l = l->next)
299 def = (definition *) l->val;
300 if (def->def_kind == DEF_PROGRAM)
302 write_real_program (def);
306 /* write out dispatcher for each program */
307 for (l = defined; l != NULL; l = l->next)
309 def = (definition *) l->val;
310 if (def->def_kind == DEF_PROGRAM)
312 write_program (def, storage);
319 /* write out definition of internal function (e.g. _printmsg_1(...))
320 which calls server's defintion of actual function (e.g. printmsg_1(...)).
321 Unpacks single user argument of printmsg_1 to call-by-value format
322 expected by printmsg_1. */
323 static void
324 write_real_program (const definition * def)
326 version_list *vp;
327 proc_list *proc;
328 decl_list *l;
330 if (!newstyle)
331 return; /* not needed for old style */
332 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
334 for (proc = vp->procs; proc != NULL; proc = proc->next)
336 f_print (fout, "\n");
337 internal_proctype (proc);
338 f_print (fout, "\n_");
339 pvname (proc->proc_name, vp->vers_num);
340 if (Cflag)
342 f_print (fout, "(");
343 /* arg name */
344 if (proc->arg_num > 1)
345 f_print (fout, proc->args.argname);
346 else
347 ptype (proc->args.decls->decl.prefix,
348 proc->args.decls->decl.type, 0);
349 f_print (fout, " *argp, struct svc_req *%s)\n",
350 RQSTP);
352 else
354 f_print (fout, "(argp, %s)\n", RQSTP);
355 /* arg name */
356 if (proc->arg_num > 1)
357 f_print (fout, "\t%s *argp;\n", proc->args.argname);
358 else
360 f_print (fout, "\t");
361 ptype (proc->args.decls->decl.prefix,
362 proc->args.decls->decl.type, 0);
363 f_print (fout, " *argp;\n");
365 f_print (fout, " struct svc_req *%s;\n", RQSTP);
368 f_print (fout, "{\n");
369 f_print (fout, "\treturn(");
370 if (Cflag)
371 pvname_svc (proc->proc_name, vp->vers_num);
372 else
373 pvname (proc->proc_name, vp->vers_num);
374 f_print (fout, "(");
375 if (proc->arg_num < 2)
376 { /* single argument */
377 if (!streq (proc->args.decls->decl.type, "void"))
378 f_print (fout, "*argp, "); /* non-void */
380 else
382 for (l = proc->args.decls; l != NULL; l = l->next)
383 f_print (fout, "argp->%s, ", l->decl.name);
385 f_print (fout, "%s));\n}\n", RQSTP);
390 static void
391 write_program (const definition * def, const char *storage)
393 version_list *vp;
394 proc_list *proc;
395 int filled;
397 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
399 f_print (fout, "\n");
400 if (storage != NULL)
402 f_print (fout, "%s ", storage);
404 f_print (fout, "void\n");
405 pvname (def->def_name, vp->vers_num);
407 if (Cflag)
409 f_print (fout, "(struct svc_req *%s, ", RQSTP);
410 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
412 else
414 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
415 f_print (fout, " struct svc_req *%s;\n", RQSTP);
416 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
419 f_print (fout, "{\n");
421 filled = 0;
422 f_print (fout, "\tunion {\n");
423 for (proc = vp->procs; proc != NULL; proc = proc->next)
425 if (proc->arg_num < 2)
426 { /* single argument */
427 if (streq (proc->args.decls->decl.type,
428 "void"))
430 continue;
432 filled = 1;
433 f_print (fout, "\t\t");
434 ptype (proc->args.decls->decl.prefix,
435 proc->args.decls->decl.type, 0);
436 pvname (proc->proc_name, vp->vers_num);
437 f_print (fout, "_arg;\n");
440 else
442 filled = 1;
443 f_print (fout, "\t\t%s", proc->args.argname);
444 f_print (fout, " ");
445 pvname (proc->proc_name, vp->vers_num);
446 f_print (fout, "_arg;\n");
449 if (!filled)
451 f_print (fout, "\t\tint fill;\n");
453 f_print (fout, "\t} %s;\n", ARG);
454 f_print (fout, "\tchar *%s;\n", RESULT);
456 if (Cflag)
458 f_print (fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
459 f_print (fout,
460 "\tchar *(*%s)(char *, struct svc_req *);\n",
461 ROUTINE);
463 else
465 f_print (fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
466 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
469 f_print (fout, "\n");
471 if (timerflag)
472 f_print (fout, "\t_rpcsvcdirty = 1;\n");
473 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
474 if (!nullproc (vp->procs))
476 f_print (fout, "\tcase NULLPROC:\n");
477 f_print (fout,
478 Cflag
479 ? "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
480 : "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
481 TRANSP);
482 print_return ("\t\t");
483 f_print (fout, "\n");
485 for (proc = vp->procs; proc != NULL; proc = proc->next)
487 f_print (fout, "\tcase %s:\n", proc->proc_name);
488 if (proc->arg_num < 2)
489 { /* single argument */
490 p_xdrfunc (ARG, proc->args.decls->decl.type);
492 else
494 p_xdrfunc (ARG, proc->args.argname);
496 p_xdrfunc (RESULT, proc->res_type);
497 if (Cflag)
498 f_print (fout,
499 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
500 ROUTINE);
501 else
502 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
504 if (newstyle)
505 { /* new style: calls internal routine */
506 f_print (fout, "_");
508 if (Cflag && !newstyle)
509 pvname_svc (proc->proc_name, vp->vers_num);
510 else
511 pvname (proc->proc_name, vp->vers_num);
512 f_print (fout, ";\n");
513 f_print (fout, "\t\tbreak;\n\n");
515 f_print (fout, "\tdefault:\n");
516 printerr ("noproc", TRANSP);
517 print_return ("\t\t");
518 f_print (fout, "\t}\n");
520 f_print (fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
521 if (Cflag)
522 printif ("getargs", TRANSP, "(caddr_t) &", ARG);
523 else
524 printif ("getargs", TRANSP, "&", ARG);
525 printerr ("decode", TRANSP);
526 print_return ("\t\t");
527 f_print (fout, "\t}\n");
529 if (Cflag)
530 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
531 RESULT, ROUTINE, ARG, RQSTP);
532 else
533 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
534 RESULT, ROUTINE, ARG, RQSTP);
535 f_print (fout,
536 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
537 RESULT, TRANSP, RESULT, RESULT);
538 printerr ("systemerr", TRANSP);
539 f_print (fout, "\t}\n");
541 if (Cflag)
542 printif ("freeargs", TRANSP, "(caddr_t) &", ARG);
543 else
544 printif ("freeargs", TRANSP, "&", ARG);
545 (void) sprintf (_errbuf, "unable to free arguments");
546 print_err_message ("\t\t");
547 f_print (fout, "\t\texit(1);\n");
548 f_print (fout, "\t}\n");
549 print_return ("\t");
550 f_print (fout, "}\n");
554 static void
555 printerr (const char *err, const char *transp)
557 f_print (fout, "\t\tsvcerr_%s(%s);\n", err, transp);
560 static void
561 printif (const char *proc, const char *transp, const char *prefix,
562 const char *arg)
564 f_print (fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
565 proc, transp, arg, prefix, arg);
569 nullproc (const proc_list * proc)
571 for (; proc != NULL; proc = proc->next)
573 if (streq (proc->proc_num, "0"))
575 return (1);
578 return (0);
581 static void
582 write_inetmost (const char *infile)
584 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
585 f_print (fout, "\tint sock;\n");
586 f_print (fout, "\tint proto;\n");
587 f_print (fout, "\tstruct sockaddr_in saddr;\n");
588 f_print (fout, "\tint asize = sizeof (saddr);\n");
589 f_print (fout, "\n");
590 f_print (fout,
591 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
592 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
593 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
594 f_print (fout, "\t\t\texit(1);\n");
595 f_print (fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
596 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
597 f_print (fout, "\t\t\texit(1);\n");
598 f_print (fout, "\t\tsock = 0;\n");
599 f_print (fout, "\t\t_rpcpmstart = 1;\n");
600 f_print (fout, "\t\tproto = 0;\n");
601 open_log_file (infile, "\t\t");
602 f_print (fout, "\t} else {\n");
603 write_rpc_svc_fg (infile, "\t\t");
604 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
605 print_pmapunset ("\t\t");
606 f_print (fout, "\t}\n");
609 static void
610 print_return (const char *space)
612 if (exitnow)
613 f_print (fout, "%sexit(0);\n", space);
614 else
616 if (timerflag)
617 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
618 f_print (fout, "%sreturn;\n", space);
622 static void
623 print_pmapunset (const char *space)
625 list *l;
626 definition *def;
627 version_list *vp;
629 for (l = defined; l != NULL; l = l->next)
631 def = (definition *) l->val;
632 if (def->def_kind == DEF_PROGRAM)
634 for (vp = def->def.pr.versions; vp != NULL;
635 vp = vp->next)
637 f_print (fout, "%s(void) pmap_unset(%s, %s);\n",
638 space, def->def_name, vp->vers_name);
644 static void
645 print_err_message (const char *space)
647 if (logflag)
648 f_print (fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
649 else if (inetdflag || pmflag)
650 f_print (fout, "%s_msgout(\"%s\");\n", space, _errbuf);
651 else
652 f_print (fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
656 * Write the server auxiliary function ( _msgout, timeout)
658 void
659 write_svc_aux (int nomain)
661 if (!logflag)
662 write_msg_out ();
663 if (!nomain)
664 write_timeout_func ();
668 * Write the _msgout function
671 void
672 write_msg_out (void)
674 f_print (fout, "\n");
675 f_print (fout, "static\n");
676 if (!Cflag)
678 f_print (fout, "void _msgout(msg)\n");
679 f_print (fout, "\tchar *msg;\n");
681 else
683 f_print (fout, "void _msgout(char* msg)\n");
685 f_print (fout, "{\n");
686 f_print (fout, "#ifdef RPC_SVC_FG\n");
687 if (inetdflag || pmflag)
688 f_print (fout, "\tif (_rpcpmstart)\n");
689 f_print (fout, "\t\tsyslog(LOG_ERR, msg);\n");
690 f_print (fout, "\telse\n");
691 f_print (fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
692 f_print (fout, "#else\n");
693 f_print (fout, "\tsyslog(LOG_ERR, msg);\n");
694 f_print (fout, "#endif\n");
695 f_print (fout, "}\n");
699 * Write the timeout function
701 static void
702 write_timeout_func (void)
704 if (!timerflag)
705 return;
706 f_print (fout, "\n");
707 f_print (fout, "static void\n");
708 #ifdef __GNU_LIBRARY__
709 if (Cflag)
710 f_print (fout, "closedown(int sig)\n");
711 else
712 f_print (fout, "closedown(sig)\n\tint sig;\n");
713 #else
714 f_print (fout, "closedown()\n");
715 #endif
716 f_print (fout, "{\n");
717 #ifdef __GNU_LIBRARY__
718 f_print (fout, "\t(void) signal(sig, %s closedown);\n",
719 Cflag ? "(SIG_PF)" : "(void(*)())");
720 #endif
721 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
722 f_print (fout, "\t\textern fd_set svc_fdset;\n");
723 f_print (fout, "\t\tstatic int size;\n");
724 f_print (fout, "\t\tint i, openfd;\n");
725 if (tirpcflag && pmflag)
727 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
728 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
730 else
732 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
734 f_print (fout, "\t\t\texit(0);\n");
735 f_print (fout, "\t\tif (size == 0) {\n");
736 if (tirpcflag)
738 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
739 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
740 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
741 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
742 f_print (fout, "\t\t\t\treturn;\n");
744 else
746 f_print (fout, "\t\t\tsize = getdtablesize();\n");
748 f_print (fout, "\t\t}\n");
749 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
750 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
751 f_print (fout, "\t\t\t\topenfd++;\n");
752 f_print (fout, "\t\tif (openfd <= 1)\n");
753 f_print (fout, "\t\t\texit(0);\n");
754 f_print (fout, "\t}\n");
755 f_print (fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
756 f_print (fout, "}\n");
760 * Write the most of port monitor support
762 static void
763 write_pm_most (const char *infile, int netflag)
765 list *l;
766 definition *def;
767 version_list *vp;
769 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
770 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
771 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
772 f_print (fout, "\t\tchar *netid;\n");
773 if (!netflag)
774 { /* Not included by -n option */
775 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
776 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
778 if (timerflag)
779 f_print (fout, "\t\tint pmclose;\n");
780 /* not necessary, defined in /usr/include/stdlib */
781 /* f_print(fout, "\t\textern char *getenv();\n"); */
782 f_print (fout, "\n");
783 f_print (fout, "\t\t_rpcpmstart = 1;\n");
784 if (logflag)
785 open_log_file (infile, "\t\t");
786 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
787 sprintf (_errbuf, "cannot get transport name");
788 print_err_message ("\t\t\t");
789 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
790 sprintf (_errbuf, "cannot get transport info");
791 print_err_message ("\t\t\t");
792 f_print (fout, "\t\t}\n");
794 * A kludgy support for inetd services. Inetd only works with
795 * sockmod, and RPC works only with timod, hence all this jugglery
797 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
798 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
799 sprintf (_errbuf, "could not get the right module");
800 print_err_message ("\t\t\t\t");
801 f_print (fout, "\t\t\t\texit(1);\n");
802 f_print (fout, "\t\t\t}\n");
803 f_print (fout, "\t\t}\n");
804 if (timerflag)
805 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
806 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
807 TRANSP);
808 sprintf (_errbuf, "cannot create server handle");
809 print_err_message ("\t\t\t");
810 f_print (fout, "\t\t\texit(1);\n");
811 f_print (fout, "\t\t}\n");
812 f_print (fout, "\t\tif (nconf)\n");
813 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
814 for (l = defined; l != NULL; l = l->next)
816 def = (definition *) l->val;
817 if (def->def_kind != DEF_PROGRAM)
819 continue;
821 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
823 f_print (fout,
824 "\t\tif (!svc_reg(%s, %s, %s, ",
825 TRANSP, def->def_name, vp->vers_name);
826 pvname (def->def_name, vp->vers_num);
827 f_print (fout, ", 0)) {\n");
828 (void) sprintf (_errbuf, "unable to register (%s, %s).",
829 def->def_name, vp->vers_name);
830 print_err_message ("\t\t\t");
831 f_print (fout, "\t\t\texit(1);\n");
832 f_print (fout, "\t\t}\n");
835 if (timerflag)
837 f_print (fout, "\t\tif (pmclose) {\n");
838 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
839 Cflag ? "(SIG_PF)" : "(void(*)())");
840 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
841 f_print (fout, "\t\t}\n");
843 f_print (fout, "\t\tsvc_run();\n");
844 f_print (fout, "\t\texit(1);\n");
845 f_print (fout, "\t\t/* NOTREACHED */\n");
846 f_print (fout, "\t}\n");
850 * Support for backgrounding the server if self started.
852 static void
853 write_rpc_svc_fg (const char *infile, const char *sp)
855 f_print (fout, "#ifndef RPC_SVC_FG\n");
856 f_print (fout, "%sint size;\n", sp);
857 if (tirpcflag)
858 f_print (fout, "%sstruct rlimit rl;\n", sp);
859 if (inetdflag)
860 f_print (fout, "%sint pid, i;\n\n", sp);
861 f_print (fout, "%spid = fork();\n", sp);
862 f_print (fout, "%sif (pid < 0) {\n", sp);
863 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
864 f_print (fout, "%s\texit(1);\n", sp);
865 f_print (fout, "%s}\n", sp);
866 f_print (fout, "%sif (pid)\n", sp);
867 f_print (fout, "%s\texit(0);\n", sp);
868 /* get number of file descriptors */
869 if (tirpcflag)
871 f_print (fout, "%srl.rlim_max = 0;\n", sp);
872 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
873 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
874 f_print (fout, "%s\texit(1);\n", sp);
876 else
878 f_print (fout, "%ssize = getdtablesize();\n", sp);
881 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
882 f_print (fout, "%s\t(void) close(i);\n", sp);
883 /* Redirect stderr and stdout to console */
884 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
885 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
886 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
887 /* This removes control of the controlling terminal */
888 if (tirpcflag)
889 f_print (fout, "%ssetsid();\n", sp);
890 else
892 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
893 f_print (fout, "%sif (i >= 0) {\n", sp);
894 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
895 f_print (fout, "%s\t(void) close(i);\n", sp);
896 f_print (fout, "%s}\n", sp);
898 if (!logflag)
899 open_log_file (infile, sp);
900 f_print (fout, "#endif\n");
901 if (logflag)
902 open_log_file (infile, sp);
905 static void
906 open_log_file (const char *infile, const char *sp)
908 char *s;
910 s = strrchr (infile, '.');
911 if (s)
912 *s = '\0';
913 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
914 if (s)
915 *s = '.';
922 * write a registration for the given transport for Inetd
924 void
925 write_inetd_register (const char *transp)
927 list *l;
928 definition *def;
929 version_list *vp;
930 const char *sp;
931 int isudp;
932 char tmpbuf[32];
934 if (inetdflag)
935 sp = "\t";
936 else
937 sp = "";
938 if (streq (transp, "udp"))
939 isudp = 1;
940 else
941 isudp = 0;
942 f_print (fout, "\n");
943 if (inetdflag)
945 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
946 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
948 f_print (fout, "%s\t%s = svc%s_create(%s",
949 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
950 if (!isudp)
951 f_print (fout, ", 0, 0");
952 f_print (fout, ");\n");
953 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
954 (void) sprintf (_errbuf, "cannot create %s service.", transp);
955 (void) sprintf (tmpbuf, "%s\t\t", sp);
956 print_err_message (tmpbuf);
957 f_print (fout, "%s\t\texit(1);\n", sp);
958 f_print (fout, "%s\t}\n", sp);
960 if (inetdflag)
962 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
963 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
964 sp, isudp ? "UDP" : "TCP");
966 for (l = defined; l != NULL; l = l->next)
968 def = (definition *) l->val;
969 if (def->def_kind != DEF_PROGRAM)
971 continue;
973 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
975 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
976 sp, TRANSP, def->def_name, vp->vers_name);
977 pvname (def->def_name, vp->vers_num);
978 if (inetdflag)
979 f_print (fout, ", proto)) {\n");
980 else
981 f_print (fout, ", IPPROTO_%s)) {\n",
982 isudp ? "UDP" : "TCP");
983 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
984 def->def_name, vp->vers_name, transp);
985 print_err_message (tmpbuf);
986 f_print (fout, "%s\t\texit(1);\n", sp);
987 f_print (fout, "%s\t}\n", sp);
990 if (inetdflag)
991 f_print (fout, "\t}\n");