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