MFC:
[dragonfly.git] / usr.bin / rpcgen / rpc_svcout.c
blob5ecd8ec4ae660a349efdcb534fac43ab581707d5
1 /*
2 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
3 * unrestricted use provided that this legend is included on all tape
4 * media and as a part of the software program in whole or part. Users
5 * may copy or modify Sun RPC without charge, but are not authorized
6 * to license or distribute it to anyone else except as part of a product or
7 * program developed by the user.
8 *
9 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
10 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
11 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13 * Sun RPC is provided with no support and without any obligation on the
14 * part of Sun Microsystems, Inc. to assist in its use, correction,
15 * modification or enhancement.
17 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
18 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
19 * OR ANY PART THEREOF.
21 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
22 * or profits or other special, indirect and consequential damages, even if
23 * Sun has been advised of the possibility of such damages.
25 * Sun Microsystems, Inc.
26 * 2550 Garcia Avenue
27 * Mountain View, California 94043
29 * @(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_svcout.c,v 1.6.6.1 2001/07/19 01:38:22 kris Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_svcout.c,v 1.4 2004/06/19 16:40:36 joerg Exp $
34 #ident "@(#)rpc_svcout.c 1.4 90/04/13 SMI"
37 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
38 * Copyright (C) 1987, Sun Microsystems, Inc.
40 #include <stdio.h>
41 #include <string.h>
42 #include "rpc_parse.h"
43 #include "rpc_util.h"
45 static char RQSTP[] = "rqstp";
46 static char TRANSP[] = "transp";
47 static char ARG[] = "argument";
48 static char RESULT[] = "result";
49 static char ROUTINE[] = "local";
50 static char RETVAL[] = "retval";
52 char _errbuf[256]; /* For all messages */
54 void internal_proctype(proc_list *);
55 static void write_real_program(definition *);
56 static void write_program(definition *, char *);
57 static void printerr(char *, char *);
58 static void printif(char *, char *, char *, char *);
59 static void write_inetmost(char *);
60 static void print_return(char *);
61 static void print_pmapunset(char *);
62 static void print_err_message(char *);
63 static void write_timeout_func(void);
64 static void write_pm_most(char *, int);
65 static void write_rpc_svc_fg(char *, char *);
66 static void open_log_file(char *, char *);
67 static void write_msg_out(void);
68 int nullpro(proc_list *);
70 static void
71 p_xdrfunc(char* rname, char* typename)
73 if (Cflag)
74 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
75 rname, stringfix(typename));
76 else
77 f_print(fout, "\t\txdr_%s = xdr_%s;\n",
78 rname, stringfix(typename));
81 void
82 internal_proctype(proc_list *plist)
84 f_print(fout, "static ");
85 ptype(plist->res_prefix, plist->res_type, 1);
86 f_print(fout, "*");
91 * write most of the service, that is, everything but the registrations.
93 void
94 write_most(char *infile, int netflag, int nomain)
96 if (inetdflag || pmflag) {
97 char* var_type;
98 var_type = (nomain? "extern" : "static");
99 f_print(fout, "%s int _rpcpmstart;", var_type);
100 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
101 if (!tirpcflag) {
102 f_print(fout, "%s int _rpcfdtype;", var_type);
103 f_print(fout, "\n\t\t /* Whether Stream or "
104 "Datagram ? */\n");
107 if (timerflag) {
108 f_print(fout, " /* States a server can be in "
109 "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 "
115 "serviced */\n");
117 if (mtflag) {
118 f_print(fout, "mutex_t _svcstate_lock;");
119 f_print(fout, "\t\t\t/* Mutex lock for "
120 "variable _rpcsvcstate */\n");
125 write_svc_aux(nomain);
127 /* write out dispatcher and stubs */
128 write_programs(nomain? (char *)NULL : "static");
130 if (nomain)
131 return;
133 f_print(fout, "\nmain()\n");
134 f_print(fout, "{\n");
135 if (inetdflag) {
136 write_inetmost(infile);
137 /* Includes call to write_rpc_svc_fg() */
138 } else {
139 if (tirpcflag) {
140 if (netflag) {
141 f_print(fout,
142 "\tregister SVCXPRT *%s;\n", TRANSP);
143 f_print(fout,
144 "\tstruct netconfig *nconf = NULL;\n");
146 f_print(fout, "\tpid_t pid;\n");
147 f_print(fout, "\tint i;\n");
148 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
150 if (mtflag & timerflag)
151 f_print(fout, "\tmutex_init(&_svcstate_lock, "
152 "USYNC_THREAD, NULL);\n");
154 write_pm_most(infile, netflag);
155 f_print(fout, "\telse {\n");
156 write_rpc_svc_fg(infile, "\t\t");
157 f_print(fout, "\t}\n");
158 } else {
159 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
160 f_print(fout, "\n");
161 print_pmapunset("\t");
165 if (logflag && !inetdflag)
166 open_log_file(infile, "\t");
170 * write a registration for the given transport
172 void
173 write_netid_register(char *transp)
175 list *l;
176 definition *def;
177 version_list *vp;
178 char *sp;
179 char tmpbuf[32];
181 sp = "";
182 f_print(fout, "\n");
183 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
184 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
185 sprintf(_errbuf, "cannot find %s netid.", transp);
186 sprintf(tmpbuf, "%s\t\t", sp);
187 print_err_message(tmpbuf);
188 f_print(fout, "%s\t\texit(1);\n", sp);
189 f_print(fout, "%s\t}\n", sp);
190 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
191 sp, TRANSP);
192 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
193 sprintf(_errbuf, "cannot create %s service.", transp);
194 print_err_message(tmpbuf);
195 f_print(fout, "%s\t\texit(1);\n", sp);
196 f_print(fout, "%s\t}\n", sp);
198 for (l = defined; l != NULL; l = l->next) {
199 def = (definition *) l->val;
200 if (def->def_kind != DEF_PROGRAM)
201 continue;
202 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
203 f_print(fout, "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
204 sp, def->def_name, vp->vers_name);
205 f_print(fout, "%s\tif (!svc_reg(%s, %s, %s, ",
206 sp, TRANSP, def->def_name, vp->vers_name);
207 pvname(def->def_name, vp->vers_num);
208 f_print(fout, ", nconf)) {\n");
209 sprintf(_errbuf, "unable to register (%s, %s, %s).",
210 def->def_name, vp->vers_name, transp);
211 print_err_message(tmpbuf);
212 f_print(fout, "%s\t\texit(1);\n", sp);
213 f_print(fout, "%s\t}\n", sp);
216 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
220 * write a registration for the given transport for TLI
222 void
223 write_nettype_register(char *transp)
225 list *l;
226 definition *def;
227 version_list *vp;
229 for (l = defined; l != NULL; l = l->next) {
230 def = (definition *) l->val;
231 if (def->def_kind != DEF_PROGRAM)
232 continue;
233 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
234 f_print(fout, "\tif (!svc_create(");
235 pvname(def->def_name, vp->vers_num);
236 f_print(fout, ", %s, %s, \"%s\")) {\n",
237 def->def_name, vp->vers_name, transp);
238 sprintf(_errbuf, "unable to create (%s, %s) for %s.",
239 def->def_name, vp->vers_name, transp);
240 print_err_message("\t\t");
241 f_print(fout, "\t\texit(1);\n");
242 f_print(fout, "\t}\n");
248 * write the rest of the service
250 void
251 write_rest(void)
253 f_print(fout, "\n");
254 if (inetdflag) {
255 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
256 sprintf(_errbuf, "could not create a handle");
257 print_err_message("\t\t");
258 f_print(fout, "\t\texit(1);\n");
259 f_print(fout, "\t}\n");
260 if (timerflag) {
261 f_print(fout, "\tif (_rpcpmstart) {\n");
262 f_print(fout,
263 "\t\tsignal(SIGALRM, %s closedown);\n",
264 Cflag? "(SIG_PF)":"(void(*)())");
265 f_print(fout, "\t\talarm(_RPCSVC_CLOSEDOWN/2);\n");
266 f_print(fout, "\t}\n");
269 f_print(fout, "\tsvc_run();\n");
270 sprintf(_errbuf, "svc_run returned");
271 print_err_message("\t");
272 f_print(fout, "\texit(1);\n");
273 f_print(fout, "\t/* NOTREACHED */\n");
274 f_print(fout, "}\n");
277 void
278 write_programs(char *storage)
280 list *l;
281 definition *def;
283 /* write out stubs for procedure definitions */
284 for (l = defined; l != NULL; l = l->next) {
285 def = (definition *) l->val;
286 if (def->def_kind == DEF_PROGRAM)
287 write_real_program(def);
290 /* write out dispatcher for each program */
291 for (l = defined; l != NULL; l = l->next) {
292 def = (definition *) l->val;
293 if (def->def_kind == DEF_PROGRAM)
294 write_program(def, storage);
299 * write out definition of internal function (e.g. _printmsg_1(...))
300 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
301 * Unpacks single user argument of printmsg_1 to call-by-value format
302 * expected by printmsg_1.
304 static void
305 write_real_program(definition *def)
307 version_list *vp;
308 proc_list *proc;
309 decl_list *l;
311 if (!newstyle)
312 return; /* not needed for old style */
313 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
314 for (proc = vp->procs; proc != NULL; proc = proc->next) {
315 f_print(fout, "\n");
316 if (!mtflag)
317 internal_proctype(proc);
318 else
319 f_print(fout, "int");
320 f_print(fout, "\n_");
321 pvname(proc->proc_name, vp->vers_num);
322 if (Cflag) {
323 f_print(fout, "(");
324 /* arg name */
325 if (proc->arg_num > 1)
326 f_print(fout, proc->args.argname);
327 else
328 ptype(proc->args.decls->decl.prefix,
329 proc->args.decls->decl.type, 0);
330 if (mtflag) {
331 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
332 RESULT, RQSTP);
334 else
335 f_print(fout, " *argp, struct svc_req *%s)\n",
336 RQSTP);
338 } else {
339 if (mtflag)
340 f_print(fout, "(argp, %s, %s)\n", RESULT, RQSTP);
341 else
342 f_print(fout, "(argp, %s)\n", RQSTP);
343 /* arg name */
344 if (proc->arg_num > 1)
345 f_print(fout, "\t%s *argp;\n",
346 proc->args.argname);
347 else {
348 f_print(fout, "\t");
349 ptype(proc->args.decls->decl.prefix,
350 proc->args.decls->decl.type, 0);
351 f_print(fout, " *argp;\n");
353 if (mtflag)
354 f_print(fout, "\tvoid *%s;\n", RESULT);
355 f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
358 f_print(fout, "{\n");
359 f_print(fout, "\treturn (");
360 if (Cflag || mtflag) /* for mtflag, arguments are different */
361 pvname_svc(proc->proc_name, vp->vers_num);
362 else
363 pvname(proc->proc_name, vp->vers_num);
364 f_print(fout, "(");
365 if (proc->arg_num < 2) { /* single argument */
366 if (!streq(proc->args.decls->decl.type, "void"))
367 f_print(fout, "*argp, "); /* non-void */
368 } else {
369 for (l = proc->args.decls; l != NULL;
370 l = l->next)
371 f_print(fout, "argp->%s, ",
372 l->decl.name);
374 if (mtflag)
375 f_print(fout, "%s, ",RESULT);
376 f_print(fout, "%s));\n}\n", RQSTP);
381 static void
382 write_program(definition *def, char *storage)
384 version_list *vp;
385 proc_list *proc;
386 int filled;
388 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
389 f_print(fout, "\n");
390 if (storage != NULL)
391 f_print(fout, "%s ", storage);
392 f_print(fout, "void\n");
393 pvname(def->def_name, vp->vers_num);
395 if (Cflag) {
396 f_print(fout, "(struct svc_req *%s, ", RQSTP);
397 f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
398 } else {
399 f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
400 f_print(fout, " struct svc_req *%s;\n", RQSTP);
401 f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
404 f_print(fout, "{\n");
406 filled = 0;
407 f_print(fout, "\tunion {\n");
408 for (proc = vp->procs; proc != NULL; proc = proc->next) {
409 if (proc->arg_num < 2) { /* single argument */
410 if (streq(proc->args.decls->decl.type, "void"))
411 continue;
412 filled = 1;
413 f_print(fout, "\t\t");
414 ptype(proc->args.decls->decl.prefix,
415 proc->args.decls->decl.type, 0);
416 pvname(proc->proc_name, vp->vers_num);
417 f_print(fout, "_arg;\n");
419 } else {
420 filled = 1;
421 f_print(fout, "\t\t%s", proc->args.argname);
422 f_print(fout, " ");
423 pvname(proc->proc_name, vp->vers_num);
424 f_print(fout, "_arg;\n");
427 if (!filled)
428 f_print(fout, "\t\tint fill;\n");
429 f_print(fout, "\t} %s;\n", ARG);
431 if (mtflag) {
432 f_print(fout, "\tunion {\n");
433 for (proc = vp->procs; proc != NULL; proc = proc->next) {
434 f_print(fout, "\t\t");
435 ptype(proc->res_prefix, proc->res_type, 0);
436 pvname(proc->proc_name, vp->vers_num);
437 f_print(fout, "_res;\n");
439 f_print(fout, "\t} %s;\n", RESULT);
440 f_print(fout, "\tbool_t %s;\n", RETVAL);
442 } else {
443 f_print(fout, "\tchar *%s;\n", RESULT);
446 if (Cflag) {
447 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n",
448 ARG, RESULT);
449 if (mtflag)
450 f_print(fout,
451 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
452 ROUTINE);
453 else
454 f_print(fout,
455 "\tchar *(*%s)(char *, struct svc_req *);\n",
456 ROUTINE);
457 } else {
458 f_print(fout,
459 "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n",
460 ARG, RESULT);
461 if (mtflag)
462 f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
463 else
464 f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
466 f_print(fout, "\n");
468 if (timerflag) {
469 if (mtflag)
470 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
472 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
473 if (mtflag)
474 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
477 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
478 if (!nullproc(vp->procs)) {
479 f_print(fout, "\tcase NULLPROC:\n");
480 f_print(fout,
481 Cflag
482 ? "\t\t(void) svc_sendreply(%s,\n\t\t\t\
483 (xdrproc_t) xdr_void, (char *)NULL);\n"
484 : "\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\t\
485 (char *)NULL);\n",
486 TRANSP);
487 print_return("\t\t");
488 f_print(fout, "\n");
490 for (proc = vp->procs; proc != NULL; proc = proc->next) {
491 f_print(fout, "\tcase %s:\n", proc->proc_name);
492 if (proc->arg_num < 2) {
493 /* single argument */
494 p_xdrfunc(ARG, proc->args.decls->decl.type);
495 } else {
496 p_xdrfunc(ARG, proc->args.argname);
498 p_xdrfunc(RESULT, proc->res_type);
500 if (Cflag)
501 if (mtflag)
502 f_print(fout,
503 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
504 ROUTINE);
505 else
506 f_print(fout,
507 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
508 ROUTINE);
509 else
510 if (mtflag)
511 f_print(fout, "\t\t%s = (bool_t (*)()) ",
512 ROUTINE);
513 else
515 f_print(fout, "\t\t%s = (char *(*)()) ",
516 ROUTINE);
517 if (newstyle) {
518 /* new style: calls internal routine */
519 f_print(fout, "_");
521 if ((Cflag || mtflag) && !newstyle)
522 pvname_svc(proc->proc_name, vp->vers_num);
523 else
524 pvname(proc->proc_name, vp->vers_num);
525 f_print(fout, ";\n");
526 f_print(fout, "\t\tbreak;\n\n");
528 f_print(fout, "\tdefault:\n");
529 printerr("noproc", TRANSP);
530 print_return("\t\t");
531 f_print(fout, "\t}\n");
533 f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
534 ARG, ARG);
535 if (Cflag)
536 printif("getargs", TRANSP, "(caddr_t) &", ARG);
537 else
538 printif("getargs", TRANSP, "&", ARG);
539 printerr("decode", TRANSP);
540 print_return("\t\t");
541 f_print(fout, "\t}\n");
543 if (!mtflag)
544 if (Cflag)
545 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
546 RESULT, ROUTINE, ARG, RQSTP);
547 else
548 f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
549 RESULT, ROUTINE, ARG, RQSTP);
550 else
551 if (Cflag)
552 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
553 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
554 else
555 f_print(fout, "\t%s = (bool_t) (*%s)(&%s, &%s, %s);\n",
556 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
561 if (mtflag)
562 f_print(fout,
563 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
564 RETVAL, TRANSP, RESULT, RESULT);
565 else
566 f_print(fout,
567 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
568 RESULT, TRANSP, RESULT, RESULT);
570 printerr("systemerr", TRANSP);
571 f_print(fout, "\t}\n");
573 if (Cflag)
574 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
575 else
576 printif("freeargs", TRANSP, "&", ARG);
577 sprintf(_errbuf, "unable to free arguments");
578 print_err_message("\t\t");
579 f_print(fout, "\t\texit(1);\n");
580 f_print(fout, "\t}\n");
581 /* print out free routine */
582 if (mtflag) {
583 f_print(fout,"\tif (!");
584 pvname(def->def_name, vp->vers_num);
585 f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
586 TRANSP, RESULT, RESULT);
587 sprintf(_errbuf, "unable to free results");
588 print_err_message("\t\t");
589 f_print(fout, "\n");
591 print_return("\t");
592 f_print(fout, "}\n");
596 static void
597 printerr(char *err, char *transp)
599 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
602 static void
603 printif(char *proc, char *transp, char *prefix, char *arg)
605 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
606 proc, transp, arg, prefix, arg);
610 nullproc(proc_list *proc)
612 for (; proc != NULL; proc = proc->next) {
613 if (streq(proc->proc_num, "0"))
614 return(1);
616 return(0);
619 static void
620 write_inetmost(char *infile)
622 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
623 f_print(fout, "\tint sock;\n");
624 f_print(fout, "\tint proto;\n");
625 f_print(fout, "\tstruct sockaddr_in saddr;\n");
626 f_print(fout, "\tint asize = sizeof (saddr);\n");
627 f_print(fout, "\n");
628 f_print(fout, "\tif (getsockname(0, "
629 "(struct sockaddr *)&saddr, &asize) == 0) {\n");
630 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
631 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
632 f_print(fout, "\t\t\texit(1);\n");
633 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
634 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
635 f_print(fout, "\t\t\texit(1);\n");
636 f_print(fout, "\t\tsock = 0;\n");
637 f_print(fout, "\t\t_rpcpmstart = 1;\n");
638 f_print(fout, "\t\tproto = 0;\n");
639 open_log_file(infile, "\t\t");
640 f_print(fout, "\t} else {\n");
641 write_rpc_svc_fg(infile, "\t\t");
642 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
643 print_pmapunset("\t\t");
644 f_print(fout, "\t}\n");
647 static void
648 print_return(space)
649 char *space;
651 if (exitnow)
652 f_print(fout, "%sexit(0);\n", space);
653 else {
654 if (timerflag) {
655 if (mtflag)
656 f_print(fout, "%smutex_lock(&_svcstate_lock);\n", space);
657 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
658 if (mtflag)
659 f_print(fout, "%smutex_unlock(&_svcstate_lock);\n", space);
661 f_print(fout, "%sreturn;\n", space);
665 static void
666 print_pmapunset(char *space)
668 list *l;
669 definition *def;
670 version_list *vp;
672 for (l = defined; l != NULL; l = l->next) {
673 def = (definition *) l->val;
674 if (def->def_kind == DEF_PROGRAM) {
675 for (vp = def->def.pr.versions; vp != NULL;
676 vp = vp->next) {
677 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
678 space, def->def_name, vp->vers_name);
684 static void
685 print_err_message(char *space)
687 if (logflag)
688 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
689 else if (inetdflag || pmflag)
690 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
691 else
692 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
696 * Write the server auxiliary function (_msgout, timeout)
698 void
699 write_svc_aux(int nomain)
701 if (!logflag)
702 write_msg_out();
703 if (!nomain)
704 write_timeout_func();
708 * Write the _msgout function
711 static void
712 write_msg_out(void)
714 f_print(fout, "\n");
716 * Avoid making _msgout() static -- it's useful to have it visible
717 * in the toplevel RPC server code.
719 f_print(fout, "static\n");
721 if (!Cflag) {
722 f_print(fout, "void _msgout(msg)\n");
723 f_print(fout, "\tchar *msg;\n");
724 } else {
725 f_print(fout, "void _msgout(char* msg)\n");
727 f_print(fout, "{\n");
728 f_print(fout, "#ifdef RPC_SVC_FG\n");
729 if (inetdflag || pmflag)
730 f_print(fout, "\tif (_rpcpmstart)\n");
731 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
732 f_print(fout, "\telse\n");
733 f_print(fout,
734 "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
735 f_print(fout, "#else\n");
736 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
737 f_print(fout, "#endif\n");
738 f_print(fout, "}\n");
742 * Write the timeout function
744 static void
745 write_timeout_func(void)
747 if (!timerflag)
748 return;
750 f_print(fout, "\n");
751 f_print(fout, "static void\n");
752 if (!Cflag) {
753 f_print(fout, "closedown(sig)\n");
754 f_print(fout, "\tint sig;\n");
755 } else {
756 f_print(fout, "closedown(int sig)\n");
758 f_print(fout, "{\n");
759 if (mtflag)
760 f_print(fout, "\tmutex_lock(&_svcstate_lock);\n");
761 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
762 f_print(fout, "\t\textern fd_set svc_fdset;\n");
763 f_print(fout, "\t\tstatic int size;\n");
764 f_print(fout, "\t\tint i, openfd;\n");
765 if (tirpcflag && pmflag) {
766 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
767 f_print(fout,
768 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
769 } else {
770 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
772 f_print(fout, "\t\t\texit(0);\n");
773 f_print(fout, "\t\tif (size == 0) {\n");
774 if (tirpcflag) {
775 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
776 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
777 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
778 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
780 if (mtflag)
781 f_print(fout, "\t\t\t\tmutex_unlock(&_svcstate_lock);\n");
783 f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n");
784 } else {
785 f_print(fout, "\t\t\tsize = getdtablesize();\n");
787 f_print(fout, "\t\t}\n");
788 f_print(fout,
789 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
790 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
791 f_print(fout, "\t\t\t\topenfd++;\n");
792 f_print(fout, "\t\tif (openfd <= 1)\n");
793 f_print(fout, "\t\t\texit(0);\n");
794 f_print(fout, "\t}\n");
795 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
796 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
797 if (mtflag)
798 f_print(fout, "\tmutex_unlock(&_svcstate_lock);\n");
800 f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
801 Cflag? "(SIG_PF)" : "(void(*)())");
802 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
803 f_print(fout, "}\n");
808 * Write the most of port monitor support
810 static void
811 write_pm_most(char *infile, int netflag)
813 list *l;
814 definition *def;
815 version_list *vp;
817 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
818 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
819 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
820 f_print(fout, "\t\tchar *netid;\n");
821 if (!netflag) { /* Not included by -n option */
822 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
823 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
825 if (timerflag)
826 f_print(fout, "\t\tint pmclose;\n");
828 * Not necessary, defined in /usr/include/stdlib
829 * f_print(fout, "\t\textern char *getenv();\n");
831 f_print(fout, "\n");
832 f_print(fout, "\t\t_rpcpmstart = 1;\n");
833 open_log_file(infile, "\t\t");
834 f_print(fout, "\n\t\tif ((netid = \
835 getenv(\"NLSPROVIDER\")) == NULL) {\n");
837 if (timerflag) {
838 f_print(fout, "\t\t/* started from inetd */\n");
839 f_print(fout, "\t\t\tpmclose = 1;\n");
841 f_print(fout,
842 "\t\t} else {\n");
843 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
844 sprintf(_errbuf, "cannot get transport info");
845 print_err_message("\t\t\t\t");
846 if (timerflag)
847 f_print(fout, "\n\t\t\tpmclose = \
848 (t_getstate(0) != T_DATAXFER);\n");
849 f_print(fout, "\t\t}\n");
851 * A kludgy support for inetd services. Inetd only works with
852 * sockmod, and RPC works only with timod, hence all this jugglery
854 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
855 f_print(fout,
856 "\t\t\tif (ioctl(0, I_POP, 0) || \
857 ioctl(0, I_PUSH, \"timod\")) {\n");
858 sprintf(_errbuf, "could not get the right module");
859 print_err_message("\t\t\t\t");
860 f_print(fout, "\t\t\t\texit(1);\n");
861 f_print(fout, "\t\t\t}\n");
862 f_print(fout, "\t\t}\n");
863 f_print(fout,
864 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
865 == NULL) {\n",
866 TRANSP);
867 sprintf(_errbuf, "cannot create server handle");
868 print_err_message("\t\t\t");
869 f_print(fout, "\t\t\texit(1);\n");
870 f_print(fout, "\t\t}\n");
871 f_print(fout, "\t\tif (nconf)\n");
872 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
873 for (l = defined; l != NULL; l = l->next) {
874 def = (definition *) l->val;
875 if (def->def_kind != DEF_PROGRAM)
876 continue;
877 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
878 f_print(fout, "\t\tif (!svc_reg(%s, %s, %s, ",
879 TRANSP, def->def_name, vp->vers_name);
880 pvname(def->def_name, vp->vers_num);
881 f_print(fout, ", 0)) {\n");
882 sprintf(_errbuf, "unable to register (%s, %s).",
883 def->def_name, vp->vers_name);
884 print_err_message("\t\t\t");
885 f_print(fout, "\t\t\texit(1);\n");
886 f_print(fout, "\t\t}\n");
889 if (timerflag) {
890 f_print(fout, "\t\tif (pmclose) {\n");
891 f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
892 Cflag? "(SIG_PF)" : "(void(*)())");
893 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
894 f_print(fout, "\t\t}\n");
896 f_print(fout, "\t\tsvc_run();\n");
897 f_print(fout, "\t\texit(1);\n");
898 f_print(fout, "\t\t/* NOTREACHED */\n");
899 f_print(fout, "\t}");
903 * Support for backgrounding the server if self started.
905 static void
906 write_rpc_svc_fg(char *infile, char *sp)
908 f_print(fout, "#ifndef RPC_SVC_FG\n");
909 f_print(fout, "%sint size;\n", sp);
910 if (tirpcflag)
911 f_print(fout, "%sstruct rlimit rl;\n", sp);
912 if (inetdflag)
913 f_print(fout, "%sint pid, i;\n\n", sp);
914 f_print(fout, "%spid = fork();\n", sp);
915 f_print(fout, "%sif (pid < 0) {\n", sp);
916 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
917 f_print(fout, "%s\texit(1);\n", sp);
918 f_print(fout, "%s}\n", sp);
919 f_print(fout, "%sif (pid)\n", sp);
920 f_print(fout, "%s\texit(0);\n", sp);
921 /* get number of file descriptors */
922 if (tirpcflag) {
923 f_print(fout, "%srl.rlim_max = 0;\n", sp);
924 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
925 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
926 f_print(fout, "%s\texit(1);\n", sp);
927 } else {
928 f_print(fout, "%ssize = getdtablesize();\n", sp);
931 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
932 f_print(fout, "%s\t(void) close(i);\n", sp);
933 /* Redirect stderr and stdout to console */
934 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
935 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
936 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
937 /* This removes control of the controlling terminal */
938 if (tirpcflag) {
939 f_print(fout, "%ssetsid();\n", sp);
940 } else {
941 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
942 f_print(fout, "%sif (i >= 0) {\n", sp);
943 f_print(fout,
944 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
945 f_print(fout, "%s\t(void) close(i);\n", sp);
946 f_print(fout, "%s}\n", sp);
948 if (!logflag)
949 open_log_file(infile, sp);
950 f_print(fout, "#endif\n");
951 if (logflag)
952 open_log_file(infile, sp);
955 static void
956 open_log_file(char *infile, char *sp)
958 char *s;
960 s = strrchr(infile, '.');
961 if (s)
962 *s = '\0';
963 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
964 if (s)
965 *s = '.';
969 * write a registration for the given transport for Inetd
971 void
972 write_inetd_register(char *transp)
974 list *l;
975 definition *def;
976 version_list *vp;
977 char *sp;
978 int isudp;
979 char tmpbuf[32];
981 if (inetdflag)
982 sp = "\t";
983 else
984 sp = "";
985 if (streq(transp, "udp"))
986 isudp = 1;
987 else
988 isudp = 0;
989 f_print(fout, "\n");
990 if (inetdflag) {
991 f_print(fout,
992 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
993 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
995 f_print(fout, "%s\t%s = svc%s_create(%s",
996 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
997 if (!isudp)
998 f_print(fout, ", 0, 0");
999 f_print(fout, ");\n");
1000 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1001 sprintf(_errbuf, "cannot create %s service.", transp);
1002 sprintf(tmpbuf, "%s\t\t", sp);
1003 print_err_message(tmpbuf);
1004 f_print(fout, "%s\t\texit(1);\n", sp);
1005 f_print(fout, "%s\t}\n", sp);
1007 if (inetdflag) {
1008 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1009 f_print(fout, "%s\tproto = IPPROTO_%s;\n", sp,
1010 isudp ? "UDP": "TCP");
1012 for (l = defined; l != NULL; l = l->next) {
1013 def = (definition *) l->val;
1014 if (def->def_kind != DEF_PROGRAM)
1015 continue;
1016 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1017 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
1018 sp, TRANSP, def->def_name, vp->vers_name);
1019 pvname(def->def_name, vp->vers_num);
1020 if (inetdflag)
1021 f_print(fout, ", proto)) {\n");
1022 else
1023 f_print(fout, ", IPPROTO_%s)) {\n",
1024 isudp ? "UDP": "TCP");
1025 sprintf(_errbuf, "unable to register (%s, %s, %s).",
1026 def->def_name, vp->vers_name, transp);
1027 print_err_message(tmpbuf);
1028 f_print(fout, "%s\t\texit(1);\n", sp);
1029 f_print(fout, "%s\t}\n", sp);
1032 if (inetdflag)
1033 f_print(fout, "\t}\n");