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