Add the missing "; \".
[glibc.git] / sunrpc / rpc_svcout.c
blob1887b75e429146283e4dc5c37dbc0694ec37b16c
1 /*
2 * From: @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following
12 * disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 * * Neither the name of Sun Microsystems, Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived
16 * from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
25 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
35 #include <stdio.h>
36 #include <string.h>
37 #include "rpc_parse.h"
38 #include "rpc_util.h"
39 #include "proto.h"
41 static const char RQSTP[] = "rqstp";
42 static const char TRANSP[] = "transp";
43 static const char ARG[] = "argument";
44 static const char RESULT[] = "result";
45 static const char ROUTINE[] = "local";
46 static char RETVAL[] = "retval";
48 char _errbuf[256]; /* For all messages */
50 static void internal_proctype (const proc_list * plist);
51 static void p_xdrfunc (const char *rname, const char *typename);
52 static void write_real_program (const definition * def);
53 static void write_program (const definition * def, const char *storage);
54 static void printerr (const char *err, const char *transp);
55 static void printif (const char *proc, const char *transp, const char *arg);
56 static void write_inetmost (const char *infile);
57 static void print_return (const char *space);
58 static void print_pmapunset (const char *space);
59 static void print_err_message (const char *space);
60 static void write_timeout_func (void);
61 static void write_pm_most (const char *infile, int netflag);
62 static void write_rpc_svc_fg (const char *infile, const char *sp);
63 static void open_log_file (const char *infile, const char *sp);
65 static void
66 p_xdrfunc (const char *rname, const char *typename)
68 if (Cflag)
69 f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
70 stringfix (typename));
71 else
72 f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
75 void
76 internal_proctype (const proc_list * plist)
78 f_print (fout, "static ");
79 ptype (plist->res_prefix, plist->res_type, 1);
80 f_print (fout, "*");
85 * write most of the service, that is, everything but the registrations.
87 void
88 write_most (const char *infile /* our name */ , int netflag, int nomain)
90 if (inetdflag || pmflag)
92 const char *var_type;
93 #ifdef __GNU_LIBRARY__
94 /* WHY? */
95 var_type = (nomain ? "extern" : "");
96 #else
97 var_type = (nomain ? "extern" : "static");
98 #endif
99 f_print (fout, "%s int _rpcpmstart;", var_type);
100 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
101 if (!tirpcflag)
103 f_print (fout, "%s int _rpcfdtype;", var_type);
104 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
106 if (timerflag)
108 #if 0
109 f_print (fout, "%s int _rpcsvcdirty;", var_type);
110 f_print (fout, "\t/* Still serving ? */\n");
111 #else
112 f_print(fout, " /* States a server can be in wrt request */\n\n");
113 f_print(fout, "#define\t_IDLE 0\n");
114 f_print(fout, "#define\t_SERVED 1\n");
115 f_print(fout, "#define\t_SERVING 2\n\n");
116 f_print(fout, "static int _rpcsvcstate = _IDLE;");
117 f_print(fout, "\t /* Set when a request is serviced */\n");
119 if (mtflag)
121 f_print (fout, "mutex_t _svcstate_lock;");
122 f_print (fout,
123 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
125 #endif
127 write_svc_aux (nomain);
129 /* write out dispatcher and stubs */
130 write_programs (nomain ? NULL : "static");
132 if (nomain)
133 return;
135 #ifdef __GNU_LIBRARY__
136 if (Cflag)
137 f_print (fout, "\nint\nmain (int argc, char **argv)\n");
138 else
140 f_print (fout, "\nint\nmain (argc, argv)\n");
141 f_print (fout, "\tint argc;\n");
142 f_print (fout, "\tchar **argv;\n");
144 #else
145 f_print (fout, "\nmain()\n");
146 #endif
147 f_print (fout, "{\n");
148 if (inetdflag)
150 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
152 else
154 if (tirpcflag)
156 if (netflag)
158 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
159 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
161 f_print (fout, "\tpid_t pid;\n");
162 f_print (fout, "\tint i;\n");
163 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
165 if (mtflag & timerflag)
166 f_print (fout,
167 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
169 write_pm_most (infile, netflag);
170 f_print (fout, "\telse {\n");
171 write_rpc_svc_fg (infile, "\t\t");
172 f_print (fout, "\t}\n");
174 else
176 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
177 f_print (fout, "\n");
178 print_pmapunset ("\t");
182 if (logflag && !inetdflag)
184 open_log_file (infile, "\t");
189 * write a registration for the given transport
191 void
192 write_netid_register (const char *transp)
194 list *l;
195 definition *def;
196 version_list *vp;
197 const char *sp;
198 char tmpbuf[32];
200 sp = "";
201 f_print (fout, "\n");
202 f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
203 f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
204 (void) sprintf (_errbuf, "cannot find %s netid.", transp);
205 sprintf (tmpbuf, "%s\t\t", sp);
206 print_err_message (tmpbuf);
207 f_print (fout, "%s\t\texit (1);\n", sp);
208 f_print (fout, "%s\t}\n", sp);
209 f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
210 sp, TRANSP /*, transp *//* ?!?... */ );
211 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
212 sprintf (_errbuf, "cannot create %s service.", transp);
213 print_err_message (tmpbuf);
214 f_print (fout, "%s\t\texit (1);\n", sp);
215 f_print (fout, "%s\t}\n", sp);
217 for (l = defined; l != NULL; l = l->next)
219 def = (definition *) l->val;
220 if (def->def_kind != DEF_PROGRAM)
222 continue;
224 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
226 f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
227 sp, def->def_name, vp->vers_name);
228 f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
229 sp, TRANSP, def->def_name, vp->vers_name);
230 pvname (def->def_name, vp->vers_num);
231 f_print (fout, ", nconf)) {\n");
232 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
233 def->def_name, vp->vers_name, transp);
234 print_err_message (tmpbuf);
235 f_print (fout, "%s\t\texit (1);\n", sp);
236 f_print (fout, "%s\t}\n", sp);
239 f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
243 * write a registration for the given transport for TLI
245 void
246 write_nettype_register (const char *transp)
248 list *l;
249 definition *def;
250 version_list *vp;
252 for (l = defined; l != NULL; l = l->next)
254 def = (definition *) l->val;
255 if (def->def_kind != DEF_PROGRAM)
257 continue;
259 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
261 f_print (fout, "\tif (!svc_create (");
262 pvname (def->def_name, vp->vers_num);
263 f_print (fout, ", %s, %s, \"%s\")) {\n ",
264 def->def_name, vp->vers_name, transp);
265 (void) sprintf (_errbuf,
266 "unable to create (%s, %s) for %s.",
267 def->def_name, vp->vers_name, transp);
268 print_err_message ("\t\t");
269 f_print (fout, "\t\texit (1);\n");
270 f_print (fout, "\t}\n");
276 * write the rest of the service
278 void
279 write_rest (void)
281 f_print (fout, "\n");
282 if (inetdflag)
284 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
285 (void) sprintf (_errbuf, "could not create a handle");
286 print_err_message ("\t\t");
287 f_print (fout, "\t\texit (1);\n");
288 f_print (fout, "\t}\n");
289 if (timerflag)
291 f_print (fout, "\tif (_rpcpmstart) {\n");
292 f_print (fout,
293 "\t\t(void) signal (SIGALRM, %s closedown);\n",
294 Cflag ? "(SIG_PF)" : "(void(*)())");
295 f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
296 f_print (fout, "\t}\n");
299 f_print (fout, "\tsvc_run ();\n");
300 (void) sprintf (_errbuf, "svc_run returned");
301 print_err_message ("\t");
302 f_print (fout, "\texit (1);\n");
303 f_print (fout, "\t/* NOTREACHED */\n");
304 f_print (fout, "}\n");
307 void
308 write_programs (const char *storage)
310 list *l;
311 definition *def;
313 /* write out stubs for procedure definitions */
314 for (l = defined; l != NULL; l = l->next)
316 def = (definition *) l->val;
317 if (def->def_kind == DEF_PROGRAM)
319 write_real_program (def);
323 /* write out dispatcher for each program */
324 for (l = defined; l != NULL; l = l->next)
326 def = (definition *) l->val;
327 if (def->def_kind == DEF_PROGRAM)
329 write_program (def, storage);
334 /* write out definition of internal function (e.g. _printmsg_1(...))
335 which calls server's defintion of actual function (e.g. printmsg_1(...)).
336 Unpacks single user argument of printmsg_1 to call-by-value format
337 expected by printmsg_1. */
338 static void
339 write_real_program (const definition * def)
341 version_list *vp;
342 proc_list *proc;
343 decl_list *l;
345 if (!newstyle)
346 return; /* not needed for old style */
347 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
349 for (proc = vp->procs; proc != NULL; proc = proc->next)
351 fprintf (fout, "\n");
352 if (!mtflag)
353 internal_proctype (proc);
354 else
355 f_print (fout, "int");
356 f_print (fout, "\n_");
357 pvname (proc->proc_name, vp->vers_num);
358 if (Cflag)
360 f_print (fout, " (");
361 /* arg name */
362 if (proc->arg_num > 1)
363 f_print (fout, proc->args.argname);
364 else
365 ptype (proc->args.decls->decl.prefix,
366 proc->args.decls->decl.type, 0);
367 if (mtflag)
369 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
370 RESULT, RQSTP);
372 else
373 f_print (fout, " *argp, struct svc_req *%s)\n",
374 RQSTP);
376 else
378 if (mtflag)
379 f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
380 else
381 f_print (fout, " (argp, %s)\n", RQSTP);
382 /* arg name */
383 if (proc->arg_num > 1)
384 f_print (fout, "\t%s *argp;\n", proc->args.argname);
385 else
387 f_print (fout, "\t");
388 ptype (proc->args.decls->decl.prefix,
389 proc->args.decls->decl.type, 0);
390 f_print (fout, " *argp;\n");
392 f_print (fout, " struct svc_req *%s;\n", RQSTP);
395 f_print (fout, "{\n");
396 f_print (fout, "\treturn (");
397 if (Cflag || mtflag)
398 pvname_svc (proc->proc_name, vp->vers_num);
399 else
400 pvname (proc->proc_name, vp->vers_num);
401 f_print (fout, "(");
402 if (proc->arg_num < 2)
403 { /* single argument */
404 if (!streq (proc->args.decls->decl.type, "void"))
405 f_print (fout, "*argp, "); /* non-void */
407 else
409 for (l = proc->args.decls; l != NULL; l = l->next)
410 f_print (fout, "argp->%s, ", l->decl.name);
412 if (mtflag)
413 f_print (fout, "%s, ", RESULT);
414 f_print (fout, "%s));\n}\n", RQSTP);
419 static void
420 write_program (const definition * def, const char *storage)
422 version_list *vp;
423 proc_list *proc;
424 int filled;
426 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
428 f_print (fout, "\n");
429 if (storage != NULL)
431 f_print (fout, "%s ", storage);
433 f_print (fout, "void\n");
434 pvname (def->def_name, vp->vers_num);
436 if (Cflag)
438 f_print (fout, "(struct svc_req *%s, ", RQSTP);
439 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
441 else
443 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
444 f_print (fout, " struct svc_req *%s;\n", RQSTP);
445 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
448 f_print (fout, "{\n");
450 filled = 0;
451 f_print (fout, "\tunion {\n");
452 for (proc = vp->procs; proc != NULL; proc = proc->next)
454 if (proc->arg_num < 2)
455 { /* single argument */
456 if (streq (proc->args.decls->decl.type,
457 "void"))
459 continue;
461 filled = 1;
462 f_print (fout, "\t\t");
463 ptype (proc->args.decls->decl.prefix,
464 proc->args.decls->decl.type, 0);
465 pvname (proc->proc_name, vp->vers_num);
466 f_print (fout, "_arg;\n");
469 else
471 filled = 1;
472 f_print (fout, "\t\t%s", proc->args.argname);
473 f_print (fout, " ");
474 pvname (proc->proc_name, vp->vers_num);
475 f_print (fout, "_arg;\n");
478 if (!filled)
480 f_print (fout, "\t\tint fill;\n");
482 f_print (fout, "\t} %s;\n", ARG);
483 if (mtflag)
485 f_print(fout, "\tunion {\n");
486 for (proc = vp->procs; proc != NULL; proc = proc->next)
487 if (!streq (proc->res_type, "void"))
489 f_print(fout, "\t\t");
490 ptype(proc->res_prefix, proc->res_type, 0);
491 pvname(proc->proc_name, vp->vers_num);
492 f_print(fout, "_res;\n");
494 f_print(fout, "\t} %s;\n", RESULT);
495 f_print(fout, "\tbool_t %s;\n", RETVAL);
497 } else
498 f_print (fout, "\tchar *%s;\n", RESULT);
500 if (Cflag)
502 f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
503 if (mtflag)
504 f_print(fout,
505 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
506 ROUTINE);
507 else
508 f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
509 ROUTINE);
511 else
513 f_print (fout, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG, RESULT);
514 if (mtflag)
515 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
516 else
517 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
519 f_print (fout, "\n");
521 if (timerflag)
522 #if 0
523 f_print (fout, "\t_rpcsvcdirty = 1;\n");
524 #else
526 if (mtflag)
527 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
528 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
529 if (mtflag)
530 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
532 #endif
534 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
535 if (!nullproc (vp->procs))
537 f_print (fout, "\tcase NULLPROC:\n");
538 f_print (fout,
539 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
540 TRANSP);
541 print_return ("\t\t");
542 f_print (fout, "\n");
544 for (proc = vp->procs; proc != NULL; proc = proc->next)
546 f_print (fout, "\tcase %s:\n", proc->proc_name);
547 if (proc->arg_num < 2)
548 { /* single argument */
549 p_xdrfunc (ARG, proc->args.decls->decl.type);
551 else
553 p_xdrfunc (ARG, proc->args.argname);
555 p_xdrfunc (RESULT, proc->res_type);
556 if (Cflag)
558 if (mtflag)
559 f_print(fout,
560 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
561 ROUTINE);
562 else
563 f_print (fout,
564 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
565 ROUTINE);
567 else
568 if (mtflag)
569 f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
570 else
571 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
573 if (newstyle)
574 { /* new style: calls internal routine */
575 f_print (fout, "_");
577 if ((Cflag || mtflag) && !newstyle)
578 pvname_svc (proc->proc_name, vp->vers_num);
579 else
580 pvname (proc->proc_name, vp->vers_num);
581 f_print (fout, ";\n");
582 f_print (fout, "\t\tbreak;\n\n");
584 f_print (fout, "\tdefault:\n");
585 printerr ("noproc", TRANSP);
586 print_return ("\t\t");
587 f_print (fout, "\t}\n");
589 f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
590 printif ("getargs", TRANSP, ARG);
591 printerr ("decode", TRANSP);
592 print_return ("\t\t");
593 f_print (fout, "\t}\n");
595 if (!mtflag)
597 if (Cflag)
598 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
599 RESULT, ROUTINE, ARG, RQSTP);
600 else
601 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
602 RESULT, ROUTINE, ARG, RQSTP);
604 else
605 if (Cflag)
606 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
607 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
608 else
609 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
610 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
611 if (mtflag)
612 f_print(fout,
613 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
614 RETVAL, TRANSP, RESULT, RESULT);
615 else
616 f_print(fout,
617 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
618 RESULT, TRANSP, RESULT, RESULT);
620 printerr ("systemerr", TRANSP);
621 f_print (fout, "\t}\n");
623 printif ("freeargs", TRANSP, ARG);
625 sprintf (_errbuf, "unable to free arguments");
626 print_err_message ("\t\t");
627 f_print (fout, "\t\texit (1);\n");
628 f_print (fout, "\t}\n");
629 /* print out free routine */
630 if (mtflag)
632 f_print(fout,"\tif (!");
633 pvname(def->def_name, vp->vers_num);
634 f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
635 TRANSP, RESULT, RESULT);
636 (void) sprintf(_errbuf, "unable to free results");
637 print_err_message("\t\t");
638 f_print(fout, "\n");
640 print_return ("\t");
641 f_print (fout, "}\n");
645 static void
646 printerr (const char *err, const char *transp)
648 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
651 static void
652 printif (const char *proc, const char *transp, const char *arg)
654 f_print (fout, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
655 proc, transp, arg, arg);
659 nullproc (const proc_list * proc)
661 for (; proc != NULL; proc = proc->next)
663 if (streq (proc->proc_num, "0"))
665 return 1;
668 return 0;
671 static void
672 write_inetmost (const char *infile)
674 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
675 f_print (fout, "\tint sock;\n");
676 f_print (fout, "\tint proto;\n");
677 f_print (fout, "\tstruct sockaddr_in saddr;\n");
678 f_print (fout, "\tint asize = sizeof (saddr);\n");
679 f_print (fout, "\n");
680 f_print (fout,
681 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
682 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
683 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
684 f_print (fout, "\t\t\texit (1);\n");
685 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
686 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
687 f_print (fout, "\t\t\texit (1);\n");
688 f_print (fout, "\t\tsock = 0;\n");
689 f_print (fout, "\t\t_rpcpmstart = 1;\n");
690 f_print (fout, "\t\tproto = 0;\n");
691 open_log_file (infile, "\t\t");
692 f_print (fout, "\t} else {\n");
693 write_rpc_svc_fg (infile, "\t\t");
694 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
695 print_pmapunset ("\t\t");
696 f_print (fout, "\t}\n");
699 static void
700 print_return (const char *space)
702 if (exitnow)
703 f_print (fout, "%sexit (0);\n", space);
704 else
706 if (timerflag)
708 #if 0
709 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
710 #else
711 if (mtflag)
712 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
713 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
714 if (mtflag)
715 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
716 #endif
718 f_print (fout, "%sreturn;\n", space);
722 static void
723 print_pmapunset (const char *space)
725 list *l;
726 definition *def;
727 version_list *vp;
729 for (l = defined; l != NULL; l = l->next)
731 def = (definition *) l->val;
732 if (def->def_kind == DEF_PROGRAM)
734 for (vp = def->def.pr.versions; vp != NULL;
735 vp = vp->next)
737 f_print (fout, "%spmap_unset (%s, %s);\n",
738 space, def->def_name, vp->vers_name);
744 static void
745 print_err_message (const char *space)
747 if (logflag)
748 f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
749 else if (inetdflag || pmflag)
750 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
751 else
752 f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
756 * Write the server auxiliary function ( _msgout, timeout)
758 void
759 write_svc_aux (int nomain)
761 if (!logflag)
762 write_msg_out ();
763 if (!nomain)
764 write_timeout_func ();
768 * Write the _msgout function
771 void
772 write_msg_out (void)
774 f_print (fout, "\n");
775 f_print (fout, "static\n");
776 if (!Cflag)
778 f_print (fout, "void _msgout (msg)\n");
779 f_print (fout, "\tchar *msg;\n");
781 else
783 f_print (fout, "void _msgout (char* msg)\n");
785 f_print (fout, "{\n");
786 f_print (fout, "#ifdef RPC_SVC_FG\n");
787 if (inetdflag || pmflag)
788 f_print (fout, "\tif (_rpcpmstart)\n");
789 f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
790 f_print (fout, "\telse\n");
791 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
792 f_print (fout, "#else\n");
793 f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
794 f_print (fout, "#endif\n");
795 f_print (fout, "}\n");
799 * Write the timeout function
801 static void
802 write_timeout_func (void)
804 if (!timerflag)
805 return;
806 f_print (fout, "\n");
807 f_print (fout, "static void\n");
808 if (Cflag)
809 f_print (fout, "closedown (int sig)\n");
810 else
811 f_print (fout, "closedown (sig)\n\tint sig;\n");
812 f_print (fout, "{\n");
814 #if defined (__GNU_LIBRARY__) && 0
815 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
816 Cflag ? "(SIG_PF)" : "(void(*)())");
817 #endif
818 if (mtflag)
819 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
820 #if 0
821 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
822 #else
823 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
824 #endif
825 f_print (fout, "\t\textern fd_set svc_fdset;\n");
826 f_print (fout, "\t\tstatic int size;\n");
827 f_print (fout, "\t\tint i, openfd;\n");
828 if (tirpcflag && pmflag)
830 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
831 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
833 else
835 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
837 f_print (fout, "\t\t\texit (0);\n");
838 f_print (fout, "\t\tif (size == 0) {\n");
839 if (tirpcflag)
841 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
842 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
843 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
844 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
845 if (mtflag)
846 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
847 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
849 else
851 f_print (fout, "\t\t\tsize = getdtablesize();\n");
853 f_print (fout, "\t\t}\n");
854 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
855 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
856 f_print (fout, "\t\t\t\topenfd++;\n");
857 f_print (fout, "\t\tif (openfd <= 1)\n");
858 f_print (fout, "\t\t\texit (0);\n");
859 f_print (fout, "\t}\n");
860 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
861 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
862 if (mtflag)
863 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
864 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
865 Cflag? "(SIG_PF)" : "(void(*)())");
866 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
867 f_print (fout, "}\n");
871 * Write the most of port monitor support
873 static void
874 write_pm_most (const char *infile, int netflag)
876 list *l;
877 definition *def;
878 version_list *vp;
880 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
881 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
882 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
883 f_print (fout, "\t\tchar *netid;\n");
884 if (!netflag)
885 { /* Not included by -n option */
886 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
887 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
889 if (timerflag)
890 f_print (fout, "\t\tint pmclose;\n");
891 /* not necessary, defined in /usr/include/stdlib */
892 /* f_print(fout, "\t\textern char *getenv();\n"); */
893 f_print (fout, "\n");
894 f_print (fout, "\t\t_rpcpmstart = 1;\n");
895 if (logflag)
896 open_log_file (infile, "\t\t");
897 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
898 sprintf (_errbuf, "cannot get transport name");
899 print_err_message ("\t\t\t");
900 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
901 sprintf (_errbuf, "cannot get transport info");
902 print_err_message ("\t\t\t");
903 f_print (fout, "\t\t}\n");
905 * A kludgy support for inetd services. Inetd only works with
906 * sockmod, and RPC works only with timod, hence all this jugglery
908 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
909 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
910 sprintf (_errbuf, "could not get the right module");
911 print_err_message ("\t\t\t\t");
912 f_print (fout, "\t\t\t\texit(1);\n");
913 f_print (fout, "\t\t\t}\n");
914 f_print (fout, "\t\t}\n");
915 if (timerflag)
916 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
917 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
918 TRANSP);
919 sprintf (_errbuf, "cannot create server handle");
920 print_err_message ("\t\t\t");
921 f_print (fout, "\t\t\texit(1);\n");
922 f_print (fout, "\t\t}\n");
923 f_print (fout, "\t\tif (nconf)\n");
924 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
925 for (l = defined; l != NULL; l = l->next)
927 def = (definition *) l->val;
928 if (def->def_kind != DEF_PROGRAM)
930 continue;
932 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
934 f_print (fout,
935 "\t\tif (!svc_reg(%s, %s, %s, ",
936 TRANSP, def->def_name, vp->vers_name);
937 pvname (def->def_name, vp->vers_num);
938 f_print (fout, ", 0)) {\n");
939 (void) sprintf (_errbuf, "unable to register (%s, %s).",
940 def->def_name, vp->vers_name);
941 print_err_message ("\t\t\t");
942 f_print (fout, "\t\t\texit(1);\n");
943 f_print (fout, "\t\t}\n");
946 if (timerflag)
948 f_print (fout, "\t\tif (pmclose) {\n");
949 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
950 Cflag ? "(SIG_PF)" : "(void(*)())");
951 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
952 f_print (fout, "\t\t}\n");
954 f_print (fout, "\t\tsvc_run();\n");
955 f_print (fout, "\t\texit(1);\n");
956 f_print (fout, "\t\t/* NOTREACHED */\n");
957 f_print (fout, "\t}\n");
961 * Support for backgrounding the server if self started.
963 static void
964 write_rpc_svc_fg (const char *infile, const char *sp)
966 f_print (fout, "#ifndef RPC_SVC_FG\n");
967 f_print (fout, "%sint size;\n", sp);
968 if (tirpcflag)
969 f_print (fout, "%sstruct rlimit rl;\n", sp);
970 if (inetdflag)
971 f_print (fout, "%sint pid, i;\n\n", sp);
972 f_print (fout, "%spid = fork();\n", sp);
973 f_print (fout, "%sif (pid < 0) {\n", sp);
974 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
975 f_print (fout, "%s\texit(1);\n", sp);
976 f_print (fout, "%s}\n", sp);
977 f_print (fout, "%sif (pid)\n", sp);
978 f_print (fout, "%s\texit(0);\n", sp);
979 /* get number of file descriptors */
980 if (tirpcflag)
982 f_print (fout, "%srl.rlim_max = 0;\n", sp);
983 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
984 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
985 f_print (fout, "%s\texit(1);\n", sp);
987 else
989 f_print (fout, "%ssize = getdtablesize();\n", sp);
992 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
993 f_print (fout, "%s\t(void) close(i);\n", sp);
994 /* Redirect stderr and stdout to console */
995 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
996 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
997 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
998 /* This removes control of the controlling terminal */
999 if (tirpcflag)
1000 f_print (fout, "%ssetsid();\n", sp);
1001 else
1003 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1004 f_print (fout, "%sif (i >= 0) {\n", sp);
1005 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
1006 f_print (fout, "%s\t(void) close(i);\n", sp);
1007 f_print (fout, "%s}\n", sp);
1009 if (!logflag)
1010 open_log_file (infile, sp);
1011 f_print (fout, "#endif\n");
1012 if (logflag)
1013 open_log_file (infile, sp);
1016 static void
1017 open_log_file (const char *infile, const char *sp)
1019 char *s;
1021 s = strrchr (infile, '.');
1022 if (s)
1023 *s = '\0';
1024 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1025 if (s)
1026 *s = '.';
1030 * write a registration for the given transport for Inetd
1032 void
1033 write_inetd_register (const char *transp)
1035 list *l;
1036 definition *def;
1037 version_list *vp;
1038 const char *sp;
1039 int isudp;
1040 char tmpbuf[32];
1042 if (inetdflag)
1043 sp = "\t";
1044 else
1045 sp = "";
1046 if (streq (transp, "udp") || streq (transp, "udp6"))
1047 isudp = 1;
1048 else
1049 isudp = 0;
1050 f_print (fout, "\n");
1051 if (inetdflag)
1053 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1054 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1056 f_print (fout, "%s\t%s = svc%s_create(%s",
1057 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1058 if (!isudp)
1059 f_print (fout, ", 0, 0");
1060 f_print (fout, ");\n");
1061 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1062 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1063 (void) sprintf (tmpbuf, "%s\t\t", sp);
1064 print_err_message (tmpbuf);
1065 f_print (fout, "%s\t\texit(1);\n", sp);
1066 f_print (fout, "%s\t}\n", sp);
1068 if (inetdflag)
1070 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1071 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1072 sp, isudp ? "UDP" : "TCP");
1074 for (l = defined; l != NULL; l = l->next)
1076 def = (definition *) l->val;
1077 if (def->def_kind != DEF_PROGRAM)
1079 continue;
1081 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1083 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1084 sp, TRANSP, def->def_name, vp->vers_name);
1085 pvname (def->def_name, vp->vers_num);
1086 if (inetdflag)
1087 f_print (fout, ", proto)) {\n");
1088 else
1089 f_print (fout, ", IPPROTO_%s)) {\n",
1090 isudp ? "UDP" : "TCP");
1091 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1092 def->def_name, vp->vers_name, transp);
1093 print_err_message (tmpbuf);
1094 f_print (fout, "%s\t\texit(1);\n", sp);
1095 f_print (fout, "%s\t}\n", sp);
1098 if (inetdflag)
1099 f_print (fout, "\t}\n");