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.
7 * Copyright (C) 2009-2012, Milan Broz
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * version 2 as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "cryptsetup.h"
28 int opt_batch_mode = 0;
30 /* interrupt handling */
31 volatile int quit = 0;
32 static int signals_blocked = 0;
34 static void int_handler(int sig __attribute__((__unused__)))
39 int tools_signals_blocked(void)
41 return signals_blocked;
44 void set_int_block(int block)
46 sigset_t signals_open;
48 log_dbg("%slocking interruption on signal.", block ? "B" : "Unb");
50 sigemptyset(&signals_open);
51 sigaddset(&signals_open, SIGINT);
52 sigaddset(&signals_open, SIGTERM);
53 sigprocmask(block ? SIG_SETMASK : SIG_UNBLOCK, &signals_open, NULL);
54 signals_blocked = block;
58 void set_int_handler(int block)
60 struct sigaction sigaction_open;
62 log_dbg("Installing SIGINT/SIGTERM handler.");
63 memset(&sigaction_open, 0, sizeof(struct sigaction));
64 sigaction_open.sa_handler = int_handler;
65 sigaction(SIGINT, &sigaction_open, 0);
66 sigaction(SIGTERM, &sigaction_open, 0);
70 void check_signal(int *r)
76 __attribute__((format(printf, 5, 6)))
77 void clogger(struct crypt_device *cd, int level, const char *file, int line,
78 const char *format, ...)
83 va_start(argp, format);
85 if (vasprintf(&target, format, argp) > 0) {
87 crypt_log(cd, level, target);
90 printf("# %s:%d %s\n", file ?: "?", line, target);
93 printf("# %s\n", target);
101 void tool_log(int level, const char *msg, void *usrptr __attribute__((unused)))
105 case CRYPT_LOG_NORMAL:
108 case CRYPT_LOG_VERBOSE:
112 case CRYPT_LOG_ERROR:
115 case CRYPT_LOG_DEBUG:
117 printf("# %s\n", msg);
120 fprintf(stderr, "Internal error on logging class for msg: %s", msg);
125 void quiet_log(int level, const char *msg, void *usrptr)
127 if (!opt_verbose && (level == CRYPT_LOG_ERROR || level == CRYPT_LOG_NORMAL))
128 level = CRYPT_LOG_VERBOSE;
129 tool_log(level, msg, usrptr);
132 int yesDialog(const char *msg, void *usrptr __attribute__((unused)))
138 block = tools_signals_blocked();
142 if(isatty(STDIN_FILENO) && !opt_batch_mode) {
143 log_std("\nWARNING!\n========\n");
144 log_std("%s\n\nAre you sure? (Type uppercase yes): ", msg);
145 if(getline(&answer, &size, stdin) == -1) {
147 /* Aborted by signal */
149 log_err(_("Error reading response from terminal.\n"));
151 log_dbg("Query interrupted on signal.");
152 } else if(strcmp(answer, "YES\n"))
163 void show_status(int errcode)
165 char error[256], *error_;
171 log_std(_("Command successful.\n"));
175 crypt_get_error(error, sizeof(error));
178 error_ = strerror_r(-errcode, error, sizeof(error));
179 if (error_ != error) {
180 strncpy(error, error_, sizeof(error));
181 error[sizeof(error) - 1] = '\0';
185 log_err(_("Command failed with code %i"), -errcode);
187 log_err(": %s\n", error);
192 const char *uuid_or_device(const char *spec)
194 static char device[PATH_MAX];
196 int i = 0, uuid_len = 5;
198 /* Check if it is correct UUID=<LUKS_UUID> format */
199 if (spec && !strncmp(spec, "UUID=", uuid_len)) {
200 strcpy(device, "/dev/disk/by-uuid/");
201 ptr = &device[strlen(device)];
203 while ((s = spec[i++]) && i < PATH_MAX) {
204 if (!isxdigit(s) && s != '-')
205 return spec; /* Bail it out */
217 __attribute__ ((noreturn)) void usage(poptContext popt_context,
218 int exitcode, const char *error,
221 poptPrintUsage(popt_context, stderr, 0);
223 log_err("%s: %s\n", more, error);
224 poptFreeContext(popt_context);
228 void dbg_version_and_cmd(int argc, const char **argv)
232 log_std("# %s %s processing \"", PACKAGE_NAME, PACKAGE_VERSION);
233 for (i = 0; i < argc; i++) {
236 log_std("%s", argv[i]);
241 /* Translate exit code to simple codes */
242 int translate_errno(int r)
245 case 0: r = EXIT_SUCCESS; break;
247 case -EBUSY: r = 5; break;
249 case -ENODEV: r = 4; break;
250 case -ENOMEM: r = 3; break;
251 case -EPERM: r = 2; break;
255 default: r = EXIT_FAILURE;