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 "lease-server.h"
18 #include "socket-path.h"
25 #include <sys/epoll.h>
27 #include <sys/socket.h>
29 #include <sys/types.h>
33 #define SOCK_LOCK_SUFFIX ".lock"
36 * An 'active' client is one that either
38 * - is requesting ownership of a lease (which will
39 * disconnect the current owner if granted)
41 * There can only be at most one of each kind of client at the same
42 * time. Any other client connections are queued in the
43 * listen() backlog, waiting to be accept()'ed.
45 #define ACTIVE_CLIENTS 2
51 struct ls_server *server;
52 struct ls_client *client;
57 struct ls_socket socket;
58 struct ls_server *serv;
63 struct lease_handle *lease_handle;
64 struct sockaddr_un address;
65 int server_socket_lock;
67 struct ls_socket listen;
68 struct ls_client clients[ACTIVE_CLIENTS];
74 struct ls_server *servers;
78 static struct ls_client *client_connect(struct ls *ls, struct ls_server *serv)
80 int cfd = accept(serv->listen.fd, NULL, NULL);
82 DEBUG_LOG("accept failed on %s: %s\n", serv->address.sun_path,
87 struct ls_client *client = NULL;
89 for (int i = 0; i < ACTIVE_CLIENTS; i++) {
90 client = &serv->clients[i];
91 if (!client->is_connected)
99 client->socket.fd = cfd;
101 struct epoll_event ev = {
103 .data.ptr = &client->socket,
105 if (epoll_ctl(ls->epoll_fd, EPOLL_CTL_ADD, cfd, &ev)) {
106 DEBUG_LOG("epoll_ctl add failed: %s\n", strerror(errno));
111 client->is_connected = true;
115 static int create_socket_lock(struct sockaddr_un *addr)
119 int lockfile_len = sizeof(addr->sun_path) + sizeof(SOCK_LOCK_SUFFIX);
120 char lockfile[lockfile_len];
121 int len = snprintf(lockfile, lockfile_len, "%s%s", addr->sun_path,
124 if (len < 0 || len >= lockfile_len) {
125 DEBUG_LOG("Can't create socket lock filename\n");
129 lock_fd = open(lockfile, O_CREAT | O_RDWR,
130 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
133 ERROR_LOG("Cannot access runtime directory\n");
137 if (flock(lock_fd, LOCK_EX | LOCK_NB)) {
139 "socket %s: in use. Possible duplicate lease name or "
140 "mutiple drm-lease-manager instances running\n",
149 static bool server_setup(struct ls *ls, struct ls_server *serv,
150 struct lease_handle *lease_handle)
152 struct sockaddr_un *address = &serv->address;
154 if (!sockaddr_set_lease_server_path(address, lease_handle->name))
157 int socket_lock = create_socket_lock(address);
161 /* The socket address is now owned by this instance, so any existing
162 * sockets can safely be removed */
163 unlink(address->sun_path);
165 address->sun_family = AF_UNIX;
167 int server_socket = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
168 if (server_socket < 0) {
169 DEBUG_LOG("Socket creation failed: %s\n", strerror(errno));
173 if (bind(server_socket, (struct sockaddr *)address, sizeof(*address))) {
174 ERROR_LOG("Failed to create named socket at %s: %s\n",
175 address->sun_path, strerror(errno));
176 close(server_socket);
180 if (listen(server_socket, 0)) {
181 DEBUG_LOG("listen failed on %s: %s\n", address->sun_path,
183 close(server_socket);
184 unlink(address->sun_path);
188 for (int i = 0; i < ACTIVE_CLIENTS; i++) {
189 struct ls_client *client = &serv->clients[i];
191 client->socket.client = client;
194 serv->lease_handle = lease_handle;
195 serv->server_socket_lock = socket_lock;
197 serv->listen.fd = server_socket;
198 serv->listen.server = serv;
199 serv->listen.is_server = true;
201 struct epoll_event ev = {
203 .data.ptr = &serv->listen,
206 if (epoll_ctl(ls->epoll_fd, EPOLL_CTL_ADD, server_socket, &ev)) {
207 DEBUG_LOG("epoll_ctl add failed: %s\n", strerror(errno));
208 close(server_socket);
209 unlink(address->sun_path);
213 INFO_LOG("Lease server (%s) initialized at %s\n", lease_handle->name,
218 static void server_shutdown(struct ls *ls, struct ls_server *serv)
220 if (unlink(serv->address.sun_path)) {
221 WARN_LOG("Server socket %s delete failed: %s\n",
222 serv->address.sun_path, strerror(errno));
225 epoll_ctl(ls->epoll_fd, EPOLL_CTL_DEL, serv->listen.fd, NULL);
226 close(serv->listen.fd);
228 for (int i = 0; i < ACTIVE_CLIENTS; i++)
229 ls_disconnect_client(ls, &serv->clients[i]);
231 close(serv->server_socket_lock);
234 struct ls *ls_create(struct lease_handle **lease_handles, int count)
236 assert(lease_handles);
239 struct ls *ls = calloc(1, sizeof(struct ls));
241 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
245 ls->servers = calloc(count, sizeof(struct ls_server));
247 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
251 ls->epoll_fd = epoll_create1(0);
252 if (ls->epoll_fd < 0) {
253 DEBUG_LOG("epoll_create failed: %s\n", strerror(errno));
257 for (int i = 0; i < count; i++) {
258 if (!server_setup(ls, &ls->servers[i], lease_handles[i]))
268 void ls_destroy(struct ls *ls)
272 for (int i = 0; i < ls->nservers; i++)
273 server_shutdown(ls, &ls->servers[i]);
280 bool ls_get_request(struct ls *ls, struct ls_req *req)
286 while (request < 0) {
287 struct epoll_event ev;
288 if (epoll_wait(ls->epoll_fd, &ev, 1, -1) < 0) {
291 DEBUG_LOG("epoll_wait failed: %s\n", strerror(errno));
295 struct ls_socket *sock = ev.data.ptr;
298 struct ls_server *server;
299 struct ls_client *client;
301 if (sock->is_server) {
302 if (!(ev.events & POLLIN))
305 server = sock->server;
306 client = client_connect(ls, server);
308 request = LS_REQ_GET_LEASE;
310 if (!(ev.events & POLLHUP))
313 client = sock->client;
314 server = client->serv;
315 request = LS_REQ_RELEASE_LEASE;
318 req->lease_handle = server->lease_handle;
319 req->client = client;
325 bool ls_send_fd(struct ls *ls, struct ls_client *client, int fd)
336 .iov_len = sizeof(data),
339 char ctrl_buf[CMSG_SPACE(sizeof(int))] = {0};
341 struct msghdr msg = {
344 .msg_controllen = sizeof(ctrl_buf),
345 .msg_control = ctrl_buf,
348 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
349 cmsg->cmsg_level = SOL_SOCKET;
350 cmsg->cmsg_type = SCM_RIGHTS;
351 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
352 *((int *)CMSG_DATA(cmsg)) = fd;
354 struct ls_server *serv = client->serv;
356 if (sendmsg(client->socket.fd, &msg, 0) < 0) {
357 DEBUG_LOG("sendmsg failed on %s: %s\n", serv->address.sun_path,
362 INFO_LOG("Lease request granted on %s\n", serv->address.sun_path);
366 void ls_disconnect_client(struct ls *ls, struct ls_client *client)
371 if (!client->is_connected)
374 epoll_ctl(ls->epoll_fd, EPOLL_CTL_DEL, client->socket.fd, NULL);
375 close(client->socket.fd);
376 client->is_connected = false;