Beispiel #1
0
/* {{{ pipe_close */
static void pipe_close(pipe_t *p) {
	close_descriptor(p->parent);
	close_descriptor(p->child);
#ifdef WIN32
	close(p->fd);
#endif
}
Beispiel #2
0
static void pipe_close_parent(pipe_t *p) {
	/* don't close stdin */
	close_descriptor(p->parent);
	if (dup2(p->child, p->fd) != p->fd) {
		perror("pipe_child dup2");
	} else {
		close_descriptor(p->child);
		p->child = p->fd;
	}
}
Beispiel #3
0
int cmd_checksum (int argc, const char** argv)
{
  struct descriptor_d d;
  int result = 0;
  unsigned long crc = 0;
  ssize_t cb;

  if (argc < 2)
    return ERROR_PARAM;

  if (   (result = parse_descriptor (argv[1], &d))
      || (result = open_descriptor (&d))) {
    printf ("Unable to open '%s'\n", argv[1]);
    goto fail;
  }

  cb = d.length - d.index;

  result = region_checksum (0, &d, regionChecksumSpinner | regionChecksumLength,
                            &crc);
  if (result)
    goto fail;

  printf ("\rcrc32 0x%lx (%lu) over %d (0x%x) bytes\n", ~crc, ~crc, cb, cb);

 fail:
  close_descriptor (&d);

  return result;
}
int main(int argc, char ** argv) {
  struct timeval tv;
  struct sockaddr_in saddr;
  struct buffer * state_struct[FD_SETSIZE];
  int len_recv = 0, r_len = 0, n_connection = 0;
  // Socket ev timer
  tv.tv_sec = TIME_DIFF;
  tv.tv_usec = 0;

  /* 
    Sets for reading and writing, set_r and 
    set_w is temporary sets in each iteration
  */
  fd_set set_r, set_w, m_set_r, m_set_w;
  size_t max_index = 0;

  size_t s = socket(AF_INET, SOCK_STREAM, 0);
  saddr.sin_family = AF_INET;
  saddr.sin_port = htons(atoi(argv[1]));
  saddr.sin_addr.s_addr = INADDR_ANY;

  if(bind(s, (const struct sockaddr*)&saddr, sizeof(saddr)) < 0) {
    printd("Error of bind socket", 0);
    exit(EXIT_FAILURE);
  }

  if(listen(s, FD_SETSIZE) == -1) {
    printd("Error of listening socket", 0);
    exit(EXIT_FAILURE);
  }

  // Do master socket is nonblocking
  set_nonblock(s);

  printd("Succeed bind and listen socket server on port %i \n", atoi(argv[1]));

  FD_ZERO(&m_set_r);
  FD_ZERO(&m_set_w);
  // For master socket use only reading set
  FD_SET(s, &m_set_r);
  
  // Set max socket index will be equal index of master socket
  // and will be grow when adding clients. Instead you can use FD_SETSIZE
  max_index = s;

  while(1) {
    memcpy(&set_r, &m_set_r, sizeof(m_set_r));
    memcpy(&set_w, &m_set_w, sizeof(m_set_w));

    int rc = select((max_index + 1), &set_r, &set_w, NULL, &tv);
    
    if(rc < 0) {
      printd("Error of select multiplexor", 1);
      exit(EXIT_FAILURE);
    }

    for(int i = 0; i <= max_index; i ++) {
      // Check time window with old awaiting socket
      if(state_struct[i]->upset_time > 0) {
        time_t ctime = clock() / CLOCKS_PER_SEC;
        if(ctime - state_struct[i]->upset_time > TIME_DIFF) {
          close_descriptor(i, state_struct, &m_set_w, &m_set_r);
          continue;
        }
      }

      if(FD_ISSET(i, &set_r)) {        
        if(i == s) {
          n_connection = accept(s, NULL, NULL);
          set_nonblock(n_connection);

          if(n_connection < 0) {
            printd("Error of accepting new connection", 0);
            exit(EXIT_FAILURE);
          }

          // Set number new file descriptor in read/write sets
          FD_SET(n_connection, &m_set_r);
          FD_SET(n_connection, &m_set_w);

          // Initialize state struct for peer
          struct buffer data_connection;
          data_connection.rdc = 0;
          data_connection.rwc = 0;
          data_connection.rd = malloc(BUFFER_SIZE);
          // Don't use write buffer
          //data_connection.wr = malloc(BUFFER_SIZE);

          // Set time window
          data_connection.upset_time = clock() / CLOCKS_PER_SEC;
          state_struct[n_connection] = &data_connection;

          if(max_index < n_connection)
            max_index = n_connection;
        } else {
          char recv_buffer[RW_CHUNK_SIZE];
          len_recv = 0;

          r_len = state_struct[i]->rdc + RW_CHUNK_SIZE;
          // Float chunk size
          if(r_len > BUFFER_SIZE) {
            r_len = (BUFFER_SIZE - state_struct[i]->rdc);
          }

          len_recv = read(i, &recv_buffer, r_len);

          if(!len_recv && r_len) {
            close_descriptor(i, state_struct, &m_set_w, &m_set_r);
          } else if(len_recv == -1) {
            if(errno == EAGAIN || errno == EWOULDBLOCK) {
              continue;
            } else {
              close_descriptor(i, state_struct, &m_set_w, &m_set_r);
            }

          } else if(len_recv) {
            state_struct[i]->upset_time = clock() / CLOCKS_PER_SEC;
            // Copy data to peer buffer with offset
            strncpy((state_struct[i]->rd + state_struct[i]->rdc), recv_buffer, len_recv);
            state_struct[i]->rdc += len_recv;
            printd(state_struct[i]->rd, 0);
            printd("\n", 0);
          }
        }
      }

      if(FD_ISSET(i, &set_w)) {
        if(state_struct[i]->rdc - state_struct[i]->rwc > 0) {
          if(state_struct[i]->rwc < state_struct[i]->rdc) {
            // Set offset from recv buffer
            int w_len = state_struct[i]->rdc - state_struct[i]->rwc;
            // Align write chunk
            if(w_len > RW_CHUNK_SIZE) {
              w_len = RW_CHUNK_SIZE;
            }

            size_t len_r = write(i, (state_struct[i]->rd + state_struct[i]->rwc), w_len);
            
            if(len_r) {
              state_struct[i]->rwc += w_len;
              printd("Wrote %i bytes \n", state_struct[i]->rwc);
            }

            if(state_struct[i]->rwc == state_struct[i]->rdc) {
              state_struct[i]->rdc = 0;
              state_struct[i]->rwc = 0;
              memset(state_struct[i]->rd, 0, BUFFER_SIZE - 1);
            }
          }
        }
      }
    }
  }

  exit(EXIT_SUCCESS);
}
Beispiel #5
0
/* {{{ pipe_close */
static void pipe_close(pipe_t *p) {
	close_descriptor(p->parent);
	close_descriptor(p->child);
}
Beispiel #6
0
static void pipe_close_child(pipe_t *p) {
	close_descriptor(p->child);
	p->fd = p->parent;
}
Beispiel #7
0
static void pipe_close_child(pipe_t *p) {
	close_descriptor(p->child);
	p->fd = _open_osfhandle((long)p->parent,
			(p->fd == 0 ? O_RDONLY : O_WRONLY)|O_BINARY);
}
Beispiel #8
0
ErrnoError DescriptorHolder::Close() {
  DCHECK(IsValid());
  return close_descriptor(fd_);
}
Beispiel #9
0
int cmd_dump (int argc, const char** argv)
{
    struct descriptor_d d;
    int result = 0;
    unsigned long index;
    unsigned long more;
    static int width;

    /* Parse arguments */
    while (argc > 1 && *argv[1] == '-') {
        switch (argv[1][1]) {
        case '1':
        case '2':
        case '4':
            width = argv[1][1] - '0';
            break;
        default:
            return ERROR_PARAM;
            break;
        }
        --argc;
        ++argv;
    }

    if (argc < 2)
        return ERROR_PARAM;

    if ((result = parse_descriptor (argv[1], &d))) {
        printf ("Unable to open '%s'\n", argv[1]);
        goto fail;
    }

    if (!d.length)
        d.length = 16*8;		/* ** FIXME: get from environment */

    if ((result = open_descriptor (&d)))
        goto fail;

    d.width = width;		/* Request I/O of the same width as display */

    index = d.start;
    more = index + MORE_PAGE;

    /* *** FIXME: it would be a very good idea to let this function read
       *** more than 16 bytes from the input stream.  It might be
       *** expensive to fetch the incoming data (e.g. NAND) so we'd like
       *** to be friendly.  That said, it might not matter much since
       *** we're just showing it to the user.  */

    while (index < d.start + d.length) {
        char __aligned rgb[16];
        size_t available = sizeof (rgb);
        int cb;

        if (available > d.start + d.length - index)
            available = d.start + d.length - index;

        cb = d.driver->read (&d, rgb, available);

        if (cb < 0)
            goto fail;

        if (cb == 0) {
            result = ERROR_RESULT (ERROR_FAILURE, "premature end of input");
            goto fail;
        }

        dumpw (rgb, cb, index, width);
        index += cb;

        if (index >= more && index < d.start + d.length) {
            char ch;

            printf (" --More-- ");
            console->read (0, &ch, 1);
            switch (ch) {
            default:
            case 'q':
            case '.':
                index = d.start + d.length;
                break;

            case '\r':
            case '\n':
                more = index + MORE_LINE;
                break;

            case ' ':
                more = index + MORE_PAGE;
                break;
            }
            printf ("\r");
        }
    }

    printf ("         \r");

fail:
    close_descriptor (&d);

    return result;
}
Beispiel #10
0
int cmd_compare (int argc, const char** argv)
{
  struct descriptor_d din;
  struct descriptor_d dout;
  int result = 0;
  ssize_t cbCompare = 0;
  size_t cbTotal = 0;
  int count = 1;

  if (argc >= 3 && strcmp (argv[1], "-c") == 0) {
    count = simple_strtoul (argv[2], NULL, 0);
    argc -= 2;
    argv += 2;
  }

  if (count <= 0)
    ERROR_RETURN (ERROR_PARAM, "count must be >0");

  if (argc < 3)
    return ERROR_PARAM;

  if (   (result = parse_descriptor (argv[1], &din))
      || (result = open_descriptor (&din))) {
    printf ("Unable to open '%s'\n", argv[1]);
    goto fail_early;
  }

  if (   (result = parse_descriptor (argv[2], &dout))
      || (result = open_descriptor (&din))) {
    printf ("Unable to open '%s'\n", argv[2]);
    goto fail;
  }

  if (!din.driver->read || !dout.driver->read) {
    result = ERROR_UNSUPPORTED;
    goto fail;
  }

  /* Make lengths the same */
  cbTotal = din.length;
  if (dout.length && dout.length < din.length)
    cbTotal = dout.length;
  din.length = cbTotal;
  dout.length = cbTotal;

  while (cbCompare < cbTotal && count) {
    char __aligned rgbIn [512];
    char __aligned rgbOut[512];
    ssize_t cbIn;
    ssize_t cbOut;
    ssize_t cb;

    cbIn  = din.driver ->read (&din,  rgbIn,  sizeof (rgbIn));
    cbOut = dout.driver->read (&dout, rgbOut, sizeof (rgbOut));

    if (cbIn != cbOut) {
      printf ("\rregions not the same length\n");
      result = ERROR_FALSE;
      goto fail;
    }

    for (cb = 0; cb < cbIn; ++cb) {
      if (rgbIn[cb] != rgbOut[cb]) {
	printf ("\rregions differ 0x%02x != 0x%02x at %d (0x%x)\n",
		rgbIn[cb], rgbOut[cb], cb + cbCompare, cb + cbCompare);
	result = ERROR_FALSE;
	if (--count == 0)
	  goto fail;
      }
    }
    cbCompare += cbIn;
  }

  if (result == 0)
    printf ("\r%d bytes the same\n", cbCompare);

 fail:
  close_descriptor (&din);
  close_descriptor (&dout);
 fail_early:

  return result;
}