예제 #1
0
/*
 * The client inherits sevaral things that aren't needed to run a game
 * Free them here.
 */
static void
post_fork_cleanup(void)
{
    int i;
    struct client_data *ccur, *cnext;

    /* forking doesn't actually close any of the CLOEXEC file descriptors.
       CLOEXEC is still nice to have and we can use it as a flag to get rid of
       lots of stuff here. */
    for (i = 0; i < get_open_max(); i++)
        if (fcntl(i, F_GETFD) & FD_CLOEXEC)
            close(i);

    for (ccur = disconnected_list_head.next; ccur; ccur = cnext) {
        cnext = ccur->next;
        free(ccur);
    }

    for (ccur = connected_list_head.next; ccur; ccur = cnext) {
        cnext = ccur->next;
        free(ccur);
    }

    free(fd_to_client);
}
예제 #2
0
파일: open_max.c 프로젝트: haveTryTwo/utils
int main(int argc, char* argv[])
{
    long open_max = 0;
    int ret = MY_OK;

    ret = get_open_max(&open_max);
    if (MY_OK == ret)
    {
        printf("open max is : %ld\n", open_max);
    }

    return ret;
}
예제 #3
0
파일: main.c 프로젝트: epitron/dillo
/*! \todo
 * \li Add a dpid_idle_timeout variable to dpidrc
 * \bug Infinite loop if plugin crashes before it accepts a connection
 */
