Add initial version
[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                progname);
32 }
33
34 const char *opts = "vh";
35 const struct option options[] = {
36     {"help", no_argument, NULL, 'h'},
37     {"verbose", no_argument, NULL, 'v'},
38     {NULL, 0, NULL, 0},
39 };
40
41 int main(int argc, char **argv)
42 {
43         char *device = "/dev/dri/card0";
44
45         bool debug_log = false;
46
47         int c;
48         while ((c = getopt_long(argc, argv, opts, options, NULL)) != -1) {
49                 int ret = EXIT_FAILURE;
50                 switch (c) {
51                 case 'v':
52                         debug_log = true;
53                         break;
54                 case 'h':
55                         ret = EXIT_SUCCESS;
56                         /* fall through */
57                 default:
58                         usage(argv[0]);
59                         return ret;
60                 }
61         }
62
63         if (optind < argc)
64                 device = argv[optind];
65
66         dlm_log_enable_debug(debug_log);
67
68         struct lm *lm = lm_create(device);
69         if (!lm) {
70                 ERROR_LOG("DRM Lease initialization failed\n");
71                 return EXIT_FAILURE;
72         }
73
74         struct lease_handle **lease_handles = NULL;
75         int count_ids = lm_get_lease_handles(lm, &lease_handles);
76         assert(count_ids > 0);
77
78         struct ls *ls = ls_create(lease_handles, count_ids);
79         if (!ls) {
80                 lm_destroy(lm);
81                 ERROR_LOG("Client socket initialization failed\n");
82                 return EXIT_FAILURE;
83         }
84
85         struct ls_req req;
86         while (ls_get_request(ls, &req)) {
87                 switch (req.type) {
88                 case LS_REQ_GET_LEASE: {
89                         int fd = lm_lease_grant(lm, req.lease_handle);
90                         if (fd < 0) {
91                                 ERROR_LOG(
92                                     "Can't fulfill lease request: lease=%s\n",
93                                     req.lease_handle->name);
94                                 ls_disconnect_client(ls, req.server);
95                                 break;
96                         }
97
98                         if (!ls_send_fd(ls, req.server, fd)) {
99                                 ERROR_LOG(
100                                     "Client communication error: lease=%s\n",
101                                     req.lease_handle->name);
102                                 ls_disconnect_client(ls, req.server);
103                                 lm_lease_revoke(lm, req.lease_handle);
104                         }
105                         break;
106                 }
107                 case LS_REQ_RELEASE_LEASE:
108                         ls_disconnect_client(ls, req.server);
109                         lm_lease_revoke(lm, req.lease_handle);
110                         break;
111                 default:
112                         ERROR_LOG("Internal error: Invalid lease request\n");
113                         goto done;
114                 }
115         }
116 done:
117         ls_destroy(ls);
118         lm_destroy(lm);
119         return EXIT_FAILURE;
120 }