Cleanup of command_startup().
[pwmd.git] / src / cache.c
blob4905fc2e77e24da58bd32f1a4580ea57e6246299
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 gboolean reset_entry(file_cache_t *p)
186 if (p && !p->refcount) {
187 free_entry(p);
188 return TRUE;
190 else if (p)
191 memset(p->key, 0, sizeof(p->key));
193 return FALSE;
196 gboolean cache_clear(const guchar *md5file, gint which)
198 file_cache_t *p = md5file ? get_entry(md5file) : NULL;
199 gint t, i;
201 if (which == 1) {
202 if (!p)
203 return FALSE;
205 reset_entry(p);
206 send_status_all(STATUS_CACHE);
207 return TRUE;
210 again:
211 for (i = 0, t = g_slist_length(key_cache); i < t; i++) {
212 p = g_slist_nth_data(key_cache, i);
214 if (reset_entry(p))
215 goto again;
218 send_status_all(STATUS_CACHE);
219 return TRUE;
222 gboolean cache_iscached(const guchar *md5file)
224 file_cache_t *p = get_entry(md5file);
226 return p ? valid_key(p->key, sizeof(p->key)) : FALSE;
229 gboolean cache_reset_timeout(const guchar *md5file, glong timeout)
231 file_cache_t *p = get_entry(md5file);
233 if (!p)
234 return FALSE;
236 if (p->reset == -2)
237 return cache_set_timeout(md5file, timeout);
239 return cache_set_timeout(md5file, p->reset);
242 gboolean cache_set_timeout(const guchar *md5file, glong timeout)
244 file_cache_t *p = get_entry(md5file);
246 if (!p)
247 return FALSE;
249 if (timeout == -2)
250 p->timeout = p->reset;
251 else
252 p->reset = p->timeout = timeout;
254 return TRUE;
257 gboolean cache_update_key(const guchar *md5file, const guchar *shakey)
259 file_cache_t *p = get_entry(md5file);
261 if (!p)
262 return cache_add_file(md5file, shakey);
264 if (p->reset)
265 memcpy(p->key, shakey, sizeof(p->key));
267 return TRUE;
270 gboolean cache_get_key(const guchar *md5file, guchar *shakey)
272 file_cache_t *p = get_entry(md5file);
274 if (!p || valid_key(p->key, sizeof(p->key)) == FALSE)
275 return FALSE;
277 memcpy(shakey, p->key, sizeof(p->key));
278 return TRUE;
281 gboolean cache_has_file(const guchar *md5file)
283 file_cache_t *p = get_entry(md5file);
285 return p ? TRUE : FALSE;
288 gboolean cache_decr_refcount(const guchar *md5file)
290 file_cache_t *p = get_entry(md5file);
292 if (!p)
293 return FALSE;
295 p->refcount--;
297 if (p->refcount < 0)
298 p->refcount = 0;
300 return TRUE;
303 gboolean cache_incr_refcount(const guchar *md5file)
305 file_cache_t *p = get_entry(md5file);
307 if (!p)
308 return FALSE;
310 p->refcount++;
311 return TRUE;