Add support for lease transition
[src/drm-lease-manager.git] / drm-lease-manager / main.c
1 /* Copyright 2020-2021 IGEL Co., Ltd.
2  *
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
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include "lease-manager.h"
17 #include "lease-server.h"
18 #include "log.h"
19
20 #include <assert.h>
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24
25 static void usage(const char *progname)
26 {
27         printf("Usage: %s [OPTIONS] [<DRM device>]\n\n"
28                "Options:\n"
29                "-h, --help \tPrint this help\n"
30                "-v, --verbose \tEnable verbose debug messages\n"
31                "-t, --lease-transfer \tAllow lease transfter to new clients\n",
32                progname);
33 }
34
35 const char *opts = "vth";
36 const struct option options[] = {
37     {"help", no_argument, NULL, 'h'},
38     {"verbose", no_argument, NULL, 'v'},
39     {"lease-transfer", no_argument, NULL, 't'},
40     {NULL, 0, NULL, 0},
41 };
42
43 int main(int argc, char **argv)
44 {
45         char *device = "/dev/dri/card0";
46
47         bool debug_log = false;
48         bool can_transfer_leases = false;
49
50         int c;
51         while ((c = getopt_long(argc, argv, opts, options, NULL)) != -1) {
52                 int ret = EXIT_FAILURE;
53                 switch (c) {
54                 case 'v':
55                         debug_log = true;
56                         break;
57                 case 't':
58                         can_transfer_leases = true;
59                         break;
60                 case 'h':
61                         ret = EXIT_SUCCESS;
62                         /* fall through */
63                 default:
64                         usage(argv[0]);
65                         return ret;
66                 }
67         }
68
69         if (optind < argc)
70                 device = argv[optind];
71
72         dlm_log_enable_debug(debug_log);
73
74         struct lm *lm = lm_create(device);
75         if (!lm) {
76                 ERROR_LOG("DRM Lease initialization failed\n");
77                 return EXIT_FAILURE;
78         }
79
80         struct lease_handle **lease_handles = NULL;
81         int count_ids = lm_get_lease_handles(lm, &lease_handles);
82         assert(count_ids > 0);
83
84         struct ls *ls = ls_create(lease_handles, count_ids);
85         if (!ls) {
86                 lm_destroy(lm);
87                 ERROR_LOG("Client socket initialization failed\n");
88                 return EXIT_FAILURE;
89         }
90
91         struct ls_req req;
92         while (ls_get_request(ls, &req)) {
93                 switch (req.type) {
94                 case LS_REQ_GET_LEASE: {
95                         int fd = lm_lease_grant(lm, req.lease_handle);
96
97                         if (fd < 0 && can_transfer_leases)
98                                 fd = lm_lease_transfer(lm, req.lease_handle);
99
100                         if (fd < 0) {
101                                 ERROR_LOG(
102                                     "Can't fulfill lease request: lease=%s\n",
103                                     req.lease_handle->name);
104                                 ls_disconnect_client(ls, req.client);
105                                 break;
106                         }
107
108                         struct ls_client *active_client =
109                             req.lease_handle->user_data;
110                         if (active_client)
111                                 ls_disconnect_client(ls, active_client);
112
113                         req.lease_handle->user_data = req.client;
114
115                         if (!ls_send_fd(ls, req.client, fd)) {
116                                 ERROR_LOG(
117                                     "Client communication error: lease=%s\n",
118                                     req.lease_handle->name);
119                                 ls_disconnect_client(ls, req.client);
120                                 lm_lease_revoke(lm, req.lease_handle);
121                         }
122                         break;
123                 }
124                 case LS_REQ_RELEASE_LEASE:
125                         ls_disconnect_client(ls, req.client);
126                         req.lease_handle->user_data = NULL;
127                         lm_lease_revoke(lm, req.lease_handle);
128                         break;
129                 default:
130                         ERROR_LOG("Internal error: Invalid lease request\n");
131                         goto done;
132                 }
133         }
134 done:
135         ls_destroy(ls);
136         lm_destroy(lm);
137         return EXIT_FAILURE;
138 }