File: performance.h

package info (click to toggle)
hydra 9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 4,008 kB
  • sloc: ansic: 21,638; sh: 2,166; makefile: 110
file content (74 lines) | stat: -rw-r--r-- 1,659 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <unistd.h>
#include <netdb.h>
#include <errno.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
#include <fcntl.h>

/* handles select errors */
int32_t my_select(int32_t fd, fd_set * fdread, fd_set * fdwrite, fd_set * fdex, long sec, long usec) {
  int32_t ret_val;
  struct timeval stv;
  fd_set *fdr2, *fdw2, *fde2;

  do {
    fdr2 = fdread;
    fdw2 = fdwrite;
    fde2 = fdex;
    stv.tv_sec = sec;
    stv.tv_usec = usec;
    if (debug > 1) printf("before select\n");
    ret_val = select(fd, fdr2, fdw2, fde2, &stv);
    if (debug > 1) printf("after select\n");
  /* XXX select() sometimes returns errno=EINTR (signal found) */
  } while (ret_val == -1 && errno == EINTR);

  return ret_val;
}

/*reads in a non-blocking way*/
ssize_t read_safe(int32_t fd, void *buffer, size_t len) {
  int32_t r = 0;
  int32_t total = 0;
  uint32_t toread = len;
  fd_set fr;
  struct timeval tv;
  int32_t ret = 0;

  (void)fcntl(fd, F_SETFL, O_NONBLOCK);
  do {
    FD_ZERO(&fr);
    FD_SET(fd, &fr);
    tv.tv_sec = 0;
    tv.tv_usec = 250000;
    ret = select(fd + 1, &fr, 0, 0, &tv);
  /* XXX select() sometimes return errno=EINTR (signal found) */
  } while (ret == -1 && errno == EINTR);

  if (ret < 0) {
    if (debug) {
      perror("select");
      printf("df:%d\n", fd);
    }
    return -1;
  }

  if (ret > 0) {
    while ((r = read(fd, (char*) ((char*)buffer + total), toread))) {
      if (r == -1) {
        if (errno == EAGAIN)
          break;
        return -1;
      }
      total += r;
      toread -= r;
      if (total == len)
        return len;
      if (r == 0)
        return 0;
    }
  }

  return total;
}