Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

387 lines (339 sloc) 16.38 kB
// -*- mode: C; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
// vim: set softtabstop=4 shiftwidth=4 tabstop=4 expandtab:
/*************************************************************************
* Copyright 2009-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.
*
* This file may incorporate work covered under the following copyright
* and permission notice:
*
* Software License Agreement (BSD License)
*
* Copyright (c) 2008, Regents of the University of California
* All rights reserved.
*
* Redistribution and use of this software in source and binary forms,
* with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. USERS OF THIS SOFTWARE ACKNOWLEDGE
* THE POSSIBLE PRESENCE OF OTHER OPEN SOURCE LICENSED MATERIAL,
* COPYRIGHTED MATERIAL OR PATENTED MATERIAL IN THIS SOFTWARE,
* AND IF ANY SUCH MATERIAL IS DISCOVERED THE PARTY DISCOVERING
* IT MAY INFORM DR. RICH WOLSKI AT THE UNIVERSITY OF CALIFORNIA,
* SANTA BARBARA WHO WILL THEN ASCERTAIN THE MOST APPROPRIATE REMEDY,
* WHICH IN THE REGENTS' DISCRETION MAY INCLUDE, WITHOUT LIMITATION,
* REPLACEMENT OF THE CODE SO IDENTIFIED, LICENSING OF THE CODE SO
* IDENTIFIED, OR WITHDRAWAL OF THE CODE CAPABILITY TO THE EXTENT
* NEEDED TO COMPLY WITH ANY SUCH LICENSES OR RIGHTS.
************************************************************************/
//!
//! @file util/config.c
//! Need to provide description
//!
/*----------------------------------------------------------------------------*\
| |
| INCLUDES |
| |
\*----------------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/errno.h>
#include <assert.h>
#include <string.h>
#include "eucalyptus.h"
#include "misc.h"
#include "config.h"
/*----------------------------------------------------------------------------*\
| |
| DEFINES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| TYPEDEFS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| ENUMERATIONS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STRUCTURES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| EXTERNAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/* Should preferably be handled in header file */
/*----------------------------------------------------------------------------*\
| |
| GLOBAL VARIABLES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| STATIC VARIABLES |
| |
\*----------------------------------------------------------------------------*/
static int configRestartLen = 0;
static int configNoRestartLen = 0;
static char *configValuesRestart[256];
static char *configValuesNoRestart[256];
static configEntry *configKeysRestart;
static configEntry *configKeysNoRestart;
static time_t lastConfigMtime = 0;
/*----------------------------------------------------------------------------*\
| |
| EXPORTED PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
void configInitValues(configEntry newConfigKeysRestart[], configEntry newConfigKeysNoRestart[]);
int isConfigModified(char configFiles[][MAX_PATH], int numFiles);
char *configFileValue(const char *key);
boolean configFileValueLong(const char *key, long *val);
int readConfigFile(char configFiles[][MAX_PATH], int numFiles);
void configReadLogParams(int *log_level_out, int *log_roll_number_out, long *log_max_size_bytes_out, char **log_prefix);
/*----------------------------------------------------------------------------*\
| |
| STATIC PROTOTYPES |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| MACROS |
| |
\*----------------------------------------------------------------------------*/
/*----------------------------------------------------------------------------*\
| |
| IMPLEMENTATION |
| |
\*----------------------------------------------------------------------------*/
//!
//! Initialize our local configuration key parameters
//!
//! @param[in] newConfigKeysRestart
//! @param[in] newConfigKeysNoRestart
//!
void configInitValues(configEntry newConfigKeysRestart[], configEntry newConfigKeysNoRestart[])
{
configKeysRestart = newConfigKeysRestart;
configKeysNoRestart = newConfigKeysNoRestart;
}
//!
//! Checks wether or not a given list of configuration file has been modified since we
//! last read them.
//!
//! @param[in] configFiles the list of configuration file names.
//! @param[in] numFiles the number of file names in the list
//!
//! @return 0 if the configuration files were not modified; 1 if the configuration files were
//! modified; and -1 if an error occured while poking at the configuration files.
//!
int isConfigModified(char configFiles[][MAX_PATH], int numFiles)
{
int i = 0;
time_t configMtime = 0;
struct stat statbuf = { 0 };
for (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) {
LOGERROR("could not stat config files (%s,%s)\n", configFiles[0], configFiles[1]);
return (-1);
}
if (lastConfigMtime != configMtime) {
LOGDEBUG("current mtime=%ld, stored mtime=%ld\n", configMtime, lastConfigMtime);
lastConfigMtime = configMtime;
return (1);
}
return (0);
}
//!
//! Retrieves a configuration value based on the given keyname.
//!
//! @param[in] key the name of the key for which we're looking for its paired value
//!
//! @return a string copy of the value matching the provided keyname or NULL if the
//! key is not found. The caller of this function is responsible to free the
//! returned string value.
//!
char *configFileValue(const char *key)
{
int i = 0;
if (key != NULL) {
for (i = 0; i < configRestartLen; i++) {
if (configKeysRestart[i].key) {
if (!strcmp(configKeysRestart[i].key, key)) {
// have a match among values that restart
if (configValuesRestart[i])
return (strdup(configValuesRestart[i]));
if (configKeysRestart[i].defaultValue)
return (strdup(configKeysRestart[i].defaultValue));
return (NULL);
}
}
}
for (i = 0; i < configNoRestartLen; i++) {
if (configKeysNoRestart[i].key) {
if (!strcmp(configKeysNoRestart[i].key, key)) {
if (configValuesNoRestart[i])
return (strdup(configValuesNoRestart[i]));
if (configKeysNoRestart[i].defaultValue)
return (strdup(configKeysNoRestart[i].defaultValue));
return (NULL);
}
}
}
}
return (NULL);
}
//!
//! Retrieves a "long" integer value from the configuration entry.
//!
//! @param[in] key the name of the key for which we're looking for its paired value
//! @param[out] val the matching long value to be set if 'key' is found
//!
//! @return TRUE if the key was found and the matching value was a valid long integer and
//! the 'val' value is set appropriately, otherwise FALSE is returned.
//!
boolean configFileValueLong(const char *key, long *val)
{
long v = 0;
boolean found = FALSE;
char *endptr = NULL;
char *tmpstr = configFileValue(key);
if ((tmpstr != NULL) && (val != NULL)) {
errno = 0;
v = strtoll(tmpstr, &endptr, 10);
if ((errno == 0) && ((*endptr) == '\0')) {
// successful complete conversion
*val = v;
found = TRUE;
}
EUCA_FREE(tmpstr);
}
return (found);
}
//!
//! Reads a list of configuration files and fill in our configuration holders
//!
//! @param[in] configFiles a list of configuration file path
//! @param[in] numFiles the number of configuration files in the list
//!
//! @return the number of configuration files parsed
//!
int readConfigFile(char configFiles[][MAX_PATH], int numFiles)
{
int i = 0;
int ret = 0;
char *old = NULL;
char *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))) {
LOGWARN("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));
}
EUCA_FREE(new);
} else {
LOGINFO("read (%s=%s, default=%s)\n", configKeysRestart[i].key, SP(new), SP(configKeysRestart[i].defaultValue));
EUCA_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))) {
LOGINFO("configuration file changed (KEY=%s, ORIGVALUE=%s, NEWVALUE=%s): change will take effect immediately.\n", configKeysNoRestart[i].key, SP(old), SP(new));
ret++;
EUCA_FREE(configValuesNoRestart[i]);
configValuesNoRestart[i] = new;
} else {
EUCA_FREE(new);
}
} else {
LOGINFO("read (%s=%s, default=%s)\n", configKeysNoRestart[i].key, SP(new), SP(configKeysNoRestart[i].defaultValue));
EUCA_FREE(configValuesNoRestart[i]);
configValuesNoRestart[i] = new;
ret++;
}
}
configNoRestartLen = i;
return (ret);
}
//!
//! Helper for reading log-related params from eucalyptus.conf
//!
//! @param[out] log_level_out
//! @param[out] log_roll_number_out
//! @param[out] log_max_size_bytes_out
//! @param[out] log_prefix
//!
void configReadLogParams(int *log_level_out, int *log_roll_number_out, long *log_max_size_bytes_out, char **log_prefix)
{
long l = 0;
char *s = configFileValue("LOGLEVEL");
assert(s != NULL); // configFileValue should return default
*log_level_out = log_level_int(s);
configFileValueLong("LOGROLLNUMBER", &l);
*log_roll_number_out = ((int)l);
configFileValueLong("LOGMAXSIZE", log_max_size_bytes_out);
*log_prefix = configFileValue("LOGPREFIX");
}
Jump to Line
Something went wrong with that request. Please try again.