示例#1
0
// used by non-threaded version
static void
handleIsoConnectionsThreadless(IsoServer self)
{
    Socket connectionSocket;

    if ((connectionSocket = ServerSocket_accept((ServerSocket) self->serverSocket)) != NULL) {

#if (CONFIG_MAXIMUM_TCP_CLIENT_CONNECTIONS != -1)
        if (private_IsoServer_getConnectionCounter(self) >= CONFIG_MAXIMUM_TCP_CLIENT_CONNECTIONS) {
            if (DEBUG_ISO_SERVER)
                printf("ISO_SERVER: maximum number of connections reached -> reject connection attempt.\n");

            Socket_destroy(connectionSocket);

            handleClientConnections(self);

            return;
        }
#endif

        IsoConnection isoConnection = IsoConnection_create(connectionSocket, self);

        private_IsoServer_increaseConnectionCounter(self);

        addClientConnection(self, isoConnection);

        self->connectionHandler(ISO_CONNECTION_OPENED, self->connectionHandlerParameter,
                isoConnection);

    }

    handleClientConnections(self);
}
示例#2
0
// Main server process that continuously runs until cntrl-c
int main(int argc, char* argv[])
{
  pid_t spid, term_pid; /* pid_t is typedef for Linux process ID */ 
  int chld_status;
  bool forever = true;

  if (argc < 2)
  {
    printf("No port specified\n");
    return (-1);
  }

  /* create a "welcoming" socket at the specified port */
  welcome_socket = ServerSocket_new(atoi(argv[1]));
  if (welcome_socket < 0)
  {
    printf("Failed new server socket\n");
    return (-1);
  }

  /* The daemon infinite loop begins here; terminate with external action*/
  while (forever)
  {
    /* accept an incoming client connection; blocks the
     * process until a connection attempt by a client.
     * creates a new data transfer socket.
     */
    connect_socket = ServerSocket_accept(welcome_socket);
    if (connect_socket < 0)
    {
      printf("Failed accept on server socket\n");
      exit (-1);
    }
    spid = fork();  /* create child == service process */
    if (spid == -1) 
    {
      perror("fork"); 
      exit (-1);
    }
    if (spid == 0) 
    {/* code for the service process */
      ForkExecuteRedirect_service();
      Socket_close(connect_socket);
      exit (0);
    } /* end service process */
    else /* daemon process closes its connect socket */
    {
      Socket_close(connect_socket);
      /* reap a zombie every time through the loop, avoid blocking*/
      term_pid = waitpid(-1, &chld_status, WNOHANG);
    }
  } /* end of infinite loop for daemon process */
}
示例#3
0
int main(int argc, char* argv[]){

  bool forever = true;
  if (argc != NUM_ARGS){

    printf("Please enter the listening port number only.");
    return (ERROR);
  }

  /* create a "welcoming" socket at the specified port */
  welcome_socket = ServerSocket_new(atoi(argv[1]));
  if (welcome_socket < NORMAL){

    printf("Failed new server socket\n");
    return (ERROR);
  }
  connect_socket = ServerSocket_accept(welcome_socket);
  if (connect_socket < NORMAL){
    printf("Failed accept on server socket\n");
    exit (ERROR);
  }
  
  shell_service();
}
示例#4
0
int main(int argc, char *argv[])
{
  int ID;
  char line[MAX_LINE];
  FILE *tmp;
  FILE *output;

  //Argument 1: Welcoming port
  if (argc < 2)
  {
    printf("No port specified\n");
    exit(-1);
  }

  /* Create a welcome socket at the specified port */
  welcome_socket = ServerSocket_new(atoi(argv[1]));
  if (welcome_socket < 0)
  {
    printf("Failed new server socket\n");
    exit(-1);
  }

  /* Open a connect socket through the welcom socket*/
  connect_socket = ServerSocket_accept(welcome_socket);
  if (connect_socket < 0)
  {
    printf("Failed connect socket\n");
    exit(-1);
  }

  Socket_close(welcome_socket);  //Close the welcome socket as it is unused

  pid_t childPID;
  char *argcv[MAXARGSIZE];
  int child_status;

  /* Run until exited*/
  while ( 1 )
  {
    ID = (int)getpid();                    // Get PID of the Current Process
    sprintf(filename, "tmp%d", ID);        // Make file name from the current pid
    tmp = freopen(filename, "w", stdout);  // Redirect stdout to the tmp file

    read_line(line);
  
    childPID = fork();  //Child Process to read from client and execute
    if (childPID < 0)
    {
      printf("Fork error\n");
      exit(-1);
    }

    if (childPID == 0)  //Child process
    {
      tokenize(line, argv);  //Tokenize the input line into arguments
      errno = 0;
      // Check if the file name is valid
      if (execvp(*argv, argv) == -1 ) //Run the command in the arguments list
      {
	printf("Execvp error: %d\n", errno);
        exit(-1);
      }      
    }
    else if (childPID > 0)
    {
      int flag = 0;

      //Wait until the child process finishes
      if (waitpid(-1, &child_status, 0) == -1) {
	printf("Server: wait error\n");
      }
      //Check signals for child process
      if (WIFSIGNALED(child_status)) {
	printf("Server: WIFSIGNALED ERROR: %d\n", WTERMSIG(child_status));
      }
      else if (WIFSTOPPED(child_status)) {
	printf("Server: WIFSTOPPED ERROR: %d\n", WSTOPSIG(child_status));
      }
 
      fclose(tmp);  //Close the redirected temp file

      output_results();  //Ouput thte reuslts back into socket
    }
  }
}
示例#5
0
int main(int argc, char* argv[])
{
  bool doneFlag = false;
  int i;
  int rc;
  int c;
  unsigned char tmp_name[MAX_TMP];
  unsigned char id_str[MAX_TMP];
  int id;
  int childPID, term_pid;
  int chld_status;
  char fc;
  char file_line[MAX_LINE];
  char * args[MAX_ARGS];


    /* variable names for file "handles" */
  FILE *tmpFP;
  FILE *fp;

  if (argc < 2)
  {
    printf("No port specified\n");
    return (-1);
  }

  /* create a "welcoming" socket at the specified port */
  welcome_socket = ServerSocket_new(atoi(argv[1]));
  if (welcome_socket < 0)
  {
    printf("Failed new server socket\n");
    return (-1);
  }

  id = (int) getpid();
  sprintf(id_str, "%d", id);
  strcpy(tmp_name,"tmp");
  strcat(tmp_name, id_str); 

  

     /* accept an incoming client connection; blocks the
      * process until a connection attempt by a client.
      * creates a new data transfer socket.
      */
      connect_socket = ServerSocket_accept(welcome_socket);
      if (connect_socket < 0)
      {
       printf("Failed accept on server socket\n");
       exit (-1);
     }

     Socket_close(welcome_socket);
     char input[MAX_LINE]={'\0'};
     memset(input,'\0',sizeof(char)*MAX_LINE);
     while(1){
      for (i = 0; i < MAX_LINE; i++) {
        c = Socket_getc(connect_socket);
        if (c == EOF) {
          remove(tmp_name);
          doneFlag = true;
          for (i=0; i<=strlen(ERROR2); i++){
            Socket_putc(ERROR2[i], connect_socket);
          }
         // printf("Socket_getc EOF or error\n"); 
          return; /* assume socket EOF ends service for this client */           
        }
        else {
          if (c == '\0') {
            input[i] = '\0';
            break;
          }
          if (c == '\n') {
            i--;
          }
          else{       
           input[i] = c;
         }
       }
     }

       /* be sure the string is terminated if max size reached */
     if (i == MAX_LINE) input[i-1] = '\0';


     fp = freopen(tmp_name, "w", stdout);
     childPID = fork();

     if (childPID == 0){
        //code for child: 

      char * token = strtok(input, " \t");

        args[0] = token; // set first "argument" to the command itself

        int count = 1;
        // loop to fill args[] with the remaining arguments (if any)
        while (token != NULL) {
          token = strtok(NULL, " ");
          
          args[count] = token;
          count++;
          if (count > (MAX_ARGS+2)){
            // handle it
          }
        }
        
        args[count] = NULL; // the array must be null terminated for execvp() to work
        int execCode = execvp(args[0], args);
        if (execCode == -1) {
          for (i=0; i<=strlen(ERROR1); i++){
            Socket_putc(ERROR1[i], connect_socket);
          }
         // exit(0);
          //goto end;
         // printf("Error: invalid command.\n");
        }
      }

      else if (childPID == -1) {
        printf("Problem forking.\n");
      }
      
      else {
        //parent code:
        term_pid = waitpid(childPID, &chld_status, 0);
        if (doneFlag == false){
         char file_c;

         if ((tmpFP = fopen (tmp_name, "r")) == NULL) {
          fprintf (stderr, "error opening tmp file\n");
          exit (-1);
        }
        while ((fc = fgetc(tmpFP)) != EOF) {
          Socket_putc(fc, connect_socket);
        }
      }

      if (term_pid == -1) 
        perror("waitpid"); 

      else
      {
        if (WIFEXITED(chld_status)) {
          char str[] = "PID %d exited, status = %d\n";
          char str2[256];
          sprintf(str2, str, childPID, WEXITSTATUS(chld_status));
          for (i=0; i<=strlen(str2); i++){
            Socket_putc(str2[i], connect_socket);
          }
           //printf("PID %d exited, status = %d\n", childPID, WEXITSTATUS(chld_status));
        }
        else{
         printf("PID %d did not exit normally\n", childPID);
        }
     }
     


   }

   end:;
 }

 Socket_close(connect_socket);
 exit (0);
}