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