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