[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.10

1.10    ! deraadt     1: /*     $OpenBSD: rpc_svcout.c,v 1.8 2001/11/24 19:17:47 deraadt 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.10    ! 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",
        !            76:                    rname, stringfix(typename));
        !            77:        else
        !            78:                f_print(fout, "\t\txdr_%s = xdr_%s;\n", rname,
        !            79:                    stringfix(typename));
        !            80: }
1.1       deraadt    81:
1.5       deraadt    82: void
1.1       deraadt    83: internal_proctype(plist)
                     84:        proc_list *plist;
                     85: {
                     86:        f_print(fout, "static ");
1.10    ! deraadt    87:        ptype(plist->res_prefix, plist->res_type, 1);
        !            88:        f_print(fout, "*");
1.1       deraadt    89: }
                     90:
                     91: /*
1.10    ! 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.10    ! deraadt    98:        int nomain;
1.1       deraadt    99: {
                    100:        if (inetdflag || pmflag) {
1.10    ! deraadt   101:                char *var_type;
1.1       deraadt   102:                var_type = (nomain? "extern" : "static");
1.10    ! 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.10    ! deraadt   108:                        f_print(fout, "%s int _rpcsvcdirty;", var_type);
1.1       deraadt   109:                        f_print(fout, "\t/* Still serving ? */\n");
                    110:                }
1.10    ! deraadt   111:                write_svc_aux(nomain);
1.1       deraadt   112:        }
                    113:        /* write out dispatcher and stubs */
