forked from jcnelson/vdev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
187 lines (130 loc) · 4.44 KB
/
config.cpp
File metadata and controls
187 lines (130 loc) · 4.44 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
/*
vdev: a virtual device manager for *nix
Copyright (C) 2014 Jude Nelson
This program is dual-licensed: you can redistribute it and/or modify
it under the terms of the GNU General Public License version 3 or later as
published by the Free Software Foundation. For the terms of this
license, see LICENSE.LGPLv3+ or <http://www.gnu.org/licenses/>.
You are free to use this program under the terms of the GNU General
Public License, 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.
Alternatively, you are free to use this program under the terms of the
Internet Software Consortium License, but WITHOUT ANY WARRANTY; without
even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
For the terms of this license, see LICENSE.ISC or
<http://www.isc.org/downloads/software-support-policy/isc-license/>.
*/
#include "config.h"
#define INI_MAX_LINE 4096
#define INI_STOP_ON_FIRST_ERROR 1
#include "ini.h"
// ini parser callback
static int vdev_config_ini_parser( void* userdata, char const* section, char const* name, char const* value ) {
struct vdev_config* conf = (struct vdev_config*)userdata;
if( strcmp(section, VDEV_CONFIG_NAME) == 0 ) {
if( strcmp(name, VDEV_CONFIG_FIRMWARE_DIR) == 0 ) {
// save this
conf->firmware_dir = vdev_strdup_or_null( value );
return 1;
}
if( strcmp(name, VDEV_CONFIG_ACLS) == 0 ) {
// save this
conf->acls_dir = vdev_strdup_or_null( value );
return 1;
}
if( strcmp(name, VDEV_CONFIG_ACTIONS) == 0 ) {
// save this
conf->acts_dir = vdev_strdup_or_null( value );
return 1;
}
if( strcmp(name, VDEV_CONFIG_PSTAT) == 0 ) {
if( strcmp(value, VDEV_CONFIG_NAME_PSTAT_HASH) == 0 ) {
// force sha256 check on pstat
conf->pstat_discipline |= PSTAT_HASH;
return 1;
}
fprintf(stderr, "Invalid value '%s' for '%s'\n", value, name );
return 0;
}
fprintf(stderr, "Unrecognized '%s' field '%s'\n", section, name);
return 1;
}
if( strcmp( section, VDEV_OS_CONFIG_NAME) == 0 ) {
// OS-specific config value
try {
(*conf->os_config)[ string(name) ] = string(value);
return 1;
}
catch( bad_alloc& ba ) {
// out of memory
return 0;
}
}
fprintf(stderr, "Unrecognized field '%s'\n", name);
return 1;
}
// config sanity check
int vdev_config_sanity_check( struct vdev_config* conf ) {
int rc = 0;
if( conf->acls_dir == NULL ) {
fprintf(stderr, "ERR: Config file is missing 'acls='\n");
rc = -EINVAL;
}
if( conf->acts_dir == NULL ) {
fprintf(stderr, "ERR: Config file is missing 'actions='\n");
rc = -EINVAL;
}
return rc;
}
// initialize a config
int vdev_config_init( struct vdev_config* conf ) {
memset( conf, 0, sizeof(struct vdev_config) );
conf->os_config = new (nothrow) vdev_config_map_t();
if( conf->os_config == NULL ) {
return -ENOMEM;
}
return 0;
}
// load from a file, by path
int vdev_config_load( char const* path, struct vdev_config* conf ) {
FILE* f = NULL;
int rc = 0;
f = fopen( path, "r" );
if( f == NULL ) {
rc = -errno;
return rc;
}
rc = vdev_config_load_file( f, conf );
fclose( f );
return rc;
}
// load from a file
int vdev_config_load_file( FILE* file, struct vdev_config* conf ) {
int rc = 0;
rc = ini_parse_file( file, vdev_config_ini_parser, conf );
if( rc != 0 ) {
vdev_error("ini_parse_file(config) rc = %d\n", rc );
}
return rc;
}
// free a config
int vdev_config_free( struct vdev_config* conf ) {
if( conf->firmware_dir != NULL ) {
free( conf->firmware_dir );
conf->firmware_dir = NULL;
}
if( conf->acls_dir != NULL ) {
free( conf->acls_dir );
conf->acls_dir = NULL;
}
if( conf->acts_dir != NULL ) {
free( conf->acts_dir );
conf->acts_dir = NULL;
}
if( conf->os_config != NULL ) {
delete conf->os_config;
conf->os_config = NULL;
}
return 0;
}