Add 'optional' property to connector configuration
[src/drm-lease-manager.git] / drm-lease-manager / lease-config.c
1 /* Copyright 2022 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-config.h"
17 #include "log.h"
18 #include <errno.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <toml.h>
23
24 #define CONFIG_ERROR(x, ...) ERROR_LOG("%s: " x, filename, ##__VA_ARGS__)
25
26 static bool populate_connector_config(struct lease_config *config,
27                                       toml_table_t *global_table,
28                                       toml_array_t *conns)
29 {
30         int nconnectors = toml_array_nelem(conns);
31         config->connectors = calloc(nconnectors, sizeof(*config->connectors));
32         if (!config->connectors) {
33                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
34                 return false;
35         }
36
37         config->nconnectors = nconnectors;
38
39         for (int i = 0; i < config->nconnectors; i++) {
40                 struct connector_config *conn_config = &config->connectors[i];
41                 toml_datum_t conn = toml_string_at(conns, i);
42                 if (!conn.ok) {
43                         ERROR_LOG("Invalid connector in lease %s: idx:%d\n",
44                                   config->lease_name, i);
45                         return false;
46                 }
47                 conn_config->name = conn.u.s;
48
49                 toml_table_t *conn_config_data =
50                     toml_table_in(global_table, conn.u.s);
51                 if (!conn_config_data)
52                         continue;
53
54                 toml_datum_t optional =
55                     toml_bool_in(conn_config_data, "optional");
56                 if (optional.ok)
57                         config->connectors[i].optional = optional.u.b;
58         }
59         return true;
60 }
61
62 int parse_config(char *filename, struct lease_config **parsed_config)
63 {
64         struct lease_config *config = NULL;
65         int nconfigs, i, ret = 0;
66         char parse_error[160];
67
68         FILE *fp = fopen(filename, "r");
69         if (!fp)
70                 return 0;
71
72         toml_table_t *t_config =
73             toml_parse_file(fp, parse_error, sizeof parse_error);
74         if (!t_config) {
75                 CONFIG_ERROR("configuration file parse error: %s\n",
76                              parse_error);
77                 fclose(fp);
78                 return 0;
79         }
80
81         toml_array_t *leases = toml_array_in(t_config, "lease");
82         if (!leases) {
83                 CONFIG_ERROR(
84                     "Invalid config - cannot find any 'lease' configs");
85                 goto err;
86         }
87         nconfigs = toml_array_nelem(leases);
88         config = calloc(nconfigs, sizeof *config);
89
90         if (!config) {
91                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
92                 goto err;
93         }
94
95         for (i = 0; i < toml_array_nelem(leases); i++) {
96                 toml_table_t *lease = toml_table_at(leases, i);
97
98                 toml_datum_t name = toml_string_in(lease, "name");
99                 if (!name.ok) {
100                         CONFIG_ERROR("Invalid lease name in entry #%d\n", i);
101                         goto err_free_config;
102                 }
103
104                 config[i].lease_name = name.u.s;
105
106                 toml_array_t *conns = toml_array_in(lease, "connectors");
107                 if (conns &&
108                     !populate_connector_config(&config[i], t_config, conns)) {
109                         CONFIG_ERROR("Error configuring lease: %s\n",
110                                      config[i].lease_name);
111                         goto err_free_config;
112                 }
113         }
114
115         *parsed_config = config;
116         ret = nconfigs;
117 err:
118         toml_free(t_config);
119         fclose(fp);
120         return ret;
121 err_free_config:
122         release_config(i, config);
123         goto err;
124 }
125
126 void release_config(int num_leases, struct lease_config *config)
127 {
128         for (int i = 0; i < num_leases; i++) {
129                 struct lease_config *c = &config[i];
130                 free(c->lease_name);
131                 for (int j = 0; j < c->nconnectors; j++)
132                         free(c->connectors[j].name);
133                 free(c->connectors);
134         }
135         free(config);
136 }