-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.c
More file actions
332 lines (295 loc) · 7.36 KB
/
config.c
File metadata and controls
332 lines (295 loc) · 7.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/*
* Copyright 2016 Dima Stepanov
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <sys/mman.h>
#include <string.h>
#include "config.h"
#include "plant_limits.h"
enum {
KEY_PLANDB,
KEY_PLANPATH,
MAXNUM_KEYS,
KEY_PRIVATE_CONFPATH,
ALLNUM_KEYS
};
/* Describe the configuration values. */
struct config_keys_s {
char name[CMDNAME_LENGTH];
char value[MAXNAME_LENGTH];
};
struct config_keys_s config_keys[ALLNUM_KEYS] = {
{"PlanDB", ""},
{"PlanPath", ""},
{"", ""},
{"KP_CONFPATH", ""}
};
/* Show current configuration. */
static void show_config()
{
int i;
printf("Current configuration:\n");
for (i = 0; i < ALLNUM_KEYS; i++) {
if (i == MAXNUM_KEYS) {
printf("\tPrivate keys:\n");
continue;
}
printf("\t%s = %s\n", config_keys[i].name,
config_keys[i].value);
}
}
/* Initialize some keys to the default values. */
static void config_init_keys()
{
memcpy(config_keys[KEY_PLANPATH].value,
config_keys[KEY_PRIVATE_CONFPATH].value,
MAXNAME_LENGTH);
}
/* Parse the configuration file and initialize structure. */
static void config_parse_file(FILE *file)
{
char str[CONFIG_MAXSTR_LENGTH];
char *name;
char *val;
int i;
struct config_keys_s *conf;
config_init_keys();
while (fgets(str, CONFIG_MAXSTR_LENGTH, file)) {
/* Get parameter name and its value. */
name = strtok(str, "=\"");
if (!name) {
continue;
}
val = strtok(NULL, "=\"");
if (!val) {
continue;
}
conf = config_keys;
for (i = 0; i < MAXNUM_KEYS; i++) {
if (!strncmp(conf->name, name,
strlen(conf->name))) {
strncpy(conf->value, val, MAXNAME_LENGTH);
conf->value[MAXNAME_LENGTH - 1] = 0;
break;
}
conf++;
}
}
}
/*
* Set the required value for the key in the configuration file. Return
* 0 in case of success, otherwise return 1.
*/
static int config_file_set_value(int fd, int key, char *value)
{
char *buf;
char *start;
char *end;
off_t new_size;
off_t old_size;
char arg[CONFIG_MAXSTR_LENGTH];
int arg_len;
old_size = lseek(fd, 0, SEEK_END);
if (old_size == (off_t)-1) {
printf("Can't get the size of the configuration file.\n");
return 1;
}
snprintf(arg, CONFIG_MAXSTR_LENGTH, "%s=\"%s\"\n",
config_keys[key].name,
value);
arg_len = strlen(arg);
/*
* If required string is truncated, then replace the symbols at
* the end of the string.
*/
if (arg_len == CONFIG_MAXSTR_LENGTH - 1) {
arg[arg_len - 2] = '\"';
arg[arg_len - 1] = '\n';
}
/*
* Note that it can be a new argument in file, so the file should
* be truncated.
*/
new_size = old_size + arg_len;
if (ftruncate(fd, new_size)) {
printf("Can't truncate configuration to new %ld size.\n",
new_size);
return 1;
}
buf = mmap(0, new_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (buf == MAP_FAILED) {
printf("Can't mmap configuration file into memory.\n");
return 1;
}
start = buf;
end = strchr(start, '\n');
if (!end) {
end = buf + old_size;
}
while (start != end) {
if (!strncmp(config_keys[key].name, start,
strlen(config_keys[key].name))) {
/*
* The required value is found, check the size
* of string to change in file. The '\n' character
* should also be taken into consideration.
*/
end++;
/* Make enough space for new option and copy it. */
memmove(start + arg_len, end, old_size - (buf - end));
memcpy(start, arg, arg_len);
/*
* Don't forget to reduce the file size. Cause
* the option is overwritten.
*/
if (ftruncate(fd, new_size - (end - start))) {
printf("Can't truncate configuration to new %ld size.\n",
new_size - (end - start));
munmap(buf, new_size);
return 1;
}
break;
}
start = end;
while ((start < (buf + old_size)) && (*start == '\n')) {
start++;
}
if (start >= (buf + old_size)) {
break;
}
/* Find another string in file. */
end = strchr(start, '\n');
if (!end) {
end = buf + old_size;
}
}
if (start >= (buf + old_size)) {
/*
* No option found, in this case just copy data, to the
* end of file.
*/
memcpy(buf + old_size, arg, arg_len);
}
munmap(buf, new_size);
return 0;
}
/*
* Set the root directory for the configuration file. Return 0 in case
* of success, otherwise, return 1.
*/
static int config_set_private_confpath()
{
struct passwd *pwd;
struct stat st;
char path[CONFIG_PATH_LENGTH];
/*
* Check that configuration directory exists. If not create
* directory.
*/
pwd = getpwuid(getuid());
if (pwd == NULL) {
printf("Can't find home directory for the current user.\n");
return 1;
}
snprintf(path, CONFIG_PATH_LENGTH, "%s/%s", pwd->pw_dir, CONFIG_DIR);
if (stat(path, &st)) {
if (errno != ENOENT) {
printf("Can't get stat for the %s directory.\n", path);
return 1;
}
if (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH)) {
printf("Can't create new %s directory.\n", path);
return 1;
}
} else if (!S_ISDIR(st.st_mode)) {
printf("Configuration %s path is not a directory.\n", path);
return 1;
}
strncpy(config_keys[KEY_PRIVATE_CONFPATH].value,
path, MAXNAME_LENGTH);
config_keys[KEY_PRIVATE_CONFPATH].value[MAXNAME_LENGTH - 1] = 0;
return 0;
}
/* Return the current path to the plan databases. */
static char *config_get_private_confpath()
{
return config_keys[KEY_PRIVATE_CONFPATH].value;
}
/*
* Parse configuration file to obtain main application variables. Return
* 0 in case of success, otherwise return 1.
*/
int config_init(int show)
{
char path[CONFIG_PATH_LENGTH];
FILE *file;
if (config_set_private_confpath()) {
return 1;
}
/* Open the configuration file. Create it if required. */
snprintf(path, CONFIG_PATH_LENGTH, "%s/%s",
config_get_private_confpath(), CONFIG_NAME);
file = fopen(path, "a+");
if (file == NULL) {
printf("Can't open the %s configuration file.\n",
path);
return 1;
}
config_parse_file(file);
fclose(file);
if (show) {
show_config();
}
return 0;
}
/*
* Set the "PlanDB" configuration option to the required value. Return 0
* in case of success, otherwise return 1.
*/
int config_set_plandb(char *name)
{
char path[CONFIG_PATH_LENGTH];
int fd;
snprintf(path, CONFIG_PATH_LENGTH, "%s/%s",
config_get_private_confpath(), CONFIG_NAME);
fd = open(path, O_RDWR);
if (fd == -1) {
printf("Can't open the %s configuration file.\n",
path);
return 1;
}
config_file_set_value(fd, KEY_PLANDB, name);
close(fd);
strncpy(config_keys[KEY_PLANDB].value, name, MAXNAME_LENGTH);
config_keys[KEY_PLANDB].value[MAXNAME_LENGTH - 1] = 0;
return 0;
}
/* Return the current plan name. */
char *config_get_plandb()
{
return config_keys[KEY_PLANDB].value;
}
/* Return the current path to the plan databases. */
char *config_get_planpath()
{
return config_keys[KEY_PLANPATH].value;
}