• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-WNDR4500v2-V1.0.0.60_1.0.38/ap/gpl/timemachine/db-4.7.25.NC/build_vxworks/db_checkpoint/
1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 1996,2008 Oracle.  All rights reserved.
5 *
6 * $Id: db_checkpoint.c,v 12.22 2008/01/08 20:58:11 bostic Exp $
7 */
8
9#include "db_config.h"
10
11#include "db_int.h"
12
13#ifndef lint
14static const char copyright[] =
15    "Copyright (c) 1996,2008 Oracle.  All rights reserved.\n";
16#endif
17
18int	 db_checkpoint_main __P((int, char *[]));
19int	 db_checkpoint_usage __P((void));
20int	 db_checkpoint_version_check __P((void));
21
22const char *progname;
23
24int
25db_checkpoint(args)
26	char *args;
27{
28	int argc;
29	char **argv;
30
31	__db_util_arg("db_checkpoint", args, &argc, &argv);
32	return (db_checkpoint_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
33}
34
35#include <stdio.h>
36#define	ERROR_RETURN	ERROR
37
38int
39db_checkpoint_main(argc, argv)
40	int argc;
41	char *argv[];
42{
43	extern char *optarg;
44	extern int optind, __db_getopt_reset;
45	DB_ENV	*dbenv;
46	time_t now;
47	long argval;
48	u_int32_t flags, kbytes, minutes, seconds;
49	int ch, exitval, once, ret, verbose;
50	char *home, *logfile, *passwd, time_buf[CTIME_BUFLEN];
51
52	if ((progname = __db_rpath(argv[0])) == NULL)
53		progname = argv[0];
54	else
55		++progname;
56
57	if ((ret = db_checkpoint_version_check()) != 0)
58		return (ret);
59
60	/*
61	 * !!!
62	 * Don't allow a fully unsigned 32-bit number, some compilers get
63	 * upset and require it to be specified in hexadecimal and so on.
64	 */
65#define	MAX_UINT32_T	2147483647
66
67	dbenv = NULL;
68	kbytes = minutes = 0;
69	exitval = once = verbose = 0;
70	flags = 0;
71	home = logfile = passwd = NULL;
72	__db_getopt_reset = 1;
73	while ((ch = getopt(argc, argv, "1h:k:L:P:p:Vv")) != EOF)
74		switch (ch) {
75		case '1':
76			once = 1;
77			flags = DB_FORCE;
78			break;
79		case 'h':
80			home = optarg;
81			break;
82		case 'k':
83			if (__db_getlong(NULL, progname,
84			    optarg, 1, (long)MAX_UINT32_T, &argval))
85				return (EXIT_FAILURE);
86			kbytes = (u_int32_t)argval;
87			break;
88		case 'L':
89			logfile = optarg;
90			break;
91		case 'P':
92			passwd = strdup(optarg);
93			memset(optarg, 0, strlen(optarg));
94			if (passwd == NULL) {
95				fprintf(stderr, "%s: strdup: %s\n",
96				    progname, strerror(errno));
97				return (EXIT_FAILURE);
98			}
99			break;
100		case 'p':
101			if (__db_getlong(NULL, progname,
102			    optarg, 1, (long)MAX_UINT32_T, &argval))
103				return (EXIT_FAILURE);
104			minutes = (u_int32_t)argval;
105			break;
106		case 'V':
107			printf("%s\n", db_version(NULL, NULL, NULL));
108			return (EXIT_SUCCESS);
109		case 'v':
110			verbose = 1;
111			break;
112		case '?':
113		default:
114			return (db_checkpoint_usage());
115		}
116	argc -= optind;
117	argv += optind;
118
119	if (argc != 0)
120		return (db_checkpoint_usage());
121
122	if (once == 0 && kbytes == 0 && minutes == 0) {
123		(void)fprintf(stderr,
124		    "%s: at least one of -1, -k and -p must be specified\n",
125		    progname);
126		return (db_checkpoint_usage());
127	}
128
129	/* Handle possible interruptions. */
130	__db_util_siginit();
131
132	/* Log our process ID. */
133	if (logfile != NULL && __db_util_logset(progname, logfile))
134		goto shutdown;
135
136	/*
137	 * Create an environment object and initialize it for error
138	 * reporting.
139	 */
140	if ((ret = db_env_create(&dbenv, 0)) != 0) {
141		fprintf(stderr,
142		    "%s: db_env_create: %s\n", progname, db_strerror(ret));
143		goto shutdown;
144	}
145
146	dbenv->set_errfile(dbenv, stderr);
147	dbenv->set_errpfx(dbenv, progname);
148
149	if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
150	    passwd, DB_ENCRYPT_AES)) != 0) {
151		dbenv->err(dbenv, ret, "set_passwd");
152		goto shutdown;
153	}
154
155	/*
156	 * If attaching to a pre-existing environment fails, create a
157	 * private one and try again.
158	 */
159	if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0 &&
160	    (!once || ret == DB_VERSION_MISMATCH ||
161	    (ret = dbenv->open(dbenv, home,
162	    DB_CREATE | DB_INIT_TXN | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
163		dbenv->err(dbenv, ret, "DB_ENV->open");
164		goto shutdown;
165	}
166
167	/*
168	 * If we have only a time delay, then we'll sleep the right amount
169	 * to wake up when a checkpoint is necessary.  If we have a "kbytes"
170	 * field set, then we'll check every 30 seconds.
171	 */
172	seconds = kbytes != 0 ? 30 : minutes * 60;
173	while (!__db_util_interrupted()) {
174		if (verbose) {
175			(void)time(&now);
176			dbenv->errx(dbenv,
177		    "checkpoint begin: %s", __os_ctime(&now, time_buf));
178		}
179
180		if ((ret = dbenv->txn_checkpoint(dbenv,
181		    kbytes, minutes, flags)) != 0) {
182			dbenv->err(dbenv, ret, "txn_checkpoint");
183			goto shutdown;
184		}
185
186		if (verbose) {
187			(void)time(&now);
188			dbenv->errx(dbenv,
189		    "checkpoint complete: %s", __os_ctime(&now, time_buf));
190		}
191
192		if (once)
193			break;
194
195		__os_yield(dbenv->env, seconds, 0);
196	}
197
198	if (0) {
199shutdown:	exitval = 1;
200	}
201
202	/* Clean up the logfile. */
203	if (logfile != NULL)
204		(void)remove(logfile);
205
206	/* Clean up the environment. */
207	if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
208		exitval = 1;
209		fprintf(stderr,
210		    "%s: dbenv->close: %s\n", progname, db_strerror(ret));
211	}
212
213	if (passwd != NULL)
214		free(passwd);
215
216	/* Resend any caught signal. */
217	__db_util_sigresend();
218
219	return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
220}
221
222int
223db_checkpoint_usage()
224{
225	(void)fprintf(stderr, "usage: %s [-1Vv]\n\t%s\n", progname,
226	    "[-h home] [-k kbytes] [-L file] [-P password] [-p min]");
227	return (EXIT_FAILURE);
228}
229
230int
231db_checkpoint_version_check()
232{
233	int v_major, v_minor, v_patch;
234
235	/* Make sure we're loaded with the right version of the DB library. */
236	(void)db_version(&v_major, &v_minor, &v_patch);
237	if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
238		fprintf(stderr,
239	"%s: version %d.%d doesn't match library version %d.%d\n",
240		    progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
241		    v_major, v_minor);
242		return (EXIT_FAILURE);
243	}
244	return (0);
245}
246