Changeset 4131 for trunk/packages/vizservers/nanoscale/server.c
- Timestamp:
- Jan 17, 2014, 10:08:51 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanoscale/server.c
r4130 r4131 98 98 s++; 99 99 } 100 length = snprintf(message, MSG_LEN, "nanoscale (%d %d) %s: %s:%d ", 100 length = snprintf(message, MSG_LEN, "nanoscale (%d %d) %s: %s:%d ", 101 101 serverPid, getpid(), syslogLevels[priority], s, lineNum); 102 102 length += vsnprintf(message + length, MSG_LEN - length, fmt, lst); … … 109 109 } 110 110 111 static void 111 static void 112 112 Help(const char *program) 113 113 { … … 139 139 140 140 static int 141 ParseSwitches(Tcl_Interp *interp, RenderServer *serverPtr, int *objcPtr, 141 ParseSwitches(Tcl_Interp *interp, RenderServer *serverPtr, int *objcPtr, 142 142 Tcl_Obj ***objvPtr) 143 143 { … … 158 158 if ((c == 'i') && (strcmp(string, "-input") == 0)) { 159 159 int f; 160 160 161 161 if (Tcl_GetIntFromObj(interp, objv[i+1], &f) != TCL_OK) { 162 162 return TCL_ERROR; … … 165 165 } else if ((c == 'o') && (strcmp(string, "-output") == 0)) { 166 166 int f; 167 167 168 168 if (Tcl_GetIntFromObj(interp, objv[i+1], &f) != TCL_OK) { 169 169 return TCL_ERROR; … … 172 172 } else if ((c == 'l') && (strcmp(string, "-logstdout") == 0)) { 173 173 int state; 174 174 175 175 if (Tcl_GetBooleanFromObj(interp, objv[i+1], &state) != TCL_OK) { 176 176 return TCL_ERROR; … … 210 210 * register_server <name> <port> <cmd> <environ> 211 211 * 212 * where 212 * where 213 213 * 214 214 * name Token for the render server. 215 215 * port Port to listen to accept connections. 216 216 * cmd Command to be run to start the render server. 217 * environ Name-value pairs of representing environment 217 * environ Name-value pairs of representing environment 218 218 * variables. 219 219 * 220 * Note that "cmd" and "environ" are variable and backslash 221 * substituted. A listener socket automatically is established on 222 * the given port to accept client requests. 223 * 220 * Note that "cmd" and "environ" are variable and backslash 221 * substituted. A listener socket automatically is established on 222 * the given port to accept client requests. 223 * 224 224 * Example: 225 225 * … … 248 248 249 249 if (objc < 4) { 250 Tcl_AppendResult(interp, "wrong # args: should be \"", 251 Tcl_GetString(objv[0]), " serverName port ?flags? cmd ?environ?", 250 Tcl_AppendResult(interp, "wrong # args: should be \"", 251 Tcl_GetString(objv[0]), " serverName port ?flags? cmd ?environ?", 252 252 (char *)NULL); 253 253 return TCL_ERROR; … … 259 259 hPtr = Tcl_CreateHashEntry(&serverTable, (char *)((long)port), &isNew); 260 260 if (!isNew) { 261 Tcl_AppendResult(interp, "a server is already listening on port ", 261 Tcl_AppendResult(interp, "a server is already listening on port ", 262 262 Tcl_GetString(objv[2]), (char *)NULL); 263 263 return TCL_ERROR; … … 271 271 goto error; 272 272 } 273 objPtr = Tcl_SubstObj(interp, objv[3], 273 objPtr = Tcl_SubstObj(interp, objv[3], 274 274 TCL_SUBST_VARIABLES | TCL_SUBST_BACKSLASHES); 275 if (Tcl_SplitList(interp, Tcl_GetString(objPtr), &numCmdArgs, 275 if (Tcl_SplitList(interp, Tcl_GetString(objPtr), &numCmdArgs, 276 276 (const char ***)&cmdArgs) != TCL_OK) { 277 277 goto error; … … 279 279 serverPtr->cmdArgs = cmdArgs; 280 280 serverPtr->numCmdArgs = numCmdArgs; 281 281 282 282 numEnvArgs = 0; 283 283 envArgs = NULL; 284 284 if (objc == 5) { 285 objPtr = Tcl_SubstObj(interp, objv[4], 285 objPtr = Tcl_SubstObj(interp, objv[4], 286 286 TCL_SUBST_VARIABLES | TCL_SUBST_BACKSLASHES); 287 if (Tcl_SplitList(interp, Tcl_GetString(objPtr), &numEnvArgs, 287 if (Tcl_SplitList(interp, Tcl_GetString(objPtr), &numEnvArgs, 288 288 (const char ***)&envArgs) != TCL_OK) { 289 289 goto error; 290 290 } 291 291 if (numEnvArgs & 0x1) { 292 Tcl_AppendResult(interp, "odd # elements in enviroment list", 292 Tcl_AppendResult(interp, "odd # elements in enviroment list", 293 293 (char *)NULL); 294 294 goto error; … … 305 305 goto error; 306 306 } 307 307 308 308 /* If the render server instance should be killed, drop the socket address 309 309 * reservation immediately, don't linger. */ … … 338 338 } 339 339 340 static int 340 static int 341 341 ParseServersFile(const char *fileName) 342 342 { … … 345 345 interp = Tcl_CreateInterp(); 346 346 Tcl_MakeSafe(interp); 347 Tcl_CreateObjCommand(interp, "register_server", RegisterServerCmd, NULL, 347 Tcl_CreateObjCommand(interp, "register_server", RegisterServerCmd, NULL, 348 348 NULL); 349 349 if (Tcl_EvalFile(interp, fileName) != TCL_OK) { … … 355 355 } 356 356 357 int 357 int 358 358 main(int argc, char **argv) 359 359 { … … 363 363 fd_set serverFds; 364 364 int maxFd; /* Highest file descriptor in use. */ 365 char display[200]; /* String used to manage the X 366 * DISPLAY variable for each render 365 char display[200]; /* String used to manage the X 366 * DISPLAY variable for each render 367 367 * server instance. */ 368 368 int maxCards; /* Maximum number of video cards, each … … 373 373 Tcl_HashSearch iter; 374 374 const char *fileName; /* Path to servers file. */ 375 375 376 376 serverPid = getpid(); 377 377 screenNum = 0; … … 432 432 if (!ParseServersFile(fileName)) { 433 433 exit(1); 434 } 434 } 435 435 436 436 if (serverTable.numEntries == 0) { … … 453 453 hPtr = Tcl_NextHashEntry(&iter)) { 454 454 RenderServer *serverPtr; 455 455 456 456 serverPtr = Tcl_GetHashValue(hPtr); 457 457 FD_SET(serverPtr->listenerFd, &serverFds); … … 480 480 serverPtr = Tcl_GetHashValue(hPtr); 481 481 if (!FD_ISSET(serverPtr->listenerFd, &readFds)) { 482 continue; 482 continue; 483 483 } 484 484 /* Rotate the display's screen number. If we have multiple video … … 491 491 length = sizeof(newaddr); 492 492 #ifdef HAVE_ACCEPT4 493 sock = accept4(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 493 sock = accept4(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 494 494 &length, SOCK_CLOEXEC); 495 495 #else 496 sock = accept(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 496 sock = accept(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 497 497 &length); 498 498 #endif 499 499 if (sock < 0) { 500 ERROR("Can't accept server \"%s\": %s", serverPtr->name, 500 ERROR("Can't accept server \"%s\": %s", serverPtr->name, 501 501 strerror(errno)); 502 502 exit(1); … … 506 506 flags |= FD_CLOEXEC; 507 507 if (fcntl(sock, F_SETFD, flags) < 0) { 508 ERROR("Can't set FD_CLOEXEC on socket \"%s\": %s", 508 ERROR("Can't set FD_CLOEXEC on socket \"%s\": %s", 509 509 serverPtr->name, strerror(errno)); 510 510 exit(1); 511 511 } 512 512 #endif 513 INFO("Connecting \"%s\" to %s\n", serverPtr->name, 513 INFO("Connecting \"%s\" to %s\n", serverPtr->name, 514 514 inet_ntoa(newaddr.sin_addr)); 515 515 … … 517 517 child = fork(); 518 518 if (child < 0) { 519 ERROR("Can't fork \"%s\": %s", serverPtr->name, 519 ERROR("Can't fork \"%s\": %s", serverPtr->name, 520 520 strerror(errno)); 521 521 continue; 522 } 522 } 523 523 if (child == 0) { /* Child process. */ 524 524 int i; … … 526 526 umask(0); 527 527 if ((!debug) && (setsid() < 0)) { 528 ERROR("Can't setsid \"%s\": %s", serverPtr->name, 528 ERROR("Can't setsid \"%s\": %s", serverPtr->name, 529 529 strerror(errno)); 530 530 exit(1); 531 531 } 532 532 if ((!debug) && ((chdir("/")) < 0)) { 533 ERROR("Can't change to root directory for \"%s\": %s", 533 ERROR("Can't change to root directory for \"%s\": %s", 534 534 serverPtr->name, strerror(errno)); 535 535 exit(1); … … 539 539 int newFd; 540 540 541 sprintf(path, "%s/%s-%d.log", LOGDIR, 541 sprintf(path, "%s/%s-%d.log", LOGDIR, 542 542 serverPtr->name, getpid()); 543 543 if (serverPtr->logStdout) { … … 546 546 newFd = open("/dev/null", O_WRONLY, 0600); 547 547 } 548 if (newFd < 0) { 548 if (newFd < 0) { 549 549 ERROR("%s: can't open \"%s\": %s", serverPtr->name, 550 550 path, strerror(errno)); 551 551 exit(1); 552 } 553 if (dup2(newFd, 1) < 0) { 554 ERROR("%s: can't dup stdout to \"%s\": %s", 552 } 553 if (dup2(newFd, 1) < 0) { 554 ERROR("%s: can't dup stdout to \"%s\": %s", 555 555 serverPtr->name, path, strerror(errno)); 556 556 exit(1); 557 557 } 558 558 if (dup2(newFd, 2) < 0) { 559 ERROR("%s: can't dup stderr to \"%s\": %s", 559 ERROR("%s: can't dup stderr to \"%s\": %s", 560 560 serverPtr->name, path, strerror(errno)); 561 561 exit(1); … … 565 565 int newFd; 566 566 567 sprintf(path, "%s/%s-%d.stdout", LOGDIR, 567 sprintf(path, "%s/%s-%d.stdout", LOGDIR, 568 568 serverPtr->name, getpid()); 569 569 if (serverPtr->logStdout) { … … 572 572 newFd = open("/dev/null", O_WRONLY, 0600); 573 573 } 574 if (newFd < 0) { 574 if (newFd < 0) { 575 575 ERROR("%s: can't open \"%s\": %s", serverPtr->name, 576 576 path, strerror(errno)); 577 577 exit(1); 578 } 579 if (dup2(newFd, 1) < 0) { 580 ERROR("%s: can't dup stdout to \"%s\": %s", 578 } 579 if (dup2(newFd, 1) < 0) { 580 ERROR("%s: can't dup stdout to \"%s\": %s", 581 581 serverPtr->name, path, strerror(errno)); 582 582 exit(1); 583 583 } 584 sprintf(path, "%s/%s-%d.stderr", LOGDIR, 584 sprintf(path, "%s/%s-%d.stderr", LOGDIR, 585 585 serverPtr->name, getpid()); 586 586 if (serverPtr->logStderr) { … … 589 589 newFd = open("/dev/null", O_WRONLY, 0600); 590 590 } 591 if (newFd < 0) { 591 if (newFd < 0) { 592 592 ERROR("%s: can't open \"%s\": %s", serverPtr->name, 593 593 path, strerror(errno)); 594 594 exit(1); 595 } 596 if (dup2(newFd, 1) < 0) { 597 ERROR("%s: can't dup stderr to \"%s\": %s", 595 } 596 if (dup2(newFd, 1) < 0) { 597 ERROR("%s: can't dup stderr to \"%s\": %s", 598 598 serverPtr->name, path, strerror(errno)); 599 599 exit(1); 600 600 } 601 } 601 } 602 602 /* Dup the socket to descriptors, normally 3 and 4 */ 603 603 if (dup2(sock, serverPtr->inputFd) < 0) { /* Stdin */ 604 ERROR("%s: can't dup stdin: %s", serverPtr->name, 604 ERROR("%s: can't dup stdin: %s", serverPtr->name, 605 605 strerror(errno)); 606 606 exit(1); 607 607 } 608 608 if (dup2(sock, serverPtr->outputFd) < 0) { /* Stdout */ 609 ERROR("%s: can't dup stdout: %s", serverPtr->name, 609 ERROR("%s: can't dup stdout: %s", serverPtr->name, 610 610 strerror(errno)); 611 611 exit(1); … … 622 622 setenv(serverPtr->envArgs[i], serverPtr->envArgs[i+1], 1); 623 623 } 624 INFO("Executing %s: client %s, %s in=%d out=%d on DISPLAY=%s", 625 serverPtr->name, inet_ntoa(newaddr.sin_addr), 624 INFO("Executing %s: client %s, %s in=%d out=%d on DISPLAY=%s", 625 serverPtr->name, inet_ntoa(newaddr.sin_addr), 626 626 serverPtr->cmdArgs[0], serverPtr->inputFd, 627 627 serverPtr->outputFd, display); 628 628 /* Replace the current process with the render server. */ 629 629 execvp(serverPtr->cmdArgs[0], serverPtr->cmdArgs); 630 ERROR("Can't execute \"%s\": %s", serverPtr->cmdArgs[0], 630 ERROR("Can't execute \"%s\": %s", serverPtr->cmdArgs[0], 631 631 strerror(errno)); 632 632 exit(1); … … 634 634 close(sock); 635 635 } 636 } 636 } 637 637 } 638 638 exit(1);
Note: See TracChangeset
for help on using the changeset viewer.