Try to read first sector from device to properly check that device is ready.

git-svn-id: https://cryptsetup.googlecode.com/svn/trunk@106 36d66b0a-2a48-0410-832c-cd162a569da5
This commit is contained in:
Milan Broz
2009-09-08 10:29:50 +00:00
parent a47856ac49
commit dfadce6d80
4 changed files with 23 additions and 13 deletions

View File

@@ -1,5 +1,6 @@
2009-09-08 Milan Broz <mbroz@redhat.com> 2009-09-08 Milan Broz <mbroz@redhat.com>
* Use dm-uuid for all crypt devices, contains device type and name now. * Use dm-uuid for all crypt devices, contains device type and name now.
* Try to read first sector from device to properly check that device is ready.
2009-09-02 Milan Broz <mbroz@redhat.com> 2009-09-02 Milan Broz <mbroz@redhat.com>
* Add luksSuspend (freeze device and wipe key) and luksResume (with provided passphrase). * Add luksSuspend (freeze device and wipe key) and luksResume (with provided passphrase).

View File

@@ -513,7 +513,7 @@ static int _crypt_init(struct crypt_device **cd,
dm_exit(); dm_exit();
if (r) if (r)
return r; return -EINVAL;
crypt_set_log_callback(*cd, log_wrapper, options->icb->log); crypt_set_log_callback(*cd, log_wrapper, options->icb->log);
crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog); crypt_set_confirm_callback(*cd, yesDialog_wrapper, options->icb->yesDialog);
@@ -526,7 +526,7 @@ static int _crypt_init(struct crypt_device **cd,
if (load && !init_by_name) if (load && !init_by_name)
r = crypt_load(*cd, type, NULL); r = crypt_load(*cd, type, NULL);
if (type && !(*cd)->type) { if (!r && type && !(*cd)->type) {
(*cd)->type = strdup(type); (*cd)->type = strdup(type);
if (!(*cd)->type) if (!(*cd)->type)
r = -ENOMEM; r = -ENOMEM;
@@ -934,7 +934,7 @@ int crypt_init(struct crypt_device **cd, const char *device)
log_dbg("Allocating crypt device %s context.", device); log_dbg("Allocating crypt device %s context.", device);
if (!device_ready(NULL, device, 0)) if (!device_ready(NULL, device, O_RDONLY))
return -ENOTBLK; return -ENOTBLK;
if (!(h = malloc(sizeof(struct crypt_device)))) if (!(h = malloc(sizeof(struct crypt_device))))

View File

@@ -505,27 +505,36 @@ out_err:
int device_ready(struct crypt_device *cd, const char *device, int mode) int device_ready(struct crypt_device *cd, const char *device, int mode)
{ {
int devfd; int devfd, r = 1;
ssize_t s;
struct stat st; struct stat st;
char buf[512];
if(stat(device, &st) < 0) { if(stat(device, &st) < 0) {
log_err(cd, _("Device %s doesn't exist or access denied.\n"), device); log_err(cd, _("Device %s doesn't exist or access denied.\n"), device);
return 0; return 0;
} }
if (!mode) log_dbg("Trying to open and read device %s.", device);
return 1;
devfd = open(device, mode | O_DIRECT | O_SYNC); devfd = open(device, mode | O_DIRECT | O_SYNC);
if(devfd < 0) { if(devfd < 0) {
log_err(cd, _("Can't open device %s for %s%s access.\n"), device, log_err(cd, _("Cannot open device %s for %s%s access.\n"), device,
(mode & O_EXCL) ? _("exclusive ") : "", (mode & O_EXCL) ? _("exclusive ") : "",
(mode & O_RDWR) ? _("writable") : _("read-only")); (mode & O_RDWR) ? _("writable") : _("read-only"));
return 0; return 0;
} }
/* Try to read first sector */
s = read_blockwise(devfd, buf, sizeof(buf));
if (s < 0 || s != sizeof(buf)) {
log_err(cd, _("Cannot read device %s.\n"), device);
r = 0;
}
memset(buf, 0, sizeof(buf));
close(devfd); close(devfd);
return 1; return r;
} }
int get_device_infos(const char *device, struct device_infos *infos, struct crypt_device *cd) int get_device_infos(const char *device, struct device_infos *infos, struct crypt_device *cd)

View File

@@ -133,7 +133,7 @@ static struct interface_callbacks cmd_icb = {
static void show_status(int errcode) static void show_status(int errcode)
{ {
char error[256]; char error[256], *error_;
if(!errcode && opt_verbose) { if(!errcode && opt_verbose) {
log_std(_("Command successful.\n")); log_std(_("Command successful.\n"));
@@ -142,11 +142,11 @@ static void show_status(int errcode)
crypt_get_error(error, sizeof(error)); crypt_get_error(error, sizeof(error));
if (!opt_verbose) { if (!error[0]) {
char *error_ = strerror_r(errcode, error, sizeof(error)); error_ = strerror_r(errcode, error, sizeof(error));
if (error_ != error) { if (error_ != error) {
strncpy(error, error_, sizeof(error)); strncpy(error, error_, sizeof(error));
error[sizeof error - 1] = '\0'; error[sizeof(error) - 1] = '\0';
} }
} }