rpc_svcout.c revision 1.25 1 /* $NetBSD: rpc_svcout.c,v 1.25 2013/12/15 00:40:17 christos Exp $ */
2 /*
3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4 * unrestricted use provided that this legend is included on all tape
5 * media and as a part of the software program in whole or part. Users
6 * may copy or modify Sun RPC without charge, but are not authorized
7 * to license or distribute it to anyone else except as part of a product or
8 * program developed by the user or with the express written consent of
9 * Sun Microsystems, Inc.
10 *
11 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
12 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
13 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
14 *
15 * Sun RPC is provided with no support and without any obligation on the
16 * part of Sun Microsystems, Inc. to assist in its use, correction,
17 * modification or enhancement.
18 *
19 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
20 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
21 * OR ANY PART THEREOF.
22 *
23 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
24 * or profits or other special, indirect and consequential damages, even if
25 * Sun has been advised of the possibility of such damages.
26 *
27 * Sun Microsystems, Inc.
28 * 2550 Garcia Avenue
29 * Mountain View, California 94043
30 */
31
32 #if HAVE_NBTOOL_CONFIG_H
33 #include "nbtool_config.h"
34 #endif
35
36 #include <sys/cdefs.h>
37 #if defined(__RCSID) && !defined(lint)
38 #if 0
39 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
40 #else
41 __RCSID("$NetBSD: rpc_svcout.c,v 1.25 2013/12/15 00:40:17 christos Exp $");
42 #endif
43 #endif
44
45 /*
46 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
47 */
48 #include <stdio.h>
49 #include <string.h>
50 #include "rpc_scan.h"
51 #include "rpc_parse.h"
52 #include "rpc_util.h"
53
54 static char RQSTP[] = "rqstp";
55 static char TRANSP[] = "transp";
56 static char ARG[] = "argument";
57 static char RESULT[] = "result";
58 static char ROUTINE[] = "local";
59
60 static void p_xdrfunc(const char *, const char *);
61 static void internal_proctype(proc_list *);
62 static void write_real_program(definition *);
63 static void write_program(definition *, const char *);
64 static void printerr(const char *, const char *);
65 static void printif(const char *, const char *, const char *, const char *);
66 static void write_inetmost(char *);
67 static void print_return(const char *);
68 static void print_pmapunset(const char *);
69 static void print_err_message(const char *);
70 static void write_timeout_func(void);
71 static void write_caller_func(void);
72 static void write_pm_most(char *, int);
73 static void write_rpc_svc_fg(char *, const char *);
74 static void open_log_file(char *, const char *);
75 static const char *aster(const char *);
76
77 char _errbuf[256]; /* For all messages */
78
79 static void
80 p_xdrfunc(const char *rname, const char *typename)
81 {
82 f_print(fout, "\t\txdr_%s = (xdrproc_t)xdr_%s;\n", rname,
83 stringfix(typename));
84 }
85
86 static void
87 internal_proctype(proc_list *plist)
88 {
89 f_print(fout, "static ");
90 ptype(plist->res_prefix, plist->res_type, 1);
91 if (!Mflag)
92 f_print(fout, "*");
93 }
94
95
96 /*
97 * write most of the service, that is, everything but the registrations.
98 */
99 void
100 write_most(char *infile /* our name */, int netflag, int nomain)
101 {
102 if (inetdflag || pmflag) {
103 const char *var_type;
104 var_type = (nomain ? "" : "static ");
105 f_print(fout, "%sint _rpcpmstart;", var_type);
106 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
107 f_print(fout, "%sint _rpcfdtype;", var_type);
108 f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
109 if (timerflag) {
110 f_print(fout, "%sint _rpcsvcdirty;", var_type);
111 f_print(fout, "\t/* Still serving ? */\n");
112 }
113 write_svc_aux(nomain);
114 }
115 /* write out dispatcher and stubs */
116 write_programs(nomain ? NULL : "static");
117
118 if (nomain)
119 return;
120
121 f_print(fout, "\nint\n");
122 f_print(fout, "main(int argc, char *argv[])\n");
123 f_print(fout, "{\n");
124 if (inetdflag) {
125 write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */
126 } else {
127 if (tirpcflag) {
128 if (netflag) {
129 f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
130 f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
131 }
132 f_print(fout, "\tpid_t pid;\n");
133 f_print(fout, "\tint i;\n");
134 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
135 write_pm_most(infile, netflag);
136 f_print(fout, "\telse {\n");
137 write_rpc_svc_fg(infile, "\t\t");
138 f_print(fout, "\t}\n");
139 } else {
140 f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
141 f_print(fout, "\n");
142 print_pmapunset("\t");
143 }
144 }
145
146 if (logflag && !inetdflag) {
147 open_log_file(infile, "\t");
148 }
149 }
150 /*
151 * write a registration for the given transport
152 */
153 void
154 write_netid_register(const char *transp)
155 {
156 list *l;
157 definition *def;
158 version_list *vp;
159 const char *sp;
160 char tmpbuf[32];
161
162 sp = "";
163 f_print(fout, "\n");
164 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
165 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
166 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
167 sprintf(tmpbuf, "%s\t\t", sp);
168 print_err_message(tmpbuf);
169 f_print(fout, "%s\t\texit(1);\n", sp);
170 f_print(fout, "%s\t}\n", sp);
171 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
172 sp, TRANSP);
173 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
174 (void) sprintf(_errbuf, "cannot create %s service.", transp);
175 print_err_message(tmpbuf);
176 f_print(fout, "%s\t\texit(1);\n", sp);
177 f_print(fout, "%s\t}\n", sp);
178
179 for (l = defined; l != NULL; l = l->next) {
180 def = (definition *) l->val;
181 if (def->def_kind != DEF_PROGRAM) {
182 continue;
183 }
184 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
185 f_print(fout,
186 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
187 sp, def->def_name, vp->vers_name);
188 f_print(fout,
189 "%s\tif (!svc_reg(%s, %s, %s, ",
190 sp, TRANSP, def->def_name, vp->vers_name);
191 pvname(def->def_name, vp->vers_num);
192 f_print(fout, ", nconf)) {\n");
193 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
194 def->def_name, vp->vers_name, transp);
195 print_err_message(tmpbuf);
196 f_print(fout, "%s\t\texit(1);\n", sp);
197 f_print(fout, "%s\t}\n", sp);
198 }
199 }
200 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
201 }
202 /*
203 * write a registration for the given transport for TLI
204 */
205 void
206 write_nettype_register(const char *transp)
207 {
208 list *l;
209 definition *def;
210 version_list *vp;
211
212 for (l = defined; l != NULL; l = l->next) {
213 def = (definition *) l->val;
214 if (def->def_kind != DEF_PROGRAM) {
215 continue;
216 }
217 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
218 f_print(fout, "\tif (!svc_create(");
219 pvname(def->def_name, vp->vers_num);
220 f_print(fout, ", %s, %s, \"%s\")) {\n ",
221 def->def_name, vp->vers_name, transp);
222 (void) sprintf(_errbuf,
223 "unable to create (%s, %s) for %s.",
224 def->def_name, vp->vers_name, transp);
225 print_err_message("\t\t");
226 f_print(fout, "\t\texit(1);\n");
227 f_print(fout, "\t}\n");
228 }
229 }
230 }
231 /*
232 * write the rest of the service
233 */
234 void
235 write_rest(void)
236 {
237 f_print(fout, "\n");
238 if (inetdflag) {
239 f_print(fout, "\tif (%s == NULL) {\n", TRANSP);
240 (void) sprintf(_errbuf, "could not create a handle");
241 print_err_message("\t\t");
242 f_print(fout, "\t\texit(1);\n");
243 f_print(fout, "\t}\n");
244 if (timerflag) {
245 f_print(fout, "\tif (_rpcpmstart) {\n");
246 f_print(fout,
247 "\t\t(void) signal(SIGALRM, (SIG_PF)closedown);\n");
248 f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
249 f_print(fout, "\t}\n");
250 }
251 }
252 f_print(fout, "\tsvc_run();\n");
253 (void) sprintf(_errbuf, "svc_run returned");
254 print_err_message("\t");
255 f_print(fout, "\texit(1);\n");
256 f_print(fout, "\t/* NOTREACHED */\n");
257 f_print(fout, "}\n");
258 }
259
260 void
261 write_programs(const char *storage)
262 {
263 list *l;
264 definition *def;
265
266 /* write out stubs for procedure definitions */
267 for (l = defined; l != NULL; l = l->next) {
268 def = (definition *) l->val;
269 if (def->def_kind == DEF_PROGRAM) {
270 write_real_program(def);
271 }
272 }
273
274 /* write out dispatcher for each program */
275 for (l = defined; l != NULL; l = l->next) {
276 def = (definition *) l->val;
277 if (def->def_kind == DEF_PROGRAM) {
278 write_program(def, storage);
279 }
280 }
281
282
283 }
284 /* write out definition of internal function (e.g. _printmsg_1(...))
285 which calls server's definition of actual function (e.g. printmsg_1(...)).
286 Unpacks single user argument of printmsg_1 to call-by-value format
287 expected by printmsg_1. */
288 static void
289 write_real_program(definition *def)
290 {
291 version_list *vp;
292 proc_list *proc;
293 decl_list *l;
294
295 if (!newstyle)
296 return; /* not needed for old style */
297 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
298 for (proc = vp->procs; proc != NULL; proc = proc->next) {
299 f_print(fout, "\n");
300 internal_proctype(proc);
301 f_print(fout, "\n_");
302 pvname(proc->proc_name, vp->vers_num);
303 f_print(fout, "(");
304 /* arg name */
305 if (proc->arg_num > 1)
306 f_print(fout, "%s ",
307 proc->args.argname);
308 else
309 ptype(proc->args.decls->decl.prefix,
310 proc->args.decls->decl.type, 0);
311 f_print(fout, "*argp, ");
312 if (Mflag) {
313 if (streq(proc->res_type, "void"))
314 f_print(fout, "char ");
315 else
316 ptype(proc->res_prefix,
317 proc->res_type, 0);
318 f_print(fout, "%sresult, ",
319 aster(proc->res_type));
320 }
321 f_print(fout, "struct svc_req *%s)\n", RQSTP);
322 f_print(fout, "{\n");
323 f_print(fout, "\treturn (");
324 pvname_svc(proc->proc_name, vp->vers_num);
325 f_print(fout, "(");
326 if (proc->arg_num < 2) { /* single argument */
327 if (!streq(proc->args.decls->decl.type, "void"))
328 f_print(fout, "*argp, "); /* non-void */
329 } else {
330 for (l = proc->args.decls; l != NULL; l = l->next)
331 f_print(fout, "argp->%s, ", l->decl.name);
332 }
333 f_print(fout, "%s));\n}\n", RQSTP);
334 }
335 }
336 }
337
338 static void
339 write_program(definition *def, const char *storage)
340 {
341 version_list *vp;
342 proc_list *proc;
343 int filled;
344
345 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
346 f_print(fout, "\n");
347 if (storage != NULL) {
348 f_print(fout, "%s ", storage);
349 }
350 f_print(fout, "void ");
351 pvname(def->def_name, vp->vers_num);
352 f_print(fout, "(struct svc_req *%s, ", RQSTP);
353 f_print(fout, "SVCXPRT *%s);\n", TRANSP);
354 f_print(fout, "\n");
355 if (storage != NULL) {
356 f_print(fout, "%s ", storage);
357 }
358 f_print(fout, "void\n");
359 pvname(def->def_name, vp->vers_num);
360
361 f_print(fout, "(struct svc_req *%s, ", RQSTP);
362 f_print(fout, "SVCXPRT *%s)\n", TRANSP);
363 f_print(fout, "{\n");
364
365 filled = 0;
366 f_print(fout, "\tunion {\n");
367 for (proc = vp->procs; proc != NULL; proc = proc->next) {
368 if (proc->arg_num < 2) { /* single argument */
369 if (streq(proc->args.decls->decl.type,
370 "void")) {
371 continue;
372 }
373 filled = 1;
374 f_print(fout, "\t\t");
375 ptype(proc->args.decls->decl.prefix,
376 proc->args.decls->decl.type, 0);
377 pvname(proc->proc_name, vp->vers_num);
378 f_print(fout, "_arg;\n");
379
380 } else {
381 filled = 1;
382 f_print(fout, "\t\t%s", proc->args.argname);
383 f_print(fout, " ");
384 pvname(proc->proc_name, vp->vers_num);
385 f_print(fout, "_arg;\n");
386 }
387 }
388 if (!filled) {
389 f_print(fout, "\t\tint fill;\n");
390 }
391 f_print(fout, "\t} %s;\n", ARG);
392 if (Mflag) {
393 f_print(fout, "\tunion {\n");
394 for (proc = vp->procs; proc != NULL; proc = proc->next) {
395 f_print(fout, "\t\t");
396 if (streq(proc->res_type, "void"))
397 f_print(fout, "char ");
398 else
399 ptype(proc->res_prefix, proc->res_type,
400 1);
401 pvname(proc->proc_name, vp->vers_num);
402 f_print(fout, "_res;\n");
403 }
404 f_print(fout, "\t} %s;\n", RESULT);
405 f_print(fout, "\tbool_t retval;\n");
406 } else
407 f_print(fout, "\tchar *%s;\n", RESULT);
408
409 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
410 if (Mflag)
411 f_print(fout,
412 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
413 ROUTINE);
414 else
415 f_print(fout,
416 "\tchar *(*%s)(char *, struct svc_req *);\n",
417 ROUTINE);
418
419 f_print(fout, "\n");
420
421 if (callerflag)
422 f_print(fout, "\tcaller = transp;\n"); /* EVAS */
423 if (timerflag)
424 f_print(fout, "\t_rpcsvcdirty = 1;\n");
425 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
426 if (!nullproc(vp->procs)) {
427 f_print(fout, "\tcase NULLPROC:\n");
428 f_print(fout,
429 "\t\t(void) svc_sendreply(%s, (xdrproc_t)xdr_void, NULL);\n", TRANSP);
430 print_return("\t\t");
431 f_print(fout, "\n");
432 }
433 for (proc = vp->procs; proc != NULL; proc = proc->next) {
434 f_print(fout, "\tcase %s:\n", proc->proc_name);
435 if (proc->arg_num < 2) { /* single argument */
436 p_xdrfunc(ARG, proc->args.decls->decl.type);
437 } else {
438 p_xdrfunc(ARG, proc->args.argname);
439 }
440 p_xdrfunc(RESULT, proc->res_type);
441 if (Mflag)
442 f_print(fout,
443 "\t\t%s = (bool_t (*)(char *, void *, struct svc_req *))",
444 ROUTINE);
445 else
446 f_print(fout,
447 "\t\t%s = (char *(*)(char *, struct svc_req *))",
448 ROUTINE);
449
450 if (newstyle) /* new style: calls internal routine */
451 f_print(fout, "_");
452 pvname_svc(proc->proc_name, vp->vers_num);
453 f_print(fout, ";\n");
454 f_print(fout, "\t\tbreak;\n\n");
455 }
456 f_print(fout, "\tdefault:\n");
457 printerr("noproc", TRANSP);
458 print_return("\t\t");
459 f_print(fout, "\t}\n");
460
461 f_print(fout, "\t(void) memset(&%s, 0, sizeof(%s));\n", ARG, ARG);
462 printif("getargs", TRANSP, "(caddr_t)&", ARG);
463 printerr("decode", TRANSP);
464 print_return("\t\t");
465 f_print(fout, "\t}\n");
466
467 if (Mflag)
468 f_print(fout, "\tretval = (*%s)((char *)&%s, (void *)&%s, %s);\n",
469 ROUTINE, ARG, RESULT, RQSTP);
470 else
471 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
472 RESULT, ROUTINE, ARG, RQSTP);
473 if (Mflag)
474 f_print(fout,
475 "\tif (retval > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
476 TRANSP, RESULT, RESULT);
477 else
478 f_print(fout,
479 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
480 RESULT, TRANSP, RESULT, RESULT);
481 printerr("systemerr", TRANSP);
482 f_print(fout, "\t}\n");
483
484 printif("freeargs", TRANSP, "(caddr_t)&", ARG);
485 (void) sprintf(_errbuf, "unable to free arguments");
486 print_err_message("\t\t");
487 f_print(fout, "\t\texit(1);\n");
488 f_print(fout, "\t}\n");
489
490 if (Mflag) {
491 f_print(fout, "\tif (!");
492 pvname(def->def_name, vp->vers_num);
493 f_print(fout, "_freeresult");
494 f_print(fout, "(%s, xdr_%s, (caddr_t)&%s)) {\n",
495 TRANSP, RESULT, RESULT);
496 (void) sprintf(_errbuf, "unable to free results");
497 print_err_message("\t\t");
498 f_print(fout, "\t\texit(1);\n");
499 f_print(fout, "\t}\n");
500 }
501
502 print_return("\t");
503 f_print(fout, "}\n");
504 }
505 }
506
507 static void
508 printerr(const char *err, const char *transp)
509 {
510 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
511 }
512
513 static void
514 printif(const char *proc, const char *transp, const char *prefix, const char *arg)
515 {
516 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
517 proc, transp, arg, prefix, arg);
518 }
519
520 int
521 nullproc(proc_list *proc)
522 {
523 for (; proc != NULL; proc = proc->next) {
524 if (streq(proc->proc_num, "0")) {
525 return (1);
526 }
527 }
528 return (0);
529 }
530
531 static void
532 write_inetmost(char *infile)
533 {
534 f_print(fout, "\tSVCXPRT *%s = NULL;\n", TRANSP);
535 f_print(fout, "\tint sock;\n");
536 f_print(fout, "\tint proto = 0;\n");
537 f_print(fout, "\tstruct sockaddr_in saddr;\n");
538 f_print(fout, "\tsocklen_t asize = (socklen_t)sizeof(saddr);\n");
539 f_print(fout, "\n");
540 f_print(fout,
541 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
542 f_print(fout, "\t\tsocklen_t ssize = (socklen_t)sizeof(int);\n\n");
543 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
544 f_print(fout, "\t\t\texit(1);\n");
545 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
546 f_print(fout, "\t\t\t\t(void *)&_rpcfdtype, &ssize) == -1)\n");
547 f_print(fout, "\t\t\texit(1);\n");
548 f_print(fout, "\t\tsock = 0;\n");
549 f_print(fout, "\t\t_rpcpmstart = 1;\n");
550 f_print(fout, "\t\tproto = 0;\n");
551 open_log_file(infile, "\t\t");
552 f_print(fout, "\t} else {\n");
553 write_rpc_svc_fg(infile, "\t\t");
554 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
555 print_pmapunset("\t\t");
556 f_print(fout, "\t}\n");
557 }
558
559 static void
560 print_return(const char *space)
561 {
562 if (exitnow)
563 f_print(fout, "%sexit(0);\n", space);
564 else {
565 if (timerflag)
566 f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
567 f_print(fout, "%sreturn;\n", space);
568 }
569 }
570
571 static void
572 print_pmapunset(const char *space)
573 {
574 list *l;
575 definition *def;
576 version_list *vp;
577
578 for (l = defined; l != NULL; l = l->next) {
579 def = (definition *) l->val;
580 if (def->def_kind == DEF_PROGRAM) {
581 for (vp = def->def.pr.versions; vp != NULL;
582 vp = vp->next) {
583 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
584 space, def->def_name, vp->vers_name);
585 }
586 }
587 }
588 }
589
590 static void
591 print_err_message(const char *space)
592 {
593 if (logflag)
594 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
595 else
596 if (inetdflag || pmflag)
597 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
598 else
599 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
600 }
601 /*
602 * Write the server auxiliary function ( _msgout, timeout)
603 */
604 void
605 write_svc_aux(int nomain)
606 {
607 if (!logflag)
608 write_msg_out();
609 if (!nomain)
610 write_timeout_func();
611 if (callerflag) /* EVAS */
612 write_caller_func(); /* EVAS */
613 }
614 /*
615 * Write the _msgout function
616 */
617 void
618 write_msg_out(void)
619 {
620 f_print(fout, "\n");
621 f_print(fout, "static\n");
622 f_print(fout, "void _msgout(const char *msg)\n");
623 f_print(fout, "{\n");
624 f_print(fout, "#ifdef RPC_SVC_FG\n");
625 if (inetdflag || pmflag)
626 f_print(fout, "\tif (_rpcpmstart)\n");
627 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
628 f_print(fout, "\telse\n");
629 f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
630 f_print(fout, "#else\n");
631 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
632 f_print(fout, "#endif\n");
633 f_print(fout, "}\n");
634 }
635 /*
636 * Write the timeout function
637 */
638 static void
639 write_timeout_func(void)
640 {
641 if (!timerflag)
642 return;
643 f_print(fout, "\n");
644 f_print(fout, "static void closedown(void);\n");
645 f_print(fout, "\n");
646 f_print(fout, "static void\n");
647 f_print(fout, "closedown()\n");
648 f_print(fout, "{\n");
649 f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
650 f_print(fout, "\t\textern fd_set svc_fdset;\n");
651 f_print(fout, "\t\tstatic int size;\n");
652 f_print(fout, "\t\tint i, openfd;\n");
653 if (tirpcflag && pmflag) {
654 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
655 f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
656 } else {
657 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
658 }
659 f_print(fout, "\t\t\texit(0);\n");
660 f_print(fout, "\t\tif (size == 0) {\n");
661 if (tirpcflag) {
662 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
663 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
664 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
665 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n");
666 f_print(fout, "\t\t\t\treturn;\n");
667 } else {
668 f_print(fout, "\t\t\tsize = getdtablesize();\n");
669 }
670 f_print(fout, "\t\t}\n");
671 f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
672 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
673 f_print(fout, "\t\t\t\topenfd++;\n");
674 f_print(fout, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
675 f_print(fout, "\t\t\texit(0);\n");
676 f_print(fout, "\t}\n");
677 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
678 f_print(fout, "}\n");
679 }
680
681 static void
682 write_caller_func(void)
683 { /* EVAS */
684 #define P(s) f_print(fout, s);
685
686 P("\n");
687 P("char *svc_caller()\n");
688 P("{\n");
689 P(" struct sockaddr_in actual;\n");
690 P(" struct hostent *hp;\n");
691 P(" static struct in_addr prev;\n");
692 P(" static char cname[128];\n\n");
693
694 P(" actual = *svc_getcaller(caller);\n\n");
695
696 P(" if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
697 P(" sizeof(struct in_addr)) == 0)\n");
698 P(" return (cname);\n\n");
699
700 P(" prev = actual.sin_addr;\n\n");
701
702 P(" hp = gethostbyaddr((char *)&actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
703 P(" if (hp == NULL) { /* dummy one up */\n");
704 P(" extern char *inet_ntoa();\n");
705 P(" strlcpy(cname, inet_ntoa(actual.sin_addr), sizeof(cname));\n");
706 P(" } else {\n");
707 P(" strlcpy(cname, hp->h_name, sizeof(cname));\n");
708 P(" }\n\n");
709
710 P(" return (cname);\n");
711 P("}\n");
712
713 #undef P
714 }
715 /*
716 * Write the most of port monitor support
717 */
718 static void
719 write_pm_most(char *infile, int netflag)
720 {
721 list *l;
722 definition *def;
723 version_list *vp;
724
725 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
726 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
727 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
728 f_print(fout, "\t\tchar *netid;\n");
729 if (!netflag) { /* Not included by -n option */
730 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
731 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
732 }
733 if (timerflag)
734 f_print(fout, "\t\tint pmclose;\n");
735 /* not necessary, defined in /usr/include/stdlib */
736 /* f_print(fout, "\t\textern char *getenv();\n");*/
737 f_print(fout, "\n");
738 f_print(fout, "\t\t_rpcpmstart = 1;\n");
739 if (logflag)
740 open_log_file(infile, "\t\t");
741 f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
742 sprintf(_errbuf, "cannot get transport name");
743 print_err_message("\t\t\t");
744 f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
745 sprintf(_errbuf, "cannot get transport info");
746 print_err_message("\t\t\t");
747 f_print(fout, "\t\t}\n");
748 /*
749 * A kludgy support for inetd services. Inetd only works with
750 * sockmod, and RPC works only with timod, hence all this jugglery
751 */
752 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
753 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
754 sprintf(_errbuf, "could not get the right module");
755 print_err_message("\t\t\t\t");
756 f_print(fout, "\t\t\t\texit(1);\n");
757 f_print(fout, "\t\t\t}\n");
758 f_print(fout, "\t\t}\n");
759 if (timerflag)
760 f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
761 f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
762 TRANSP);
763 sprintf(_errbuf, "cannot create server handle");
764 print_err_message("\t\t\t");
765 f_print(fout, "\t\t\texit(1);\n");
766 f_print(fout, "\t\t}\n");
767 f_print(fout, "\t\tif (nconf)\n");
768 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
769 for (l = defined; l != NULL; l = l->next) {
770 def = (definition *) l->val;
771 if (def->def_kind != DEF_PROGRAM) {
772 continue;
773 }
774 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
775 f_print(fout,
776 "\t\tif (!svc_reg(%s, %s, %s, ",
777 TRANSP, def->def_name, vp->vers_name);
778 pvname(def->def_name, vp->vers_num);
779 f_print(fout, ", 0)) {\n");
780 (void) sprintf(_errbuf, "unable to register (%s, %s).",
781 def->def_name, vp->vers_name);
782 print_err_message("\t\t\t");
783 f_print(fout, "\t\t\texit(1);\n");
784 f_print(fout, "\t\t}\n");
785 }
786 }
787 if (timerflag) {
788 f_print(fout, "\t\tif (pmclose) {\n");
789 f_print(fout, "\t\t\t(void) signal(SIGALRM, (SIGPF)closedown);\n");
790 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
791 f_print(fout, "\t\t}\n");
792 }
793 f_print(fout, "\t\tsvc_run();\n");
794 f_print(fout, "\t\texit(1);\n");
795 f_print(fout, "\t\t/* NOTREACHED */\n");
796 f_print(fout, "\t}\n");
797 }
798 /*
799 * Support for backgrounding the server if self started.
800 */
801 static void
802 write_rpc_svc_fg(char *infile, const char *sp)
803 {
804 f_print(fout, "#ifndef RPC_SVC_FG\n");
805 f_print(fout, "%sint size;\n", sp);
806 if (tirpcflag)
807 f_print(fout, "%sstruct rlimit rl;\n", sp);
808 if (inetdflag)
809 f_print(fout, "%sint pid, i;\n\n", sp);
810 f_print(fout, "%spid = fork();\n", sp);
811 f_print(fout, "%sif (pid < 0) {\n", sp);
812 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
813 f_print(fout, "%s\texit(1);\n", sp);
814 f_print(fout, "%s}\n", sp);
815 f_print(fout, "%sif (pid)\n", sp);
816 f_print(fout, "%s\texit(0);\n", sp);
817 /* get number of file descriptors */
818 if (tirpcflag) {
819 f_print(fout, "%srl.rlim_max = 0;\n", sp);
820 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
821 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
822 f_print(fout, "%s\texit(1);\n", sp);
823 } else {
824 f_print(fout, "%ssize = getdtablesize();\n", sp);
825 }
826
827 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
828 f_print(fout, "%s\t(void) close(i);\n", sp);
829 /* Redirect stderr and stdout to console */
830 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
831 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
832 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
833 /* This removes control of the controlling terminal */
834 if (tirpcflag)
835 f_print(fout, "%ssetsid();\n", sp);
836 else {
837 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
838 f_print(fout, "%sif (i >= 0) {\n", sp);
839 f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, NULL);\n", sp);
840 f_print(fout, "%s\t(void) close(i);\n", sp);
841 f_print(fout, "%s}\n", sp);
842 }
843 if (!logflag)
844 open_log_file(infile, sp);
845 f_print(fout, "#endif\n");
846 if (logflag)
847 open_log_file(infile, sp);
848 }
849
850 static void
851 open_log_file(char *infile, const char *sp)
852 {
853 char *s, *p;
854
855 s = strrchr(infile, '.');
856 if (s)
857 *s = '\0';
858 p = strrchr(infile, '/');
859 if (p)
860 p++;
861 else
862 p = infile;
863 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, p);
864 if (s)
865 *s = '.';
866 }
867
868 /*
869 * write a registration for the given transport for Inetd
870 */
871 void
872 write_inetd_register(const char *transp)
873 {
874 list *l;
875 definition *def;
876 version_list *vp;
877 const char *sp;
878 int isudp;
879 char tmpbuf[32];
880
881 if (inetdflag)
882 sp = "\t";
883 else
884 sp = "";
885 if (streq(transp, "udp"))
886 isudp = 1;
887 else
888 isudp = 0;
889 f_print(fout, "\n");
890 if (inetdflag) {
891 f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
892 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
893 }
894 if (inetdflag && streq(transp, "tcp")) {
895 f_print(fout, "%s\tif (_rpcpmstart)\n", sp);
896
897 f_print(fout, "%s\t\t%s = svc%s_create(%s",
898 sp, TRANSP, "fd", inetdflag ? "sock" : "RPC_ANYSOCK");
899 if (!isudp)
900 f_print(fout, ", 0, 0");
901 f_print(fout, ");\n");
902
903 f_print(fout, "%s\telse\n", sp);
904
905 f_print(fout, "%s\t\t%s = svc%s_create(%s",
906 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
907 if (!isudp)
908 f_print(fout, ", 0, 0");
909 f_print(fout, ");\n");
910
911 } else {
912 f_print(fout, "%s\t%s = svc%s_create(%s",
913 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK");
914 if (!isudp)
915 f_print(fout, ", 0, 0");
916 f_print(fout, ");\n");
917 }
918 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
919 (void) sprintf(_errbuf, "cannot create %s service.", transp);
920 (void) sprintf(tmpbuf, "%s\t\t", sp);
921 print_err_message(tmpbuf);
922 f_print(fout, "%s\t\texit(1);\n", sp);
923 f_print(fout, "%s\t}\n", sp);
924
925 if (inetdflag) {
926 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
927 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
928 sp, isudp ? "UDP" : "TCP");
929 }
930 for (l = defined; l != NULL; l = l->next) {
931 def = (definition *) l->val;
932 if (def->def_kind != DEF_PROGRAM) {
933 continue;
934 }
935 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
936 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
937 sp, TRANSP, def->def_name, vp->vers_name);
938 pvname(def->def_name, vp->vers_num);
939 if (inetdflag)
940 f_print(fout, ", proto)) {\n");
941 else
942 f_print(fout, ", IPPROTO_%s)) {\n",
943 isudp ? "UDP" : "TCP");
944 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
945 def->def_name, vp->vers_name, transp);
946 print_err_message(tmpbuf);
947 f_print(fout, "%s\t\texit(1);\n", sp);
948 f_print(fout, "%s\t}\n", sp);
949 }
950 }
951 if (inetdflag)
952 f_print(fout, "\t}\n");
953 }
954
955 static const char *
956 aster(const char *type)
957 {
958 if (isvectordef(type, REL_ALIAS)) {
959 return ("");
960 } else {
961 return ("*");
962 }
963 }
964