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
|
/*
* Copyright (c) 2002-2013 Balabit
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* As an additional exemption you are allowed to compile & link against the
* OpenSSL libraries as published by the OpenSSL project. See the file
* COPYING for details.
*
*/
#include "reloc.h"
#include "cache.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Cache *path_cache;
typedef struct _PathResolver
{
CacheResolver super;
GHashTable *configure_variables;
} PathResolver;
void
path_resolver_add_configure_variable(CacheResolver *s, const gchar *name, const gchar *value)
{
PathResolver *self = (PathResolver *) s;
g_hash_table_insert(self->configure_variables, g_strdup(name), g_strdup(value));
}
static void
path_resolver_populate_configure_variables(PathResolver *self, const gchar *sysprefix)
{
path_resolver_add_configure_variable(&self->super, "${prefix}", sysprefix);
path_resolver_add_configure_variable(&self->super, "${exec_prefix}", SYSLOG_NG_PATH_EXECPREFIX);
path_resolver_add_configure_variable(&self->super, "${libexecdir}", SYSLOG_NG_PATH_LIBEXECDIR);
path_resolver_add_configure_variable(&self->super, "${datarootdir}", SYSLOG_NG_PATH_DATAROOTDIR);
path_resolver_add_configure_variable(&self->super, "${datadir}", SYSLOG_NG_PATH_DATADIR);
path_resolver_add_configure_variable(&self->super, "${pkgdatadir}", SYSLOG_NG_PATH_PKGDATADIR);
path_resolver_add_configure_variable(&self->super, "${localstatedir}", SYSLOG_NG_PATH_LOCALSTATEDIR);
path_resolver_add_configure_variable(&self->super, "${moduledir}", SYSLOG_NG_PATH_MODULEDIR);
}
/* NOTE: we really don't have an easy way to handle errors, as installation
* paths are resolved quite early while syslog-ng starts up. The best is to
* abort, as an error is either a problem in the configure variables OR a
* coding mistake. For now, we desperately try to write an error message to
* stderr and then abort the process. */
#define handle_fatal_error(fmt, ...) \
do { \
fprintf(stderr, fmt, __VA_ARGS__); \
g_assert_not_reached(); \
} while (0)
gpointer
path_resolver_resolve(CacheResolver *s, const gchar *orig)
{
PathResolver *self = (PathResolver *) s;
gchar *subst_start = NULL;
gchar *subst_end = NULL;
gchar *new_value, *prefix, *suffix, *replacement, *value;
gchar *confvar;
gint prefix_len, confvar_len;
/* Find variable enclosed within ${...}, replace with the value in
* configure_variables. Repeat until no more substitutions can be made.
* */
value = g_strdup(orig);
subst_start = strstr(value, "${");
while (subst_start != NULL)
{
prefix = NULL;
prefix_len = 0;
suffix = NULL;
subst_end = strchr(subst_start, '}');
if (subst_end == NULL)
handle_fatal_error("Relocation resolution error: missing '}' in string '%s'. Please re-compile syslog-ng with proper path variables.\n",
value);
/* extract confvar */
confvar_len = (subst_end + 1) - subst_start;
confvar = g_strndup(subst_start, confvar_len);
/* lookup replacement */
replacement = g_hash_table_lookup(self->configure_variables, confvar);
if (replacement == NULL)
handle_fatal_error("Relocation resolution error: Unknown configure variable: '%s' in string '%s'.\nPlease re-compile syslog-ng with proper path variables.\n",
confvar, value);
g_free(confvar);
/* replace confvar with replacement */
suffix = (subst_end + 1);
prefix_len = subst_start - value;
prefix = g_strndup(value, prefix_len);
/* construct the new value */
new_value = g_strconcat(prefix, replacement, suffix, NULL);
g_free(prefix);
/* commit the new value */
g_free(value);
value = new_value;
subst_start = strstr(value, "${");
}
return value;
}
static void
path_resolver_free(CacheResolver *s)
{
PathResolver *self = (PathResolver *) s;
g_hash_table_unref(self->configure_variables);
}
CacheResolver *
path_resolver_new(const gchar *sysprefix)
{
PathResolver *self = g_new0(PathResolver, 1);
self->super.resolve_elem = path_resolver_resolve;
self->super.free_elem = g_free;
self->super.free_fn = path_resolver_free;
self->configure_variables = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
path_resolver_populate_configure_variables(self, sysprefix);
return &self->super;
}
static const gchar *
lookup_sysprefix(void)
{
gchar *v;
v = getenv("SYSLOGNG_PREFIX");
if (v)
return v;
return SYSLOG_NG_PATH_PREFIX;
}
const gchar *
get_installation_path_for(const gchar *template)
{
reloc_init();
return cache_lookup(path_cache, template);
}
gchar *
resolve_path_variables_in_text(const gchar *text)
{
reloc_init();
return cache_resolve(path_cache, text);
}
/* NOTE: to be used in test programs only to override paths to external files */
void
override_installation_path_for(const gchar *template, const gchar *value)
{
reloc_init();
cache_populate(path_cache, template, value);
}
void
reloc_init(void)
{
if (!path_cache)
path_cache = cache_new(path_resolver_new(lookup_sysprefix()));
}
void
reloc_deinit(void)
{
if (path_cache)
{
cache_free(path_cache);
path_cache = NULL;
}
}
|