1.10    ! deraadt   114:        write_programs(nomain? (char *)NULL : "static");
1.1       deraadt   115:
1.10    ! 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.10    ! 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.10    ! 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.10    ! deraadt   166:        snprintf(tmpbuf, sizeof 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.10    ! 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.10    ! deraadt   180:                if (def->def_kind != DEF_PROGRAM)
1.1       deraadt   181:                        continue;
                    182:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    183:                        f_print(fout,
1.10    ! deraadt   184:                            "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
        !           185:                            sp, def->def_name, vp->vers_name);
1.1       deraadt   186:                        f_print(fout,
1.10    ! deraadt   187:                            "%s\tif (!svc_reg(%s, %s, %s, ",
        !           188:                            sp, TRANSP, def->def_name, vp->vers_name);
1.1       deraadt   189:                        pvname(def->def_name, vp->vers_num);
                    190:                        f_print(fout, ", nconf)) {\n");
                    191:                        (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
1.10    ! deraadt   192:                            def->def_name, vp->vers_name, transp);
1.1       deraadt   193:                        print_err_message(tmpbuf);
                    194:                        f_print(fout, "%s\t\texit(1);\n", sp);
                    195:                        f_print(fout, "%s\t}\n", sp);
                    196:                }
                    197:        }
                    198:        f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
                    199: }
                    200:
                    201: /*
                    202:  * write a registration for the given transport for TLI
                    203:  */
                    204: void
                    205: write_nettype_register(transp)
                    206:        char *transp;
                    207: {
                    208:        list *l;
                    209:        definition *def;
                    210:        version_list *vp;
                    211:
                    212:        for (l = defined; l != NULL; l = l->next) {
                    213:                def = (definition *) l->val;
1.10    ! deraadt   214:                if (def->def_kind != DEF_PROGRAM)
1.1       deraadt   215:                        continue;
                    216:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    217:                        f_print(fout, "\tif (!svc_create(");
                    218:                        pvname(def->def_name, vp->vers_num);
1.6       deraadt   219:                        f_print(fout, ", %s, %s, \"%s\")) {\n",
1.10    ! deraadt   220:                            def->def_name, vp->vers_name, transp);
1.1       deraadt   221:                        (void) sprintf(_errbuf,
1.10    ! deraadt   222:                            "unable to create (%s, %s) for %s.",
        !           223:                            def->def_name, vp->vers_name, transp);
1.1       deraadt   224:                        print_err_message("\t\t");
                    225:                        f_print(fout, "\t\texit(1);\n");
                    226:                        f_print(fout, "\t}\n");
                    227:                }
                    228:        }
                    229: }
                    230:
                    231: /*
1.10    ! deraadt   232:  * write the rest of the service
1.1       deraadt   233:  */
                    234: void
                    235: write_rest()
                    236: {
                    237:        f_print(fout, "\n");
                    238:        if (inetdflag) {
                    239:                f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
                    240:                (void) sprintf(_errbuf, "could not create a handle");
                    241:                print_err_message("\t\t");
                    242:                f_print(fout, "\t\texit(1);\n");
                    243:                f_print(fout, "\t}\n");
                    244:                if (timerflag) {
                    245:                        f_print(fout, "\tif (_rpcpmstart) {\n");
1.10    ! deraadt   246:                        f_print(fout,
        !           247:                            "\t\t(void) signal(SIGALRM, %s closedown);\n",
        !           248:                            Cflag? "(SIG_PF)" : "(void(*)())");
1.1       deraadt   249:                        f_print(fout, "\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
                    250:                        f_print(fout, "\t}\n");
                    251:                }
                    252:        }
                    253:        f_print(fout, "\tsvc_run();\n");
                    254:        (void) sprintf(_errbuf, "svc_run returned");
                    255:        print_err_message("\t");
                    256:        f_print(fout, "\texit(1);\n");
                    257:        f_print(fout, "\t/* NOTREACHED */\n");
                    258:        f_print(fout, "}\n");
                    259: }
                    260:
                    261: void
                    262: write_programs(storage)
                    263:        char *storage;
                    264: {
1.10    ! deraadt   265:        definition *def;
1.9       deraadt   266:        list *l;
1.1       deraadt   267:
                    268:        /* write out stubs for procedure  definitions */
                    269:        for (l = defined; l != NULL; l = l->next) {
                    270:                def = (definition *) l->val;
1.10    ! deraadt   271:                if (def->def_kind == DEF_PROGRAM)
1.1       deraadt   272:                        write_real_program(def);
                    273:        }
                    274:
                    275:        /* write out dispatcher for each program */
                    276:        for (l = defined; l != NULL; l = l->next) {
                    277:                def = (definition *) l->val;
1.10    ! deraadt   278:                if (def->def_kind == DEF_PROGRAM)
1.1       deraadt   279:                        write_program(def, storage);
                    280:        }
                    281: }
                    282:
                    283: /* write out definition of internal function (e.g. _printmsg_1(...))
                    284:    which calls server's defintion of actual function (e.g. printmsg_1(...)).
                    285:    Unpacks single user argument of printmsg_1 to call-by-value format
                    286:    expected by printmsg_1. */
1.5       deraadt   287: static
1.1       deraadt   288: write_real_program(def)
                    289:        definition *def;
                    290: {
                    291:        version_list *vp;
                    292:        proc_list *proc;
                    293:        decl_list *l;
                    294:
1.10    ! deraadt   295:        if (!newstyle) return;  /* not needed for old style */
1.1       deraadt   296:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    297:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    298:                        f_print(fout, "\n");
                    299:                        internal_proctype(proc);
                    300:                        f_print(fout, "\n_");
                    301:                        pvname(proc->proc_name, vp->vers_num);
1.10    ! deraadt   302:                        if (Cflag) {
        !           303:                          f_print(fout, "(");
1.1       deraadt   304:                          /* arg name */
                    305:                          if (proc->arg_num > 1)
                    306:                            f_print(fout, proc->args.argname);
                    307:                          else
1.10    ! deraadt   308:                            ptype(proc->args.decls->decl.prefix,
1.1       deraadt   309:                                  proc->args.decls->decl.type, 0);
1.10    ! deraadt   310:                          f_print(fout, " *argp, struct svc_req *%s)\n",
1.1       deraadt   311:                                  RQSTP);
                    312:                        } else {
1.10    ! deraadt   313:                          f_print(fout, "(argp, %s)\n", RQSTP);
1.1       deraadt   314:                          /* arg name */
                    315:                          if (proc->arg_num > 1)
                    316:                            f_print(fout, "\t%s *argp;\n", proc->args.argname);
                    317:                          else {
                    318:                            f_print(fout, "\t");
1.10    ! deraadt   319:                            ptype(proc->args.decls->decl.prefix,
1.1       deraadt   320:                                  proc->args.decls->decl.type, 0);
                    321:                            f_print(fout, " *argp;\n");
                    322:                          }
                    323:                          f_print(fout, "       struct svc_req *%s;\n", RQSTP);
                    324:                        }
                    325:
                    326:                        f_print(fout, "{\n");
                    327:                        f_print(fout, "\treturn(");
                    328:                        pvname_svc(proc->proc_name, vp->vers_num);
                    329:                        f_print(fout, "(");
                    330:                        if (proc->arg_num < 2) { /* single argument */
1.10    ! deraadt   331:                          if (!streq(proc->args.decls->decl.type, "void"))
1.1       deraadt   332:                            f_print(fout, "*argp, ");  /* non-void */
                    333:                        } else {
1.10    ! deraadt   334:                          for (l = proc->args.decls;  l != NULL; l = l->next)
1.1       deraadt   335:                            f_print(fout, "argp->%s, ", l->decl.name);
                    336:                        }
                    337:                        f_print(fout, "%s));\n}\n", RQSTP);
1.10    ! deraadt   338:                }
1.1       deraadt   339:        }
                    340: }
                    341:
