[BACK]Return to rpc_svcout.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / rpcgen

Annotation of src/usr.bin/rpcgen/rpc_svcout.c, Revision 1.9

1.8       deraadt     1: /*     $OpenBSD: rpc_svcout.c,v 1.7 2001/11/24 17:59:22 miod Exp $     */
1.1       deraadt     2: /*     $NetBSD: rpc_svcout.c,v 1.7 1995/06/24 14:59:59 pk Exp $        */
                      3: /*
                      4:  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
                      5:  * unrestricted use provided that this legend is included on all tape
                      6:  * media and as a part of the software program in whole or part.  Users
                      7:  * may copy or modify Sun RPC without charge, but are not authorized
                      8:  * to license or distribute it to anyone else except as part of a product or
                      9:  * program developed by the user or with the express written consent of
                     10:  * Sun Microsystems, Inc.
                     11:  *
                     12:  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
                     13:  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
                     14:  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
                     15:  *
                     16:  * Sun RPC is provided with no support and without any obligation on the
                     17:  * part of Sun Microsystems, Inc. to assist in its use, correction,
                     18:  * modification or enhancement.
                     19:  *
                     20:  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
                     21:  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
                     22:  * OR ANY PART THEREOF.
                     23:  *
                     24:  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
                     25:  * or profits or other special, indirect and consequential damages, even if
                     26:  * Sun has been advised of the possibility of such damages.
                     27:  *
                     28:  * Sun Microsystems, Inc.
                     29:  * 2550 Garcia Avenue
                     30:  * Mountain View, California  94043
                     31:  */
                     32:
                     33: #ifndef lint
                     34:  static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI";
                     35: #endif
                     36:
                     37: /*
                     38:  * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
                     39:  */
                     40: #include <sys/cdefs.h>
                     41: #include <stdio.h>
                     42: #include <string.h>
                     43: #include "rpc_parse.h"
                     44: #include "rpc_util.h"
                     45:
                     46: static char RQSTP[] = "rqstp";
                     47: static char TRANSP[] = "transp";
                     48: static char ARG[] = "argument";
                     49: static char RESULT[] = "result";
                     50: static char ROUTINE[] = "local";
                     51:
                     52: char _errbuf[256];     /* For all messages */
                     53:
1.5       deraadt    54: void internal_proctype __P((proc_list *));
                     55: static write_real_program __P((definition *));
                     56: static write_program __P((definition *, char *));
                     57: static printerr __P((char *, char *));
                     58: static printif __P((char *, char *, char *, char *));
                     59: static write_inetmost __P((char *));
                     60: static print_return __P((char *));
                     61: static print_pmapunset __P((char *));
                     62: static print_err_message __P((char *));
                     63: static write_timeout_func __P((void));
                     64: static write_pm_most __P((char *, int));
                     65: static write_caller_func __P((void));
                     66: static write_rpc_svc_fg __P((char *, char *));
                     67: static open_log_file __P((char *, char *));
1.1       deraadt    68:
1.5       deraadt    69: static
1.9     ! deraadt    70: p_xdrfunc( rname, typename )
        !            71: char* rname;
        !            72: char* typename;
        !            73: {
        !            74:   if( Cflag )
        !            75:     f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname,
        !            76:            stringfix(typename) );
        !            77:   else
        !            78:     f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname, stringfix(typename) );
        !            79: }
1.1       deraadt    80:
1.5       deraadt    81: void
1.1       deraadt    82: internal_proctype(plist)
                     83:        proc_list *plist;
                     84: {
                     85:        f_print(fout, "static ");
1.9     ! deraadt    86:        ptype( plist->res_prefix, plist->res_type, 1 );
        !            87:        f_print( fout, "*" );
1.1       deraadt    88: }
                     89:
1.9     ! deraadt    90:
1.1       deraadt    91: /*
1.9     ! deraadt    92:  * write most of the service, that is, everything but the registrations.
1.1       deraadt    93:  */
                     94: void
                     95: write_most(infile, netflag, nomain)
                     96:        char *infile;           /* our name */
                     97:        int netflag;
