g13: Move some code to a separate module.

* g13/g13-common.c, g13/g13-common.h: New.
* g13/Makefile.am (g13_SOURCES): Add new files.
* g13/g13.c (g13_errors_seen): Move to g13-common.c.
(cmdline_conttype): New.
(main): Use g13_init_signals and g13_install_emergency_cleanup.
(emergency_cleanup, g13_exit): Move to g13-common.c.
* g13/g13.h: Move OPT and some other code to g13-common.h.

Signed-off-by: Werner Koch <wk@gnupg.org>
This commit is contained in:
Werner Koch 2015-09-14 18:49:32 +02:00
parent 8eb3a1797a
commit 9e65bbd255
No known key found for this signature in database
GPG Key ID: E3FDFF218E45B72B
6 changed files with 190 additions and 101 deletions

View File

@ -30,6 +30,7 @@ AM_CFLAGS = $(LIBGCRYPT_CFLAGS) $(LIBASSUAN_CFLAGS) $(NPTH_CFLAGS)
g13_SOURCES = \
g13.c g13.h \
g13-common.c g13-common.h \
keyblob.h \
utils.c utils.h \
server.c server.h \

86
g13/g13-common.c Normal file
View File

@ -0,0 +1,86 @@
/* g13-common.c - Common code for G13 modules
* Copyright (C) 2009, 2015 Werner Koch
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <errno.h>
#include <assert.h>
#include "g13-common.h"
#include <gcrypt.h>
#include <assuan.h>
#include "i18n.h"
#include "sysutils.h"
/* Global variable to keep an error count. */
int g13_errors_seen = 0;
/* Note: This function is used by signal handlers!. */
static void
emergency_cleanup (void)
{
gcry_control (GCRYCTL_TERM_SECMEM);
}
/* Wrapper around gnupg_init_signals. */
void
g13_init_signals (void)
{
gnupg_init_signals (0, emergency_cleanup);
}
/* Install a regular exit handler to make real sure that the secure
memory gets wiped out. */
void
g13_install_emergency_cleanup (void)
{
if (atexit (emergency_cleanup))
{
log_error ("atexit failed\n");
g13_exit (2);
}
}
/* Use this function instead of exit() in all g13 modules. */
void
g13_exit (int rc)
{
gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
if (opt.debug & DBG_MEMSTAT_VALUE)
{
gcry_control( GCRYCTL_DUMP_MEMORY_STATS );
gcry_control( GCRYCTL_DUMP_RANDOM_STATS );
}
if (opt.debug)
gcry_control (GCRYCTL_DUMP_SECMEM_STATS );
emergency_cleanup ();
rc = rc? rc : log_get_errorcount(0)? 2 : g13_errors_seen? 1 : 0;
exit (rc);
}

93
g13/g13-common.h Normal file
View File

@ -0,0 +1,93 @@
/* g13.h - Global definitions for G13.
* Copyright (C) 2009 Free Software Foundation, Inc.
* Copyright (C) 2009, 2015 Werner Koch.
*
* This file is part of GnuPG.
*
* GnuPG is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GnuPG is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
#ifndef G13_COMMON_H
#define G13_COMMON_H
#ifdef GPG_ERR_SOURCE_DEFAULT
#error GPG_ERR_SOURCE_DEFAULT already defined
#endif
#define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_G13
#include <gpg-error.h>
#include "../common/util.h"
#include "../common/status.h"
#include "../common/session-env.h"
/* Debug values and macros. */
#define DBG_MOUNT_VALUE 1 /* Debug mount or device stuff. */
#define DBG_CRYPTO_VALUE 4 /* Debug low level crypto. */
#define DBG_MEMORY_VALUE 32 /* Debug memory allocation stuff. */
#define DBG_MEMSTAT_VALUE 128 /* Show memory statistics. */
#define DBG_IPC_VALUE 1024 /* Debug assuan communication. */
#define DBG_MOUNT (opt.debug & DBG_MOUNT_VALUE)
#define DBG_CRYPTO (opt.debug & DBG_CRYPTO_VALUE)
#define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE)
#define DBG_IPC (opt.debug & DBG_IPC_VALUE)
/* A large struct named "opt" to keep global flags. Note that this
struct is used by g13 and g13-syshelp and thus some fields may only
make sense for one of them. */
struct
{
unsigned int debug; /* Debug flags (DBG_foo_VALUE). */
int verbose; /* Verbosity level. */
int quiet; /* Be as quiet as possible. */
int dry_run; /* Don't change any persistent data. */
const char *homedir; /* Configuration directory name. */
const char *config_filename; /* Name of the used config file. */
/* Filename of the AGENT program. */
const char *agent_program;
/* Filename of the GPG program. Unless set via an program option it
is initialzed at the first engine startup to the standard gpg
filename. */
const char *gpg_program;
/* Environment variables passed along to the engine. */
char *display;
char *ttyname;
char *ttytype;
char *lc_ctype;
char *lc_messages;
char *xauthority;
char *pinentry_user_data;
session_env_t session_env;
/* Name of the output file - FIXME: what is this? */
const char *outfile;
} opt;
/*-- g13-common.c --*/
void g13_init_signals (void);
void g13_install_emergency_cleanup (void);
void g13_exit (int rc);
/*-- server.c and g13-sh-cmd.c --*/
gpg_error_t g13_status (ctrl_t ctrl, int no, ...) GPGRT_ATTR_SENTINEL(0);
#endif /*G13_COMMON_H*/

View File

