Commit 789f1693 authored by Thomas Guillem's avatar Thomas Guillem

keystore/memory: remove locked singleton

parent 3078d5de
...@@ -66,17 +66,6 @@ struct vlc_keystore_sys ...@@ -66,17 +66,6 @@ struct vlc_keystore_sys
bool b_error; bool b_error;
}; };
static struct
{
vlc_mutex_t lock;
unsigned int i_ref_count;
vlc_keystore_sys * p_sys;
} instance = {
.lock = VLC_STATIC_MUTEX,
.i_ref_count = 0,
.p_sys = NULL
};
static const char *const ppsz_keys[] = { static const char *const ppsz_keys[] = {
"protocol", "protocol",
"user", "user",
...@@ -351,11 +340,8 @@ static int ...@@ -351,11 +340,8 @@ static int
Store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], Store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
const uint8_t *p_secret, size_t i_secret_len, const char *psz_label) const uint8_t *p_secret, size_t i_secret_len, const char *psz_label)
{ {
vlc_mutex_lock(&instance.lock);
(void) psz_label; (void) psz_label;
vlc_keystore_sys *p_sys = p_keystore->p_sys; vlc_keystore_sys *p_sys = p_keystore->p_sys;
assert(p_sys == instance.p_sys);
struct list *p_list = &p_sys->list; struct list *p_list = &p_sys->list;
vlc_keystore_entry *p_entry = list_get_entry(p_list, ppsz_values, NULL); vlc_keystore_entry *p_entry = list_get_entry(p_list, ppsz_values, NULL);
...@@ -365,31 +351,22 @@ Store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], ...@@ -365,31 +351,22 @@ Store(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
{ {
p_entry = list_new_entry(p_list); p_entry = list_new_entry(p_list);
if (!p_entry) if (!p_entry)
goto error; return VLC_EGENERIC;
} }
if (values_copy((const char **)p_entry->ppsz_values, ppsz_values)) if (values_copy((const char **)p_entry->ppsz_values, ppsz_values))
goto error; return VLC_EGENERIC;
if (vlc_keystore_entry_set_secret(p_entry, p_secret, i_secret_len)) if (vlc_keystore_entry_set_secret(p_entry, p_secret, i_secret_len))
goto error; return VLC_EGENERIC;
int i_ret = list_save(p_sys, &p_sys->list);
vlc_mutex_unlock(&instance.lock);
return i_ret;
error: return list_save(p_sys, &p_sys->list);
vlc_mutex_unlock(&instance.lock);
return VLC_EGENERIC;
} }
static unsigned int static unsigned int
Find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], Find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
vlc_keystore_entry **pp_entries) vlc_keystore_entry **pp_entries)
{ {
vlc_mutex_lock(&instance.lock);
vlc_keystore_sys *p_sys = p_keystore->p_sys; vlc_keystore_sys *p_sys = p_keystore->p_sys;
assert(p_sys == instance.p_sys);
struct list *p_list = &p_sys->list; struct list *p_list = &p_sys->list;
struct list out_list = { 0 }; struct list out_list = { 0 };
vlc_keystore_entry *p_entry; vlc_keystore_entry *p_entry;
...@@ -416,17 +393,13 @@ Find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX], ...@@ -416,17 +393,13 @@ Find(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX],
*pp_entries = out_list.p_entries; *pp_entries = out_list.p_entries;
vlc_mutex_unlock(&instance.lock);
return out_list.i_count; return out_list.i_count;
} }
static unsigned int static unsigned int
Remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX]) Remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
{ {
vlc_mutex_lock(&instance.lock);
vlc_keystore_sys *p_sys = p_keystore->p_sys; vlc_keystore_sys *p_sys = p_keystore->p_sys;
assert(p_sys == instance.p_sys);
struct list *p_list = &p_sys->list; struct list *p_list = &p_sys->list;
vlc_keystore_entry *p_entry; vlc_keystore_entry *p_entry;
unsigned i_index = 0, i_count = 0; unsigned i_index = 0, i_count = 0;
...@@ -438,18 +411,17 @@ Remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX]) ...@@ -438,18 +411,17 @@ Remove(vlc_keystore *p_keystore, const char *const ppsz_values[KEY_MAX])
} }
if (list_save(p_sys, &p_sys->list) != VLC_SUCCESS) if (list_save(p_sys, &p_sys->list) != VLC_SUCCESS)
{
vlc_mutex_unlock(&instance.lock);
return 0; return 0;
}
vlc_mutex_unlock(&instance.lock);
return i_count; return i_count;
} }
static void static void
CleanUp(vlc_keystore_sys *p_sys) Close(vlc_object_t *p_this)
{ {
vlc_keystore *p_keystore = (vlc_keystore *)p_this;
vlc_keystore_sys *p_sys = p_keystore->p_sys;
if (p_sys->p_file) if (p_sys->p_file)
{ {
if (p_sys->b_error) if (p_sys->b_error)
...@@ -476,84 +448,55 @@ Open(vlc_object_t *p_this) ...@@ -476,84 +448,55 @@ Open(vlc_object_t *p_this)
vlc_keystore *p_keystore = (vlc_keystore *)p_this; vlc_keystore *p_keystore = (vlc_keystore *)p_this;
vlc_keystore_sys *p_sys; vlc_keystore_sys *p_sys;
vlc_mutex_lock(&instance.lock); char *psz_file = var_InheritString(p_this, "keystore-file");
if (!psz_file)
return VLC_EGENERIC;
/* The p_sys context is shared and protected between all threads */ p_keystore->p_sys = p_sys = calloc(1, sizeof(vlc_keystore_sys));
if (instance.i_ref_count == 0) if (!p_sys)
{ {
char *psz_file = var_InheritString(p_this, "keystore-file"); free(psz_file);
if (!psz_file) return VLC_EGENERIC;
return VLC_EGENERIC; }
p_sys = calloc(1, sizeof(vlc_keystore_sys));
if (!p_sys)
{
free(psz_file);
return VLC_EGENERIC;
}
p_sys->psz_file = psz_file; p_sys->psz_file = psz_file;
p_sys->p_file = vlc_fopen(p_sys->psz_file, "a+"); p_sys->p_file = vlc_fopen(p_sys->psz_file, "a+");
p_sys->i_fd = -1; p_sys->i_fd = -1;
if (!p_sys->p_file) if (!p_sys->p_file)
{ {
CleanUp(p_sys); Close(p_this);
return VLC_EGENERIC; return VLC_EGENERIC;
} }
int i_fd = fileno(p_sys->p_file); int i_fd = fileno(p_sys->p_file);
if (i_fd == -1) if (i_fd == -1)
{ {
CleanUp(p_sys); Close(p_this);
return VLC_EGENERIC; return VLC_EGENERIC;
} }
/* Fail if an other LibVLC process acquired the file lock. /* Fail if an other LibVLC process acquired the file lock.
* If HAVE_FLOCK is not defined, the running OS is most likely Windows * If HAVE_FLOCK is not defined, the running OS is most likely Windows
* and a lock was already acquired when the file was opened. */ * and a lock was already acquired when the file was opened. */
#ifdef HAVE_FLOCK #ifdef HAVE_FLOCK
if (flock(i_fd, LOCK_EX|LOCK_NB) != 0) if (flock(i_fd, LOCK_EX|LOCK_NB) != 0)
{ {
CleanUp(p_sys); Close(p_this);
return VLC_EGENERIC; return VLC_EGENERIC;
} }
#endif #endif
p_sys->i_fd = i_fd; p_sys->i_fd = i_fd;
if (list_read(p_sys, &p_sys->list) != VLC_SUCCESS) if (list_read(p_sys, &p_sys->list) != VLC_SUCCESS)
{ {
CleanUp(p_sys); Close(p_this);
return VLC_EGENERIC; return VLC_EGENERIC;
}
instance.p_sys = p_sys;
} }
else
p_sys = instance.p_sys;
instance.i_ref_count++;
p_keystore->p_sys = p_sys;
p_keystore->pf_store = Store; p_keystore->pf_store = Store;
p_keystore->pf_find = Find; p_keystore->pf_find = Find;
p_keystore->pf_remove = Remove; p_keystore->pf_remove = Remove;
vlc_mutex_unlock(&instance.lock);
return VLC_SUCCESS; return VLC_SUCCESS;
} }
static void
Close(vlc_object_t *p_this)
{
(void) p_this;
vlc_mutex_lock(&instance.lock);
assert(((vlc_keystore *)p_this)->p_sys == instance.p_sys);
if (--instance.i_ref_count == 0)
{
CleanUp(instance.p_sys);
instance.p_sys = NULL;
}
vlc_mutex_unlock(&instance.lock);
}
...@@ -47,7 +47,7 @@ static const struct ...@@ -47,7 +47,7 @@ static const struct
bool b_test_default; bool b_test_default;
} keystore_args[] = } keystore_args[] =
{ {
{ "memory", true }, { "memory", false },
/* Following keystores are tested only when asked explicitly by the tester /* Following keystores are tested only when asked explicitly by the tester
* (with "-a" argv) */ * (with "-a" argv) */
{ "secret", false }, { "secret", false },
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment