[BZ #1978]
[glibc.git] / sunrpc / rpc_svcout.c
blob6774cc8c0fa50bba53cf8195d7e30919eabbbcad
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
36 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
38 #include <stdio.h>
39 #include <string.h>
40 #include "rpc_parse.h"
41 #include "rpc_util.h"
42 #include "proto.h"
44 static const char RQSTP[] = "rqstp";
45 static const char TRANSP[] = "transp";
46 static const char ARG[] = "argument";
47 static const char RESULT[] = "result";
48 static const char ROUTINE[] = "local";
49 static char RETVAL[] = "retval";
51 char _errbuf[256]; /* For all messages */
53 static void internal_proctype (const proc_list * plist);
54 static void p_xdrfunc (const char *rname, const char *typename);
55 static void write_real_program (const definition * def);
56 static void write_program (const definition * def, const char *storage);
57 static void printerr (const char *err, const char *transp);
58 static void printif (const char *proc, const char *transp, const char *arg);
59 static void write_inetmost (const char *infile);
60 static void print_return (const char *space);
61 static void print_pmapunset (const char *space);
62 static void print_err_message (const char *space);
63 static void write_timeout_func (void);
64 static void write_pm_most (const char *infile, int netflag);
65 static void write_rpc_svc_fg (const char *infile, const char *sp);
66 static void open_log_file (const char *infile, const char *sp);
68 static void
69 p_xdrfunc (const char *rname, const char *typename)
71 if (Cflag)
72 f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
73 stringfix (typename));
74 else
75 f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
78 void
79 internal_proctype (const proc_list * plist)
81 f_print (fout, "static ");
82 ptype (plist->res_prefix, plist->res_type, 1);
83 f_print (fout, "*");
88 * write most of the service, that is, everything but the registrations.
90 void
91 write_most (const char *infile /* our name */ , int netflag, int nomain)
93 if (inetdflag || pmflag)
95 const char *var_type;
96 #ifdef __GNU_LIBRARY__
97 /* WHY? */
98 var_type = (nomain ? "extern" : "");
99 #else
100 var_type = (nomain ? "extern" : "static");
101 #endif
102 f_print (fout, "%s int _rpcpmstart;", var_type);
103 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
104 if (!tirpcflag)
106 f_print (fout, "%s int _rpcfdtype;", var_type);
107 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
109 if (timerflag)
111 #if 0
112 f_print (fout, "%s int _rpcsvcdirty;", var_type);
113 f_print (fout, "\t/* Still serving ? */\n");
114 #else
115 f_print(fout, " /* States a server can be in wrt request */\n\n");
116 f_print(fout, "#define\t_IDLE 0\n");
117 f_print(fout, "#define\t_SERVED 1\n");
118 f_print(fout, "#define\t_SERVING 2\n\n");
119 f_print(fout, "static int _rpcsvcstate = _IDLE;");
120 f_print(fout, "\t /* Set when a request is serviced */\n");
122 if (mtflag)
124 f_print (fout, "mutex_t _svcstate_lock;");
125 f_print (fout,
126 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
128 #endif
130 write_svc_aux (nomain);
132 /* write out dispatcher and stubs */
133 write_programs (nomain ? NULL : "static");
135 if (nomain)
136 return;
138 #ifdef __GNU_LIBRARY__
139 if (Cflag)
140 f_print (fout, "\nint\nmain (int argc, char **argv)\n");
141 else
143 f_print (fout, "\nint\nmain (argc, argv)\n");
144 f_print (fout, "\tint argc;\n");
145 f_print (fout, "\tchar **argv;\n");
147 #else
148 f_print (fout, "\nmain()\n");
149 #endif
150 f_print (fout, "{\n");
151 if (inetdflag)
153 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
155 else
157 if (tirpcflag)
159 if (netflag)
161 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
162 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
164 f_print (fout, "\tpid_t pid;\n");
165 f_print (fout, "\tint i;\n");
166 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
168 if (mtflag & timerflag)
169 f_print (fout,
170 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
172 write_pm_most (infile, netflag);
173 f_print (fout, "\telse {\n");
174 write_rpc_svc_fg (infile, "\t\t");
175 f_print (fout, "\t}\n");
177 else
179 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
180 f_print (fout, "\n");
181 print_pmapunset ("\t");
185 if (logflag && !inetdflag)
187 open_log_file (infile, "\t");
192 * write a registration for the given transport
194 void
195 write_netid_register (const char *transp)
197 list *l;
198 definition *def;
199 version_list *vp;
200 const char *sp;
201 char tmpbuf[32];
203 sp = "";
204 f_print (fout, "\n");
205 f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
206 f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
207 (void) sprintf (_errbuf, "cannot find %s netid.", transp);
208 sprintf (tmpbuf, "%s\t\t", sp);
209 print_err_message (tmpbuf);
210 f_print (fout, "%s\t\texit (1);\n", sp);
211 f_print (fout, "%s\t}\n", sp);
212 f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
213 sp, TRANSP /*, transp *//* ?!?... */ );
214 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
215 sprintf (_errbuf, "cannot create %s service.", transp);
216 print_err_message (tmpbuf);
217 f_print (fout, "%s\t\texit (1);\n", sp);
218 f_print (fout, "%s\t}\n", sp);
220 for (l = defined; l != NULL; l = l->next)
222 def = (definition *) l->val;
223 if (def->def_kind != DEF_PROGRAM)
225 continue;
227 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
229 f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
230 sp, def->def_name, vp->vers_name);
231 f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
232 sp, TRANSP, def->def_name, vp->vers_name);
233 pvname (def->def_name, vp->vers_num);
234 f_print (fout, ", nconf)) {\n");
235 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
236 def->def_name, vp->vers_name, transp);
237 print_err_message (tmpbuf);
238 f_print (fout, "%s\t\texit (1);\n", sp);
239 f_print (fout, "%s\t}\n", sp);
242 f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
246 * write a registration for the given transport for TLI
248 void
249 write_nettype_register (const char *transp)
251 list *l;
252 definition *def;
253 version_list *vp;
255 for (l = defined; l != NULL; l = l->next)
257 def = (definition *) l->val;
258 if (def->def_kind != DEF_PROGRAM)
260 continue;
262 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
264 f_print (fout, "\tif (!svc_create (");
265 pvname (def->def_name, vp->vers_num);
266 f_print (fout, ", %s, %s, \"%s\")) {\n ",
267 def->def_name, vp->vers_name, transp);
268 (void) sprintf (_errbuf,
269 "unable to create (%s, %s) for %s.",
270 def->def_name, vp->vers_name, transp);
271 print_err_message ("\t\t");
272 f_print (fout, "\t\texit (1);\n");
273 f_print (fout, "\t}\n");
279 * write the rest of the service
281 void
282 write_rest (void)
284 f_print (fout, "\n");
285 if (inetdflag)
287 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
288 (void) sprintf (_errbuf, "could not create a handle");
289 print_err_message ("\t\t");
290 f_print (fout, "\t\texit (1);\n");
291 f_print (fout, "\t}\n");
292 if (timerflag)
294 f_print (fout, "\tif (_rpcpmstart) {\n");
295 f_print (fout,
296 "\t\t(void) signal (SIGALRM, %s closedown);\n",
297 Cflag ? "(SIG_PF)" : "(void(*)())");
298 f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
299 f_print (fout, "\t}\n");
302 f_print (fout, "\tsvc_run ();\n");
303 (void) sprintf (_errbuf, "svc_run returned");
304 print_err_message ("\t");
305 f_print (fout, "\texit (1);\n");
306 f_print (fout, "\t/* NOTREACHED */\n");
307 f_print (fout, "}\n");
310 void
311 write_programs (const char *storage)
313 list *l;
314 definition *def;
316 /* write out stubs for procedure definitions */
317 for (l = defined; l != NULL; l = l->next)
319 def = (definition *) l->val;
320 if (def->def_kind == DEF_PROGRAM)
322 write_real_program (def);
326 /* write out dispatcher for each program */
327 for (l = defined; l != NULL; l = l->next)
329 def = (definition *) l->val;
330 if (def->def_kind == DEF_PROGRAM)
332 write_program (def, storage);
337 /* write out definition of internal function (e.g. _printmsg_1(...))
338 which calls server's defintion of actual function (e.g. printmsg_1(...)).
339 Unpacks single user argument of printmsg_1 to call-by-value format
340 expected by printmsg_1. */
341 static void
342 write_real_program (const definition * def)
344 version_list *vp;
345 proc_list *proc;
346 decl_list *l;
348 if (!newstyle)
349 return; /* not needed for old style */
350 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
352 for (proc = vp->procs; proc != NULL; proc = proc->next)
354 fprintf (fout, "\n");
355 if (!mtflag)
356 internal_proctype (proc);
357 else
358 f_print (fout, "int");
359 f_print (fout, "\n_");
360 pvname (proc->proc_name, vp->vers_num);
361 if (Cflag)
363 f_print (fout, " (");
364 /* arg name */
365 if (proc->arg_num > 1)
366 f_print (fout, proc->args.argname);
367 else
368 ptype (proc->args.decls->decl.prefix,
369 proc->args.decls->decl.type, 0);
370 if (mtflag)
372 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
373 RESULT, RQSTP);
375 else
376 f_print (fout, " *argp, struct svc_req *%s)\n",
377 RQSTP);
379 else
381 if (mtflag)
382 f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
383 else
384 f_print (fout, " (argp, %s)\n", RQSTP);
385 /* arg name */
386 if (proc->arg_num > 1)
387 f_print (fout, "\t%s *argp;\n", proc->args.argname);
388 else
390 f_print (fout, "\t");
391 ptype (proc->args.decls->decl.prefix,
392 proc->args.decls->decl.type, 0);
393 f_print (fout, " *argp;\n");
395 f_print (fout, " struct svc_req *%s;\n", RQSTP);
398 f_print (fout, "{\n");
399 f_print (fout, "\treturn (");
400 if (Cflag || mtflag)
401 pvname_svc (proc->proc_name, vp->vers_num);
402 else
403 pvname (proc->proc_name, vp->vers_num);
404 f_print (fout, "(");
405 if (proc->arg_num < 2)
406 { /* single argument */
407 if (!streq (proc->args.decls->decl.type, "void"))
408 f_print (fout, "*argp, "); /* non-void */
410 else
412 for (l = proc->args.decls; l != NULL; l = l->next)
413 f_print (fout, "argp->%s, ", l->decl.name);
415 if (mtflag)
416 f_print (fout, "%s, ", RESULT);
417 f_print (fout, "%s));\n}\n", RQSTP);
422 static void
423 write_program (const definition * def, const char *storage)
425 version_list *vp;
426 proc_list *proc;
427 int filled;
429 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
431 f_print (fout, "\n");
432 if (storage != NULL)
434 f_print (fout, "%s ", storage);
436 f_print (fout, "void\n");
437 pvname (def->def_name, vp->vers_num);
439 if (Cflag)
441 f_print (fout, "(struct svc_req *%s, ", RQSTP);
442 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
444 else
446 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
447 f_print (fout, " struct svc_req *%s;\n", RQSTP);
448 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
451 f_print (fout, "{\n");
453 filled = 0;
454 f_print (fout, "\tunion {\n");
455 for (proc = vp->procs; proc != NULL; proc = proc->next)
457 if (proc->arg_num < 2)
458 { /* single argument */
459 if (streq (proc->args.decls->decl.type,
460 "void"))
462 continue;
464 filled = 1;
465 f_print (fout, "\t\t");
466 ptype (proc->args.decls->decl.prefix,
467 proc->args.decls->decl.type, 0);
468 pvname (proc->proc_name, vp->vers_num);
469 f_print (fout, "_arg;\n");
472 else
474 filled = 1;
475 f_print (fout, "\t\t%s", proc->args.argname);
476 f_print (fout, " ");
477 pvname (proc->proc_name, vp->vers_num);
478 f_print (fout, "_arg;\n");
481 if (!filled)
483 f_print (fout, "\t\tint fill;\n");
485 f_print (fout, "\t} %s;\n", ARG);
486 if (mtflag)
488 f_print(fout, "\tunion {\n");
489 for (proc = vp->procs; proc != NULL; proc = proc->next)
490 if (!streq (proc->res_type, "void"))
492 f_print(fout, "\t\t");
493 ptype(proc->res_prefix, proc->res_type, 0);
494 pvname(proc->proc_name, vp->vers_num);
495 f_print(fout, "_res;\n");
497 f_print(fout, "\t} %s;\n", RESULT);
498 f_print(fout, "\tbool_t %s;\n", RETVAL);
500 } else
501 f_print (fout, "\tchar *%s;\n", RESULT);
503 if (Cflag)
505 f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
506 if (mtflag)
507 f_print(fout,
508 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
509 ROUTINE);
510 else
511 f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
512 ROUTINE);
514 else
516 f_print (fout, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG, RESULT);
517 if (mtflag)
518 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
519 else
520 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
522 f_print (fout, "\n");
524 if (timerflag)
525 #if 0
526 f_print (fout, "\t_rpcsvcdirty = 1;\n");
527 #else
529 if (mtflag)
530 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
531 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
532 if (mtflag)
533 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
535 #endif
537 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
538 if (!nullproc (vp->procs))
540 f_print (fout, "\tcase NULLPROC:\n");
541 f_print (fout,
542 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
543 TRANSP);
544 print_return ("\t\t");
545 f_print (fout, "\n");
547 for (proc = vp->procs; proc != NULL; proc = proc->next)
549 f_print (fout, "\tcase %s:\n", proc->proc_name);
550 if (proc->arg_num < 2)
551 { /* single argument */
552 p_xdrfunc (ARG, proc->args.decls->decl.type);
554 else
556 p_xdrfunc (ARG, proc->args.argname);
558 p_xdrfunc (RESULT, proc->res_type);
559 if (Cflag)
561 if (mtflag)
562 f_print(fout,
563 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
564 ROUTINE);
565 else
566 f_print (fout,
567 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
568 ROUTINE);
570 else
571 if (mtflag)
572 f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
573 else
574 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
576 if (newstyle)
577 { /* new style: calls internal routine */
578 f_print (fout, "_");
580 if ((Cflag || mtflag) && !newstyle)
581 pvname_svc (proc->proc_name, vp->vers_num);
582 else
583 pvname (proc->proc_name, vp->vers_num);
584 f_print (fout, ";\n");
585 f_print (fout, "\t\tbreak;\n\n");
587 f_print (fout, "\tdefault:\n");
588 printerr ("noproc", TRANSP);
589 print_return ("\t\t");
590 f_print (fout, "\t}\n");
592 f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
593 printif ("getargs", TRANSP, ARG);
594 printerr ("decode", TRANSP);
595 print_return ("\t\t");
596 f_print (fout, "\t}\n");
598 if (!mtflag)
600 if (Cflag)
601 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
602 RESULT, ROUTINE, ARG, RQSTP);
603 else
604 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
605 RESULT, ROUTINE, ARG, RQSTP);
607 else
608 if (Cflag)
609 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
610 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
611 else
612 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
613 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
614 if (mtflag)
615 f_print(fout,
616 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
617 RETVAL, TRANSP, RESULT, RESULT);
618 else
619 f_print(fout,
620 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
621 RESULT, TRANSP, RESULT, RESULT);
623 printerr ("systemerr", TRANSP);
624 f_print (fout, "\t}\n");
626 printif ("freeargs", TRANSP, ARG);
628 sprintf (_errbuf, "unable to free arguments");
629 print_err_message ("\t\t");
630 f_print (fout, "\t\texit (1);\n");
631 f_print (fout, "\t}\n");
632 /* print out free routine */
633 if (mtflag)
635 f_print(fout,"\tif (!");
636 pvname(def->def_name, vp->vers_num);
637 f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
638 TRANSP, RESULT, RESULT);
639 (void) sprintf(_errbuf, "unable to free results");
640 print_err_message("\t\t");
641 f_print(fout, "\n");
643 print_return ("\t");
644 f_print (fout, "}\n");
648 static void
649 printerr (const char *err, const char *transp)
651 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
654 static void
655 printif (const char *proc, const char *transp, const char *arg)
657 f_print (fout, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
658 proc, transp, arg, arg);
662 nullproc (const proc_list * proc)
664 for (; proc != NULL; proc = proc->next)
666 if (streq (proc->proc_num, "0"))
668 return 1;
671 return 0;
674 static void
675 write_inetmost (const char *infile)
677 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
678 f_print (fout, "\tint sock;\n");
679 f_print (fout, "\tint proto;\n");
680 f_print (fout, "\tstruct sockaddr_in saddr;\n");
681 f_print (fout, "\tint asize = sizeof (saddr);\n");
682 f_print (fout, "\n");
683 f_print (fout,
684 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
685 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
686 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
687 f_print (fout, "\t\t\texit (1);\n");
688 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
689 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
690 f_print (fout, "\t\t\texit (1);\n");
691 f_print (fout, "\t\tsock = 0;\n");
692 f_print (fout, "\t\t_rpcpmstart = 1;\n");
693 f_print (fout, "\t\tproto = 0;\n");
694 open_log_file (infile, "\t\t");
695 f_print (fout, "\t} else {\n");
696 write_rpc_svc_fg (infile, "\t\t");
697 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
698 print_pmapunset ("\t\t");
699 f_print (fout, "\t}\n");
702 static void
703 print_return (const char *space)
705 if (exitnow)
706 f_print (fout, "%sexit (0);\n", space);
707 else
709 if (timerflag)
711 #if 0
712 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
713 #else
714 if (mtflag)
715 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
716 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
717 if (mtflag)
718 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
719 #endif
721 f_print (fout, "%sreturn;\n", space);
725 static void
726 print_pmapunset (const char *space)
728 list *l;
729 definition *def;
730 version_list *vp;
732 for (l = defined; l != NULL; l = l->next)
734 def = (definition *) l->val;
735 if (def->def_kind == DEF_PROGRAM)
737 for (vp = def->def.pr.versions; vp != NULL;
738 vp = vp->next)
740 f_print (fout, "%spmap_unset (%s, %s);\n",
741 space, def->def_name, vp->vers_name);
747 static void
748 print_err_message (const char *space)
750 if (logflag)
751 f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
752 else if (inetdflag || pmflag)
753 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
754 else
755 f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
759 * Write the server auxiliary function ( _msgout, timeout)
761 void
762 write_svc_aux (int nomain)
764 if (!logflag)
765 write_msg_out ();
766 if (!nomain)
767 write_timeout_func ();
771 * Write the _msgout function
774 void
775 write_msg_out (void)
777 f_print (fout, "\n");
778 f_print (fout, "static\n");
779 if (!Cflag)
781 f_print (fout, "void _msgout (msg)\n");
782 f_print (fout, "\tchar *msg;\n");
784 else
786 f_print (fout, "void _msgout (char* msg)\n");
788 f_print (fout, "{\n");
789 f_print (fout, "#ifdef RPC_SVC_FG\n");
790 if (inetdflag || pmflag)
791 f_print (fout, "\tif (_rpcpmstart)\n");
792 f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
793 f_print (fout, "\telse\n");
794 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
795 f_print (fout, "#else\n");
796 f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
797 f_print (fout, "#endif\n");
798 f_print (fout, "}\n");
802 * Write the timeout function
804 static void
805 write_timeout_func (void)
807 if (!timerflag)
808 return;
809 f_print (fout, "\n");
810 f_print (fout, "static void\n");
811 if (Cflag)
812 f_print (fout, "closedown (int sig)\n");
813 else
814 f_print (fout, "closedown (sig)\n\tint sig;\n");
815 f_print (fout, "{\n");
817 #if defined (__GNU_LIBRARY__) && 0
818 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
819 Cflag ? "(SIG_PF)" : "(void(*)())");
820 #endif
821 if (mtflag)
822 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
823 #if 0
824 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
825 #else
826 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
827 #endif
828 f_print (fout, "\t\textern fd_set svc_fdset;\n");
829 f_print (fout, "\t\tstatic int size;\n");
830 f_print (fout, "\t\tint i, openfd;\n");
831 if (tirpcflag && pmflag)
833 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
834 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
836 else
838 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
840 f_print (fout, "\t\t\texit (0);\n");
841 f_print (fout, "\t\tif (size == 0) {\n");
842 if (tirpcflag)
844 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
845 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
846 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
847 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
848 if (mtflag)
849 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
850 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
852 else
854 f_print (fout, "\t\t\tsize = getdtablesize();\n");
856 f_print (fout, "\t\t}\n");
857 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
858 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
859 f_print (fout, "\t\t\t\topenfd++;\n");
860 f_print (fout, "\t\tif (openfd <= 1)\n");
861 f_print (fout, "\t\t\texit (0);\n");
862 f_print (fout, "\t}\n");
863 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
864 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
865 if (mtflag)
866 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
867 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
868 Cflag? "(SIG_PF)" : "(void(*)())");
869 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
870 f_print (fout, "}\n");
874 * Write the most of port monitor support
876 static void
877 write_pm_most (const char *infile, int netflag)
879 list *l;
880 definition *def;
881 version_list *vp;
883 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
884 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
885 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
886 f_print (fout, "\t\tchar *netid;\n");
887 if (!netflag)
888 { /* Not included by -n option */
889 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
890 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
892 if (timerflag)
893 f_print (fout, "\t\tint pmclose;\n");
894 /* not necessary, defined in /usr/include/stdlib */
895 /* f_print(fout, "\t\textern char *getenv();\n"); */
896 f_print (fout, "\n");
897 f_print (fout, "\t\t_rpcpmstart = 1;\n");
898 if (logflag)
899 open_log_file (infile, "\t\t");
900 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
901 sprintf (_errbuf, "cannot get transport name");
902 print_err_message ("\t\t\t");
903 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
904 sprintf (_errbuf, "cannot get transport info");
905 print_err_message ("\t\t\t");
906 f_print (fout, "\t\t}\n");
908 * A kludgy support for inetd services. Inetd only works with
909 * sockmod, and RPC works only with timod, hence all this jugglery
911 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
912 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
913 sprintf (_errbuf, "could not get the right module");
914 print_err_message ("\t\t\t\t");
915 f_print (fout, "\t\t\t\texit(1);\n");
916 f_print (fout, "\t\t\t}\n");
917 f_print (fout, "\t\t}\n");
918 if (timerflag)
919 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
920 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
921 TRANSP);
922 sprintf (_errbuf, "cannot create server handle");
923 print_err_message ("\t\t\t");
924 f_print (fout, "\t\t\texit(1);\n");
925 f_print (fout, "\t\t}\n");
926 f_print (fout, "\t\tif (nconf)\n");
927 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
928 for (l = defined; l != NULL; l = l->next)
930 def = (definition *) l->val;
931 if (def->def_kind != DEF_PROGRAM)
933 continue;
935 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
937 f_print (fout,
938 "\t\tif (!svc_reg(%s, %s, %s, ",
939 TRANSP, def->def_name, vp->vers_name);
940 pvname (def->def_name, vp->vers_num);
941 f_print (fout, ", 0)) {\n");
942 (void) sprintf (_errbuf, "unable to register (%s, %s).",
943 def->def_name, vp->vers_name);
944 print_err_message ("\t\t\t");
945 f_print (fout, "\t\t\texit(1);\n");
946 f_print (fout, "\t\t}\n");
949 if (timerflag)
951 f_print (fout, "\t\tif (pmclose) {\n");
952 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
953 Cflag ? "(SIG_PF)" : "(void(*)())");
954 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
955 f_print (fout, "\t\t}\n");
957 f_print (fout, "\t\tsvc_run();\n");
958 f_print (fout, "\t\texit(1);\n");
959 f_print (fout, "\t\t/* NOTREACHED */\n");
960 f_print (fout, "\t}\n");
964 * Support for backgrounding the server if self started.
966 static void
967 write_rpc_svc_fg (const char *infile, const char *sp)
969 f_print (fout, "#ifndef RPC_SVC_FG\n");
970 f_print (fout, "%sint size;\n", sp);
971 if (tirpcflag)
972 f_print (fout, "%sstruct rlimit rl;\n", sp);
973 if (inetdflag)
974 f_print (fout, "%sint pid, i;\n\n", sp);
975 f_print (fout, "%spid = fork();\n", sp);
976 f_print (fout, "%sif (pid < 0) {\n", sp);
977 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
978 f_print (fout, "%s\texit(1);\n", sp);
979 f_print (fout, "%s}\n", sp);
980 f_print (fout, "%sif (pid)\n", sp);
981 f_print (fout, "%s\texit(0);\n", sp);
982 /* get number of file descriptors */
983 if (tirpcflag)
985 f_print (fout, "%srl.rlim_max = 0;\n", sp);
986 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
987 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
988 f_print (fout, "%s\texit(1);\n", sp);
990 else
992 f_print (fout, "%ssize = getdtablesize();\n", sp);
995 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
996 f_print (fout, "%s\t(void) close(i);\n", sp);
997 /* Redirect stderr and stdout to console */
998 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
999 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
1000 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
1001 /* This removes control of the controlling terminal */
1002 if (tirpcflag)
1003 f_print (fout, "%ssetsid();\n", sp);
1004 else
1006 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1007 f_print (fout, "%sif (i >= 0) {\n", sp);
1008 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
1009 f_print (fout, "%s\t(void) close(i);\n", sp);
1010 f_print (fout, "%s}\n", sp);
1012 if (!logflag)
1013 open_log_file (infile, sp);
1014 f_print (fout, "#endif\n");
1015 if (logflag)
1016 open_log_file (infile, sp);
1019 static void
1020 open_log_file (const char *infile, const char *sp)
1022 char *s;
1024 s = strrchr (infile, '.');
1025 if (s)
1026 *s = '\0';
1027 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1028 if (s)
1029 *s = '.';
1033 * write a registration for the given transport for Inetd
1035 void
1036 write_inetd_register (const char *transp)
1038 list *l;
1039 definition *def;
1040 version_list *vp;
1041 const char *sp;
1042 int isudp;
1043 char tmpbuf[32];
1045 if (inetdflag)
1046 sp = "\t";
1047 else
1048 sp = "";
1049 if (streq (transp, "udp") || streq (transp, "udp6"))
1050 isudp = 1;
1051 else
1052 isudp = 0;
1053 f_print (fout, "\n");
1054 if (inetdflag)
1056 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1057 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1059 f_print (fout, "%s\t%s = svc%s_create(%s",
1060 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1061 if (!isudp)
1062 f_print (fout, ", 0, 0");
1063 f_print (fout, ");\n");
1064 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1065 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1066 (void) sprintf (tmpbuf, "%s\t\t", sp);
1067 print_err_message (tmpbuf);
1068 f_print (fout, "%s\t\texit(1);\n", sp);
1069 f_print (fout, "%s\t}\n", sp);
1071 if (inetdflag)
1073 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1074 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1075 sp, isudp ? "UDP" : "TCP");
1077 for (l = defined; l != NULL; l = l->next)
1079 def = (definition *) l->val;
1080 if (def->def_kind != DEF_PROGRAM)
1082 continue;
1084 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1086 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1087 sp, TRANSP, def->def_name, vp->vers_name);
1088 pvname (def->def_name, vp->vers_num);
1089 if (inetdflag)
1090 f_print (fout, ", proto)) {\n");
1091 else
1092 f_print (fout, ", IPPROTO_%s)) {\n",
1093 isudp ? "UDP" : "TCP");
1094 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1095 def->def_name, vp->vers_name, transp);
1096 print_err_message (tmpbuf);
1097 f_print (fout, "%s\t\texit(1);\n", sp);
1098 f_print (fout, "%s\t}\n", sp);
1101 if (inetdflag)
1102 f_print (fout, "\t}\n");