Debian Codegraph @

build_vxworks/util/db_log_verify.c

raw
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: db_log_verify.c,v 0f73af5ae3da 2010/05/10 05:38:40 alexander $
7 */
8 #include "db_config.h"
9
10 #include "db_int.h"
11
12 #define MB 1024 * 1024
13
14 int db_log_verify_main __P((int, char *[]));
15 int db_log_verify_lsn_arg __P((char *, DB_LSN *));
16 int db_log_verify_usage __P((void));
17 int db_log_verify_version_check __P((void));
18 int db_log_verify_app_record __P((DB_ENV *, DBT *, DB_LSN *, db_recops));
19 const char *progname;
20
21 int
22 db_log_verify(args)
23 char *args;
24 {
25 int argc;
26 char **argv;
27
28 __db_util_arg("db_log_verify", args, &argc, &argv);
29 return (db_log_verify_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
30 }
31
32 #include <stdio.h>
33 #define ERROR_RETURN ERROR
34
35 int
36 db_log_verify_main(argc, argv)
37 int argc;
38 char *argv[];
39 {
40 extern char *optarg;
41 extern int optind, __db_getopt_reset;
42 DB_ENV *dbenv;
43 DB_LSN start, stop;
44 int ch, cmb, exitval, nflag, rflag, ret, vsn_mismtch;
45 time_t starttime, endtime;
46 char *dbfile, *dbname, *home, *lvhome, *passwd;
47 DB_LOG_VERIFY_CONFIG lvconfig;
48
49 vsn_mismtch = 0;
50 if ((progname = __db_rpath(argv[0])) == NULL)
51 progname = argv[0];
52 else
53 ++progname;
54
55 if ((ret = db_log_verify_version_check()) != 0)
56 return (ret);
57
58 dbenv = NULL;
59 dbfile = dbname = home = lvhome = passwd = NULL;
60 exitval = nflag = rflag = 0;
61 starttime = endtime = 0;
62 ZERO_LSN(start);
63 ZERO_LSN(stop);
64
65 memset(&lvconfig, 0, sizeof(lvconfig));
66
67 __db_getopt_reset = 1;
68 while ((ch = getopt(argc, argv, "b:cC:d:D:e:h:H:NP:Vvs:z:")) != EOF)
69 switch (ch) {
70 case 'b':
71 /* Don't use getsubopt(3), not all systems have it. */
72 if (db_log_verify_lsn_arg(optarg, &start))
73 return (db_log_verify_usage());
74 break;
75 case 'c':
76 lvconfig.continue_after_fail = 1;
77 break;
78 case 'C':
79 cmb = atoi(optarg);
80 if (cmb <= 0)
81 return (db_log_verify_usage());
82 lvconfig.cachesize = cmb * MB;
83 break;
84 case 'd':
85 dbfile = optarg;
86 break;
87 case 'D':
88 dbname = optarg;
89 break;
90 case 'e':
91 /* Don't use getsubopt(3), not all systems have it. */
92 if (db_log_verify_lsn_arg(optarg, &stop))
93 return (db_log_verify_usage());
94 break;
95 case 'h':
96 home = optarg;
97 break;
98 case 'H':
99 lvhome = optarg;
100 break;
101 case 'N':
102 nflag = 1;
103 break;
104 case 'P':
105 if ((ret = __os_strdup(NULL, optarg, &passwd)) != 0) {
106 __db_err(NULL, ret, "__os_strdup: ");
107 return (EXIT_FAILURE);
108 }
109 memset(optarg, 0, strlen(optarg));
110 break;
111 case 'V':
112 printf("%s\n", db_version(NULL, NULL, NULL));
113 return (EXIT_SUCCESS);
114 case 'v':
115 lvconfig.verbose = 1;
116 break;
117 case 's':
118 starttime = atoi(optarg);
119 break;
120 case 'z':
121 endtime = atoi(optarg);
122 break;
123 case '?':
124 default:
125 return (db_log_verify_usage());
126 }
127 argc -= optind;
128 argv += optind;
129
130 if (argc > 0)
131 return (db_log_verify_usage());
132
133 lvconfig.temp_envhome = lvhome;
134 lvconfig.dbfile = dbfile;
135 lvconfig.dbname = dbname;
136 lvconfig.start_lsn = start;
137 lvconfig.end_lsn = stop;
138 lvconfig.start_time = starttime;
139 lvconfig.end_time = endtime;
140
141 create_again:
142 /*
143 * Create an environment object and initialize it for error
144 * reporting.
145 */
146 if ((ret = db_env_create(&dbenv, 0)) != 0) {
147 fprintf(stderr,
148 "%s: db_env_create: %s\n", progname, db_strerror(ret));
149 goto err;
150 }
151
152 dbenv->set_errfile(dbenv, stderr);
153 dbenv->set_errpfx(dbenv, progname);
154
155 if (nflag) {
156 if ((ret = dbenv->set_flags(dbenv, DB_NOLOCKING, 1)) != 0) {
157 dbenv->err(dbenv, ret, "set_flags: DB_NOLOCKING");
158 goto err;
159 }
160 if ((ret = dbenv->set_flags(dbenv, DB_NOPANIC, 1)) != 0) {
161 dbenv->err(dbenv, ret, "set_flags: DB_NOPANIC");
162 goto err;
163 }
164 }
165
166 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
167 passwd, DB_ENCRYPT_AES)) != 0) {
168 dbenv->err(dbenv, ret, "set_passwd");
169 goto err;
170 }
171
172 /*
173 * Set up an app-specific dispatch function so that we can gracefully
174 * handle app-specific log records.
175 */
176 if ((ret = dbenv->set_app_dispatch(
177 dbenv, db_log_verify_app_record)) != 0) {
178 dbenv->err(dbenv, ret, "app_dispatch");
179 goto err;
180 }
181
182 /*
183 * An environment is required, but as all we're doing is reading log
184 * files, we create one if it doesn't already exist. If we create
185 * it, create it private so it automatically goes away when we're done.
186 * If we are reading the replication database, do not open the env
187 * with logging, because we don't want to log the opens.
188 */
189 if (!vsn_mismtch && (ret = dbenv->open(dbenv, home,
190 DB_USE_ENVIRON, 0)) != 0) {
191 if (dbenv->close(dbenv, 0) != 0) {
192 dbenv = NULL;
193 goto err;
194 }
195 vsn_mismtch = 1;
196 goto create_again;
197 }
198 if (vsn_mismtch && (ret = dbenv->open(dbenv, home, DB_CREATE |
199 DB_INIT_LOG | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0) {
200 dbenv->err(dbenv, ret, "DB_ENV->open");
201 goto err;
202 }
203
204 /* Handle possible interruptions. */
205 __db_util_siginit();
206
207 if ((ret = dbenv->log_verify(dbenv, &lvconfig)) != 0)
208 goto err;
209
210 if (0) {
211 err: exitval = 1;
212 }
213
214 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
215 exitval = 1;
216 fprintf(stderr,
217 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
218 }
219
220 /* Resend any caught signal. */
221 __db_util_sigresend();
222
223 if (passwd != NULL)
224 __os_free(NULL, passwd);
225
226 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
227 }
228
229 int
230 db_log_verify_usage()
231 {
232 fprintf(stderr, "\nusage: %s %s\n%s\n%s\n%s\n%s\n", progname,
233 "[-NcvV] [-h home] "
234 "[-H temporary environment home for internal use]",
235 "[-P password] [-C cache size in megabytes]",
236 "[-d db file name] [-D db name]",
237 "[-b file/offset] [-e file/offset]",
238 "[-s start time] [-z end time]");
239
240 return (EXIT_FAILURE);
241 }
242
243 int
244 db_log_verify_version_check()
245 {
246 int v_major, v_minor, v_patch;
247
248 /* Make sure we're loaded with the right version of the DB library. */
249 (void)db_version(&v_major, &v_minor, &v_patch);
250 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
251 fprintf(stderr, DB_STR_A("5003",
252 "%s: version %d.%d doesn't match library version %d.%d\n",
253 "%s %d %d %d %d\n"), progname,
254 DB_VERSION_MAJOR, DB_VERSION_MINOR,
255 v_major, v_minor);
256 return (EXIT_FAILURE);
257 }
258 return (0);
259 }
260
261 /*
262 * Print an unknown, application-specific log record as best we can, this is
263 * all we can do to such a log record during the verification. The counting
264 * is done in __db_dispatch because we can't pass the log verify handle into
265 * this function.
266 */
267 int
268 db_log_verify_app_record(dbenv, dbt, lsnp, op)
269 DB_ENV *dbenv;
270 DBT *dbt;
271 DB_LSN *lsnp;
272 db_recops op;
273 {
274 u_int32_t i, len, len2, rectype;
275 int ret;
276 u_int8_t ch;
277 char *buf, *p;
278
279 DB_ASSERT(dbenv->env, op == DB_TXN_LOG_VERIFY);
280 COMPQUIET(op, DB_TXN_LOG_VERIFY);
281 ch = 0;
282 ret = 0;
283 i = len = len2 = rectype = 0;
284 buf = p = NULL;
285
286 /*
287 * Fetch the rectype, which always must be at the beginning of the
288 * record (if dispatching is to work at all).
289 */
290 memcpy(&rectype, dbt->data, sizeof(rectype));
291
292 /*
293 * Applications may wish to customize the output here based on the
294 * rectype. We just print the entire log record in the generic
295 * mixed-hex-and-printable format we use for binary data.
296 */
297 if ((ret = __os_malloc(dbenv->env,
298 len = 256 + 2 * dbt->size, &buf)) != 0)
299 goto err;
300 memset(buf, 0, len);
301 snprintf(buf, len, DB_STR_A("5004",
302 "[%lu][%lu] App-specific log record: %lu\n\tdata: ",
303 "%lu %lu %lu"), (u_long)lsnp->file, (u_long)lsnp->offset,
304 (u_long)rectype);
305
306 /*
307 * Each unprintable character takes up several bytes, so be aware of
308 * memory violation.
309 */
310 for (i = 0; i < dbt->size && len2 < len; i++) {
311 ch = ((u_int8_t *)dbt->data)[i];
312 len2 = (u_int32_t)strlen(buf);
313 p = buf + len2;
314 snprintf(p, len - len2 - 1,
315 isprint(ch) || ch == 0x0a ? "%c" : "%#x ", ch);
316 }
317 len2 = (u_int32_t)strlen(buf);
318 p = buf + len2;
319 snprintf(p, len - len2 - 1, "\n\n");
320 __db_msg(dbenv->env, "%s", buf);
321
322 err: if (buf != NULL)
323 __os_free(dbenv->env, buf);
324 return (ret);
325 }
326
327 /*
328 * lsn_arg --
329 * Parse a LSN argument.
330 */
331 int
332 db_log_verify_lsn_arg(arg, lsnp)
333 char *arg;
334 DB_LSN *lsnp;
335 {
336 u_long uval;
337 char *p;
338
339 /*
340 * Expected format is: lsn.file/lsn.offset.
341 */
342 if ((p = strchr(arg, '/')) == NULL)
343 return (1);
344 *p = '\0';
345
346 if (__db_getulong(NULL, progname, arg, 0, UINT32_MAX, &uval))
347 return (1);
348 lsnp->file = uval;
349 if (__db_getulong(NULL, progname, p + 1, 0, UINT32_MAX, &uval))
350 return (1);
351 lsnp->offset = uval;
352 return (0);
353 }