1 | /*- |
---|
2 | * See the file LICENSE for redistribution information. |
---|
3 | * |
---|
4 | * Copyright (c) 1996, 1997, 1998, 1999, 2000 |
---|
5 | * Sleepycat Software. All rights reserved. |
---|
6 | */ |
---|
7 | |
---|
8 | #include "db_config.h" |
---|
9 | |
---|
10 | #ifndef lint |
---|
11 | static const char copyright[] = |
---|
12 | "Copyright (c) 1996-2000\nSleepycat Software Inc. All rights reserved.\n"; |
---|
13 | static const char revid[] = |
---|
14 | "$Id: db_checkpoint.c,v 1.1.1.2 2002-02-11 16:26:57 ghudson Exp $"; |
---|
15 | #endif |
---|
16 | |
---|
17 | #ifndef NO_SYSTEM_INCLUDES |
---|
18 | #include <sys/types.h> |
---|
19 | |
---|
20 | #if TIME_WITH_SYS_TIME |
---|
21 | #include <sys/time.h> |
---|
22 | #include <time.h> |
---|
23 | #else |
---|
24 | #if HAVE_SYS_TIME_H |
---|
25 | #include <sys/time.h> |
---|
26 | #else |
---|
27 | #include <time.h> |
---|
28 | #endif |
---|
29 | #endif |
---|
30 | |
---|
31 | #include <errno.h> |
---|
32 | #include <limits.h> |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <unistd.h> |
---|
36 | #endif |
---|
37 | |
---|
38 | #include "db_int.h" |
---|
39 | #include "db_page.h" |
---|
40 | #include "btree.h" |
---|
41 | #include "hash.h" |
---|
42 | #include "qam.h" |
---|
43 | #include "common_ext.h" |
---|
44 | #include "clib_ext.h" |
---|
45 | |
---|
46 | char *check __P((DB_ENV *, long, long)); |
---|
47 | int main __P((int, char *[])); |
---|
48 | void usage __P((void)); |
---|
49 | |
---|
50 | DB_ENV *dbenv; |
---|
51 | const char |
---|
52 | *progname = "db_checkpoint"; /* Program name. */ |
---|
53 | |
---|
54 | int |
---|
55 | main(argc, argv) |
---|
56 | int argc; |
---|
57 | char *argv[]; |
---|
58 | { |
---|
59 | extern char *optarg; |
---|
60 | extern int optind; |
---|
61 | time_t now; |
---|
62 | long argval; |
---|
63 | u_int32_t flags, kbytes, minutes, seconds; |
---|
64 | int ch, e_close, exitval, once, ret, verbose; |
---|
65 | char *home, *logfile; |
---|
66 | |
---|
67 | /* |
---|
68 | * !!! |
---|
69 | * Don't allow a fully unsigned 32-bit number, some compilers get |
---|
70 | * upset and require it to be specified in hexadecimal and so on. |
---|
71 | */ |
---|
72 | #define MAX_UINT32_T 2147483647 |
---|
73 | |
---|
74 | kbytes = minutes = 0; |
---|
75 | e_close = exitval = once = verbose = 0; |
---|
76 | flags = 0; |
---|
77 | home = logfile = NULL; |
---|
78 | while ((ch = getopt(argc, argv, "1h:k:L:p:Vv")) != EOF) |
---|
79 | switch (ch) { |
---|
80 | case '1': |
---|
81 | once = 1; |
---|
82 | flags = DB_FORCE; |
---|
83 | break; |
---|
84 | case 'h': |
---|
85 | home = optarg; |
---|
86 | break; |
---|
87 | case 'k': |
---|
88 | (void)__db_getlong(NULL, progname, |
---|
89 | optarg, 1, (long)MAX_UINT32_T, &argval); |
---|
90 | kbytes = argval; |
---|
91 | break; |
---|
92 | case 'L': |
---|
93 | logfile = optarg; |
---|
94 | break; |
---|
95 | case 'p': |
---|
96 | (void)__db_getlong(NULL, progname, |
---|
97 | optarg, 1, (long)MAX_UINT32_T, &argval); |
---|
98 | minutes = argval; |
---|
99 | break; |
---|
100 | case 'V': |
---|
101 | printf("%s\n", db_version(NULL, NULL, NULL)); |
---|
102 | exit(0); |
---|
103 | case 'v': |
---|
104 | verbose = 1; |
---|
105 | break; |
---|
106 | case '?': |
---|
107 | default: |
---|
108 | usage(); |
---|
109 | goto shutdown; |
---|
110 | } |
---|
111 | argc -= optind; |
---|
112 | argv += optind; |
---|
113 | |
---|
114 | if (argc != 0) |
---|
115 | usage(); |
---|
116 | |
---|
117 | if (once == 0 && kbytes == 0 && minutes == 0) { |
---|
118 | (void)fprintf(stderr, |
---|
119 | "%s: at least one of -1, -k and -p must be specified\n", |
---|
120 | progname); |
---|
121 | exit (1); |
---|
122 | } |
---|
123 | |
---|
124 | /* Handle possible interruptions. */ |
---|
125 | __db_util_siginit(); |
---|
126 | |
---|
127 | /* Log our process ID. */ |
---|
128 | if (logfile != NULL && __db_util_logset(progname, logfile)) |
---|
129 | goto shutdown; |
---|
130 | |
---|
131 | /* |
---|
132 | * Create an environment object and initialize it for error |
---|
133 | * reporting. |
---|
134 | */ |
---|
135 | if ((ret = db_env_create(&dbenv, 0)) != 0) { |
---|
136 | fprintf(stderr, |
---|
137 | "%s: db_env_create: %s\n", progname, db_strerror(ret)); |
---|
138 | goto shutdown; |
---|
139 | } |
---|
140 | e_close = 1; |
---|
141 | |
---|
142 | dbenv->set_errfile(dbenv, stderr); |
---|
143 | dbenv->set_errpfx(dbenv, progname); |
---|
144 | |
---|
145 | /* Initialize the environment. */ |
---|
146 | if ((ret = dbenv->open(dbenv, home, |
---|
147 | DB_INIT_LOG | DB_INIT_TXN | DB_INIT_MPOOL | DB_USE_ENVIRON, |
---|
148 | 0)) != 0) { |
---|
149 | dbenv->err(dbenv, ret, "open"); |
---|
150 | goto shutdown; |
---|
151 | } |
---|
152 | |
---|
153 | /* Register the standard pgin/pgout functions, in case we do I/O. */ |
---|
154 | if ((ret = |
---|
155 | memp_register(dbenv, DB_FTYPE_SET, __db_pgin, __db_pgout)) != 0) { |
---|
156 | dbenv->err(dbenv, ret, |
---|
157 | "failed to register access method functions"); |
---|
158 | goto shutdown; |
---|
159 | } |
---|
160 | |
---|
161 | /* |
---|
162 | * If we have only a time delay, then we'll sleep the right amount |
---|
163 | * to wake up when a checkpoint is necessary. If we have a "kbytes" |
---|
164 | * field set, then we'll check every 30 seconds. |
---|
165 | */ |
---|
166 | seconds = kbytes != 0 ? 30 : minutes * 60; |
---|
167 | while (!__db_util_interrupted()) { |
---|
168 | if (verbose) { |
---|
169 | (void)time(&now); |
---|
170 | dbenv->errx(dbenv, "checkpoint: %s", ctime(&now)); |
---|
171 | } |
---|
172 | |
---|
173 | ret = txn_checkpoint(dbenv, kbytes, minutes, flags); |
---|
174 | while (ret == DB_INCOMPLETE) { |
---|
175 | if (verbose) |
---|
176 | dbenv->errx(dbenv, |
---|
177 | "checkpoint did not finish, retrying\n"); |
---|
178 | (void)__os_sleep(dbenv, 2, 0); |
---|
179 | ret = txn_checkpoint(dbenv, 0, 0, flags); |
---|
180 | } |
---|
181 | if (ret != 0) { |
---|
182 | dbenv->err(dbenv, ret, "txn_checkpoint"); |
---|
183 | goto shutdown; |
---|
184 | } |
---|
185 | |
---|
186 | if (once) |
---|
187 | break; |
---|
188 | |
---|
189 | (void)__os_sleep(dbenv, seconds, 0); |
---|
190 | } |
---|
191 | |
---|
192 | if (0) { |
---|
193 | shutdown: exitval = 1; |
---|
194 | } |
---|
195 | |
---|
196 | /* Clean up the logfile. */ |
---|
197 | if (logfile != NULL) |
---|
198 | remove(logfile); |
---|
199 | |
---|
200 | /* Clean up the environment. */ |
---|
201 | if (e_close && (ret = dbenv->close(dbenv, 0)) != 0) { |
---|
202 | exitval = 1; |
---|
203 | fprintf(stderr, |
---|
204 | "%s: dbenv->close: %s\n", progname, db_strerror(ret)); |
---|
205 | } |
---|
206 | |
---|
207 | /* Resend any caught signal. */ |
---|
208 | __db_util_sigresend(); |
---|
209 | |
---|
210 | return (exitval); |
---|
211 | } |
---|
212 | |
---|
213 | void |
---|
214 | usage() |
---|
215 | { |
---|
216 | (void)fprintf(stderr, |
---|
217 | "usage: db_checkpoint [-1Vv] [-h home] [-k kbytes] [-L file] [-p min]\n"); |
---|
218 | exit(1); |
---|
219 | } |
---|