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 if (!serv->clients[i].is_connected) {
91 client = &serv->clients[i];
100 client->socket.fd = cfd;
102 struct epoll_event ev = {
104 .data.ptr = &client->socket,
106 if (epoll_ctl(ls->epoll_fd, EPOLL_CTL_ADD, cfd, &ev)) {
107 DEBUG_LOG("epoll_ctl add failed: %s\n", strerror(errno));
112 client->is_connected = true;
116 static int create_socket_lock(struct sockaddr_un *addr)
120 int lockfile_len = sizeof(addr->sun_path) + sizeof(SOCK_LOCK_SUFFIX);
121 char lockfile[lockfile_len];
122 int len = snprintf(lockfile, lockfile_len, "%s%s", addr->sun_path,
125 if (len < 0 || len >= lockfile_len) {
126 DEBUG_LOG("Can't create socket lock filename\n");
130 lock_fd = open(lockfile, O_CREAT | O_RDWR,
131 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
134 ERROR_LOG("Cannot access runtime directory\n");
138 if (flock(lock_fd, LOCK_EX | LOCK_NB)) {
140 "socket %s: in use. Possible duplicate lease name or "
141 "mutiple drm-lease-manager instances running\n",
150 static bool server_setup(struct ls *ls, struct ls_server *serv,
151 struct lease_handle *lease_handle)
153 struct sockaddr_un *address = &serv->address;
155 if (!sockaddr_set_lease_server_path(address, lease_handle->name))
158 int socket_lock = create_socket_lock(address);
162 /* The socket address is now owned by this instance, so any existing
163 * sockets can safely be removed */
164 unlink(address->sun_path);
166 address->sun_family = AF_UNIX;
168 int server_socket = socket(PF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0);
169 if (server_socket < 0) {
170 DEBUG_LOG("Socket creation failed: %s\n", strerror(errno));
174 if (bind(server_socket, (struct sockaddr *)address, sizeof(*address))) {
175 ERROR_LOG("Failed to create named socket at %s: %s\n",
176 address->sun_path, strerror(errno));
177 close(server_socket);
181 if (listen(server_socket, 0)) {
182 DEBUG_LOG("listen failed on %s: %s\n", address->sun_path,
184 close(server_socket);
185 unlink(address->sun_path);
189 for (int i = 0; i < ACTIVE_CLIENTS; i++) {
190 struct ls_client *client = &serv->clients[i];
192 client->socket.client = client;
195 serv->lease_handle = lease_handle;
196 serv->server_socket_lock = socket_lock;
198 serv->listen.fd = server_socket;
199 serv->listen.server = serv;
200 serv->listen.is_server = true;
202 struct epoll_event ev = {
204 .data.ptr = &serv->listen,
207 if (epoll_ctl(ls->epoll_fd, EPOLL_CTL_ADD, server_socket, &ev)) {
208 DEBUG_LOG("epoll_ctl add failed: %s\n", strerror(errno));
209 close(server_socket);
210 unlink(address->sun_path);
214 INFO_LOG("Lease server (%s) initialized at %s\n", lease_handle->name,
219 static void server_shutdown(struct ls *ls, struct ls_server *serv)
221 if (unlink(serv->address.sun_path)) {
222 WARN_LOG("Server socket %s delete failed: %s\n",
223 serv->address.sun_path, strerror(errno));
226 epoll_ctl(ls->epoll_fd, EPOLL_CTL_DEL, serv->listen.fd, NULL);
227 close(serv->listen.fd);
229 for (int i = 0; i < ACTIVE_CLIENTS; i++)
230 ls_disconnect_client(ls, &serv->clients[i]);
232 close(serv->server_socket_lock);
235 struct ls *ls_create(struct lease_handle **lease_handles, int count)
237 assert(lease_handles);
240 struct ls *ls = calloc(1, sizeof(struct ls));
242 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
246 ls->servers = calloc(count, sizeof(struct ls_server));
248 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
252 ls->epoll_fd = epoll_create1(0);
253 if (ls->epoll_fd < 0) {
254 DEBUG_LOG("epoll_create failed: %s\n", strerror(errno));
258 for (int i = 0; i < count; i++) {
259 if (!server_setup(ls, &ls->servers[i], lease_handles[i]))
269 void ls_destroy(struct ls *ls)
273 for (int i = 0; i < ls->nservers; i++)
274 server_shutdown(ls, &ls->servers[i]);
281 bool ls_get_request(struct ls *ls, struct ls_req *req)
287 while (request < 0) {
288 struct epoll_event ev;
289 if (epoll_wait(ls->epoll_fd, &ev, 1, -1) < 0) {
292 DEBUG_LOG("epoll_wait failed: %s\n", strerror(errno));
296 struct ls_socket *sock = ev.data.ptr;
299 struct ls_server *server;
300 struct ls_client *client;
302 if (sock->is_server) {
303 if (!(ev.events & POLLIN))
306 server = sock->server;
307 client = client_connect(ls, server);
309 request = LS_REQ_GET_LEASE;
311 if (!(ev.events & POLLHUP))
314 client = sock->client;
315 server = client->serv;
316 request = LS_REQ_RELEASE_LEASE;
319 req->lease_handle = server->lease_handle;
320 req->client = client;
326 bool ls_send_fd(struct ls *ls, struct ls_client *client, int fd)
337 .iov_len = sizeof(data),
340 char ctrl_buf[CMSG_SPACE(sizeof(int))] = {0};
342 struct msghdr msg = {
345 .msg_controllen = sizeof(ctrl_buf),
346 .msg_control = ctrl_buf,
349 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
350 cmsg->cmsg_level = SOL_SOCKET;
351 cmsg->cmsg_type = SCM_RIGHTS;
352 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
353 *((int *)CMSG_DATA(cmsg)) = fd;
355 struct ls_server *serv = client->serv;
357 if (sendmsg(client->socket.fd, &msg, 0) < 0) {
358 DEBUG_LOG("sendmsg failed on %s: %s\n", serv->address.sun_path,
363 INFO_LOG("Lease request granted on %s\n", serv->address.sun_path);
367 void ls_disconnect_client(struct ls *ls, struct ls_client *client)
372 if (!client->is_connected)
375 epoll_ctl(ls->epoll_fd, EPOLL_CTL_DEL, client->socket.fd, NULL);
376 close(client->socket.fd);
377 client->is_connected = false;