1.9     ! deraadt    98:         int nomain;
1.1       deraadt    99: {
                    100:        if (inetdflag || pmflag) {
1.9     ! deraadt   101:                char* var_type;
1.1       deraadt   102:                var_type = (nomain? "extern" : "static");
1.9     ! deraadt   103:                f_print(fout, "%s int _rpcpmstart;", var_type );
        !           104:                f_print(fout, "\t\t/* Started by a port monitor ? */\n");
        !           105:                f_print(fout, "%s int _rpcfdtype;", var_type );
1.1       deraadt   106:                f_print(fout, "\t\t/* Whether Stream or Datagram ? */\n");
                    107:                if (timerflag) {
1.9     ! deraadt   108:                        f_print(fout, "%s int _rpcsvcdirty;", var_type );
1.1       deraadt   109:                        f_print(fout, "\t/* Still serving ? */\n");
                    110:                }
1.9     ! deraadt   111:                write_svc_aux( nomain );
1.1       deraadt   112:        }
                    113:        /* write out dispatcher and stubs */
1.9     ! deraadt   114:        write_programs( nomain? (char *)NULL : "static" );
1.1       deraadt   115:
1.9     ! deraadt   116:         if( nomain )
        !           117:          return;
1.1       deraadt   118:
                    119:        f_print(fout, "\nmain()\n");
                    120:        f_print(fout, "{\n");
                    121:        if (inetdflag) {
                    122:                write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */
                    123:        } else {
1.9     ! deraadt   124:          if( tirpcflag ) {
1.1       deraadt   125:                if (netflag) {
1.6       deraadt   126:                        f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
1.1       deraadt   127:                        f_print(fout, "\tstruct netconfig *nconf = NULL;\n");
                    128:                }
                    129:                f_print(fout, "\tpid_t pid;\n");
                    130:                f_print(fout, "\tint i;\n");
                    131:                f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n");
                    132:                write_pm_most(infile, netflag);
                    133:                f_print(fout, "\telse {\n");
                    134:                write_rpc_svc_fg(infile, "\t\t");
                    135:                f_print(fout, "\t}\n");
                    136:              } else {
1.6       deraadt   137:                f_print(fout, "\tSVCXPRT *%s;\n", TRANSP);
1.1       deraadt   138:                f_print(fout, "\n");
                    139:                print_pmapunset("\t");
                    140:              }
                    141:        }
                    142:
                    143:        if (logflag && !inetdflag) {
                    144:                open_log_file(infile, "\t");
                    145:        }
                    146: }
                    147:
                    148: /*
1.9     ! deraadt   149:  * write a registration for the given transport
1.1       deraadt   150:  */
                    151: void
                    152: write_netid_register(transp)
                    153:        char *transp;
                    154: {
                    155:        list *l;
                    156:        definition *def;
                    157:        version_list *vp;
                    158:        char *sp;
                    159:        char tmpbuf[32];
                    160:
                    161:        sp = "";
                    162:        f_print(fout, "\n");
                    163:        f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
                    164:        f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
                    165:        (void) sprintf(_errbuf, "cannot find %s netid.", transp);
1.9     ! deraadt   166:        sprintf(tmpbuf, "%s\t\t", sp);
1.1       deraadt   167:        print_err_message(tmpbuf);
                    168:        f_print(fout, "%s\t\texit(1);\n", sp);
                    169:        f_print(fout, "%s\t}\n", sp);
                    170:        f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
1.9     ! deraadt   171:                        sp, TRANSP);
1.1       deraadt   172:        f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
                    173:        (void) sprintf(_errbuf, "cannot create %s service.", transp);
                    174:        print_err_message(tmpbuf);
                    175:        f_print(fout, "%s\t\texit(1);\n", sp);
                    176:        f_print(fout, "%s\t}\n", sp);
                    177:
                    178:        for (l = defined; l != NULL; l = l->next) {
                    179:                def = (definition *) l->val;
1.9     ! deraadt   180:                if (def->def_kind != DEF_PROGRAM) {
1.1       deraadt   181:                        continue;
1.9     ! deraadt   182:                }
1.1       deraadt   183:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    184:                        f_print(fout,
1.9     ! deraadt   185:                                "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
        !           186:                                sp, def->def_name, vp->vers_name);
1.1       deraadt   187:                        f_print(fout,
1.9     ! deraadt   188:                                "%s\tif (!svc_reg(%s, %s, %s, ",
        !           189:                                sp, TRANSP, def->def_name, vp->vers_name);
1.1       deraadt   190:                        pvname(def->def_name, vp->vers_num);
                    191:                        f_print(fout, ", nconf)) {\n");
                    192:                        (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
1.9     ! deraadt   193:                                        def->def_name, vp->vers_name, transp);
1.1       deraadt   194:                        print_err_message(tmpbuf);
                    195:                        f_print(fout, "%s\t\texit(1);\n", sp);
                    196:                        f_print(fout, "%s\t}\n", sp);
                    197:                }
                    198:        }
                    199:        f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
                    200: }
                    201:
                    202: /*
                    203:  * write a registration for the given transport for TLI
                    204:  */
                    205: void
                    206: write_nettype_register(transp)
                    207:        char *transp;
                    208: {
                    209:        list *l;
                    210:        definition *def;
                    211:        version_list *vp;
                    212:
                    213:        for (l = defined; l != NULL; l = l->next) {
                    214:                def = (definition *) l->val;
1.9     ! deraadt   215:                if (def->def_kind != DEF_PROGRAM) {
1.1       deraadt   216:                        continue;
1.9     ! deraadt   217:                }
1.1       deraadt   218:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    219:                        f_print(fout, "\tif (!svc_create(");
                    220:                        pvname(def->def_name, vp->vers_num);
1.6       deraadt   221:                        f_print(fout, ", %s, %s, \"%s\")) {\n",
1.9     ! deraadt   222:                                def->def_name, vp->vers_name, transp);
1.1       deraadt   223:                        (void) sprintf(_errbuf,
1.9     ! deraadt   224:                                "unable to create (%s, %s) for %s.",
        !           225:                                        def->def_name, vp->vers_name, transp);
1.1       deraadt   226:                        print_err_message("\t\t");
                    227:                        f_print(fout, "\t\texit(1);\n");
                    228:                        f_print(fout, "\t}\n");
                    229:                }
                    230:        }
                    231: }
                    232:
                    233: /*
1.9     ! deraadt   234:  * write the rest of the service
1.1       deraadt   235:  */
                    236: void
                    237: write_rest()
                    238: {
                    239:        f_print(fout, "\n");
                    240:        if (inetdflag) {
                    241:                f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
                    242:                (void) sprintf(_errbuf, "could not create a handle");
                    243:                print_err_message("\t\t");
                    244:                f_print(fout, "\t\texit(1);\n");
                    245:                f_print(fout, "\t}\n");
                    246:                if (timerflag) {
                    247:                        f_print(fout, "\tif (_rpcpmstart) {\n");
1.9     ! deraadt   248:                        f_print(fout,
        !           249:                                "\t\t(void) signal(SIGALRM, %s closedown);\n",
        !           250:                                Cflag? "(SIG_PF)" : "(void(*)())" );
1.1       deraadt   251:                        f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
                    252:                        f_print(fout, "\t}\n");
                    253:                }
                    254:        }
                    255:        f_print(fout, "\tsvc_run();\n");
                    256:        (void) sprintf(_errbuf, "svc_run returned");
                    257:        print_err_message("\t");
                    258:        f_print(fout, "\texit(1);\n");
                    259:        f_print(fout, "\t/* NOTREACHED */\n");
                    260:        f_print(fout, "}\n");
                    261: }
                    262:
                    263: void
                    264: write_programs(storage)
                    265:        char *storage;
                    266: {
1.9     ! deraadt   267:        list *l;
1.8       deraadt   268:        definition *def;
1.1       deraadt   269:
                    270:        /* write out stubs for procedure  definitions */
                    271:        for (l = defined; l != NULL; l = l->next) {
                    272:                def = (definition *) l->val;
1.9     ! deraadt   273:                if (def->def_kind == DEF_PROGRAM) {
1.1       deraadt   274:                        write_real_program(def);
1.9     ! deraadt   275:                }
1.1       deraadt   276:        }
                    277:
                    278:        /* write out dispatcher for each program */
                    279:        for (l = defined; l != NULL; l = l->next) {
                    280:                def = (definition *) l->val;
1.9     ! deraadt   281:                if (def->def_kind == DEF_PROGRAM) {
1.1       deraadt   282:                        write_program(def, storage);
1.9     ! deraadt   283:                }
1.1       deraadt   284:        }
1.9     ! deraadt   285:
        !           286:
1.1       deraadt   287: }
                    288:
                    289: /* write out definition of internal function (e.g. _printmsg_1(...))
                    290:    which calls server's defintion of actual function (e.g. printmsg_1(...)).
                    291:    Unpacks single user argument of printmsg_1 to call-by-value format
                    292:    expected by printmsg_1. */
