int _poll(struct pollfd *fds, unsigned int nfds, int timeout) { struct pthread *curthread = _get_curthread(); struct timespec ts; int numfds = nfds; int i, ret = 0; struct pthread_poll_data data; if (numfds > _thread_dtablesize) { numfds = _thread_dtablesize; } /* Check if a timeout was specified: */ if (timeout == INFTIM) { /* Wait for ever: */ _thread_kern_set_timeout(NULL); } else if (timeout > 0) { /* Convert the timeout in msec to a timespec: */ ts.tv_sec = timeout / 1000; ts.tv_nsec = (timeout % 1000) * 1000000; /* Set the wake up time: */ _thread_kern_set_timeout(&ts); } else if (timeout < 0) { /* a timeout less than zero but not == INFTIM is invalid */ errno = EINVAL; return (-1); } if (((ret = __sys_poll(fds, numfds, 0)) == 0) && (timeout != 0)) { data.nfds = numfds; data.fds = fds; /* * Clear revents in case of a timeout which leaves fds * unchanged: */ for (i = 0; i < numfds; i++) { fds[i].revents = 0; } curthread->data.poll_data = &data; curthread->interrupted = 0; _thread_kern_sched_state(PS_POLL_WAIT, __FILE__, __LINE__); if (curthread->interrupted) { errno = EINTR; ret = -1; } else { ret = data.nfds; } } return (ret); }
ssize_t _sendmsg(int fd, const struct msghdr *msg, int flags) { struct pthread *curthread = _get_curthread(); int ret; if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { while ((ret = __sys_sendmsg(fd, msg, flags)) < 0) { if (!(_thread_fd_getflags(fd) & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(NULL); curthread->interrupted = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); /* Check if the operation was interrupted: */ if (curthread->interrupted) { errno = EINTR; ret = -1; break; } } else { ret = -1; break; } } _FD_UNLOCK(fd, FD_WRITE); } return (ret); }
ssize_t recvfrom(int fd, void *buf, size_t len, int flags, struct sockaddr * from, socklen_t *from_len) { struct pthread *curthread = _get_curthread(); ssize_t ret; /* This is a cancellation point: */ _thread_enter_cancellation_point(); if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { while ((ret = _thread_sys_recvfrom(fd, buf, len, flags, from, from_len)) < 0) { if (!(_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) && !(flags & MSG_DONTWAIT) && ((errno == EWOULDBLOCK) || (errno == EAGAIN))) { curthread->data.fd.fd = fd; /* Set the timeout: */ _thread_kern_set_timeout(_FD_RCVTIMEO(fd)); curthread->interrupted = 0; curthread->closing_fd = 0; curthread->timeout = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; break; } else if (curthread->closing_fd) { /* Return an error status: */ errno = EBADF; ret = -1; break; } else if (curthread->timeout) { /* Return an error status: */ errno = EWOULDBLOCK; ret = -1; break; } } else { ret = -1; break; } } _FD_UNLOCK(fd, FD_READ); } /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); return (ret); }
ssize_t read(int fd, void *buf, size_t nbytes) { struct pthread *curthread = _get_curthread(); ssize_t ret; int type; /* This is a cancellation point: */ _thread_enter_cancellation_point(); /* POSIX says to do just this: */ if (nbytes == 0) ret = 0; /* Lock the file descriptor for read: */ else if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { /* Get the read/write mode type: */ type = _thread_fd_table[fd]->status_flags->flags & O_ACCMODE; /* Check if the file is not open for read: */ if (type != O_RDONLY && type != O_RDWR) { /* File is not open for read: */ errno = EBADF; ret = -1; } /* Perform a non-blocking read syscall: */ else while ((ret = _thread_sys_read(fd, buf, nbytes)) < 0) { if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ curthread->interrupted = 0; curthread->closing_fd = 0; _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* * Check if the operation was * interrupted by a signal or * a closing fd. */ if (curthread->interrupted) { errno = EINTR; ret = -1; break; } else if (curthread->closing_fd) { errno = EBADF; ret = -1; break; } } else { break; } } _FD_UNLOCK(fd, FD_READ); } /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); return (ret); }
ssize_t writev(int fd, const struct iovec * iov, int iovcnt) { struct pthread *curthread = _get_curthread(); int blocking; int idx = 0; int type; ssize_t num = 0; size_t cnt; ssize_t n; ssize_t ret; struct iovec liov[20]; struct iovec *p_iov = liov; /* This is a cancellation point: */ _thread_enter_cancellation_point(); /* Check if the array size exceeds to compiled in size: */ if (iovcnt > (int) (sizeof(liov) / sizeof(struct iovec))) { /* Allocate memory for the local array: */ if ((p_iov = (struct iovec *) malloc((size_t)iovcnt * sizeof(struct iovec))) == NULL) { /* Insufficient memory: */ errno = ENOMEM; _thread_leave_cancellation_point(); return (-1); } } else if (iovcnt <= 0) { errno = EINVAL; _thread_leave_cancellation_point(); return (-1); } /* Copy the caller's array so that it can be modified locally: */ memcpy(p_iov,iov,(size_t)iovcnt * sizeof(struct iovec)); /* Lock the file descriptor for write: */ if ((ret = _FD_LOCK(fd, FD_WRITE, NULL)) == 0) { /* Get the read/write mode type: */ type = _thread_fd_table[fd]->status_flags->flags & O_ACCMODE; /* Check if the file is not open for write: */ if (type != O_WRONLY && type != O_RDWR) { /* File is not open for write: */ errno = EBADF; _FD_UNLOCK(fd, FD_WRITE); if (p_iov != liov) free(p_iov); _thread_leave_cancellation_point(); return (-1); } /* Check if file operations are to block */ blocking = ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0); /* * Loop while no error occurs and until the expected number * of bytes are written if performing a blocking write: */ while (ret == 0) { /* Perform a non-blocking write syscall: */ n = _thread_sys_writev(fd, &p_iov[idx], iovcnt - idx); /* Check if one or more bytes were written: */ if (n > 0) { /* * Keep a count of the number of bytes * written: */ num += n; /* * Enter a loop to check if a short write * occurred and move the index to the * array entry where the short write * ended: */ cnt = (size_t)n; while (cnt > 0 && idx < iovcnt) { /* * If the residual count exceeds * the size of this vector, then * it was completely written: */ if (cnt >= p_iov[idx].iov_len) /* * Decrement the residual * count and increment the * index to the next array * entry: */ cnt -= p_iov[idx++].iov_len; else { /* * This entry was only * partially written, so * adjust it's length * and base pointer ready * for the next write: */ p_iov[idx].iov_len -= cnt; p_iov[idx].iov_base = (char *)p_iov[idx].iov_base + (ptrdiff_t)cnt; cnt = 0; } } } else if (n == 0) { /* * Avoid an infinite loop if the last iov_len is * 0. */ while (idx < iovcnt && p_iov[idx].iov_len == 0) idx++; if (idx == iovcnt) { ret = num; break; } } /* * If performing a blocking write, check if the * write would have blocked or if some bytes * were written but there are still more to * write: */ if (blocking && ((n < 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) || (n >= 0 && idx < iovcnt))) { curthread->data.fd.fd = fd; _thread_kern_set_timeout(NULL); /* Reset the interrupted operation flag: */ curthread->interrupted = 0; curthread->closing_fd = 0; _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); /* * Check if the operation was * interrupted by a signal */ if (curthread->interrupted || curthread->closing_fd) { if (num > 0) { /* Return partial success: */ ret = num; } else { /* Return an error: */ if (curthread->closing_fd) errno = EBADF; else errno = EINTR; ret = -1; } } /* * If performing a non-blocking write, * just return whatever the write syscall did: */ } else if (!blocking) { /* A non-blocking call might return zero: */ ret = n; break; /* * If there was an error, return partial success * (if any bytes were written) or else the error: */ } else if (n < 0) { if (num > 0) ret = num; else ret = n; /* Check if the write has completed: */ } else if (idx == iovcnt) /* Return the number of bytes written: */ ret = num; } _FD_UNLOCK(fd, FD_WRITE); } /* If memory was allocated for the array, free it: */ if (p_iov != liov) free(p_iov); /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); return (ret); }
int accept(int fd, struct sockaddr * name, socklen_t *namelen) { struct pthread *curthread = _get_curthread(); int ret; int newfd; enum fd_entry_mode init_mode; /* This is a cancellation point: */ _thread_enter_cancellation_point(); /* Lock the file descriptor: */ if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { /* Enter a loop to wait for a connection request: */ while ((ret = _thread_sys_accept(fd, name, namelen)) < 0) { /* Check if the socket is to block: */ if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0 && (errno == EWOULDBLOCK || errno == EAGAIN)) { /* Save the socket file descriptor: */ curthread->data.fd.fd = fd; curthread->data.fd.fname = __FILE__; curthread->data.fd.branch = __LINE__; /* Set the timeout: */ _thread_kern_set_timeout(NULL); curthread->interrupted = 0; curthread->closing_fd = 0; /* Schedule the next thread: */ _thread_kern_sched_state(PS_FDR_WAIT, __FILE__, __LINE__); /* Check if the wait was interrupted: */ if (curthread->interrupted) { /* Return an error status: */ errno = EINTR; ret = -1; break; } else if (curthread->closing_fd) { /* Return an error status: */ errno = EBADF; ret = -1; break; } } else { /* * Another error has occurred, so exit the * loop here: */ break; } } /* * If no errors initialize the file descriptor table * for the new socket. If the client's view of the * status_flags for fd is blocking, then force newfd * to be viewed as blocking too. */ if (ret != -1) { newfd = ret; if ((_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) == 0) init_mode = FD_INIT_BLOCKING; else init_mode = FD_INIT_NEW; if((ret = _thread_fd_table_init(newfd, init_mode, NULL)) != -1) ret = newfd; else { /* quitely close the fd */ _thread_sys_close(ret); } } /* Unlock the file descriptor: */ _FD_UNLOCK(fd, FD_RDWR); } /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); /* Return the socket file descriptor or -1 on error: */ return (ret); }
int connect(int fd, const struct sockaddr * name, socklen_t namelen) { struct pthread *curthread = _get_curthread(); struct sockaddr tmpname; socklen_t errnolen, tmpnamelen; int ret; /* This is a cancellation point: */ _thread_enter_cancellation_point(); if ((ret = _FD_LOCK(fd, FD_RDWR, NULL)) == 0) { if ((ret = _thread_sys_connect(fd, name, namelen)) < 0) { if (!(_thread_fd_table[fd]->status_flags->flags & O_NONBLOCK) && ((errno == EWOULDBLOCK) || (errno == EINPROGRESS) || (errno == EALREADY) || (errno == EAGAIN))) { curthread->data.fd.fd = fd; /* Reset the interrupted operation flag: */ curthread->interrupted = 0; curthread->closing_fd = 0; /* Set the timeout: */ _thread_kern_set_timeout(NULL); _thread_kern_sched_state(PS_FDW_WAIT, __FILE__, __LINE__); /* * Check if the operation was * interrupted by a signal or * a closing fd. */ if (curthread->interrupted) { errno = EINTR; ret = -1; } else if (curthread->closing_fd) { errno = EBADF; ret = -1; } else { tmpnamelen = sizeof(tmpname); /* 0 now lets see if it really worked */ if (((ret = _thread_sys_getpeername(fd, &tmpname, &tmpnamelen)) < 0) && (errno == ENOTCONN)) { /* * Get the error, this function * should not fail */ errnolen = sizeof(errno); _thread_sys_getsockopt(fd, SOL_SOCKET, SO_ERROR, &errno, &errnolen); } } } else { ret = -1; } } _FD_UNLOCK(fd, FD_RDWR); } /* No longer in a cancellation point: */ _thread_leave_cancellation_point(); return (ret); }