1.10    ! deraadt   342: static
1.1       deraadt   343: write_program(def, storage)
                    344:        definition *def;
                    345:        char *storage;
                    346: {
                    347:        version_list *vp;
                    348:        proc_list *proc;
                    349:        int filled;
                    350:
                    351:        for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    352:                f_print(fout, "\n");
1.10    ! deraadt   353:                if (storage != NULL)
1.1       deraadt   354:                        f_print(fout, "%s ", storage);
                    355:                f_print(fout, "void\n");
                    356:                pvname(def->def_name, vp->vers_num);
                    357:
                    358:                if (Cflag) {
1.10    ! deraadt   359:                        f_print(fout, "(struct svc_req *%s, ", RQSTP);
        !           360:                        f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
1.1       deraadt   361:                } else {
1.10    ! deraadt   362:                        f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
        !           363:                        f_print(fout, " struct svc_req *%s;\n", RQSTP);
        !           364:                        f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
1.1       deraadt   365:                }
                    366:                f_print(fout, "{\n");
                    367:
                    368:                filled = 0;
                    369:                f_print(fout, "\tunion {\n");
                    370:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    371:                        if (proc->arg_num < 2) { /* single argument */
1.10    ! deraadt   372:                                if (streq(proc->args.decls->decl.type,
        !           373:                                    "void"))
1.1       deraadt   374:                                        continue;
                    375:                                filled = 1;
                    376:                                f_print(fout, "\t\t");
1.10    ! deraadt   377:                                ptype(proc->args.decls->decl.prefix,
        !           378:                                    proc->args.decls->decl.type, 0);
1.1       deraadt   379:                                pvname(proc->proc_name, vp->vers_num);
                    380:                                f_print(fout, "_arg;\n");
                    381:
1.10    ! deraadt   382:                        } else {
1.1       deraadt   383:                                filled = 1;
                    384:                                f_print(fout, "\t\t%s", proc->args.argname);
                    385:                                f_print(fout, " ");
                    386:                                pvname(proc->proc_name, vp->vers_num);
                    387:                                f_print(fout, "_arg;\n");
                    388:                        }
                    389:                }
1.10    ! deraadt   390:                if (!filled)
1.1       deraadt   391:                        f_print(fout, "\t\tint fill;\n");
                    392:                f_print(fout, "\t} %s;\n", ARG);
                    393:                f_print(fout, "\tchar *%s;\n", RESULT);
                    394:
                    395:                if (Cflag) {
1.10    ! deraadt   396:                        f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT);
        !           397:                        f_print(fout,
1.1       deraadt   398:                            "\tchar *(*%s)(char *, struct svc_req *);\n",
                    399:                            ROUTINE);
                    400:                } else {
1.10    ! deraadt   401:                        f_print(fout, "\tbool_t (*xdr_%s)(), (*xdr_%s)();\n",
        !           402:                            ARG, RESULT);
        !           403:                        f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
1.1       deraadt   404:                }
                    405:                f_print(fout, "\n");
                    406:
1.10    ! deraadt   407:                if (callerflag)
1.1       deraadt   408:                        f_print(fout, "\tcaller = transp;\n"); /*EVAS*/
                    409:                if (timerflag)
                    410:                        f_print(fout, "\t_rpcsvcdirty = 1;\n");
                    411:                f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
                    412:                if (!nullproc(vp->procs)) {
                    413:                        f_print(fout, "\tcase NULLPROC:\n");
                    414:                        f_print(fout,
1.10    ! deraadt   415:                            Cflag
        !           416:                            ? "\t\t(void) svc_sendreply(%s, (xdrproc_t) xdr_void, (char *)NULL);\n"
        !           417:                            : "\t\t(void) svc_sendreply(%s, xdr_void, (char *)NULL);\n",
        !           418:                            TRANSP);
1.1       deraadt   419:                        print_return("\t\t");
                    420:                        f_print(fout, "\n");
                    421:                }
                    422:                for (proc = vp->procs; proc != NULL; proc = proc->next) {
                    423:                        f_print(fout, "\tcase %s:\n", proc->proc_name);
                    424:                        if (proc->arg_num < 2) { /* single argument */
1.10    ! deraadt   425:                                p_xdrfunc(ARG, proc->args.decls->decl.type);
1.1       deraadt   426:                        } else {
1.10    ! deraadt   427:                                p_xdrfunc(ARG, proc->args.argname);
1.1       deraadt   428:                        }
1.10    ! deraadt   429:                        p_xdrfunc(RESULT, proc->res_type);
        !           430:                        if (Cflag)
        !           431:                                f_print(fout,
1.1       deraadt   432:                                    "\t\t%s = (char *(*)(char *, struct svc_req *)) ",
                    433:                                    ROUTINE);
                    434:                        else
1.10    ! deraadt   435:                                f_print(fout, "\t\t%s = (char *(*)()) ", ROUTINE);
1.1       deraadt   436:
                    437:                        if (newstyle) { /* new style: calls internal routine */
                    438:                                f_print(fout,"_");
                    439:                        }
1.10    ! deraadt   440:                        if (!newstyle)
        !           441:                                pvname_svc(proc->proc_name, vp->vers_num);
1.1       deraadt   442:                        else
1.10    ! deraadt   443:                                pvname(proc->proc_name, vp->vers_num);
1.1       deraadt   444:                        f_print(fout, ";\n");
                    445:                        f_print(fout, "\t\tbreak;\n\n");
                    446:                }
                    447:                f_print(fout, "\tdefault:\n");
                    448:                printerr("noproc", TRANSP);
                    449:                print_return("\t\t");
                    450:                f_print(fout, "\t}\n");
                    451:
                    452:                f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG);