1.5       deraadt   293: static
1.1       deraadt   294: write_real_program(def)
                    295:        definition *def;
                    296: {
                    297:        version_list *vp;
                    298:        proc_list *proc;
                    299:        decl_list *l;
                    300:
1.9     ! deraadt   301:        if( !newstyle ) return;  /* not needed for old style */
1.1       deraadt   302:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    303:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    304:                        f_print(fout, "\n");
                    305:                        internal_proctype(proc);
                    306:                        f_print(fout, "\n_");
                    307:                        pvname(proc->proc_name, vp->vers_num);
1.9     ! deraadt   308:                        if( Cflag ) {
        !           309:                          f_print(fout, "(" );
1.1       deraadt   310:                          /* arg name */
                    311:                          if (proc->arg_num > 1)
                    312:                            f_print(fout, proc->args.argname);
                    313:                          else
1.9     ! deraadt   314:                            ptype(proc->args.decls->decl.prefix,
1.1       deraadt   315:                                  proc->args.decls->decl.type, 0);
1.9     ! deraadt   316:                          f_print(fout, " *argp, struct svc_req *%s)\n",
1.1       deraadt   317:                                  RQSTP);
                    318:                        } else {
1.9     ! deraadt   319:                          f_print(fout, "(argp, %s)\n", RQSTP );
1.1       deraadt   320:                          /* arg name */
                    321:                          if (proc->arg_num > 1)
                    322:                            f_print(fout, "\t%s *argp;\n", proc->args.argname);
                    323:                          else {
                    324:                            f_print(fout, "\t");
1.9     ! deraadt   325:                            ptype(proc->args.decls->decl.prefix,
1.1       deraadt   326:                                  proc->args.decls->decl.type, 0);
                    327:                            f_print(fout, " *argp;\n");
                    328:                          }
                    329:                          f_print(fout, "       struct svc_req *%s;\n", RQSTP);
                    330:                        }
                    331:
                    332:                        f_print(fout, "{\n");
                    333:                        f_print(fout, "\treturn(");
                    334:                        pvname_svc(proc->proc_name, vp->vers_num);
                    335:                        f_print(fout, "(");
                    336:                        if (proc->arg_num < 2) { /* single argument */
1.9     ! deraadt   337:                          if (!streq( proc->args.decls->decl.type, "void"))
1.1       deraadt   338:                            f_print(fout, "*argp, ");  /* non-void */
                    339:                        } else {
1.9     ! deraadt   340:                          for (l = proc->args.decls;  l != NULL; l = l->next)
1.1       deraadt   341:                            f_print(fout, "argp->%s, ", l->decl.name);
                    342:                        }
                    343:                        f_print(fout, "%s));\n}\n", RQSTP);
1.9     ! deraadt   344:                }
1.1       deraadt   345:        }
                    346: }
                    347:
