AVX audit test failures with gcc 4.6
[glibc.git] / sunrpc / rpc_svcout.c
blob71d9abd97db34d004a239881d264edac23c03d85
1 /*
2 * From: @(#)rpc_svcout.c 1.29 89/03/30
4 * Copyright (c) 2010, Oracle America, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials
14 * provided with the distribution.
15 * * Neither the name of the "Oracle America, Inc." nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
26 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
36 #include <stdio.h>
37 #include <string.h>
38 #include "rpc_parse.h"
39 #include "rpc_util.h"
40 #include "proto.h"
42 static const char RQSTP[] = "rqstp";
43 static const char TRANSP[] = "transp";
44 static const char ARG[] = "argument";
45 static const char RESULT[] = "result";
46 static const char ROUTINE[] = "local";
47 static char RETVAL[] = "retval";
49 char _errbuf[256]; /* For all messages */
51 static void internal_proctype (const proc_list * plist);
52 static void p_xdrfunc (const char *rname, const char *typename);
53 static void write_real_program (const definition * def);
54 static void write_program (const definition * def, const char *storage);
55 static void printerr (const char *err, const char *transp);
56 static void printif (const char *proc, const char *transp, const char *arg);
57 static void write_inetmost (const char *infile);
58 static void print_return (const char *space);
59 static void print_pmapunset (const char *space);
60 static void print_err_message (const char *space);
61 static void write_timeout_func (void);
62 static void write_pm_most (const char *infile, int netflag);
63 static void write_rpc_svc_fg (const char *infile, const char *sp);
64 static void open_log_file (const char *infile, const char *sp);
66 static void
67 p_xdrfunc (const char *rname, const char *typename)
69 if (Cflag)
70 f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
71 stringfix (typename));
72 else
73 f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
76 void
77 internal_proctype (const proc_list * plist)
79 f_print (fout, "static ");
80 ptype (plist->res_prefix, plist->res_type, 1);
81 f_print (fout, "*");
86 * write most of the service, that is, everything but the registrations.
88 void
89 write_most (const char *infile /* our name */ , int netflag, int nomain)
91 if (inetdflag || pmflag)
93 const char *var_type;
94 #ifdef __GNU_LIBRARY__
95 /* WHY? */
96 var_type = (nomain ? "extern" : "");
97 #else
98 var_type = (nomain ? "extern" : "static");
99 #endif
100 f_print (fout, "%s int _rpcpmstart;", var_type);
101 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
102 if (!tirpcflag)
104 f_print (fout, "%s int _rpcfdtype;", var_type);
105 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
107 if (timerflag)
109 #if 0
110 f_print (fout, "%s int _rpcsvcdirty;", var_type);
111 f_print (fout, "\t/* Still serving ? */\n");
112 #else
113 f_print(fout, " /* States a server can be in wrt request */\n\n");
114 f_print(fout, "#define\t_IDLE 0\n");
115 f_print(fout, "#define\t_SERVED 1\n");
116 f_print(fout, "#define\t_SERVING 2\n\n");
117 f_print(fout, "static int _rpcsvcstate = _IDLE;");
118 f_print(fout, "\t /* Set when a request is serviced */\n");
120 if (mtflag)
122 f_print (fout, "mutex_t _svcstate_lock;");
123 f_print (fout,
124 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
126 #endif
128 write_svc_aux (nomain);
130 /* write out dispatcher and stubs */
131 write_programs (nomain ? NULL : "static");
133 if (nomain)
134 return;
136 #ifdef __GNU_LIBRARY__
137 if (Cflag)
138 f_print (fout, "\nint\nmain (int argc, char **argv)\n");
139 else
141 f_print (fout, "\nint\nmain (argc, argv)\n");
142 f_print (fout, "\tint argc;\n");
143 f_print (fout, "\tchar **argv;\n");
145 #else
146 f_print (fout, "\nmain()\n");
147 #endif
148 f_print (fout, "{\n");
149 if (inetdflag)
151 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
153 else
155 if (tirpcflag)
157 if (netflag)
159 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
160 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
162 f_print (fout, "\tpid_t pid;\n");
163 f_print (fout, "\tint i;\n");
164 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
166 if (mtflag & timerflag)
167 f_print (fout,
168 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
170 write_pm_most (infile, netflag);
171 f_print (fout, "\telse {\n");
172 write_rpc_svc_fg (infile, "\t\t");
173 f_print (fout, "\t}\n");
175 else
177 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
178 f_print (fout, "\n");
179 print_pmapunset ("\t");
183 if (logflag && !inetdflag)
185 open_log_file (infile, "\t");
190 * write a registration for the given transport
192 void
193 write_netid_register (const char *transp)
195 list *l;
196 definition *def;
197 version_list *vp;
198 const char *sp;
199 char tmpbuf[32];
201 sp = "";
202 f_print (fout, "\n");
203 f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
204 f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
205 (void) sprintf (_errbuf, "cannot find %s netid.", transp);
206 sprintf (tmpbuf, "%s\t\t", sp);
207 print_err_message (tmpbuf);
208 f_print (fout, "%s\t\texit (1);\n", sp);
209 f_print (fout, "%s\t}\n", sp);
210 f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
211 sp, TRANSP /*, transp *//* ?!?... */ );
212 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
213 sprintf (_errbuf, "cannot create %s service.", transp);
214 print_err_message (tmpbuf);
215 f_print (fout, "%s\t\texit (1);\n", sp);
216 f_print (fout, "%s\t}\n", sp);
218 for (l = defined; l != NULL; l = l->next)
220 def = (definition *) l->val;
221 if (def->def_kind != DEF_PROGRAM)
223 continue;
225 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
227 f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
228 sp, def->def_name, vp->vers_name);
229 f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
230 sp, TRANSP, def->def_name, vp->vers_name);
231 pvname (def->def_name, vp->vers_num);
232 f_print (fout, ", nconf)) {\n");
233 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
234 def->def_name, vp->vers_name, transp);
235 print_err_message (tmpbuf);
236 f_print (fout, "%s\t\texit (1);\n", sp);
237 f_print (fout, "%s\t}\n", sp);
240 f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
244 * write a registration for the given transport for TLI
246 void
247 write_nettype_register (const char *transp)
249 list *l;
250 definition *def;
251 version_list *vp;
253 for (l = defined; l != NULL; l = l->next)
255 def = (definition *) l->val;
256 if (def->def_kind != DEF_PROGRAM)
258 continue;
260 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
262 f_print (fout, "\tif (!svc_create (");
263 pvname (def->def_name, vp->vers_num);
264 f_print (fout, ", %s, %s, \"%s\")) {\n ",
265 def->def_name, vp->vers_name, transp);
266 (void) sprintf (_errbuf,
267 "unable to create (%s, %s) for %s.",
268 def->def_name, vp->vers_name, transp);
269 print_err_message ("\t\t");
270 f_print (fout, "\t\texit (1);\n");
271 f_print (fout, "\t}\n");
277 * write the rest of the service
279 void
280 write_rest (void)
282 f_print (fout, "\n");
283 if (inetdflag)
285 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
286 (void) sprintf (_errbuf, "could not create a handle");
287 print_err_message ("\t\t");
288 f_print (fout, "\t\texit (1);\n");
289 f_print (fout, "\t}\n");
290 if (timerflag)
292 f_print (fout, "\tif (_rpcpmstart) {\n");
293 f_print (fout,
294 "\t\t(void) signal (SIGALRM, %s closedown);\n",
295 Cflag ? "(SIG_PF)" : "(void(*)())");
296 f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
297 f_print (fout, "\t}\n");
300 f_print (fout, "\tsvc_run ();\n");
301 (void) sprintf (_errbuf, "svc_run returned");
302 print_err_message ("\t");
303 f_print (fout, "\texit (1);\n");
304 f_print (fout, "\t/* NOTREACHED */\n");
305 f_print (fout, "}\n");
308 void
309 write_programs (const char *storage)
311 list *l;
312 definition *def;
314 /* write out stubs for procedure definitions */
315 for (l = defined; l != NULL; l = l->next)
317 def = (definition *) l->val;
318 if (def->def_kind == DEF_PROGRAM)
320 write_real_program (def);
324 /* write out dispatcher for each program */
325 for (l = defined; l != NULL; l = l->next)
327 def = (definition *) l->val;
328 if (def->def_kind == DEF_PROGRAM)
330 write_program (def, storage);
335 /* write out definition of internal function (e.g. _printmsg_1(...))
336 which calls server's defintion of actual function (e.g. printmsg_1(...)).
337 Unpacks single user argument of printmsg_1 to call-by-value format
338 expected by printmsg_1. */
339 static void
340 write_real_program (const definition * def)
342 version_list *vp;
343 proc_list *proc;
344 decl_list *l;
346 if (!newstyle)
347 return; /* not needed for old style */
348 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
350 for (proc = vp->procs; proc != NULL; proc = proc->next)
352 fprintf (fout, "\n");
353 if (!mtflag)
354 internal_proctype (proc);
355 else
356 f_print (fout, "int");
357 f_print (fout, "\n_");
358 pvname (proc->proc_name, vp->vers_num);
359 if (Cflag)
361 f_print (fout, " (");
362 /* arg name */
363 if (proc->arg_num > 1)
364 f_print (fout, proc->args.argname);
365 else
366 ptype (proc->args.decls->decl.prefix,
367 proc->args.decls->decl.type, 0);
368 if (mtflag)
370 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
371 RESULT, RQSTP);
373 else
374 f_print (fout, " *argp, struct svc_req *%s)\n",
375 RQSTP);
377 else
379 if (mtflag)
380 f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
381 else
382 f_print (fout, " (argp, %s)\n", RQSTP);
383 /* arg name */
384 if (proc->arg_num > 1)
385 f_print (fout, "\t%s *argp;\n", proc->args.argname);
386 else
388 f_print (fout, "\t");
389 ptype (proc->args.decls->decl.prefix,
390 proc->args.decls->decl.type, 0);
391 f_print (fout, " *argp;\n");
393 f_print (fout, " struct svc_req *%s;\n", RQSTP);
396 f_print (fout, "{\n");
397 f_print (fout, "\treturn (");
398 if (Cflag || mtflag)
399 pvname_svc (proc->proc_name, vp->vers_num);
400 else
401 pvname (proc->proc_name, vp->vers_num);
402 f_print (fout, "(");
403 if (proc->arg_num < 2)
404 { /* single argument */
405 if (!streq (proc->args.decls->decl.type, "void"))
406 f_print (fout, "*argp, "); /* non-void */
408 else
410 for (l = proc->args.decls; l != NULL; l = l->next)
411 f_print (fout, "argp->%s, ", l->decl.name);
413 if (mtflag)
414 f_print (fout, "%s, ", RESULT);
415 f_print (fout, "%s));\n}\n", RQSTP);
420 static void
421 write_program (const definition * def, const char *storage)
423 version_list *vp;
424 proc_list *proc;
425 int filled;
427 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
429 f_print (fout, "\n");
430 if (storage != NULL)
432 f_print (fout, "%s ", storage);
434 f_print (fout, "void\n");
435 pvname (def->def_name, vp->vers_num);
437 if (Cflag)
439 f_print (fout, "(struct svc_req *%s, ", RQSTP);
440 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
442 else
444 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
445 f_print (fout, " struct svc_req *%s;\n", RQSTP);
446 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
449 f_print (fout, "{\n");
451 filled = 0;
452 f_print (fout, "\tunion {\n");
453 for (proc = vp->procs; proc != NULL; proc = proc->next)
455 if (proc->arg_num < 2)
456 { /* single argument */
457 if (streq (proc->args.decls->decl.type,
458 "void"))
460 continue;
462 filled = 1;
463 f_print (fout, "\t\t");
464 ptype (proc->args.decls->decl.prefix,
465 proc->args.decls->decl.type, 0);
466 pvname (proc->proc_name, vp->vers_num);
467 f_print (fout, "_arg;\n");
470 else
472 filled = 1;
473 f_print (fout, "\t\t%s", proc->args.argname);
474 f_print (fout, " ");
475 pvname (proc->proc_name, vp->vers_num);
476 f_print (fout, "_arg;\n");
479 if (!filled)
481 f_print (fout, "\t\tint fill;\n");
483 f_print (fout, "\t} %s;\n", ARG);
484 if (mtflag)
486 f_print(fout, "\tunion {\n");
487 for (proc = vp->procs; proc != NULL; proc = proc->next)
488 if (!streq (proc->res_type, "void"))
490 f_print(fout, "\t\t");
491 ptype(proc->res_prefix, proc->res_type, 0);
492 pvname(proc->proc_name, vp->vers_num);
493 f_print(fout, "_res;\n");
495 f_print(fout, "\t} %s;\n", RESULT);
496 f_print(fout, "\tbool_t %s;\n", RETVAL);
498 } else
499 f_print (fout, "\tchar *%s;\n", RESULT);
501 if (Cflag)
503 f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
504 if (mtflag)
505 f_print(fout,
506 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
507 ROUTINE);
508 else
509 f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
510 ROUTINE);
512 else
514 f_print (fout, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG, RESULT);
515 if (mtflag)
516 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
517 else
518 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
520 f_print (fout, "\n");
522 if (timerflag)
523 #if 0
524 f_print (fout, "\t_rpcsvcdirty = 1;\n");
525 #else
527 if (mtflag)
528 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
529 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
530 if (mtflag)
531 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
533 #endif
535 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
536 if (!nullproc (vp->procs))
538 f_print (fout, "\tcase NULLPROC:\n");
539 f_print (fout,
540 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
541 TRANSP);
542 print_return ("\t\t");
543 f_print (fout, "\n");
545 for (proc = vp->procs; proc != NULL; proc = proc->next)
547 f_print (fout, "\tcase %s:\n", proc->proc_name);
548 if (proc->arg_num < 2)
549 { /* single argument */
550 p_xdrfunc (ARG, proc->args.decls->decl.type);
552 else
554 p_xdrfunc (ARG, proc->args.argname);
556 p_xdrfunc (RESULT, proc->res_type);
557 if (Cflag)
559 if (mtflag)
560 f_print(fout,
561 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
562 ROUTINE);
563 else
564 f_print (fout,
565 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
566 ROUTINE);
568 else
569 if (mtflag)
570 f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
571 else
572 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
574 if (newstyle)
575 { /* new style: calls internal routine */
576 f_print (fout, "_");
578 if ((Cflag || mtflag) && !newstyle)
579 pvname_svc (proc->proc_name, vp->vers_num);
580 else
581 pvname (proc->proc_name, vp->vers_num);
582 f_print (fout, ";\n");
583 f_print (fout, "\t\tbreak;\n\n");
585 f_print (fout, "\tdefault:\n");
586 printerr ("noproc", TRANSP);
587 print_return ("\t\t");
588 f_print (fout, "\t}\n");
590 f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
591 printif ("getargs", TRANSP, ARG);
592 printerr ("decode", TRANSP);
593 print_return ("\t\t");
594 f_print (fout, "\t}\n");
596 if (!mtflag)
598 if (Cflag)
599 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
600 RESULT, ROUTINE, ARG, RQSTP);
601 else
602 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
603 RESULT, ROUTINE, ARG, RQSTP);
605 else
606 if (Cflag)
607 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
608 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
609 else
610 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
611 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
612 if (mtflag)
613 f_print(fout,
614 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
615 RETVAL, TRANSP, RESULT, RESULT);
616 else
617 f_print(fout,
618 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
619 RESULT, TRANSP, RESULT, RESULT);
621 printerr ("systemerr", TRANSP);
622 f_print (fout, "\t}\n");
624 printif ("freeargs", TRANSP, ARG);
626 sprintf (_errbuf, "unable to free arguments");
627 print_err_message ("\t\t");
628 f_print (fout, "\t\texit (1);\n");
629 f_print (fout, "\t}\n");
630 /* print out free routine */
631 if (mtflag)
633 f_print(fout,"\tif (!");
634 pvname(def->def_name, vp->vers_num);
635 f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
636 TRANSP, RESULT, RESULT);
637 (void) sprintf(_errbuf, "unable to free results");
638 print_err_message("\t\t");
639 f_print(fout, "\n");
641 print_return ("\t");
642 f_print (fout, "}\n");
646 static void
647 printerr (const char *err, const char *transp)
649 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
652 static void
653 printif (const char *proc, const char *transp, const char *arg)
655 f_print (fout, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
656 proc, transp, arg, arg);
660 nullproc (const proc_list * proc)
662 for (; proc != NULL; proc = proc->next)
664 if (streq (proc->proc_num, "0"))
666 return 1;
669 return 0;
672 static void
673 write_inetmost (const char *infile)
675 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
676 f_print (fout, "\tint sock;\n");
677 f_print (fout, "\tint proto;\n");
678 f_print (fout, "\tstruct sockaddr_in saddr;\n");
679 f_print (fout, "\tint asize = sizeof (saddr);\n");
680 f_print (fout, "\n");
681 f_print (fout,
682 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
683 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
684 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
685 f_print (fout, "\t\t\texit (1);\n");
686 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
687 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
688 f_print (fout, "\t\t\texit (1);\n");
689 f_print (fout, "\t\tsock = 0;\n");
690 f_print (fout, "\t\t_rpcpmstart = 1;\n");
691 f_print (fout, "\t\tproto = 0;\n");
692 open_log_file (infile, "\t\t");
693 f_print (fout, "\t} else {\n");
694 write_rpc_svc_fg (infile, "\t\t");
695 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
696 print_pmapunset ("\t\t");
697 f_print (fout, "\t}\n");
700 static void
701 print_return (const char *space)
703 if (exitnow)
704 f_print (fout, "%sexit (0);\n", space);
705 else
707 if (timerflag)
709 #if 0
710 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
711 #else
712 if (mtflag)
713 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
714 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
715 if (mtflag)
716 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
717 #endif
719 f_print (fout, "%sreturn;\n", space);
723 static void
724 print_pmapunset (const char *space)
726 list *l;
727 definition *def;
728 version_list *vp;
730 for (l = defined; l != NULL; l = l->next)
732 def = (definition *) l->val;
733 if (def->def_kind == DEF_PROGRAM)
735 for (vp = def->def.pr.versions; vp != NULL;
736 vp = vp->next)
738 f_print (fout, "%spmap_unset (%s, %s);\n",
739 space, def->def_name, vp->vers_name);
745 static void
746 print_err_message (const char *space)
748 if (logflag)
749 f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
750 else if (inetdflag || pmflag)
751 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
752 else
753 f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
757 * Write the server auxiliary function ( _msgout, timeout)
759 void
760 write_svc_aux (int nomain)
762 if (!logflag)
763 write_msg_out ();
764 if (!nomain)
765 write_timeout_func ();
769 * Write the _msgout function
772 void
773 write_msg_out (void)
775 f_print (fout, "\n");
776 f_print (fout, "static\n");
777 if (!Cflag)
779 f_print (fout, "void _msgout (msg)\n");
780 f_print (fout, "\tchar *msg;\n");
782 else
784 f_print (fout, "void _msgout (char* msg)\n");
786 f_print (fout, "{\n");
787 f_print (fout, "#ifdef RPC_SVC_FG\n");
788 if (inetdflag || pmflag)
789 f_print (fout, "\tif (_rpcpmstart)\n");
790 f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
791 f_print (fout, "\telse\n");
792 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
793 f_print (fout, "#else\n");
794 f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
795 f_print (fout, "#endif\n");
796 f_print (fout, "}\n");
800 * Write the timeout function
802 static void
803 write_timeout_func (void)
805 if (!timerflag)
806 return;
807 f_print (fout, "\n");
808 f_print (fout, "static void\n");
809 if (Cflag)
810 f_print (fout, "closedown (int sig)\n");
811 else
812 f_print (fout, "closedown (sig)\n\tint sig;\n");
813 f_print (fout, "{\n");
815 #if defined (__GNU_LIBRARY__) && 0
816 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
817 Cflag ? "(SIG_PF)" : "(void(*)())");
818 #endif
819 if (mtflag)
820 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
821 #if 0
822 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
823 #else
824 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
825 #endif
826 f_print (fout, "\t\textern fd_set svc_fdset;\n");
827 f_print (fout, "\t\tstatic int size;\n");
828 f_print (fout, "\t\tint i, openfd;\n");
829 if (tirpcflag && pmflag)
831 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
832 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
834 else
836 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
838 f_print (fout, "\t\t\texit (0);\n");
839 f_print (fout, "\t\tif (size == 0) {\n");
840 if (tirpcflag)
842 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
843 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
844 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
845 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
846 if (mtflag)
847 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
848 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
850 else
852 f_print (fout, "\t\t\tsize = getdtablesize();\n");
854 f_print (fout, "\t\t}\n");
855 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
856 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
857 f_print (fout, "\t\t\t\topenfd++;\n");
858 f_print (fout, "\t\tif (openfd <= 1)\n");
859 f_print (fout, "\t\t\texit (0);\n");
860 f_print (fout, "\t}\n");
861 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
862 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
863 if (mtflag)
864 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
865 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
866 Cflag? "(SIG_PF)" : "(void(*)())");
867 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
868 f_print (fout, "}\n");
872 * Write the most of port monitor support
874 static void
875 write_pm_most (const char *infile, int netflag)
877 list *l;
878 definition *def;
879 version_list *vp;
881 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
882 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
883 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
884 f_print (fout, "\t\tchar *netid;\n");
885 if (!netflag)
886 { /* Not included by -n option */
887 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
888 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
890 if (timerflag)
891 f_print (fout, "\t\tint pmclose;\n");
892 /* not necessary, defined in /usr/include/stdlib */
893 /* f_print(fout, "\t\textern char *getenv();\n"); */
894 f_print (fout, "\n");
895 f_print (fout, "\t\t_rpcpmstart = 1;\n");
896 if (logflag)
897 open_log_file (infile, "\t\t");
898 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
899 sprintf (_errbuf, "cannot get transport name");
900 print_err_message ("\t\t\t");
901 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
902 sprintf (_errbuf, "cannot get transport info");
903 print_err_message ("\t\t\t");
904 f_print (fout, "\t\t}\n");
906 * A kludgy support for inetd services. Inetd only works with
907 * sockmod, and RPC works only with timod, hence all this jugglery
909 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
910 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
911 sprintf (_errbuf, "could not get the right module");
912 print_err_message ("\t\t\t\t");
913 f_print (fout, "\t\t\t\texit(1);\n");
914 f_print (fout, "\t\t\t}\n");
915 f_print (fout, "\t\t}\n");
916 if (timerflag)
917 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
918 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
919 TRANSP);
920 sprintf (_errbuf, "cannot create server handle");
921 print_err_message ("\t\t\t");
922 f_print (fout, "\t\t\texit(1);\n");
923 f_print (fout, "\t\t}\n");
924 f_print (fout, "\t\tif (nconf)\n");
925 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
926 for (l = defined; l != NULL; l = l->next)
928 def = (definition *) l->val;
929 if (def->def_kind != DEF_PROGRAM)
931 continue;
933 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
935 f_print (fout,
936 "\t\tif (!svc_reg(%s, %s, %s, ",
937 TRANSP, def->def_name, vp->vers_name);
938 pvname (def->def_name, vp->vers_num);
939 f_print (fout, ", 0)) {\n");
940 (void) sprintf (_errbuf, "unable to register (%s, %s).",
941 def->def_name, vp->vers_name);
942 print_err_message ("\t\t\t");
943 f_print (fout, "\t\t\texit(1);\n");
944 f_print (fout, "\t\t}\n");
947 if (timerflag)
949 f_print (fout, "\t\tif (pmclose) {\n");
950 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
951 Cflag ? "(SIG_PF)" : "(void(*)())");
952 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
953 f_print (fout, "\t\t}\n");
955 f_print (fout, "\t\tsvc_run();\n");
956 f_print (fout, "\t\texit(1);\n");
957 f_print (fout, "\t\t/* NOTREACHED */\n");
958 f_print (fout, "\t}\n");
962 * Support for backgrounding the server if self started.
964 static void
965 write_rpc_svc_fg (const char *infile, const char *sp)
967 f_print (fout, "#ifndef RPC_SVC_FG\n");
968 f_print (fout, "%sint size;\n", sp);
969 if (tirpcflag)
970 f_print (fout, "%sstruct rlimit rl;\n", sp);
971 if (inetdflag)
972 f_print (fout, "%sint pid, i;\n\n", sp);
973 f_print (fout, "%spid = fork();\n", sp);
974 f_print (fout, "%sif (pid < 0) {\n", sp);
975 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
976 f_print (fout, "%s\texit(1);\n", sp);
977 f_print (fout, "%s}\n", sp);
978 f_print (fout, "%sif (pid)\n", sp);
979 f_print (fout, "%s\texit(0);\n", sp);
980 /* get number of file descriptors */
981 if (tirpcflag)
983 f_print (fout, "%srl.rlim_max = 0;\n", sp);
984 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
985 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
986 f_print (fout, "%s\texit(1);\n", sp);
988 else
990 f_print (fout, "%ssize = getdtablesize();\n", sp);
993 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
994 f_print (fout, "%s\t(void) close(i);\n", sp);
995 /* Redirect stderr and stdout to console */
996 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
997 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
998 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
999 /* This removes control of the controlling terminal */
1000 if (tirpcflag)
1001 f_print (fout, "%ssetsid();\n", sp);
1002 else
1004 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1005 f_print (fout, "%sif (i >= 0) {\n", sp);
1006 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
1007 f_print (fout, "%s\t(void) close(i);\n", sp);
1008 f_print (fout, "%s}\n", sp);
1010 if (!logflag)
1011 open_log_file (infile, sp);
1012 f_print (fout, "#endif\n");
1013 if (logflag)
1014 open_log_file (infile, sp);
1017 static void
1018 open_log_file (const char *infile, const char *sp)
1020 char *s;
1022 s = strrchr (infile, '.');
1023 if (s)
1024 *s = '\0';
1025 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1026 if (s)
1027 *s = '.';
1031 * write a registration for the given transport for Inetd
1033 void
1034 write_inetd_register (const char *transp)
1036 list *l;
1037 definition *def;
1038 version_list *vp;
1039 const char *sp;
1040 int isudp;
1041 char tmpbuf[32];
1043 if (inetdflag)
1044 sp = "\t";
1045 else
1046 sp = "";
1047 if (streq (transp, "udp") || streq (transp, "udp6"))
1048 isudp = 1;
1049 else
1050 isudp = 0;
1051 f_print (fout, "\n");
1052 if (inetdflag)
1054 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1055 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1057 f_print (fout, "%s\t%s = svc%s_create(%s",
1058 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1059 if (!isudp)
1060 f_print (fout, ", 0, 0");
1061 f_print (fout, ");\n");
1062 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1063 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1064 (void) sprintf (tmpbuf, "%s\t\t", sp);
1065 print_err_message (tmpbuf);
1066 f_print (fout, "%s\t\texit(1);\n", sp);
1067 f_print (fout, "%s\t}\n", sp);
1069 if (inetdflag)
1071 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1072 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1073 sp, isudp ? "UDP" : "TCP");
1075 for (l = defined; l != NULL; l = l->next)
1077 def = (definition *) l->val;
1078 if (def->def_kind != DEF_PROGRAM)
1080 continue;
1082 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1084 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1085 sp, TRANSP, def->def_name, vp->vers_name);
1086 pvname (def->def_name, vp->vers_num);
1087 if (inetdflag)
1088 f_print (fout, ", proto)) {\n");
1089 else
1090 f_print (fout, ", IPPROTO_%s)) {\n",
1091 isudp ? "UDP" : "TCP");
1092 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1093 def->def_name, vp->vers_name, transp);
1094 print_err_message (tmpbuf);
1095 f_print (fout, "%s\t\texit(1);\n", sp);
1096 f_print (fout, "%s\t}\n", sp);
1099 if (inetdflag)
1100 f_print (fout, "\t}\n");