1 /* Copyright 2020-2021 IGEL Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "test-socket-client.h"
27 #include <sys/socket.h>
31 #include "dlm-protocol.h"
32 #include "socket-path.h"
34 #define DEFAULT_RECV_TIMEOUT (100) // timeout in ms to receive data from server
40 struct test_config *config;
43 static void send_lease_request(int socket, enum dlm_opcode opcode)
45 struct dlm_client_request req = {
48 send_dlm_client_request(socket, &req);
51 static void client_gst_socket_status(int socket_fd, struct test_config *config)
54 config->connection_completed = true;
56 struct pollfd pfd = {.fd = socket_fd, .events = POLLIN};
57 if (poll(&pfd, 1, config->recv_timeout) <= 0)
60 if (pfd.revents & POLLHUP)
61 config->connection_completed = false;
63 if (pfd.revents & POLLIN)
64 config->has_data = true;
69 static int receive_fd_from_socket(int sockfd)
73 char buf[CMSG_SPACE(sizeof(int))];
77 struct iovec iov = {.iov_base = &data, .iov_len = sizeof(data)};
82 .msg_controllen = sizeof(u.buf),
85 if (recvmsg(sockfd, &msg, 0) < 0)
89 for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
90 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
91 ck_assert_int_eq(cmsg->cmsg_level, SOL_SOCKET);
93 if (cmsg->cmsg_type != SCM_RIGHTS)
96 int nfds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
97 ck_assert_int_eq(nfds, 1);
98 recv_fd = *(int *)CMSG_DATA(cmsg);
103 static void *test_client_thread(void *arg)
105 struct client_state *cstate = arg;
106 struct test_config *config = cstate->config;
108 struct sockaddr_un address = {
109 .sun_family = AF_UNIX,
113 sockaddr_set_lease_server_path(&address, config->lease->name),
116 int client = socket(PF_UNIX, SOCK_SEQPACKET, 0);
117 ck_assert_int_ge(client, 0);
120 ret = connect(client, (struct sockaddr *)&address, sizeof(address));
122 printf("Connect failed;: %s\n", strerror(errno));
127 send_lease_request(client, DLM_GET_LEASE);
129 if (!config->recv_timeout)
130 config->recv_timeout = DEFAULT_RECV_TIMEOUT;
132 client_gst_socket_status(client, config);
134 if (config->has_data) {
135 config->received_fd = receive_fd_from_socket(client);
138 cstate->socket_fd = client;
139 send_lease_request(client, DLM_RELEASE_LEASE);
144 struct client_state *test_client_start(struct test_config *test_config)
146 struct client_state *cstate = malloc(sizeof(*cstate));
148 *cstate = (struct client_state){
149 .config = test_config,
152 pthread_create(&cstate->tid, NULL, test_client_thread, cstate);
157 void test_client_stop(struct client_state *cstate)
160 ck_assert_ptr_ne(cstate, NULL);
162 pthread_join(cstate->tid, NULL);
164 if (cstate->socket_fd >= 0)
165 close(cstate->socket_fd);
170 void test_config_cleanup(struct test_config *config)
172 if (config->has_data && config->received_fd >= 0)
173 close(config->received_fd);