1.9     ! deraadt   348: static
1.1       deraadt   349: write_program(def, storage)
                    350:        definition *def;
                    351:        char *storage;
                    352: {
                    353:        version_list *vp;
                    354:        proc_list *proc;
                    355:        int filled;
                    356:
                    357:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    358:                f_print(fout, "\n");
1.9     ! deraadt   359:                if (storage != NULL) {
1.1       deraadt   360:                        f_print(fout, "%s ", storage);
1.9     ! deraadt   361:                }
1.1       deraadt   362:                f_print(fout, "void\n");
                    363:                pvname(def->def_name, vp->vers_num);
                    364:
                    365:                if (Cflag) {
1.9     ! deraadt   366:                   f_print(fout, "(struct svc_req *%s, ", RQSTP);
        !           367:                   f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
1.1       deraadt   368:                } else {
1.9     ! deraadt   369:                   f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
        !           370:                   f_print(fout, "      struct svc_req *%s;\n", RQSTP);
        !           371:                   f_print(fout, "      register SVCXPRT *%s;\n", TRANSP);
1.1       deraadt   372:                }
1.9     ! deraadt   373:
1.1       deraadt   374:                f_print(fout, "{\n");
                    375:
                    376:                filled = 0;
                    377:                f_print(fout, "\tunion {\n");
                    378:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    379:                        if (proc->arg_num < 2) { /* single argument */
1.9     ! deraadt   380:                                if (streq(proc->args.decls->decl.type,
        !           381:                                          "void")) {
1.1       deraadt   382:                                        continue;
1.9     ! deraadt   383:                                }
1.1       deraadt   384:                                filled = 1;
                    385:                                f_print(fout, "\t\t");
1.9     ! deraadt   386:                                ptype(proc->args.decls->decl.prefix,
        !           387:                                      proc->args.decls->decl.type, 0);
1.1       deraadt   388:                                pvname(proc->proc_name, vp->vers_num);
                    389:                                f_print(fout, "_arg;\n");
                    390:
1.9     ! deraadt   391:                        }
        !           392:                        else {
1.1       deraadt   393:                                filled = 1;
                    394:                                f_print(fout, "\t\t%s", proc->args.argname);
                    395:                                f_print(fout, " ");
                    396:                                pvname(proc->proc_name, vp->vers_num);
                    397:                                f_print(fout, "_arg;\n");
                    398:                        }
                    399:                }
1.9     ! deraadt   400:                if (!filled) {
1.1       deraadt   401:                        f_print(fout, "\t\tint fill;\n");
1.9     ! deraadt   402:                }
1.1       deraadt   403:                f_print(fout, "\t} %s;\n", ARG);
                    404:                f_print(fout, "\tchar *%s;\n", RESULT);
                    405:
                    406:                if (Cflag) {
1.9     ! deraadt   407:                    f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
        !           408:                    f_print(fout,
1.1       deraadt   409:                            "\tchar *(*%s)(char *, struct svc_req *);\n",
                    410:                            ROUTINE);
                    411:                } else {
1.9     ! deraadt   412:                    f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n", ARG, RESULT);
        !           413:                    f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
1.1       deraadt   414:                }
1.9     ! deraadt   415:
1.1       deraadt   416:                f_print(fout, "\n");
                    417:
1.9     ! deraadt   418:                if (callerflag)
1.1       deraadt   419:                        f_print(fout, "\tcaller = transp;\n"); /*EVAS*/
                    420:                if (timerflag)
                    421:                        f_print(fout, "\t_rpcsvcdirty = 1;\n");
                    422:                f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
                    423:                if (!nullproc(vp->procs)) {
                    424:                        f_print(fout, "\tcase NULLPROC:\n");
                    425:                        f_print(fout,
1.9     ! deraadt   426:                        Cflag
        !           427:                        ? "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
        !           428:                        : "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
        !           429:                                        TRANSP);
1.1       deraadt   430:                        print_return("\t\t");
                    431:                        f_print(fout, "\n");
                    432:                }
                    433:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    434:                        f_print(fout, "\tcase %s:\n", proc->proc_name);
                    435:                        if (proc->arg_num < 2) { /* single argument */
1.9     ! deraadt   436:                          p_xdrfunc( ARG, proc->args.decls->decl.type);
1.1       deraadt   437:                        } else {
1.9     ! deraadt   438:                          p_xdrfunc( ARG, proc->args.argname);
1.1       deraadt   439:                        }
1.9     ! deraadt   440:                        p_xdrfunc( RESULT, proc->res_type);
        !           441:                        if( Cflag )
        !           442:                            f_print(fout,
1.1       deraadt   443:                                    "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
                    444:                                    ROUTINE);
                    445:                        else
1.9     ! deraadt   446:                            f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
1.1       deraadt   447:
                    448:                        if (newstyle) { /* new style: calls internal routine */
                    449:                                f_print(fout,"_");
                    450:                        }
1.9     ! deraadt   451:                        if (!newstyle )
        !           452:                          pvname_svc(proc->proc_name, vp->vers_num);
1.1       deraadt   453:                        else
1.9     ! deraadt   454:                          pvname(proc->proc_name, vp->vers_num);
1.1       deraadt   455:                        f_print(fout, ";\n");
                    456:                        f_print(fout, "\t\tbreak;\n\n");
                    457:                }
                    458:                f_print(fout, "\tdefault:\n");
                    459:                printerr("noproc", TRANSP);
                    460:                print_return("\t\t");
                    461:                f_print(fout, "\t}\n");
                    462:
                    463:                f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
