2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char copyright[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid[] = "@(#)db_checkpoint.c 10.12 (Sleepycat) 9/4/97";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
37 #include "common_ext.h"
39 char *check __P((DB_ENV *, long, long));
40 int checkpoint __P((DB_ENV *, char *, int));
41 DB_ENV *db_init __P((char *));
42 int logpid __P((char *, int));
43 int main __P((int, char *[]));
44 void onint __P((int));
45 void siginit __P((void));
46 void usage __P((void));
49 time_t now; /* Checkpoint time. */
51 *progname = "db_checkpoint"; /* Program name. */
62 long kbytes, minutes, seconds;
63 int ch, eval, verbose;
66 home = logfile = NULL;
69 while ((ch = getopt(argc, argv, "h:k:L:p:v")) != EOF)
75 get_long(optarg, 1, LONG_MAX, &kbytes);
81 get_long(optarg, 1, LONG_MAX, &minutes);
96 if (kbytes == 0 && minutes == 0) {
97 warnx("at least one of -k and -p must be specified");
101 /* Initialize the environment. */
102 dbenv = db_init(home);
104 if (logfile != NULL && logpid(logfile, 1)) {
105 (void)db_appexit(dbenv);
110 * If we have only a time delay, then we'll sleep the right amount
111 * to wake up when a checkpoint is necessary. If we have a "kbytes"
112 * field set, then we'll check every 30 seconds.
115 seconds = kbytes != 0 ? 30 : minutes * 60;
116 while (!interrupted) {
117 (void)__db_sleep(seconds, 0);
121 printf("checkpoint: %s", ctime(&now));
123 errno = txn_checkpoint(dbenv->tx_info, kbytes, minutes);
125 while (errno == DB_INCOMPLETE) {
128 "checkpoint did not finish, retrying");
129 (void)__db_sleep(2, 0);
130 errno = txn_checkpoint(dbenv->tx_info, 0, 0);
135 __db_err(dbenv, "checkpoint: %s", strerror(errno));
140 if (logfile != NULL && logpid(logfile, 0))
144 (void)signal(interrupted, SIG_DFL);
145 (void)raise(interrupted);
149 return (db_appexit(dbenv) || eval ? 1 : 0);
154 * Initialize the environment.
162 if ((dbenv = (DB_ENV *)calloc(sizeof(DB_ENV), 1)) == NULL) {
166 dbenv->db_errfile = stderr;
167 dbenv->db_errpfx = progname;
169 if ((errno = db_appinit(home, NULL, dbenv,
170 DB_INIT_LOG | DB_INIT_TXN | DB_INIT_MPOOL | DB_USE_ENVIRON)) != 0)
171 err(1, "db_appinit");
173 if (memp_register(dbenv->mp_info,
174 DB_FTYPE_BTREE, __bam_pgin, __bam_pgout) ||
175 memp_register(dbenv->mp_info,
176 DB_FTYPE_HASH, __ham_pgin, __ham_pgout)) {
177 (void)db_appexit(dbenv);
179 "db_appinit: failed to register access method functions");
189 * Log that we're running.
192 logpid(fname, is_open)
200 if ((fp = fopen(fname, "w")) == NULL) {
206 "%s: %lu %s", progname, (u_long)getpid(), ctime(&now));
215 * Initialize the set of signals for which we want to clean up.
216 * Generally, we try not to leave the shared regions locked if
223 (void)signal(SIGHUP, onint);
225 (void)signal(SIGINT, onint);
227 (void)signal(SIGKILL, onint);
229 (void)signal(SIGTERM, onint);
234 * Interrupt signal handler.
240 if ((interrupted = signo) == 0)
241 interrupted = SIGINT;
247 (void)fprintf(stderr,
248 "usage: db_checkpoint [-v] [-h home] [-k kbytes] [-L file] [-p min]\n");