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-server.h"
24 #include <sys/socket.h>
28 #include "dlm-protocol.h"
29 #include "socket-path.h"
30 #include "test-helpers.h"
32 static void send_fd_list_over_socket(int socket, int nfds, int *fds)
40 int bufsize = CMSG_SPACE(nfds * sizeof(int));
41 char *buf = malloc(bufsize);
46 .msg_controllen = bufsize,
50 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
51 cmsg->cmsg_level = SOL_SOCKET;
52 cmsg->cmsg_type = SCM_RIGHTS;
53 cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int));
54 memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * nfds);
56 ck_assert_int_gt(sendmsg(socket, &msg, 0), 0);
60 static void expect_client_command(int socket, enum dlm_opcode opcode)
62 struct dlm_client_request req;
63 ck_assert_int_eq(receive_dlm_client_request(socket, &req), true);
64 ck_assert_int_eq(req.opcode, opcode);
71 bool is_server_started;
73 struct test_config *config;
76 static void *test_server_thread(void *arg)
78 struct server_state *sstate = arg;
79 struct test_config *config = sstate->config;
81 struct sockaddr_un address = {
82 .sun_family = AF_UNIX,
86 sockaddr_set_lease_server_path(&address, config->lease_name), true);
88 int server = socket(PF_UNIX, SOCK_SEQPACKET, 0);
89 ck_assert_int_ge(server, 0);
91 unlink(address.sun_path);
94 ret = bind(server, (struct sockaddr *)&address, sizeof(address));
95 ck_assert_int_eq(ret, 0);
97 ret = listen(server, 1);
98 ck_assert_int_eq(ret, 0);
100 sstate->is_server_started = true;
101 pthread_cond_signal(&sstate->cond);
103 int client = accept(server, NULL, NULL);
104 /* accept is the cancellation point for this thread. If
105 * pthread_cancel() is called on this thread, accept() may return
106 * -1, so don't assert on it. */
113 expect_client_command(client, DLM_GET_LEASE);
115 if (config->send_no_data)
118 if (config->send_data_without_fd) {
120 write(client, &data, 1);
124 if (config->nfds == 0)
127 config->fds = calloc(config->nfds, sizeof(int));
129 for (int i = 0; i < config->nfds; i++)
130 config->fds[i] = get_dummy_fd();
132 send_fd_list_over_socket(client, config->nfds, config->fds);
133 expect_client_command(client, DLM_RELEASE_LEASE);
140 struct server_state *test_server_start(struct test_config *test_config)
142 struct server_state *sstate = malloc(sizeof(*sstate));
144 *sstate = (struct server_state){
145 .lock = PTHREAD_MUTEX_INITIALIZER,
146 .cond = PTHREAD_COND_INITIALIZER,
147 .is_server_started = false,
148 .config = test_config,
151 pthread_create(&sstate->tid, NULL, test_server_thread, sstate);
153 pthread_mutex_lock(&sstate->lock);
154 while (!sstate->is_server_started)
155 pthread_cond_wait(&sstate->cond, &sstate->lock);
156 pthread_mutex_unlock(&sstate->lock);
160 void test_server_stop(struct server_state *sstate)
163 ck_assert_ptr_ne(sstate, NULL);
165 pthread_cancel(sstate->tid);
166 pthread_join(sstate->tid, NULL);
171 void test_config_cleanup(struct test_config *config)
176 for (int i = 0; i < config->nfds; i++)
177 close(config->fds[i]);