cryptsetup: use static destructor and DEFINE_MAIN_FUNCTION() macro

This commit is contained in:
Yu Watanabe 2018-11-20 17:18:08 +09:00 committed by Zbigniew Jędrzejewski-Szmek
parent 88325bf6e5
commit 3a40f366b2

View file

@ -14,6 +14,7 @@
#include "escape.h" #include "escape.h"
#include "fileio.h" #include "fileio.h"
#include "log.h" #include "log.h"
#include "main-func.h"
#include "mount-util.h" #include "mount-util.h"
#include "parse-util.h" #include "parse-util.h"
#include "path-util.h" #include "path-util.h"
@ -53,6 +54,11 @@ static uint64_t arg_offset = 0;
static uint64_t arg_skip = 0; static uint64_t arg_skip = 0;
static usec_t arg_timeout = USEC_INFINITY; static usec_t arg_timeout = USEC_INFINITY;
STATIC_DESTRUCTOR_REGISTER(arg_cipher, freep);
STATIC_DESTRUCTOR_REGISTER(arg_hash, freep);
STATIC_DESTRUCTOR_REGISTER(arg_header, freep);
STATIC_DESTRUCTOR_REGISTER(arg_tcrypt_keyfiles, strv_freep);
/* Options Debian's crypttab knows we don't: /* Options Debian's crypttab knows we don't:
precheck= precheck=
@ -595,18 +601,16 @@ static int help(void) {
return 0; return 0;
} }
int main(int argc, char *argv[]) { static int run(int argc, char *argv[]) {
_cleanup_(crypt_freep) struct crypt_device *cd = NULL; _cleanup_(crypt_freep) struct crypt_device *cd = NULL;
int r = -EINVAL; int r;
if (argc <= 1) { if (argc <= 1)
r = help(); return help();
goto finish;
}
if (argc < 3) { if (argc < 3) {
log_error("This program requires at least two arguments."); log_error("This program requires at least two arguments.");
goto finish; return -EINVAL;
} }
log_setup_service(); log_setup_service();
@ -624,7 +628,7 @@ int main(int argc, char *argv[]) {
if (argc < 4) { if (argc < 4) {
log_error("attach requires at least two arguments."); log_error("attach requires at least two arguments.");
goto finish; return -EINVAL;
} }
if (argc >= 5 && if (argc >= 5 &&
@ -639,8 +643,9 @@ int main(int argc, char *argv[]) {
} }
if (argc >= 6 && argv[5][0] && !streq(argv[5], "-")) { if (argc >= 6 && argv[5][0] && !streq(argv[5], "-")) {
if (parse_options(argv[5]) < 0) r = parse_options(argv[5]);
goto finish; if (r < 0)
return r;
} }
/* A delicious drop of snake oil */ /* A delicious drop of snake oil */
@ -651,18 +656,15 @@ int main(int argc, char *argv[]) {
r = crypt_init(&cd, arg_header); r = crypt_init(&cd, arg_header);
} else } else
r = crypt_init(&cd, argv[3]); r = crypt_init(&cd, argv[3]);
if (r < 0) { if (r < 0)
log_error_errno(r, "crypt_init() failed: %m"); return log_error_errno(r, "crypt_init() failed: %m");
goto finish;
}
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
status = crypt_status(cd, argv[2]); status = crypt_status(cd, argv[2]);
if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) { if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
log_info("Volume %s already active.", argv[2]); log_info("Volume %s already active.", argv[2]);
r = 0; return 0;
goto finish;
} }
if (arg_readonly) if (arg_readonly)
@ -695,7 +697,7 @@ int main(int argc, char *argv[]) {
if (r == -EAGAIN) if (r == -EAGAIN)
continue; continue;
if (r < 0) if (r < 0)
goto finish; return r;
} }
if (streq_ptr(arg_type, CRYPT_TCRYPT)) if (streq_ptr(arg_type, CRYPT_TCRYPT))
@ -713,18 +715,15 @@ int main(int argc, char *argv[]) {
key_file = NULL; key_file = NULL;
continue; continue;
} }
if (r != -EPERM) { if (r != -EPERM)
log_error_errno(r, "Failed to activate: %m"); return log_error_errno(r, "Failed to activate: %m");
goto finish;
}
log_warning("Invalid passphrase."); log_warning("Invalid passphrase.");
} }
if (arg_tries != 0 && tries >= arg_tries) { if (arg_tries != 0 && tries >= arg_tries) {
log_error("Too many attempts; giving up."); log_error("Too many attempts; giving up.");
r = -EPERM; return -EPERM;
goto finish;
} }
} else if (streq(argv[1], "detach")) { } else if (streq(argv[1], "detach")) {
@ -732,34 +731,23 @@ int main(int argc, char *argv[]) {
r = crypt_init_by_name(&cd, argv[2]); r = crypt_init_by_name(&cd, argv[2]);
if (r == -ENODEV) { if (r == -ENODEV) {
log_info("Volume %s already inactive.", argv[2]); log_info("Volume %s already inactive.", argv[2]);
r = 0; return 0;
goto finish;
}
if (r < 0) {
log_error_errno(r, "crypt_init_by_name() failed: %m");
goto finish;
} }
if (r < 0)
return log_error_errno(r, "crypt_init_by_name() failed: %m");
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL); crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
r = crypt_deactivate(cd, argv[2]); r = crypt_deactivate(cd, argv[2]);
if (r < 0) { if (r < 0)
log_error_errno(r, "Failed to deactivate: %m"); return log_error_errno(r, "Failed to deactivate: %m");
goto finish;
}
} else { } else {
log_error("Unknown verb %s.", argv[1]); log_error("Unknown verb %s.", argv[1]);
goto finish; return -EINVAL;
} }
r = 0; return 0;
finish:
free(arg_cipher);
free(arg_hash);
free(arg_header);
strv_free(arg_tcrypt_keyfiles);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
} }
DEFINE_MAIN_FUNCTION(run);