Bug #351570: Hidden function fixes: rc_conf_value.

Refactor rc_conf_value into librc for use in library context.
Also requires moving:
- rc_conf internal static
- Defines: PROFILE_ENV, SYS_WHITELIST, USR_WHITELIST, RC_PATH_PREFIX
  moved to rc.h with new RC_ prefix added.
- Defines: RC_CONF, RC_CONF_OLD moved to rc.h.

Signed-off-by: Robin H. Johnson <robbat2@gentoo.org>
This commit is contained in:
Robin H. Johnson 2011-01-17 19:22:53 +00:00
parent 6e876bca13
commit 2b7c2b8cf1
7 changed files with 73 additions and 60 deletions

View File

@ -218,3 +218,54 @@ rc_config_value(RC_STRINGLIST *list, const char *entry)
return NULL;
}
librc_hidden_def(rc_config_value)
/* Global for caching the strings loaded from rc.conf to avoid reparsing for
* each rc_conf_value call */
static RC_STRINGLIST *rc_conf = NULL;
char *
rc_conf_value(const char *setting)
{
RC_STRINGLIST *old;
RC_STRING *s;
char *p;
if (! rc_conf) {
rc_conf = rc_config_load(RC_CONF);
#ifdef DEBUG_MEMORY
atexit(_free_rc_conf);
#endif
/* Support old configs */
if (exists(RC_CONF_OLD)) {
old = rc_config_load(RC_CONF_OLD);
TAILQ_CONCAT(rc_conf, old, entries);
#ifdef DEBUG_MEMORY
free(old);
#endif
}
/* Convert old uppercase to lowercase */
TAILQ_FOREACH(s, rc_conf, entries) {
p = s->value;
while (p && *p && *p != '=') {
if (isupper((unsigned char)*p))
*p = tolower((unsigned char)*p);
p++;
}
}
}
return rc_config_value(rc_conf, setting);
}
librc_hidden_def(rc_conf_value)
#ifdef DEBUG_MEMORY
static void
_free_rc_conf(void)
{
rc_stringlist_free(rc_conf);
}
#endif

View File

@ -72,6 +72,7 @@
#define librc_hidden_proto(x) hidden_proto(x)
#define librc_hidden_def(x) hidden_def(x)
librc_hidden_proto(rc_conf_value)
librc_hidden_proto(rc_config_list)
librc_hidden_proto(rc_config_load)
librc_hidden_proto(rc_config_value)

View File

@ -43,6 +43,14 @@ __BEGIN_DECLS
#define RC_CONFDIR RC_SYSCONFDIR "/conf.d"
#define RC_PLUGINDIR RC_LIBDIR "/plugins"
#define RC_PROFILE_ENV RC_SYSCONFDIR "/profile.env"
#define RC_SYS_WHITELIST RC_LIBEXECDIR "/conf.d/env_whitelist"
#define RC_USR_WHITELIST RC_SYSCONFDIR "/conf.d/env_whitelist"
#define RC_CONF RC_SYSCONFDIR "/rc.conf"
#define RC_CONF_OLD RC_SYSCONFDIR "/conf.d/rc"
#define RC_PATH_PREFIX RC_LIBEXECDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"
/* PKG_PREFIX is where packages are installed if different from the base OS
* On Gentoo this is normally unset, on FreeBSD /usr/local and on NetBSD
* /usr/pkg. */
@ -462,6 +470,9 @@ RC_STRINGLIST *rc_config_load(const char *);
/*! Return the value of the entry from a key=value list. */
char *rc_config_value(RC_STRINGLIST *, const char *);
/*! Return the value of the entry from rc.conf. */
char *rc_conf_value(const char *);
/*! Check if a variable is a boolean and return its value.
* If variable is not a boolean then we set errno to be ENOENT when it does
* not exist or EINVAL if it's not a boolean.

View File

@ -1,5 +1,6 @@
RC_1.0 {
global:
rc_conf_value;
rc_config_list;
rc_config_load;
rc_config_value;

View File

@ -52,62 +52,8 @@
#include "rc-misc.h"
#include "version.h"
#define PROFILE_ENV RC_SYSCONFDIR "/profile.env"
#define SYS_WHITELIST RC_LIBEXECDIR "/conf.d/env_whitelist"
#define USR_WHITELIST RC_SYSCONFDIR "/conf.d/env_whitelist"
#define RC_CONF RC_SYSCONFDIR "/rc.conf"
#define RC_CONF_OLD RC_SYSCONFDIR "/conf.d/rc"
#define PATH_PREFIX RC_LIBEXECDIR "/bin:/bin:/sbin:/usr/bin:/usr/sbin"
static RC_STRINGLIST *rc_conf = NULL;
extern char **environ;
#ifdef DEBUG_MEMORY
static void
_free_rc_conf(void)
{
rc_stringlist_free(rc_conf);
}
#endif
char *
rc_conf_value(const char *setting)
{
RC_STRINGLIST *old;
RC_STRING *s;
char *p;
if (! rc_conf) {
rc_conf = rc_config_load(RC_CONF);
#ifdef DEBUG_MEMORY
atexit(_free_rc_conf);
#endif
/* Support old configs */
if (exists(RC_CONF_OLD)) {
old = rc_config_load(RC_CONF_OLD);
TAILQ_CONCAT(rc_conf, old, entries);
#ifdef DEBUG_MEMORY
free(old);
#endif
}
/* Convert old uppercase to lowercase */
TAILQ_FOREACH(s, rc_conf, entries) {
p = s->value;
while (p && *p && *p != '=') {
if (isupper((unsigned char)*p))
*p = tolower((unsigned char)*p);
p++;
}
}
}
return rc_config_value(rc_conf, setting);
}
bool
rc_conf_yesno(const char *setting)
{
@ -135,7 +81,7 @@ env_filter(void)
/* Add the user defined list of vars */
env_allow = rc_stringlist_split(rc_conf_value("rc_env_allow"), " ");
profile = rc_config_load(PROFILE_ENV);
profile = rc_config_load(RC_PROFILE_ENV);
/* Copy the env and work from this so we can manipulate it safely */
env_list = rc_stringlist_new();
@ -181,7 +127,7 @@ env_filter(void)
void
env_config(void)
{
size_t pplen = strlen(PATH_PREFIX);
size_t pplen = strlen(RC_PATH_PREFIX);
char *path;
char *p;
char *e;
@ -199,16 +145,16 @@ env_config(void)
for a little extra security */
path = getenv("PATH");
if (! path)
setenv("PATH", PATH_PREFIX, 1);
else if (strncmp (PATH_PREFIX, path, pplen) != 0) {
setenv("PATH", RC_PATH_PREFIX, 1);
else if (strncmp (RC_PATH_PREFIX, path, pplen) != 0) {
l = strlen(path) + pplen + 3;
e = p = xmalloc(sizeof(char) * l);
p += snprintf(p, l, "%s", PATH_PREFIX);
p += snprintf(p, l, "%s", RC_PATH_PREFIX);
/* Now go through the env var and only add bits not in our
* PREFIX */
while ((token = strsep(&path, ":"))) {
np = npp = xstrdup(PATH_PREFIX);
np = npp = xstrdup(RC_PATH_PREFIX);
while ((tok = strsep(&npp, ":")))
if (strcmp(tok, token) == 0)
break;

View File

@ -1,3 +1,4 @@
rc_conf_value
rc_config_list
rc_config_load
rc_config_value

View File

@ -1,3 +1,5 @@
rc_conf_value
rc_conf_value@@RC_1.0
rc_config_list
rc_config_list@@RC_1.0
rc_config_load