summaryrefslogtreecommitdiffstats
path: root/libdlmclient/dlmclient.c
blob: dbe26097c8e61e6d73cdce43e9a9ad9b4abb4b5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/* 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 "dlm-protocol.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_SEQPACKET, 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_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)
{
	lease->lease_fd = receive_lease_fd(lease->dlm_server_sock);

	if (lease->lease_fd < 0)
		goto err;

	return true;

err:
	switch (errno) {
	case EACCES:
		DEBUG_LOG("Lease request rejected by DRM lease manager\n");
		break;
	case EPROTO:
		DEBUG_LOG("Unexpected data received from lease manager\n");
		break;
	default:
		DEBUG_LOG("Lease manager receive data error: %s\n",
			  strerror(errno));
		break;
	}
	return false;
}

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));
		return NULL;
	}

	if (!lease_connect(lease, name)) {
		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)
{
	if (!lease)
		return;

	lease_send_request(lease, DLM_RELEASE_LEASE);
	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;
}