ssize_t read(int fd, void *buf, size_t count) { static int (*libc_read)(int fd, void *buf, size_t count); hook(read); if (pending_frames(fd)) push_pending_frames(fd, read); return libc_read(fd, buf, count); }
ssize_t read (int fd, void *buf, size_t n) { #if __DEBUG__ printf("function %s called\n", __func__); #endif if (!is_simptcp_descriptor(fd)) { return libc_read(fd, buf, n); } /* Here comes the code for the read related to simtcp */ return recv(fd, buf, n, MSG_WAITALL); }
ssize_t read(int fd, void *buf, size_t count) { char *fd_path, *env_random_str; size_t size, fd_path_size; if (libc_read == NULL) { libc_read = dlsym(RTLD_NEXT, "read"); if (libc_read == NULL) { fprintf(stderr, "dlsym for read error: %s\n", dlerror()); goto out; } } char *path = malloc(PATH_MAX * sizeof(char)); memset(path, (int) '\0', PATH_MAX * sizeof(char)); pid_t pid = getpid(); sprintf(path, "/proc/%d/fd/%d", pid, fd); fd_path = malloc(PATH_MAX * sizeof(char)); if (fd_path == NULL) { fprintf(stderr, "Failed to malloc"); goto out; } fd_path_size = readlink(path, fd_path, PATH_MAX * sizeof(char)); if (fd_path_size == -1) { fprintf(stderr, "Failed to readlink"); goto out; } if (strcmp(fd_path, "/dev/random") == 0 || strcmp(fd_path, "/dev/urandom") == 0) { env_random_str = getenv(ENVNAME); if (env_random_str == NULL) { goto out; } size = MIN(count, strlen(env_random_str)); strncpy(buf, env_random_str, size); return size; } out: return libc_read(fd, buf, count); }
void packetSocket::readFromFile() { /*---------------------------------------------------------------------------- Read some data from the underlying stream socket. Read as much as is available right now, up to 4K. Update 'this' to reflect the data read. E.g. if we read an entire packet, we add it to the packet buffer (this->readBuffer). If we read the first part of a packet, we add it to the packet accumulator (*this->packetAccumP). If we read the end of a packet, we add the full packet to the packet buffer and empty the packet accumulator. Etc. -----------------------------------------------------------------------------*/ bool wouldblock; wouldblock = false; while (this->readBuffer.empty() && !this->eof && !wouldblock) { unsigned char buffer[4096]; ssize_t rc; rc = libc_read(this->sockFd, buffer, sizeof(buffer)); if (rc < 0) { if (errno == EWOULDBLOCK) wouldblock = true; else throwf("read() of socket failed with errno %d (%s)", errno, strerror(errno)); } else { size_t const bytesRead(rc); if (bytesRead == 0) { this->eof = true; this->verifyNothingAccumulated(); } else this->processBytesRead(buffer, bytesRead); } } }