mirror of
https://github.com/torvalds/linux.git
synced 2026-05-30 18:13:41 +02:00
vsock/test: add send_buf() utility function
Move the code of send_byte() out in a new utility function that can be used to send a generic buffer. This new function can be used when we need to send a custom buffer and not just a single 'A' byte. Signed-off-by: Stefano Garzarella <sgarzare@redhat.com> Reviewed-by: Arseniy Krasnov <avkrasnov@salutedevices.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
parent
a0bcb83577
commit
12329bd51f
|
|
@ -211,6 +211,59 @@ int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
|
||||||
return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
|
return vsock_accept(cid, port, clientaddrp, SOCK_SEQPACKET);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Transmit bytes from a buffer and check the return value.
|
||||||
|
*
|
||||||
|
* expected_ret:
|
||||||
|
* <0 Negative errno (for testing errors)
|
||||||
|
* 0 End-of-file
|
||||||
|
* >0 Success (bytes successfully written)
|
||||||
|
*/
|
||||||
|
void send_buf(int fd, const void *buf, size_t len, int flags,
|
||||||
|
ssize_t expected_ret)
|
||||||
|
{
|
||||||
|
ssize_t nwritten = 0;
|
||||||
|
ssize_t ret;
|
||||||
|
|
||||||
|
timeout_begin(TIMEOUT);
|
||||||
|
do {
|
||||||
|
ret = send(fd, buf + nwritten, len - nwritten, flags);
|
||||||
|
timeout_check("send");
|
||||||
|
|
||||||
|
if (ret == 0 || (ret < 0 && errno != EINTR))
|
||||||
|
break;
|
||||||
|
|
||||||
|
nwritten += ret;
|
||||||
|
} while (nwritten < len);
|
||||||
|
timeout_end();
|
||||||
|
|
||||||
|
if (expected_ret < 0) {
|
||||||
|
if (ret != -1) {
|
||||||
|
fprintf(stderr, "bogus send(2) return value %zd (expected %zd)\n",
|
||||||
|
ret, expected_ret);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
if (errno != -expected_ret) {
|
||||||
|
perror("send");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ret < 0) {
|
||||||
|
perror("send");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nwritten != expected_ret) {
|
||||||
|
if (ret == 0)
|
||||||
|
fprintf(stderr, "unexpected EOF while sending bytes\n");
|
||||||
|
|
||||||
|
fprintf(stderr, "bogus send(2) bytes written %zd (expected %zd)\n",
|
||||||
|
nwritten, expected_ret);
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/* Receive bytes in a buffer and check the return value.
|
/* Receive bytes in a buffer and check the return value.
|
||||||
*
|
*
|
||||||
* expected_ret:
|
* expected_ret:
|
||||||
|
|
@ -273,43 +326,8 @@ void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret)
|
||||||
void send_byte(int fd, int expected_ret, int flags)
|
void send_byte(int fd, int expected_ret, int flags)
|
||||||
{
|
{
|
||||||
const uint8_t byte = 'A';
|
const uint8_t byte = 'A';
|
||||||
ssize_t nwritten;
|
|
||||||
|
|
||||||
timeout_begin(TIMEOUT);
|
send_buf(fd, &byte, sizeof(byte), flags, expected_ret);
|
||||||
do {
|
|
||||||
nwritten = send(fd, &byte, sizeof(byte), flags);
|
|
||||||
timeout_check("write");
|
|
||||||
} while (nwritten < 0 && errno == EINTR);
|
|
||||||
timeout_end();
|
|
||||||
|
|
||||||
if (expected_ret < 0) {
|
|
||||||
if (nwritten != -1) {
|
|
||||||
fprintf(stderr, "bogus send(2) return value %zd\n",
|
|
||||||
nwritten);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (errno != -expected_ret) {
|
|
||||||
perror("write");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (nwritten < 0) {
|
|
||||||
perror("write");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (nwritten == 0) {
|
|
||||||
if (expected_ret == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
fprintf(stderr, "unexpected EOF while sending byte\n");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
if (nwritten != sizeof(byte)) {
|
|
||||||
fprintf(stderr, "bogus send(2) return value %zd\n", nwritten);
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Receive one byte and check the return value.
|
/* Receive one byte and check the return value.
|
||||||
|
|
|
||||||
|
|
@ -42,6 +42,8 @@ int vsock_stream_accept(unsigned int cid, unsigned int port,
|
||||||
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
|
int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
|
||||||
struct sockaddr_vm *clientaddrp);
|
struct sockaddr_vm *clientaddrp);
|
||||||
void vsock_wait_remote_close(int fd);
|
void vsock_wait_remote_close(int fd);
|
||||||
|
void send_buf(int fd, const void *buf, size_t len, int flags,
|
||||||
|
ssize_t expected_ret);
|
||||||
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
|
void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
|
||||||
void send_byte(int fd, int expected_ret, int flags);
|
void send_byte(int fd, int expected_ret, int flags);
|
||||||
void recv_byte(int fd, int expected_ret, int flags);
|
void recv_byte(int fd, int expected_ret, int flags);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user