1.10    ! deraadt   453:                printif ("getargs", TRANSP, "(caddr_t) &", ARG);
1.1       deraadt   454:                printerr("decode", TRANSP);
                    455:                print_return("\t\t");
                    456:                f_print(fout, "\t}\n");
                    457:
                    458:                if (Cflag)
1.10    ! deraadt   459:                        f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n",
1.1       deraadt   460:                            RESULT, ROUTINE, ARG, RQSTP);
                    461:                else
1.10    ! deraadt   462:                        f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
1.1       deraadt   463:                            RESULT, ROUTINE, ARG, RQSTP);
1.10    ! deraadt   464:                f_print(fout,
        !           465:                    "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n",
        !           466:                    RESULT, TRANSP, RESULT, RESULT);
1.1       deraadt   467:                printerr("systemerr", TRANSP);
                    468:                f_print(fout, "\t}\n");
                    469:
1.10    ! deraadt   470:                printif ("freeargs", TRANSP, "(caddr_t) &", ARG);
1.1       deraadt   471:                (void) sprintf(_errbuf, "unable to free arguments");
                    472:                print_err_message("\t\t");
                    473:                f_print(fout, "\t\texit(1);\n");
                    474:                f_print(fout, "\t}\n");
                    475:                print_return("\t");
                    476:                f_print(fout, "}\n");
                    477:        }
                    478: }
                    479:
1.5       deraadt   480: static
1.1       deraadt   481: printerr(err, transp)
                    482:        char *err;
                    483:        char *transp;
                    484: {
                    485:        f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
                    486: }
                    487:
1.5       deraadt   488: static
1.10    ! deraadt   489: printif (proc, transp, prefix, arg)
1.1       deraadt   490:        char *proc;
                    491:        char *transp;
                    492:        char *prefix;
                    493:        char *arg;
                    494: {
                    495:        f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n",
1.10    ! deraadt   496:            proc, transp, arg, prefix, arg);
1.1       deraadt   497: }
                    498:
                    499: nullproc(proc)
                    500:        proc_list *proc;
                    501: {
                    502:        for (; proc != NULL; proc = proc->next) {
1.10    ! deraadt   503:                if (streq(proc->proc_num, "0"))
1.1       deraadt   504:                        return (1);
                    505:        }
                    506:        return (0);
                    507: }
                    508:
