Fixed empty passwords and iterations == -1 with xml_import() and
[pwmd.git] / src / cache.h
blob62e5bde67e18d5203e3dfed403890d76f9a0bcb6
1 /* vim:tw=78:ts=8:sw=4:set ft=c: */
2 /*
3 Copyright (C) 2006-2007 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 02111-1307 USA
19 #ifndef CACHE_H
20 #define CACHE_H
22 #include <pth.h>
24 #ifdef DEBUG
25 #define CACHE_LOCK_DEBUG log_write(N_("LOCK %s tid=%p"), __FUNCTION__, pth_self());
26 #define CACHE_UNLOCK_DEBUG log_write(N_("UNLOCK %s tid=%p"), __FUNCTION__, pth_self());
27 #else
28 #define CACHE_LOCK_DEBUG
29 #define CACHE_UNLOCK_DEBUG
30 #endif
32 #define CACHE_TRYLOCK(ctx) if (pth_mutex_acquire(&cache_mutex, TRUE, NULL) == FALSE) {\
33 if (errno == EBUSY) { \
34 log_write(N_("%s(): cache mutex LOCKED"), __FUNCTION__); \
35 if (ctx) \
36 assuan_write_status(ctx, "LOCKED", N_("Waiting for lock")); \
37 pth_mutex_acquire(&cache_mutex, FALSE, NULL); \
38 } \
39 else \
40 log_write("%s(%i): %s", __FUNCTION__, __LINE__, strerror(errno)); \
43 #define CACHE_LOCK(ctx) CACHE_LOCK_DEBUG CACHE_TRYLOCK(ctx)
44 #define CACHE_UNLOCK CACHE_UNLOCK_DEBUG pth_mutex_release(&cache_mutex)
46 typedef struct {
47 guchar filename[16]; /* MD5 hash. */
48 guchar key[32]; /* SHA256/AES256/gcrykeysize */
49 gboolean used; /* If this cache slot is used. */
50 glong timeout; /* adjust_cache_time_thread(). */
51 glong reset; /* To reset .timeout. */
52 pth_mutex_t *mutex; /* Not related to cache_mutex. */
53 gint refcount; /* The number of associated clients. */
54 } file_cache_t;
56 void *key_cache;
57 glong cache_size;
58 pth_mutex_t cache_mutex;
60 void cache_adjust_timer(void);
61 gboolean cache_set_timeout(const guchar *md5filename, glong timeout);
62 gboolean cache_reset_timeout(const guchar *md5filename, glong timeout);
63 gboolean cache_iscached(const guchar *md5filename);
64 gboolean cache_clear(const guchar *md5filename, gint which);
65 gboolean cache_add_file(const guchar *md5file, const guchar *shakey);
66 gboolean cache_get_key(const guchar *md5file, guchar *shakey);
67 gboolean cache_update_key(const guchar *md5filename, const guchar *shakey);
68 gint cache_file_count(void);
69 gboolean cache_has_file(const guchar *md5file);
70 void send_cache_status_all(void);
71 gboolean cache_get_mutex(const guchar *md5filename, pth_mutex_t **result);
72 gboolean cache_decr_refcount(const guchar *md5filename);
73 gboolean cache_incr_refcount(const guchar *md5filename);
75 #endif