| 1 |
/*- |
| 2 |
* See the file LICENSE for redistribution information. |
| 3 |
* |
| 4 |
* Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved. |
| 5 |
* |
| 6 |
* $Id$ |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "db_config.h" |
| 10 |
|
| 11 |
#include "db_int.h" |
| 12 |
|
| 13 |
#ifndef lint |
| 14 |
static const char copyright[] = |
| 15 |
"Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n"; |
| 16 |
#endif |
| 17 |
|
| 18 |
void db_recover_feedback __P((DB_ENV *, int, int)); |
| 19 |
int db_recover_main __P((int, char *[])); |
| 20 |
int db_recover_read_timestamp __P((char *, time_t *)); |
| 21 |
int db_recover_usage __P((void)); |
| 22 |
int db_recover_version_check __P((void)); |
| 23 |
|
| 24 |
const char *progname; |
| 25 |
int newline_needed; |
| 26 |
|
| 27 |
int |
| 28 |
db_recover(args) |
| 29 |
char *args; |
| 30 |
{ |
| 31 |
int argc; |
| 32 |
char **argv; |
| 33 |
|
| 34 |
__db_util_arg("db_recover", args, &argc, &argv); |
| 35 |
return (db_recover_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS); |
| 36 |
} |
| 37 |
|
| 38 |
#include <stdio.h> |
| 39 |
#define ERROR_RETURN ERROR |
| 40 |
|
| 41 |
int |
| 42 |
db_recover_main(argc, argv) |
| 43 |
int argc; |
| 44 |
char *argv[]; |
| 45 |
{ |
| 46 |
extern char *optarg; |
| 47 |
extern int optind, __db_getopt_reset; |
| 48 |
DB_ENV *dbenv; |
| 49 |
time_t timestamp; |
| 50 |
u_int32_t flags; |
| 51 |
int ch, exitval, fatal_recover, ret, retain_env, set_feedback, verbose; |
| 52 |
char *home, *passwd; |
| 53 |
|
| 54 |
if ((progname = __db_rpath(argv[0])) == NULL) |
| 55 |
progname = argv[0]; |
| 56 |
else |
| 57 |
++progname; |
| 58 |
|
| 59 |
if ((ret = db_recover_version_check()) != 0) |
| 60 |
return (ret); |
| 61 |
|
| 62 |
home = passwd = NULL; |
| 63 |
timestamp = 0; |
| 64 |
exitval = fatal_recover = retain_env = set_feedback = verbose = 0; |
| 65 |
__db_getopt_reset = 1; |
| 66 |
while ((ch = getopt(argc, argv, "cefh:P:t:Vv")) != EOF) |
| 67 |
switch (ch) { |
| 68 |
case 'c': |
| 69 |
fatal_recover = 1; |
| 70 |
break; |
| 71 |
case 'e': |
| 72 |
retain_env = 1; |
| 73 |
break; |
| 74 |
case 'f': |
| 75 |
set_feedback = 1; |
| 76 |
break; |
| 77 |
case 'h': |
| 78 |
home = optarg; |
| 79 |
break; |
| 80 |
case 'P': |
| 81 |
if (passwd != NULL) { |
| 82 |
fprintf(stderr, DB_STR("5137", |
| 83 |
"Password may not be specified twice")); |
| 84 |
free(passwd); |
| 85 |
return (EXIT_FAILURE); |
| 86 |
} |
| 87 |
passwd = strdup(optarg); |
| 88 |
memset(optarg, 0, strlen(optarg)); |
| 89 |
if (passwd == NULL) { |
| 90 |
fprintf(stderr, DB_STR_A("5021", |
| 91 |
"%s: strdup: %s\n", "%s %s\n"), |
| 92 |
progname, strerror(errno)); |
| 93 |
return (EXIT_FAILURE); |
| 94 |
} |
| 95 |
break; |
| 96 |
case 't': |
| 97 |
if ((ret = db_recover_read_timestamp(optarg, ×tamp)) != 0) |
| 98 |
return (ret); |
| 99 |
break; |
| 100 |
case 'V': |
| 101 |
printf("%s\n", db_version(NULL, NULL, NULL)); |
| 102 |
return (EXIT_SUCCESS); |
| 103 |
case 'v': |
| 104 |
verbose = 1; |
| 105 |
break; |
| 106 |
case '?': |
| 107 |
default: |
| 108 |
return (db_recover_usage()); |
| 109 |
} |
| 110 |
argc -= optind; |
| 111 |
argv += optind; |
| 112 |
|
| 113 |
if (argc != 0) |
| 114 |
return (db_recover_usage()); |
| 115 |
|
| 116 |
/* Handle possible interruptions. */ |
| 117 |
__db_util_siginit(); |
| 118 |
|
| 119 |
/* |
| 120 |
* Create an environment object and initialize it for error |
| 121 |
* reporting. |
| 122 |
*/ |
| 123 |
if ((ret = db_env_create(&dbenv, 0)) != 0) { |
| 124 |
fprintf(stderr, |
| 125 |
"%s: db_env_create: %s\n", progname, db_strerror(ret)); |
| 126 |
return (EXIT_FAILURE); |
| 127 |
} |
| 128 |
dbenv->set_errfile(dbenv, stderr); |
| 129 |
dbenv->set_errpfx(dbenv, progname); |
| 130 |
if (set_feedback) |
| 131 |
(void)dbenv->set_feedback(dbenv, db_recover_feedback); |
| 132 |
if (verbose) |
| 133 |
(void)dbenv->set_verbose(dbenv, DB_VERB_RECOVERY, 1); |
| 134 |
if (timestamp && |
| 135 |
(ret = dbenv->set_tx_timestamp(dbenv, ×tamp)) != 0) { |
| 136 |
dbenv->err(dbenv, ret, "DB_ENV->set_timestamp"); |
| 137 |
goto err; |
| 138 |
} |
| 139 |
|
| 140 |
if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv, |
| 141 |
passwd, DB_ENCRYPT_AES)) != 0) { |
| 142 |
dbenv->err(dbenv, ret, "set_passwd"); |
| 143 |
goto err; |
| 144 |
} |
| 145 |
|
| 146 |
/* |
| 147 |
* Initialize the environment -- we don't actually do anything |
| 148 |
* else, that all that's needed to run recovery. |
| 149 |
* |
| 150 |
* Note that unless the caller specified the -e option, we use a |
| 151 |
* private environment, as we're about to create a region, and we |
| 152 |
* don't want to to leave it around. If we leave the region around, |
| 153 |
* the application that should create it will simply join it instead, |
| 154 |
* and will then be running with incorrectly sized (and probably |
| 155 |
* terribly small) caches. Applications that use -e should almost |
| 156 |
* certainly use DB_CONFIG files in the directory. |
| 157 |
*/ |
| 158 |
flags = 0; |
| 159 |
LF_SET(DB_CREATE | DB_INIT_LOG | |
| 160 |
DB_INIT_MPOOL | DB_INIT_TXN | DB_USE_ENVIRON); |
| 161 |
LF_SET(fatal_recover ? DB_RECOVER_FATAL : DB_RECOVER); |
| 162 |
LF_SET(retain_env ? DB_INIT_LOCK : DB_PRIVATE); |
| 163 |
if ((ret = dbenv->open(dbenv, home, flags, 0)) != 0) { |
| 164 |
dbenv->err(dbenv, ret, "DB_ENV->open"); |
| 165 |
goto err; |
| 166 |
} |
| 167 |
|
| 168 |
if (0) { |
| 169 |
err: exitval = 1; |
| 170 |
} |
| 171 |
|
| 172 |
/* Flush to the next line of the output device. */ |
| 173 |
if (newline_needed) |
| 174 |
printf("\n"); |
| 175 |
|
| 176 |
/* Clean up the environment. */ |
| 177 |
if ((ret = dbenv->close(dbenv, 0)) != 0) { |
| 178 |
exitval = 1; |
| 179 |
fprintf(stderr, |
| 180 |
"%s: dbenv->close: %s\n", progname, db_strerror(ret)); |
| 181 |
} |
| 182 |
if (passwd != NULL) |
| 183 |
free(passwd); |
| 184 |
|
| 185 |
/* Resend any caught signal. */ |
| 186 |
__db_util_sigresend(); |
| 187 |
|
| 188 |
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 189 |
} |
| 190 |
|
| 191 |
/* |
| 192 |
* db_recover_feedback -- |
| 193 |
* Provide feedback on recovery progress. |
| 194 |
*/ |
| 195 |
void |
| 196 |
db_recover_feedback(dbenv, opcode, percent) |
| 197 |
DB_ENV *dbenv; |
| 198 |
int opcode; |
| 199 |
int percent; |
| 200 |
{ |
| 201 |
COMPQUIET(dbenv, NULL); |
| 202 |
|
| 203 |
if (opcode == DB_RECOVER) { |
| 204 |
printf(DB_STR_A("5022", "\rrecovery %d%% complete", "%d"), |
| 205 |
percent); |
| 206 |
(void)fflush(stdout); |
| 207 |
newline_needed = 1; |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
#define ATOI2(ar) ((ar)[0] - '0') * 10 + ((ar)[1] - '0'); (ar) += 2; |
| 212 |
|
| 213 |
/* |
| 214 |
* read_timestamp -- |
| 215 |
* Convert a time argument to Epoch seconds. |
| 216 |
* |
| 217 |
* Copyright (c) 1993 |
| 218 |
* The Regents of the University of California. All rights reserved. |
| 219 |
* |
| 220 |
* Redistribution and use in source and binary forms, with or without |
| 221 |
* modification, are permitted provided that the following conditions |
| 222 |
* are met: |
| 223 |
* 1. Redistributions of source code must retain the above copyright |
| 224 |
* notice, this list of conditions and the following disclaimer. |
| 225 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 226 |
* notice, this list of conditions and the following disclaimer in the |
| 227 |
* documentation and/or other materials provided with the distribution. |
| 228 |
* 3. Neither the name of the University nor the names of its contributors |
| 229 |
* may be used to endorse or promote products derived from this software |
| 230 |
* without specific prior written permission. |
| 231 |
* |
| 232 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
| 233 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 234 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 235 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 236 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 237 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 238 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 239 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 240 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 241 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 242 |
* SUCH DAMAGE. |
| 243 |
*/ |
| 244 |
int |
| 245 |
db_recover_read_timestamp(arg, timep) |
| 246 |
char *arg; |
| 247 |
time_t *timep; |
| 248 |
{ |
| 249 |
struct tm *t; |
| 250 |
time_t now; |
| 251 |
int yearset; |
| 252 |
char *p; |
| 253 |
/* Start with the current time. */ |
| 254 |
(void)time(&now); |
| 255 |
if ((t = localtime(&now)) == NULL) { |
| 256 |
fprintf(stderr, DB_STR_A("5023", "%s: localtime: %s\n", |
| 257 |
"%s %s\n"), progname, strerror(errno)); |
| 258 |
return (EXIT_FAILURE); |
| 259 |
} |
| 260 |
/* [[CC]YY]MMDDhhmm[.SS] */ |
| 261 |
if ((p = strchr(arg, '.')) == NULL) |
| 262 |
t->tm_sec = 0; /* Seconds defaults to 0. */ |
| 263 |
else { |
| 264 |
if (strlen(p + 1) != 2) |
| 265 |
goto terr; |
| 266 |
*p++ = '\0'; |
| 267 |
t->tm_sec = ATOI2(p); |
| 268 |
} |
| 269 |
|
| 270 |
yearset = 0; |
| 271 |
switch (strlen(arg)) { |
| 272 |
case 12: /* CCYYMMDDhhmm */ |
| 273 |
t->tm_year = ATOI2(arg); |
| 274 |
t->tm_year *= 100; |
| 275 |
yearset = 1; |
| 276 |
/* FALLTHROUGH */ |
| 277 |
case 10: /* YYMMDDhhmm */ |
| 278 |
if (yearset) { |
| 279 |
yearset = ATOI2(arg); |
| 280 |
t->tm_year += yearset; |
| 281 |
} else { |
| 282 |
yearset = ATOI2(arg); |
| 283 |
if (yearset < 69) |
| 284 |
t->tm_year = yearset + 2000; |
| 285 |
else |
| 286 |
t->tm_year = yearset + 1900; |
| 287 |
} |
| 288 |
t->tm_year -= 1900; /* Convert to UNIX time. */ |
| 289 |
/* FALLTHROUGH */ |
| 290 |
case 8: /* MMDDhhmm */ |
| 291 |
t->tm_mon = ATOI2(arg); |
| 292 |
--t->tm_mon; /* Convert from 01-12 to 00-11 */ |
| 293 |
t->tm_mday = ATOI2(arg); |
| 294 |
t->tm_hour = ATOI2(arg); |
| 295 |
t->tm_min = ATOI2(arg); |
| 296 |
break; |
| 297 |
default: |
| 298 |
goto terr; |
| 299 |
} |
| 300 |
|
| 301 |
t->tm_isdst = -1; /* Figure out DST. */ |
| 302 |
|
| 303 |
*timep = mktime(t); |
| 304 |
if (*timep == -1) { |
| 305 |
terr: fprintf(stderr, DB_STR_A("5024", |
| 306 |
"%s: out of range or illegal time specification: [[CC]YY]MMDDhhmm[.SS]", |
| 307 |
"%s"), progname); |
| 308 |
return (EXIT_FAILURE); |
| 309 |
} |
| 310 |
return (0); |
| 311 |
} |
| 312 |
|
| 313 |
int |
| 314 |
db_recover_usage() |
| 315 |
{ |
| 316 |
(void)fprintf(stderr, "usage: %s %s\n", progname, |
| 317 |
"[-cefVv] [-h home] [-P password] [-t [[CC]YY]MMDDhhmm[.SS]]"); |
| 318 |
return (EXIT_FAILURE); |
| 319 |
} |
| 320 |
|
| 321 |
int |
| 322 |
db_recover_version_check() |
| 323 |
{ |
| 324 |
int v_major, v_minor, v_patch; |
| 325 |
|
| 326 |
/* Make sure we're loaded with the right version of the DB library. */ |
| 327 |
(void)db_version(&v_major, &v_minor, &v_patch); |
| 328 |
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { |
| 329 |
fprintf(stderr, DB_STR_A("5025", |
| 330 |
"%s: version %d.%d doesn't match library version %d.%d\n", |
| 331 |
"%s %d %d %d %d\n"), progname, |
| 332 |
DB_VERSION_MAJOR, DB_VERSION_MINOR, |
| 333 |
v_major, v_minor); |
| 334 |
return (EXIT_FAILURE); |
| 335 |
} |
| 336 |
return (0); |
| 337 |
} |