1.5       deraadt   509: static
1.1       deraadt   510: write_inetmost(infile)
                    511:        char *infile;
                    512: {
                    513:        f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
                    514:        f_print(fout, "\tint sock;\n");
                    515:        f_print(fout, "\tint proto;\n");
                    516:        f_print(fout, "\tstruct sockaddr_in saddr;\n");
                    517:        f_print(fout, "\tint asize = sizeof (saddr);\n");
                    518:        f_print(fout, "\n");
1.10    ! deraadt   519:        f_print(fout,
1.1       deraadt   520:        "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
                    521:        f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
                    522:        f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
                    523:        f_print(fout, "\t\t\texit(1);\n");
                    524:        f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
1.6       deraadt   525:        f_print(fout, "\t\t    (char *)&_rpcfdtype, &ssize) == -1)\n");
1.1       deraadt   526:        f_print(fout, "\t\t\texit(1);\n");
                    527:        f_print(fout, "\t\tsock = 0;\n");
                    528:        f_print(fout, "\t\t_rpcpmstart = 1;\n");
                    529:        f_print(fout, "\t\tproto = 0;\n");
                    530:        open_log_file(infile, "\t\t");
                    531:        f_print(fout, "\t} else {\n");
                    532:        write_rpc_svc_fg(infile, "\t\t");
                    533:        f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
                    534:        print_pmapunset("\t\t");
                    535:        f_print(fout, "\t}\n");
                    536: }
                    537:
1.5       deraadt   538: static
1.1       deraadt   539: print_return(space)
                    540:        char *space;
                    541: {
                    542:        if (exitnow)
                    543:                f_print(fout, "%sexit(0);\n", space);
                    544:        else {
                    545:                if (timerflag)
                    546:                        f_print(fout, "%s_rpcsvcdirty = 0;\n", space);
                    547:                f_print(fout, "%sreturn;\n", space);
                    548:        }
                    549: }
                    550:
1.5       deraadt   551: static
1.1       deraadt   552: print_pmapunset(space)
                    553:        char *space;
                    554: {
1.10    ! deraadt   555:        version_list *vp;
        !           556:        definition *def;
1.9       deraadt   557:        list *l;
1.1       deraadt   558:
                    559:        for (l = defined; l != NULL; l = l->next) {
                    560:                def = (definition *) l->val;
                    561:                if (def->def_kind == DEF_PROGRAM) {
                    562:                        for (vp = def->def.pr.versions; vp != NULL;
1.10    ! deraadt   563:                            vp = vp->next) {
1.1       deraadt   564:                                f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
1.10    ! deraadt   565:                                    space, def->def_name, vp->vers_name);
1.1       deraadt   566:                        }
                    567:                }
                    568:        }
                    569: }
                    570:
1.5       deraadt   571: static
1.1       deraadt   572: print_err_message(space)
                    573:        char *space;
                    574: {
                    575:        if (logflag)
                    576:                f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf);
                    577:        else if (inetdflag || pmflag)
                    578:                f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf);
                    579:        else
                    580:                f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf);
                    581: }
                    582:
                    583: /*
1.10    ! deraadt   584:  * Write the server auxiliary function (_msgout, timeout)
1.1       deraadt   585:  */
                    586: void
1.10    ! deraadt   587: write_svc_aux(nomain)
1.1       deraadt   588:      int nomain;
                    589: {
                    590:        if (!logflag)
                    591:                write_msg_out();
1.10    ! deraadt   592:        if (!nomain)
        !           593:                write_timeout_func();
1.1       deraadt   594:        if (callerflag)                 /*EVAS*/
                    595:                write_caller_func();    /*EVAS*/
                    596: }
                    597:
                    598: /*
                    599:  * Write the _msgout function
                    600:  */
                    601:
                    602: write_msg_out()
                    603: {
                    604:        f_print(fout, "\n");
                    605:        f_print(fout, "static\n");
1.10    ! deraadt   606:        if (!Cflag) {
        !           607:                f_print(fout, "void _msgout(msg)\n");
        !           608:                f_print(fout, "\tchar *msg;\n");
1.1       deraadt   609:        } else {
1.10    ! deraadt   610:                f_print(fout, "void _msgout(char *msg)\n");
1.1       deraadt   611:        }
                    612:        f_print(fout, "{\n");
                    613:        f_print(fout, "#ifdef RPC_SVC_FG\n");
                    614:        if (inetdflag || pmflag)
                    615:                f_print(fout, "\tif (_rpcpmstart)\n");
                    616:        f_print(fout, "\t\tsyslog(LOG_ERR, msg);\n");
1.6       deraadt   617:        f_print(fout, "\telse {\n");
                    618:        f_print(fout, "\t\t(void) write(STDERR_FILENO, msg, strlen(msg));\n");
                    619:        f_print(fout, "\t\t(void) write(STDERR_FILENO, \"\\n\", 1);\n");
                    620:        f_print(fout, "\t}\n#else\n");
1.1       deraadt   621:        f_print(fout, "\tsyslog(LOG_ERR, msg);\n");
                    622:        f_print(fout, "#endif\n");
                    623:        f_print(fout, "}\n");
                    624: }
                    625:
                    626: /*
                    627:  * Write the timeout function
                    628:  */
