#include <errno.h>
#include <assert.h>
+#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/uio.h>
return (i != l);
}
+static int
+read_tmo(int fd, void *ptr, unsigned len, double tmo)
+{
+ int i;
+ struct pollfd pfd[1];
+
+ pfd->fd = fd;
+ pfd->events = POLLIN;
+ i = poll(pfd, 1, (int)(tmo * 1e3));
+ if (i == 0) {
+ errno = ETIMEDOUT;
+ return (-1);
+ }
+ return (read(fd, ptr, len));
+}
+
int
-cli_readres(int fd, unsigned *status, char **ptr)
+cli_readres(int fd, unsigned *status, char **ptr, double tmo)
{
- char res[CLI_LINE0_LEN + 1]; /* For NUL */
+ char res[CLI_LINE0_LEN]; /* For NUL */
int i, j;
unsigned u, v;
char *p;
- i = read(fd, res, CLI_LINE0_LEN);
+ i = read_tmo(fd, res, CLI_LINE0_LEN, tmo);
if (i < 0)
return (i);
assert(i == CLI_LINE0_LEN); /* XXX: handle */
*status = u;
p = malloc(v + 1);
assert(p != NULL);
- i = read(fd, p, v + 1);
+ i = read_tmo(fd, p, v + 1, tmo);
if (i < 0) {
free(p);
return (i);
};
int cli_writeres(int fd, struct cli *cli);
-int cli_readres(int fd, unsigned *status, char **ptr);
+int cli_readres(int fd, unsigned *status, char **ptr, double tmo);
extern struct cli_proto CLI_cmds[];
cli_func_t cli_func_ping;
assert(i == v);
free(p);
- i = cli_readres(cli_i, &u, &p);
+ i = cli_readres(cli_i, &u, &p, 3.0);
assert(i == 0);
cli_result(cli, u);
cli_out(cli, "%s", p);
assert(i == strlen(p));
free(p);
- i = cli_readres(cli_i, &j, resp);
+ i = cli_readres(cli_i, &j, resp, 3.0);
+ assert(i == 0);
if (status != NULL)
*status = j;
return (j == CLIS_OK ? 0 : j);