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