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