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
31 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
32 * Copyright (C) 1987, Sun Microsystems, Inc.
36 #include "rpc_parse.h"
40 static char RQSTP[] = "rqstp";
41 static char TRANSP[] = "transp";
42 static char ARG[] = "argument";
43 static char RESULT[] = "result";
44 static char ROUTINE[] = "local";
45 static char RETVAL[] = "retval";
47 static char _errbuf[256]; /* For all messages */
49 void internal_proctype( proc_list * );
50 static void write_real_program( definition * );
51 static void write_program(definition *, const char *);
52 static void printerr(const char *, const char *);
53 static void printif(const char *, const char *, const char *, const char *);
54 static void write_inetmost(const char *);
55 static void print_return(const char *);
56 static void print_pmapunset(const char *);
57 static void print_err_message(const char *);
58 static void write_timeout_func( void );
59 static void write_pm_most(const char *, int);
60 static void write_rpc_svc_fg(const char *, const char *);
61 static void open_log_file(const char *, const char *);
62 static void write_msg_out( void );
66 p_xdrfunc(const char *rname, const char *typename)
68 f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n",
69 rname, stringfix(typename));
73 internal_proctype(proc_list *plist)
75 f_print(fout, "static ");
76 ptype(plist->res_prefix, plist->res_type, 1);
82 * write most of the service, that is, everything but the registrations.
85 write_most(const char *infile, int netflag, int nomain)
87 if (inetdflag || pmflag) {
89 var_type = (nomain? "extern" : "static");
90 f_print(fout, "%s int _rpcpmstart;", var_type);
91 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
92 if (!tirpcflag || tirpc_socket) {
93 f_print(fout, "%s int _rpcfdtype;", var_type);
94 f_print(fout, "\n\t\t /* Whether Stream or \
99 f_print(fout, " /* States a server can be in \
100 wrt request */\n\n");
101 f_print(fout, "#define\t_IDLE 0\n");
102 f_print(fout, "#define\t_SERVED 1\n");
103 f_print(fout, "#define\t_SERVING 2\n\n");
104 f_print(fout, "static int _rpcsvcstate = _IDLE;");
105 f_print(fout, "\t /* Set when a request is \
109 f_print(fout, "pthread_mutex_t _svcstate_lock;");
110 f_print(fout, "\t\t\t/* Mutex lock for variable _rpcsvcstate */\n");
116 write_svc_aux(nomain);
118 /* write out dispatcher and stubs */
119 write_programs((char *)NULL);
124 f_print(fout, "\nint\n");
125 f_print(fout, "main()\n");
126 f_print(fout, "{\n");
128 write_inetmost(infile);
129 /* Includes call to write_rpc_svc_fg() */
134 "\tregister SVCXPRT *%s;\n", TRANSP);
136 "\tstruct netconfig *nconf = NULL;\n");
138 f_print(fout, "\tpid_t pid;\n");
139 f_print(fout, "\tint i;\n");
142 f_print(fout, "\tstruct sockaddr_storage saddr;\n");
143 f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n\n");
145 f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
148 if (mtflag & timerflag)
149 f_print(fout, "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
151 write_pm_most(infile, netflag);
152 f_print(fout, "\telse {\n");
153 write_rpc_svc_fg(infile, "\t\t");
154 f_print(fout, "\t}\n");
156 write_rpc_svc_fg(infile, "\t\t");
159 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
161 print_pmapunset("\t");
165 if (logflag && !inetdflag) {
166 open_log_file(infile, "\t");
171 * write a registration for the given transport
174 write_netid_register(const char *transp)
184 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
185 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
186 (void) sprintf(_errbuf, "cannot find %s netid.", transp);
187 sprintf(tmpbuf, "%s\t\t", sp);
188 print_err_message(tmpbuf);
189 f_print(fout, "%s\t\texit(1);\n", sp);
190 f_print(fout, "%s\t}\n", sp);
192 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, ",
194 f_print(fout,"nconf, 0, RPC_MAXDATASIZE, RPC_MAXDATASIZE);\n");
197 "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
200 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
201 (void) sprintf(_errbuf, "cannot create %s service.", transp);
202 print_err_message(tmpbuf);
203 f_print(fout, "%s\t\texit(1);\n", sp);
204 f_print(fout, "%s\t}\n", sp);
206 for (l = defined; l != NULL; l = l->next) {
207 def = (definition *) l->val;
208 if (def->def_kind != DEF_PROGRAM) {
211 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
213 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
214 sp, def->def_name, vp->vers_name);
216 "%s\tif (!svc_reg(%s, %s, %s, ",
217 sp, TRANSP, def->def_name, vp->vers_name);
218 pvname(def->def_name, vp->vers_num);
219 f_print(fout, ", nconf)) {\n");
220 (void) sprintf(_errbuf,
221 "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) {
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 (void) sprintf(_errbuf,
252 "unable to create (%s, %s) for %s.",
253 def->def_name, vp->vers_name, transp);
254 print_err_message("\t\t");
255 f_print(fout, "\t\texit(1);\n");
256 f_print(fout, "\t}\n");
262 * write the rest of the service
269 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
270 (void) sprintf(_errbuf, "could not create a handle");
271 print_err_message("\t\t");
272 f_print(fout, "\t\texit(1);\n");
273 f_print(fout, "\t}\n");
275 f_print(fout, "\tif (_rpcpmstart) {\n");
277 "\t\t(void) signal(SIGALRM, closedown);\n");
278 f_print(fout, "\t\t(void) \
279 alarm(_RPCSVC_CLOSEDOWN/2);\n");
280 f_print(fout, "\t}\n");
283 f_print(fout, "\tsvc_run();\n");
284 (void) sprintf(_errbuf, "svc_run returned");
285 print_err_message("\t");
286 f_print(fout, "\texit(1);\n");
287 f_print(fout, "\t/* NOTREACHED */\n");
288 f_print(fout, "}\n");
292 write_programs(const char *storage)
297 /* write out stubs for procedure definitions */
298 for (l = defined; l != NULL; l = l->next) {
299 def = (definition *) l->val;
300 if (def->def_kind == DEF_PROGRAM) {
301 write_real_program(def);
305 /* write out dispatcher for each program */
306 for (l = defined; l != NULL; l = l->next) {
307 def = (definition *) l->val;
308 if (def->def_kind == DEF_PROGRAM) {
309 write_program(def, storage);
317 * write out definition of internal function (e.g. _printmsg_1(...))
318 * which calls server's definition of actual function (e.g. printmsg_1(...)).
319 * Unpacks single user argument of printmsg_1 to call-by-value format
320 * expected by printmsg_1.
323 write_real_program(definition *def)
329 if (!newstyle) return; /* not needed for old style */
330 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
331 for (proc = vp->procs; proc != NULL; proc = proc->next) {
334 internal_proctype(proc);
336 f_print(fout, "int");
337 f_print(fout, "\n_");
338 pvname(proc->proc_name, vp->vers_num);
341 if (proc->arg_num > 1)
342 fputs(proc->args.argname, fout);
344 ptype(proc->args.decls->decl.prefix,
345 proc->args.decls->decl.type, 0);
347 f_print(fout, " *argp, void *%s, struct svc_req *%s)\n",
353 f_print(fout, " *argp, struct svc_req *%s)\n",
356 f_print(fout, "{\n");
357 f_print(fout, "\treturn (");
358 pvname_svc(proc->proc_name, vp->vers_num);
360 if (proc->arg_num < 2) { /* single argument */
361 if (!streq(proc->args.decls->decl.type, "void"))
362 f_print(fout, "*argp, "); /* non-void */
364 for (l = proc->args.decls; l != NULL;
366 f_print(fout, "argp->%s, ",
370 f_print(fout, "%s, ",RESULT);
371 f_print(fout, "%s));\n}\n", RQSTP);
377 write_program(definition *def, const char *storage)
383 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
385 if (storage != NULL) {
386 f_print(fout, "%s ", storage);
388 f_print(fout, "void\n");
389 pvname(def->def_name, vp->vers_num);
391 f_print(fout, "(struct svc_req *%s, ", RQSTP);
392 f_print(fout, "SVCXPRT *%s)\n", TRANSP);
393 f_print(fout, "{\n");
396 f_print(fout, "\tunion {\n");
397 for (proc = vp->procs; proc != NULL; proc = proc->next) {
398 if (proc->arg_num < 2) { /* single argument */
399 if (streq(proc->args.decls->decl.type,
404 f_print(fout, "\t\t");
405 ptype(proc->args.decls->decl.prefix,
406 proc->args.decls->decl.type, 0);
407 pvname(proc->proc_name, vp->vers_num);
408 f_print(fout, "_arg;\n");
412 f_print(fout, "\t\t%s", proc->args.argname);
414 pvname(proc->proc_name, vp->vers_num);
415 f_print(fout, "_arg;\n");
419 f_print(fout, "\t\tint fill;\n");
421 f_print(fout, "\t} %s;\n", ARG);
424 f_print(fout, "\tunion {\n");
425 for (proc = vp->procs; proc != NULL; proc = proc->next) {
426 if (streq(proc->res_type, "void")) {
429 f_print(fout, "\t\t");
430 ptype(proc->res_prefix, proc->res_type, 0);
431 pvname(proc->proc_name, vp->vers_num);
432 f_print(fout, "_res;\n");
434 f_print(fout, "\t} %s;\n", RESULT);
435 f_print(fout, "\tbool_t %s;\n", RETVAL);
438 f_print(fout, "\tchar *%s;\n", RESULT);
440 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
443 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
447 "\tchar *(*%s)(char *, struct svc_req *);\n",
453 f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n");
455 f_print(fout, "\t_rpcsvcstate = _SERVING;\n");
457 f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
460 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
461 if (!nullproc(vp->procs)) {
462 f_print(fout, "\tcase NULLPROC:\n");
464 "\t\t(void) svc_sendreply(%s,\n\t\t\t"
465 "(xdrproc_t) xdr_void, (char *)NULL);\n",
467 print_return("\t\t");
470 for (proc = vp->procs; proc != NULL; proc = proc->next) {
471 f_print(fout, "\tcase %s:\n", proc->proc_name);
472 if (proc->arg_num < 2) { /* single argument */
473 p_xdrfunc(ARG, proc->args.decls->decl.type);
475 p_xdrfunc(ARG, proc->args.argname);
477 p_xdrfunc(RESULT, proc->res_type);
481 "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))",
485 "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
487 if (newstyle) { /* new style: calls internal routine */
491 pvname_svc(proc->proc_name, vp->vers_num);
493 pvname(proc->proc_name, vp->vers_num);
494 f_print(fout, ";\n");
495 f_print(fout, "\t\tbreak;\n\n");
497 f_print(fout, "\tdefault:\n");
498 printerr("noproc", TRANSP);
499 print_return("\t\t");
500 f_print(fout, "\t}\n");
503 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
505 printif("getargs", TRANSP, "(caddr_t) &", ARG);
506 printerr("decode", TRANSP);
507 print_return("\t\t");
508 f_print(fout, "\t}\n");
511 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
512 RESULT, ROUTINE, ARG, RQSTP);
514 f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n",
515 RETVAL, ROUTINE, ARG, RESULT, RQSTP);
520 "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n",
521 RETVAL, TRANSP, RESULT, RESULT);
524 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
525 RESULT, TRANSP, RESULT, RESULT);
527 printerr("systemerr", TRANSP);
528 f_print(fout, "\t}\n");
530 printif("freeargs", TRANSP, "(caddr_t) &", ARG);
531 (void) sprintf(_errbuf, "unable to free arguments");
532 print_err_message("\t\t");
533 f_print(fout, "\t\texit(1);\n");
534 f_print(fout, "\t}\n");
535 /* print out free routine */
537 f_print(fout,"\tif (!");
538 pvname(def->def_name, vp->vers_num);
539 f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n",
540 TRANSP, RESULT, RESULT);
541 (void) sprintf(_errbuf, "unable to free results");
542 print_err_message("\t\t");
546 f_print(fout, "}\n");
551 printerr(const char *err, const char *transp)
553 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
557 printif(const char *proc, const char *transp, const char *prefix,
560 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n",
561 proc, transp, arg, prefix, arg);
565 nullproc(proc_list *proc)
567 for (; proc != NULL; proc = proc->next) {
568 if (streq(proc->proc_num, "0")) {
576 write_inetmost(const char *infile)
578 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
579 f_print(fout, "\tint sock;\n");
580 f_print(fout, "\tint proto;\n");
581 f_print(fout, "\tstruct sockaddr_in saddr;\n");
582 f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n");
585 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
586 f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n\n");
587 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
588 f_print(fout, "\t\t\texit(1);\n");
589 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
590 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
591 f_print(fout, "\t\t\texit(1);\n");
592 f_print(fout, "\t\tsock = 0;\n");
593 f_print(fout, "\t\t_rpcpmstart = 1;\n");
594 f_print(fout, "\t\tproto = 0;\n");
595 open_log_file(infile, "\t\t");
596 f_print(fout, "\t} else {\n");
597 write_rpc_svc_fg(infile, "\t\t");
598 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
599 print_pmapunset("\t\t");
600 f_print(fout, "\t}\n");
604 print_return(const char *space)
607 f_print(fout, "%sexit(0);\n", space);
611 f_print(fout, "%spthread_mutex_lock(&_svcstate_lock);\n", space);
612 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
614 f_print(fout, "%spthread_mutex_unlock(&_svcstate_lock);\n", space);
616 f_print(fout, "%sreturn;\n", space);
621 print_pmapunset(const char *space)
627 for (l = defined; l != NULL; l = l->next) {
628 def = (definition *) l->val;
629 if (def->def_kind == DEF_PROGRAM) {
630 for (vp = def->def.pr.versions; vp != NULL;
632 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
633 space, def->def_name, vp->vers_name);
640 print_err_message(const char *space)
643 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
644 else if (inetdflag || pmflag)
645 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
647 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
651 * Write the server auxiliary function (_msgout, timeout)
654 write_svc_aux(int nomain)
659 write_timeout_func();
663 * Write the _msgout function
671 * Avoid making _msgout() static -- it's useful to have it visible
672 * in the toplevel RPC server code.
674 f_print(fout, "static\n");
675 f_print(fout, "void _msgout(const char* msg)\n");
676 f_print(fout, "{\n");
677 f_print(fout, "#ifdef RPC_SVC_FG\n");
678 if (inetdflag || pmflag)
679 f_print(fout, "\tif (_rpcpmstart)\n");
680 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n");
681 f_print(fout, "\telse\n");
683 "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n");
684 f_print(fout, "#else\n");
685 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n");
686 f_print(fout, "#endif\n");
687 f_print(fout, "}\n");
691 * Write the timeout function
694 write_timeout_func(void)
700 f_print(fout, "static void\n");
701 f_print(fout, "closedown(int sig)\n");
702 f_print(fout, "{\n");
704 f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n");
705 f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n");
706 f_print(fout, "\t\textern fd_set svc_fdset;\n");
707 f_print(fout, "\t\tstatic int size;\n");
708 f_print(fout, "\t\tint i, openfd;\n");
709 if (tirpcflag && pmflag) {
710 f_print(fout, "\t\tstruct t_info tinfo;\n\n");
712 "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
714 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
716 f_print(fout, "\t\t\texit(0);\n");
717 f_print(fout, "\t\tif (size == 0) {\n");
719 f_print(fout, "\t\t\tstruct rlimit rl;\n\n");
720 f_print(fout, "\t\t\trl.rlim_max = 0;\n");
721 f_print(fout, "\t\t\tif (getrlimit(RLIMIT_NOFILE, &rl) == -1)\n");
722 f_print(fout, "\t\t\t\treturn;\n");
723 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n");
726 f_print(fout, "\t\t\t\tpthread_mutex_unlock(&_svcstate_lock);\n");
728 f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n");
730 f_print(fout, "\t\t\tsize = getdtablesize();\n");
732 f_print(fout, "\t\t}\n");
734 "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n");
735 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
736 f_print(fout, "\t\t\t\topenfd++;\n");
737 f_print(fout, "\t\tif (openfd <= 1)\n");
738 f_print(fout, "\t\t\texit(0);\n");
739 f_print(fout, "\t}\n");
740 f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n");
741 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
743 f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n");
745 f_print(fout, "\t(void) signal(SIGALRM, closedown);\n");
746 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
747 f_print(fout, "}\n");
752 * Write the most of port monitor support
755 write_pm_most(const char *infile, int netflag)
763 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
764 f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n");
766 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
767 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
768 f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
770 f_print(fout, "\t\tchar *netid;\n");
771 if (!netflag) { /* Not included by -n option */
772 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
773 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
776 f_print(fout, "\t\tint pmclose;\n");
778 * Not necessary, defined in /usr/include/stdlib
779 * f_print(fout, "\t\textern char *getenv();\n");
783 f_print(fout, "\t\tif (saddr.ss_family != AF_INET &&\n");
784 f_print(fout, "\t\t saddr.ss_family != AF_INET6)\n");
785 f_print(fout, "\t\t\texit(1);\n");
786 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
787 f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n");
788 f_print(fout, "\t\t\texit(1);\n");
790 f_print(fout, "\t\t_rpcpmstart = 1;\n");
791 open_log_file(infile, "\t\t");
792 f_print(fout, "\n\t\tif ((netid = \
793 getenv(\"NLSPROVIDER\")) == NULL) {\n");
796 f_print(fout, "\t\t/* started from inetd */\n");
797 f_print(fout, "\t\t\tpmclose = 1;\n");
801 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
802 sprintf(_errbuf, "cannot get transport info");
803 print_err_message("\t\t\t\t");
806 f_print(fout, "\n\t\t\tpmclose = 1;\t/* XXX */\n");
809 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
811 f_print(fout, "\t\t}\n");
813 * A kludgy support for inetd services. Inetd only works with
814 * sockmod, and RPC works only with timod, hence all this jugglery
817 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
818 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ");
819 f_print(fout, "ioctl(0, I_PUSH, \"timod\")) {\n");
820 sprintf(_errbuf, "could not get the right module");
821 print_err_message("\t\t\t\t");
822 f_print(fout, "\t\t\t\texit(1);\n");
823 f_print(fout, "\t\t\t}\n");
824 f_print(fout, "\t\t}\n");
828 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, \
829 RPC_MAXDATASIZE, RPC_MAXDATASIZE)) \
834 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \
838 sprintf(_errbuf, "cannot create server handle");
839 print_err_message("\t\t\t");
840 f_print(fout, "\t\t\texit(1);\n");
841 f_print(fout, "\t\t}\n");
842 f_print(fout, "\t\tif (nconf)\n");
843 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
844 for (l = defined; l != NULL; l = l->next) {
845 def = (definition *) l->val;
846 if (def->def_kind != DEF_PROGRAM) {
849 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
851 "\t\tif (!svc_reg(%s, %s, %s, ",
852 TRANSP, def->def_name, vp->vers_name);
853 pvname(def->def_name, vp->vers_num);
854 f_print(fout, ", 0)) {\n");
855 (void) sprintf(_errbuf, "unable to register (%s, %s).",
856 def->def_name, vp->vers_name);
857 print_err_message("\t\t\t");
858 f_print(fout, "\t\t\texit(1);\n");
859 f_print(fout, "\t\t}\n");
863 f_print(fout, "\t\tif (pmclose) {\n");
864 f_print(fout, "\t\t\t(void) signal(SIGALRM, closedown);\n");
865 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
866 f_print(fout, "\t\t}\n");
868 f_print(fout, "\t\tsvc_run();\n");
869 f_print(fout, "\t\texit(1);\n");
870 f_print(fout, "\t\t/* NOTREACHED */\n");
871 f_print(fout, "\t}");
875 * Support for backgrounding the server if self started.
878 write_rpc_svc_fg(const char *infile, const char *sp)
880 f_print(fout, "#ifndef RPC_SVC_FG\n");
881 f_print(fout, "%sint size;\n", sp);
883 f_print(fout, "%sstruct rlimit rl;\n", sp);
885 f_print(fout, "%sint pid, i;\n\n", sp);
886 f_print(fout, "%spid = fork();\n", sp);
887 f_print(fout, "%sif (pid < 0) {\n", sp);
888 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
889 f_print(fout, "%s\texit(1);\n", sp);
890 f_print(fout, "%s}\n", sp);
891 f_print(fout, "%sif (pid)\n", sp);
892 f_print(fout, "%s\texit(0);\n", sp);
893 /* get number of file descriptors */
895 f_print(fout, "%srl.rlim_max = 0;\n", sp);
896 f_print(fout, "%sif (getrlimit(RLIMIT_NOFILE, &rl) == -1) {\n",
898 f_print(fout, "%s\tperror(\"getrlimit\");\n", sp);
899 f_print(fout, "%s\texit(1);\n", sp);
900 f_print(fout, "%s}\n", sp);
901 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
902 f_print(fout, "%s\texit(1);\n", sp);
904 f_print(fout, "%ssize = getdtablesize();\n", sp);
907 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
908 f_print(fout, "%s\t(void) close(i);\n", sp);
909 /* Redirect stderr and stdout to console */
910 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
911 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
912 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
913 /* This removes control of the controlling terminal */
915 f_print(fout, "%ssetsid();\n", sp);
917 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
918 f_print(fout, "%sif (i >= 0) {\n", sp);
920 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
921 f_print(fout, "%s\t(void) close(i);\n", sp);
922 f_print(fout, "%s}\n", sp);
925 open_log_file(infile, sp);
926 f_print(fout, "#endif\n");
928 open_log_file(infile, sp);
932 open_log_file(const char *infile, const char *sp)
936 s = strrchr(infile, '.');
939 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
948 * write a registration for the given transport for Inetd
951 write_inetd_register(const char *transp)
964 if (streq(transp, "udp"))
971 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
972 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
974 f_print(fout, "%s\t%s = svc%s_create(%s",
975 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
977 f_print(fout, ", 0, 0");
978 f_print(fout, ");\n");
979 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
980 (void) sprintf(_errbuf, "cannot create %s service.", transp);
981 (void) sprintf(tmpbuf, "%s\t\t", sp);
982 print_err_message(tmpbuf);
983 f_print(fout, "%s\t\texit(1);\n", sp);
984 f_print(fout, "%s\t}\n", sp);
987 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
988 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
989 sp, isudp ? "UDP": "TCP");
991 for (l = defined; l != NULL; l = l->next) {
992 def = (definition *) l->val;
993 if (def->def_kind != DEF_PROGRAM) {
996 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
997 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
998 sp, TRANSP, def->def_name, vp->vers_name);
999 pvname(def->def_name, vp->vers_num);
1001 f_print(fout, ", proto)) {\n");
1003 f_print(fout, ", IPPROTO_%s)) {\n",
1004 isudp ? "UDP": "TCP");
1005 (void) sprintf(_errbuf,
1006 "unable to register (%s, %s, %s).",
1007 def->def_name, vp->vers_name, transp);
1008 print_err_message(tmpbuf);
1009 f_print(fout, "%s\t\texit(1);\n", sp);
1010 f_print(fout, "%s\t}\n", sp);
1014 f_print(fout, "\t}\n");