Fix for deprecations in glib 2.31.0
[platform/upstream/libsecret.git] / egg / egg-libgcrypt.c
1 /* 
2  * gnome-keyring
3  * 
4  * Copyright (C) 2008 Stefan Walter
5  * 
6  * This program is free software; you can redistribute it and/or modify 
7  * it under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *  
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *  
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19  * 02111-1307, USA.  
20  */
21
22 #include "config.h"
23
24 #include "egg-libgcrypt.h"
25 #include "egg-secure-memory.h"
26
27 #include <glib.h>
28
29 #include <gcrypt.h>
30
31 EGG_SECURE_DECLARE (libgcrypt);
32
33 static void
34 log_handler (gpointer unused, int unknown, const gchar *msg, va_list va)
35 {
36         /* TODO: Figure out additional arguments */
37         g_logv ("gcrypt", G_LOG_LEVEL_MESSAGE, msg, va);
38 }
39
40 static int 
41 no_mem_handler (gpointer unused, size_t sz, unsigned int unknown)
42 {
43         /* TODO: Figure out additional arguments */
44         g_error ("couldn't allocate %lu bytes of memory", 
45                  (unsigned long int)sz);
46         return 0;
47 }
48
49 static void
50 fatal_handler (gpointer unused, int unknown, const gchar *msg)
51 {
52         /* TODO: Figure out additional arguments */
53         g_log ("gcrypt", G_LOG_LEVEL_ERROR, "%s", msg);
54 }
55
56 static int
57 glib_thread_mutex_init (void **lock)
58 {
59         *lock = g_slice_new (GMutex);
60         g_mutex_init (*lock);
61         return 0;
62 }
63
64 static int 
65 glib_thread_mutex_destroy (void **lock)
66 {
67         g_mutex_clear (*lock);
68         g_slice_free (GMutex, *lock);
69         return 0;
70 }
71
72 static int 
73 glib_thread_mutex_lock (void **lock)
74 {
75         g_mutex_lock (*lock);
76         return 0;
77 }
78
79 static int 
80 glib_thread_mutex_unlock (void **lock)
81 {
82         g_mutex_unlock (*lock);
83         return 0;
84 }
85
86 static struct gcry_thread_cbs glib_thread_cbs = {
87         GCRY_THREAD_OPTION_USER, NULL,
88         glib_thread_mutex_init, glib_thread_mutex_destroy,
89         glib_thread_mutex_lock, glib_thread_mutex_unlock,
90         NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL 
91 };
92
93 void
94 egg_libgcrypt_initialize (void)
95 {
96         static volatile gsize gcrypt_initialized = 0;
97         unsigned seed;
98
99         if (g_once_init_enter (&gcrypt_initialized)) {
100                 
101                 /* Only initialize libgcrypt if it hasn't already been initialized */
102                 if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
103                         if (g_thread_supported())
104                                 gcry_control (GCRYCTL_SET_THREAD_CBS, &glib_thread_cbs);
105                         gcry_check_version (LIBGCRYPT_VERSION);
106                         gcry_set_log_handler (log_handler, NULL);
107                         gcry_set_outofcore_handler (no_mem_handler, NULL);
108                         gcry_set_fatalerror_handler (fatal_handler, NULL);
109                         gcry_set_allocation_handler ((gcry_handler_alloc_t)g_malloc, 
110                                                      (gcry_handler_alloc_t)egg_secure_alloc, 
111                                                      egg_secure_check, 
112                                                      (gcry_handler_realloc_t)egg_secure_realloc, 
113                                                      egg_secure_free);
114                         gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
115                 }
116                 
117                 gcry_create_nonce (&seed, sizeof (seed));
118                 srand (seed);
119
120                 g_once_init_leave (&gcrypt_initialized, 1);
121         }
122 }