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"
27 int opt_batch_mode = 0;
29 /* interrupt handling */
30 volatile int quit = 0;
32 static void int_handler(int sig __attribute__((__unused__)))
37 void set_int_block(int block)
39 sigset_t signals_open;
41 sigemptyset(&signals_open);
42 sigaddset(&signals_open, SIGINT);
43 sigaddset(&signals_open, SIGTERM);
44 sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
47 void set_int_handler(void)
49 struct sigaction sigaction_open;
51 memset(&sigaction_open, 0, sizeof(struct sigaction));
52 sigaction_open.sa_handler = int_handler;
53 sigaction(SIGINT, &sigaction_open, 0);
54 sigaction(SIGTERM, &sigaction_open, 0);
58 __attribute__((format(printf, 5, 6)))
59 void clogger(struct crypt_device *cd, int level, const char *file, int line,
60 const char *format, ...)
65 va_start(argp, format);
67 if (vasprintf(&target, format, argp) > 0) {
69 crypt_log(cd, level, target);
72 printf("# %s:%d %s\n", file ?: "?", line, target);
75 printf("# %s\n", target);
83 void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)))
87 case CRYPT_LOG_NORMAL:
90 case CRYPT_LOG_VERBOSE:
99 printf("# %s\n", msg);
102 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
107 void quiet_log(int level, const char *msg, void *usrptr)
109 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
110 level = CRYPT_LOG_VERBOSE;
111 tool_log(level, msg, usrptr);
114 int yesDialog(const char *msg, void *usrptr __attribute__((unused)))
120 if(isatty(STDIN_FILENO) && !opt_batch_mode) {
121 log_std("\nWARNING!\n========\n");
122 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
123 if(getline(&answer, &size, stdin) == -1) {
128 if(strcmp(answer, "YES\n"))
136 void show_status(int errcode)
138 char error[256], *error_;
144 log_std(_("Command successful.\n"));
148 crypt_get_error(error, sizeof(error));
151 error_ = strerror_r(-errcode, error, sizeof(error));
152 if (error_ != error) {
153 strncpy(error, error_, sizeof(error));
154 error[sizeof(error) - 1] = '\0';
158 log_err(_("Command failed with code %i"), -errcode);
160 log_err(": %s\n", error);
165 const char *uuid_or_device(const char *spec)
167 static char device[PATH_MAX];
169 int i = 0, uuid_len = 5;
171 /* Check if it is correct UUID=<LUKS_UUID> format */
172 if (spec && !strncmp(spec, "UUID=", uuid_len)) {
173 strcpy(device, "/dev/disk/by-uuid/");
174 ptr = &device[strlen(device)];
176 while ((s = spec[i++]) && i < PATH_MAX) {
177 if (!isxdigit(s) && s != '-')
178 return spec; /* Bail it out */
190 __attribute__ ((noreturn)) void usage(poptContext popt_context,
191 int exitcode, const char *error,
194 poptPrintUsage(popt_context, stderr, 0);
196 log_err("%s: %s\n", more, error);
197 poptFreeContext(popt_context);
201 void dbg_version_and_cmd(int argc, const char **argv)
205 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
206 for (i = 0; i < argc; i++) {
209 log_std("%s", argv[i]);
214 /* Translate exit code to simple codes */
215 int translate_errno(int r)
218 case 0: r = EXIT_SUCCESS; break;
220 case -EBUSY: r = 5; break;
222 case -ENODEV: r = 4; break;
223 case -ENOMEM: r = 3; break;
224 case -EPERM: r = 2; break;
228 default: r = EXIT_FAILURE;