1 /* $NetBSD: rpc_svcout.c,v 1.26 2013/12/15 06:07:39 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 39 static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI"; 40 #else 41 __RCSID("$NetBSD: rpc_svcout.c,v 1.26 2013/12/15 06:07:39 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 54 static char RQSTP[] = "rqstp"; 55 static char TRANSP[] = "transp"; 56 static char ARG[] = "argument"; 57 static char RESULT[] = "result"; 58 static char ROUTINE[] = "local"; 59 60 static void p_xdrfunc(const char *, const char *); 61 static void internal_proctype(proc_list *); 62 static void write_real_program(definition *); 63 static void write_program(definition *, const char *); 64 static void printerr(const char *, const char *); 65 static void printif(const char *, const char *, const char *, const char *); 66 static void write_inetmost(char *); 67 static void print_return(const char *); 68 static void print_pmapunset(const char *); 69 static void print_err_message(const char *); 70 static void write_timeout_func(void); 71 static void write_caller_func(void); 72 static void write_pm_most(char *, int); 73 static void write_rpc_svc_fg(char *, const char *); 74 static void open_log_file(char *, const char *); 75 static const char *aster(const char *); 76 77 char _errbuf[256]; /* For all messages */ 78 79 static void 80 p_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 86 static void 87 internal_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 */ 99 void 100 write_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 */ 155 void 156 write_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 */ 207 void 208 write_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 */ 236 void 237 write_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 262 void 263 write_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. */ 290 static void 291 write_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 340 static void 341 write_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 f_print(fout, "\n"); 357 if (storage != NULL) { 358 f_print(fout, "%s ", storage); 359 } 360 f_print(fout, "void\n"); 361 pvname(def->def_name, vp->vers_num); 362 363 f_print(fout, "(struct svc_req *%s, ", RQSTP); 364 f_print(fout, "SVCXPRT *%s)\n", TRANSP); 365 f_print(fout, "{\n"); 366 367 filled = 0; 368 f_print(fout, "\tunion {\n"); 369 for (proc = vp->procs; proc != NULL; proc = proc->next) { 370 if (proc->arg_num < 2) { /* single argument */ 371 if (streq(proc->args.decls->decl.type, 372 "void")) { 373 continue; 374 } 375 filled = 1; 376 f_print(fout, "\t\t"); 377 ptype(proc->args.decls->decl.prefix, 378 proc->args.decls->decl.type, 0); 379 pvname(proc->proc_name, vp->vers_num); 380 f_print(fout, "_arg;\n"); 381 382 } else { 383 filled = 1; 384 f_print(fout, "\t\t%s", proc->args.argname); 385 f_print(fout, " "); 386 pvname(proc->proc_name, vp->vers_num); 387 f_print(fout, "_arg;\n"); 388 } 389 } 390 if (!filled) { 391 f_print(fout, "\t\tint fill;\n"); 392 } 393 f_print(fout, "\t} %s;\n", ARG); 394 if (Mflag) { 395 f_print(fout, "\tunion {\n"); 396 for (proc = vp->procs; proc != NULL; proc = proc->next) { 397 f_print(fout, "\t\t"); 398 if (streq(proc->res_type, "void")) 399 f_print(fout, "char "); 400 else 401 ptype(proc->res_prefix, proc->res_type, 402 1); 403 pvname(proc->proc_name, vp->vers_num); 404 f_print(fout, "_res;\n"); 405 } 406 f_print(fout, "\t} %s;\n", RESULT); 407 f_print(fout, "\tbool_t retval;\n"); 408 } else 409 f_print(fout, "\tchar *%s;\n", RESULT); 410 411 f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT); 412 if (Mflag) 413 f_print(fout, 414 "\tbool_t (*%s)(char *, void *, struct svc_req *);\n", 415 ROUTINE); 416 else 417 f_print(fout, 418 "\tchar *(*%s)(char *, struct svc_req *);\n", 419 ROUTINE); 420 421 f_print(fout, "\n"); 422 423 if (callerflag) 424 f_print(fout, "\tcaller = transp;\n"); /* EVAS */ 425 if (timerflag) 426 f_print(fout, "\t_rpcsvcdirty = 1;\n"); 427 f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP); 428 if (!nullproc(vp->procs)) { 429 f_print(fout, "\tcase NULLPROC:\n"); 430 f_print(fout, 431 "\t\t(void) svc_sendreply(%s, (xdrproc_t)xdr_void, NULL);\n", TRANSP); 432 print_return("\t\t"); 433 f_print(fout, "\n"); 434 } 435 for (proc = vp->procs; proc != NULL; proc = proc->next) { 436 f_print(fout, "\tcase %s:\n", proc->proc_name); 437 if (proc->arg_num < 2) { /* single argument */ 438 p_xdrfunc(ARG, proc->args.decls->decl.type); 439 } else { 440 p_xdrfunc(ARG, proc->args.argname); 441 } 442 p_xdrfunc(RESULT, proc->res_type); 443 if (Mflag) 444 f_print(fout, 445 "\t\t%s = (bool_t (*)(char *, void *, struct svc_req *))", 446 ROUTINE); 447 else 448 f_print(fout, 449 "\t\t%s = (char *(*)(char *, struct svc_req *))", 450 ROUTINE); 451 452 if (newstyle) /* new style: calls internal routine */ 453 f_print(fout, "_"); 454 pvname_svc(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(&%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 (Mflag) 470 f_print(fout, "\tretval = (*%s)((char *)&%s, (void *)&%s, %s);\n", 471 ROUTINE, ARG, RESULT, RQSTP); 472 else 473 f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n", 474 RESULT, ROUTINE, ARG, RQSTP); 475 if (Mflag) 476 f_print(fout, 477 "\tif (retval > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n", 478 TRANSP, RESULT, RESULT); 479 else 480 f_print(fout, 481 "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n", 482 RESULT, TRANSP, RESULT, RESULT); 483 printerr("systemerr", TRANSP); 484 f_print(fout, "\t}\n"); 485 486 printif("freeargs", TRANSP, "(caddr_t)&", ARG); 487 (void) sprintf(_errbuf, "unable to free arguments"); 488 print_err_message("\t\t"); 489 f_print(fout, "\t\texit(1);\n"); 490 f_print(fout, "\t}\n"); 491 492 if (Mflag) { 493 f_print(fout, "\tif (!"); 494 pvname(def->def_name, vp->vers_num); 495 f_print(fout, "_freeresult"); 496 f_print(fout, "(%s, xdr_%s, (caddr_t)&%s)) {\n", 497 TRANSP, RESULT, RESULT); 498 (void) sprintf(_errbuf, "unable to free results"); 499 print_err_message("\t\t"); 500 f_print(fout, "\t\texit(1);\n"); 501 f_print(fout, "\t}\n"); 502 } 503 504 print_return("\t"); 505 f_print(fout, "}\n"); 506 } 507 } 508 509 static void 510 printerr(const char *err, const char *transp) 511 { 512 f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp); 513 } 514 515 static void 516 printif(const char *proc, const char *transp, const char *prefix, const char *arg) 517 { 518 f_print(fout, "\tif (!svc_%s(%s, xdr_%s, %s%s)) {\n", 519 proc, transp, arg, prefix, arg); 520 } 521 522 int 523 nullproc(proc_list *proc) 524 { 525 for (; proc != NULL; proc = proc->next) { 526 if (streq(proc->proc_num, "0")) { 527 return (1); 528 } 529 } 530 return (0); 531 } 532 533 static void 534 write_inetmost(char *infile) 535 { 536 f_print(fout, "\tSVCXPRT *%s = NULL;\n", TRANSP); 537 f_print(fout, "\tint sock;\n"); 538 f_print(fout, "\tint proto = 0;\n"); 539 f_print(fout, "\tstruct sockaddr_in saddr;\n"); 540 f_print(fout, "\tsocklen_t asize = (socklen_t)sizeof(saddr);\n"); 541 f_print(fout, "\n"); 542 f_print(fout, 543 "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n"); 544 f_print(fout, "\t\tsocklen_t ssize = (socklen_t)sizeof(int);\n\n"); 545 f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n"); 546 f_print(fout, "\t\t\texit(1);\n"); 547 f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n"); 548 f_print(fout, "\t\t\t\t(void *)&_rpcfdtype, &ssize) == -1)\n"); 549 f_print(fout, "\t\t\texit(1);\n"); 550 f_print(fout, "\t\tsock = 0;\n"); 551 f_print(fout, "\t\t_rpcpmstart = 1;\n"); 552 f_print(fout, "\t\tproto = 0;\n"); 553 open_log_file(infile, "\t\t"); 554 f_print(fout, "\t} else {\n"); 555 write_rpc_svc_fg(infile, "\t\t"); 556 f_print(fout, "\t\tsock = RPC_ANYSOCK;\n"); 557 print_pmapunset("\t\t"); 558 f_print(fout, "\t}\n"); 559 } 560 561 static void 562 print_return(const char *space) 563 { 564 if (exitnow) 565 f_print(fout, "%sexit(0);\n", space); 566 else { 567 if (timerflag) 568 f_print(fout, "%s_rpcsvcdirty = 0;\n", space); 569 f_print(fout, "%sreturn;\n", space); 570 } 571 } 572 573 static void 574 print_pmapunset(const char *space) 575 { 576 list *l; 577 definition *def; 578 version_list *vp; 579 580 for (l = defined; l != NULL; l = l->next) { 581 def = (definition *) l->val; 582 if (def->def_kind == DEF_PROGRAM) { 583 for (vp = def->def.pr.versions; vp != NULL; 584 vp = vp->next) { 585 f_print(fout, "%s(void) pmap_unset(%s, %s);\n", 586 space, def->def_name, vp->vers_name); 587 } 588 } 589 } 590 } 591 592 static void 593 print_err_message(const char *space) 594 { 595 if (logflag) 596 f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf); 597 else 598 if (inetdflag || pmflag) 599 f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf); 600 else 601 f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf); 602 } 603 /* 604 * Write the server auxiliary function ( _msgout, timeout) 605 */ 606 void 607 write_svc_aux(int nomain) 608 { 609 if (!logflag) 610 write_msg_out(); 611 if (!nomain) 612 write_timeout_func(); 613 if (callerflag) /* EVAS */ 614 write_caller_func(); /* EVAS */ 615 } 616 /* 617 * Write the _msgout function 618 */ 619 void 620 write_msg_out(void) 621 { 622 f_print(fout, "\n"); 623 f_print(fout, "static\n"); 624 f_print(fout, "void _msgout(const char *msg)\n"); 625 f_print(fout, "{\n"); 626 f_print(fout, "#ifdef RPC_SVC_FG\n"); 627 if (inetdflag || pmflag) 628 f_print(fout, "\tif (_rpcpmstart)\n"); 629 f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n"); 630 f_print(fout, "\telse\n"); 631 f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n"); 632 f_print(fout, "#else\n"); 633 f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n"); 634 f_print(fout, "#endif\n"); 635 f_print(fout, "}\n"); 636 } 637 /* 638 * Write the timeout function 639 */ 640 static void 641 write_timeout_func(void) 642 { 643 if (!timerflag) 644 return; 645 f_print(fout, "\n"); 646 f_print(fout, "static void closedown(void);\n"); 647 f_print(fout, "\n"); 648 f_print(fout, "static void\n"); 649 f_print(fout, "closedown()\n"); 650 f_print(fout, "{\n"); 651 f_print(fout, "\tif (_rpcsvcdirty == 0) {\n"); 652 f_print(fout, "\t\textern fd_set svc_fdset;\n"); 653 f_print(fout, "\t\tstatic int size;\n"); 654 f_print(fout, "\t\tint i, openfd;\n"); 655 if (tirpcflag && pmflag) { 656 f_print(fout, "\t\tstruct t_info tinfo;\n\n"); 657 f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n"); 658 } else { 659 f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n"); 660 } 661 f_print(fout, "\t\t\texit(0);\n"); 662 f_print(fout, "\t\tif (size == 0) {\n"); 663 if (tirpcflag) { 664 f_print(fout, "\t\t\tstruct rlimit rl;\n\n"); 665 f_print(fout, "\t\t\trl.rlim_max = 0;\n"); 666 f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n"); 667 f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0)\n"); 668 f_print(fout, "\t\t\t\treturn;\n"); 669 } else { 670 f_print(fout, "\t\t\tsize = getdtablesize();\n"); 671 } 672 f_print(fout, "\t\t}\n"); 673 f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n"); 674 f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n"); 675 f_print(fout, "\t\t\t\topenfd++;\n"); 676 f_print(fout, "\t\tif (openfd <= (_rpcpmstart?0:1))\n"); 677 f_print(fout, "\t\t\texit(0);\n"); 678 f_print(fout, "\t}\n"); 679 f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN);\n"); 680 f_print(fout, "}\n"); 681 } 682 683 static void 684 write_caller_func(void) 685 { /* EVAS */ 686 #define P(s) f_print(fout, s); 687 688 P("\n"); 689 P("char *svc_caller()\n"); 690 P("{\n"); 691 P(" struct sockaddr_in actual;\n"); 692 P(" struct hostent *hp;\n"); 693 P(" static struct in_addr prev;\n"); 694 P(" static char cname[128];\n\n"); 695 696 P(" actual = *svc_getcaller(caller);\n\n"); 697 698 P(" if (memcmp((char *)&actual.sin_addr, (char *)&prev,\n"); 699 P(" sizeof(struct in_addr)) == 0)\n"); 700 P(" return (cname);\n\n"); 701 702 P(" prev = actual.sin_addr;\n\n"); 703 704 P(" hp = gethostbyaddr((char *)&actual.sin_addr, sizeof(actual.sin_addr), AF_INET);\n"); 705 P(" if (hp == NULL) { /* dummy one up */\n"); 706 P(" extern char *inet_ntoa();\n"); 707 P(" strlcpy(cname, inet_ntoa(actual.sin_addr), sizeof(cname));\n"); 708 P(" } else {\n"); 709 P(" strlcpy(cname, hp->h_name, sizeof(cname));\n"); 710 P(" }\n\n"); 711 712 P(" return (cname);\n"); 713 P("}\n"); 714 715 #undef P 716 } 717 /* 718 * Write the most of port monitor support 719 */ 720 static void 721 write_pm_most(char *infile, int netflag) 722 { 723 list *l; 724 definition *def; 725 version_list *vp; 726 727 f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n"); 728 f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||"); 729 f_print(fout, " !strcmp(mname, \"timod\"))) {\n"); 730 f_print(fout, "\t\tchar *netid;\n"); 731 if (!netflag) { /* Not included by -n option */ 732 f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n"); 733 f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP); 734 } 735 if (timerflag) 736 f_print(fout, "\t\tint pmclose;\n"); 737 /* not necessary, defined in /usr/include/stdlib */ 738 /* f_print(fout, "\t\textern char *getenv();\n");*/ 739 f_print(fout, "\n"); 740 f_print(fout, "\t\t_rpcpmstart = 1;\n"); 741 if (logflag) 742 open_log_file(infile, "\t\t"); 743 f_print(fout, "\t\tif ((netid = getenv(\"NLSPROVIDER\")) == NULL) {\n"); 744 sprintf(_errbuf, "cannot get transport name"); 745 print_err_message("\t\t\t"); 746 f_print(fout, "\t\t} else if ((nconf = getnetconfigent(netid)) == NULL) {\n"); 747 sprintf(_errbuf, "cannot get transport info"); 748 print_err_message("\t\t\t"); 749 f_print(fout, "\t\t}\n"); 750 /* 751 * A kludgy support for inetd services. Inetd only works with 752 * sockmod, and RPC works only with timod, hence all this jugglery 753 */ 754 f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n"); 755 f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || ioctl(0, I_PUSH, \"timod\")) {\n"); 756 sprintf(_errbuf, "could not get the right module"); 757 print_err_message("\t\t\t\t"); 758 f_print(fout, "\t\t\t\texit(1);\n"); 759 f_print(fout, "\t\t\t}\n"); 760 f_print(fout, "\t\t}\n"); 761 if (timerflag) 762 f_print(fout, "\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n"); 763 f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) == NULL) {\n", 764 TRANSP); 765 sprintf(_errbuf, "cannot create server handle"); 766 print_err_message("\t\t\t"); 767 f_print(fout, "\t\t\texit(1);\n"); 768 f_print(fout, "\t\t}\n"); 769 f_print(fout, "\t\tif (nconf)\n"); 770 f_print(fout, "\t\t\tfreenetconfigent(nconf);\n"); 771 for (l = defined; l != NULL; l = l->next) { 772 def = (definition *) l->val; 773 if (def->def_kind != DEF_PROGRAM) { 774 continue; 775 } 776 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 777 f_print(fout, 778 "\t\tif (!svc_reg(%s, %s, %s, ", 779 TRANSP, def->def_name, vp->vers_name); 780 pvname(def->def_name, vp->vers_num); 781 f_print(fout, ", 0)) {\n"); 782 (void) sprintf(_errbuf, "unable to register (%s, %s).", 783 def->def_name, vp->vers_name); 784 print_err_message("\t\t\t"); 785 f_print(fout, "\t\t\texit(1);\n"); 786 f_print(fout, "\t\t}\n"); 787 } 788 } 789 if (timerflag) { 790 f_print(fout, "\t\tif (pmclose) {\n"); 791 f_print(fout, "\t\t\t(void) signal(SIGALRM, (SIGPF)closedown);\n"); 792 f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN);\n"); 793 f_print(fout, "\t\t}\n"); 794 } 795 f_print(fout, "\t\tsvc_run();\n"); 796 f_print(fout, "\t\texit(1);\n"); 797 f_print(fout, "\t\t/* NOTREACHED */\n"); 798 f_print(fout, "\t}\n"); 799 } 800 /* 801 * Support for backgrounding the server if self started. 802 */ 803 static void 804 write_rpc_svc_fg(char *infile, const char *sp) 805 { 806 f_print(fout, "#ifndef RPC_SVC_FG\n"); 807 f_print(fout, "%sint size;\n", sp); 808 if (tirpcflag) 809 f_print(fout, "%sstruct rlimit rl;\n", sp); 810 if (inetdflag) 811 f_print(fout, "%sint pid, i;\n\n", sp); 812 f_print(fout, "%spid = fork();\n", sp); 813 f_print(fout, "%sif (pid < 0) {\n", sp); 814 f_print(fout, "%s\tperror(\"cannot fork\");\n", sp); 815 f_print(fout, "%s\texit(1);\n", sp); 816 f_print(fout, "%s}\n", sp); 817 f_print(fout, "%sif (pid)\n", sp); 818 f_print(fout, "%s\texit(0);\n", sp); 819 /* get number of file descriptors */ 820 if (tirpcflag) { 821 f_print(fout, "%srl.rlim_max = 0;\n", sp); 822 f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp); 823 f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp); 824 f_print(fout, "%s\texit(1);\n", sp); 825 } else { 826 f_print(fout, "%ssize = getdtablesize();\n", sp); 827 } 828 829 f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp); 830 f_print(fout, "%s\t(void) close(i);\n", sp); 831 /* Redirect stderr and stdout to console */ 832 f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp); 833 f_print(fout, "%s(void) dup2(i, 1);\n", sp); 834 f_print(fout, "%s(void) dup2(i, 2);\n", sp); 835 /* This removes control of the controlling terminal */ 836 if (tirpcflag) 837 f_print(fout, "%ssetsid();\n", sp); 838 else { 839 f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp); 840 f_print(fout, "%sif (i >= 0) {\n", sp); 841 f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, NULL);\n", sp); 842 f_print(fout, "%s\t(void) close(i);\n", sp); 843 f_print(fout, "%s}\n", sp); 844 } 845 if (!logflag) 846 open_log_file(infile, sp); 847 f_print(fout, "#endif\n"); 848 if (logflag) 849 open_log_file(infile, sp); 850 } 851 852 static void 853 open_log_file(char *infile, const char *sp) 854 { 855 char *s, *p; 856 857 s = strrchr(infile, '.'); 858 if (s) 859 *s = '\0'; 860 p = strrchr(infile, '/'); 861 if (p) 862 p++; 863 else 864 p = infile; 865 f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, p); 866 if (s) 867 *s = '.'; 868 } 869 870 /* 871 * write a registration for the given transport for Inetd 872 */ 873 void 874 write_inetd_register(const char *transp) 875 { 876 list *l; 877 definition *def; 878 version_list *vp; 879 const char *sp; 880 int isudp; 881 char tmpbuf[32]; 882 883 if (inetdflag) 884 sp = "\t"; 885 else 886 sp = ""; 887 if (streq(transp, "udp")) 888 isudp = 1; 889 else 890 isudp = 0; 891 f_print(fout, "\n"); 892 if (inetdflag) { 893 f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n", 894 isudp ? "SOCK_DGRAM" : "SOCK_STREAM"); 895 } 896 if (inetdflag && streq(transp, "tcp")) { 897 f_print(fout, "%s\tif (_rpcpmstart)\n", sp); 898 899 f_print(fout, "%s\t\t%s = svc%s_create(%s", 900 sp, TRANSP, "fd", inetdflag ? "sock" : "RPC_ANYSOCK"); 901 if (!isudp) 902 f_print(fout, ", 0, 0"); 903 f_print(fout, ");\n"); 904 905 f_print(fout, "%s\telse\n", sp); 906 907 f_print(fout, "%s\t\t%s = svc%s_create(%s", 908 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK"); 909 if (!isudp) 910 f_print(fout, ", 0, 0"); 911 f_print(fout, ");\n"); 912 913 } else { 914 f_print(fout, "%s\t%s = svc%s_create(%s", 915 sp, TRANSP, transp, inetdflag ? "sock" : "RPC_ANYSOCK"); 916 if (!isudp) 917 f_print(fout, ", 0, 0"); 918 f_print(fout, ");\n"); 919 } 920 f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP); 921 (void) sprintf(_errbuf, "cannot create %s service.", transp); 922 (void) sprintf(tmpbuf, "%s\t\t", sp); 923 print_err_message(tmpbuf); 924 f_print(fout, "%s\t\texit(1);\n", sp); 925 f_print(fout, "%s\t}\n", sp); 926 927 if (inetdflag) { 928 f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp); 929 f_print(fout, "%s\tproto = IPPROTO_%s;\n", 930 sp, isudp ? "UDP" : "TCP"); 931 } 932 for (l = defined; l != NULL; l = l->next) { 933 def = (definition *) l->val; 934 if (def->def_kind != DEF_PROGRAM) { 935 continue; 936 } 937 for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { 938 f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ", 939 sp, TRANSP, def->def_name, vp->vers_name); 940 pvname(def->def_name, vp->vers_num); 941 if (inetdflag) 942 f_print(fout, ", proto)) {\n"); 943 else 944 f_print(fout, ", IPPROTO_%s)) {\n", 945 isudp ? "UDP" : "TCP"); 946 (void) sprintf(_errbuf, "unable to register (%s, %s, %s).", 947 def->def_name, vp->vers_name, transp); 948 print_err_message(tmpbuf); 949 f_print(fout, "%s\t\texit(1);\n", sp); 950 f_print(fout, "%s\t}\n", sp); 951 } 952 } 953 if (inetdflag) 954 f_print(fout, "\t}\n"); 955 } 956 957 static const char * 958 aster(const char *type) 959 { 960 if (isvectordef(type, REL_ALIAS)) { 961 return (""); 962 } else { 963 return ("*"); 964 } 965 } 966