mirror of
git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2025-08-05 16:54:27 +00:00

int read_with_timeout(int fd, char *buf, size_t count, long usec) As a regular read(2), but allows to specify a timeout in micro-seconds. Returns -EAGAIN on timeout. Implemented using select(). Signed-off-by: Eduard Zingerman <eddyz87@gmail.com> Link: https://lore.kernel.org/r/20241112110906.3045278-3-eddyz87@gmail.com Signed-off-by: Alexei Starovoitov <ast@kernel.org>
21 lines
450 B
C
21 lines
450 B
C
// SPDX-License-Identifier: GPL-2.0
|
|
#include <sys/select.h>
|
|
#include <unistd.h>
|
|
#include <errno.h>
|
|
|
|
int read_with_timeout(int fd, char *buf, size_t count, long usec)
|
|
{
|
|
const long M = 1000 * 1000;
|
|
struct timeval tv = { usec / M, usec % M };
|
|
fd_set fds;
|
|
int err;
|
|
|
|
FD_ZERO(&fds);
|
|
FD_SET(fd, &fds);
|
|
err = select(fd + 1, &fds, NULL, NULL, &tv);
|
|
if (err < 0)
|
|
return err;
|
|
if (FD_ISSET(fd, &fds))
|
|
return read(fd, buf, count);
|
|
return -EAGAIN;
|
|
}
|