Skip to content

Commit ac12b7e

Browse files
mmhalkuba-moo
authored andcommitted
vsock/test: Introduce vsock_connect_fd()
Distill timeout-guarded vsock_connect_fd(). Adapt callers. Suggested-by: Stefano Garzarella <[email protected]> Reviewed-by: Stefano Garzarella <[email protected]> Signed-off-by: Michal Luczaj <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 852a00c commit ac12b7e

File tree

2 files changed

+18
-28
lines changed

2 files changed

+18
-28
lines changed

tools/testing/vsock/util.c

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -120,27 +120,33 @@ int vsock_bind(unsigned int cid, unsigned int port, int type)
120120
return fd;
121121
}
122122

123-
/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
124-
int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
123+
int vsock_connect_fd(int fd, unsigned int cid, unsigned int port)
125124
{
126-
struct sockaddr_vm sa_server = {
125+
struct sockaddr_vm sa = {
127126
.svm_family = AF_VSOCK,
128127
.svm_cid = cid,
129128
.svm_port = port,
130129
};
131-
132-
int client_fd, ret;
133-
134-
client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
130+
int ret;
135131

136132
timeout_begin(TIMEOUT);
137133
do {
138-
ret = connect(client_fd, (struct sockaddr *)&sa_server, sizeof(sa_server));
134+
ret = connect(fd, (struct sockaddr *)&sa, sizeof(sa));
139135
timeout_check("connect");
140136
} while (ret < 0 && errno == EINTR);
141137
timeout_end();
142138

143-
if (ret < 0) {
139+
return ret;
140+
}
141+
142+
/* Bind to <bind_port>, connect to <cid, port> and return the file descriptor. */
143+
int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_port, int type)
144+
{
145+
int client_fd;
146+
147+
client_fd = vsock_bind(VMADDR_CID_ANY, bind_port, type);
148+
149+
if (vsock_connect_fd(client_fd, cid, port)) {
144150
perror("connect");
145151
exit(EXIT_FAILURE);
146152
}
@@ -151,17 +157,6 @@ int vsock_bind_connect(unsigned int cid, unsigned int port, unsigned int bind_po
151157
/* Connect to <cid, port> and return the file descriptor. */
152158
int vsock_connect(unsigned int cid, unsigned int port, int type)
153159
{
154-
union {
155-
struct sockaddr sa;
156-
struct sockaddr_vm svm;
157-
} addr = {
158-
.svm = {
159-
.svm_family = AF_VSOCK,
160-
.svm_port = port,
161-
.svm_cid = cid,
162-
},
163-
};
164-
int ret;
165160
int fd;
166161

167162
control_expectln("LISTENING");
@@ -172,20 +167,14 @@ int vsock_connect(unsigned int cid, unsigned int port, int type)
172167
exit(EXIT_FAILURE);
173168
}
174169

175-
timeout_begin(TIMEOUT);
176-
do {
177-
ret = connect(fd, &addr.sa, sizeof(addr.svm));
178-
timeout_check("connect");
179-
} while (ret < 0 && errno == EINTR);
180-
timeout_end();
181-
182-
if (ret < 0) {
170+
if (vsock_connect_fd(fd, cid, port)) {
183171
int old_errno = errno;
184172

185173
close(fd);
186174
fd = -1;
187175
errno = old_errno;
188176
}
177+
189178
return fd;
190179
}
191180

tools/testing/vsock/util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct test_case {
3939
void init_signals(void);
4040
unsigned int parse_cid(const char *str);
4141
unsigned int parse_port(const char *str);
42+
int vsock_connect_fd(int fd, unsigned int cid, unsigned int port);
4243
int vsock_connect(unsigned int cid, unsigned int port, int type);
4344
int vsock_accept(unsigned int cid, unsigned int port,
4445
struct sockaddr_vm *clientaddrp, int type);

0 commit comments

Comments
 (0)