aboutsummaryrefslogtreecommitdiffstats
path: root/libdlmclient
diff options
context:
space:
mode:
Diffstat (limited to 'libdlmclient')
-rw-r--r--libdlmclient/dlmclient.c35
-rw-r--r--libdlmclient/test/test-socket-server.c13
2 files changed, 41 insertions, 7 deletions
diff --git a/libdlmclient/dlmclient.c b/libdlmclient/dlmclient.c
index 32493d3..03c08a8 100644
--- a/libdlmclient/dlmclient.c
+++ b/libdlmclient/dlmclient.c
@@ -14,6 +14,8 @@
*/
#include "dlmclient.h"
+
+#include "dlm-protocol.h"
#include "log.h"
#include "socket-path.h"
@@ -46,7 +48,7 @@ static bool lease_connect(struct dlm_lease *lease, const char *name)
if (!sockaddr_set_lease_server_path(&sa, name))
return false;
- int dlm_server_sock = socket(AF_UNIX, SOCK_STREAM, 0);
+ int dlm_server_sock = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (dlm_server_sock < 0) {
DEBUG_LOG("Socket creation failed: %s\n", strerror(errno));
return false;
@@ -65,6 +67,19 @@ static bool lease_connect(struct dlm_lease *lease, const char *name)
return true;
}
+static bool lease_send_request(struct dlm_lease *lease, enum dlm_opcode opcode)
+{
+ struct dlm_client_request request = {
+ .opcode = opcode,
+ };
+
+ if (!send_dlm_client_request(lease->dlm_server_sock, &request)) {
+ DEBUG_LOG("Socket data send error: %s\n", strerror(errno));
+ return false;
+ }
+ return true;
+}
+
static bool lease_recv_fd(struct dlm_lease *lease)
{
char ctrl_buf[CMSG_SPACE(sizeof(int))] = {0};
@@ -131,6 +146,7 @@ static bool lease_recv_fd(struct dlm_lease *lease)
struct dlm_lease *dlm_get_lease(const char *name)
{
+ int saved_errno;
struct dlm_lease *lease = calloc(1, sizeof(struct dlm_lease));
if (!lease) {
DEBUG_LOG("can't allocate memory : %s\n", strerror(errno));
@@ -142,13 +158,19 @@ struct dlm_lease *dlm_get_lease(const char *name)
return NULL;
}
- if (!lease_recv_fd(lease)) {
- close(lease->dlm_server_sock);
- free(lease);
- return NULL;
- }
+ if (!lease_send_request(lease, DLM_GET_LEASE))
+ goto err;
+
+ if (!lease_recv_fd(lease))
+ goto err;
return lease;
+
+err:
+ saved_errno = errno;
+ dlm_release_lease(lease);
+ errno = saved_errno;
+ return NULL;
}
void dlm_release_lease(struct dlm_lease *lease)
@@ -156,6 +178,7 @@ void dlm_release_lease(struct dlm_lease *lease)
if (!lease)
return;
+ lease_send_request(lease, DLM_RELEASE_LEASE);
close(lease->lease_fd);
close(lease->dlm_server_sock);
free(lease);
diff --git a/libdlmclient/test/test-socket-server.c b/libdlmclient/test/test-socket-server.c
index 281aaf7..6aaa4e4 100644
--- a/libdlmclient/test/test-socket-server.c
+++ b/libdlmclient/test/test-socket-server.c
@@ -25,6 +25,7 @@
#include <sys/un.h>
#include <unistd.h>
+#include "dlm-protocol.h"
#include "socket-path.h"
#include "test-helpers.h"
@@ -56,6 +57,13 @@ static void send_fd_list_over_socket(int socket, int nfds, int *fds)
free(buf);
}
+static void expect_client_command(int socket, enum dlm_opcode opcode)
+{
+ struct dlm_client_request req;
+ ck_assert_int_eq(receive_dlm_client_request(socket, &req), true);
+ ck_assert_int_eq(req.opcode, opcode);
+}
+
struct server_state {
pthread_t tid;
pthread_mutex_t lock;
@@ -77,7 +85,7 @@ static void *test_server_thread(void *arg)
ck_assert_int_eq(
sockaddr_set_lease_server_path(&address, config->lease_name), true);
- int server = socket(PF_UNIX, SOCK_STREAM, 0);
+ int server = socket(PF_UNIX, SOCK_SEQPACKET, 0);
ck_assert_int_ge(server, 0);
unlink(address.sun_path);
@@ -102,6 +110,8 @@ static void *test_server_thread(void *arg)
return NULL;
}
+ expect_client_command(client, DLM_GET_LEASE);
+
if (config->send_no_data)
goto done;
@@ -120,6 +130,7 @@ static void *test_server_thread(void *arg)
config->fds[i] = get_dummy_fd();
send_fd_list_over_socket(client, config->nfds, config->fds);
+ expect_client_command(client, DLM_RELEASE_LEASE);
done:
close(client);
close(server);