Debian Codegraph @

build_vxworks/util/db_checkpoint.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$
7 */
8
9 #include "db_config.h"
10
11 #include "db_int.h"
12
13 #ifndef lint
14 static const char copyright[] =
15 "Copyright (c) 1996, 2013 Oracle and/or its affiliates. All rights reserved.\n";
16 #endif
17
18 int db_checkpoint_main __P((int, char *[]));
19 int db_checkpoint_usage __P((void));
20 int db_checkpoint_version_check __P((void));
21
22 const char *progname;
23
24 int
25 db_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
38 int
39 db_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 if (passwd != NULL) {
93 fprintf(stderr, DB_STR("5134",
94 "Password may not be specified twice"));
95 free(passwd);
96 return (EXIT_FAILURE);
97 }
98 passwd = strdup(optarg);
99 memset(optarg, 0, strlen(optarg));
100 if (passwd == NULL) {
101 fprintf(stderr, DB_STR_A("5121",
102 "%s: strdup: %s\n", "%s %s"),
103 progname, strerror(errno));
104 return (EXIT_FAILURE);
105 }
106 break;
107 case 'p':
108 if (__db_getlong(NULL, progname,
109 optarg, 1, (long)MAX_UINT32_T, &argval))
110 return (EXIT_FAILURE);
111 minutes = (u_int32_t)argval;
112 break;
113 case 'V':
114 printf("%s\n", db_version(NULL, NULL, NULL));
115 return (EXIT_SUCCESS);
116 case 'v':
117 verbose = 1;
118 break;
119 case '?':
120 default:
121 return (db_checkpoint_usage());
122 }
123 argc -= optind;
124 argv += optind;
125
126 if (argc != 0)
127 return (db_checkpoint_usage());
128
129 if (once == 0 && kbytes == 0 && minutes == 0) {
130 (void)fprintf(stderr, DB_STR_A("5122",
131 "%s: at least one of -1, -k and -p must be specified\n",
132 "%s\n"), progname);
133 return (db_checkpoint_usage());
134 }
135
136 /* Handle possible interruptions. */
137 __db_util_siginit();
138
139 /* Log our process ID. */
140 if (logfile != NULL && __db_util_logset(progname, logfile))
141 goto err;
142
143 /*
144 * Create an environment object and initialize it for error
145 * reporting.
146 */
147 if ((ret = db_env_create(&dbenv, 0)) != 0) {
148 fprintf(stderr,
149 "%s: db_env_create: %s\n", progname, db_strerror(ret));
150 goto err;
151 }
152
153 dbenv->set_errfile(dbenv, stderr);
154 dbenv->set_errpfx(dbenv, progname);
155
156 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
157 passwd, DB_ENCRYPT_AES)) != 0) {
158 dbenv->err(dbenv, ret, "set_passwd");
159 goto err;
160 }
161
162 /*
163 * If attaching to a pre-existing environment fails, create a
164 * private one and try again. Turn on DB_THREAD in case a repmgr
165 * application wants to do checkpointing using this utility: repmgr
166 * requires DB_THREAD for all env handles.
167 */
168 #ifdef HAVE_REPLICATION_THREADS
169 #define ENV_FLAGS (DB_THREAD | DB_USE_ENVIRON)
170 #else
171 #define ENV_FLAGS DB_USE_ENVIRON
172 #endif
173 if ((ret = dbenv->open(dbenv, home, ENV_FLAGS, 0)) != 0 &&
174 (!once || ret == DB_VERSION_MISMATCH || ret == DB_REP_LOCKOUT ||
175 (ret = dbenv->open(dbenv, home,
176 DB_CREATE | DB_INIT_TXN | DB_PRIVATE | DB_USE_ENVIRON, 0)) != 0)) {
177 dbenv->err(dbenv, ret, "DB_ENV->open");
178 goto err;
179 }
180
181 /*
182 * If we have only a time delay, then we'll sleep the right amount
183 * to wake up when a checkpoint is necessary. If we have a "kbytes"
184 * field set, then we'll check every 30 seconds.
185 */
186 seconds = kbytes != 0 ? 30 : minutes * 60;
187 while (!__db_util_interrupted()) {
188 if (verbose) {
189 (void)time(&now);
190 dbenv->errx(dbenv, DB_STR_A("5123",
191 "checkpoint begin: %s", "%s"),
192 __os_ctime(&now, time_buf));
193 }
194
195 if ((ret = dbenv->txn_checkpoint(dbenv,
196 kbytes, minutes, flags)) != 0) {
197 dbenv->err(dbenv, ret, "txn_checkpoint");
198 goto err;
199 }
200
201 if (verbose) {
202 (void)time(&now);
203 dbenv->errx(dbenv, DB_STR_A("5124",
204 "checkpoint complete: %s", "%s"),
205 __os_ctime(&now, time_buf));
206 }
207
208 if (once)
209 break;
210
211 __os_yield(dbenv->env, seconds, 0);
212 }
213
214 if (0) {
215 err: exitval = 1;
216 }
217
218 /* Clean up the logfile. */
219 if (logfile != NULL)
220 (void)remove(logfile);
221
222 /* Clean up the environment. */
223 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
224 exitval = 1;
225 fprintf(stderr,
226 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
227 }
228
229 if (passwd != NULL)
230 free(passwd);
231
232 /* Resend any caught signal. */
233 __db_util_sigresend();
234
235 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
236 }
237
238 int
239 db_checkpoint_usage()
240 {
241 (void)fprintf(stderr, "usage: %s [-1Vv]\n\t%s\n", progname,
242 "[-h home] [-k kbytes] [-L file] [-P password] [-p min]");
243 return (EXIT_FAILURE);
244 }
245
246 int
247 db_checkpoint_version_check()
248 {
249 int v_major, v_minor, v_patch;
250
251 /* Make sure we're loaded with the right version of the DB library. */
252 (void)db_version(&v_major, &v_minor, &v_patch);
253 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
254 fprintf(stderr, DB_STR_A("5125",
255 "%s: version %d.%d doesn't match library version %d.%d\n",
256 "%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR,
257 DB_VERSION_MINOR, v_major, v_minor);
258 return (EXIT_FAILURE);
259 }
260 return (0);
261 }