X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;ds=sidebyside;f=src%2Fcrypt_reencrypt.c;h=642b80495486cf4a9f4224bd3b3296cfb3021ab3;hb=32c2bd422225f8015bf6435382c6c85af6287863;hp=3154d59fb5e8a97ad28dc999095ee8729283215d;hpb=8984e4741407b0bcf19ece1860ceeba09d183b93;p=platform%2Fupstream%2Fcryptsetup.git diff --git a/src/crypt_reencrypt.c b/src/crypt_reencrypt.c index 3154d59..642b804 100644 --- a/src/crypt_reencrypt.c +++ b/src/crypt_reencrypt.c @@ -17,6 +17,8 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#define PACKAGE_REENC "crypt_reencrypt" + #define _LARGEFILE64_SOURCE #define _FILE_OFFSET_BITS 64 #define SECTOR_SIZE 512 @@ -99,7 +101,8 @@ int MAGIC_L = 6; typedef enum { MAKE_UNUSABLE, MAKE_USABLE, - CHECK_UNUSABLE + CHECK_UNUSABLE, + CHECK_OPEN, } header_magic; __attribute__((format(printf, 5, 6))) @@ -196,15 +199,27 @@ static int alignment(int fd) return alignment; } -static int device_magic(struct reenc_ctx *rc, header_magic set_magic) +static int device_check(struct reenc_ctx *rc, header_magic set_magic) { char *buf = NULL; int r, devfd; ssize_t s; - devfd = open(rc->device, O_RDWR | O_DIRECT); - if (devfd == -1) - return errno == EBUSY ? -EBUSY : -EINVAL; + devfd = open(rc->device, O_RDWR | O_EXCL | O_DIRECT); + if (devfd == -1) { + if (errno == EBUSY) { + log_err(_("Cannot exclusively open %s, device in use.\n"), + rc->device); + return -EBUSY; + } + log_err(_("Cannot open device %s\n"), rc->device); + return -EINVAL; + } + + if (set_magic == CHECK_OPEN) { + r = 0; + goto out; + } if (posix_memalign((void *)&buf, alignment(devfd), SECTOR_SIZE)) { log_err(_("Allocation of aligned memory failed.\n")); @@ -788,7 +803,7 @@ static int initialize_uuid(struct reenc_ctx *rc) rc->device_uuid = strdup(crypt_get_uuid(cd)); else /* Reencryption already in progress - magic header? */ - r = device_magic(rc, CHECK_UNUSABLE); + r = device_check(rc, CHECK_UNUSABLE); crypt_free(cd); return r; @@ -803,13 +818,18 @@ static int init_passphrase1(struct reenc_ctx *rc, struct crypt_device *cd, retry_count = opt_tries ?: 1; while (retry_count--) { + set_int_handler(); r = crypt_get_key(msg, &rc->p[slot].password, &rc->p[slot].passwordLen, 0, 0, NULL /*opt_key_file*/, 0, 0, cd); if (r < 0) return r; + if (quit) + return -EAGAIN; + /* library uses sigint internally, until it is fixed...*/ + set_int_block(1); r = crypt_activate_by_passphrase(cd, NULL, slot_check, rc->p[slot].password, rc->p[slot].passwordLen, 0); @@ -910,6 +930,9 @@ static int initialize_context(struct reenc_ctx *rc, const char *device) if (!(rc->device = strndup(device, PATH_MAX))) return -ENOMEM; + if (device_check(rc, CHECK_OPEN) < 0) + return -EINVAL; + if (initialize_uuid(rc)) { log_err(_("Device %s is not a valid LUKS device.\n"), device); return -EINVAL; @@ -964,7 +987,8 @@ static void destroy_context(struct reenc_ctx *rc) if ((rc->reencrypt_direction == FORWARD && rc->device_offset == rc->device_size) || - rc->device_offset == 0) { + (rc->reencrypt_direction == BACKWARD && + rc->device_offset == 0)) { unlink(rc->log_file); unlink(rc->header_file_org); unlink(rc->header_file_new); @@ -990,7 +1014,7 @@ static int run_reencrypt(const char *device) if (!rc.in_progress) { if ((r = initialize_passphrase(&rc, rc.device)) || (r = backup_luks_headers(&rc)) || - (r = device_magic(&rc, MAKE_UNUSABLE))) + (r = device_check(&rc, MAKE_UNUSABLE))) goto out; } else { if ((r = initialize_passphrase(&rc, rc.header_file_new))) @@ -1033,7 +1057,7 @@ static void _dbg_version_and_cmd(int argc, const char **argv) { int i; - log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION); + log_std("# %s %s processing \"", PACKAGE_REENC, PACKAGE_VERSION); for (i = 0; i < argc; i++) { if (i) log_std(" "); @@ -1087,16 +1111,13 @@ int main(int argc, const char **argv) poptSetOtherOptionHelp(popt_context, N_("[OPTION...] ]")); - while((r = poptGetNextOpt(popt_context)) > 0) { - if (r < 0) - break; - } - + while((r = poptGetNextOpt(popt_context)) > 0) ; if (r < -1) usage(popt_context, EXIT_FAILURE, poptStrerror(r), poptBadOption(popt_context, POPT_BADOPTION_NOALIAS)); + if (opt_version_mode) { - log_std("%s %s\n", PACKAGE_NAME, PACKAGE_VERSION); + log_std("%s %s\n", PACKAGE_REENC, PACKAGE_VERSION); poptFreeContext(popt_context); exit(EXIT_SUCCESS); } @@ -1137,10 +1158,6 @@ int main(int argc, const char **argv) case -ENODEV: r = 4; break; case -ENOMEM: r = 3; break; case -EPERM: r = 2; break; - case -EAGAIN: - case -EINVAL: - case -ENOENT: - case -ENOSYS: default: r = EXIT_FAILURE; } return r;