e246445e038882ef40d94775b92d8dfe70ebed22
[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_array_t *conns)
28 {
29         int nconnectors = toml_array_nelem(conns);
30         config->connectors = calloc(nconnectors, sizeof(*config->connectors));
31         if (!config->connectors) {
32                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
33                 return false;
34         }
35
36         config->nconnectors = nconnectors;
37
38         for (int i = 0; i < config->nconnectors; i++) {
39                 toml_datum_t conn = toml_string_at(conns, i);
40                 if (!conn.ok) {
41                         ERROR_LOG("Invalid connector in lease %s: idx:%d\n",
42                                   config->lease_name, i);
43                         return false;
44                 }
45                 config->connectors[i].name = conn.u.s;
46         }
47         return true;
48 }
49
50 int parse_config(char *filename, struct lease_config **parsed_config)
51 {
52         struct lease_config *config = NULL;
53         int nconfigs, i, ret = 0;
54         char parse_error[160];
55
56         FILE *fp = fopen(filename, "r");
57         if (!fp)
58                 return 0;
59
60         toml_table_t *t_config =
61             toml_parse_file(fp, parse_error, sizeof parse_error);
62         if (!t_config) {
63                 CONFIG_ERROR("configuration file parse error: %s\n",
64                              parse_error);
65                 fclose(fp);
66                 return 0;
67         }
68
69         toml_array_t *leases = toml_array_in(t_config, "lease");
70         if (!leases) {
71                 CONFIG_ERROR(
72                     "Invalid config - cannot find any 'lease' configs");
73                 goto err;
74         }
75         nconfigs = toml_array_nelem(leases);
76         config = calloc(nconfigs, sizeof *config);
77
78         if (!config) {
79                 DEBUG_LOG("Memory allocation failed: %s\n", strerror(errno));
80                 goto err;
81         }
82
83         for (i = 0; i < toml_array_nelem(leases); i++) {
84                 toml_table_t *lease = toml_table_at(leases, i);
85
86                 toml_datum_t name = toml_string_in(lease, "name");
87                 if (!name.ok) {
88                         CONFIG_ERROR("Invalid lease name in entry #%d\n", i);
89                         goto err_free_config;
90                 }
91
92                 config[i].lease_name = name.u.s;
93
94                 toml_array_t *conns = toml_array_in(lease, "connectors");
95                 if (conns && !populate_connector_config(&config[i], conns)) {
96                         CONFIG_ERROR("Error configuring lease: %s\n",
97                                      config[i].lease_name);
98                         goto err_free_config;
99                 }
100         }
101
102         *parsed_config = config;
103         ret = nconfigs;
104 err:
105         toml_free(t_config);
106         fclose(fp);
107         return ret;
108 err_free_config:
109         release_config(i, config);
110         goto err;
111 }
112
113 void release_config(int num_leases, struct lease_config *config)
114 {
115         for (int i = 0; i < num_leases; i++) {
116                 struct lease_config *c = &config[i];
117                 free(c->lease_name);
118                 for (int j = 0; j < c->nconnectors; j++)
119                         free(c->connectors[j].name);
120                 free(c->connectors);
121         }
122         free(config);
123 }