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