chiark / gitweb /
dirmngr: New debug message on correctly initialized libdns.
[gnupg2.git] / g13 / g13-common.c
1 /* g13-common.c - Common code for G13 modules
2  * Copyright (C) 2009, 2015 Werner Koch
3  *
4  * This file is part of GnuPG.
5  *
6  * GnuPG is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GnuPG is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20
21 #include <config.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdarg.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #include "g13-common.h"
30 #include <gcrypt.h>
31 #include <assuan.h>
32 #include "i18n.h"
33 #include "sysutils.h"
34
35
36
37 /* Global variable to keep an error count. */
38 int g13_errors_seen = 0;
39
40
41
42 /* Note: This function is used by signal handlers!. */
43 static void
44 emergency_cleanup (void)
45 {
46   gcry_control (GCRYCTL_TERM_SECMEM);
47 }
48
49
50 /* Wrapper around gnupg_init_signals.  */
51 void
52 g13_init_signals (void)
53 {
54   gnupg_init_signals (0, emergency_cleanup);
55 }
56
57
58 /* Install a regular exit handler to make real sure that the secure
59    memory gets wiped out.  */
60 void
61 g13_install_emergency_cleanup (void)
62 {
63   if (atexit (emergency_cleanup))
64     {
65       log_error ("atexit failed\n");
66       g13_exit (2);
67     }
68 }
69
70
71 /* Use this function instead of exit() in all g13 modules.  */
72 void
73 g13_exit (int rc)
74 {
75   gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
76   if (opt.debug & DBG_MEMSTAT_VALUE)
77     {
78       gcry_control( GCRYCTL_DUMP_MEMORY_STATS );
79       gcry_control( GCRYCTL_DUMP_RANDOM_STATS );
80     }
81   if (opt.debug)
82     gcry_control (GCRYCTL_DUMP_SECMEM_STATS );
83   emergency_cleanup ();
84   rc = rc? rc : log_get_errorcount(0)? 2 : g13_errors_seen? 1 : 0;
85   exit (rc);
86 }