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