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