#include "escape.h"
#include "fileio.h"
#include "log.h"
+#include "main-func.h"
#include "mount-util.h"
#include "parse-util.h"
#include "path-util.h"
#include "string-util.h"
#include "strv.h"
-#include "terminal-util.h"
+#include "pretty-print.h"
#include "util.h"
/* internal helper */
static uint64_t arg_skip = 0;
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:
precheck=
name = name_buffer ? name_buffer : vol;
- if (asprintf(&text, "Please enter passphrase for disk %s!", name) < 0)
+ if (asprintf(&text, "Please enter passphrase for disk %s:", name) < 0)
return log_oom();
id = strjoina("cryptsetup:", disk_path);
assert(strv_length(passwords) == 1);
- if (asprintf(&text, "Please enter passphrase for disk %s! (verification)", name) < 0)
+ if (asprintf(&text, "Please enter passphrase for disk %s (verification):", name) < 0)
return log_oom();
id = strjoina("cryptsetup-verification:", disk_path);
return 0;
}
-int main(int argc, char *argv[]) {
+static int run(int argc, char *argv[]) {
_cleanup_(crypt_freep) struct crypt_device *cd = NULL;
- int r = -EINVAL;
+ int r;
- if (argc <= 1) {
- r = help();
- goto finish;
- }
+ if (argc <= 1)
+ return help();
if (argc < 3) {
log_error("This program requires at least two arguments.");
- goto finish;
+ return -EINVAL;
}
- log_set_target(LOG_TARGET_AUTO);
- log_parse_environment();
- log_open();
+ log_setup_service();
umask(0022);
if (argc < 4) {
log_error("attach requires at least two arguments.");
- goto finish;
+ return -EINVAL;
}
if (argc >= 5 &&
}
if (argc >= 6 && argv[5][0] && !streq(argv[5], "-")) {
- if (parse_options(argv[5]) < 0)
- goto finish;
+ r = parse_options(argv[5]);
+ if (r < 0)
+ return r;
}
/* A delicious drop of snake oil */
r = crypt_init(&cd, arg_header);
} else
r = crypt_init(&cd, argv[3]);
- if (r < 0) {
- log_error_errno(r, "crypt_init() failed: %m");
- goto finish;
- }
+ if (r < 0)
+ return log_error_errno(r, "crypt_init() failed: %m");
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
status = crypt_status(cd, argv[2]);
if (IN_SET(status, CRYPT_ACTIVE, CRYPT_BUSY)) {
log_info("Volume %s already active.", argv[2]);
- r = 0;
- goto finish;
+ return 0;
}
if (arg_readonly)
if (r == -EAGAIN)
continue;
if (r < 0)
- goto finish;
+ return r;
}
if (streq_ptr(arg_type, CRYPT_TCRYPT))
key_file = NULL;
continue;
}
- if (r != -EPERM) {
- log_error_errno(r, "Failed to activate: %m");
- goto finish;
- }
+ if (r != -EPERM)
+ return log_error_errno(r, "Failed to activate: %m");
log_warning("Invalid passphrase.");
}
if (arg_tries != 0 && tries >= arg_tries) {
log_error("Too many attempts; giving up.");
- r = -EPERM;
- goto finish;
+ return -EPERM;
}
} else if (streq(argv[1], "detach")) {
r = crypt_init_by_name(&cd, argv[2]);
if (r == -ENODEV) {
log_info("Volume %s already inactive.", argv[2]);
- r = 0;
- goto finish;
- }
- if (r < 0) {
- log_error_errno(r, "crypt_init_by_name() failed: %m");
- goto finish;
+ return 0;
}
+ if (r < 0)
+ return log_error_errno(r, "crypt_init_by_name() failed: %m");
crypt_set_log_callback(cd, cryptsetup_log_glue, NULL);
r = crypt_deactivate(cd, argv[2]);
- if (r < 0) {
- log_error_errno(r, "Failed to deactivate: %m");
- goto finish;
- }
+ if (r < 0)
+ return log_error_errno(r, "Failed to deactivate: %m");
} else {
log_error("Unknown verb %s.", argv[1]);
- goto finish;
+ return -EINVAL;
}
- r = 0;
-
-finish:
- free(arg_cipher);
- free(arg_hash);
- free(arg_header);
- strv_free(arg_tcrypt_keyfiles);
-
- return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
+ return 0;
}
+
+DEFINE_MAIN_FUNCTION(run);