1.5       deraadt   629: static
1.1       deraadt   630: write_timeout_func()
                    631: {
                    632:        if (!timerflag)
                    633:                return;
                    634:        f_print(fout, "\n");
                    635:        f_print(fout, "static void\n");
                    636:        f_print(fout, "closedown()\n");
                    637:        f_print(fout, "{\n");
1.6       deraadt   638:        f_print(fout, "\tint save_errno = errno;\n\n");
1.1       deraadt   639:        f_print(fout, "\tif (_rpcsvcdirty == 0) {\n");
1.3       deraadt   640:        f_print(fout, "\t\textern fd_set *__svc_fdset;\n");
                    641:        f_print(fout, "\t\textern int __svc_fdsetsize;\n");
1.1       deraadt   642:        f_print(fout, "\t\tint i, openfd;\n");
                    643:        if (tirpcflag && pmflag) {
                    644:                f_print(fout, "\t\tstruct t_info tinfo;\n\n");
                    645:                f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n");
                    646:        } else {
                    647:                f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n");
                    648:        }
1.6       deraadt   649:        f_print(fout, "\t\t\t_exit(0);\n");
1.3       deraadt   650:        f_print(fout, "\t\tfor (i = 0, openfd = 0; i < __svc_fdsetsize && openfd < 2; i++)\n");
                    651:        f_print(fout, "\t\t\tif (FD_ISSET(i, __svc_fdset))\n");
1.1       deraadt   652:        f_print(fout, "\t\t\t\topenfd++;\n");
                    653:        f_print(fout, "\t\tif (openfd <= (_rpcpmstart?0:1))\n");
1.6       deraadt   654:        f_print(fout, "\t\t\t_exit(0);\n");
1.1       deraadt   655:        f_print(fout, "\t}\n");
                    656:        f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
1.6       deraadt   657:        f_print(fout, "\terrno = save_errno;\n");
1.1       deraadt   658:        f_print(fout, "}\n");
                    659: }
                    660:
1.5       deraadt   661: static
1.1       deraadt   662: write_caller_func()                    /*EVAS*/
                    663: {
                    664: #define        P(s)    f_print(fout, s);
                    665:
                    666: P("\n");
                    667: P("char *svc_caller()\n");
                    668: P("{\n");
                    669: P("    struct sockaddr_in actual;\n");
                    670: P("    struct hostent *hp;\n");
                    671: P("    static struct in_addr prev;\n");
                    672: P("    static char cname[128];\n\n");
                    673:
                    674: P("    actual = *svc_getcaller(caller);\n\n");
                    675:
                    676: P("    if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n");
                    677: P("             sizeof(struct in_addr)) == 0)\n");
                    678: P("            return (cname);\n\n");
                    679:
                    680: P("    prev = actual.sin_addr;\n\n");
                    681:
                    682: P("    hp = gethostbyaddr((char *) &actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n");
                    683: P("    if (hp == NULL) {                       /* dummy one up */\n");
                    684: P("            extern char *inet_ntoa();\n");
                    685: P("            strcpy(cname, inet_ntoa(actual.sin_addr));\n");
                    686: P("    } else {\n");
                    687: P("            strcpy(cname, hp->h_name);\n");
                    688: P("    }\n\n");
                    689:
                    690: P("    return (cname);\n");
                    691: P("}\n");
                    692:
                    693: #undef P
                    694: }
                    695:
                    696: /*
                    697:  * Write the most of port monitor support
                    698:  */
