summaryrefslogtreecommitdiff
path: root/module
diff options
context:
space:
mode:
authorStef Walter <stefw@collabora.co.uk>2011-01-30 08:26:23 -0600
committerStef Walter <stefw@collabora.co.uk>2011-01-30 08:26:23 -0600
commit14dfb79ca65dd80e117103c4f8852ae2b4a419a0 (patch)
tree2b568bac82b2dae2299e9030686b7f5c78da7c3d /module
parent4375e297b19bc2177e17cc5616e75d96be053328 (diff)
Configuration tests.
Diffstat (limited to 'module')
-rw-r--r--module/conf.c25
-rw-r--r--module/conf.h6
-rw-r--r--module/p11-kit-lib.c8
3 files changed, 22 insertions, 17 deletions
diff --git a/module/conf.c b/module/conf.c
index a0a3917..1e0d565 100644
--- a/module/conf.c
+++ b/module/conf.c
@@ -54,16 +54,19 @@
#include <unistd.h>
static void
-errmsg (const char *filename, const char* msg, ...)
+errmsg (conf_error_func error_func, const char* msg, ...)
{
#define MAX_MSGLEN 1024
char buf[MAX_MSGLEN];
va_list ap;
+ if (!error_func)
+ return;
+
va_start (ap, msg);
vsnprintf (buf, MAX_MSGLEN, msg, ap);
buf[MAX_MSGLEN - 1] = 0;
- conf_error (filename, buf);
+ error_func (buf);
va_end (ap);
}
@@ -112,7 +115,8 @@ strtrim (char* data)
*/
static char*
-read_config_file (const char* filename, int flags)
+read_config_file (const char* filename, int flags,
+ conf_error_func error_func)
{
char* config = NULL;
FILE* f = NULL;
@@ -129,7 +133,7 @@ read_config_file (const char* filename, int flags)
errno = ENOMEM;
return config;
}
- errmsg (filename, "couldn't open config file: %s", filename);
+ errmsg (error_func, "couldn't open config file: %s", filename);
return NULL;
}
@@ -137,19 +141,19 @@ read_config_file (const char* filename, int flags)
if (fseek (f, 0, SEEK_END) == -1 ||
(len = ftell (f)) == -1 ||
fseek (f, 0, SEEK_SET) == -1) {
- errmsg (filename, "couldn't seek config file: %s", filename);
+ errmsg (error_func, "couldn't seek config file: %s", filename);
return NULL;
}
if ((config = (char*)malloc (len + 2)) == NULL) {
- errmsg (filename, "out of memory");
+ errmsg (error_func, "out of memory");
errno = ENOMEM;
return NULL;
}
/* And read in one block */
if (fread (config, 1, len, f) != len) {
- errmsg (filename, "couldn't read config file: %s", filename);
+ errmsg (error_func, "couldn't read config file: %s", filename);
return NULL;
}
@@ -166,7 +170,8 @@ read_config_file (const char* filename, int flags)
}
hash_t*
-conf_parse_file (const char* filename, int flags)
+conf_parse_file (const char* filename, int flags,
+ conf_error_func error_func)
{
char *name;
char *value;
@@ -178,7 +183,7 @@ conf_parse_file (const char* filename, int flags)
assert (filename);
/* Adds an extra newline to end of file */
- config = read_config_file (filename, flags);
+ config = read_config_file (filename, flags, error_func);
if (!config)
return NULL;
@@ -198,7 +203,7 @@ conf_parse_file (const char* filename, int flags)
/* Look for the break between name = value on the same line */
value = name + strcspn (name, ":=");
if (!*value) {
- errmsg (filename, "%s: invalid config line: %s", filename, name);
+ errmsg (error_func, "%s: invalid config line: %s", filename, name);
errno = EINVAL;
break;
}
diff --git a/module/conf.h b/module/conf.h
index 2a9e2f3..84138d2 100644
--- a/module/conf.h
+++ b/module/conf.h
@@ -42,10 +42,10 @@ enum {
CONF_IGNORE_MISSING = 0x01,
};
-extern void conf_error (const char *filename,
- const char *message);
+typedef void (*conf_error_func) (const char *message);
hash_t* conf_parse_file (const char *filename,
- int flags);
+ int flags,
+ conf_error_func error_func);
#endif /* __CONF_H__ */
diff --git a/module/p11-kit-lib.c b/module/p11-kit-lib.c
index 1b7a99c..a244203 100644
--- a/module/p11-kit-lib.c
+++ b/module/p11-kit-lib.c
@@ -101,8 +101,8 @@ warning (const char* msg, ...)
va_end (va);
}
-void
-conf_error (const char *filename, const char *buffer)
+static void
+conf_error (const char *buffer)
{
/* called from conf.c */
fprintf (stderr, "p11-kit: %s\n", buffer);
@@ -184,7 +184,7 @@ load_module_from_config_unlocked (const char *configfile, const char *name)
if (!module)
return CKR_HOST_MEMORY;
- module->config = conf_parse_file (configfile, 0);
+ module->config = conf_parse_file (configfile, 0, conf_error);
if (!module->config) {
free_module_unlocked (module);
if (errno == ENOMEM)
@@ -335,7 +335,7 @@ load_registered_modules_unlocked (void)
assert (!gl.config);
/* Load the main configuration */
- config = conf_parse_file (PKCS11_CONFIG_FILE, CONF_IGNORE_MISSING);
+ config = conf_parse_file (PKCS11_CONFIG_FILE, CONF_IGNORE_MISSING, conf_error);
if (!config) {
if (errno == ENOMEM)
return CKR_HOST_MEMORY;