1.9     ! deraadt   464:                printif("getargs", TRANSP, "(caddr_t) &", ARG);
1.1       deraadt   465:                printerr("decode", TRANSP);
                    466:                print_return("\t\t");
                    467:                f_print(fout, "\t}\n");
                    468:
                    469:                if (Cflag)
1.9     ! deraadt   470:                    f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
1.1       deraadt   471:                            RESULT, ROUTINE, ARG, RQSTP);
                    472:                else
1.9     ! deraadt   473:                    f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
1.1       deraadt   474:                            RESULT, ROUTINE, ARG, RQSTP);
1.9     ! deraadt   475:                f_print(fout,
        !           476:                        "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
        !           477:                        RESULT, TRANSP, RESULT, RESULT);
1.1       deraadt   478:                printerr("systemerr", TRANSP);
                    479:                f_print(fout, "\t}\n");
                    480:
1.9     ! deraadt   481:                printif("freeargs", TRANSP, "(caddr_t) &", ARG);
1.1       deraadt   482:                (void) sprintf(_errbuf, "unable to free arguments");
                    483:                print_err_message("\t\t");
                    484:                f_print(fout, "\t\texit(1);\n");
                    485:                f_print(fout, "\t}\n");
                    486:                print_return("\t");
                    487:                f_print(fout, "}\n");
                    488:        }
                    489: }
                    490:
1.5       deraadt   491: static
1.1       deraadt   492: printerr(err, transp)
                    493:        char *err;
                    494:        char *transp;
                    495: {
                    496:        f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
                    497: }
                    498:
1.5       deraadt   499: static
1.9     ! deraadt   500: printif(proc, transp, prefix, arg)
1.1       deraadt   501:        char *proc;
                    502:        char *transp;
                    503:        char *prefix;
                    504:        char *arg;
                    505: {
                    506:        f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
1.9     ! deraadt   507:                proc, transp, arg, prefix, arg);
1.1       deraadt   508: }
                    509:
                    510: nullproc(proc)
                    511:        proc_list *proc;
                    512: {
                    513:        for (; proc != NULL; proc = proc->next) {
1.9     ! deraadt   514:                if (streq(proc->proc_num, "0")) {
1.1       deraadt   515:                        return (1);
1.9     ! deraadt   516:                }
1.1       deraadt   517:        }
                    518:        return (0);
                    519: }
                    520:
1.5       deraadt   521: static
1.1       deraadt   522: write_inetmost(infile)
                    523:        char *infile;
                    524: {
                    525:        f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
                    526:        f_print(fout, "\tint sock;\n");
                    527:        f_print(fout, "\tint proto;\n");
                    528:        f_print(fout, "\tstruct sockaddr_in saddr;\n");
                    529:        f_print(fout, "\tint asize = sizeof (saddr);\n");
                    530:        f_print(fout, "\n");
1.9     ! deraadt   531:        f_print(fout,
1.1       deraadt   532:        "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
                    533:        f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
                    534:        f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
                    535:        f_print(fout, "\t\t\texit(1);\n");
                    536:        f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
1.6       deraadt   537:        f_print(fout, "\t\t    (char *)&_rpcfdtype, &ssize) == -1)\n");
1.1       deraadt   538:        f_print(fout, "\t\t\texit(1);\n");
                    539:        f_print(fout, "\t\tsock = 0;\n");
                    540:        f_print(fout, "\t\t_rpcpmstart = 1;\n");
                    541:        f_print(fout, "\t\tproto = 0;\n");
                    542:        open_log_file(infile, "\t\t");
                    543:        f_print(fout, "\t} else {\n");
                    544:        write_rpc_svc_fg(infile, "\t\t");
                    545:        f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
                    546:        print_pmapunset("\t\t");
                    547:        f_print(fout, "\t}\n");
                    548: }
                    549:
1.5       deraadt   550: static
1.1       deraadt   551: print_return(space)
                    552:        char *space;
                    553: {
                    554:        if (exitnow)
                    555:                f_print(fout, "%sexit(0);\n", space);
                    556:        else {
                    557:                if (timerflag)
                    558:                        f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
                    559:                f_print(fout, "%sreturn;\n", space);
                    560:        }
                    561: }
                    562:
1.5       deraadt   563: static
1.1       deraadt   564: print_pmapunset(space)
                    565:        char *space;
                    566: {
1.9     ! deraadt   567:        list *l;
        !           568:        definition *def;
1.8       deraadt   569:        version_list *vp;
1.1       deraadt   570:
                    571:        for (l = defined; l != NULL; l = l->next) {
                    572:                def = (definition *) l->val;
                    573:                if (def->def_kind == DEF_PROGRAM) {
                    574:                        for (vp = def->def.pr.versions; vp != NULL;
1.9     ! deraadt   575:                                        vp = vp->next) {
1.1       deraadt   576:                                f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
1.9     ! deraadt   577:                                        space, def->def_name, vp->vers_name);
1.1       deraadt   578:                        }
                    579:                }
                    580:        }
                    581: }
                    582:
1.5       deraadt   583: static
1.1       deraadt   584: print_err_message(space)
                    585:        char *space;
                    586: {
                    587:        if (logflag)
                    588:                f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
                    589:        else if (inetdflag || pmflag)
                    590:                f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
                    591:        else
                    592:                f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
                    593: }
                    594:
                    595: /*
1.9     ! deraadt   596:  * Write the server auxiliary function ( _msgout, timeout)
1.1       deraadt   597:  */
                    598: void