1.5       deraadt   699: static
1.1       deraadt   700: write_pm_most(infile, netflag)
                    701:        char *infile;
                    702:        int netflag;
                    703: {
                    704:        list *l;
                    705:        definition *def;
                    706:        version_list *vp;
                    707:
                    708:        f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n");
                    709:        f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||");
                    710:        f_print(fout, " !strcmp(mname, \"timod\"))) {\n");
                    711:        f_print(fout, "\t\tchar *netid;\n");
                    712:        if (!netflag) { /* Not included by -n option */
                    713:                f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
                    714:                f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
                    715:        }
1.10    ! deraadt   716:        if (timerflag)
1.1       deraadt   717:          f_print(fout, "\t\tint pmclose;\n");
                    718: /* not necessary, defined in /usr/include/stdlib */
                    719: /*     f_print(fout, "\t\textern char *getenv();\n");*/
                    720:        f_print(fout, "\n");
                    721:        f_print(fout, "\t\t_rpcpmstart = 1;\n");
                    722:        if (logflag)
                    723:                open_log_file(infile, "\t\t");
                    724:        f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
                    725:        sprintf(_errbuf, "cannot get transport name");
                    726:        print_err_message("\t\t\t");
                    727:        f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n");
                    728:        sprintf(_errbuf, "cannot get transport info");
                    729:        print_err_message("\t\t\t");
                    730:        f_print(fout, "\t\t}\n");
                    731:        /*
                    732:         * A kludgy support for inetd services. Inetd only works with
                    733:         * sockmod, and RPC works only with timod, hence all this jugglery
                    734:         */
                    735:        f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n");
                    736:        f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n");
                    737:        sprintf(_errbuf, "could not get the right module");
                    738:        print_err_message("\t\t\t\t");
                    739:        f_print(fout, "\t\t\t\texit(1);\n");
                    740:        f_print(fout, "\t\t\t}\n");
                    741:        f_print(fout, "\t\t}\n");
1.10    ! deraadt   742:        if (timerflag)
1.1       deraadt   743:          f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
                    744:        f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
                    745:                        TRANSP);
                    746:        sprintf(_errbuf, "cannot create server handle");
                    747:        print_err_message("\t\t\t");
                    748:        f_print(fout, "\t\t\texit(1);\n");
                    749:        f_print(fout, "\t\t}\n");
                    750:        f_print(fout, "\t\tif (nconf)\n");
                    751:        f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
                    752:        for (l = defined; l != NULL; l = l->next) {
                    753:                def = (definition *) l->val;
                    754:                if (def->def_kind != DEF_PROGRAM) {
                    755:                        continue;
                    756:                }
                    757:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    758:                        f_print(fout,
                    759:                                "\t\tif (!svc_reg(%s, %s, %s, ",
                    760:                                TRANSP, def->def_name, vp->vers_name);
                    761:                        pvname(def->def_name, vp->vers_num);
                    762:                        f_print(fout, ", 0)) {\n");
                    763:                        (void) sprintf(_errbuf, "unable to register (%s, %s).",
                    764:                                        def->def_name, vp->vers_name);
                    765:                        print_err_message("\t\t\t");
                    766:                        f_print(fout, "\t\t\texit(1);\n");
                    767:                        f_print(fout, "\t\t}\n");
                    768:                }
                    769:        }
                    770:        if (timerflag) {
                    771:                f_print(fout, "\t\tif (pmclose) {\n");
                    772:                f_print(fout, "\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1.10    ! deraadt   773:                                Cflag? "(SIG_PF)" : "(void(*)())");
1.1       deraadt   774:                f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n");
                    775:                f_print(fout, "\t\t}\n");
                    776:        }
                    777:        f_print(fout, "\t\tsvc_run();\n");
                    778:        f_print(fout, "\t\texit(1);\n");
                    779:        f_print(fout, "\t\t/* NOTREACHED */\n");
                    780:        f_print(fout, "\t}\n");
                    781: }
                    782:
                    783: /*
                    784:  * Support for backgrounding the server if self started.
                    785:  */
1.5       deraadt   786: static
1.1       deraadt   787: write_rpc_svc_fg(infile, sp)
                    788:        char *infile;
                    789:        char *sp;
                    790: {
                    791:        f_print(fout, "#ifndef RPC_SVC_FG\n");
                    792:        f_print(fout, "%sint size;\n", sp);
1.10    ! deraadt   793:        if (tirpcflag)
1.1       deraadt   794:                f_print(fout, "%sstruct rlimit rl;\n", sp);
                    795:        if (inetdflag)
                    796:                f_print(fout, "%sint pid, i;\n\n", sp);
                    797:        f_print(fout, "%spid = fork();\n", sp);
                    798:        f_print(fout, "%sif (pid < 0) {\n", sp);
                    799:        f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
                    800:        f_print(fout, "%s\texit(1);\n", sp);
                    801:        f_print(fout, "%s}\n", sp);
                    802:        f_print(fout, "%sif (pid)\n", sp);
                    803:        f_print(fout, "%s\texit(0);\n", sp);
                    804:        /* get number of file descriptors */
1.10    ! deraadt   805:        if (tirpcflag) {
1.1       deraadt   806:          f_print(fout, "%srl.rlim_max = 0;\n", sp);
                    807:          f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
                    808:          f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp);
                    809:          f_print(fout, "%s\texit(1);\n", sp);
                    810:        } else {
                    811:          f_print(fout, "%ssize = getdtablesize();\n", sp);
                    812:        }
                    813:
                    814:        f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp);
                    815:        f_print(fout, "%s\t(void) close(i);\n", sp);
                    816:        /* Redirect stderr and stdout to console */
                    817:        f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp);
                    818:        f_print(fout, "%s(void) dup2(i, 1);\n", sp);
                    819:        f_print(fout, "%s(void) dup2(i, 2);\n", sp);
                    820:        /* This removes control of the controlling terminal */
