2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 2012 Oracle and/or its affiliates. All rights reserved.
6 * $Id: db_log_verify.c,v 0f73af5ae3da 2010/05/10 05:38:40 alexander $
12 #define MB 1024 * 1024
14 int main __P((int, char *[]));
15 int lsn_arg __P((char *, DB_LSN *));
16 int usage __P((void));
17 int version_check __P((void));
18 int db_log_verify_app_record __P((DB_ENV *, DBT *, DB_LSN *, db_recops));
30 int ch, cmb, exitval, nflag, rflag, ret, vsn_mismtch;
31 time_t starttime, endtime;
32 char *dbfile, *dbname, *home, *lvhome, *passwd;
33 DB_LOG_VERIFY_CONFIG lvconfig;
36 if ((progname = __db_rpath(argv[0])) == NULL)
41 if ((ret = version_check()) != 0)
45 dbfile = dbname = home = lvhome = passwd = NULL;
46 exitval = nflag = rflag = 0;
47 starttime = endtime = 0;
51 memset(&lvconfig, 0, sizeof(lvconfig));
53 while ((ch = getopt(argc, argv, "b:cC:d:D:e:h:H:NP:Vvs:z:")) != EOF)
56 /* Don't use getsubopt(3), not all systems have it. */
57 if (lsn_arg(optarg, &start))
61 lvconfig.continue_after_fail = 1;
67 lvconfig.cachesize = cmb * MB;
76 /* Don't use getsubopt(3), not all systems have it. */
77 if (lsn_arg(optarg, &stop))
90 if ((ret = __os_strdup(NULL, optarg, &passwd)) != 0) {
91 __db_err(NULL, ret, "__os_strdup: ");
92 return (EXIT_FAILURE);
94 memset(optarg, 0, strlen(optarg));
97 printf("%s\n", db_version(NULL, NULL, NULL));
98 return (EXIT_SUCCESS);
100 lvconfig.verbose = 1;
103 starttime = atoi(optarg);
106 endtime = atoi(optarg);
118 lvconfig.temp_envhome = lvhome;
119 lvconfig.dbfile = dbfile;
120 lvconfig.dbname = dbname;
121 lvconfig.start_lsn = start;
122 lvconfig.end_lsn = stop;
123 lvconfig.start_time = starttime;
124 lvconfig.end_time = endtime;
128 * Create an environment object and initialize it for error
131 if ((ret = db_env_create(&dbenv, 0)) != 0) {
133 "%s: db_env_create: %s\n", progname, db_strerror(ret));
137 dbenv->set_errfile(dbenv, stderr);
138 dbenv->set_errpfx(dbenv, progname);
141 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
142 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
145 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
146 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
151 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
152 passwd, DB_ENCRYPT_AES)) != 0) {
153 dbenv->err(dbenv, ret, "set_passwd");
158 * Set up an app-specific dispatch function so that we can gracefully
159 * handle app-specific log records.
161 if ((ret = dbenv->set_app_dispatch(
162 dbenv, db_log_verify_app_record)) != 0) {
163 dbenv->err(dbenv, ret, "app_dispatch");
168 * An environment is required, but as all we're doing is reading log
169 * files, we create one if it doesn't already exist. If we create
170 * it, create it private so it automatically goes away when we're done.
171 * If we are reading the replication database, do not open the env
172 * with logging, because we don't want to log the opens.
174 if (!vsn_mismtch && (ret = dbenv->open(dbenv, home,
175 DB_USE_ENVIRON, 0)) != 0) {
176 if (dbenv->close(dbenv, 0) != 0) {
183 if (vsn_mismtch && (ret = dbenv->open(dbenv, home, DB_CREATE |
184 DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
185 dbenv->err(dbenv, ret, "DB_ENV->open");
189 /* Handle possible interruptions. */
192 if ((ret = dbenv->log_verify(dbenv, &lvconfig)) != 0)
199 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
202 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
205 /* Resend any caught signal. */
206 __db_util_sigresend();
209 __os_free(NULL, passwd);
211 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
217 fprintf(stderr, "\nusage: %s %s\n%s\n%s\n%s\n%s\n", progname,
219 "[-H temporary environment home for internal use]",
220 "[-P password] [-C cache size in megabytes]",
221 "[-d db file name] [-D db name]",
222 "[-b file/offset] [-e file/offset]",
223 "[-s start time] [-z end time]");
225 return (EXIT_FAILURE);
231 int v_major, v_minor, v_patch;
233 /* Make sure we're loaded with the right version of the DB library. */
234 (void)db_version(&v_major, &v_minor, &v_patch);
235 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
236 fprintf(stderr, DB_STR_A("5003",
237 "%s: version %d.%d doesn't match library version %d.%d\n",
238 "%s %d %d %d %d\n"), progname,
239 DB_VERSION_MAJOR, DB_VERSION_MINOR,
241 return (EXIT_FAILURE);
247 * Print an unknown, application-specific log record as best we can, this is
248 * all we can do to such a log record during the verification. The counting
249 * is done in __db_dispatch because we can't pass the log verify handle into
253 db_log_verify_app_record(dbenv, dbt, lsnp, op)
259 u_int32_t i, len, len2, rectype;
264 DB_ASSERT(dbenv->env, op == DB_TXN_LOG_VERIFY);
265 COMPQUIET(op, DB_TXN_LOG_VERIFY);
268 i = len = len2 = rectype = 0;
272 * Fetch the rectype, which always must be at the beginning of the
273 * record (if dispatching is to work at all).
275 memcpy(&rectype, dbt->data, sizeof(rectype));
278 * Applications may wish to customize the output here based on the
279 * rectype. We just print the entire log record in the generic
280 * mixed-hex-and-printable format we use for binary data.
282 if ((ret = __os_malloc(dbenv->env,
283 len = 256 + 2 * dbt->size, &buf)) != 0)
286 snprintf(buf, len, DB_STR_A("5004",
287 "[%lu][%lu] App-specific log record: %lu\n\tdata: ",
288 "%lu %lu %lu"), (u_long)lsnp->file, (u_long)lsnp->offset,
292 * Each unprintable character takes up several bytes, so be aware of
295 for (i = 0; i < dbt->size && len2 < len; i++) {
296 ch = ((u_int8_t *)dbt->data)[i];
297 len2 = (u_int32_t)strlen(buf);
299 snprintf(p, len - len2 - 1,
300 isprint(ch) || ch == 0x0a ? "%c" : "%#x ", ch);
302 len2 = (u_int32_t)strlen(buf);
304 snprintf(p, len - len2 - 1, "\n\n");
305 __db_msg(dbenv->env, "%s", buf);
307 err: if (buf != NULL)
308 __os_free(dbenv->env, buf);
314 * Parse a LSN argument.
325 * Expected format is: lsn.file/lsn.offset.
327 if ((p = strchr(arg, '/')) == NULL)
331 if (__db_getulong(NULL, progname, arg, 0, UINT32_MAX, &uval))
334 if (__db_getulong(NULL, progname, p + 1, 0, UINT32_MAX, &uval))