aboutsummaryrefslogtreecommitdiffstats
path: root/libdlmclient/dlmclient.c
blob: 32493d37fb3134393f08f17b1f0ab4ac02ba46b6 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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;
}