@ -186,10 +186,6 @@ static struct debug_flags_s debug_flags [] =
/* The timer tick interval used by the idle task. */
#define TIMERTICK_INTERVAL_SEC (1)
/* Global variable to keep an error count. */
int g13_errors_seen = 0;
/* It is possible that we are currently running under setuid permissions. */
static int maybe_setuid = 1;
@ -204,11 +200,14 @@ static int shutdown_pending;
static npth_t idle_task_thread;
/* The container type as specified on the command line. */
static int cmdline_conttype;
static void set_cmd (enum cmd_and_opt_values *ret_cmd,
enum cmd_and_opt_values new_cmd );
static void emergency_cleanup (void);
static void start_idle_task (void);
static void join_idle_task (void);
@ -374,7 +373,7 @@ main ( int argc, char **argv)
may_coredump = disable_core_dumps ();
gnupg_init_signals (0, emergency_cleanup);
g13_init_signals ();
dotlock_create (NULL, 0); /* Register locking cleanup. */
@ -646,13 +645,8 @@ main ( int argc, char **argv)
/* Setup the debug flags for all subsystems. */
set_debug ();
/* Install a regular exit handler to make real sure that the secure
memory gets wiped out. */
if (atexit (emergency_cleanup))
{
log_error ("atexit failed\n");
g13_exit (2);
}
/* Install emergency cleanup handler. */
g13_install_emergency_cleanup ();
/* Terminate if we found any error until now. */
if (log_get_errorcount(0))
@ -761,36 +755,11 @@ main ( int argc, char **argv)
}
/* Note: This function is used by signal handlers!. */
static void
emergency_cleanup (void)
{
gcry_control (GCRYCTL_TERM_SECMEM );
}
void
g13_exit (int rc)
{
gcry_control (GCRYCTL_UPDATE_RANDOM_SEED_FILE);
if (opt.debug & DBG_MEMSTAT_VALUE)
{
gcry_control( GCRYCTL_DUMP_MEMORY_STATS );
gcry_control( GCRYCTL_DUMP_RANDOM_STATS );
}
if (opt.debug)
gcry_control (GCRYCTL_DUMP_SECMEM_STATS );
emergency_cleanup ();
rc = rc? rc : log_get_errorcount(0)? 2 : g13_errors_seen? 1 : 0;
exit (rc);
}
/* Store defaults into the per-connection CTRL object. */
void
g13_init_default_ctrl (struct server_control_s *ctrl)
{
ctrl->conttype = CONTTYPE_ENCFS;
ctrl->conttype = cmdline_conttype? cmdline_conttype : CONTTYPE_ENCFS;
}

View File

@ -20,62 +20,8 @@
#ifndef G13_H
#define G13_H
#ifdef GPG_ERR_SOURCE_DEFAULT
#error GPG_ERR_SOURCE_DEFAULT already defined
#endif
#define GPG_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_G13
#include <gpg-error.h>
#include "g13-common.h"
#include "../common/util.h"
#include "../common/status.h"
#include "../common/session-env.h"
/* A large struct named "opt" to keep global flags. */
struct
{
unsigned int debug; /* Debug flags (DBG_foo_VALUE). */
int verbose; /* Verbosity level. */
int quiet; /* Be as quiet as possible. */
int dry_run; /* Don't change any persistent data. */
const char *homedir; /* Configuration directory name. */
const char *config_filename; /* Name of the used config file. */
/* Filename of the AGENT program. */
const char *agent_program;
/* Filename of the GPG program. Unless set via an program option it
is initialzed at the first engine startup to the standard gpg
filename. */
const char *gpg_program;
/* Environment variables passed along to the engine. */
char *display;
char *ttyname;
char *ttytype;
char *lc_ctype;
char *lc_messages;
char *xauthority;
char *pinentry_user_data;
session_env_t session_env;
/* Name of the output file - FIXME: what is this? */
const char *outfile;
} opt;
/* Debug values and macros. */
#define DBG_MOUNT_VALUE 1 /* Debug mount or device stuff. */
#define DBG_CRYPTO_VALUE 4 /* Debug low level crypto. */
#define DBG_MEMORY_VALUE 32 /* Debug memory allocation stuff. */
#define DBG_MEMSTAT_VALUE 128 /* Show memory statistics. */
#define DBG_IPC_VALUE 1024 /* Debug assuan communication. */
#define DBG_MOUNT (opt.debug & DBG_MOUNT_VALUE)
#define DBG_CRYPTO (opt.debug & DBG_CRYPTO_VALUE)
#define DBG_MEMORY (opt.debug & DBG_MEMORY_VALUE)
#define DBG_IPC (opt.debug & DBG_IPC_VALUE)
/* Forward declaration for an object defined in server.c. */
struct server_local_s;
@ -100,13 +46,7 @@ struct server_control_s
};
/*-- g13.c --*/
void g13_exit (int rc);
void g13_init_default_ctrl (struct server_control_s *ctrl);
/*-- server.c (commonly used, thus declared here) --*/
gpg_error_t g13_status (ctrl_t ctrl, int no, ...) GPGRT_ATTR_SENTINEL(0);
#endif /*G13_H*/

View File

@ -470,7 +470,7 @@ runner_cancel (runner_t runner)
{
runner->canceled = 1; /* Mark that we canceled this one already. */
/* FIXME: This does only work if the thread emits status lines. We
need to change the trhead to wait on an event. */
need to change the thread to wait on an event. */
runner->cancel_flag = 1;
/* For now we use the brutal way and kill the process. */
gnupg_kill_process (runner->pid);