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