Print this page
rpcgen should only produce ANSI code
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/cmd/rpcgen/rpc_svcout.c
+++ new/usr/src/cmd/rpcgen/rpc_svcout.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
↓ open down ↓ |
12 lines elided |
↑ open up ↑ |
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 + * Copyright 2014 Garrett D'Amore <garrett@damore.org>
24 + *
23 25 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 26 * Use is subject to license terms.
25 27 */
26 28 /* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
27 29 /* All Rights Reserved */
28 30 /*
29 31 * University Copyright- Copyright (c) 1982, 1986, 1988
30 32 * The Regents of the University of California
31 33 * All Rights Reserved
32 34 *
33 35 * University Acknowledgment- Portions of this document are derived from
34 36 * software developed by the University of California, Berkeley, and its
35 37 * contributors.
36 38 */
37 39
38 40 /*
39 41 * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler
40 42 */
41 43 #include <stdio.h>
42 44 #include <string.h>
43 45 #include <stdarg.h>
44 46 #include "rpc_parse.h"
45 47 #include "rpc_util.h"
46 48
47 49 extern int nullproc(proc_list *);
48 50
49 51 static char RQSTP[] = "rqstp";
50 52 static char TRANSP[] = "transp";
51 53 static char ARG[] = "argument";
52 54 static char RESULT[] = "result";
53 55 static char ROUTINE[] = "local";
54 56 static char RETVAL[] = "retval";
55 57
56 58 #define ERRBUFLEN 256
57 59
58 60 static void internal_proctype(proc_list *);
59 61 static void write_real_program(definition *);
60 62 static void write_programs(char *);
61 63 static void write_program(definition *, char *);
62 64 static void printerr(char *, char *);
63 65 static void write_svc_aux(int);
64 66 static void printif(char *, char *, char *, char *);
65 67 static void write_inetmost(char *);
66 68 static void print_return(char *);
67 69 static void print_pmapunset(char *);
↓ open down ↓ |
35 lines elided |
↑ open up ↑ |
68 70 static void print_err_message(const char *, const char *, ...);
69 71 static void write_msg_out(void);
70 72 static void write_timeout_func(void);
71 73 static void write_pm_most(char *, int);
72 74 static void write_rpc_svc_fg(char *, char *);
73 75 static void open_log_file(char *, char *);
74 76
75 77 static void
76 78 p_xdrfunc(char *rname, char *typename)
77 79 {
78 - if (Cflag) {
79 - f_print(fout, "\t\t_xdr_%s = (xdrproc_t)\n", rname);
80 - f_print(fout, "\t\t xdr_%s;\n", stringfix(typename));
81 - } else {
82 - f_print(fout, "\t\t_xdr_%s = xdr_%s;\n",
83 - rname, stringfix(typename));
84 - }
80 + f_print(fout, "\t\t_xdr_%s = (xdrproc_t)\n", rname);
81 + f_print(fout, "\t\t xdr_%s;\n", stringfix(typename));
85 82 }
86 83
87 84 static void
88 85 internal_proctype(proc_list *plist)
89 86 {
90 87 f_print(fout, "static ");
91 88 ptype(plist->res_prefix, plist->res_type, 1);
92 89 f_print(fout, "*");
93 90 }
94 91
95 92
96 93 static void
97 94 write_mtauto(void)
98 95 {
99 96 f_print(fout, "\tif (!rpc_control(RPC_SVC_MTMODE_SET, &mode)) {\n");
100 97 print_err_message("\t\t", "unable to set automatic MT mode.");
101 98 f_print(fout, "\t\texit(1);\n\t}\n");
102 99 }
103 100
104 101 /*
105 102 * write most of the service, that is, everything but the registrations.
106 103 */
107 104 void
108 105 write_most(char *infile, int netflag, int nomain)
109 106 {
110 107 if (inetdflag || pmflag) {
111 108 char *var_type;
112 109 var_type = (nomain? "extern" : "static");
113 110 f_print(fout, "%s int _rpcpmstart;", var_type);
114 111 f_print(fout, "\t\t/* Started by a port monitor ? */\n");
115 112 if (!tirpcflag) {
116 113 f_print(fout, "%s int _rpcfdtype;", var_type);
117 114 f_print(fout,
118 115 "\t\t /* Whether Stream or Datagram ? */\n");
119 116 }
120 117
121 118 if (timerflag) {
122 119 f_print(fout,
123 120 "\n/* States a server can be in wrt request */\n\n");
124 121 f_print(fout, "#define\t_IDLE 0\n");
125 122 f_print(fout, "#define\t_SERVED 1\n\n");
126 123 if (nomain) {
127 124 f_print(fout,
128 125 "/* LINTED static unused if no main */\n");
129 126 }
130 127 f_print(fout,
131 128 "static int _rpcsvcstate = _IDLE;");
132 129 f_print(fout,
133 130 "\t/* Set when a request is serviced */\n");
134 131 f_print(fout, "static int _rpcsvccount = 0;");
135 132 f_print(fout,
136 133 "\t\t/* Number of requests being serviced */\n");
137 134
138 135 if (mtflag) {
139 136 f_print(fout, "mutex_t _svcstate_lock;");
140 137 f_print(fout,
141 138 "\t\t/* lock for _rpcsvcstate, _rpcsvccount */\n");
142 139
143 140 }
144 141 }
145 142
146 143 write_svc_aux(nomain);
147 144 }
148 145 /* write out dispatcher and stubs */
149 146 write_programs(nomain ? NULL : "static");
150 147
151 148 if (nomain)
152 149 return;
153 150
154 151 f_print(fout, "\nint\nmain()\n");
155 152 f_print(fout, "{\n");
156 153 if (inetdflag) {
157 154 write_inetmost(infile);
158 155 /* Includes call to write_rpc_svc_fg() */
159 156 } else {
160 157 if (tirpcflag) {
161 158 if (netflag) {
162 159 f_print(fout,
163 160 "\tregister SVCXPRT *%s;\n", TRANSP);
164 161 f_print(fout,
165 162 "\tstruct netconfig *nconf = NULL;\n");
166 163 }
167 164 f_print(fout, "\tpid_t pid;\n");
168 165 f_print(fout, "\tint i;\n");
169 166 if (mtauto) {
170 167 f_print(fout,
171 168 "\tint mode = RPC_SVC_MT_AUTO;\n\n");
172 169 write_mtauto();
173 170 } else
174 171 f_print(fout, "\n");
175 172
176 173 if (mtflag & timerflag)
177 174 f_print(fout,
178 175 "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n");
179 176
180 177 write_pm_most(infile, netflag);
181 178 f_print(fout, "\telse {\n");
182 179 write_rpc_svc_fg(infile, "\t\t");
183 180 f_print(fout, "\t}\n");
184 181 } else {
185 182 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
186 183 f_print(fout, "\n");
187 184 print_pmapunset("\t");
188 185 }
189 186 }
190 187
191 188 if (logflag && !inetdflag) {
192 189 open_log_file(infile, "\t");
193 190 }
194 191 }
195 192
196 193 /*
197 194 * write a registration for the given transport
198 195 */
199 196 void
200 197 write_netid_register(char *transp)
201 198 {
202 199 list *l;
203 200 definition *def;
204 201 version_list *vp;
205 202 char *sp;
206 203 char tmpbuf[32];
207 204
208 205 sp = "";
209 206 f_print(fout, "\n");
210 207 f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp);
211 208 f_print(fout, "%s\tif (nconf == NULL) {\n", sp);
212 209 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
213 210 print_err_message(tmpbuf, "cannot find %s netid.", transp);
214 211 f_print(fout, "%s\t\texit(1);\n", sp);
215 212 f_print(fout, "%s\t}\n", sp);
216 213 f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n",
217 214 sp, TRANSP);
218 215 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
219 216 print_err_message(tmpbuf, "cannot create %s service.", transp);
220 217 f_print(fout, "%s\t\texit(1);\n", sp);
221 218 f_print(fout, "%s\t}\n", sp);
222 219
223 220 for (l = defined; l != NULL; l = l->next) {
224 221 def = (definition *) l->val;
225 222 if (def->def_kind != DEF_PROGRAM) {
226 223 continue;
227 224 }
228 225 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
229 226 f_print(fout,
230 227 "%s\t(void) rpcb_unset(%s, %s, nconf);\n",
231 228 sp, def->def_name, vp->vers_name);
232 229 f_print(fout,
233 230 "%s\tif (!svc_reg(%s, %s, %s, ",
234 231 sp, TRANSP, def->def_name, vp->vers_name);
235 232 pvname(def->def_name, vp->vers_num);
236 233 f_print(fout, ", nconf)) {\n");
237 234 print_err_message(tmpbuf,
238 235 "unable to register (%s, %s, %s).",
239 236 def->def_name, vp->vers_name, transp);
240 237 f_print(fout, "%s\t\texit(1);\n", sp);
241 238 f_print(fout, "%s\t}\n", sp);
242 239 }
243 240 }
244 241 f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp);
245 242 }
246 243
247 244 /*
248 245 * write a registration for the given transport for TLI
249 246 */
250 247 void
251 248 write_nettype_register(char *transp)
252 249 {
253 250 list *l;
254 251 definition *def;
255 252 version_list *vp;
256 253
257 254 for (l = defined; l != NULL; l = l->next) {
258 255 def = (definition *) l->val;
259 256 if (def->def_kind != DEF_PROGRAM) {
260 257 continue;
261 258 }
262 259 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
263 260 f_print(fout, "\tif (!svc_create(\n");
264 261 f_print(fout, "\t ");
265 262 pvname(def->def_name, vp->vers_num);
266 263 f_print(fout, ", %s, %s,\n",
267 264 def->def_name, vp->vers_name);
268 265 f_print(fout, "\t \"%s\")) {\n", transp);
269 266 print_err_message("\t\t",
270 267 "unable to create (%s, %s) for %s.",
271 268 def->def_name, vp->vers_name, transp);
272 269 f_print(fout, "\t\texit(1);\n");
273 270 f_print(fout, "\t}\n");
274 271 }
275 272 }
276 273 }
277 274
278 275 /*
279 276 * write the rest of the service
280 277 */
281 278 void
282 279 write_rest(void)
283 280 {
284 281 f_print(fout, "\n");
285 282 if (inetdflag) {
286 283 f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP);
287 284 print_err_message("\t\t", "could not create a handle");
288 285 f_print(fout, "\t\texit(1);\n");
289 286 f_print(fout, "\t}\n");
290 287 if (timerflag) {
291 288 f_print(fout, "\tif (_rpcpmstart) {\n");
292 289 if (mtflag) {
↓ open down ↓ |
198 lines elided |
↑ open up ↑ |
293 290 f_print(fout,
294 291 "\t\tif (thr_create(NULL, 0, closedown, NULL, 0, NULL) != 0) {\n");
295 292 print_err_message("\t\t\t",
296 293 "cannot create closedown thread");
297 294 f_print(fout, "\t\t\texit(1);\n");
298 295 f_print(fout, "\t\t}\n");
299 296 f_print(fout, "\t}\n");
300 297 } else {
301 298 f_print(fout,
302 299 "\t\t(void) signal(SIGALRM, %s closedown);\n",
303 - Cflag? "(SIG_PF)":"(void(*)())");
300 + "(SIG_PF)");
304 301 f_print(fout,
305 302 "\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
306 303 f_print(fout, "\t}\n");
307 304 }
308 305 }
309 306 }
310 307 f_print(fout, "\tsvc_run();\n");
311 308 print_err_message("\t", "svc_run returned");
312 309 f_print(fout, "\texit(1);\n");
313 310 f_print(fout, "\t/* NOTREACHED */\n");
314 311 f_print(fout, "}\n");
315 312 }
316 313
317 314 static void
318 315 write_programs(char *storage)
319 316 {
320 317 list *l;
321 318 definition *def;
322 319
323 320 /* write out stubs for procedure definitions */
324 321 for (l = defined; l != NULL; l = l->next) {
325 322 def = (definition *) l->val;
326 323 if (def->def_kind == DEF_PROGRAM)
327 324 write_real_program(def);
328 325 }
329 326
330 327 /* write out dispatcher for each program */
331 328 for (l = defined; l != NULL; l = l->next) {
332 329 def = (definition *) l->val;
333 330 if (def->def_kind == DEF_PROGRAM)
334 331 write_program(def, storage);
335 332 }
336 333
337 334
338 335 }
339 336
340 337 /*
341 338 * write out definition of internal function (e.g. _printmsg_1(...))
342 339 * which calls server's defintion of actual function (e.g. printmsg_1(...)).
343 340 * Unpacks single user argument of printmsg_1 to call-by-value format
344 341 * expected by printmsg_1.
345 342 */
346 343 static void
347 344 write_real_program(definition *def)
348 345 {
349 346 version_list *vp;
350 347 proc_list *proc;
351 348 decl_list *l;
352 349
353 350 if (!newstyle)
354 351 return; /* not needed for old style */
355 352 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
356 353 for (proc = vp->procs; proc != NULL; proc = proc->next) {
357 354 int oneway = streq(proc->res_type, "oneway");
358 355
359 356 f_print(fout, "\n");
↓ open down ↓ |
46 lines elided |
↑ open up ↑ |
360 357 if (proc->arg_num < 2 &&
361 358 streq(proc->args.decls->decl.type, "void")) {
362 359 f_print(fout, "/* ARGSUSED */\n");
363 360 }
364 361 if (!mtflag)
365 362 internal_proctype(proc);
366 363 else
367 364 f_print(fout, "int");
368 365 f_print(fout, "\n_");
369 366 pvname(proc->proc_name, vp->vers_num);
370 - if (Cflag) {
371 - f_print(fout, "(\n");
372 - f_print(fout, " ");
373 - /* arg name */
374 - if (proc->arg_num > 1)
375 - /* LINTED variable format */
376 - f_print(fout, proc->args.argname);
377 - else
378 - ptype(proc->args.decls->decl.prefix,
379 - proc->args.decls->decl.type, 0);
380 - f_print(fout, " *argp,\n");
381 - if (mtflag) {
382 - f_print(fout, " ");
383 - ptype(proc->res_prefix,
384 - proc->res_type, 1);
385 - f_print(fout, "*%s,\n", RESULT);
386 - }
387 - f_print(fout, " struct svc_req *%s)\n",
388 - RQSTP);
389 367
390 - } else {
391 - if (mtflag)
392 - f_print(fout, "(argp, %s, %s)\n",
393 - RESULT, RQSTP);
394 - else
395 - f_print(fout, "(argp, %s)\n", RQSTP);
396 - /* arg name */
397 - if (proc->arg_num > 1)
398 - f_print(fout, "\t%s *argp;\n",
399 - proc->args.argname);
400 - else {
401 - f_print(fout, "\t");
402 - ptype(proc->args.decls->decl.prefix,
403 - proc->args.decls->decl.type, 0);
404 - f_print(fout, " *argp;\n");
405 - }
406 - if (mtflag)
407 - f_print(fout, "\tvoid *%s;\n", RESULT);
408 - f_print(fout, "\tstruct svc_req *%s;\n", RQSTP);
368 + f_print(fout, "(\n");
369 + f_print(fout, " ");
370 + /* arg name */
371 + if (proc->arg_num > 1)
372 + /* LINTED variable format */
373 + f_print(fout, proc->args.argname);
374 + else
375 + ptype(proc->args.decls->decl.prefix,
376 + proc->args.decls->decl.type, 0);
377 + f_print(fout, " *argp,\n");
378 + if (mtflag) {
379 + f_print(fout, " ");
380 + ptype(proc->res_prefix, proc->res_type, 1);
381 + f_print(fout, "*%s,\n", RESULT);
409 382 }
383 + f_print(fout, " struct svc_req *%s)\n", RQSTP);
410 384
411 385 f_print(fout, "{\n");
412 386 f_print(fout, "\treturn (");
413 387 /* for mtflag, arguments are different */
414 - if (Cflag || mtflag)
415 - pvname_svc(proc->proc_name, vp->vers_num);
416 - else
417 - pvname(proc->proc_name, vp->vers_num);
388 + pvname_svc(proc->proc_name, vp->vers_num);
418 389 f_print(fout, "(");
419 390 if (proc->arg_num < 2) { /* single argument */
420 391 /* only print if non-void */
421 392 if (!streq(proc->args.decls->decl.type, "void"))
422 393 f_print(fout, "*argp, ");
423 394 } else {
424 395 f_print(fout, "\n");
425 396 for (l = proc->args.decls; l != NULL;
426 397 l = l->next)
427 398 f_print(fout, "\t argp->%s,\n",
428 399 l->decl.name);
429 400 f_print(fout, "\t ");
430 401 }
431 402 if (mtflag && !oneway)
432 403 f_print(fout, "%s, ", RESULT);
433 404 f_print(fout, "%s));\n}\n", RQSTP);
434 405 }
435 406 }
436 407 }
437 408
438 409 static void
439 410 write_program(definition *def, char *storage)
440 411 {
441 412 version_list *vp;
442 413 proc_list *proc;
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
443 414 int filled;
444 415
445 416 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
446 417 f_print(fout, "\n");
447 418 if (storage != NULL) {
448 419 f_print(fout, "%s ", storage);
449 420 }
450 421 f_print(fout, "void\n");
451 422 pvname(def->def_name, vp->vers_num);
452 423
453 - if (Cflag) {
454 - f_print(fout, "(struct svc_req *%s, ", RQSTP);
455 - f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
456 - } else {
457 - f_print(fout, "(%s, %s)\n", RQSTP, TRANSP);
458 - f_print(fout, " struct svc_req *%s;\n", RQSTP);
459 - f_print(fout, " register SVCXPRT *%s;\n", TRANSP);
460 - }
424 + f_print(fout, "(struct svc_req *%s, ", RQSTP);
425 + f_print(fout, "register SVCXPRT *%s)\n", TRANSP);
461 426
462 427 f_print(fout, "{\n");
463 428
464 429 filled = 0;
465 430 f_print(fout, "\tunion {\n");
466 431 for (proc = vp->procs; proc != NULL; proc = proc->next) {
467 432 if (proc->arg_num < 2) { /* single argument */
468 433 if (streq(proc->args.decls->decl.type,
469 434 "void")) {
470 435 continue;
471 436 }
472 437 filled = 1;
473 438 f_print(fout, "\t\t");
474 439 ptype(proc->args.decls->decl.prefix,
475 440 proc->args.decls->decl.type, 0);
476 441 pvname(proc->proc_name, vp->vers_num);
477 442 f_print(fout, "_arg;\n");
478 443
479 444 } else {
480 445 filled = 1;
481 446 f_print(fout, "\t\t%s", proc->args.argname);
482 447 f_print(fout, " ");
483 448 pvname(proc->proc_name, vp->vers_num);
484 449 f_print(fout, "_arg;\n");
485 450 }
486 451 }
487 452 if (!filled) {
488 453 f_print(fout, "\t\tint fill;\n");
489 454 }
490 455 f_print(fout, "\t} %s;\n", ARG);
491 456
492 457 if (mtflag) {
493 458 filled = 0;
494 459 f_print(fout, "\tunion {\n");
495 460 for (proc = vp->procs; proc != NULL;
496 461 proc = proc->next) {
497 462 if (streq(proc->res_type, "void") ||
498 463 streq(proc->res_type, "oneway"))
499 464 continue;
500 465 filled = 1;
501 466 f_print(fout, "\t\t");
502 467 ptype(proc->res_prefix, proc->res_type, 0);
503 468 pvname(proc->proc_name, vp->vers_num);
↓ open down ↓ |
33 lines elided |
↑ open up ↑ |
504 469 f_print(fout, "_res;\n");
505 470 }
506 471 if (!filled)
507 472 f_print(fout, "\t\tint fill;\n");
508 473 f_print(fout, "\t} %s;\n", RESULT);
509 474 f_print(fout, "\tbool_t %s;\n", RETVAL);
510 475
511 476 } else
512 477 f_print(fout, "\tchar *%s;\n", RESULT);
513 478
514 - if (Cflag) {
515 - f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n",
516 - ARG, RESULT);
517 - if (mtflag)
518 - f_print(fout,
519 -"\tbool_t (*%s)(char *, void *, struct svc_req *);\n",
520 - ROUTINE);
521 - else
522 - f_print(fout,
523 -"\tchar *(*%s)(char *, struct svc_req *);\n",
524 - ROUTINE);
525 - } else {
526 - f_print(fout,
527 - "\tbool_t (*_xdr_%s)(), (*_xdr_%s)();\n",
528 - ARG, RESULT);
529 - if (mtflag)
530 - f_print(fout, "\tbool_t (*%s)();\n", ROUTINE);
531 - else
532 - f_print(fout, "\tchar *(*%s)();\n", ROUTINE);
533 - }
479 + f_print(fout, "\txdrproc_t _xdr_%s, _xdr_%s;\n", ARG, RESULT);
480 + if (mtflag)
481 + f_print(fout, "\tbool_t "
482 + "(*%s)(char *, void *, struct svc_req *);\n",
483 + ROUTINE);
484 + else
485 + f_print(fout, "\tchar *(*%s)"
486 + "(char *, struct svc_req *);\n",
487 + ROUTINE);
534 488 f_print(fout, "\n");
535 489
536 490 if (timerflag) {
537 491 if (mtflag)
538 492 f_print(fout,
539 493 "\t(void) mutex_lock(&_svcstate_lock);\n");
540 494
541 495 f_print(fout, "\t_rpcsvccount++;\n");
542 496 if (mtflag)
543 497 f_print(fout,
544 498 "\t(void) mutex_unlock(&_svcstate_lock);\n");
545 499 }
546 500
547 501 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP);
548 502 if (!nullproc(vp->procs)) {
549 503 f_print(fout, "\tcase NULLPROC:\n");
550 504 f_print(fout,
551 - Cflag ?
552 -"\t\t(void) svc_sendreply(%s,\n\t\t (xdrproc_t)xdr_void, NULL);\n" :
553 -"\t\t(void) svc_sendreply(%s, xdr_void,\n\t\t\tNULL);\n",
505 +"\t\t(void) svc_sendreply(%s,\n\t\t (xdrproc_t)xdr_void, NULL);\n",
554 506 TRANSP);
555 507 print_return("\t\t");
556 508 f_print(fout, "\n");
557 509 }
558 510 for (proc = vp->procs; proc != NULL; proc = proc->next) {
559 511 f_print(fout, "\tcase %s:\n", proc->proc_name);
560 512 if (proc->arg_num < 2) { /* single argument */
561 513 p_xdrfunc(ARG, proc->args.decls->decl.type);
562 514 } else {
563 515 p_xdrfunc(ARG, proc->args.argname);
564 516 }
565 517
566 518 if (streq(proc->res_type, "oneway")) {
567 519 /* One-way call */
568 520 f_print(fout, "\t\t_xdr_%s = NULL;\n", RESULT);
569 521 } else {
570 522 p_xdrfunc(RESULT, proc->res_type);
571 523 }
572 - if (Cflag) {
573 - if (mtflag) {
574 - f_print(fout,
575 - "\t\t%s = (bool_t (*) (char *, "
576 - "void *, struct svc_req *))",
577 - ROUTINE);
578 - } else {
579 - f_print(fout,
580 - "\t\t%s = (char *(*)(char *, "
581 - "struct svc_req *))",
582 - ROUTINE);
583 - }
524 + if (mtflag) {
525 + f_print(fout,
526 + "\t\t%s = (bool_t (*) (char *, "
527 + "void *, struct svc_req *))",
528 + ROUTINE);
584 529 } else {
585 - if (mtflag) {
586 - f_print(fout,
587 - "\t\t%s = (bool_t (*)())",
588 - ROUTINE);
589 - } else {
590 - f_print(fout, "\t\t%s = (char *(*)())",
591 - ROUTINE);
592 - }
530 + f_print(fout,
531 + "\t\t%s = (char *(*)(char *, "
532 + "struct svc_req *))",
533 + ROUTINE);
593 534 }
594 535
595 536 f_print(fout, "\n\t\t ");
596 537 if (newstyle) { /* new style: calls internal routine */
597 538 f_print(fout, "_");
598 539 }
599 - if ((Cflag || mtflag) && !newstyle)
540 + if (!newstyle)
600 541 pvname_svc(proc->proc_name, vp->vers_num);
601 542 else
602 543 pvname(proc->proc_name, vp->vers_num);
603 544 f_print(fout, ";\n");
604 545 f_print(fout, "\t\tbreak;\n\n");
605 546 }
606 547 f_print(fout, "\tdefault:\n");
607 548 printerr("noproc", TRANSP);
608 549 print_return("\t\t");
609 550 f_print(fout, "\t}\n");
610 551
611 552 f_print(fout,
612 553 "\t(void) memset((char *)&%s, 0, sizeof (%s));\n",
613 554 ARG, ARG);
614 555 printif("getargs", TRANSP, "(caddr_t)&", ARG);
615 556 printerr("decode", TRANSP);
616 557 print_return("\t\t");
617 558 f_print(fout, "\t}\n");
618 559
619 560 if (!mtflag)
620 - if (Cflag)
621 - f_print(fout,
622 - "\t%s = (*%s)((char *)&%s, %s);\n",
623 - RESULT, ROUTINE, ARG, RQSTP);
624 - else
625 - f_print(fout, "\t%s = (*%s)(&%s, %s);\n",
626 - RESULT, ROUTINE, ARG, RQSTP);
561 + f_print(fout,
562 + "\t%s = (*%s)((char *)&%s, %s);\n",
563 + RESULT, ROUTINE, ARG, RQSTP);
627 564 else
628 - if (Cflag)
629 - f_print(fout,
630 -"\t%s = (bool_t)(*%s)((char *)&%s, (void *)&%s, %s);\n",
631 - RETVAL, ROUTINE, ARG, RESULT, RQSTP);
632 - else
633 - f_print(fout,
634 -"\t%s = (bool_t)(*%s)(&%s, &%s, %s);\n",
635 - RETVAL, ROUTINE, ARG, RESULT, RQSTP);
565 + f_print(fout,
566 + "\t%s = (bool_t)(*%s)"
567 + "((char *)&%s, (void *)&%s, %s);\n",
568 + RETVAL, ROUTINE, ARG, RESULT, RQSTP);
636 569
637 570
638 571
639 -
640 572 if (mtflag)
641 573 f_print(fout,
642 574 "\tif (_xdr_%s && %s > 0 &&\n"
643 575 "\t !svc_sendreply(%s, _xdr_%s, (char *)&%s)) {\n",
644 576 RESULT, RETVAL, TRANSP, RESULT, RESULT);
645 577 else
646 578 f_print(fout,
647 579 "\tif (_xdr_%s && %s != NULL &&\n"
648 580 "\t !svc_sendreply(%s, _xdr_%s, %s)) {\n",
649 581 RESULT, RESULT, TRANSP, RESULT, RESULT);
650 582
651 583 printerr("systemerr", TRANSP);
652 584 f_print(fout, "\t}\n");
653 585
654 586 printif("freeargs", TRANSP, "(caddr_t)&", ARG);
655 587 print_err_message("\t\t", "unable to free arguments");
656 588 f_print(fout, "\t\texit(1);\n");
657 589 f_print(fout, "\t}\n");
658 590 /* print out free routine */
659 591 if (mtflag) {
660 592 f_print(fout, "\tif (_xdr_%s != NULL) {\n", RESULT);
661 593 f_print(fout, "\t\tif (!");
662 594
663 595 pvname(def->def_name, vp->vers_num);
664 596 f_print(fout, "_freeresult(%s, _xdr_%s,\n",
665 597 TRANSP, RESULT);
666 598 f_print(fout, "\t\t (caddr_t)&%s))\n",
667 599 RESULT);
668 600 print_err_message("\t\t\t", "unable to free results");
669 601 f_print(fout, "\n");
670 602 f_print(fout, "\t}\n");
671 603 };
672 604 print_return("\t");
673 605 f_print(fout, "}\n");
674 606 }
675 607 }
676 608
677 609 static void
678 610 printerr(char *err, char *transp)
679 611 {
680 612 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp);
681 613 }
682 614
683 615 static void
684 616 printif(char *proc, char *transp, char *prefix, char *arg)
685 617 {
686 618 f_print(fout, "\tif (!svc_%s(%s, _xdr_%s, %s%s)) {\n",
687 619 proc, transp, arg, prefix, arg);
688 620 }
689 621
690 622 int
691 623 nullproc(proc_list *proc)
692 624 {
693 625 for (; proc != NULL; proc = proc->next) {
694 626 if (streq(proc->proc_num, "0"))
695 627 return (1);
696 628 }
697 629 return (0);
698 630 }
699 631
700 632 static void
701 633 write_inetmost(char *infile)
702 634 {
703 635 f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP);
704 636 f_print(fout, "\tint sock;\n");
705 637 f_print(fout, "\tint proto;\n");
706 638 f_print(fout, "\tstruct sockaddr_in saddr;\n");
707 639 f_print(fout, "\tint asize = sizeof (saddr);\n");
708 640 f_print(fout, "\n");
709 641 f_print(fout,
710 642 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n");
711 643 f_print(fout, "\t\tint ssize = sizeof (int);\n\n");
712 644 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n");
713 645 f_print(fout, "\t\t\texit(1);\n");
714 646 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n");
715 647 f_print(fout, "\t\t (char *)&_rpcfdtype, &ssize) == -1)\n");
716 648 f_print(fout, "\t\t\texit(1);\n");
717 649 f_print(fout, "\t\tsock = 0;\n");
718 650 f_print(fout, "\t\t_rpcpmstart = 1;\n");
719 651 f_print(fout, "\t\tproto = 0;\n");
720 652 open_log_file(infile, "\t\t");
721 653 f_print(fout, "\t} else {\n");
722 654 write_rpc_svc_fg(infile, "\t\t");
723 655 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n");
724 656 print_pmapunset("\t\t");
725 657 f_print(fout, "\t}\n");
726 658 }
727 659
728 660 static void
729 661 print_return(char *space)
730 662 {
731 663 if (exitnow) {
732 664 f_print(fout, "%sexit(0);\n", space);
733 665 return;
734 666 }
735 667 if (timerflag) {
736 668 if (mtflag) {
737 669 f_print(fout,
738 670 "%s(void) mutex_lock(&_svcstate_lock);\n",
739 671 space);
740 672 }
741 673 f_print(fout, "%s_rpcsvccount--;\n", space);
742 674 f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space);
743 675 if (mtflag) {
744 676 f_print(fout,
745 677 "%s(void) mutex_unlock(&_svcstate_lock);\n",
746 678 space);
747 679 }
748 680 }
749 681 f_print(fout, "%sreturn; /* CSTYLED */\n", space);
750 682 }
751 683
752 684 static void
753 685 print_pmapunset(char *space)
754 686 {
755 687 list *l;
756 688 definition *def;
757 689 version_list *vp;
758 690
759 691 for (l = defined; l != NULL; l = l->next) {
760 692 def = (definition *)l->val;
761 693 if (def->def_kind == DEF_PROGRAM) {
762 694 for (vp = def->def.pr.versions; vp != NULL;
763 695 vp = vp->next) {
764 696 f_print(fout, "%s(void) pmap_unset(%s, %s);\n",
765 697 space, def->def_name, vp->vers_name);
766 698 }
767 699 }
768 700 }
769 701 }
770 702
771 703 static void
772 704 print_err_message(const char *space, const char *fmt, ...)
773 705 {
774 706 char errbuf[ERRBUFLEN];
775 707 va_list va;
776 708
777 709 va_start(va, fmt);
778 710 (void) vsnprintf(errbuf, sizeof (errbuf), fmt, va);
779 711 va_end(va);
780 712
781 713 if (logflag)
782 714 f_print(fout, "%ssyslog(LOG_ERR, \"%%s\",\n", space);
783 715 else if (inetdflag || pmflag)
784 716 f_print(fout, "%sRPC_MSGOUT(\"%%s\",\n", space);
785 717 else
786 718 f_print(fout, "%sfprintf(stderr, \"%%s\",\n", space);
787 719 f_print(fout, "%s \"%s\");\n", space, errbuf);
788 720 }
789 721
790 722 /*
791 723 * Write the server auxiliary function (RPC_MSGOUT, timeout)
792 724 */
793 725 static void
794 726 write_svc_aux(int nomain)
795 727 {
796 728 if (!logflag)
797 729 write_msg_out();
798 730 if (!nomain)
799 731 write_timeout_func();
800 732 }
801 733
802 734 /*
803 735 * Write the RPC_MSGOUT function
804 736 *
805 737 * Note that while we define RPC_MSGOUT to be printf-like, all existing
↓ open down ↓ |
156 lines elided |
↑ open up ↑ |
806 738 * calls are of the form "%s","<msg>" and this implementation assumes that
807 739 * trivial case. If in the future it's desirable to generate richer calls
808 740 * this implementation can change to match. This way we don't (yet) have
809 741 * to introduce varargs into the generated code.
810 742 */
811 743 static void
812 744 write_msg_out(void)
813 745 {
814 746 f_print(fout, "\n");
815 747 f_print(fout, "#if\tdefined(RPC_MSGOUT)\n");
816 - if (!Cflag) {
817 - f_print(fout, "extern void RPC_MSGOUT();\n");
818 - } else {
819 - f_print(fout, "extern void RPC_MSGOUT(const char *, ...);\n");
820 - }
748 + f_print(fout, "extern void RPC_MSGOUT(const char *, ...);\n");
821 749 f_print(fout, "#else\t/* defined(RPC_MSGOUT) */\n");
822 750 f_print(fout, "static ");
823 - if (!Cflag) {
824 - f_print(fout, "void\nRPC_MSGOUT(fmt, msg)\n");
825 - f_print(fout, "\tchar *fmt;\n");
826 - f_print(fout, "\tchar *msg;\n");
827 - } else {
828 - f_print(fout, "void\nRPC_MSGOUT(const char *fmt, char *msg)\n");
829 - }
751 + f_print(fout, "void\nRPC_MSGOUT(const char *fmt, char *msg)\n");
830 752 f_print(fout, "{\n");
831 753 f_print(fout, "#ifdef RPC_SVC_FG\n");
832 754 if (inetdflag || pmflag)
833 755 f_print(fout, "\tif (_rpcpmstart)\n");
834 756 f_print(fout, "\t\tsyslog(LOG_ERR, fmt, msg);\n");
835 757 f_print(fout, "\telse {\n");
836 758 f_print(fout, "\t\t(void) fprintf(stderr, fmt, msg);\n");
837 759 f_print(fout, "\t\t(void) putc('\\n', stderr);\n");
838 760 f_print(fout, "\t}\n");
839 761 f_print(fout, "#else\n");
840 762 f_print(fout, "\tsyslog(LOG_ERR, fmt, msg);\n");
841 763 f_print(fout, "#endif\n");
842 764 f_print(fout, "}\n");
843 765 f_print(fout, "#endif\t/* defined(RPC_MSGOUT) */\n");
844 766 }
845 767
846 768 /*
847 769 * Write the timeout function
848 770 */
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
849 771 static void
850 772 write_timeout_func(void)
851 773 {
852 774 if (!timerflag)
853 775 return;
854 776
855 777 f_print(fout, "\n");
856 778 if (mtflag) {
857 779 f_print(fout, "/*ARGSUSED*/\n");
858 780 f_print(fout, "static void *\n");
859 - if (!Cflag) {
860 - f_print(fout, "closedown(arg)\n");
861 - f_print(fout, "\tvoid *arg;\n");
862 - } else
863 - f_print(fout, "closedown(void *arg)\n");
781 + f_print(fout, "closedown(void *arg)\n");
864 782 f_print(fout, "{\n");
865 783 f_print(fout, "\t/*CONSTCOND*/\n");
866 784 f_print(fout, "\twhile (1) {\n");
867 785 f_print(fout, "\t\t(void) sleep(_RPCSVC_CLOSEDOWN/2);\n\n");
868 786 f_print(fout,
869 787 "\t\tif (mutex_trylock(&_svcstate_lock) != 0)\n");
870 788 f_print(fout, "\t\t\tcontinue;\n\n");
871 789 f_print(fout,
872 790 "\t\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
873 791 if (tirpcflag) {
874 792 f_print(fout, "\t\t\tint size;\n");
875 793 } else {
876 794 f_print(fout, "\t\t\textern fd_set svc_fdset;\n");
877 795 f_print(fout, "\t\t\tstatic int size;\n");
878 796 }
879 797 f_print(fout, "\t\t\tint i, openfd = 0;\n\n");
880 798 if (tirpcflag) {
881 799 f_print(fout, "\t\t\tsize = svc_max_pollfd;\n");
882 800 } else {
883 801 f_print(fout, "\t\t\tif (size == 0) {\n");
884 802 f_print(fout, "\t\t\t\tsize = getdtablesize();\n");
885 803 f_print(fout, "\t\t\t}\n");
886 804 }
887 805 f_print(fout,
888 806 "\t\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
889 807 if (tirpcflag) {
890 808 f_print(fout, "\t\t\t\tif (svc_pollfd[i].fd >= 0)\n");
891 809 } else {
892 810 f_print(fout, "\t\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
893 811 }
894 812 f_print(fout, "\t\t\t\t\topenfd++;\n");
895 813 f_print(fout, "\t\t\tif (openfd <= 1)\n");
↓ open down ↓ |
22 lines elided |
↑ open up ↑ |
896 814 f_print(fout, "\t\t\t\texit(0);\n");
897 815 f_print(fout, "\t\t} else\n");
898 816 f_print(fout, "\t\t\t_rpcsvcstate = _IDLE;\n\n");
899 817 f_print(fout, "\t\t(void) mutex_unlock(&_svcstate_lock);\n");
900 818 f_print(fout, "\t}\n");
901 819 f_print(fout, "}\n");
902 820 return;
903 821 }
904 822
905 823 f_print(fout, "static void\n");
906 - if (!Cflag) {
907 - f_print(fout, "closedown(sig)\n");
908 - f_print(fout, "\tint sig;\n");
909 - } else
910 - f_print(fout, "closedown(int sig)\n");
824 + f_print(fout, "closedown(int sig)\n");
911 825 f_print(fout, "{\n");
912 826 f_print(fout, "\tif (_rpcsvcstate == _IDLE && _rpcsvccount == 0) {\n");
913 827 if (tirpcflag) {
914 828 f_print(fout, "\t\tint size;\n");
915 829 } else {
916 830 f_print(fout, "\t\textern fd_set svc_fdset;\n");
917 831 f_print(fout, "\t\tstatic int size;\n");
918 832 }
919 833 f_print(fout, "\t\tint i, openfd = 0;\n\n");
920 834 if (tirpcflag) {
921 835 f_print(fout, "\t\tsize = svc_max_pollfd;\n");
922 836 } else {
923 837 f_print(fout, "\t\tif (size == 0) {\n");
924 838 f_print(fout, "\t\t\tsize = getdtablesize();\n");
925 839 f_print(fout, "\t\t}\n");
926 840 }
927 841 f_print(fout,
928 842 "\t\tfor (i = 0; i < size && openfd < 2; i++)\n");
929 843 if (tirpcflag) {
↓ open down ↓ |
9 lines elided |
↑ open up ↑ |
930 844 f_print(fout, "\t\t\tif (svc_pollfd[i].fd >= 0)\n");
931 845 } else {
932 846 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n");
933 847 }
934 848 f_print(fout, "\t\t\t\topenfd++;\n");
935 849 f_print(fout, "\t\tif (openfd <= 1)\n");
936 850 f_print(fout, "\t\t\texit(0);\n");
937 851 f_print(fout, "\t} else\n");
938 852 f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n");
939 853
940 - f_print(fout, "\t(void) signal(SIGALRM, %s closedown);\n",
941 - Cflag? "(SIG_PF)" : "(void(*)())");
854 + f_print(fout, "\t(void) signal(SIGALRM, (SIG_PF) closedown);\n");
942 855 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
943 856 f_print(fout, "}\n");
944 857 }
945 858
946 859 /*
947 860 * Write the most of port monitor support
948 861 */
949 862 static void
950 863 write_pm_most(char *infile, int netflag)
951 864 {
952 865 list *l;
953 866 definition *def;
954 867 version_list *vp;
955 868
956 869 f_print(fout, "\t(void) sigset(SIGPIPE, SIG_IGN);\n\n");
957 870 f_print(fout, "\t/*\n");
958 871 f_print(fout, "\t * If stdin looks like a TLI endpoint, we assume\n");
959 872 f_print(fout, "\t * that we were started by a port monitor. If\n");
960 873 f_print(fout, "\t * t_getstate fails with TBADF, this is not a\n");
961 874 f_print(fout, "\t * TLI endpoint.\n");
962 875 f_print(fout, "\t */\n");
963 876 f_print(fout, "\tif (t_getstate(0) != -1 || t_errno != TBADF) {\n");
964 877 f_print(fout, "\t\tchar *netid;\n");
965 878 if (!netflag) { /* Not included by -n option */
966 879 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n");
967 880 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP);
968 881 }
969 882 if (timerflag)
970 883 f_print(fout, "\t\tint pmclose;\n");
971 884 /*
972 885 * Not necessary, defined in /usr/include/stdlib
973 886 * f_print(fout, "\t\textern char *getenv();\n");
974 887 */
975 888 f_print(fout, "\n");
976 889 f_print(fout, "\t\t_rpcpmstart = 1;\n");
977 890 open_log_file(infile, "\t\t");
978 891 f_print(fout,
979 892 "\n\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n");
980 893
981 894 if (timerflag) {
982 895 f_print(fout, "\t\t/* started from inetd */\n");
983 896 f_print(fout, "\t\t\tpmclose = 1;\n");
984 897 }
985 898 f_print(fout, "\t\t} else {\n");
986 899 f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n");
987 900 print_err_message("\t\t\t\t", "cannot get transport info");
988 901 if (timerflag)
989 902 f_print(fout,
990 903 "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n");
991 904 f_print(fout, "\t\t}\n");
992 905 f_print(fout,
993 906 "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n",
994 907 TRANSP);
995 908 print_err_message("\t\t\t", "cannot create server handle");
996 909 f_print(fout, "\t\t\texit(1);\n");
997 910 f_print(fout, "\t\t}\n");
998 911 f_print(fout, "\t\tif (nconf)\n");
999 912 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n");
1000 913 for (l = defined; l != NULL; l = l->next) {
1001 914 def = (definition *) l->val;
1002 915 if (def->def_kind != DEF_PROGRAM) {
1003 916 continue;
1004 917 }
1005 918 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1006 919 f_print(fout,
1007 920 "\t\tif (!svc_reg(%s, %s, %s,\n",
1008 921 TRANSP, def->def_name, vp->vers_name);
1009 922 f_print(fout, "\t\t ");
1010 923 pvname(def->def_name, vp->vers_num);
1011 924 f_print(fout, ", 0)) {\n");
1012 925 print_err_message("\t\t\t",
1013 926 "unable to register (%s, %s).",
1014 927 def->def_name, vp->vers_name);
1015 928 f_print(fout, "\t\t\texit(1);\n");
1016 929 f_print(fout, "\t\t}\n");
1017 930 }
1018 931 }
1019 932 if (timerflag) {
↓ open down ↓ |
68 lines elided |
↑ open up ↑ |
1020 933 f_print(fout, "\t\tif (pmclose) {\n");
1021 934 if (mtflag) {
1022 935 f_print(fout,
1023 936 "\t\t\tif (thr_create(NULL, 0, closedown, NULL,\n\t\t\t 0, NULL) != 0) {\n");
1024 937 print_err_message("\t\t\t\t",
1025 938 "cannot create closedown thread");
1026 939 f_print(fout, "\t\t\t\texit(1);\n");
1027 940 f_print(fout, "\t\t\t}\n");
1028 941 } else {
1029 942 f_print(fout,
1030 -"\t\t\t(void) signal(SIGALRM, %s closedown);\n",
1031 - Cflag? "(SIG_PF)" : "(void(*)())");
943 +"\t\t\t(void) signal(SIGALRM, (SIG_PF) closedown);\n");
1032 944 f_print(fout,
1033 945 "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n");
1034 946 }
1035 947 f_print(fout, "\t\t}\n");
1036 948 }
1037 949 f_print(fout, "\t\tsvc_run();\n");
1038 950 f_print(fout, "\t\texit(1);\n");
1039 951 f_print(fout, "\t\t/* NOTREACHED */\n");
1040 952 f_print(fout, "\t}");
1041 953 }
1042 954
1043 955 /*
1044 956 * Support for backgrounding the server if self started.
1045 957 */
1046 958 static void
1047 959 write_rpc_svc_fg(char *infile, char *sp)
1048 960 {
1049 961 f_print(fout, "#ifndef RPC_SVC_FG\n");
1050 962 f_print(fout, "#pragma weak closefrom\n");
1051 963 f_print(fout, "%sextern void closefrom();\n", sp);
1052 964 f_print(fout, "%sint size;\n", sp);
1053 965 if (tirpcflag)
1054 966 f_print(fout, "%sstruct rlimit rl;\n", sp);
1055 967 if (inetdflag)
1056 968 f_print(fout, "%sint pid, i;\n\n", sp);
1057 969 f_print(fout, "%spid = fork();\n", sp);
1058 970 f_print(fout, "%sif (pid < 0) {\n", sp);
1059 971 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp);
1060 972 f_print(fout, "%s\texit(1);\n", sp);
1061 973 f_print(fout, "%s}\n", sp);
1062 974 f_print(fout, "%sif (pid)\n", sp);
1063 975 f_print(fout, "%s\texit(0);\n", sp);
1064 976 /* close all file descriptors */
1065 977 if (tirpcflag) {
1066 978 f_print(fout, "%sif (closefrom != NULL)\n", sp);
1067 979 f_print(fout, "%s\tclosefrom(0);\n", sp);
1068 980 f_print(fout, "%selse {\n", sp);
1069 981 f_print(fout, "%s\trl.rlim_max = 0;\n", sp);
1070 982 f_print(fout, "%s\tgetrlimit(RLIMIT_NOFILE, &rl);\n", sp);
1071 983 f_print(fout, "%s\tif ((size = rl.rlim_max) == 0)\n", sp);
1072 984 f_print(fout, "%s\t\texit(1);\n", sp);
1073 985 f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
1074 986 f_print(fout, "%s\t\t(void) close(i);\n", sp);
1075 987 f_print(fout, "%s}\n", sp);
1076 988 } else {
1077 989 f_print(fout, "%s\tsize = getdtablesize();\n", sp);
1078 990 f_print(fout, "%s\tfor (i = 0; i < size; i++)\n", sp);
1079 991 f_print(fout, "%s\t\t(void) close(i);\n", sp);
1080 992 }
1081 993 /* Redirect stderr and stdout to /dev/null */
1082 994 f_print(fout, "%si = open(\"/dev/null\", 2);\n", sp);
1083 995 f_print(fout, "%s(void) dup2(i, 1);\n", sp);
1084 996 f_print(fout, "%s(void) dup2(i, 2);\n", sp);
1085 997 /* This removes control of the controlling terminal */
1086 998 if (tirpcflag)
1087 999 f_print(fout, "%ssetsid();\n", sp);
1088 1000 else {
1089 1001 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp);
1090 1002 f_print(fout, "%sif (i >= 0) {\n", sp);
1091 1003 f_print(fout,
1092 1004 "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp);
1093 1005 f_print(fout, "%s\t(void) close(i);\n", sp);
1094 1006 f_print(fout, "%s}\n", sp);
1095 1007 }
1096 1008 if (!logflag)
1097 1009 open_log_file(infile, sp);
1098 1010 f_print(fout, "#endif\n");
1099 1011 if (logflag)
1100 1012 open_log_file(infile, sp);
1101 1013 }
1102 1014
1103 1015 static void
1104 1016 open_log_file(char *infile, char *sp)
1105 1017 {
1106 1018 char *s;
1107 1019
1108 1020 s = strrchr(infile, '.');
1109 1021 if (s)
1110 1022 *s = '\0';
1111 1023 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile);
1112 1024 if (s)
1113 1025 *s = '.';
1114 1026 }
1115 1027
1116 1028 /*
1117 1029 * write a registration for the given transport for Inetd
1118 1030 */
1119 1031 void
1120 1032 write_inetd_register(char *transp)
1121 1033 {
1122 1034 list *l;
1123 1035 definition *def;
1124 1036 version_list *vp;
1125 1037 char *sp;
1126 1038 int isudp;
1127 1039 char tmpbuf[32];
1128 1040
1129 1041 if (inetdflag)
1130 1042 sp = "\t";
1131 1043 else
1132 1044 sp = "";
1133 1045 if (streq(transp, "udp"))
1134 1046 isudp = 1;
1135 1047 else
1136 1048 isudp = 0;
1137 1049 f_print(fout, "\n");
1138 1050 if (inetdflag) {
1139 1051 f_print(fout,
1140 1052 "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n",
1141 1053 isudp ? "SOCK_DGRAM" : "SOCK_STREAM");
1142 1054 }
1143 1055 f_print(fout, "%s\t%s = svc%s_create(%s",
1144 1056 sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK");
1145 1057 if (!isudp)
1146 1058 f_print(fout, ", 0, 0");
1147 1059 f_print(fout, ");\n");
1148 1060 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP);
1149 1061 (void) snprintf(tmpbuf, sizeof (tmpbuf), "%s\t\t", sp);
1150 1062 print_err_message(tmpbuf, "cannot create %s service.", transp);
1151 1063 f_print(fout, "%s\t\texit(1);\n", sp);
1152 1064 f_print(fout, "%s\t}\n", sp);
1153 1065
1154 1066 if (inetdflag) {
1155 1067 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp);
1156 1068 f_print(fout, "%s\tproto = IPPROTO_%s;\n",
1157 1069 sp, isudp ? "UDP": "TCP");
1158 1070 }
1159 1071 for (l = defined; l != NULL; l = l->next) {
1160 1072 def = (definition *) l->val;
1161 1073 if (def->def_kind != DEF_PROGRAM)
1162 1074 continue;
1163 1075 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) {
1164 1076 f_print(fout, "%s\tif (!svc_register(%s, %s, %s,\n",
1165 1077 sp, TRANSP, def->def_name, vp->vers_name);
1166 1078 f_print(fout, "%s\t ", sp);
1167 1079 pvname(def->def_name, vp->vers_num);
1168 1080 if (inetdflag)
1169 1081 f_print(fout, ", proto)) {\n");
1170 1082 else
1171 1083 f_print(fout, ", IPPROTO_%s)) {\n",
1172 1084 isudp ? "UDP": "TCP");
1173 1085 print_err_message(tmpbuf,
1174 1086 "unable to register (%s, %s, %s).",
1175 1087 def->def_name, vp->vers_name, transp);
1176 1088 f_print(fout, "%s\t\texit(1);\n", sp);
1177 1089 f_print(fout, "%s\t}\n", sp);
1178 1090 }
1179 1091 }
1180 1092 if (inetdflag)
1181 1093 f_print(fout, "\t}\n");
1182 1094 }
↓ open down ↓ |
141 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX