Commit d7960b93 authored by Milan Broz's avatar Milan Broz

Simplify global error call.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@622 36d66b0a-2a48-0410-832c-cd162a569da5
parent 4e9fa4d2
......@@ -23,13 +23,6 @@
#define DEFAULT_DISK_ALIGNMENT 1048576 /* 1MiB */
#define DEFAULT_MEM_ALIGNMENT 4096
/* private struct crypt_options flags */
#define CRYPT_FLAG_FREE_DEVICE (1 << 24)
#define CRYPT_FLAG_FREE_CIPHER (1 << 25)
#define CRYPT_FLAG_PRIVATE_MASK ((unsigned int)-1 << 24)
#define at_least(a, b) ({ __typeof__(a) __at_least = (a); (__at_least >= (b))?__at_least:(b); })
struct crypt_device;
......@@ -45,10 +38,6 @@ void crypt_free_volume_key(struct volume_key *vk);
int crypt_confirm(struct crypt_device *cd, const char *msg);
void set_error_va(const char *fmt, va_list va);
void set_error(const char *fmt, ...);
const char *get_error(void);
char *crypt_lookup_dev(const char *dev_id);
int crypt_sysfs_check_crypt_segment(const char *device, uint64_t offset, uint64_t size);
int crypt_sysfs_get_rotational(int major, int minor, int *rotational);
......@@ -73,9 +62,7 @@ void logger(struct crypt_device *cd, int class, const char *file, int line, cons
#define log_dbg(x...) logger(NULL, CRYPT_LOG_DEBUG, __FILE__, __LINE__, x)
#define log_std(c, x...) logger(c, CRYPT_LOG_NORMAL, __FILE__, __LINE__, x)
#define log_verbose(c, x...) logger(c, CRYPT_LOG_VERBOSE, __FILE__, __LINE__, x)
#define log_err(c, x...) do { \
logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x); \
set_error(x); } while(0)
#define log_err(c, x...) logger(c, CRYPT_LOG_ERROR, __FILE__, __LINE__, x)
int crypt_get_debug_level(void);
void debug_processes_using_device(const char *name);
......
......@@ -420,7 +420,6 @@ int dm_create_device(const char *name,
struct dm_task *dmt = NULL;
struct dm_info dmi;
char *params = NULL;
char *error = NULL;
char dev_uuid[DM_UUID_LEN] = {0};
int r = -EINVAL;
uint32_t read_ahead = 0;
......@@ -497,18 +496,9 @@ out:
cookie = 0;
}
if (r < 0 && !reload) {
if (get_error())
error = strdup(get_error());
if (r < 0 && !reload)
dm_remove_device(name, 0, 0);
if (error) {
set_error(error);
free(error);
}
}
out_no_removal:
if (cookie && _dm_use_udev())
(void)_dm_udev_wait(cookie);
......
......@@ -220,7 +220,7 @@ int LUKS_hdr_restore(
buffer_size = hdr_file.payloadOffset << SECTOR_SHIFT;
if (r || buffer_size < LUKS_ALIGN_KEYSLOTS) {
log_err(ctx, _("Backup file do not contain valid LUKS header.\n"));
log_err(ctx, _("Backup file doesn't contain valid LUKS header.\n"));
r = -EINVAL;
goto out;
}
......@@ -288,7 +288,7 @@ int LUKS_hdr_restore(
close(devfd);
/* Be sure to reload new data */
r = LUKS_read_phdr(device, hdr, 0, ctx);
r = LUKS_read_phdr(device, hdr, 1, ctx);
out:
if (devfd != -1)
close(devfd);
......@@ -309,8 +309,6 @@ static int _check_and_convert_hdr(const char *device,
log_dbg("LUKS header not detected.");
if (require_luks_device)
log_err(ctx, _("Device %s is not a valid LUKS device.\n"), device);
else
set_error(_("Device %s is not a valid LUKS device."), device);
r = -EINVAL;
} else if((hdr->version = ntohs(hdr->version)) != 1) { /* Convert every uint16/32_t item from network byte order */
log_err(ctx, _("Unsupported LUKS version %d.\n"), hdr->version);
......@@ -689,8 +687,7 @@ int LUKS_set_key(const char *device, unsigned int keyIndex,
hdr->keyblock[keyIndex].keyMaterialOffset,
ctx);
if (r < 0) {
if(!get_error())
log_err(ctx, _("Failed to write to key storage.\n"));
log_err(ctx, _("Failed to write to key storage.\n"));
goto out;
}
......
......@@ -73,6 +73,10 @@ struct crypt_device {
void *password_usrptr;
};
/* Global error */
/* FIXME: not thread safe, remove this later */
static char global_error[512] = {0};
/* Log helper */
static void (*_default_log)(int level, const char *msg, void *usrptr) = NULL;
static int _debug_level = 0;
......@@ -87,6 +91,7 @@ int crypt_get_debug_level(void)
return _debug_level;
}
void crypt_log(struct crypt_device *cd, int level, const char *msg)
{
if (cd && cd->log)
......@@ -95,6 +100,16 @@ void crypt_log(struct crypt_device *cd, int level, const char *msg)
_default_log(level, msg, NULL);
}
/* Set global error, ugly hack... */
void set_global_error(const char *error)
{
size_t size = strlen(error);
strncpy(global_error, error, sizeof(global_error) - 2);
if (size < sizeof(global_error) && global_error[size - 1] == '\n')
global_error[size - 1] = '\0';
}
__attribute__((format(printf, 5, 6)))
void logger(struct crypt_device *cd, int level, const char *file,
int line, const char *format, ...)
......@@ -104,7 +119,7 @@ void logger(struct crypt_device *cd, int level, const char *file,
va_start(argp, format);
if (vasprintf(&target, format, argp) > 0) {
if (vasprintf(&target, format, argp) > 0 ) {
if (level >= 0) {
crypt_log(cd, level, target);
#ifdef CRYPT_DEBUG
......@@ -114,6 +129,9 @@ void logger(struct crypt_device *cd, int level, const char *file,
} else if (_debug_level)
printf("# %s\n", target);
#endif
if (level == CRYPT_LOG_ERROR)
set_global_error(target);
}
va_end(argp);
......@@ -411,16 +429,15 @@ void crypt_set_password_callback(struct crypt_device *cd,
cd->password_usrptr = usrptr;
}
/* Deprecated global error interface */
void crypt_get_error(char *buf, size_t size)
{
const char *error = get_error();
if (!buf || size < 1)
set_error(NULL);
else if (error) {
strncpy(buf, error, size - 1);
global_error[0] = '\0';
else if (*global_error) {
strncpy(buf, global_error, size - 1);
buf[size - 1] = '\0';
set_error(NULL);
global_error[0] = '\0';
} else
buf[0] = '\0';
}
......
......@@ -38,46 +38,6 @@
#include "libcryptsetup.h"
#include "internal.h"
static char *error=NULL;
__attribute__((format(printf, 1, 0)))
void set_error_va(const char *fmt, va_list va)
{
int r;
if(error) {
free(error);
error = NULL;
}
if(!fmt) return;
r = vasprintf(&error, fmt, va);
if (r < 0) {
free(error);
error = NULL;
return;
}
if (r && error[r - 1] == '\n')
error[r - 1] = '\0';
}
__attribute__((format(printf, 1, 2)))
void set_error(const char *fmt, ...)
{
va_list va;
va_start(va, fmt);
set_error_va(fmt, va);
va_end(va);
}
const char *get_error(void)
{
return error;
}
static int get_alignment(int fd)
{
int alignment = DEFAULT_MEM_ALIGNMENT;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment