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 $
34 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
35 * Copyright (C) 1987, Sun Microsystems, Inc.
39 #include "rpc_parse.h"
43 static char RQSTP
[] = "rqstp";
44 static char TRANSP
[] = "transp";
45 static char ARG
[] = "argument";
46 static char RESULT
[] = "result";
47 static char ROUTINE
[] = "local";
48 static char RETVAL
[] = "retval";
50 char _errbuf
[256]; /* For all messages */
52 void internal_proctype(proc_list
*);
53 static void write_real_program(definition
*);
54 static void write_program(definition
*, const char *);
55 static void printerr(const char *, const char *);
56 static void printif(const char *, const char *, const char *, const char *);
57 static void write_inetmost(const char *);
58 static void print_return(const char *);
59 static void print_pmapunset(const char *);
60 static void print_err_message(const char *);
61 static void write_timeout_func(void);
62 static void write_pm_most(const char *, int);
63 static void write_rpc_svc_fg(const char *, const char *);
64 static void open_log_file(const char *, const char *);
65 static void write_msg_out(void);
69 p_xdrfunc(const char *rname
, const char *typename
)
71 f_print(fout
, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
72 rname
, stringfix(typename
));
76 internal_proctype(proc_list
*plist
)
78 f_print(fout
, "static ");
79 ptype(plist
->res_prefix
, plist
->res_type
, 1);
85 * write most of the service, that is, everything but the registrations.
88 write_most(const char *infile
, int netflag
, int nomain
)
90 if (inetdflag
|| pmflag
) {
92 var_type
= (nomain
? "extern" : "static");
93 f_print(fout
, "%s int _rpcpmstart;", var_type
);
94 f_print(fout
, "\t\t/* Started by a port monitor ? */\n");
95 if (!tirpcflag
|| tirpc_socket
) {
96 f_print(fout
, "%s int _rpcfdtype;", var_type
);
97 f_print(fout
, "\n\t\t /* Whether Stream or "
102 f_print(fout
, " /* States a server can be in "
103 "wrt request */\n\n");
104 f_print(fout
, "#define\t_IDLE 0\n");
105 f_print(fout
, "#define\t_SERVED 1\n");
106 f_print(fout
, "#define\t_SERVING 2\n\n");
107 f_print(fout
, "static int _rpcsvcstate = _IDLE;");
108 f_print(fout
, "\t /* Set when a request is "
112 f_print(fout
, "pthread_mutex_t _svcstate_lock;");
113 f_print(fout
, "\t\t\t/* Mutex lock for "
114 "variable _rpcsvcstate */\n");
120 write_svc_aux(nomain
);
122 /* write out dispatcher and stubs */
123 write_programs(NULL
);
128 f_print(fout
, "\nint\n");
129 f_print(fout
, "main()\n");
130 f_print(fout
, "{\n");
132 write_inetmost(infile
);
133 /* Includes call to write_rpc_svc_fg() */
138 "\tregister SVCXPRT *%s;\n", TRANSP
);
140 "\tstruct netconfig *nconf = NULL;\n");
142 f_print(fout
, "\tpid_t pid;\n");
143 f_print(fout
, "\tint i;\n");
146 f_print(fout
, "\tstruct sockaddr_storage saddr;\n");
147 f_print(fout
, "\tsocklen_t asize = sizeof (saddr);\n\n");
149 f_print(fout
, "\tchar mname[FMNAMESZ + 1];\n\n");
152 if (mtflag
& timerflag
)
153 f_print(fout
, "\tmutex_init(&_svcstate_lock, "
154 "USYNC_THREAD, NULL);\n");
156 write_pm_most(infile
, netflag
);
157 f_print(fout
, "\telse {\n");
158 write_rpc_svc_fg(infile
, "\t\t");
159 f_print(fout
, "\t}\n");
161 write_rpc_svc_fg(infile
, "\t\t");
164 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
166 print_pmapunset("\t");
170 if (logflag
&& !inetdflag
)
171 open_log_file(infile
, "\t");
175 * write a registration for the given transport
178 write_netid_register(const char *transp
)
188 f_print(fout
, "%s\tnconf = getnetconfigent(\"%s\");\n", sp
, transp
);
189 f_print(fout
, "%s\tif (nconf == NULL) {\n", sp
);
190 sprintf(_errbuf
, "cannot find %s netid.", transp
);
191 sprintf(tmpbuf
, "%s\t\t", sp
);
192 print_err_message(tmpbuf
);
193 f_print(fout
, "%s\t\texit(1);\n", sp
);
194 f_print(fout
, "%s\t}\n", sp
);
196 f_print(fout
, "%s\t%s = svc_tli_create(RPC_ANYFD, ",
198 f_print(fout
,"nconf, 0, RPC_MAXDATASIZE, RPC_MAXDATASIZE);\n");
201 "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
204 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
205 sprintf(_errbuf
, "cannot create %s service.", transp
);
206 print_err_message(tmpbuf
);
207 f_print(fout
, "%s\t\texit(1);\n", sp
);
208 f_print(fout
, "%s\t}\n", sp
);
210 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
211 def
= (definition
*) l
->val
;
212 if (def
->def_kind
!= DEF_PROGRAM
)
214 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
215 f_print(fout
, "%s\trpcb_unset(%s, %s, nconf);\n",
216 sp
, def
->def_name
, vp
->vers_name
);
217 f_print(fout
, "%s\tif (!svc_reg(%s, %s, %s, ",
218 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
219 pvname(def
->def_name
, vp
->vers_num
);
220 f_print(fout
, ", nconf)) {\n");
221 sprintf(_errbuf
, "unable to register (%s, %s, %s).",
222 def
->def_name
, vp
->vers_name
, transp
);
223 print_err_message(tmpbuf
);
224 f_print(fout
, "%s\t\texit(1);\n", sp
);
225 f_print(fout
, "%s\t}\n", sp
);
228 f_print(fout
, "%s\tfreenetconfigent(nconf);\n", sp
);
232 * write a registration for the given transport for TLI
235 write_nettype_register(const char *transp
)
241 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
242 def
= (definition
*) l
->val
;
243 if (def
->def_kind
!= DEF_PROGRAM
)
245 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
246 f_print(fout
, "\tif (!svc_create(");
247 pvname(def
->def_name
, vp
->vers_num
);
248 f_print(fout
, ", %s, %s, \"%s\")) {\n",
249 def
->def_name
, vp
->vers_name
, transp
);
250 sprintf(_errbuf
, "unable to create (%s, %s) for %s.",
251 def
->def_name
, vp
->vers_name
, transp
);
252 print_err_message("\t\t");
253 f_print(fout
, "\t\texit(1);\n");
254 f_print(fout
, "\t}\n");
260 * write the rest of the service
267 f_print(fout
, "\tif (%s == NULL) {\n", TRANSP
);
268 sprintf(_errbuf
, "could not create a handle");
269 print_err_message("\t\t");
270 f_print(fout
, "\t\texit(1);\n");
271 f_print(fout
, "\t}\n");
273 f_print(fout
, "\tif (_rpcpmstart) {\n");
275 "\t\tsignal(SIGALRM, closedown);\n");
276 f_print(fout
, "\t\talarm(_RPCSVC_CLOSEDOWN/2);\n");
277 f_print(fout
, "\t}\n");
280 f_print(fout
, "\tsvc_run();\n");
281 sprintf(_errbuf
, "svc_run returned");
282 print_err_message("\t");
283 f_print(fout
, "\texit(1);\n");
284 f_print(fout
, "\t/* NOTREACHED */\n");
285 f_print(fout
, "}\n");
289 write_programs(const char *storage
)
294 /* write out stubs for procedure definitions */
295 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
296 def
= (definition
*) l
->val
;
297 if (def
->def_kind
== DEF_PROGRAM
)
298 write_real_program(def
);
301 /* write out dispatcher for each program */
302 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
303 def
= (definition
*) l
->val
;
304 if (def
->def_kind
== DEF_PROGRAM
)
305 write_program(def
, storage
);
310 * write out definition of internal function (e.g. _printmsg_1(...))
311 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
312 * Unpacks single user argument of printmsg_1 to call-by-value format
313 * expected by printmsg_1.
316 write_real_program(definition
*def
)
323 return; /* not needed for old style */
324 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
325 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
328 internal_proctype(proc
);
330 f_print(fout
, "int");
331 f_print(fout
, "\n_");
332 pvname(proc
->proc_name
, vp
->vers_num
);
335 if (proc
->arg_num
> 1)
336 f_print(fout
, "%s", proc
->args
.argname
);
338 ptype(proc
->args
.decls
->decl
.prefix
,
339 proc
->args
.decls
->decl
.type
, 0);
341 f_print(fout
, " *argp, void *%s, struct svc_req *%s)\n",
345 f_print(fout
, " *argp, struct svc_req *%s)\n",
348 f_print(fout
, "{\n");
349 f_print(fout
, "\treturn (");
350 pvname_svc(proc
->proc_name
, vp
->vers_num
);
352 if (proc
->arg_num
< 2) { /* single argument */
353 if (!streq(proc
->args
.decls
->decl
.type
, "void"))
354 f_print(fout
, "*argp, "); /* non-void */
356 for (l
= proc
->args
.decls
; l
!= NULL
;
358 f_print(fout
, "argp->%s, ",
362 f_print(fout
, "%s, ",RESULT
);
363 f_print(fout
, "%s));\n}\n", RQSTP
);
369 write_program(definition
*def
, const char *storage
)
375 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
378 f_print(fout
, "%s ", storage
);
379 f_print(fout
, "void\n");
380 pvname(def
->def_name
, vp
->vers_num
);
382 f_print(fout
, "(struct svc_req *%s, ", RQSTP
);
383 f_print(fout
, "SVCXPRT *%s)\n", TRANSP
);
384 f_print(fout
, "{\n");
387 f_print(fout
, "\tunion {\n");
388 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
389 if (proc
->arg_num
< 2) { /* single argument */
390 if (streq(proc
->args
.decls
->decl
.type
, "void"))
393 f_print(fout
, "\t\t");
394 ptype(proc
->args
.decls
->decl
.prefix
,
395 proc
->args
.decls
->decl
.type
, 0);
396 pvname(proc
->proc_name
, vp
->vers_num
);
397 f_print(fout
, "_arg;\n");
401 f_print(fout
, "\t\t%s", proc
->args
.argname
);
403 pvname(proc
->proc_name
, vp
->vers_num
);
404 f_print(fout
, "_arg;\n");
408 f_print(fout
, "\t\tint fill;\n");
409 f_print(fout
, "\t} %s;\n", ARG
);
412 f_print(fout
, "\tunion {\n");
413 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
414 if (streq(proc
->res_type
, "void"))
416 f_print(fout
, "\t\t");
417 ptype(proc
->res_prefix
, proc
->res_type
, 0);
418 pvname(proc
->proc_name
, vp
->vers_num
);
419 f_print(fout
, "_res;\n");
421 f_print(fout
, "\t} %s;\n", RESULT
);
422 f_print(fout
, "\tbool_t %s;\n", RETVAL
);
425 f_print(fout
, "\tchar *%s;\n", RESULT
);
428 f_print(fout
, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG
, RESULT
);
431 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
435 "\tchar *(*%s)(char *, struct svc_req *);\n",
441 f_print(fout
, "\tpthread_mutex_lock(&_svcstate_lock);\n");
443 f_print(fout
, "\t_rpcsvcstate = _SERVING;\n");
445 f_print(fout
, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
448 f_print(fout
, "\tswitch (%s->rq_proc) {\n", RQSTP
);
449 if (!nullproc(vp
->procs
)) {
450 f_print(fout
, "\tcase NULLPROC:\n");
452 "\t\tsvc_sendreply(%s,\n\t\t\t"
453 "(xdrproc_t) xdr_void, NULL);\n",
455 print_return("\t\t");
458 for (proc
= vp
->procs
; proc
!= NULL
; proc
= proc
->next
) {
459 f_print(fout
, "\tcase %s:\n", proc
->proc_name
);
460 if (proc
->arg_num
< 2) {
461 /* single argument */
462 p_xdrfunc(ARG
, proc
->args
.decls
->decl
.type
);
464 p_xdrfunc(ARG
, proc
->args
.argname
);
466 p_xdrfunc(RESULT
, proc
->res_type
);
470 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
474 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
476 if (newstyle
) { /* new style: calls internal routine */
480 pvname_svc(proc
->proc_name
, vp
->vers_num
);
482 pvname(proc
->proc_name
, vp
->vers_num
);
483 f_print(fout
, ";\n");
484 f_print(fout
, "\t\tbreak;\n\n");
486 f_print(fout
, "\tdefault:\n");
487 printerr("noproc", TRANSP
);
488 print_return("\t\t");
489 f_print(fout
, "\t}\n");
491 f_print(fout
, "\tmemset((char *)&%s, 0, sizeof (%s));\n",
493 printif("getargs", TRANSP
, "(caddr_t) &", ARG
);
494 printerr("decode", TRANSP
);
495 print_return("\t\t");
496 f_print(fout
, "\t}\n");
499 f_print(fout
, "\t%s = (*%s)((char *)&%s, %s);\n",
500 RESULT
, ROUTINE
, ARG
, RQSTP
);
502 f_print(fout
, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
503 RETVAL
, ROUTINE
, ARG
, RESULT
, RQSTP
);
508 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
509 RETVAL
, TRANSP
, RESULT
, RESULT
);
512 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
513 RESULT
, TRANSP
, RESULT
, RESULT
);
515 printerr("systemerr", TRANSP
);
516 f_print(fout
, "\t}\n");
518 printif("freeargs", TRANSP
, "(caddr_t) &", ARG
);
519 sprintf(_errbuf
, "unable to free arguments");
520 print_err_message("\t\t");
521 f_print(fout
, "\t\texit(1);\n");
522 f_print(fout
, "\t}\n");
523 /* print out free routine */
525 f_print(fout
,"\tif (!");
526 pvname(def
->def_name
, vp
->vers_num
);
527 f_print(fout
,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
528 TRANSP
, RESULT
, RESULT
);
529 sprintf(_errbuf
, "unable to free results");
530 print_err_message("\t\t");
534 f_print(fout
, "}\n");
539 printerr(const char *err
, const char *transp
)
541 f_print(fout
, "\t\tsvcerr_%s(%s);\n", err
, transp
);
545 printif(const char *proc
, const char *transp
, const char *prefix
,
548 f_print(fout
, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n",
549 proc
, transp
, arg
, prefix
, arg
);
553 nullproc(proc_list
*proc
)
555 for (; proc
!= NULL
; proc
= proc
->next
) {
556 if (streq(proc
->proc_num
, "0"))
563 write_inetmost(const char *infile
)
565 f_print(fout
, "\tregister SVCXPRT *%s;\n", TRANSP
);
566 f_print(fout
, "\tint sock;\n");
567 f_print(fout
, "\tint proto;\n");
568 f_print(fout
, "\tstruct sockaddr_in saddr;\n");
569 f_print(fout
, "\tsocklen_t asize = sizeof (saddr);\n");
571 f_print(fout
, "\tif (getsockname(0, "
572 "(struct sockaddr *)&saddr, &asize) == 0) {\n");
573 f_print(fout
, "\t\tsocklen_t ssize = sizeof (int);\n\n");
574 f_print(fout
, "\t\tif (saddr.sin_family != AF_INET)\n");
575 f_print(fout
, "\t\t\texit(1);\n");
576 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
577 f_print(fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
578 f_print(fout
, "\t\t\texit(1);\n");
579 f_print(fout
, "\t\tsock = 0;\n");
580 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
581 f_print(fout
, "\t\tproto = 0;\n");
582 open_log_file(infile
, "\t\t");
583 f_print(fout
, "\t} else {\n");
584 write_rpc_svc_fg(infile
, "\t\t");
585 f_print(fout
, "\t\tsock = RPC_ANYSOCK;\n");
586 print_pmapunset("\t\t");
587 f_print(fout
, "\t}\n");
591 print_return(const char *space
)
594 f_print(fout
, "%sexit(0);\n", space
);
598 f_print(fout
, "%spthread_mutex_lock(&_svcstate_lock);\n", space
);
599 f_print(fout
, "%s_rpcsvcstate = _SERVED;\n", space
);
601 f_print(fout
, "%spthread_mutex_unlock(&_svcstate_lock);\n", space
);
603 f_print(fout
, "%sreturn;\n", space
);
608 print_pmapunset(const char *space
)
614 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
615 def
= (definition
*) l
->val
;
616 if (def
->def_kind
== DEF_PROGRAM
) {
617 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
;
619 f_print(fout
, "%spmap_unset(%s, %s);\n",
620 space
, def
->def_name
, vp
->vers_name
);
627 print_err_message(const char *space
)
630 f_print(fout
, "%ssyslog(LOG_ERR, \"%s\");\n", space
, _errbuf
);
631 else if (inetdflag
|| pmflag
)
632 f_print(fout
, "%s_msgout(\"%s\");\n", space
, _errbuf
);
634 f_print(fout
, "%sfprintf(stderr, \"%s\");\n", space
, _errbuf
);
638 * Write the server auxiliary function (_msgout, timeout)
641 write_svc_aux(int nomain
)
646 write_timeout_func();
650 * Write the _msgout function
658 * Avoid making _msgout() static -- it's useful to have it visible
659 * in the toplevel RPC server code.
661 f_print(fout
, "static\n");
662 f_print(fout
, "void _msgout(const char* msg)\n");
663 f_print(fout
, "{\n");
664 f_print(fout
, "#ifdef RPC_SVC_FG\n");
665 if (inetdflag
|| pmflag
)
666 f_print(fout
, "\tif (_rpcpmstart)\n");
667 f_print(fout
, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
668 f_print(fout
, "\telse\n");
670 "\t\tfprintf(stderr, \"%%s\\n\", msg);\n");
671 f_print(fout
, "#else\n");
672 f_print(fout
, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
673 f_print(fout
, "#endif\n");
674 f_print(fout
, "}\n");
678 * Write the timeout function
681 write_timeout_func(void)
687 f_print(fout
, "static void\n");
688 f_print(fout
, "closedown(int sig)\n");
689 f_print(fout
, "{\n");
691 f_print(fout
, "\tpthread_mutex_lock(&_svcstate_lock);\n");
692 f_print(fout
, "\tif (_rpcsvcstate == _IDLE) {\n");
693 f_print(fout
, "\t\textern fd_set svc_fdset;\n");
694 f_print(fout
, "\t\tstatic int size;\n");
695 f_print(fout
, "\t\tint i, openfd;\n");
696 if (tirpcflag
&& pmflag
) {
697 f_print(fout
, "\t\tstruct t_info tinfo;\n\n");
699 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
701 f_print(fout
, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
703 f_print(fout
, "\t\t\texit(0);\n");
704 f_print(fout
, "\t\tif (size == 0) {\n");
706 f_print(fout
, "\t\t\tstruct rlimit rl;\n\n");
707 f_print(fout
, "\t\t\trl.rlim_max = 0;\n");
708 f_print(fout
, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n");
709 f_print(fout
, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
712 f_print(fout
, "\t\t\t\tpthread_mutex_unlock(&_svcstate_lock);\n");
714 f_print(fout
, "\t\t\t\treturn;\n\t\t\t}\n");
716 f_print(fout
, "\t\t\tsize = getdtablesize();\n");
718 f_print(fout
, "\t\t}\n");
720 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
721 f_print(fout
, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
722 f_print(fout
, "\t\t\t\topenfd++;\n");
723 f_print(fout
, "\t\tif (openfd <= 1)\n");
724 f_print(fout
, "\t\t\texit(0);\n");
725 f_print(fout
, "\t}\n");
726 f_print(fout
, "\tif (_rpcsvcstate == _SERVED)\n");
727 f_print(fout
, "\t\t_rpcsvcstate = _IDLE;\n\n");
729 f_print(fout
, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
731 f_print(fout
, "\tsignal(SIGALRM, closedown);\n");
732 f_print(fout
, "\talarm(_RPCSVC_CLOSEDOWN/2);\n");
733 f_print(fout
, "}\n");
738 * Write the most of port monitor support
741 write_pm_most(const char *infile
, int netflag
)
749 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
750 f_print(fout
, "\t\tsocklen_t ssize = sizeof (int);\n");
752 f_print(fout
, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
753 f_print(fout
, "\t\t(!strcmp(mname, \"sockmod\") ||");
754 f_print(fout
, " !strcmp(mname, \"timod\"))) {\n");
756 f_print(fout
, "\t\tchar *netid;\n");
757 if (!netflag
) { /* Not included by -n option */
758 f_print(fout
, "\t\tstruct netconfig *nconf = NULL;\n");
759 f_print(fout
, "\t\tSVCXPRT *%s;\n", TRANSP
);
762 f_print(fout
, "\t\tint pmclose;\n");
764 * Not necessary, defined in /usr/include/stdlib
765 * f_print(fout, "\t\textern char *getenv();\n");
769 f_print(fout
, "\t\tif (saddr.ss_family != AF_INET &&\n");
770 f_print(fout
, "\t\t saddr.ss_family != AF_INET6)\n");
771 f_print(fout
, "\t\t\texit(1);\n");
772 f_print(fout
, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
773 f_print(fout
, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
774 f_print(fout
, "\t\t\texit(1);\n");
776 f_print(fout
, "\t\t_rpcpmstart = 1;\n");
777 open_log_file(infile
, "\t\t");
778 f_print(fout
, "\n\t\tif ((netid = \
779 getenv(\"NLSPROVIDER\")) == NULL) {\n");
782 f_print(fout
, "\t\t/* started from inetd */\n");
783 f_print(fout
, "\t\t\tpmclose = 1;\n");
787 f_print(fout
, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
788 sprintf(_errbuf
, "cannot get transport info");
789 print_err_message("\t\t\t\t");
792 f_print(fout
, "\n\t\t\tpmclose = 1;\t/* XXX */\n");
795 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
797 f_print(fout
, "\t\t}\n");
799 * A kludgy support for inetd services. Inetd only works with
800 * sockmod, and RPC works only with timod, hence all this jugglery
803 f_print(fout
, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
804 f_print(fout
, "\t\t\tif (ioctl(0, I_POP, 0) || ");
805 f_print(fout
, "ioctl(0, I_PUSH, \"timod\")) {\n");
806 sprintf(_errbuf
, "could not get the right module");
807 print_err_message("\t\t\t\t");
808 f_print(fout
, "\t\t\t\texit(1);\n");
809 f_print(fout
, "\t\t\t}\n");
810 f_print(fout
, "\t\t}\n");
814 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, \
815 RPC_MAXDATASIZE, RPC_MAXDATASIZE)) \
820 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
824 sprintf(_errbuf
, "cannot create server handle");
825 print_err_message("\t\t\t");
826 f_print(fout
, "\t\t\texit(1);\n");
827 f_print(fout
, "\t\t}\n");
828 f_print(fout
, "\t\tif (nconf)\n");
829 f_print(fout
, "\t\t\tfreenetconfigent(nconf);\n");
830 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
831 def
= (definition
*) l
->val
;
832 if (def
->def_kind
!= DEF_PROGRAM
)
834 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
835 f_print(fout
, "\t\tif (!svc_reg(%s, %s, %s, ",
836 TRANSP
, def
->def_name
, vp
->vers_name
);
837 pvname(def
->def_name
, vp
->vers_num
);
838 f_print(fout
, ", 0)) {\n");
839 sprintf(_errbuf
, "unable to register (%s, %s).",
840 def
->def_name
, vp
->vers_name
);
841 print_err_message("\t\t\t");
842 f_print(fout
, "\t\t\texit(1);\n");
843 f_print(fout
, "\t\t}\n");
847 f_print(fout
, "\t\tif (pmclose) {\n");
848 f_print(fout
, "\t\t\tsignal(SIGALRM, closedown);\n");
849 f_print(fout
, "\t\t\talarm(_RPCSVC_CLOSEDOWN/2);\n");
850 f_print(fout
, "\t\t}\n");
852 f_print(fout
, "\t\tsvc_run();\n");
853 f_print(fout
, "\t\texit(1);\n");
854 f_print(fout
, "\t\t/* NOTREACHED */\n");
855 f_print(fout
, "\t}");
859 * Support for backgrounding the server if self started.
862 write_rpc_svc_fg(const char *infile
, const char *sp
)
864 f_print(fout
, "#ifndef RPC_SVC_FG\n");
865 f_print(fout
, "%sint size;\n", sp
);
867 f_print(fout
, "%sstruct rlimit rl;\n", sp
);
869 f_print(fout
, "%sint pid, i;\n\n", sp
);
870 f_print(fout
, "%spid = fork();\n", sp
);
871 f_print(fout
, "%sif (pid < 0) {\n", sp
);
872 f_print(fout
, "%s\tperror(\"cannot fork\");\n", sp
);
873 f_print(fout
, "%s\texit(1);\n", sp
);
874 f_print(fout
, "%s}\n", sp
);
875 f_print(fout
, "%sif (pid)\n", sp
);
876 f_print(fout
, "%s\texit(0);\n", sp
);
877 /* get number of file descriptors */
879 f_print(fout
, "%srl.rlim_max = 0;\n", sp
);
880 f_print(fout
, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp
);
881 f_print(fout
, "%sif ((size = rl.rlim_max) == 0)\n", sp
);
882 f_print(fout
, "%s\texit(1);\n", sp
);
884 f_print(fout
, "%ssize = getdtablesize();\n", sp
);
887 f_print(fout
, "%sfor (i = 0; i < size; i++)\n", sp
);
888 f_print(fout
, "%s\tclose(i);\n", sp
);
889 /* Redirect stderr and stdout to console */
890 f_print(fout
, "%si = open(\"/dev/console\", 2);\n", sp
);
891 f_print(fout
, "%sdup2(i, 1);\n", sp
);
892 f_print(fout
, "%sdup2(i, 2);\n", sp
);
893 /* This removes control of the controlling terminal */
895 f_print(fout
, "%ssetsid();\n", sp
);
897 f_print(fout
, "%si = open(\"/dev/tty\", 2);\n", sp
);
898 f_print(fout
, "%sif (i >= 0) {\n", sp
);
900 "%s\tioctl(i, TIOCNOTTY, NULL);\n", sp
);
901 f_print(fout
, "%s\tclose(i);\n", sp
);
902 f_print(fout
, "%s}\n", sp
);
905 open_log_file(infile
, sp
);
906 f_print(fout
, "#endif\n");
908 open_log_file(infile
, sp
);
912 open_log_file(const char *infile
, const char *sp
)
916 s
= strrchr(infile
, '.');
919 f_print(fout
, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp
, infile
);
925 * write a registration for the given transport for Inetd
928 write_inetd_register(const char *transp
)
941 if (streq(transp
, "udp"))
948 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
949 isudp
? "SOCK_DGRAM" : "SOCK_STREAM");
951 f_print(fout
, "%s\t%s = svc%s_create(%s",
952 sp
, TRANSP
, transp
, inetdflag
? "sock": "RPC_ANYSOCK");
954 f_print(fout
, ", 0, 0");
955 f_print(fout
, ");\n");
956 f_print(fout
, "%s\tif (%s == NULL) {\n", sp
, TRANSP
);
957 sprintf(_errbuf
, "cannot create %s service.", transp
);
958 sprintf(tmpbuf
, "%s\t\t", sp
);
959 print_err_message(tmpbuf
);
960 f_print(fout
, "%s\t\texit(1);\n", sp
);
961 f_print(fout
, "%s\t}\n", sp
);
964 f_print(fout
, "%s\tif (!_rpcpmstart)\n\t", sp
);
965 f_print(fout
, "%s\tproto = IPPROTO_%s;\n", sp
,
966 isudp
? "UDP": "TCP");
968 for (l
= defined
; l
!= NULL
; l
= l
->next
) {
969 def
= (definition
*) l
->val
;
970 if (def
->def_kind
!= DEF_PROGRAM
)
972 for (vp
= def
->def
.pr
.versions
; vp
!= NULL
; vp
= vp
->next
) {
973 f_print(fout
, "%s\tif (!svc_register(%s, %s, %s, ",
974 sp
, TRANSP
, def
->def_name
, vp
->vers_name
);
975 pvname(def
->def_name
, vp
->vers_num
);
977 f_print(fout
, ", proto)) {\n");
979 f_print(fout
, ", IPPROTO_%s)) {\n",
980 isudp
? "UDP": "TCP");
981 sprintf(_errbuf
, "unable to register (%s, %s, %s).",
982 def
->def_name
, vp
->vers_name
, transp
);
983 print_err_message(tmpbuf
);
984 f_print(fout
, "%s\t\texit(1);\n", sp
);
985 f_print(fout
, "%s\t}\n", sp
);
989 f_print(fout
, "\t}\n");