1.9     ! deraadt   599: write_svc_aux( nomain )
1.1       deraadt   600:      int nomain;
                    601: {
                    602:        if (!logflag)
                    603:                write_msg_out();
1.9     ! deraadt   604:        if( !nomain )
        !           605:          write_timeout_func();
1.1       deraadt   606:        if (callerflag)                 /*EVAS*/
                    607:                write_caller_func();    /*EVAS*/
                    608: }
                    609:
                    610: /*
                    611:  * Write the _msgout function
                    612:  */
                    613:
                    614: write_msg_out()
                    615: {
                    616:        f_print(fout, "\n");
                    617:        f_print(fout, "static\n");
1.9     ! deraadt   618:        if( !Cflag ) {
        !           619:          f_print(fout, "void _msgout(msg)\n");
        !           620:          f_print(fout, "\tchar *msg;\n");
1.1       deraadt   621:        } else {
1.9     ! deraadt   622:          f_print(fout, "void _msgout(char* msg)\n");
1.1       deraadt   623:        }
                    624:        f_print(fout, "{\n");
                    625:        f_print(fout, "#ifdef RPC_SVC_FG\n");
                    626:        if (inetdflag || pmflag)
                    627:                f_print(fout, "\tif (_rpcpmstart)\n");
                    628:        f_print(fout, "\t\tsyslog(LOG_ERR, msg);\n");
1.6       deraadt   629:        f_print(fout, "\telse {\n");
                    630:        f_print(fout, "\t\t(void) write(STDERR_FILENO, msg, strlen(msg));\n");
                    631:        f_print(fout, "\t\t(void) write(STDERR_FILENO, \"\\n\", 1);\n");
                    632:        f_print(fout, "\t}\n#else\n");
1.1       deraadt   633:        f_print(fout, "\tsyslog(LOG_ERR, msg);\n");
                    634:        f_print(fout, "#endif\n");
                    635:        f_print(fout, "}\n");
                    636: }
                    637:
                    638: /*
                    639:  * Write the timeout function
                    640:  */
