]> git.ipfire.org Git - thirdparty/glibc.git/blame - sunrpc/rpc_svcout.c
Add glibc.malloc.mxfast tunable
[thirdparty/glibc.git] / sunrpc / rpc_svcout.c
CommitLineData
28f540f4 1/*
a7ab6ec8 2 * From: @(#)rpc_svcout.c 1.29 89/03/30
cb636bb2 3 *
a7ab6ec8
UD
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:
0d204b0a 8 *
a7ab6ec8
UD
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.
0d204b0a 18 *
a7ab6ec8
UD
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.
0d204b0a 31 */
28f540f4
RM
32
33/*
34 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
28f540f4
RM
35 */
36#include <stdio.h>
0d204b0a 37#include <string.h>
28f540f4
RM
38#include "rpc_parse.h"
39#include "rpc_util.h"
0d204b0a 40#include "proto.h"
28f540f4 41
e7fd8a39
UD
42static const char RQSTP[] = "rqstp";
43static const char TRANSP[] = "transp";
44static const char ARG[] = "argument";
45static const char RESULT[] = "result";
46static const char ROUTINE[] = "local";
880f421f 47static char RETVAL[] = "retval";
e7fd8a39
UD
48
49char _errbuf[256]; /* For all messages */
50
1f205a47 51static void internal_proctype (const proc_list * plist);
e7fd8a39 52static void p_xdrfunc (const char *rname, const char *typename);
1f205a47
UD
53static void write_real_program (const definition * def);
54static void write_program (const definition * def, const char *storage);
e7fd8a39 55static void printerr (const char *err, const char *transp);
c64bd6c2 56static void printif (const char *proc, const char *transp, const char *arg);
e7fd8a39
UD
57static void write_inetmost (const char *infile);
58static void print_return (const char *space);
59static void print_pmapunset (const char *space);
60static void print_err_message (const char *space);
61static void write_timeout_func (void);
62static void write_pm_most (const char *infile, int netflag);
63static void write_rpc_svc_fg (const char *infile, const char *sp);
64static void open_log_file (const char *infile, const char *sp);
0d204b0a
UD
65
66static void
e7fd8a39 67p_xdrfunc (const char *rname, const char *typename)
0d204b0a 68{
e7fd8a39 69 if (Cflag)
ee586e0e 70 f_print (fout, "\t\t_xdr_%s = (xdrproc_t) xdr_%s;\n", rname,
e7fd8a39 71 stringfix (typename));
0d204b0a 72 else
ee586e0e 73 f_print (fout, "\t\t_xdr_%s = xdr_%s;\n", rname, stringfix (typename));
0d204b0a
UD
74}
75
76void
1f205a47 77internal_proctype (const proc_list * plist)
0d204b0a 78{
e7fd8a39
UD
79 f_print (fout, "static ");
80 ptype (plist->res_prefix, plist->res_type, 1);
81 f_print (fout, "*");
0d204b0a
UD
82}
83
28f540f4
RM
84
85/*
0d204b0a 86 * write most of the service, that is, everything but the registrations.
28f540f4
RM
87 */
88void
e7fd8a39 89write_most (const char *infile /* our name */ , int netflag, int nomain)
0d204b0a 90{
e7fd8a39
UD
91 if (inetdflag || pmflag)
92 {
93 const char *var_type;
e7fd8a39
UD
94 /* WHY? */
95 var_type = (nomain ? "extern" : "");
e7fd8a39
UD
96 f_print (fout, "%s int _rpcpmstart;", var_type);
97 f_print (fout, "\t\t/* Started by a port monitor ? */\n");
880f421f
UD
98 if (!tirpcflag)
99 {
100 f_print (fout, "%s int _rpcfdtype;", var_type);
101 f_print (fout, "\t\t/* Whether Stream or Datagram ? */\n");
102 }
e7fd8a39
UD
103 if (timerflag)
104 {
880f421f 105#if 0
e7fd8a39
UD
106 f_print (fout, "%s int _rpcsvcdirty;", var_type);
107 f_print (fout, "\t/* Still serving ? */\n");
880f421f
UD
108#else
109 f_print(fout, " /* States a server can be in wrt request */\n\n");
110 f_print(fout, "#define\t_IDLE 0\n");
111 f_print(fout, "#define\t_SERVED 1\n");
112 f_print(fout, "#define\t_SERVING 2\n\n");
113 f_print(fout, "static int _rpcsvcstate = _IDLE;");
114 f_print(fout, "\t /* Set when a request is serviced */\n");
115
116 if (mtflag)
117 {
118 f_print (fout, "mutex_t _svcstate_lock;");
119 f_print (fout,
120 "\t\t\t/* Mutex lock for variable_rpcsvcstate */\n");
121 }
122#endif
0d204b0a 123 }
e7fd8a39
UD
124 write_svc_aux (nomain);
125 }
126 /* write out dispatcher and stubs */
127 write_programs (nomain ? NULL : "static");
0d204b0a 128
e7fd8a39
UD
129 if (nomain)
130 return;
0d204b0a 131
e7fd8a39 132 if (Cflag)
880f421f 133 f_print (fout, "\nint\nmain (int argc, char **argv)\n");
e7fd8a39
UD
134 else
135 {
880f421f 136 f_print (fout, "\nint\nmain (argc, argv)\n");
e7fd8a39
UD
137 f_print (fout, "\tint argc;\n");
138 f_print (fout, "\tchar **argv;\n");
139 }
e7fd8a39
UD
140 f_print (fout, "{\n");
141 if (inetdflag)
142 {
143 write_inetmost (infile); /* Includes call to write_rpc_svc_fg() */
144 }
145 else
146 {
147 if (tirpcflag)
148 {
149 if (netflag)
150 {
151 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
152 f_print (fout, "\tstruct netconfig *nconf = NULL;\n");
153 }
154 f_print (fout, "\tpid_t pid;\n");
155 f_print (fout, "\tint i;\n");
156 f_print (fout, "\tchar mname[FMNAMESZ + 1];\n\n");
880f421f
UD
157
158 if (mtflag & timerflag)
159 f_print (fout,
160 "\tmutex_init (&_svcstate_lock, USYNC_THREAD, NULL);\n");
161
162 write_pm_most (infile, netflag);
e7fd8a39
UD
163 f_print (fout, "\telse {\n");
164 write_rpc_svc_fg (infile, "\t\t");
165 f_print (fout, "\t}\n");
0d204b0a 166 }
e7fd8a39
UD
167 else
168 {
169 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
170 f_print (fout, "\n");
171 print_pmapunset ("\t");
0d204b0a 172 }
e7fd8a39
UD
173 }
174
175 if (logflag && !inetdflag)
176 {
177 open_log_file (infile, "\t");
178 }
0d204b0a
UD
179}
180
181/*
182 * write a registration for the given transport
183 */
184void
e7fd8a39 185write_netid_register (const char *transp)
28f540f4 186{
e7fd8a39
UD
187 list *l;
188 definition *def;
189 version_list *vp;
190 const char *sp;
191 char tmpbuf[32];
192
193 sp = "";
194 f_print (fout, "\n");
880f421f 195 f_print (fout, "%s\tnconf = getnetconfigent (\"%s\");\n", sp, transp);
e7fd8a39
UD
196 f_print (fout, "%s\tif (nconf == NULL) {\n", sp);
197 (void) sprintf (_errbuf, "cannot find %s netid.", transp);
198 sprintf (tmpbuf, "%s\t\t", sp);
199 print_err_message (tmpbuf);
880f421f 200 f_print (fout, "%s\t\texit (1);\n", sp);
e7fd8a39 201 f_print (fout, "%s\t}\n", sp);
880f421f 202 f_print (fout, "%s\t%s = svc_tli_create (RPC_ANYFD, nconf, 0, 0, 0);\n",
e7fd8a39
UD
203 sp, TRANSP /*, transp *//* ?!?... */ );
204 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
880f421f 205 sprintf (_errbuf, "cannot create %s service.", transp);
e7fd8a39 206 print_err_message (tmpbuf);
880f421f 207 f_print (fout, "%s\t\texit (1);\n", sp);
e7fd8a39
UD
208 f_print (fout, "%s\t}\n", sp);
209
210 for (l = defined; l != NULL; l = l->next)
211 {
212 def = (definition *) l->val;
213 if (def->def_kind != DEF_PROGRAM)
214 {
215 continue;
216 }
217 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
218 {
880f421f 219 f_print (fout, "%s\t(void) rpcb_unset (%s, %s, nconf);\n",
e7fd8a39 220 sp, def->def_name, vp->vers_name);
880f421f 221 f_print (fout, "%s\tif (!svc_reg (%s, %s, %s, ",
e7fd8a39
UD
222 sp, TRANSP, def->def_name, vp->vers_name);
223 pvname (def->def_name, vp->vers_num);
224 f_print (fout, ", nconf)) {\n");
225 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
226 def->def_name, vp->vers_name, transp);
227 print_err_message (tmpbuf);
880f421f 228 f_print (fout, "%s\t\texit (1);\n", sp);
e7fd8a39 229 f_print (fout, "%s\t}\n", sp);
28f540f4 230 }
e7fd8a39 231 }
880f421f 232 f_print (fout, "%s\tfreenetconfigent (nconf);\n", sp);
28f540f4
RM
233}
234
28f540f4 235/*
0d204b0a 236 * write a registration for the given transport for TLI
28f540f4
RM
237 */
238void
e7fd8a39 239write_nettype_register (const char *transp)
28f540f4 240{
e7fd8a39
UD
241 list *l;
242 definition *def;
243 version_list *vp;
244
245 for (l = defined; l != NULL; l = l->next)
246 {
247 def = (definition *) l->val;
248 if (def->def_kind != DEF_PROGRAM)
249 {
250 continue;
28f540f4 251 }
e7fd8a39
UD
252 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
253 {
880f421f 254 f_print (fout, "\tif (!svc_create (");
e7fd8a39
UD
255 pvname (def->def_name, vp->vers_num);
256 f_print (fout, ", %s, %s, \"%s\")) {\n ",
257 def->def_name, vp->vers_name, transp);
258 (void) sprintf (_errbuf,
259 "unable to create (%s, %s) for %s.",
260 def->def_name, vp->vers_name, transp);
261 print_err_message ("\t\t");
880f421f 262 f_print (fout, "\t\texit (1);\n");
e7fd8a39
UD
263 f_print (fout, "\t}\n");
264 }
265 }
28f540f4
RM
266}
267
28f540f4 268/*
0d204b0a 269 * write the rest of the service
28f540f4
RM
270 */
271void
e7fd8a39 272write_rest (void)
28f540f4 273{
e7fd8a39
UD
274 f_print (fout, "\n");
275 if (inetdflag)
276 {
277 f_print (fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
278 (void) sprintf (_errbuf, "could not create a handle");
279 print_err_message ("\t\t");
880f421f 280 f_print (fout, "\t\texit (1);\n");
e7fd8a39
UD
281 f_print (fout, "\t}\n");
282 if (timerflag)
283 {
284 f_print (fout, "\tif (_rpcpmstart) {\n");
285 f_print (fout,
880f421f 286 "\t\t(void) signal (SIGALRM, %s closedown);\n",
e7fd8a39 287 Cflag ? "(SIG_PF)" : "(void(*)())");
880f421f 288 f_print (fout, "\t\t(void) alarm (_RPCSVC_CLOSEDOWN);\n");
e7fd8a39 289 f_print (fout, "\t}\n");
0d204b0a 290 }
e7fd8a39 291 }
880f421f 292 f_print (fout, "\tsvc_run ();\n");
e7fd8a39
UD
293 (void) sprintf (_errbuf, "svc_run returned");
294 print_err_message ("\t");
880f421f 295 f_print (fout, "\texit (1);\n");
e7fd8a39
UD
296 f_print (fout, "\t/* NOTREACHED */\n");
297 f_print (fout, "}\n");
28f540f4
RM
298}
299
300void
e7fd8a39 301write_programs (const char *storage)
28f540f4 302{
e7fd8a39
UD
303 list *l;
304 definition *def;
305
306 /* write out stubs for procedure definitions */
307 for (l = defined; l != NULL; l = l->next)
308 {
309 def = (definition *) l->val;
310 if (def->def_kind == DEF_PROGRAM)
311 {
312 write_real_program (def);
0d204b0a 313 }
e7fd8a39
UD
314 }
315
316 /* write out dispatcher for each program */
317 for (l = defined; l != NULL; l = l->next)
318 {
319 def = (definition *) l->val;
320 if (def->def_kind == DEF_PROGRAM)
321 {
322 write_program (def, storage);
28f540f4 323 }
e7fd8a39 324 }
28f540f4
RM
325}
326
0d204b0a
UD
327/* write out definition of internal function (e.g. _printmsg_1(...))
328 which calls server's defintion of actual function (e.g. printmsg_1(...)).
329 Unpacks single user argument of printmsg_1 to call-by-value format
330 expected by printmsg_1. */
331static void
1f205a47 332write_real_program (const definition * def)
0d204b0a 333{
e7fd8a39
UD
334 version_list *vp;
335 proc_list *proc;
336 decl_list *l;
337
338 if (!newstyle)
339 return; /* not needed for old style */
340 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
341 {
342 for (proc = vp->procs; proc != NULL; proc = proc->next)
343 {
880f421f
UD
344 fprintf (fout, "\n");
345 if (!mtflag)
346 internal_proctype (proc);
347 else
348 f_print (fout, "int");
e7fd8a39
UD
349 f_print (fout, "\n_");
350 pvname (proc->proc_name, vp->vers_num);
351 if (Cflag)
352 {
880f421f 353 f_print (fout, " (");
e7fd8a39
UD
354 /* arg name */
355 if (proc->arg_num > 1)
9f98c16c 356 f_print (fout, "%s", proc->args.argname);
e7fd8a39
UD
357 else
358 ptype (proc->args.decls->decl.prefix,
359 proc->args.decls->decl.type, 0);
880f421f
UD
360 if (mtflag)
361 {
362 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
363 RESULT, RQSTP);
364 }
365 else
366 f_print (fout, " *argp, struct svc_req *%s)\n",
367 RQSTP);
e7fd8a39
UD
368 }
369 else
370 {
880f421f
UD
371 if (mtflag)
372 f_print(fout, " (argp, %s, %s)\n", RESULT, RQSTP);
373 else
374 f_print (fout, " (argp, %s)\n", RQSTP);
e7fd8a39
UD
375 /* arg name */
376 if (proc->arg_num > 1)
377 f_print (fout, "\t%s *argp;\n", proc->args.argname);
378 else
379 {
380 f_print (fout, "\t");
381 ptype (proc->args.decls->decl.prefix,
382 proc->args.decls->decl.type, 0);
383 f_print (fout, " *argp;\n");
0d204b0a 384 }
e7fd8a39
UD
385 f_print (fout, " struct svc_req *%s;\n", RQSTP);
386 }
387
388 f_print (fout, "{\n");
880f421f
UD
389 f_print (fout, "\treturn (");
390 if (Cflag || mtflag)
e7fd8a39
UD
391 pvname_svc (proc->proc_name, vp->vers_num);
392 else
393 pvname (proc->proc_name, vp->vers_num);
394 f_print (fout, "(");
395 if (proc->arg_num < 2)
396 { /* single argument */
397 if (!streq (proc->args.decls->decl.type, "void"))
398 f_print (fout, "*argp, "); /* non-void */
399 }
400 else
401 {
402 for (l = proc->args.decls; l != NULL; l = l->next)
403 f_print (fout, "argp->%s, ", l->decl.name);
404 }
be916937
UD
405 if (mtflag)
406 f_print (fout, "%s, ", RESULT);
e7fd8a39 407 f_print (fout, "%s));\n}\n", RQSTP);
0d204b0a 408 }
e7fd8a39 409 }
0d204b0a
UD
410}
411
412static void
1f205a47 413write_program (const definition * def, const char *storage)
28f540f4 414{
e7fd8a39
UD
415 version_list *vp;
416 proc_list *proc;
417 int filled;
418
419 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
420 {
421 f_print (fout, "\n");
422 if (storage != NULL)
423 {
424 f_print (fout, "%s ", storage);
425 }
426 f_print (fout, "void\n");
427 pvname (def->def_name, vp->vers_num);
0d204b0a 428
e7fd8a39
UD
429 if (Cflag)
430 {
431 f_print (fout, "(struct svc_req *%s, ", RQSTP);
432 f_print (fout, "register SVCXPRT *%s)\n", TRANSP);
433 }
434 else
435 {
436 f_print (fout, "(%s, %s)\n", RQSTP, TRANSP);
437 f_print (fout, " struct svc_req *%s;\n", RQSTP);
438 f_print (fout, " register SVCXPRT *%s;\n", TRANSP);
439 }
0d204b0a 440
e7fd8a39
UD
441 f_print (fout, "{\n");
442
443 filled = 0;
444 f_print (fout, "\tunion {\n");
445 for (proc = vp->procs; proc != NULL; proc = proc->next)
446 {
447 if (proc->arg_num < 2)
448 { /* single argument */
449 if (streq (proc->args.decls->decl.type,
450 "void"))
451 {
452 continue;
28f540f4 453 }
e7fd8a39
UD
454 filled = 1;
455 f_print (fout, "\t\t");
456 ptype (proc->args.decls->decl.prefix,
457 proc->args.decls->decl.type, 0);
458 pvname (proc->proc_name, vp->vers_num);
459 f_print (fout, "_arg;\n");
460
461 }
462 else
463 {
464 filled = 1;
465 f_print (fout, "\t\t%s", proc->args.argname);
466 f_print (fout, " ");
467 pvname (proc->proc_name, vp->vers_num);
468 f_print (fout, "_arg;\n");
469 }
470 }
471 if (!filled)
472 {
473 f_print (fout, "\t\tint fill;\n");
474 }
475 f_print (fout, "\t} %s;\n", ARG);
880f421f
UD
476 if (mtflag)
477 {
478 f_print(fout, "\tunion {\n");
479 for (proc = vp->procs; proc != NULL; proc = proc->next)
480 if (!streq (proc->res_type, "void"))
481 {
482 f_print(fout, "\t\t");
483 ptype(proc->res_prefix, proc->res_type, 0);
484 pvname(proc->proc_name, vp->vers_num);
485 f_print(fout, "_res;\n");
486 }
487 f_print(fout, "\t} %s;\n", RESULT);
488 f_print(fout, "\tbool_t %s;\n", RETVAL);
489
490 } else
491 f_print (fout, "\tchar *%s;\n", RESULT);
e7fd8a39
UD
492
493 if (Cflag)
494 {
ee586e0e 495 f_print (fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
880f421f
UD
496 if (mtflag)
497 f_print(fout,
498 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
499 ROUTINE);
500 else
501 f_print (fout, "\tchar *(*%s)(char *, struct svc_req *);\n",
502 ROUTINE);
e7fd8a39
UD
503 }
504 else
505 {
c64bd6c2 506 f_print (fout, "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n", ARG, RESULT);
880f421f
UD
507 if (mtflag)
508 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
509 else
510 f_print (fout, "\tchar *(*%s)();\n", ROUTINE);
28f540f4 511 }
e7fd8a39
UD
512 f_print (fout, "\n");
513
514 if (timerflag)
880f421f 515#if 0
e7fd8a39 516 f_print (fout, "\t_rpcsvcdirty = 1;\n");
880f421f
UD
517#else
518 {
519 if (mtflag)
520 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
521 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
522 if (mtflag)
523 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
524 }
525#endif
526
e7fd8a39
UD
527 f_print (fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
528 if (!nullproc (vp->procs))
529 {
530 f_print (fout, "\tcase NULLPROC:\n");
531 f_print (fout,
c64bd6c2 532 "\t\t(void) svc_sendreply (%s, (xdrproc_t) xdr_void, (char *)NULL);\n",
e7fd8a39
UD
533 TRANSP);
534 print_return ("\t\t");
535 f_print (fout, "\n");
536 }
537 for (proc = vp->procs; proc != NULL; proc = proc->next)
538 {
539 f_print (fout, "\tcase %s:\n", proc->proc_name);
540 if (proc->arg_num < 2)
541 { /* single argument */
542 p_xdrfunc (ARG, proc->args.decls->decl.type);
543 }
544 else
545 {
546 p_xdrfunc (ARG, proc->args.argname);
547 }
548 p_xdrfunc (RESULT, proc->res_type);
549 if (Cflag)
880f421f
UD
550 {
551 if (mtflag)
552 f_print(fout,
553 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
554 ROUTINE);
555 else
556 f_print (fout,
557 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
558 ROUTINE);
559 }
e7fd8a39 560 else
880f421f
UD
561 if (mtflag)
562 f_print(fout, "\t\t%s = (bool_t (*)()) ", ROUTINE);
563 else
564 f_print (fout, "\t\t%s = (char *(*)()) ", ROUTINE);
e7fd8a39
UD
565
566 if (newstyle)
567 { /* new style: calls internal routine */
568 f_print (fout, "_");
569 }
880f421f 570 if ((Cflag || mtflag) && !newstyle)
e7fd8a39
UD
571 pvname_svc (proc->proc_name, vp->vers_num);
572 else
573 pvname (proc->proc_name, vp->vers_num);
574 f_print (fout, ";\n");
575 f_print (fout, "\t\tbreak;\n\n");
576 }
577 f_print (fout, "\tdefault:\n");
578 printerr ("noproc", TRANSP);
579 print_return ("\t\t");
580 f_print (fout, "\t}\n");
581
880f421f 582 f_print (fout, "\tmemset ((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
c64bd6c2 583 printif ("getargs", TRANSP, ARG);
e7fd8a39
UD
584 printerr ("decode", TRANSP);
585 print_return ("\t\t");
586 f_print (fout, "\t}\n");
587
880f421f 588 if (!mtflag)
8b7fb588
UD
589 {
590 if (Cflag)
591 f_print (fout, "\t%s = (*%s)((char *)&%s, %s);\n",
592 RESULT, ROUTINE, ARG, RQSTP);
593 else
594 f_print (fout, "\t%s = (*%s)(&%s, %s);\n",
595 RESULT, ROUTINE, ARG, RQSTP);
596 }
e7fd8a39 597 else
880f421f
UD
598 if (Cflag)
599 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
600 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
601 else
602 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
603 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
604 if (mtflag)
605 f_print(fout,
c64bd6c2 606 "\tif (%s > 0 && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, (char *)&%s)) {\n",
880f421f
UD
607 RETVAL, TRANSP, RESULT, RESULT);
608 else
609 f_print(fout,
c64bd6c2 610 "\tif (%s != NULL && !svc_sendreply(%s, (xdrproc_t) _xdr_%s, %s)) {\n",
880f421f
UD
611 RESULT, TRANSP, RESULT, RESULT);
612
e7fd8a39
UD
613 printerr ("systemerr", TRANSP);
614 f_print (fout, "\t}\n");
615
c64bd6c2 616 printif ("freeargs", TRANSP, ARG);
880f421f
UD
617
618 sprintf (_errbuf, "unable to free arguments");
e7fd8a39 619 print_err_message ("\t\t");
880f421f 620 f_print (fout, "\t\texit (1);\n");
e7fd8a39 621 f_print (fout, "\t}\n");
880f421f
UD
622 /* print out free routine */
623 if (mtflag)
624 {
625 f_print(fout,"\tif (!");
626 pvname(def->def_name, vp->vers_num);
ee586e0e 627 f_print(fout,"_freeresult (%s, _xdr_%s, (caddr_t) &%s))\n",
880f421f
UD
628 TRANSP, RESULT, RESULT);
629 (void) sprintf(_errbuf, "unable to free results");
630 print_err_message("\t\t");
631 f_print(fout, "\n");
632 }
e7fd8a39
UD
633 print_return ("\t");
634 f_print (fout, "}\n");
635 }
28f540f4
RM
636}
637
0d204b0a 638static void
e7fd8a39 639printerr (const char *err, const char *transp)
28f540f4 640{
880f421f 641 f_print (fout, "\t\tsvcerr_%s (%s);\n", err, transp);
28f540f4
RM
642}
643
0d204b0a 644static void
c64bd6c2 645printif (const char *proc, const char *transp, const char *arg)
28f540f4 646{
c64bd6c2
UD
647 f_print (fout, "\tif (!svc_%s (%s, (xdrproc_t) _xdr_%s, (caddr_t) &%s)) {\n",
648 proc, transp, arg, arg);
28f540f4
RM
649}
650
0d204b0a 651int
1f205a47 652nullproc (const proc_list * proc)
28f540f4 653{
e7fd8a39
UD
654 for (; proc != NULL; proc = proc->next)
655 {
656 if (streq (proc->proc_num, "0"))
657 {
880f421f 658 return 1;
28f540f4 659 }
e7fd8a39 660 }
880f421f 661 return 0;
28f540f4 662}
0d204b0a
UD
663
664static void
e7fd8a39 665write_inetmost (const char *infile)
0d204b0a 666{
e7fd8a39
UD
667 f_print (fout, "\tregister SVCXPRT *%s;\n", TRANSP);
668 f_print (fout, "\tint sock;\n");
669 f_print (fout, "\tint proto;\n");
670 f_print (fout, "\tstruct sockaddr_in saddr;\n");
671 f_print (fout, "\tint asize = sizeof (saddr);\n");
672 f_print (fout, "\n");
673 f_print (fout,
880f421f 674 "\tif (getsockname (0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
e7fd8a39
UD
675 f_print (fout, "\t\tint ssize = sizeof (int);\n\n");
676 f_print (fout, "\t\tif (saddr.sin_family != AF_INET)\n");
880f421f
UD
677 f_print (fout, "\t\t\texit (1);\n");
678 f_print (fout, "\t\tif (getsockopt (0, SOL_SOCKET, SO_TYPE,\n");
e7fd8a39 679 f_print (fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
880f421f 680 f_print (fout, "\t\t\texit (1);\n");
e7fd8a39
UD
681 f_print (fout, "\t\tsock = 0;\n");
682 f_print (fout, "\t\t_rpcpmstart = 1;\n");
683 f_print (fout, "\t\tproto = 0;\n");
684 open_log_file (infile, "\t\t");
685 f_print (fout, "\t} else {\n");
686 write_rpc_svc_fg (infile, "\t\t");
687 f_print (fout, "\t\tsock = RPC_ANYSOCK;\n");
688 print_pmapunset ("\t\t");
689 f_print (fout, "\t}\n");
0d204b0a
UD
690}
691
692static void
e7fd8a39 693print_return (const char *space)
0d204b0a 694{
e7fd8a39 695 if (exitnow)
880f421f 696 f_print (fout, "%sexit (0);\n", space);
e7fd8a39
UD
697 else
698 {
699 if (timerflag)
880f421f
UD
700 {
701#if 0
702 f_print (fout, "%s_rpcsvcdirty = 0;\n", space);
703#else
704 if (mtflag)
705 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
706 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
707 if (mtflag)
708 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
709#endif
710 }
e7fd8a39
UD
711 f_print (fout, "%sreturn;\n", space);
712 }
0d204b0a
UD
713}
714
715static void
e7fd8a39 716print_pmapunset (const char *space)
0d204b0a 717{
e7fd8a39
UD
718 list *l;
719 definition *def;
720 version_list *vp;
721
722 for (l = defined; l != NULL; l = l->next)
723 {
724 def = (definition *) l->val;
725 if (def->def_kind == DEF_PROGRAM)
726 {
727 for (vp = def->def.pr.versions; vp != NULL;
728 vp = vp->next)
729 {
880f421f 730 f_print (fout, "%spmap_unset (%s, %s);\n",
e7fd8a39
UD
731 space, def->def_name, vp->vers_name);
732 }
0d204b0a 733 }
e7fd8a39 734 }
0d204b0a
UD
735}
736
737static void
e7fd8a39 738print_err_message (const char *space)
0d204b0a 739{
e7fd8a39 740 if (logflag)
a714f366 741 f_print (fout, "%ssyslog (LOG_ERR, \"%%s\", \"%s\");\n", space, _errbuf);
e7fd8a39 742 else if (inetdflag || pmflag)
880f421f 743 f_print (fout, "%s_msgout (\"%s\");\n", space, _errbuf);
e7fd8a39 744 else
a714f366 745 f_print (fout, "%sfprintf (stderr, \"%%s\", \"%s\");\n", space, _errbuf);
0d204b0a
UD
746}
747
748/*
749 * Write the server auxiliary function ( _msgout, timeout)
750 */
751void
e7fd8a39 752write_svc_aux (int nomain)
0d204b0a 753{
e7fd8a39
UD
754 if (!logflag)
755 write_msg_out ();
756 if (!nomain)
757 write_timeout_func ();
0d204b0a
UD
758}
759
760/*
761 * Write the _msgout function
762 */
763
764void
e7fd8a39 765write_msg_out (void)
0d204b0a 766{
e7fd8a39
UD
767 f_print (fout, "\n");
768 f_print (fout, "static\n");
769 if (!Cflag)
770 {
880f421f 771 f_print (fout, "void _msgout (msg)\n");
e7fd8a39
UD
772 f_print (fout, "\tchar *msg;\n");
773 }
774 else
775 {
880f421f 776 f_print (fout, "void _msgout (char* msg)\n");
e7fd8a39
UD
777 }
778 f_print (fout, "{\n");
779 f_print (fout, "#ifdef RPC_SVC_FG\n");
780 if (inetdflag || pmflag)
781 f_print (fout, "\tif (_rpcpmstart)\n");
a714f366 782 f_print (fout, "\t\tsyslog (LOG_ERR, \"%%s\", msg);\n");
e7fd8a39 783 f_print (fout, "\telse\n");
880f421f 784 f_print (fout, "\t\tfprintf (stderr, \"%%s\\n\", msg);\n");
e7fd8a39 785 f_print (fout, "#else\n");
a714f366 786 f_print (fout, "\tsyslog (LOG_ERR, \"%%s\", msg);\n");
e7fd8a39
UD
787 f_print (fout, "#endif\n");
788 f_print (fout, "}\n");
0d204b0a
UD
789}
790
791/*
792 * Write the timeout function
793 */
794static void
e7fd8a39 795write_timeout_func (void)
0d204b0a 796{
e7fd8a39
UD
797 if (!timerflag)
798 return;
799 f_print (fout, "\n");
800 f_print (fout, "static void\n");
e7fd8a39 801 if (Cflag)
880f421f 802 f_print (fout, "closedown (int sig)\n");
e7fd8a39 803 else
880f421f 804 f_print (fout, "closedown (sig)\n\tint sig;\n");
e7fd8a39 805 f_print (fout, "{\n");
880f421f 806
1991fa03 807#if 0
880f421f 808 f_print (fout, "\t(void) signal (sig, %s closedown);\n",
e7fd8a39 809 Cflag ? "(SIG_PF)" : "(void(*)())");
0d204b0a 810#endif
880f421f
UD
811 if (mtflag)
812 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
813#if 0
e7fd8a39 814 f_print (fout, "\tif (_rpcsvcdirty == 0) {\n");
880f421f
UD
815#else
816 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
817#endif
e7fd8a39
UD
818 f_print (fout, "\t\textern fd_set svc_fdset;\n");
819 f_print (fout, "\t\tstatic int size;\n");
820 f_print (fout, "\t\tint i, openfd;\n");
821 if (tirpcflag && pmflag)
822 {
823 f_print (fout, "\t\tstruct t_info tinfo;\n\n");
824 f_print (fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
825 }
826 else
827 {
828 f_print (fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
829 }
880f421f 830 f_print (fout, "\t\t\texit (0);\n");
e7fd8a39
UD
831 f_print (fout, "\t\tif (size == 0) {\n");
832 if (tirpcflag)
833 {
834 f_print (fout, "\t\t\tstruct rlimit rl;\n\n");
835 f_print (fout, "\t\t\trl.rlim_max = 0;\n");
836 f_print (fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
880f421f
UD
837 f_print (fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
838 if (mtflag)
839 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
840 f_print (fout, "\t\t\t\treturn;\n\t\t\t}\n");
e7fd8a39
UD
841 }
842 else
843 {
844 f_print (fout, "\t\t\tsize = getdtablesize();\n");
845 }
846 f_print (fout, "\t\t}\n");
847 f_print (fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
848 f_print (fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
849 f_print (fout, "\t\t\t\topenfd++;\n");
850 f_print (fout, "\t\tif (openfd <= 1)\n");
880f421f 851 f_print (fout, "\t\t\texit (0);\n");
e7fd8a39 852 f_print (fout, "\t}\n");
880f421f
UD
853 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
854 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
855 if (mtflag)
856 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
857 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
858 Cflag? "(SIG_PF)" : "(void(*)())");
859 f_print (fout, "\talarm (_RPCSVC_CLOSEDOWN);\n");
e7fd8a39 860 f_print (fout, "}\n");
0d204b0a
UD
861}
862
863/*
864 * Write the most of port monitor support
865 */
866static void
e7fd8a39 867write_pm_most (const char *infile, int netflag)
0d204b0a 868{
e7fd8a39
UD
869 list *l;
870 definition *def;
871 version_list *vp;
872
873 f_print (fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
874 f_print (fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
875 f_print (fout, " !strcmp(mname, \"timod\"))) {\n");
876 f_print (fout, "\t\tchar *netid;\n");
877 if (!netflag)
878 { /* Not included by -n option */
879 f_print (fout, "\t\tstruct netconfig *nconf = NULL;\n");
880 f_print (fout, "\t\tSVCXPRT *%s;\n", TRANSP);
881 }
882 if (timerflag)
883 f_print (fout, "\t\tint pmclose;\n");
0d204b0a 884/* not necessary, defined in /usr/include/stdlib */
e7fd8a39
UD
885/* f_print(fout, "\t\textern char *getenv();\n"); */
886 f_print (fout, "\n");
887 f_print (fout, "\t\t_rpcpmstart = 1;\n");
888 if (logflag)
889 open_log_file (infile, "\t\t");
890 f_print (fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
891 sprintf (_errbuf, "cannot get transport name");
892 print_err_message ("\t\t\t");
893 f_print (fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
894 sprintf (_errbuf, "cannot get transport info");
895 print_err_message ("\t\t\t");
896 f_print (fout, "\t\t}\n");
897 /*
898 * A kludgy support for inetd services. Inetd only works with
899 * sockmod, and RPC works only with timod, hence all this jugglery
900 */
901 f_print (fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
902 f_print (fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
903 sprintf (_errbuf, "could not get the right module");
904 print_err_message ("\t\t\t\t");
905 f_print (fout, "\t\t\t\texit(1);\n");
906 f_print (fout, "\t\t\t}\n");
907 f_print (fout, "\t\t}\n");
908 if (timerflag)
909 f_print (fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
910 f_print (fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
911 TRANSP);
912 sprintf (_errbuf, "cannot create server handle");
913 print_err_message ("\t\t\t");
914 f_print (fout, "\t\t\texit(1);\n");
915 f_print (fout, "\t\t}\n");
916 f_print (fout, "\t\tif (nconf)\n");
917 f_print (fout, "\t\t\tfreenetconfigent(nconf);\n");
918 for (l = defined; l != NULL; l = l->next)
919 {
920 def = (definition *) l->val;
921 if (def->def_kind != DEF_PROGRAM)
922 {
923 continue;
0d204b0a 924 }
e7fd8a39
UD
925 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
926 {
927 f_print (fout,
928 "\t\tif (!svc_reg(%s, %s, %s, ",
929 TRANSP, def->def_name, vp->vers_name);
930 pvname (def->def_name, vp->vers_num);
931 f_print (fout, ", 0)) {\n");
932 (void) sprintf (_errbuf, "unable to register (%s, %s).",
933 def->def_name, vp->vers_name);
934 print_err_message ("\t\t\t");
935 f_print (fout, "\t\t\texit(1);\n");
936 f_print (fout, "\t\t}\n");
0d204b0a 937 }
e7fd8a39
UD
938 }
939 if (timerflag)
940 {
941 f_print (fout, "\t\tif (pmclose) {\n");
942 f_print (fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
943 Cflag ? "(SIG_PF)" : "(void(*)())");
944 f_print (fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
945 f_print (fout, "\t\t}\n");
946 }
947 f_print (fout, "\t\tsvc_run();\n");
948 f_print (fout, "\t\texit(1);\n");
949 f_print (fout, "\t\t/* NOTREACHED */\n");
950 f_print (fout, "\t}\n");
0d204b0a
UD
951}
952
953/*
954 * Support for backgrounding the server if self started.
955 */
956static void
e7fd8a39 957write_rpc_svc_fg (const char *infile, const char *sp)
0d204b0a 958{
e7fd8a39
UD
959 f_print (fout, "#ifndef RPC_SVC_FG\n");
960 f_print (fout, "%sint size;\n", sp);
961 if (tirpcflag)
962 f_print (fout, "%sstruct rlimit rl;\n", sp);
963 if (inetdflag)
964 f_print (fout, "%sint pid, i;\n\n", sp);
965 f_print (fout, "%spid = fork();\n", sp);
966 f_print (fout, "%sif (pid < 0) {\n", sp);
967 f_print (fout, "%s\tperror(\"cannot fork\");\n", sp);
968 f_print (fout, "%s\texit(1);\n", sp);
969 f_print (fout, "%s}\n", sp);
970 f_print (fout, "%sif (pid)\n", sp);
971 f_print (fout, "%s\texit(0);\n", sp);
972 /* get number of file descriptors */
973 if (tirpcflag)
974 {
975 f_print (fout, "%srl.rlim_max = 0;\n", sp);
976 f_print (fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
977 f_print (fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
978 f_print (fout, "%s\texit(1);\n", sp);
979 }
980 else
981 {
982 f_print (fout, "%ssize = getdtablesize();\n", sp);
983 }
984
985 f_print (fout, "%sfor (i = 0; i < size; i++)\n", sp);
986 f_print (fout, "%s\t(void) close(i);\n", sp);
987 /* Redirect stderr and stdout to console */
988 f_print (fout, "%si = open(\"/dev/console\", 2);\n", sp);
989 f_print (fout, "%s(void) dup2(i, 1);\n", sp);
990 f_print (fout, "%s(void) dup2(i, 2);\n", sp);
991 /* This removes control of the controlling terminal */
992 if (tirpcflag)
993 f_print (fout, "%ssetsid();\n", sp);
994 else
995 {
996 f_print (fout, "%si = open(\"/dev/tty\", 2);\n", sp);
997 f_print (fout, "%sif (i >= 0) {\n", sp);
998 f_print (fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
999 f_print (fout, "%s\t(void) close(i);\n", sp);
1000 f_print (fout, "%s}\n", sp);
1001 }
1002 if (!logflag)
1003 open_log_file (infile, sp);
1004 f_print (fout, "#endif\n");
1005 if (logflag)
1006 open_log_file (infile, sp);
0d204b0a
UD
1007}
1008
1009static void
e7fd8a39 1010open_log_file (const char *infile, const char *sp)
0d204b0a 1011{
e7fd8a39
UD
1012 char *s;
1013
1014 s = strrchr (infile, '.');
1015 if (s)
1016 *s = '\0';
1017 f_print (fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1018 if (s)
1019 *s = '.';
0d204b0a
UD
1020}
1021
0d204b0a
UD
1022/*
1023 * write a registration for the given transport for Inetd
1024 */
1025void
e7fd8a39 1026write_inetd_register (const char *transp)
0d204b0a 1027{
e7fd8a39
UD
1028 list *l;
1029 definition *def;
1030 version_list *vp;
1031 const char *sp;
1032 int isudp;
1033 char tmpbuf[32];
1034
1035 if (inetdflag)
1036 sp = "\t";
1037 else
1038 sp = "";
c1301d9a 1039 if (streq (transp, "udp") || streq (transp, "udp6"))
e7fd8a39
UD
1040 isudp = 1;
1041 else
1042 isudp = 0;
1043 f_print (fout, "\n");
1044 if (inetdflag)
1045 {
1046 f_print (fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1047 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1048 }
1049 f_print (fout, "%s\t%s = svc%s_create(%s",
1050 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
1051 if (!isudp)
1052 f_print (fout, ", 0, 0");
1053 f_print (fout, ");\n");
1054 f_print (fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1055 (void) sprintf (_errbuf, "cannot create %s service.", transp);
1056 (void) sprintf (tmpbuf, "%s\t\t", sp);
1057 print_err_message (tmpbuf);
1058 f_print (fout, "%s\t\texit(1);\n", sp);
1059 f_print (fout, "%s\t}\n", sp);
1060
1061 if (inetdflag)
1062 {
1063 f_print (fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1064 f_print (fout, "%s\tproto = IPPROTO_%s;\n",
1065 sp, isudp ? "UDP" : "TCP");
1066 }
1067 for (l = defined; l != NULL; l = l->next)
1068 {
1069 def = (definition *) l->val;
1070 if (def->def_kind != DEF_PROGRAM)
1071 {
1072 continue;
0d204b0a 1073 }
e7fd8a39
UD
1074 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next)
1075 {
1076 f_print (fout, "%s\tif (!svc_register(%s, %s, %s, ",
1077 sp, TRANSP, def->def_name, vp->vers_name);
1078 pvname (def->def_name, vp->vers_num);
1079 if (inetdflag)
1080 f_print (fout, ", proto)) {\n");
1081 else
1082 f_print (fout, ", IPPROTO_%s)) {\n",
1083 isudp ? "UDP" : "TCP");
1084 (void) sprintf (_errbuf, "unable to register (%s, %s, %s).",
1085 def->def_name, vp->vers_name, transp);
1086 print_err_message (tmpbuf);
1087 f_print (fout, "%s\t\texit(1);\n", sp);
1088 f_print (fout, "%s\t}\n", sp);
0d204b0a 1089 }
e7fd8a39
UD
1090 }
1091 if (inetdflag)
1092 f_print (fout, "\t}\n");
0d204b0a 1093}