summaryrefslogtreecommitdiffstats
path: root/libdlmclient
diff options
context:
space:
mode:
authorDamian Hobson-Garcia <dhobsong@igel.co.jp>2020-11-24 17:16:39 +0900
committerDamian Hobson-Garcia <dhobsong@igel.co.jp>2021-02-19 10:48:23 +0000
commitf991de200799118355fd75237a740321bda7aaa7 (patch)
tree589f536d835d81f1166410e67aedb578d6e70395 /libdlmclient
parent464ec461237b36e03c4cdb175e9d87ab502ee1d5 (diff)
Add initial version
The initial version implements the basic functionality of the client/server communication and lease management. For now, one lease is created per valid connector (dependent on CRTC availablity). Bug-AGL: SPEC-3729 Signed-off-by: Damian Hobson-Garcia <dhobsong@igel.co.jp> Change-Id: I2b37a892742cc22bdc53a5172c8ad3d8a7bb5e66
Diffstat (limited to 'libdlmclient')
-rw-r--r--libdlmclient/dlmclient.c170
-rw-r--r--libdlmclient/dlmclient.h86
-rw-r--r--libdlmclient/docs/Doxyfile.in9
-rw-r--r--libdlmclient/docs/meson.build21
-rw-r--r--libdlmclient/meson.build38
-rw-r--r--libdlmclient/test/libdlmclient-test.c291
-rw-r--r--libdlmclient/test/meson.build6
-rw-r--r--libdlmclient/test/test-socket-server.c169
-rw-r--r--libdlmclient/test/test-socket-server.h33
9 files changed, 823 insertions, 0 deletions
diff --git a/libdlmclient/dlmclient.c b/libdlmclient/dlmclient.c
new file mode 100644
index 0000000..32493d3
--- /dev/null
+++ b/libdlmclient/dlmclient.c
@@ -0,0 +1,170 @@
+/* Copyright 2020-2021 IGEL Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "dlmclient.h"
+#include "log.h"
+#include "socket-path.h"
+
+#include <assert.h>
+#include <errno.h>
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+void dlm_enable_debug_log(bool enable)
+{
+ dlm_log_enable_debug(enable);
+}
+
+struct dlm_lease {
+ int dlm_server_sock;
+ int lease_fd;
+};
+
+static bool lease_connect(struct dlm_lease *lease, const char *name)
+{
+ struct sockaddr_un sa = {
+ .sun_family = AF_UNIX,
+ };
+
+ if (!sockaddr_set_lease_server_path(&sa, name))
+ return false;
+
+ int dlm_server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ if (dlm_server_sock < 0) {
+ DEBUG_LOG("Socket creation failed: %s\n", strerror(errno));
+ return false;
+ }
+
+ while (connect(dlm_server_sock, (struct sockaddr *)&sa,
+ sizeof(struct sockaddr_un)) == -1) {
+ if (errno == EINTR)
+ continue;
+ DEBUG_LOG("Cannot connect to %s: %s\n", sa.sun_path,
+ strerror(errno));
+ close(dlm_server_sock);
+ return false;
+ }
+ lease->dlm_server_sock = dlm_server_sock;
+ return true;
+}
+
+static bool lease_recv_fd(struct dlm_lease *lease)
+{
+ char ctrl_buf[CMSG_SPACE(sizeof(int))] = {0};
+ char data[1] = {0};
+
+ struct iovec iov[1];
+ iov[0].iov_base = data;
+ iov[0].iov_len = sizeof(data);
+
+ struct msghdr msg = {
+ .msg_control = ctrl_buf,
+ .msg_controllen = CMSG_SPACE(sizeof(int)),
+ .msg_iov = iov,
+ .msg_iovlen = 1,
+ };
+
+ int ret;
+ while ((ret = recvmsg(lease->dlm_server_sock, &msg, 0)) <= 0) {
+ if (ret == 0) {
+ errno = EACCES;
+ DEBUG_LOG("Request rejected by DRM lease manager\n");
+ // TODO: Report why the request was rejected.
+ return false;
+ }
+ if (errno != EINTR) {
+ DEBUG_LOG("Socket data receive error: %s\n",
+ strerror(errno));
+ return false;
+ }
+ }
+
+ lease->lease_fd = -1;
+ struct cmsghdr *cmsg;
+ for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
+ cmsg = CMSG_NXTHDR(&msg, cmsg)) {
+ if (cmsg->cmsg_level == SOL_SOCKET &&
+ cmsg->cmsg_type == SCM_RIGHTS) {
+ int nfds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
+ int *fds = (int *)CMSG_DATA(cmsg);
+
+ if (nfds == 1) {
+ lease->lease_fd = fds[0];
+ break;
+ }
+
+ DEBUG_LOG(
+ "Expected 1 fd from lease manager. Received %d\n",
+ nfds);
+ /* Close any unexpected fds so we don't leak them. */
+ for (int i = 0; i < nfds; i++)
+ close(fds[i]);
+ break;
+ }
+ }
+
+ if (lease->lease_fd < 0) {
+ DEBUG_LOG("Expected data not received from lease manager\n");
+ errno = EPROTO;
+ return false;
+ }
+
+ return true;
+}
+
+struct dlm_lease *dlm_get_lease(const char *name)
+{
+ struct dlm_lease *lease = calloc(1, sizeof(struct dlm_lease));
+ if (!lease) {
+ DEBUG_LOG("can't allocate memory : %s\n", strerror(errno));
+ return NULL;
+ }
+
+ if (!lease_connect(lease, name)) {
+ free(lease);
+ return NULL;
+ }
+
+ if (!lease_recv_fd(lease)) {
+ close(lease->dlm_server_sock);
+ free(lease);
+ return NULL;
+ }
+
+ return lease;
+}
+
+void dlm_release_lease(struct dlm_lease *lease)
+{
+ if (!lease)
+ return;
+
+ close(lease->lease_fd);
+ close(lease->dlm_server_sock);
+ free(lease);
+}
+
+int dlm_lease_fd(struct dlm_lease *lease)
+{
+ if (!lease)
+ return -1;
+
+ return lease->lease_fd;
+}
diff --git a/libdlmclient/dlmclient.h b/libdlmclient/dlmclient.h
new file mode 100644
index 0000000..908cd6b
--- /dev/null
+++ b/libdlmclient/dlmclient.h
@@ -0,0 +1,86 @@
+/* Copyright 2020-2021 IGEL Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @file dlmclient.h
+ */
+#ifndef DLM_CLIENT_H
+#define DLM_CLIENT_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <stdbool.h>
+
+/**
+ * @brief Enable debug logging
+ *
+ * @param[in] enable enable/disable debug logging
+ */
+void dlm_enable_debug_log(bool enable);
+
+/**
+ * @brief lease handle
+ */
+struct dlm_lease;
+
+/**
+ * @brief Get a DRM lease from the lease manager
+ *
+ * @param[in] name requested lease
+ * @return A pointer to a lease handle on success.
+ * On error this function returns NULL and errno is set accordingly.
+ *
+ * Possible errors:
+ *
+ * errno | Meaning
+ * -------------|-------------------------------------------------------------
+ * EACCESS | Cannot access lease manager socket directory
+ * EACCESS | Lease request denied by lease manager
+ * ENAMETOOLONG | The path to the lease manager socket directory is too long
+ * ENOENT | Lease manager or requested lease not available
+ * ENOMEM | Out of memory during operation
+ * EPROTO | Protocol error in communication with lease manager
+ *
+ * This list is not exhaustive, and errno may be set to other error codes,
+ * especially those related to socket communication.
+ */
+struct dlm_lease *dlm_get_lease(const char *name);
+
+/**
+ * @brief Release a lease handle
+ *
+ * @details Release a lease handle. The lease handle will be invalidated and
+ * the associated DRM lease wil be revoked. Any fd's retrieved from
+ * dlm_lease_fd() will be closed.
+ * @param[in] lease pointer to lease handle
+ */
+void dlm_release_lease(struct dlm_lease *lease);
+
+/**
+ * @brief Get a DRM Master fd from a valid lease handle
+ *
+ * @param[in] lease pointer to a lease handle
+ * @return A DRM Master file descriptor for the lease on success.
+ * -1 is returned when called with a NULL lease handle.
+ */
+int dlm_lease_fd(struct dlm_lease *lease);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/libdlmclient/docs/Doxyfile.in b/libdlmclient/docs/Doxyfile.in
new file mode 100644
index 0000000..dce4244
--- /dev/null
+++ b/libdlmclient/docs/Doxyfile.in
@@ -0,0 +1,9 @@
+# General information
+PROJECT_NAME = "DRM Lease Manager Client Library"
+OUTPUT_DIRECTORY = @DOXYGEN_OUTPUT@
+INPUT = @README@ @CLIENT_HEADER_DIR@
+USE_MDFILE_AS_MAINPAGE = @README@
+FILE_PATTERNS = *.h
+GENERATE_LATEX = NO
+OPTIMIZE_OUTPUT_FOR_C = YES
+STRIP_FROM_PATH = @CLIENT_HEADER_DIR@
diff --git a/libdlmclient/docs/meson.build b/libdlmclient/docs/meson.build
new file mode 100644
index 0000000..563e5fb
--- /dev/null
+++ b/libdlmclient/docs/meson.build
@@ -0,0 +1,21 @@
+doxygen = find_program('doxygen', required : false)
+
+readme = join_paths(meson.source_root(), 'README.md')
+
+if get_option('enable-docs') and doxygen.found()
+ conf_data = configuration_data()
+ conf_data.set('README', readme)
+ conf_data.set('CLIENT_HEADER_DIR', dlmclient_header_dir)
+ conf_data.set('DOXYGEN_OUTPUT', meson.current_build_dir())
+ doxyfile = configure_file(
+ input: 'Doxyfile.in',
+ output: 'Doxyfile',
+ configuration: conf_data
+ )
+ custom_target('docs',
+ input: [doxyfile, readme, dlmclient_headers],
+ build_by_default: true,
+ command: [doxygen, '@INPUT0@'],
+ output: ['html']
+ )
+endif
diff --git a/libdlmclient/meson.build b/libdlmclient/meson.build
new file mode 100644
index 0000000..d63a842
--- /dev/null
+++ b/libdlmclient/meson.build
@@ -0,0 +1,38 @@
+dlmclient_sources = files(
+ 'dlmclient.c'
+)
+
+dlmclient_headers = files(
+ 'dlmclient.h'
+)
+
+libdlmclient = library(
+ 'dlmclient',
+ sources: dlmclient_sources,
+ version: meson.project_version(),
+ dependencies: [dlmcommon_dep],
+ install: true,
+)
+
+dlmclient_dep = declare_dependency(
+ link_with: libdlmclient,
+ include_directories: include_directories('.')
+)
+
+
+install_headers(dlmclient_headers, subdir: 'libdlmclient')
+
+pkg.generate(
+ name: 'libdlmclient',
+ libraries: libdlmclient,
+ subdirs: [ 'libdlmclient' ],
+ version: meson.project_version(),
+ description: 'DRM lease manager client library',
+)
+
+dlmclient_header_dir = meson.current_source_dir()
+subdir('docs')
+
+if enable_tests
+ subdir('test')
+endif
diff --git a/libdlmclient/test/libdlmclient-test.c b/libdlmclient/test/libdlmclient-test.c
new file mode 100644
index 0000000..5b04c09
--- /dev/null
+++ b/libdlmclient/test/libdlmclient-test.c
@@ -0,0 +1,291 @@
+/* Copyright 2020-2021 IGEL Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <check.h>
+
+#include <dirent.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "config.h"
+#include "dlmclient.h"
+#include "test-helpers.h"
+#include "test-socket-server.h"
+
+#define SOCKETDIR "/tmp"
+
+#define TEST_LEASE_NAME "test-lease"
+
+/************** Test fixutre functions *************/
+struct test_config default_test_config;
+
+static void test_setup(void)
+{
+ dlm_enable_debug_log(true);
+ setenv("DLM_RUNTIME_PATH", SOCKETDIR, 1);
+
+ default_test_config = (struct test_config){
+ .lease_name = TEST_LEASE_NAME,
+ .nfds = 1,
+ };
+}
+
+static void test_shutdown(void)
+{
+ test_config_cleanup(&default_test_config);
+}
+
+/************** Lease manager error tests *************/
+
+/* These tests verify that the client library gracefully handles
+ * failures when trying to receive data from the lease manager.
+ * Failures or errors in the lease manager should cause meaningful
+ * errors to be reported by the client. Lease manager errors should
+ * not cause crashes or invalid state in the client */
+
+/* manager_connection_err
+ *
+ * Test details: Simulate socket connection failure.
+ * Expected results: dlm_get_lease() fails.
+ */
+START_TEST(manager_connection_err)
+{
+ struct server_state *sstate = test_server_start(&default_test_config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME "-bad");
+
+ ck_assert_ptr_eq(lease, NULL);
+
+ test_server_stop(sstate);
+}
+END_TEST
+
+/* no_data_from_manager
+ *
+ * Test details: Close the remote (lease manager) without sending any data.
+ * Currently this means that the lease request has been rejected
+ * for some reason.
+ *
+ * TODO: Update this when the client-server protocol is updated to
+ * include the reason for the lease rejection.
+ *
+ * Expected results: dlm_get_lease() fails, errno set to EACCESS.
+ */
+START_TEST(no_data_from_manager)
+{
+
+ struct test_config config = {
+ .lease_name = TEST_LEASE_NAME,
+ .send_no_data = true,
+ };
+
+ struct server_state *sstate = test_server_start(&config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+
+ ck_assert_ptr_eq(lease, NULL);
+ ck_assert_int_eq(errno, EACCES);
+
+ test_server_stop(sstate);
+}
+END_TEST
+
+/* no_lease_fd_from_manager
+ *
+ * Test details: Simulate receiving response from lease manager with
+ * no fd attached. (i.e. a protocol error)
+ *
+ * Expected results: dlm_get_lease() fails, errno set to EPROTO.
+ */
+START_TEST(no_lease_fd_from_manager)
+{
+ /* Receive message from the lease manager with missing lease fd */
+ struct test_config config = {
+ .lease_name = TEST_LEASE_NAME,
+ .send_data_without_fd = true,
+ };
+
+ struct server_state *sstate = test_server_start(&config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+
+ ck_assert_ptr_eq(lease, NULL);
+ ck_assert_int_eq(errno, EPROTO);
+
+ test_server_stop(sstate);
+}
+END_TEST
+
+static void add_lease_manager_error_tests(Suite *s)
+{
+ TCase *tc = tcase_create("Lease manager error handling");
+
+ tcase_add_checked_fixture(tc, test_setup, test_shutdown);
+
+ tcase_add_test(tc, manager_connection_err);
+ tcase_add_test(tc, no_data_from_manager);
+ tcase_add_test(tc, no_lease_fd_from_manager);
+
+ suite_add_tcase(s, tc);
+}
+
+/************** Lease handling tests *****************/
+
+/* These tests verify that the client library handles the received
+ * lease data properly. Receiving the lease fds without leaks,
+ * properly passing the fds to the client application and cleaning
+ * them up on release.
+ */
+
+static int count_open_fds(void)
+{
+ int fds = 0;
+ DIR *dirp = opendir("/proc/self/fd");
+ while ((readdir(dirp) != NULL))
+ fds++;
+ closedir(dirp);
+ return fds;
+}
+
+/* receive_fd_from_manager
+ *
+ * Test details: Successfully receive a file descriptor.
+ * Expected results: dlm_get_lease() succeeds.
+ * dlm_lease_fd() returns the correct fd value.
+ */
+START_TEST(receive_fd_from_manager)
+{
+ struct server_state *sstate = test_server_start(&default_test_config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+ ck_assert_ptr_ne(lease, NULL);
+
+ int received_fd = dlm_lease_fd(lease);
+
+ int sent_fd = default_test_config.fds[0];
+
+ check_fd_equality(received_fd, sent_fd);
+
+ dlm_release_lease(lease);
+
+ test_server_stop(sstate);
+ close(sent_fd);
+}
+END_TEST
+
+/* lease_fd_is_closed_on_release
+ *
+ * Test details: Verify that dlm_release_lease() closes the lease fd.
+ * Expected results: lease fd is closed.
+ */
+START_TEST(lease_fd_is_closed_on_release)
+{
+ struct server_state *sstate = test_server_start(&default_test_config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+ ck_assert_ptr_ne(lease, NULL);
+
+ int received_fd = dlm_lease_fd(lease);
+
+ check_fd_is_open(received_fd);
+ dlm_release_lease(lease);
+ check_fd_is_closed(received_fd);
+
+ test_server_stop(sstate);
+}
+END_TEST
+
+/* dlm_lease_fd_always_returns_same_lease
+ *
+ * Test details: Verify that dlm_lease_fd() always returns the same value
+ * for a given lease.
+ * Expected results: same value is returned when called multiple times.
+ */
+START_TEST(dlm_lease_fd_always_returns_same_lease)
+{
+ struct server_state *sstate = test_server_start(&default_test_config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+ ck_assert_ptr_ne(lease, NULL);
+
+ int received_fd = dlm_lease_fd(lease);
+
+ ck_assert_int_eq(received_fd, dlm_lease_fd(lease));
+ ck_assert_int_eq(received_fd, dlm_lease_fd(lease));
+
+ dlm_release_lease(lease);
+
+ test_server_stop(sstate);
+}
+END_TEST
+
+START_TEST(verify_that_unused_fds_are_not_leaked)
+{
+ int nopen_fds = count_open_fds();
+
+ struct test_config config = {
+ .lease_name = TEST_LEASE_NAME,
+ .nfds = 2,
+ };
+
+ struct server_state *sstate = test_server_start(&config);
+
+ struct dlm_lease *lease = dlm_get_lease(TEST_LEASE_NAME);
+
+ ck_assert_ptr_eq(lease, NULL);
+ ck_assert_int_eq(errno, EPROTO);
+
+ dlm_release_lease(lease);
+
+ test_server_stop(sstate);
+
+ test_config_cleanup(&config);
+ ck_assert_int_eq(nopen_fds, count_open_fds());
+}
+END_TEST
+
+static void add_lease_handling_tests(Suite *s)
+{
+ TCase *tc = tcase_create("Lease processing tests");
+
+ tcase_add_checked_fixture(tc, test_setup, test_shutdown);
+
+ tcase_add_test(tc, receive_fd_from_manager);
+ tcase_add_test(tc, lease_fd_is_closed_on_release);
+ tcase_add_test(tc, dlm_lease_fd_always_returns_same_lease);
+ tcase_add_test(tc, verify_that_unused_fds_are_not_leaked);
+ suite_add_tcase(s, tc);
+}
+
+int main(void)
+{
+ int number_failed;
+ Suite *s;
+ SRunner *sr;
+
+ s = suite_create("DLM client library tests");
+
+ add_lease_manager_error_tests(s);
+ add_lease_handling_tests(s);
+
+ sr = srunner_create(s);
+
+ srunner_run_all(sr, CK_NORMAL);
+ number_failed = srunner_ntests_failed(sr);
+ srunner_free(sr);
+ return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
diff --git a/libdlmclient/test/meson.build b/libdlmclient/test/meson.build
new file mode 100644
index 0000000..35e8575
--- /dev/null
+++ b/libdlmclient/test/meson.build
@@ -0,0 +1,6 @@
+cl_test = executable('libdlmclient-test',
+ sources: [ 'libdlmclient-test.c', 'test-socket-server.c'],
+ dependencies: [check_dep, fff_dep, dlmcommon_dep, dlmclient_dep, thread_dep],
+ include_directories: configuration_inc)
+
+test('Client library test', cl_test, is_parallel: false)
diff --git a/libdlmclient/test/test-socket-server.c b/libdlmclient/test/test-socket-server.c
new file mode 100644
index 0000000..281aaf7
--- /dev/null
+++ b/libdlmclient/test/test-socket-server.c
@@ -0,0 +1,169 @@
+/* Copyright 2020-2021 IGEL Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "test-socket-server.h"
+#include <check.h>
+
+#include <pthread.h>
+#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <unistd.h>
+
+#include "socket-path.h"
+#include "test-helpers.h"
+
+static void send_fd_list_over_socket(int socket, int nfds, int *fds)
+{
+ char data;
+ struct iovec iov = {
+ .iov_base = &data,
+ .iov_len = 1,
+ };
+
+ int bufsize = CMSG_SPACE(nfds * sizeof(int));
+ char *buf = malloc(bufsize);
+
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ .msg_controllen = bufsize,
+ .msg_control = buf,
+ };
+
+ struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
+ cmsg->cmsg_level = SOL_SOCKET;
+ cmsg->cmsg_type = SCM_RIGHTS;
+ cmsg->cmsg_len = CMSG_LEN(nfds * sizeof(int));
+ memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * nfds);
+
+ ck_assert_int_gt(sendmsg(socket, &msg, 0), 0);
+ free(buf);
+}
+
+struct server_state {
+ pthread_t tid;
+ pthread_mutex_t lock;
+ pthread_cond_t cond;
+ bool is_server_started;
+
+ struct test_config *config;
+};
+
+static void *test_server_thread(void *arg)
+{
+ struct server_state *sstate = arg;
+ struct test_config *config = sstate->config;
+
+ struct sockaddr_un address = {
+ .sun_family = AF_UNIX,
+ };
+
+ ck_assert_int_eq(
+ sockaddr_set_lease_server_path(&address, config->lease_name), true);
+
+ int server = socket(PF_UNIX, SOCK_STREAM, 0);
+ ck_assert_int_ge(server, 0);
+
+ unlink(address.sun_path);
+
+ int ret;
+ ret = bind(server, (struct sockaddr *)&address, sizeof(address));
+ ck_assert_int_eq(ret, 0);
+
+ ret = listen(server, 1);
+ ck_assert_int_eq(ret, 0);
+
+ sstate->is_server_started = true;
+ pthread_cond_signal(&sstate->cond);
+
+ int client = accept(server, NULL, NULL);
+ /* accept is the cancellation point for this thread. If
+ * pthread_cancel() is called on this thread, accept() may return
+ * -1, so don't assert on it. */
+
+ if (client < 0) {
+ close(server);
+ return NULL;
+ }
+
+ if (config->send_no_data)
+ goto done;
+
+ if (config->send_data_without_fd) {
+ char data;
+ write(client, &data, 1);
+ goto done;
+ }
+
+ if (config->nfds == 0)
+ config->nfds = 1;
+
+ config->fds = calloc(config->nfds, sizeof(int));
+
+ for (int i = 0; i < config->nfds; i++)
+ config->fds[i] = get_dummy_fd();
+
+ send_fd_list_over_socket(client, config->nfds, config->fds);
+done:
+ close(client);
+ close(server);
+ return NULL;
+}
+
+struct server_state *test_server_start(struct test_config *test_config)
+{
+ struct server_state *sstate = malloc(sizeof(*sstate));
+
+ *sstate = (struct server_state){
+ .lock = PTHREAD_MUTEX_INITIALIZER,
+ .cond = PTHREAD_COND_INITIALIZER,
+ .is_server_started = false,
+ .config = test_config,
+ };
+
+ pthread_create(&sstate->tid, NULL, test_server_thread, sstate);
+
+ pthread_mutex_lock(&sstate->lock);
+ while (!sstate->is_server_started)
+ pthread_cond_wait(&sstate->cond, &sstate->lock);
+ pthread_mutex_unlock(&sstate->lock);
+ return sstate;
+}
+
+void test_server_stop(struct server_state *sstate)
+{
+
+ ck_assert_ptr_ne(sstate, NULL);
+
+ pthread_cancel(sstate->tid);
+ pthread_join(sstate->tid, NULL);
+
+ free(sstate);
+}
+
+void test_config_cleanup(struct test_config *config)
+{
+ if (!config->fds)
+ return;
+
+ for (int i = 0; i < config->nfds; i++)
+ close(config->fds[i]);
+
+ free(config->fds);
+}
diff --git a/libdlmclient/test/test-socket-server.h b/libdlmclient/test/test-socket-server.h
new file mode 100644
index 0000000..ebdbd2d
--- /dev/null
+++ b/libdlmclient/test/test-socket-server.h
@@ -0,0 +1,33 @@
+/* Copyright 2020-2021 IGEL Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef TEST_SOCKET_SERVER_H
+#define TEST_SOCKET_SERVER_H
+#include <stdbool.h>
+
+struct test_config {
+ char *lease_name;
+ int nfds;
+ int *fds;
+
+ bool send_data_without_fd;
+ bool send_no_data;
+};
+
+void test_config_cleanup(struct test_config *config);
+
+struct server_state *test_server_start(struct test_config *test_config);
+void test_server_stop(struct server_state *sstate);
+#endif