int main(void)
{
   int i, n = 0, open_max;
   int dpid_idle_timeout = 60 * 60; /* default, in seconds */
   struct timeval select_timeout;
   sigset_t mask_none;
   fd_set selected_set;

   dpi_attr_list = NULL;
   services_list = NULL;
   //daemon(0,0); /* Use 0,1 for feedback */
   /* TODO: call setsid() ?? */

   /* Allow read and write access, but only for the user.
    * TODO: can this cause trouble with umount? */
   umask(0077);
   /* TODO: make dpid work on any directory. */
   // chdir("/");

   /* close inherited file descriptors */
   open_max = get_open_max();
   for (i = 3; i < open_max; i++)
      dClose(i);

   /* this sleep used to unmask a race condition */
   // sleep(2);

   dpi_errno = no_errors;

   /* Get list of available dpis */
   numdpis = register_all(&dpi_attr_list);

#if 0
   /* Get name of socket directory */
   dirname = a_Dpi_sockdir_file();
   if ((sockdir = init_sockdir(dirname)) == NULL) {
      ERRMSG("main", "init_sockdir", 0);
      MSG_ERR("Failed to create socket directory\n");
      exit(1);
   }
#endif

   /* Init and get services list */
   fill_services_list(dpi_attr_list, numdpis, &services_list);

   /* Remove any sockets that may have been leftover from a crash */
   //cleanup();

   /* Initialise sockets */
   if ((numsocks = init_ids_srs_socket()) == -1) {
      switch (dpi_errno) {
      case dpid_srs_addrinuse:
         MSG_ERR("dpid refuses to start, possibly because:\n");
         MSG_ERR("\t1) An instance of dpid is already running.\n");
         MSG_ERR("\t2) A previous dpid didn't clean up on exit.\n");
         exit(1);
      default:
         //ERRMSG("main", "init_srs_socket failed", 0);
         ERRMSG("main", "init_ids_srs_socket failed", 0);
         exit(1);
      }
   }
   numsocks = init_all_dpi_sockets(dpi_attr_list);
   est_dpi_terminator();
   est_dpi_sigchld();

   (void) sigemptyset(&mask_sigchld);
   (void) sigaddset(&mask_sigchld, SIGCHLD);
   (void) sigemptyset(&mask_none);
   (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);

   printf("dpid started\n");
/* Start main loop */
   while (1) {
      do {
         (void) sigprocmask(SIG_BLOCK, &mask_sigchld, NULL);
         if (caught_sigchld) {
            handle_sigchld();
            caught_sigchld = 0;
         }
         (void) sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
         select_timeout.tv_sec = dpid_idle_timeout;
         select_timeout.tv_usec = 0;
         selected_set = sock_set;
         n = select(FD_SETSIZE, &selected_set, NULL, NULL, &select_timeout);
         if (n == 0) { /* select timed out, try to exit */
            /* BUG: This is a workaround for dpid not to exit when the
             * downloads server is active. The proper way to handle it is with
             * a dpip command that asks the server whether it's busy.
             * Note: the cookies server may lose session info too. */
            if (server_is_running("downloads"))
               continue;

            stop_active_dpis(dpi_attr_list, numdpis);
            //cleanup();
            exit(0);
         }
      } while (n == -1 && errno == EINTR);

      if (n == -1) {
         ERRMSG("main", "select", errno);
         exit(1);
      }
      /* If the service req socket is selected then service the req. */
      if (FD_ISSET(srs_fd, &selected_set)) {
         int sock_fd;
         socklen_t sin_sz;
         struct sockaddr_in sin;
         char *req = NULL;

         --n;
         assert(n >= 0);
         sin_sz = (socklen_t) sizeof(sin);
         sock_fd = accept(srs_fd, (struct sockaddr *)&sin, &sin_sz);
         if (sock_fd == -1) {
            ERRMSG("main", "accept", errno);
            MSG_ERR("accept on srs socket failed\n");
            MSG_ERR("service pending connections, and continue\n");
         } else {
            int command;
            Dsh *sh;

            sh = a_Dpip_dsh_new(sock_fd, sock_fd, 1024);
read_next:
            req = get_request(sh);
            command = get_command(sh, req);
            switch (command) {
            case AUTH_CMD:
               if (a_Dpip_check_auth(req) != -1) {
                  dFree(req);
                  goto read_next;
               }
               break;
            case BYE_CMD:
               stop_active_dpis(dpi_attr_list, numdpis);
               //cleanup();
               exit(0);
               break;
            case CHECK_SERVER_CMD:
               send_sockport(sock_fd, req, dpi_attr_list);
               break;
            case REGISTER_ALL_CMD:
               register_all_cmd();
               break;
            case UNKNOWN_CMD:
               {
               char *d_cmd = a_Dpip_build_cmd("cmd=%s msg=%s",
                                              "DpiError", "Unknown command");
               (void) CKD_WRITE(sock_fd, d_cmd);
               dFree(d_cmd);
               ERRMSG("main", "Unknown command", 0);
               MSG_ERR(" for request: %s\n", req);
               break;
               }
            case -1:
               _ERRMSG("main", "get_command failed", 0);
               break;
            }
            if (req)
               free(req);
            a_Dpip_dsh_close(sh);
            a_Dpip_dsh_free(sh);
         }
      }

      /* While there's a request on one of the plugin sockets
       * find the matching plugin and start it. */
      for (i = 0; n > 0 && i < numdpis; i++) {
         if (FD_ISSET(dpi_attr_list[i].sock_fd, &selected_set)) {
            --n;
            assert(n >= 0);

            if (dpi_attr_list[i].filter) {
               /* start a dpi filter plugin and continue watching its socket
                * for new connections */
               (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
               start_filter_plugin(dpi_attr_list[i]);
            } else {
               /* start a dpi server plugin but don't wait for new connections
                * on its socket */
               numsocks--;
               assert(numsocks >= 0);
               FD_CLR(dpi_attr_list[i].sock_fd, &sock_set);
               if ((dpi_attr_list[i].pid = fork()) == -1) {
                  ERRMSG("main", "fork", errno);
                  /* exit(1); */
               } else if (dpi_attr_list[i].pid == 0) {
                  /* child */
                  (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
                  start_server_plugin(dpi_attr_list[i]);
               }
            }
         }
      }
   }
}
예제 #4
0
/* initialize 'daemon mode' for server processes */
int
daemonize(void)
{
    int   maxfd;
    int   tmpfd;
    pid_t pid;

    pid = fork();
    if (pid < 0) {
        /* fork() failed */

        return -1;
    } else if (pid) {
        /* PARENT PROCESS */

        exit(0);
    }

    /* CHILD PROCESS */

    /* create new session */
    setsid();
    /* clear environment */
    clearenv();
    /* fork again */
    pid = fork();
    if (pid < 0) {
        /* fork failed */

        return -1;
    } else if (pid) {
        /* PARENT PROCESS */

        exit(0);
    }

    /* CHILD PROCESS */

    /* change current working directory */
    if (chdir(ADIVIX_SERVER_ROOT) < 0) {

        return -1;
    }
    /* change root directory; sandbox server process */
    if (chroot(ADIVIX_SERVER_ROOT) < 0) {

        return -1;
    }
    /* mask all but user and group read-permissions */
    umask(0337);
    /* query maximum number of open files */
    maxfd = get_open_max();
    /* close all open descriptors */
    for (tmpfd = 0 ; tmpfd < maxfd ; tmpfd++) {
	close(tmpfd);
    }
    /* set stdin, stdout, and stderr to /dev/null */
    tmpfd = open("/dev/null", O_RDWR, 0);
    if (tmpfd >= 0) {
#if defined(STDIN_FILENO)
	dup2(tmpfd, STDIN_FILENO);
	dup2(tmpfd, STDOUT_FILENO);
	dup2(tmpfd, STDERR_FILENO);
#else
	dup2(tmpfd, 0);
	dup2(tmpfd, 1);
	dup2(tmpfd, 2);
#endif
    }
    /* close descriptor for "/dev/null if not stdin, stdout, or stderr */
    if (tmpfd > 2) {
        close(tmpfd);
    }

    return 0;
}