1.10    ! deraadt   821:        if (tirpcflag)
1.1       deraadt   822:          f_print(fout, "%ssetsid();\n", sp);
                    823:        else {
                    824:          f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
                    825:          f_print(fout, "%sif (i >= 0) {\n", sp);
                    826:          f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);;
                    827:          f_print(fout, "%s\t(void) close(i);\n", sp);
                    828:          f_print(fout, "%s}\n", sp);
                    829:        }
                    830:        if (!logflag)
                    831:                open_log_file(infile, sp);
                    832:        f_print(fout, "#endif\n");
                    833:        if (logflag)
                    834:                open_log_file(infile, sp);
                    835: }
                    836:
1.5       deraadt   837: static
1.1       deraadt   838: open_log_file(infile, sp)
                    839:        char *infile;
                    840:        char *sp;
                    841: {
                    842:        char *s;
                    843:
                    844:        s = strrchr(infile, '.');
1.10    ! deraadt   845:        if (s)
1.1       deraadt   846:                *s = '\0';
                    847:        f_print(fout,"%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
                    848:        if (s)
                    849:                *s = '.';
                    850: }
                    851:
                    852: /*
                    853:  * write a registration for the given transport for Inetd
                    854:  */
                    855: void
                    856: write_inetd_register(transp)
                    857:        char *transp;
                    858: {
                    859:        list *l;
                    860:        definition *def;
                    861:        version_list *vp;
                    862:        char *sp;
                    863:        int isudp;
                    864:        char tmpbuf[32];
                    865:
                    866:        if (inetdflag)
                    867:                sp = "\t";
                    868:        else
                    869:                sp = "";
                    870:        if (streq(transp, "udp"))
                    871:                isudp = 1;
                    872:        else
                    873:                isudp = 0;
                    874:        f_print(fout, "\n");
                    875:        if (inetdflag) {
1.6       deraadt   876:                f_print(fout, "\tif (_rpcfdtype == 0 || _rpcfdtype == %s) {\n",
1.1       deraadt   877:                                isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
                    878:        }
                    879:        if (inetdflag && streq(transp, "tcp")) {
                    880:                f_print(fout, "%s\tif (_rpcpmstart)\n", sp);
                    881:
                    882:                f_print(fout, "%s\t\t%s = svc%s_create(%s",
                    883:                        sp, TRANSP, "fd", inetdflag? "sock": "RPC_ANYSOCK");
                    884:                if (!isudp)
                    885:                        f_print(fout, ", 0, 0");
                    886:                f_print(fout, ");\n");
                    887:
                    888:                f_print(fout, "%s\telse\n", sp);
                    889:
                    890:                f_print(fout, "%s\t\t%s = svc%s_create(%s",
                    891:                        sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
                    892:                if (!isudp)
                    893:                        f_print(fout, ", 0, 0");
                    894:                f_print(fout, ");\n");
                    895:
                    896:        } else {
                    897:                f_print(fout, "%s\t%s = svc%s_create(%s",
                    898:                        sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
                    899:                if (!isudp)
                    900:                        f_print(fout, ", 0, 0");
                    901:                f_print(fout, ");\n");
                    902:        }
                    903:        f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
                    904:        (void) sprintf(_errbuf, "cannot create %s service.", transp);
                    905:        (void) sprintf(tmpbuf, "%s\t\t", sp);
                    906:        print_err_message(tmpbuf);
                    907:        f_print(fout, "%s\t\texit(1);\n", sp);
                    908:        f_print(fout, "%s\t}\n", sp);
                    909:
                    910:        if (inetdflag) {
                    911:                f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
                    912:                f_print(fout, "%s\tproto = IPPROTO_%s;\n",
                    913:                                sp, isudp ? "UDP": "TCP");
                    914:        }
                    915:        for (l = defined; l != NULL; l = l->next) {
                    916:                def = (definition *) l->val;
                    917:                if (def->def_kind != DEF_PROGRAM) {
                    918:                        continue;
                    919:                }
                    920:                for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
                    921:                        f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ",
                    922:                                sp, TRANSP, def->def_name, vp->vers_name);
                    923:                        pvname(def->def_name, vp->vers_num);
                    924:                        if (inetdflag)
                    925:                                f_print(fout, ", proto)) {\n");
1.10    ! deraadt   926:                        else
1.1       deraadt   927:                                f_print(fout, ", IPPROTO_%s)) {\n",
                    928:                                        isudp ? "UDP": "TCP");
                    929:                        (void) sprintf(_errbuf, "unable to register (%s, %s, %s).",
                    930:                                        def->def_name, vp->vers_name, transp);
                    931:                        print_err_message(tmpbuf);
                    932:                        f_print(fout, "%s\t\texit(1);\n", sp);
                    933:                        f_print(fout, "%s\t}\n", sp);
                    934:                }
                    935:        }
                    936:        if (inetdflag)
                    937:                f_print(fout, "\t}\n");
                    938: }