1.5       deraadt   641: static
1.1       deraadt   642: write_timeout_func()
                    643: {
                    644:        if (!timerflag)
                    645:                return;
                    646:        f_print(fout, "\n");
                    647:        f_print(fout, "static void\n");
                    648:        f_print(fout, "closedown()\n");
                    649:        f_print(fout, "{\n");
1.6       deraadt   650:        f_print(fout, "\tint save_errno = errno;\n\n");
1.1       deraadt   651:        f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
1.3       deraadt   652:        f_print(fout, "\t\textern fd_set *__svc_fdset;\n");
                    653:        f_print(fout, "\t\textern int __svc_fdsetsize;\n");
1.1       deraadt   654:        f_print(fout, "\t\tint i, openfd;\n");
                    655:        if (tirpcflag && pmflag) {
                    656:                f_print(fout, "\t\tstruct t_info tinfo;\n\n");
                    657:                f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
                    658:        } else {
                    659:                f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
                    660:        }
1.6       deraadt   661:        f_print(fout, "\t\t\t_exit(0);\n");
1.3       deraadt   662:        f_print(fout, "\t\tfor (i = 0, openfd = 0; i < __svc_fdsetsize && openfd < 2; i++)\n");
                    663:        f_print(fout, "\t\t\tif (FD_ISSET(i, __svc_fdset))\n");
1.1       deraadt   664:        f_print(fout, "\t\t\t\topenfd++;\n");
                    665:        f_print(fout, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
1.6       deraadt   666:        f_print(fout, "\t\t\t_exit(0);\n");
1.1       deraadt   667:        f_print(fout, "\t}\n");
                    668:        f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
1.6       deraadt   669:        f_print(fout, "\terrno = save_errno;\n");
1.1       deraadt   670:        f_print(fout, "}\n");
                    671: }
                    672:
1.5       deraadt   673: static
1.1       deraadt   674: write_caller_func()                    /*EVAS*/
                    675: {
                    676: #define        P(s)    f_print(fout, s);
                    677:
                    678: P("\n");
                    679: P("char *svc_caller()\n");
                    680: P("{\n");
                    681: P("    struct sockaddr_in actual;\n");
                    682: P("    struct hostent *hp;\n");
                    683: P("    static struct in_addr prev;\n");
                    684: P("    static char cname[128];\n\n");
                    685:
                    686: P("    actual = *svc_getcaller(caller);\n\n");
                    687:
                    688: P("    if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
                    689: P("             sizeof(struct in_addr)) == 0)\n");
                    690: P("            return (cname);\n\n");
                    691:
                    692: P("    prev = actual.sin_addr;\n\n");
                    693:
                    694: P("    hp = gethostbyaddr((char *) &actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
                    695: P("    if (hp == NULL) {                       /* dummy one up */\n");
                    696: P("            extern char *inet_ntoa();\n");
                    697: P("            strcpy(cname, inet_ntoa(actual.sin_addr));\n");
                    698: P("    } else {\n");
                    699: P("            strcpy(cname, hp->h_name);\n");
                    700: P("    }\n\n");
                    701:
                    702: P("    return (cname);\n");
                    703: P("}\n");
                    704:
                    705: #undef P
                    706: }
                    707:
                    708: /*
                    709:  * Write the most of port monitor support
                    710:  */
1.5       deraadt   711: static
1.1       deraadt   712: write_pm_most(infile, netflag)
                    713:        char *infile;
                    714:        int netflag;
                    715: {
                    716:        list *l;
                    717:        definition *def;
                    718:        version_list *vp;
                    719:
                    720:        f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
                    721:        f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
                    722:        f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
                    723:        f_print(fout, "\t\tchar *netid;\n");
                    724:        if (!netflag) { /* Not included by -n option */
                    725:                f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
                    726:                f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
                    727:        }
1.9     ! deraadt   728:        if( timerflag )
1.1       deraadt   729:          f_print(fout, "\t\tint pmclose;\n");
                    730: /* not necessary, defined in /usr/include/stdlib */
                    731: /*     f_print(fout, "\t\textern char *getenv();\n");*/
                    732:        f_print(fout, "\n");
                    733:        f_print(fout, "\t\t_rpcpmstart = 1;\n");
                    734:        if (logflag)
                    735:                open_log_file(infile, "\t\t");
                    736:        f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
                    737:        sprintf(_errbuf, "cannot get transport name");
                    738:        print_err_message("\t\t\t");
                    739:        f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
                    740:        sprintf(_errbuf, "cannot get transport info");
                    741:        print_err_message("\t\t\t");
                    742:        f_print(fout, "\t\t}\n");
                    743:        /*
                    744:         * A kludgy support for inetd services. Inetd only works with
                    745:         * sockmod, and RPC works only with timod, hence all this jugglery
                    746:         */
                    747:        f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
                    748:        f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
                    749:        sprintf(_errbuf, "could not get the right module");
                    750:        print_err_message("\t\t\t\t");
                    751:        f_print(fout, "\t\t\t\texit(1);\n");
                    752:        f_print(fout, "\t\t\t}\n");
                    753:        f_print(fout, "\t\t}\n");
1.9     ! deraadt   754:        if( timerflag )
1.1       deraadt   755:          f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
                    756:        f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
                    757:                        TRANSP);
                    758:        sprintf(_errbuf, "cannot create server handle");
                    759:        print_err_message("\t\t\t");
                    760:        f_print(fout, "\t\t\texit(1);\n");
                    761:        f_print(fout, "\t\t}\n");
                    762:        f_print(fout, "\t\tif (nconf)\n");
                    763:        f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
                    764:        for (l = defined; l != NULL; l = l->next) {
                    765:                def = (definition *) l->val;
                    766:                if (def->def_kind != DEF_PROGRAM) {
                    767:                        continue;
                    768:                }
                    769:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    770:                        f_print(fout,
                    771:                                "\t\tif (!svc_reg(%s, %s, %s, ",
                    772:                                TRANSP, def->def_name, vp->vers_name);
                    773:                        pvname(def->def_name, vp->vers_num);
                    774:                        f_print(fout, ", 0)) {\n");
                    775:                        (void) sprintf(_errbuf, "unable to register (%s, %s).",
                    776:                                        def->def_name, vp->vers_name);
                    777:                        print_err_message("\t\t\t");
                    778:                        f_print(fout, "\t\t\texit(1);\n");
                    779:                        f_print(fout, "\t\t}\n");
                    780:                }
                    781:        }
                    782:        if (timerflag) {
                    783:                f_print(fout, "\t\tif (pmclose) {\n");
                    784:                f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1.9     ! deraadt   785:                                Cflag? "(SIG_PF)" : "(void(*)())" );
1.1       deraadt   786:                f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
                    787:                f_print(fout, "\t\t}\n");
                    788:        }
                    789:        f_print(fout, "\t\tsvc_run();\n");
                    790:        f_print(fout, "\t\texit(1);\n");
                    791:        f_print(fout, "\t\t/* NOTREACHED */\n");
                    792:        f_print(fout, "\t}\n");
                    793: }
                    794:
                    795: /*
                    796:  * Support for backgrounding the server if self started.
                    797:  */
1.5       deraadt   798: static
1.1       deraadt   799: write_rpc_svc_fg(infile, sp)
                    800:        char *infile;
                    801:        char *sp;
                    802: {
                    803:        f_print(fout, "#ifndef RPC_SVC_FG\n");
                    804:        f_print(fout, "%sint size;\n", sp);
1.9     ! deraadt   805:        if( tirpcflag )
1.1       deraadt   806:                f_print(fout, "%sstruct rlimit rl;\n", sp);
                    807:        if (inetdflag)
                    808:                f_print(fout, "%sint pid, i;\n\n", sp);
                    809:        f_print(fout, "%spid = fork();\n", sp);
                    810:        f_print(fout, "%sif (pid < 0) {\n", sp);
                    811:        f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
                    812:        f_print(fout, "%s\texit(1);\n", sp);
                    813:        f_print(fout, "%s}\n", sp);
                    814:        f_print(fout, "%sif (pid)\n", sp);
                    815:        f_print(fout, "%s\texit(0);\n", sp);
                    816:        /* get number of file descriptors */
1.9     ! deraadt   817:        if( tirpcflag ) {
1.1       deraadt   818:          f_print(fout, "%srl.rlim_max = 0;\n", sp);
                    819:          f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
                    820:          f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
                    821:          f_print(fout, "%s\texit(1);\n", sp);
                    822:        } else {
                    823:          f_print(fout, "%ssize = getdtablesize();\n", sp);
                    824:        }
                    825:
                    826:        f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
                    827:        f_print(fout, "%s\t(void) close(i);\n", sp);
                    828:        /* Redirect stderr and stdout to console */
                    829:        f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
                    830:        f_print(fout, "%s(void) dup2(i, 1);\n", sp);
                    831:        f_print(fout, "%s(void) dup2(i, 2);\n", sp);
                    832:        /* This removes control of the controlling terminal */
1.9     ! deraadt   833:        if( tirpcflag )
1.1       deraadt   834:          f_print(fout, "%ssetsid();\n", sp);
                    835:        else {
                    836:          f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
                    837:          f_print(fout, "%sif (i >= 0) {\n", sp);
                    838:          f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
                    839:          f_print(fout, "%s\t(void) close(i);\n", sp);
                    840:          f_print(fout, "%s}\n", sp);
                    841:        }
                    842:        if (!logflag)
                    843:                open_log_file(infile, sp);
                    844:        f_print(fout, "#endif\n");
                    845:        if (logflag)
                    846:                open_log_file(infile, sp);
                    847: }
                    848:
