2 * cryptsetup - setup cryptographic volumes for dm-crypt
4 * Copyright (C) 2004, Christophe Saout <christophe@saout.de>
5 * Copyright (C) 2004-2007, Clemens Fruhwirth <clemens@endorphin.org>
6 * Copyright (C) 2009-2012, Red Hat, Inc. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "cryptsetup.h"
26 int opt_batch_mode = 0;
28 __attribute__((format(printf, 5, 6)))
29 void clogger(struct crypt_device *cd, int level, const char *file, int line,
30 const char *format, ...)
35 va_start(argp, format);
37 if (vasprintf(&target, format, argp) > 0) {
39 crypt_log(cd, level, target);
42 printf("# %s:%d %s\n", file ?: "?", line, target);
45 printf("# %s\n", target);
53 void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)))
57 case CRYPT_LOG_NORMAL:
60 case CRYPT_LOG_VERBOSE:
69 printf("# %s\n", msg);
72 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
77 void quiet_log(int level, const char *msg, void *usrptr)
79 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
80 level = CRYPT_LOG_VERBOSE;
81 tool_log(level, msg, usrptr);
84 int yesDialog(const char *msg, void *usrptr __attribute__((unused)))
90 if(isatty(STDIN_FILENO) && !opt_batch_mode) {
91 log_std("\nWARNING!\n========\n");
92 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
93 if(getline(&answer, &size, stdin) == -1) {
98 if(strcmp(answer, "YES\n"))
106 void show_status(int errcode)
108 char error[256], *error_;
114 log_std(_("Command successful.\n"));
118 crypt_get_error(error, sizeof(error));
121 error_ = strerror_r(-errcode, error, sizeof(error));
122 if (error_ != error) {
123 strncpy(error, error_, sizeof(error));
124 error[sizeof(error) - 1] = '\0';
128 log_err(_("Command failed with code %i"), -errcode);
130 log_err(": %s\n", error);
135 const char *uuid_or_device(const char *spec)
137 static char device[PATH_MAX];
139 int i = 0, uuid_len = 5;
141 /* Check if it is correct UUID=<LUKS_UUID> format */
142 if (spec && !strncmp(spec, "UUID=", uuid_len)) {
143 strcpy(device, "/dev/disk/by-uuid/");
144 ptr = &device[strlen(device)];
146 while ((s = spec[i++]) && i < PATH_MAX) {
147 if (!isxdigit(s) && s != '-')
148 return spec; /* Bail it out */
160 __attribute__ ((noreturn)) void usage(poptContext popt_context,
161 int exitcode, const char *error,
164 poptPrintUsage(popt_context, stderr, 0);
166 log_err("%s: %s\n", more, error);
167 poptFreeContext(popt_context);
171 void dbg_version_and_cmd(int argc, const char **argv)
175 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
176 for (i = 0; i < argc; i++) {
179 log_std("%s", argv[i]);
184 /* Translate exit code to simple codes */
185 int translate_errno(int r)
188 case 0: r = EXIT_SUCCESS; break;
190 case -EBUSY: r = 5; break;
192 case -ENODEV: r = 4; break;
193 case -ENOMEM: r = 3; break;
194 case -EPERM: r = 2; break;
198 default: r = EXIT_FAILURE;