/
config.c
172 lines (155 loc) · 6.09 KB
/
config.c
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
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2012 Eucalyptus Systems, Inc.
*
* 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; version 3 of the License.
*
* 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/.
*
* Please contact Eucalyptus Systems, Inc., 6755 Hollister Ave., Goleta
* CA 93117, USA or visit http://www.eucalyptus.com/licenses/ if you need
* additional information or have any questions.
************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <assert.h>
#include "string.h"
#include "misc.h"
#include "config.h"
static int configRestartLen=0, configNoRestartLen=0;
static char *configValuesRestart[256], *configValuesNoRestart[256];
static configEntry * configKeysRestart, * configKeysNoRestart;
static time_t lastConfigMtime = 0;
void configInitValues (configEntry newConfigKeysRestart[], configEntry newConfigKeysNoRestart[])
{
configKeysRestart = newConfigKeysRestart;
configKeysNoRestart = newConfigKeysNoRestart;
}
int isConfigModified (char configFiles[][MAX_PATH], int numFiles)
{
struct stat statbuf;
time_t configMtime = 0;
for (int i=0; i<numFiles; i++) {
// stat the config file, update modification time
if (stat(configFiles[i], &statbuf) == 0) {
if (statbuf.st_mtime > 0 || statbuf.st_ctime > 0) {
if (statbuf.st_ctime > statbuf.st_mtime) {
configMtime = statbuf.st_ctime;
} else {
configMtime = statbuf.st_mtime;
}
}
}
}
if (configMtime == 0) {
logprintfl(EUCAERROR, "isConfigModified(): could not stat config files (%s,%s)\n", configFiles[0], configFiles[1]);
return -1;
}
if (lastConfigMtime!=configMtime) {
logprintfl(EUCADEBUG, "isConfigModified(): current mtime=%d, stored mtime=%d\n", configMtime, lastConfigMtime);
lastConfigMtime=configMtime;
return 1;
}
return 0;
}
char *configFileValue(const char *key)
{
int i;
for (i=0; i<configRestartLen; i++) {
if (configKeysRestart[i].key) {
if (!strcmp(configKeysRestart[i].key, key)) {
return(configValuesRestart[i] ? strdup(configValuesRestart[i]) : (configKeysRestart[i].defaultValue ? strdup(configKeysRestart[i].defaultValue) : NULL));
}
}
}
for (i=0; i<configNoRestartLen; i++) {
if (configKeysNoRestart[i].key) {
if (!strcmp(configKeysNoRestart[i].key, key)) {
return(configValuesNoRestart[i] ? strdup(configValuesNoRestart[i]) : (configKeysNoRestart[i].defaultValue ? strdup(configKeysNoRestart[i].defaultValue) : NULL));
}
}
}
return(NULL);
}
int configFileValueLong(const char *key, long *val)
{
int found = 0;
char * tmpstr = configFileValue(key);
if (tmpstr != NULL) {
char * endptr;
errno = 0;
long v = strtoll (tmpstr, &endptr, 10);
if (errno == 0 && *endptr == '\0') { // successful complete conversion
* val = v;
found = 1;
}
}
return found;
}
int readConfigFile(char configFiles[][MAX_PATH], int numFiles)
{
int i, ret=0;
char *old=NULL, *new=NULL;
for (i=0; configKeysRestart[i].key; i++) {
old = configValuesRestart[i];
new = getConfString(configFiles, numFiles, configKeysRestart[i].key);
if (configRestartLen) {
if ( (!old && new) || (old && !new) || ( (old && new) && strcmp(old, new) ) ) {
logprintfl(EUCAWARN, "readConfigFile(): configuration file changed (KEY=%s, ORIGVALUE=%s, NEWVALUE=%s): clean restart is required before this change will take effect!\n", configKeysRestart[i].key, SP(old), SP(new));
}
if (new) free(new);
} else {
logprintfl(EUCAINFO, "readConfigFile(): read (%s=%s, default=%s)\n", configKeysRestart[i].key, SP(new), SP(configKeysRestart[i].defaultValue));
if (configValuesRestart[i]) free(configValuesRestart[i]);
configValuesRestart[i] = new;
ret++;
}
}
configRestartLen = i;
for (i=0; configKeysNoRestart[i].key; i++) {
old = configValuesNoRestart[i];
new = getConfString(configFiles, numFiles, configKeysNoRestart[i].key);
if (configNoRestartLen) {
if ( (!old && new) || (old && !new) || ( (old && new) && strcmp(old, new) ) ) {
logprintfl(EUCAINFO, "readConfigFile(): configuration file changed (KEY=%s, ORIGVALUE=%s, NEWVALUE=%s): change will take effect immediately.\n", configKeysNoRestart[i].key, SP(old), SP(new));
ret++;
if (configValuesNoRestart[i]) free(configValuesNoRestart[i]);
configValuesNoRestart[i] = new;
} else {
if (new) free(new);
}
} else {
logprintfl(EUCAINFO, "readConfigFile(): read (%s=%s, default=%s)\n", configKeysNoRestart[i].key, SP(new), SP(configKeysNoRestart[i].defaultValue));
if (configValuesNoRestart[i]) free(configValuesNoRestart[i]);
configValuesNoRestart[i] = new;
ret++;
}
}
configNoRestartLen = i;
return(ret);
}
// helper for reading log-related params from eucalyptus.conf
void configReadLogParams(int *log_level_out, int *log_roll_number_out, long *log_max_size_bytes_out)
{
char * s = configFileValue ("LOGLEVEL");
// assert (s); // we should always have a value, from file or the default
* log_level_out = log_level_int (s);
free (s);
long l;
configFileValueLong ("LOGROLLNUMBER", &l);
* log_roll_number_out = (int)l;
configFileValueLong ("LOGMAXSIZE", log_max_size_bytes_out);
}