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