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
29 * @(#)rpc_svcout.c 1.4 90/04/13 SMI; 1.29 89/03/30 (C) 1987 SMI
30 * $FreeBSD: src/usr.bin/rpcgen/rpc_svcout.c,v 1.26 2008/08/12 13:38:06 dfr Exp $
31 * $DragonFly: src/usr.bin/rpcgen/rpc_svcout.c,v 1.5 2008/10/16 01:52:33 swildner Exp $
35 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
36 * Copyright (C) 1987, Sun Microsystems, Inc.
40 #include "rpc_parse.h"
44 static char RQSTP
[] = "rqstp";
45 static char TRANSP
[] = "transp";
46 static char ARG
[] = "argument";
47 static char RESULT
[] = "result";
48 static char ROUTINE
[] = "local";
49 static char RETVAL
[] = "retval";
51 char _errbuf
[256]; /* For all messages */
53 void internal_proctype(proc_list
*);
54 static void write_real_program(definition
*);
55 static void write_program(definition
*, const char *);
56 static void printerr(const char *, const char *);
57 static void printif(const char *, const char *, const char *, const char *);
58 static void write_inetmost(const char *);
59 static void print_return(const char *);
60 static void print_pmapunset(const char *);
61 static void print_err_message(const char *);
62 static void write_timeout_func(void);
63 static void write_pm_most(const char *, int);
64 static void write_rpc_svc_fg(const char *, const char *);
65 static void open_log_file(const char *, const char *);
66 static void write_msg_out(void);
70 p_xdrfunc(const char *rname
, const char *typename
)
72 f_print(fout
, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
73 rname
, stringfix(typename
));
77 internal_proctype(proc_list
*plist
)
79 f_print(fout
, "static ");
80 ptype(plist
->res_prefix
, plist
->res_type
, 1);
86 * write most of the service, that is, everything but the registrations.
89 write_most(const char *infile
, int netflag
, int nomain
)
91 if (inetdflag
|| pmflag
) {
93 var_type
= (nomain
? "extern" : "static");
94 f_print(fout
, "%s int _rpcpmstart;", var_type
);
95 f_print(fout
, "\t\t/* Started by a port monitor ? */\n");
96 if (!tirpcflag
|| tirpc_socket
) {
97 f_print(fout
, "%s int _rpcfdtype;", var_type
);
98 f_print(fout
, "\n\t\t /* Whether Stream or "
103 f_print(fout
, " /* States a server can be in "
104 "wrt request */\n\n");
105 f_print(fout
, "#define\t_IDLE 0\n");
106 f_print(fout
, "#define\t_SERVED 1\n");
107 f_print(fout
, "#define\t_SERVING 2\n\n");
108 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
109 f_print(fout
, "\t /* Set when a request is "
113 f_print(fout
, "pthread_mutex_t _svcstate_lock;");
114 f_print(fout
, "\t\t\t/* Mutex lock for "
115 "variable _rpcsvcstate */\n");
121 write_svc_aux(nomain
);
123 /* write out dispatcher and stubs */
124 write_programs((char *)NULL
);
129 f_print(fout
, "\nint\n");
130 f_print(fout
, "main()\n");
131 f_print(fout
, "{\n");
133 write_inetmost(infile
);
134 /* Includes call to write_rpc_svc_fg() */
139 "\tregister SVCXPRT *%s;\n", TRANSP
);
141 "\tstruct netconfig *nconf = NULL;\n");
143 f_print(fout
, "\tpid_t pid;\n");
144 f_print(fout
, "\tint i;\n");
147 f_print(fout
, "\tstruct sockaddr_storage saddr;\n");
148 f_print(fout
, "\tsocklen_t asize = sizeof (saddr);\n\n");
150 f_print(fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
153 if (mtflag
& timerflag
)
154 f_print(fout
, "\tmutex_init(&_svcstate_lock, "
155 "USYNC_THREAD, NULL);\n");
157 write_pm_most(infile
, netflag
);
158 f_print(fout
, "\telse {\n");
159 write_rpc_svc_fg(infile
, "\t\t");
160 f_print(fout
, "\t}\n");
162 write_rpc_svc_fg(infile
, "\t\t");
165 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
167 print_pmapunset("\t");
171 if (logflag
&& !inetdflag
)
172 open_log_file(infile
, "\t");
176 * write a registration for the given transport
179 write_netid_register(const char *transp
)
189 f_print(fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
190 f_print(fout
, "%s\tif (nconf == NULL) {\n", sp
);
191 sprintf(_errbuf
, "cannot find %s netid.", transp
);
192 sprintf(tmpbuf
, "%s\t\t", sp
);
193 print_err_message(tmpbuf
);
194 f_print(fout
, "%s\t\texit(1);\n", sp
);
195 f_print(fout
, "%s\t}\n", sp
);
197 f_print(fout
, "%s\t%s = svc_tli_create(RPC_ANYFD, ",
199 f_print(fout
,"nconf, 0, RPC_MAXDATASIZE, RPC_MAXDATASIZE);\n");
202 "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
205 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
206 sprintf(_errbuf
, "cannot create %s service.", transp
);
207 print_err_message(tmpbuf
);
208 f_print(fout
, "%s\t\texit(1);\n", sp
);
209 f_print(fout
, "%s\t}\n", sp
);
211 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
212 def
= (definition
*) l
->val
;
213 if (def
->def_kind
!= DEF_PROGRAM
)
215 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
216 f_print(fout
, "%s\trpcb_unset(%s, %s, nconf);\n",
217 sp
, def
->def_name
, vp
->vers_name
);
218 f_print(fout
, "%s\tif (!svc_reg(%s, %s, %s, ",
219 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
220 pvname(def
->def_name
, vp
->vers_num
);
221 f_print(fout
, ", nconf)) {\n");
222 sprintf(_errbuf
, "unable to register (%s, %s, %s).",
223 def
->def_name
, vp
->vers_name
, transp
);
224 print_err_message(tmpbuf
);
225 f_print(fout
, "%s\t\texit(1);\n", sp
);
226 f_print(fout
, "%s\t}\n", sp
);
229 f_print(fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
233 * write a registration for the given transport for TLI
236 write_nettype_register(const char *transp
)
242 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
243 def
= (definition
*) l
->val
;
244 if (def
->def_kind
!= DEF_PROGRAM
)
246 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
247 f_print(fout
, "\tif (!svc_create(");
248 pvname(def
->def_name
, vp
->vers_num
);
249 f_print(fout
, ", %s, %s, \"%s\")) {\n",
250 def
->def_name
, vp
->vers_name
, transp
);
251 sprintf(_errbuf
, "unable to create (%s, %s) for %s.",
252 def
->def_name
, vp
->vers_name
, transp
);
253 print_err_message("\t\t");
254 f_print(fout
, "\t\texit(1);\n");
255 f_print(fout
, "\t}\n");
261 * write the rest of the service
268 f_print(fout
, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP
);
269 sprintf(_errbuf
, "could not create a handle");
270 print_err_message("\t\t");
271 f_print(fout
, "\t\texit(1);\n");
272 f_print(fout
, "\t}\n");
274 f_print(fout
, "\tif (_rpcpmstart) {\n");
276 "\t\tsignal(SIGALRM, closedown);\n");
277 f_print(fout
, "\t\talarm(_RPCSVC_CLOSEDOWN/2);\n");
278 f_print(fout
, "\t}\n");
281 f_print(fout
, "\tsvc_run();\n");
282 sprintf(_errbuf
, "svc_run returned");
283 print_err_message("\t");
284 f_print(fout
, "\texit(1);\n");
285 f_print(fout
, "\t/* NOTREACHED */\n");
286 f_print(fout
, "}\n");
290 write_programs(const char *storage
)
295 /* write out stubs for procedure definitions */
296 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
297 def
= (definition
*) l
->val
;
298 if (def
->def_kind
== DEF_PROGRAM
)
299 write_real_program(def
);
302 /* write out dispatcher for each program */
303 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
304 def
= (definition
*) l
->val
;
305 if (def
->def_kind
== DEF_PROGRAM
)
306 write_program(def
, storage
);
311 * write out definition of internal function (e.g. _printmsg_1(...))
312 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
313 * Unpacks single user argument of printmsg_1 to call-by-value format
314 * expected by printmsg_1.
317 write_real_program(definition
*def
)
324 return; /* not needed for old style */
325 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
326 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
329 internal_proctype(proc
);
331 f_print(fout
, "int");
332 f_print(fout
, "\n_");
333 pvname(proc
->proc_name
, vp
->vers_num
);
336 if (proc
->arg_num
> 1)
337 f_print(fout
, proc
->args
.argname
);
339 ptype(proc
->args
.decls
->decl
.prefix
,
340 proc
->args
.decls
->decl
.type
, 0);
342 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
346 f_print(fout
, " *argp, struct svc_req *%s)\n",
349 f_print(fout
, "{\n");
350 f_print(fout
, "\treturn (");
351 pvname_svc(proc
->proc_name
, vp
->vers_num
);
353 if (proc
->arg_num
< 2) { /* single argument */
354 if (!streq(proc
->args
.decls
->decl
.type
, "void"))
355 f_print(fout
, "*argp, "); /* non-void */
357 for (l
= proc
->args
.decls
; l
!= NULL
;
359 f_print(fout
, "argp->%s, ",
363 f_print(fout
, "%s, ",RESULT
);
364 f_print(fout
, "%s));\n}\n", RQSTP
);
370 write_program(definition
*def
, const char *storage
)
376 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
379 f_print(fout
, "%s ", storage
);
380 f_print(fout
, "void\n");
381 pvname(def
->def_name
, vp
->vers_num
);
383 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
384 f_print(fout
, "SVCXPRT *%s)\n", TRANSP
);
385 f_print(fout
, "{\n");
388 f_print(fout
, "\tunion {\n");
389 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
390 if (proc
->arg_num
< 2) { /* single argument */
391 if (streq(proc
->args
.decls
->decl
.type
, "void"))
394 f_print(fout
, "\t\t");
395 ptype(proc
->args
.decls
->decl
.prefix
,
396 proc
->args
.decls
->decl
.type
, 0);
397 pvname(proc
->proc_name
, vp
->vers_num
);
398 f_print(fout
, "_arg;\n");
402 f_print(fout
, "\t\t%s", proc
->args
.argname
);
404 pvname(proc
->proc_name
, vp
->vers_num
);
405 f_print(fout
, "_arg;\n");
409 f_print(fout
, "\t\tint fill;\n");
410 f_print(fout
, "\t} %s;\n", ARG
);
413 f_print(fout
, "\tunion {\n");
414 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
415 if (streq(proc
->res_type
, "void"))
417 f_print(fout
, "\t\t");
418 ptype(proc
->res_prefix
, proc
->res_type
, 0);
419 pvname(proc
->proc_name
, vp
->vers_num
);
420 f_print(fout
, "_res;\n");
422 f_print(fout
, "\t} %s;\n", RESULT
);
423 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
426 f_print(fout
, "\tchar *%s;\n", RESULT
);
429 f_print(fout
, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG
, RESULT
);
432 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
436 "\tchar *(*%s)(char *, struct svc_req *);\n",
442 f_print(fout
, "\tpthread_mutex_lock(&_svcstate_lock);\n");
444 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
446 f_print(fout
, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
449 f_print(fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
450 if (!nullproc(vp
->procs
)) {
451 f_print(fout
, "\tcase NULLPROC:\n");
453 "\t\tsvc_sendreply(%s,\n\t\t\t"
454 "(xdrproc_t) xdr_void, (char *)NULL);\n",
456 print_return("\t\t");
459 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
460 f_print(fout
, "\tcase %s:\n", proc
->proc_name
);
461 if (proc
->arg_num
< 2) {
462 /* single argument */
463 p_xdrfunc(ARG
, proc
->args
.decls
->decl
.type
);
465 p_xdrfunc(ARG
, proc
->args
.argname
);
467 p_xdrfunc(RESULT
, proc
->res_type
);
471 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
475 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
477 if (newstyle
) { /* new style: calls internal routine */
481 pvname_svc(proc
->proc_name
, vp
->vers_num
);
483 pvname(proc
->proc_name
, vp
->vers_num
);
484 f_print(fout
, ";\n");
485 f_print(fout
, "\t\tbreak;\n\n");
487 f_print(fout
, "\tdefault:\n");
488 printerr("noproc", TRANSP
);
489 print_return("\t\t");
490 f_print(fout
, "\t}\n");
492 f_print(fout
, "\tmemset((char *)&%s, 0, sizeof (%s));\n",
494 printif("getargs", TRANSP
, "(caddr_t) &", ARG
);
495 printerr("decode", TRANSP
);
496 print_return("\t\t");
497 f_print(fout
, "\t}\n");
500 f_print(fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
501 RESULT
, ROUTINE
, ARG
, RQSTP
);
503 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
504 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
509 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
510 RETVAL
, TRANSP
, RESULT
, RESULT
);
513 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
514 RESULT
, TRANSP
, RESULT
, RESULT
);
516 printerr("systemerr", TRANSP
);
517 f_print(fout
, "\t}\n");
519 printif("freeargs", TRANSP
, "(caddr_t) &", ARG
);
520 sprintf(_errbuf
, "unable to free arguments");
521 print_err_message("\t\t");
522 f_print(fout
, "\t\texit(1);\n");
523 f_print(fout
, "\t}\n");
524 /* print out free routine */
526 f_print(fout
,"\tif (!");
527 pvname(def
->def_name
, vp
->vers_num
);
528 f_print(fout
,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
529 TRANSP
, RESULT
, RESULT
);
530 sprintf(_errbuf
, "unable to free results");
531 print_err_message("\t\t");
535 f_print(fout
, "}\n");
540 printerr(const char *err
, const char *transp
)
542 f_print(fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
546 printif(const char *proc
, const char *transp
, const char *prefix
,
549 f_print(fout
, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n",
550 proc
, transp
, arg
, prefix
, arg
);
554 nullproc(proc_list
*proc
)
556 for (; proc
!= NULL
; proc
= proc
->next
) {
557 if (streq(proc
->proc_num
, "0"))
564 write_inetmost(const char *infile
)
566 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
567 f_print(fout
, "\tint sock;\n");
568 f_print(fout
, "\tint proto;\n");
569 f_print(fout
, "\tstruct sockaddr_in saddr;\n");
570 f_print(fout
, "\tsocklen_t asize = sizeof (saddr);\n");
572 f_print(fout
, "\tif (getsockname(0, "
573 "(struct sockaddr *)&saddr, &asize) == 0) {\n");
574 f_print(fout
, "\t\tsocklen_t ssize = sizeof (int);\n\n");
575 f_print(fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
576 f_print(fout
, "\t\t\texit(1);\n");
577 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
578 f_print(fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
579 f_print(fout
, "\t\t\texit(1);\n");
580 f_print(fout
, "\t\tsock = 0;\n");
581 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
582 f_print(fout
, "\t\tproto = 0;\n");
583 open_log_file(infile
, "\t\t");
584 f_print(fout
, "\t} else {\n");
585 write_rpc_svc_fg(infile
, "\t\t");
586 f_print(fout
, "\t\tsock = RPC_ANYSOCK;\n");
587 print_pmapunset("\t\t");
588 f_print(fout
, "\t}\n");
592 print_return(const char *space
)
595 f_print(fout
, "%sexit(0);\n", space
);
599 f_print(fout
, "%spthread_mutex_lock(&_svcstate_lock);\n", space
);
600 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
602 f_print(fout
, "%spthread_mutex_unlock(&_svcstate_lock);\n", space
);
604 f_print(fout
, "%sreturn;\n", space
);
609 print_pmapunset(const char *space
)
615 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
616 def
= (definition
*) l
->val
;
617 if (def
->def_kind
== DEF_PROGRAM
) {
618 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
620 f_print(fout
, "%spmap_unset(%s, %s);\n",
621 space
, def
->def_name
, vp
->vers_name
);
628 print_err_message(const char *space
)
631 f_print(fout
, "%ssyslog(LOG_ERR, \"%s\");\n", space
, _errbuf
);
632 else if (inetdflag
|| pmflag
)
633 f_print(fout
, "%s_msgout(\"%s\");\n", space
, _errbuf
);
635 f_print(fout
, "%sfprintf(stderr, \"%s\");\n", space
, _errbuf
);
639 * Write the server auxiliary function (_msgout, timeout)
642 write_svc_aux(int nomain
)
647 write_timeout_func();
651 * Write the _msgout function
659 * Avoid making _msgout() static -- it's useful to have it visible
660 * in the toplevel RPC server code.
662 f_print(fout
, "static\n");
663 f_print(fout
, "void _msgout(const char* msg)\n");
664 f_print(fout
, "{\n");
665 f_print(fout
, "#ifdef RPC_SVC_FG\n");
666 if (inetdflag
|| pmflag
)
667 f_print(fout
, "\tif (_rpcpmstart)\n");
668 f_print(fout
, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
669 f_print(fout
, "\telse\n");
671 "\t\tfprintf(stderr, \"%%s\\n\", msg);\n");
672 f_print(fout
, "#else\n");
673 f_print(fout
, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
674 f_print(fout
, "#endif\n");
675 f_print(fout
, "}\n");
679 * Write the timeout function
682 write_timeout_func(void)
688 f_print(fout
, "static void\n");
689 f_print(fout
, "closedown(int sig)\n");
690 f_print(fout
, "{\n");
692 f_print(fout
, "\tpthread_mutex_lock(&_svcstate_lock);\n");
693 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
694 f_print(fout
, "\t\textern fd_set svc_fdset;\n");
695 f_print(fout
, "\t\tstatic int size;\n");
696 f_print(fout
, "\t\tint i, openfd;\n");
697 if (tirpcflag
&& pmflag
) {
698 f_print(fout
, "\t\tstruct t_info tinfo;\n\n");
700 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
702 f_print(fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
704 f_print(fout
, "\t\t\texit(0);\n");
705 f_print(fout
, "\t\tif (size == 0) {\n");
707 f_print(fout
, "\t\t\tstruct rlimit rl;\n\n");
708 f_print(fout
, "\t\t\trl.rlim_max = 0;\n");
709 f_print(fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
710 f_print(fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
713 f_print(fout
, "\t\t\t\tpthread_mutex_unlock(&_svcstate_lock);\n");
715 f_print(fout
, "\t\t\t\treturn;\n\t\t\t}\n");
717 f_print(fout
, "\t\t\tsize = getdtablesize();\n");
719 f_print(fout
, "\t\t}\n");
721 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
722 f_print(fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
723 f_print(fout
, "\t\t\t\topenfd++;\n");
724 f_print(fout
, "\t\tif (openfd <= 1)\n");
725 f_print(fout
, "\t\t\texit(0);\n");
726 f_print(fout
, "\t}\n");
727 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
728 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
730 f_print(fout
, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
732 f_print(fout
, "\tsignal(SIGALRM, closedown);\n");
733 f_print(fout
, "\talarm(_RPCSVC_CLOSEDOWN/2);\n");
734 f_print(fout
, "}\n");
739 * Write the most of port monitor support
742 write_pm_most(const char *infile
, int netflag
)
750 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
751 f_print(fout
, "\t\tsocklen_t ssize = sizeof (int);\n");
753 f_print(fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
754 f_print(fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
755 f_print(fout
, " !strcmp(mname, \"timod\"))) {\n");
757 f_print(fout
, "\t\tchar *netid;\n");
758 if (!netflag
) { /* Not included by -n option */
759 f_print(fout
, "\t\tstruct netconfig *nconf = NULL;\n");
760 f_print(fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
763 f_print(fout
, "\t\tint pmclose;\n");
765 * Not necessary, defined in /usr/include/stdlib
766 * f_print(fout, "\t\textern char *getenv();\n");
770 f_print(fout
, "\t\tif (saddr.ss_family != AF_INET &&\n");
771 f_print(fout
, "\t\t saddr.ss_family != AF_INET6)\n");
772 f_print(fout
, "\t\t\texit(1);\n");
773 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
774 f_print(fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
775 f_print(fout
, "\t\t\texit(1);\n");
777 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
778 open_log_file(infile
, "\t\t");
779 f_print(fout
, "\n\t\tif ((netid = \
780 getenv(\"NLSPROVIDER\")) == NULL) {\n");
783 f_print(fout
, "\t\t/* started from inetd */\n");
784 f_print(fout
, "\t\t\tpmclose = 1;\n");
788 f_print(fout
, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
789 sprintf(_errbuf
, "cannot get transport info");
790 print_err_message("\t\t\t\t");
793 f_print(fout
, "\n\t\t\tpmclose = 1;\t/* XXX */\n");
796 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
798 f_print(fout
, "\t\t}\n");
800 * A kludgy support for inetd services. Inetd only works with
801 * sockmod, and RPC works only with timod, hence all this jugglery
804 f_print(fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
805 f_print(fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ");
806 f_print(fout
, "ioctl(0, I_PUSH, \"timod\")) {\n");
807 sprintf(_errbuf
, "could not get the right module");
808 print_err_message("\t\t\t\t");
809 f_print(fout
, "\t\t\t\texit(1);\n");
810 f_print(fout
, "\t\t\t}\n");
811 f_print(fout
, "\t\t}\n");
815 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, \
816 RPC_MAXDATASIZE, RPC_MAXDATASIZE)) \
821 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
825 sprintf(_errbuf
, "cannot create server handle");
826 print_err_message("\t\t\t");
827 f_print(fout
, "\t\t\texit(1);\n");
828 f_print(fout
, "\t\t}\n");
829 f_print(fout
, "\t\tif (nconf)\n");
830 f_print(fout
, "\t\t\tfreenetconfigent(nconf);\n");
831 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
832 def
= (definition
*) l
->val
;
833 if (def
->def_kind
!= DEF_PROGRAM
)
835 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
836 f_print(fout
, "\t\tif (!svc_reg(%s, %s, %s, ",
837 TRANSP
, def
->def_name
, vp
->vers_name
);
838 pvname(def
->def_name
, vp
->vers_num
);
839 f_print(fout
, ", 0)) {\n");
840 sprintf(_errbuf
, "unable to register (%s, %s).",
841 def
->def_name
, vp
->vers_name
);
842 print_err_message("\t\t\t");
843 f_print(fout
, "\t\t\texit(1);\n");
844 f_print(fout
, "\t\t}\n");
848 f_print(fout
, "\t\tif (pmclose) {\n");
849 f_print(fout
, "\t\t\tsignal(SIGALRM, closedown);\n");
850 f_print(fout
, "\t\t\talarm(_RPCSVC_CLOSEDOWN/2);\n");
851 f_print(fout
, "\t\t}\n");
853 f_print(fout
, "\t\tsvc_run();\n");
854 f_print(fout
, "\t\texit(1);\n");
855 f_print(fout
, "\t\t/* NOTREACHED */\n");
856 f_print(fout
, "\t}");
860 * Support for backgrounding the server if self started.
863 write_rpc_svc_fg(const char *infile
, const char *sp
)
865 f_print(fout
, "#ifndef RPC_SVC_FG\n");
866 f_print(fout
, "%sint size;\n", sp
);
868 f_print(fout
, "%sstruct rlimit rl;\n", sp
);
870 f_print(fout
, "%sint pid, i;\n\n", sp
);
871 f_print(fout
, "%spid = fork();\n", sp
);
872 f_print(fout
, "%sif (pid < 0) {\n", sp
);
873 f_print(fout
, "%s\tperror(\"cannot fork\");\n", sp
);
874 f_print(fout
, "%s\texit(1);\n", sp
);
875 f_print(fout
, "%s}\n", sp
);
876 f_print(fout
, "%sif (pid)\n", sp
);
877 f_print(fout
, "%s\texit(0);\n", sp
);
878 /* get number of file descriptors */
880 f_print(fout
, "%srl.rlim_max = 0;\n", sp
);
881 f_print(fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
882 f_print(fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
883 f_print(fout
, "%s\texit(1);\n", sp
);
885 f_print(fout
, "%ssize = getdtablesize();\n", sp
);
888 f_print(fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
889 f_print(fout
, "%s\tclose(i);\n", sp
);
890 /* Redirect stderr and stdout to console */
891 f_print(fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
892 f_print(fout
, "%sdup2(i, 1);\n", sp
);
893 f_print(fout
, "%sdup2(i, 2);\n", sp
);
894 /* This removes control of the controlling terminal */
896 f_print(fout
, "%ssetsid();\n", sp
);
898 f_print(fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
899 f_print(fout
, "%sif (i >= 0) {\n", sp
);
901 "%s\tioctl(i, TIOCNOTTY, (char *)NULL);\n", sp
);
902 f_print(fout
, "%s\tclose(i);\n", sp
);
903 f_print(fout
, "%s}\n", sp
);
906 open_log_file(infile
, sp
);
907 f_print(fout
, "#endif\n");
909 open_log_file(infile
, sp
);
913 open_log_file(const char *infile
, const char *sp
)
917 s
= strrchr(infile
, '.');
920 f_print(fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
926 * write a registration for the given transport for Inetd
929 write_inetd_register(const char *transp
)
942 if (streq(transp
, "udp"))
949 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
950 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
952 f_print(fout
, "%s\t%s = svc%s_create(%s",
953 sp
, TRANSP
, transp
, inetdflag
? "sock": "RPC_ANYSOCK");
955 f_print(fout
, ", 0, 0");
956 f_print(fout
, ");\n");
957 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
958 sprintf(_errbuf
, "cannot create %s service.", transp
);
959 sprintf(tmpbuf
, "%s\t\t", sp
);
960 print_err_message(tmpbuf
);
961 f_print(fout
, "%s\t\texit(1);\n", sp
);
962 f_print(fout
, "%s\t}\n", sp
);
965 f_print(fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
966 f_print(fout
, "%s\tproto = IPPROTO_%s;\n", sp
,
967 isudp
? "UDP": "TCP");
969 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
970 def
= (definition
*) l
->val
;
971 if (def
->def_kind
!= DEF_PROGRAM
)
973 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
974 f_print(fout
, "%s\tif (!svc_register(%s, %s, %s, ",
975 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
976 pvname(def
->def_name
, vp
->vers_num
);
978 f_print(fout
, ", proto)) {\n");
980 f_print(fout
, ", IPPROTO_%s)) {\n",
981 isudp
? "UDP": "TCP");
982 sprintf(_errbuf
, "unable to register (%s, %s, %s).",
983 def
->def_name
, vp
->vers_name
, transp
);
984 print_err_message(tmpbuf
);
985 f_print(fout
, "%s\t\texit(1);\n", sp
);
986 f_print(fout
, "%s\t}\n", sp
);
990 f_print(fout
, "\t}\n");