CLEARCACHE command fix when no filename was specified.
[pwmd.git] / src / cache.c
blob32df29819372a64d025c0aab5fde036223560186
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2010 Ben Kibbey <bjk@luxsci.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02110-1301 USA
19 #ifdef HAVE_CONFIG_H
20 #include <config.h>
21 #endif
23 #include <stdio.h>
24 #include <unistd.h>
25 #include <glib.h>
26 #include <string.h>
27 #include <sys/mman.h>
28 #include "cache.h"
29 #include "status.h"
30 #include "mem.h"
31 #include "pwmd_error.h"
33 extern void log_write(const gchar *fmt, ...);
35 static gboolean valid_key(const guchar *key, gsize len)
37 gsize b;
39 for (b = 0; b < len; b++) {
40 if (key[b])
41 return TRUE;
44 return FALSE;
47 static file_cache_t *get_entry(const guchar *md5file)
49 gint t = g_slist_length(key_cache);
50 gint i;
52 if (!md5file)
53 return NULL;
55 for (i = 0; i < t; i++) {
56 file_cache_t *p = g_slist_nth_data(key_cache, i);
58 if (!memcmp(p->filename, md5file, sizeof(p->filename)))
59 return p;
62 return NULL;
65 gboolean cache_get_mutex(const guchar *md5file, pth_mutex_t **result)
67 file_cache_t *p = get_entry(md5file);
69 if (p)
70 *result = p->mutex;
72 return p ? TRUE : FALSE;
75 gint cache_file_count()
77 gint t = g_slist_length(key_cache);
78 gint i;
79 gint n = 0;
81 for (i = 0; i < t; i++) {
82 file_cache_t *p = g_slist_nth_data(key_cache, i);
84 if (valid_key(p->key, sizeof(p->key)) == TRUE)
85 n++;
88 return n;
91 void cache_adjust_timer()
93 gint t = g_slist_length(key_cache);
94 gint i;
96 for (i = 0; i < t; i++) {
97 file_cache_t *p = g_slist_nth_data(key_cache, i);
99 if (p->reset >= 0) {
100 if (p->timeout > 0)
101 p->timeout--;
103 if (p->timeout == 0) {
104 if (valid_key(p->key, sizeof(p->key)) == TRUE) {
105 memset(&p->key, 0, sizeof(p->key));
106 send_status_all(STATUS_CACHE);
109 p->timeout = -1;
115 gboolean cache_add_file(const guchar *md5file, const guchar *shakey)
117 file_cache_t *p = g_malloc0(sizeof(file_cache_t));
118 GSList *new;
120 if (!p) {
121 log_write("%s(%i): %s", __FUNCTION__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
122 return FALSE;
125 p->mutex = g_malloc(sizeof(pth_mutex_t));
127 if (!p->mutex) {
128 log_write("%s(%i): %s", __FUNCTION__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
129 g_free(p);
130 return FALSE;
133 pth_mutex_init(p->mutex);
134 memcpy(p->filename, md5file, sizeof(p->filename));
136 if (shakey)
137 memcpy(p->key, shakey, sizeof(p->key));
139 p->reset = p->timeout = -2;
141 if (mlock(p, sysconf(_SC_PAGESIZE)))
142 log_write("mlock(): %s", pwmd_strerror(gpg_error_from_syserror()));
144 new = g_slist_append(key_cache, p);
146 if (!new) {
147 log_write("%s(%i): %s", __FUNCTION__, __LINE__, pwmd_strerror(GPG_ERR_ENOMEM));
148 g_free(p->mutex);
149 memset(p, 0, sizeof(file_cache_t));
150 g_free(p);
151 return FALSE;
154 key_cache = new;
155 return TRUE;
158 static void free_entry(file_cache_t *p)
160 if (!p)
161 return;
163 if (p->mutex)
164 g_free(p->mutex);
166 memset(p, 0, sizeof(file_cache_t));
167 munlock(p, sysconf(_SC_PAGESIZE));
168 key_cache = g_slist_remove(key_cache, p);
169 g_free(p);
172 void cache_free()
174 for (;;) {
175 file_cache_t *p = g_slist_nth_data(key_cache, 0);
177 if (!p)
178 break;
180 free_entry(p);
184 static void reset_entry(file_cache_t *p)
186 if (p && !p->refcount)
187 free_entry(p);
188 else if (p)
189 memset(p->key, 0, sizeof(p->key));
192 gboolean cache_clear(const guchar *md5file, gint which)
194 file_cache_t *p = md5file ? get_entry(md5file) : NULL;
196 if (which == 1) {
197 if (!p)
198 return FALSE;
200 reset_entry(p);
201 send_status_all(STATUS_CACHE);
202 return TRUE;
205 do {
206 file_cache_t *p = g_slist_nth_data(key_cache, 0);
208 if (!p)
209 break;
211 reset_entry(p);
212 } while (cache_file_count());
214 send_status_all(STATUS_CACHE);
215 return TRUE;
218 gboolean cache_iscached(const guchar *md5file)
220 file_cache_t *p = get_entry(md5file);
222 return p ? valid_key(p->key, sizeof(p->key)) : FALSE;
225 gboolean cache_reset_timeout(const guchar *md5file, glong timeout)
227 file_cache_t *p = get_entry(md5file);
229 if (!p)
230 return FALSE;
232 if (p->reset == -2)
233 return cache_set_timeout(md5file, timeout);
235 return cache_set_timeout(md5file, p->reset);
238 gboolean cache_set_timeout(const guchar *md5file, glong timeout)
240 file_cache_t *p = get_entry(md5file);
242 if (!p)
243 return FALSE;
245 if (timeout == -2)
246 p->timeout = p->reset;
247 else
248 p->reset = p->timeout = timeout;
250 return TRUE;
253 gboolean cache_update_key(const guchar *md5file, const guchar *shakey)
255 file_cache_t *p = get_entry(md5file);
257 if (!p)
258 return cache_add_file(md5file, shakey);
260 if (p->reset)
261 memcpy(p->key, shakey, sizeof(p->key));
263 return TRUE;
266 gboolean cache_get_key(const guchar *md5file, guchar *shakey)
268 file_cache_t *p = get_entry(md5file);
270 if (!p || valid_key(p->key, sizeof(p->key)) == FALSE)
271 return FALSE;
273 memcpy(shakey, p->key, sizeof(p->key));
274 return TRUE;
277 gboolean cache_has_file(const guchar *md5file)
279 file_cache_t *p = get_entry(md5file);
281 return p ? TRUE : FALSE;
284 gboolean cache_decr_refcount(const guchar *md5file)
286 file_cache_t *p = get_entry(md5file);
288 if (!p)
289 return FALSE;
291 p->refcount--;
293 if (p->refcount < 0)
294 p->refcount = 0;
296 return TRUE;
299 gboolean cache_incr_refcount(const guchar *md5file)
301 file_cache_t *p = get_entry(md5file);
303 if (!p)
304 return FALSE;
306 p->refcount++;
307 return TRUE;