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.
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.
27 * Mountain View, California 94043
32 #ident "@(#)rpc_svcout.c 1.4 90/04/13 SMI"
33 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
41 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
42 * Copyright (C) 1987, Sun Microsystems, Inc.
46 #include "rpc_parse.h"
50 static char RQSTP[] = "rqstp";
51 static char TRANSP[] = "transp";
52 static char ARG[] = "argument";
53 static char RESULT[] = "result";
54 static char ROUTINE[] = "local";
55 static char RETVAL[] = "retval";
57 static char _errbuf[256]; /* For all messages */
59 void internal_proctype( proc_list * );
60 static void write_real_program( definition * );
61 static void write_program(definition *, const char *);
62 static void printerr(const char *, const char *);
63 static void printif(const char *, const char *, const char *, const char *);
64 static void write_inetmost(const char *);
65 static void print_return(const char *);
66 static void print_pmapunset(const char *);
67 static void print_err_message(const char *);
68 static void write_timeout_func( void );
69 static void write_pm_most(const char *, int);
70 static void write_rpc_svc_fg(const char *, const char *);
71 static void open_log_file(const char *, const char *);
72 static void write_msg_out( void );
76 p_xdrfunc(const char *rname, const char *typename)
78 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
79 rname, stringfix(typename));
83 internal_proctype(proc_list *plist)
85 f_print(fout, "static ");
86 ptype(plist->res_prefix, plist->res_type, 1);
92 * write most of the service, that is, everything but the registrations.
95 write_most(const char *infile, int netflag, int nomain)
97 if (inetdflag || pmflag) {
99 var_type = (nomain? "extern" : "static");
100 f_print(fout, "%s int _rpcpmstart;", var_type);
101 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
102 if (!tirpcflag || tirpc_socket) {
103 f_print(fout, "%s int _rpcfdtype;", var_type);
104 f_print(fout, "\n\t\t /* Whether Stream or \
109 f_print(fout, " /* States a server can be in \
110 wrt request */\n\n");
111 f_print(fout, "#define\t_IDLE 0\n");
112 f_print(fout, "#define\t_SERVED 1\n");
113 f_print(fout, "#define\t_SERVING 2\n\n");
114 f_print(fout, "static int _rpcsvcstate = _IDLE;");
115 f_print(fout, "\t /* Set when a request is \
119 f_print(fout, "pthread_mutex_t _svcstate_lock;");
120 f_print(fout, "\t\t\t/* Mutex lock for variable _rpcsvcstate */\n");
126 write_svc_aux(nomain);
128 /* write out dispatcher and stubs */
129 write_programs((char *)NULL);
134 f_print(fout, "\nint\n");
135 f_print(fout, "main()\n");
136 f_print(fout, "{\n");
138 write_inetmost(infile);
139 /* Includes call to write_rpc_svc_fg() */
144 "\tregister SVCXPRT *%s;\n", TRANSP);
146 "\tstruct netconfig *nconf = NULL;\n");
148 f_print(fout, "\tpid_t pid;\n");
149 f_print(fout, "\tint i;\n");
152 f_print(fout, "\tstruct sockaddr_storage saddr;\n");
153 f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n\n");
155 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
158 if (mtflag & timerflag)
159 f_print(fout, "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
161 write_pm_most(infile, netflag);
162 f_print(fout, "\telse {\n");
163 write_rpc_svc_fg(infile, "\t\t");
164 f_print(fout, "\t}\n");
166 write_rpc_svc_fg(infile, "\t\t");
169 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
171 print_pmapunset("\t");
175 if (logflag && !inetdflag) {
176 open_log_file(infile, "\t");
181 * write a registration for the given transport
184 write_netid_register(const char *transp)
194 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
195 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
196 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
197 sprintf(tmpbuf, "%s\t\t", sp);
198 print_err_message(tmpbuf);
199 f_print(fout, "%s\t\texit(1);\n", sp);
200 f_print(fout, "%s\t}\n", sp);
202 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, ",
204 f_print(fout,"nconf, 0, RPC_MAXDATASIZE, RPC_MAXDATASIZE);\n");
207 "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
210 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
211 (void) sprintf(_errbuf, "cannot create %s service.", transp);
212 print_err_message(tmpbuf);
213 f_print(fout, "%s\t\texit(1);\n", sp);
214 f_print(fout, "%s\t}\n", sp);
216 for (l = defined; l != NULL; l = l->next) {
217 def = (definition *) l->val;
218 if (def->def_kind != DEF_PROGRAM) {
221 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
223 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
224 sp, def->def_name, vp->vers_name);
226 "%s\tif (!svc_reg(%s, %s, %s, ",
227 sp, TRANSP, def->def_name, vp->vers_name);
228 pvname(def->def_name, vp->vers_num);
229 f_print(fout, ", nconf)) {\n");
230 (void) sprintf(_errbuf,
231 "unable to register (%s, %s, %s).",
232 def->def_name, vp->vers_name, transp);
233 print_err_message(tmpbuf);
234 f_print(fout, "%s\t\texit(1);\n", sp);
235 f_print(fout, "%s\t}\n", sp);
238 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
242 * write a registration for the given transport for TLI
245 write_nettype_register(const char *transp)
251 for (l = defined; l != NULL; l = l->next) {
252 def = (definition *) l->val;
253 if (def->def_kind != DEF_PROGRAM) {
256 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
257 f_print(fout, "\tif (!svc_create(");
258 pvname(def->def_name, vp->vers_num);
259 f_print(fout, ", %s, %s, \"%s\")) {\n",
260 def->def_name, vp->vers_name, transp);
261 (void) sprintf(_errbuf,
262 "unable to create (%s, %s) for %s.",
263 def->def_name, vp->vers_name, transp);
264 print_err_message("\t\t");
265 f_print(fout, "\t\texit(1);\n");
266 f_print(fout, "\t}\n");
272 * write the rest of the service
279 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
280 (void) sprintf(_errbuf, "could not create a handle");
281 print_err_message("\t\t");
282 f_print(fout, "\t\texit(1);\n");
283 f_print(fout, "\t}\n");
285 f_print(fout, "\tif (_rpcpmstart) {\n");
287 "\t\t(void) signal(SIGALRM, closedown);\n");
288 f_print(fout, "\t\t(void) \
289 alarm(_RPCSVC_CLOSEDOWN/2);\n");
290 f_print(fout, "\t}\n");
293 f_print(fout, "\tsvc_run();\n");
294 (void) sprintf(_errbuf, "svc_run returned");
295 print_err_message("\t");
296 f_print(fout, "\texit(1);\n");
297 f_print(fout, "\t/* NOTREACHED */\n");
298 f_print(fout, "}\n");
302 write_programs(const char *storage)
307 /* write out stubs for procedure definitions */
308 for (l = defined; l != NULL; l = l->next) {
309 def = (definition *) l->val;
310 if (def->def_kind == DEF_PROGRAM) {
311 write_real_program(def);
315 /* write out dispatcher for each program */
316 for (l = defined; l != NULL; l = l->next) {
317 def = (definition *) l->val;
318 if (def->def_kind == DEF_PROGRAM) {
319 write_program(def, storage);
327 * write out definition of internal function (e.g. _printmsg_1(...))
328 * which calls server's definition 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.
333 write_real_program(definition *def)
339 if (!newstyle) return; /* not needed for old style */
340 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
341 for (proc = vp->procs; proc != NULL; proc = proc->next) {
344 internal_proctype(proc);
346 f_print(fout, "int");
347 f_print(fout, "\n_");
348 pvname(proc->proc_name, vp->vers_num);
351 if (proc->arg_num > 1)
352 fputs(proc->args.argname, fout);
354 ptype(proc->args.decls->decl.prefix,
355 proc->args.decls->decl.type, 0);
357 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
363 f_print(fout, " *argp, struct svc_req *%s)\n",
366 f_print(fout, "{\n");
367 f_print(fout, "\treturn (");
368 pvname_svc(proc->proc_name, vp->vers_num);
370 if (proc->arg_num < 2) { /* single argument */
371 if (!streq(proc->args.decls->decl.type, "void"))
372 f_print(fout, "*argp, "); /* non-void */
374 for (l = proc->args.decls; l != NULL;
376 f_print(fout, "argp->%s, ",
380 f_print(fout, "%s, ",RESULT);
381 f_print(fout, "%s));\n}\n", RQSTP);
387 write_program(definition *def, const char *storage)
393 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
395 if (storage != NULL) {
396 f_print(fout, "%s ", storage);
398 f_print(fout, "void\n");
399 pvname(def->def_name, vp->vers_num);
401 f_print(fout, "(struct svc_req *%s, ", RQSTP);
402 f_print(fout, "SVCXPRT *%s)\n", TRANSP);
403 f_print(fout, "{\n");
406 f_print(fout, "\tunion {\n");
407 for (proc = vp->procs; proc != NULL; proc = proc->next) {
408 if (proc->arg_num < 2) { /* single argument */
409 if (streq(proc->args.decls->decl.type,
414 f_print(fout, "\t\t");
415 ptype(proc->args.decls->decl.prefix,
416 proc->args.decls->decl.type, 0);
417 pvname(proc->proc_name, vp->vers_num);
418 f_print(fout, "_arg;\n");
422 f_print(fout, "\t\t%s", proc->args.argname);
424 pvname(proc->proc_name, vp->vers_num);
425 f_print(fout, "_arg;\n");
429 f_print(fout, "\t\tint fill;\n");
431 f_print(fout, "\t} %s;\n", ARG);
434 f_print(fout, "\tunion {\n");
435 for (proc = vp->procs; proc != NULL; proc = proc->next) {
436 if (streq(proc->res_type, "void")) {
439 f_print(fout, "\t\t");
440 ptype(proc->res_prefix, proc->res_type, 0);
441 pvname(proc->proc_name, vp->vers_num);
442 f_print(fout, "_res;\n");
444 f_print(fout, "\t} %s;\n", RESULT);
445 f_print(fout, "\tbool_t %s;\n", RETVAL);
448 f_print(fout, "\tchar *%s;\n", RESULT);
450 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
453 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
457 "\tchar *(*%s)(char *, struct svc_req *);\n",
463 f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n");
465 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
467 f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
470 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
471 if (!nullproc(vp->procs)) {
472 f_print(fout, "\tcase NULLPROC:\n");
474 "\t\t(void) svc_sendreply(%s,\n\t\t\t"
475 "(xdrproc_t) xdr_void, (char *)NULL);\n",
477 print_return("\t\t");
480 for (proc = vp->procs; proc != NULL; proc = proc->next) {
481 f_print(fout, "\tcase %s:\n", proc->proc_name);
482 if (proc->arg_num < 2) { /* single argument */
483 p_xdrfunc(ARG, proc->args.decls->decl.type);
485 p_xdrfunc(ARG, proc->args.argname);
487 p_xdrfunc(RESULT, proc->res_type);
491 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
495 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
497 if (newstyle) { /* new style: calls internal routine */
501 pvname_svc(proc->proc_name, vp->vers_num);
503 pvname(proc->proc_name, vp->vers_num);
504 f_print(fout, ";\n");
505 f_print(fout, "\t\tbreak;\n\n");
507 f_print(fout, "\tdefault:\n");
508 printerr("noproc", TRANSP);
509 print_return("\t\t");
510 f_print(fout, "\t}\n");
513 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
515 printif("getargs", TRANSP, "(caddr_t) &", ARG);
516 printerr("decode", TRANSP);
517 print_return("\t\t");
518 f_print(fout, "\t}\n");
521 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
522 RESULT, ROUTINE, ARG, RQSTP);
524 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
525 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
530 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
531 RETVAL, TRANSP, RESULT, RESULT);
534 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
535 RESULT, TRANSP, RESULT, RESULT);
537 printerr("systemerr", TRANSP);
538 f_print(fout, "\t}\n");
540 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
541 (void) sprintf(_errbuf, "unable to free arguments");
542 print_err_message("\t\t");
543 f_print(fout, "\t\texit(1);\n");
544 f_print(fout, "\t}\n");
545 /* print out free routine */
547 f_print(fout,"\tif (!");
548 pvname(def->def_name, vp->vers_num);
549 f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
550 TRANSP, RESULT, RESULT);
551 (void) sprintf(_errbuf, "unable to free results");
552 print_err_message("\t\t");
556 f_print(fout, "}\n");
561 printerr(const char *err, const char *transp)
563 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
567 printif(const char *proc, const char *transp, const char *prefix,
570 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n",
571 proc, transp, arg, prefix, arg);
575 nullproc(proc_list *proc)
577 for (; proc != NULL; proc = proc->next) {
578 if (streq(proc->proc_num, "0")) {
586 write_inetmost(const char *infile)
588 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
589 f_print(fout, "\tint sock;\n");
590 f_print(fout, "\tint proto;\n");
591 f_print(fout, "\tstruct sockaddr_in saddr;\n");
592 f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n");
595 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
596 f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n\n");
597 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
598 f_print(fout, "\t\t\texit(1);\n");
599 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
600 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
601 f_print(fout, "\t\t\texit(1);\n");
602 f_print(fout, "\t\tsock = 0;\n");
603 f_print(fout, "\t\t_rpcpmstart = 1;\n");
604 f_print(fout, "\t\tproto = 0;\n");
605 open_log_file(infile, "\t\t");
606 f_print(fout, "\t} else {\n");
607 write_rpc_svc_fg(infile, "\t\t");
608 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
609 print_pmapunset("\t\t");
610 f_print(fout, "\t}\n");
614 print_return(const char *space)
617 f_print(fout, "%sexit(0);\n", space);
621 f_print(fout, "%spthread_mutex_lock(&_svcstate_lock);\n", space);
622 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
624 f_print(fout, "%spthread_mutex_unlock(&_svcstate_lock);\n", space);
626 f_print(fout, "%sreturn;\n", space);
631 print_pmapunset(const char *space)
637 for (l = defined; l != NULL; l = l->next) {
638 def = (definition *) l->val;
639 if (def->def_kind == DEF_PROGRAM) {
640 for (vp = def->def.pr.versions; vp != NULL;
642 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
643 space, def->def_name, vp->vers_name);
650 print_err_message(const char *space)
653 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
654 else if (inetdflag || pmflag)
655 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
657 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
661 * Write the server auxiliary function (_msgout, timeout)
664 write_svc_aux(int nomain)
669 write_timeout_func();
673 * Write the _msgout function
681 * Avoid making _msgout() static -- it's useful to have it visible
682 * in the toplevel RPC server code.
684 f_print(fout, "static\n");
685 f_print(fout, "void _msgout(const char* msg)\n");
686 f_print(fout, "{\n");
687 f_print(fout, "#ifdef RPC_SVC_FG\n");
688 if (inetdflag || pmflag)
689 f_print(fout, "\tif (_rpcpmstart)\n");
690 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
691 f_print(fout, "\telse\n");
693 "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
694 f_print(fout, "#else\n");
695 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
696 f_print(fout, "#endif\n");
697 f_print(fout, "}\n");
701 * Write the timeout function
704 write_timeout_func(void)
710 f_print(fout, "static void\n");
711 f_print(fout, "closedown(int sig)\n");
712 f_print(fout, "{\n");
714 f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n");
715 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
716 f_print(fout, "\t\textern fd_set svc_fdset;\n");
717 f_print(fout, "\t\tstatic int size;\n");
718 f_print(fout, "\t\tint i, openfd;\n");
719 if (tirpcflag && pmflag) {
720 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
722 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
724 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
726 f_print(fout, "\t\t\texit(0);\n");
727 f_print(fout, "\t\tif (size == 0) {\n");
729 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
730 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
731 f_print(fout, "\t\t\tif (getrlimit(RLIMIT_NOFILE, &rl) == -1)\n");
732 f_print(fout, "\t\t\t\treturn;\n");
733 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
736 f_print(fout, "\t\t\t\tpthread_mutex_unlock(&_svcstate_lock);\n");
738 f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n");
740 f_print(fout, "\t\t\tsize = getdtablesize();\n");
742 f_print(fout, "\t\t}\n");
744 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
745 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
746 f_print(fout, "\t\t\t\topenfd++;\n");
747 f_print(fout, "\t\tif (openfd <= 1)\n");
748 f_print(fout, "\t\t\texit(0);\n");
749 f_print(fout, "\t}\n");
750 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
751 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
753 f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
755 f_print(fout, "\t(void) signal(SIGALRM, closedown);\n");
756 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
757 f_print(fout, "}\n");
762 * Write the most of port monitor support
765 write_pm_most(const char *infile, int netflag)
773 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
774 f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n");
776 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
777 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
778 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
780 f_print(fout, "\t\tchar *netid;\n");
781 if (!netflag) { /* Not included by -n option */
782 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
783 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
786 f_print(fout, "\t\tint pmclose;\n");
788 * Not necessary, defined in /usr/include/stdlib
789 * f_print(fout, "\t\textern char *getenv();\n");
793 f_print(fout, "\t\tif (saddr.ss_family != AF_INET &&\n");
794 f_print(fout, "\t\t saddr.ss_family != AF_INET6)\n");
795 f_print(fout, "\t\t\texit(1);\n");
796 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
797 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
798 f_print(fout, "\t\t\texit(1);\n");
800 f_print(fout, "\t\t_rpcpmstart = 1;\n");
801 open_log_file(infile, "\t\t");
802 f_print(fout, "\n\t\tif ((netid = \
803 getenv(\"NLSPROVIDER\")) == NULL) {\n");
806 f_print(fout, "\t\t/* started from inetd */\n");
807 f_print(fout, "\t\t\tpmclose = 1;\n");
811 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
812 sprintf(_errbuf, "cannot get transport info");
813 print_err_message("\t\t\t\t");
816 f_print(fout, "\n\t\t\tpmclose = 1;\t/* XXX */\n");
819 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
821 f_print(fout, "\t\t}\n");
823 * A kludgy support for inetd services. Inetd only works with
824 * sockmod, and RPC works only with timod, hence all this jugglery
827 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
828 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ");
829 f_print(fout, "ioctl(0, I_PUSH, \"timod\")) {\n");
830 sprintf(_errbuf, "could not get the right module");
831 print_err_message("\t\t\t\t");
832 f_print(fout, "\t\t\t\texit(1);\n");
833 f_print(fout, "\t\t\t}\n");
834 f_print(fout, "\t\t}\n");
838 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, \
839 RPC_MAXDATASIZE, RPC_MAXDATASIZE)) \
844 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
848 sprintf(_errbuf, "cannot create server handle");
849 print_err_message("\t\t\t");
850 f_print(fout, "\t\t\texit(1);\n");
851 f_print(fout, "\t\t}\n");
852 f_print(fout, "\t\tif (nconf)\n");
853 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
854 for (l = defined; l != NULL; l = l->next) {
855 def = (definition *) l->val;
856 if (def->def_kind != DEF_PROGRAM) {
859 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
861 "\t\tif (!svc_reg(%s, %s, %s, ",
862 TRANSP, def->def_name, vp->vers_name);
863 pvname(def->def_name, vp->vers_num);
864 f_print(fout, ", 0)) {\n");
865 (void) sprintf(_errbuf, "unable to register (%s, %s).",
866 def->def_name, vp->vers_name);
867 print_err_message("\t\t\t");
868 f_print(fout, "\t\t\texit(1);\n");
869 f_print(fout, "\t\t}\n");
873 f_print(fout, "\t\tif (pmclose) {\n");
874 f_print(fout, "\t\t\t(void) signal(SIGALRM, closedown);\n");
875 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
876 f_print(fout, "\t\t}\n");
878 f_print(fout, "\t\tsvc_run();\n");
879 f_print(fout, "\t\texit(1);\n");
880 f_print(fout, "\t\t/* NOTREACHED */\n");
881 f_print(fout, "\t}");
885 * Support for backgrounding the server if self started.
888 write_rpc_svc_fg(const char *infile, const char *sp)
890 f_print(fout, "#ifndef RPC_SVC_FG\n");
891 f_print(fout, "%sint size;\n", sp);
893 f_print(fout, "%sstruct rlimit rl;\n", sp);
895 f_print(fout, "%sint pid, i;\n\n", sp);
896 f_print(fout, "%spid = fork();\n", sp);
897 f_print(fout, "%sif (pid < 0) {\n", sp);
898 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
899 f_print(fout, "%s\texit(1);\n", sp);
900 f_print(fout, "%s}\n", sp);
901 f_print(fout, "%sif (pid)\n", sp);
902 f_print(fout, "%s\texit(0);\n", sp);
903 /* get number of file descriptors */
905 f_print(fout, "%srl.rlim_max = 0;\n", sp);
906 f_print(fout, "%sif (getrlimit(RLIMIT_NOFILE, &rl) == -1) {\n",
908 f_print(fout, "%s\tperror(\"getrlimit\");\n", sp);
909 f_print(fout, "%s\texit(1);\n", sp);
910 f_print(fout, "%s}\n", sp);
911 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
912 f_print(fout, "%s\texit(1);\n", sp);
914 f_print(fout, "%ssize = getdtablesize();\n", sp);
917 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
918 f_print(fout, "%s\t(void) close(i);\n", sp);
919 /* Redirect stderr and stdout to console */
920 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
921 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
922 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
923 /* This removes control of the controlling terminal */
925 f_print(fout, "%ssetsid();\n", sp);
927 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
928 f_print(fout, "%sif (i >= 0) {\n", sp);
930 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
931 f_print(fout, "%s\t(void) close(i);\n", sp);
932 f_print(fout, "%s}\n", sp);
935 open_log_file(infile, sp);
936 f_print(fout, "#endif\n");
938 open_log_file(infile, sp);
942 open_log_file(const char *infile, const char *sp)
946 s = strrchr(infile, '.');
949 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
958 * write a registration for the given transport for Inetd
961 write_inetd_register(const char *transp)
974 if (streq(transp, "udp"))
981 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
982 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
984 f_print(fout, "%s\t%s = svc%s_create(%s",
985 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
987 f_print(fout, ", 0, 0");
988 f_print(fout, ");\n");
989 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
990 (void) sprintf(_errbuf, "cannot create %s service.", transp);
991 (void) sprintf(tmpbuf, "%s\t\t", sp);
992 print_err_message(tmpbuf);
993 f_print(fout, "%s\t\texit(1);\n", sp);
994 f_print(fout, "%s\t}\n", sp);
997 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
998 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
999 sp, isudp ? "UDP": "TCP");
1001 for (l = defined; l != NULL; l = l->next) {
1002 def = (definition *) l->val;
1003 if (def->def_kind != DEF_PROGRAM) {
1006 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1007 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
1008 sp, TRANSP, def->def_name, vp->vers_name);
1009 pvname(def->def_name, vp->vers_num);
1011 f_print(fout, ", proto)) {\n");
1013 f_print(fout, ", IPPROTO_%s)) {\n",
1014 isudp ? "UDP": "TCP");
1015 (void) sprintf(_errbuf,
1016 "unable to register (%s, %s, %s).",
1017 def->def_name, vp->vers_name, transp);
1018 print_err_message(tmpbuf);
1019 f_print(fout, "%s\t\texit(1);\n", sp);
1020 f_print(fout, "%s\t}\n", sp);
1024 f_print(fout, "\t}\n");