Ignore:
Timestamp:
Aug 15, 2011, 1:32:47 PM (13 years ago)
Author:
gah
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/packages/vizservers/nanoscale/server2.c

    r2367 r2368  
    88#include <stdlib.h>
    99#include <sys/wait.h>
     10#include <sys/stat.h>
     11#include <sys/file.h>
    1012#include <syslog.h>
    1113#include <unistd.h>
     
    239241                         NULL);
    240242    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)));
    242244        return FALSE;
    243245    }
     
    270272    strcpy(display, "DISPLAY=:0.0");
    271273    if (putenv(display) < 0) {
    272         ERROR("can't set DISPLAY variable: %s", strerror(errno));
     274        ERROR("Can't set DISPLAY variable: %s", strerror(errno));
    273275        exit(1);
    274276    }
     
    293295            maxCards = strtoul(optarg, 0, 0);
    294296            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");
    296298                return 1;
    297299            }
     
    317319         * /dev/null. */
    318320        if (daemon(0,0) < 0) {
    319             ERROR("can't daemonize nanoscale: %s", strerror(errno));
     321            ERROR("Can't daemonize nanoscale: %s", strerror(errno));
    320322            exit(1);
    321323        }
     
    327329
    328330    if (serverTable.numEntries == 0) {
    329         ERROR("no servers designated.");
     331        ERROR("No servers designated.");
    330332        exit(1);
    331333    }
     
    351353        memcpy(&readFds, &serverFds, sizeof(serverFds));
    352354        if (select(maxFd+1, &readFds, NULL, NULL, 0) <= 0) {
     355            ERROR("Select failed: %s", strerror(errno));
    353356            break;                      /* Error on select. */
    354357        }
    355         INFO("select found waiting\n");
    356358        for (hPtr = Tcl_FirstHashEntry(&serverTable, &iter); hPtr != NULL;
    357359             hPtr = Tcl_NextHashEntry(&iter)) {
    358360            RenderServer *serverPtr;
    359361            pid_t child;
     362            int f;
     363            socklen_t length;
     364            struct sockaddr_in newaddr;
    360365
    361366            serverPtr = Tcl_GetHashValue(hPtr);
    362             INFO("checking server %s\n", serverPtr->name);
    363367            if (!FD_ISSET(serverPtr->listenerFd, &readFds)) {
    364                 INFO("ignore %s (%d)\n", serverPtr->name,
    365                      serverPtr->listenerFd);
    366368                continue;               
    367369            }
    368             INFO("connection requested for  %s on (%d)\n", serverPtr->name,
    369                      serverPtr->listenerFd);
    370370            /* Rotate the display's screen number.  If we have multiple video
    371371             * cards, try to spread the jobs out among them.  */
     
    374374                dispNum = 0;
    375375            }
     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
    376388            /* Fork the new process.  Connect I/O to the new socket. */
    377389            child = fork();
    378390            if (child < 0) {
    379                 ERROR("can't fork \"%s\": %s", serverPtr->name,
     391                ERROR("Can't fork \"%s\": %s", serverPtr->name,
    380392                      strerror(errno));
    381393                continue;
    382             } else if (child == 0) {
     394            }
     395            if (child == 0) {           /* Child process. */
    383396                int i;
    384                 int f;
    385                 socklen_t length;
    386                 struct sockaddr_in newaddr;
     397                int errFd;
    387398               
    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,
    400402                          strerror(errno));
    401403                    exit(1);
    402404                }
    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
    418413                dup2(f, 0);             /* Stdin */
    419414                dup2(f, 1);             /* Stdout */
     415                errFd = open("/dev/null", O_WRONLY, 0600);
     416                dup2(errFd, 2);
    420417                for(i = 3; i <= FD_SETSIZE; i++) {
    421                     close(i);   /* Close all the other descriptors. */
     418                    close(i);           /* Close all the other descriptors. */
    422419                }
    423420                /* Set the enviroment, if necessary. */
     
    433430                /* Finally replace the current process with the render server */
    434431                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],
    436433                      strerror(errno));
    437434                exit(1);
    438435            } else {
    439                 if (!debug) {
    440                     /* Reap initial child which will exit immediately
    441                      * (grandchild continues) */
    442                     waitpid(child, NULL, 0);
    443                 }
     436                close(f);
    444437            }
    445438        }
    446439    }
    447     ERROR("select failed: %s", strerror(errno));
    448440    exit(1);
    449441}
Note: See TracChangeset for help on using the changeset viewer.