1.5       deraadt   849: static
1.1       deraadt   850: open_log_file(infile, sp)
                    851:        char *infile;
                    852:        char *sp;
                    853: {
                    854:        char *s;
                    855:
                    856:        s = strrchr(infile, '.');
1.9     ! deraadt   857:        if (s)
1.1       deraadt   858:                *s = '\0';
                    859:        f_print(fout,"%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
                    860:        if (s)
                    861:                *s = '.';
                    862: }
                    863:
1.9     ! deraadt   864:
        !           865:
        !           866:
1.1       deraadt   867: /*
                    868:  * write a registration for the given transport for Inetd
                    869:  */
                    870: void
                    871: write_inetd_register(transp)
                    872:        char *transp;
                    873: {
                    874:        list *l;
                    875:        definition *def;
                    876:        version_list *vp;
                    877:        char *sp;
                    878:        int isudp;
                    879:        char tmpbuf[32];
                    880:
                    881:        if (inetdflag)
                    882:                sp = "\t";
                    883:        else
                    884:                sp = "";
                    885:        if (streq(transp, "udp"))
                    886:                isudp = 1;
                    887:        else
                    888:                isudp = 0;
                    889:        f_print(fout, "\n");
                    890:        if (inetdflag) {
1.6       deraadt   891:                f_print(fout, "\tif (_rpcfdtype == 0 || _rpcfdtype == %s) {\n",
1.1       deraadt   892:                                isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
                    893:        }
                    894:        if (inetdflag && streq(transp, "tcp")) {
                    895:                f_print(fout, "%s\tif (_rpcpmstart)\n", sp);
                    896:
                    897:                f_print(fout, "%s\t\t%s = svc%s_create(%s",
                    898:                        sp, TRANSP, "fd", inetdflag? "sock": "RPC_ANYSOCK");
                    899:                if (!isudp)
                    900:                        f_print(fout, ", 0, 0");
                    901:                f_print(fout, ");\n");
                    902:
                    903:                f_print(fout, "%s\telse\n", sp);
                    904:
                    905:                f_print(fout, "%s\t\t%s = svc%s_create(%s",
                    906:                        sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
                    907:                if (!isudp)
                    908:                        f_print(fout, ", 0, 0");
                    909:                f_print(fout, ");\n");
                    910:
                    911:        } else {
                    912:                f_print(fout, "%s\t%s = svc%s_create(%s",
                    913:                        sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
                    914:                if (!isudp)
                    915:                        f_print(fout, ", 0, 0");
                    916:                f_print(fout, ");\n");
                    917:        }
                    918:        f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
                    919:        (void) sprintf(_errbuf, "cannot create %s service.", transp);
                    920:        (void) sprintf(tmpbuf, "%s\t\t", sp);
                    921:        print_err_message(tmpbuf);
                    922:        f_print(fout, "%s\t\texit(1);\n", sp);
                    923:        f_print(fout, "%s\t}\n", sp);
                    924:
                    925:        if (inetdflag) {
                    926:                f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
                    927:                f_print(fout, "%s\tproto = IPPROTO_%s;\n",
                    928:                                sp, isudp ? "UDP": "TCP");
                    929:        }
                    930:        for (l = defined; l != NULL; l = l->next) {
                    931:                def = (definition *) l->val;
                    932:                if (def->def_kind != DEF_PROGRAM) {
                    933:                        continue;
                    934:                }
                    935:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    936:                        f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
                    937:                                sp, TRANSP, def->def_name, vp->vers_name);
                    938:                        pvname(def->def_name, vp->vers_num);
                    939:                        if (inetdflag)
                    940:                                f_print(fout, ", proto)) {\n");
1.9     ! deraadt   941:                        else
1.1       deraadt   942:                                f_print(fout, ", IPPROTO_%s)) {\n",
                    943:                                        isudp ? "UDP": "TCP");
                    944:                        (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
                    945:                                        def->def_name, vp->vers_name, transp);
                    946:                        print_err_message(tmpbuf);
                    947:                        f_print(fout, "%s\t\texit(1);\n", sp);
                    948:                        f_print(fout, "%s\t}\n", sp);
                    949:                }
                    950:        }
                    951:        if (inetdflag)
                    952:                f_print(fout, "\t}\n");
                    953: }