Changeset 2368 for trunk/packages
- Timestamp:
- Aug 15, 2011, 1:32:47 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/packages/vizservers/nanoscale/server2.c
r2367 r2368 8 8 #include <stdlib.h> 9 9 #include <sys/wait.h> 10 #include <sys/stat.h> 11 #include <sys/file.h> 10 12 #include <syslog.h> 11 13 #include <unistd.h> … … 239 241 NULL); 240 242 if (Tcl_EvalFile(interp, fileName) != TCL_OK) { 241 ERROR(" can't add server: %s", Tcl_GetString(Tcl_GetObjResult(interp)));243 ERROR("Can't add server: %s", Tcl_GetString(Tcl_GetObjResult(interp))); 242 244 return FALSE; 243 245 } … … 270 272 strcpy(display, "DISPLAY=:0.0"); 271 273 if (putenv(display) < 0) { 272 ERROR(" can't set DISPLAY variable: %s", strerror(errno));274 ERROR("Can't set DISPLAY variable: %s", strerror(errno)); 273 275 exit(1); 274 276 } … … 293 295 maxCards = strtoul(optarg, 0, 0); 294 296 if ((maxCards < 1) || (maxCards > 10)) { 295 fprintf(stderr, " bad number of max videocards specified\n");297 fprintf(stderr, "Bad number of max videocards specified\n"); 296 298 return 1; 297 299 } … … 317 319 * /dev/null. */ 318 320 if (daemon(0,0) < 0) { 319 ERROR(" can't daemonize nanoscale: %s", strerror(errno));321 ERROR("Can't daemonize nanoscale: %s", strerror(errno)); 320 322 exit(1); 321 323 } … … 327 329 328 330 if (serverTable.numEntries == 0) { 329 ERROR(" no servers designated.");331 ERROR("No servers designated."); 330 332 exit(1); 331 333 } … … 351 353 memcpy(&readFds, &serverFds, sizeof(serverFds)); 352 354 if (select(maxFd+1, &readFds, NULL, NULL, 0) <= 0) { 355 ERROR("Select failed: %s", strerror(errno)); 353 356 break; /* Error on select. */ 354 357 } 355 INFO("select found waiting\n");356 358 for (hPtr = Tcl_FirstHashEntry(&serverTable, &iter); hPtr != NULL; 357 359 hPtr = Tcl_NextHashEntry(&iter)) { 358 360 RenderServer *serverPtr; 359 361 pid_t child; 362 int f; 363 socklen_t length; 364 struct sockaddr_in newaddr; 360 365 361 366 serverPtr = Tcl_GetHashValue(hPtr); 362 INFO("checking server %s\n", serverPtr->name);363 367 if (!FD_ISSET(serverPtr->listenerFd, &readFds)) { 364 INFO("ignore %s (%d)\n", serverPtr->name,365 serverPtr->listenerFd);366 368 continue; 367 369 } 368 INFO("connection requested for %s on (%d)\n", serverPtr->name,369 serverPtr->listenerFd);370 370 /* Rotate the display's screen number. If we have multiple video 371 371 * cards, try to spread the jobs out among them. */ … … 374 374 dispNum = 0; 375 375 } 376 /* Accept the new connection. */ 377 length = sizeof(newaddr); 378 f = accept(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 379 &length); 380 if (f < 0) { 381 ERROR("Can't accept server \"%s\": %s", serverPtr->name, 382 strerror(errno)); 383 exit(1); 384 } 385 INFO("Connecting \"%s\" to %s\n", serverPtr->name, 386 inet_ntoa(newaddr.sin_addr)); 387 376 388 /* Fork the new process. Connect I/O to the new socket. */ 377 389 child = fork(); 378 390 if (child < 0) { 379 ERROR(" can't fork \"%s\": %s", serverPtr->name,391 ERROR("Can't fork \"%s\": %s", serverPtr->name, 380 392 strerror(errno)); 381 393 continue; 382 } else if (child == 0) { 394 } 395 if (child == 0) { /* Child process. */ 383 396 int i; 384 int f; 385 socklen_t length; 386 struct sockaddr_in newaddr; 397 int errFd; 387 398 388 INFO("after fork child=%d server=\"%s\"\n", 389 getpid(), serverPtr->name); 390 /* Try to accept the connection and start the server. */ 391 392 /* Accept the new connection. */ 393 length = sizeof(newaddr); 394 INFO("Trying to accept connection for server=\"%s\"\n", 395 serverPtr->name); 396 f = accept(serverPtr->listenerFd, (struct sockaddr *)&newaddr, 397 &length); 398 if (f < 0) { 399 ERROR("can't accept server \"%s\": %s", serverPtr->name, 399 umask(0); 400 if ((!debug) && (setsid() < 0)) { 401 ERROR("Can't setsid \"%s\": %s", serverPtr->name, 400 402 strerror(errno)); 401 403 exit(1); 402 404 } 403 /* Child process. */ 404 if (!debug) { 405 /* Detach this child process from the parent nanoscale 406 * process. The current directory becomes /tmp, but don't 407 * redirect stdin/stdout/stderr to /dev/null, we'll use 408 * that to connect to the socket. */ 409 if (daemon(0, 0) < 0) { 410 ERROR("can't daemonize \"%s\": %s", serverPtr->name, 411 strerror(errno)); 412 } 413 } 414 415 INFO("child=%d Connecting \"%s\" to %s\n", 416 getpid(), serverPtr->name, inet_ntoa(newaddr.sin_addr)); 417 405 if ((!debug) && ((chdir("/")) < 0)) { 406 ERROR("Can't change to root directory for \"%s\": %s", 407 serverPtr->name, strerror(errno)); 408 exit(1); 409 } 410 411 /* Dup the descriptors and start the server. */ 412 418 413 dup2(f, 0); /* Stdin */ 419 414 dup2(f, 1); /* Stdout */ 415 errFd = open("/dev/null", O_WRONLY, 0600); 416 dup2(errFd, 2); 420 417 for(i = 3; i <= FD_SETSIZE; i++) { 421 close(i); /* Close all the other descriptors. */418 close(i); /* Close all the other descriptors. */ 422 419 } 423 420 /* Set the enviroment, if necessary. */ … … 433 430 /* Finally replace the current process with the render server */ 434 431 execvp(serverPtr->cmdArgs[0], serverPtr->cmdArgs); 435 ERROR(" can't execute \"%s\": %s", serverPtr->cmdArgs[0],432 ERROR("Can't execute \"%s\": %s", serverPtr->cmdArgs[0], 436 433 strerror(errno)); 437 434 exit(1); 438 435 } else { 439 if (!debug) { 440 /* Reap initial child which will exit immediately 441 * (grandchild continues) */ 442 waitpid(child, NULL, 0); 443 } 436 close(f); 444 437 } 445 438 } 446 439 } 447 ERROR("select failed: %s", strerror(errno));448 440 exit(1); 449 441 }
Note: See TracChangeset
for help on using the changeset viewer.