Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

db_checkpoint.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1996-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: db_checkpoint.c,v 12.6 2005/09/09 12:38:30 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef lint
00013 static const char copyright[] =
00014     "Copyright (c) 1996-2005\nSleepycat Software Inc.  All rights reserved.\n";
00015 #endif
00016 
00017 #ifndef NO_SYSTEM_INCLUDES
00018 #include <sys/types.h>
00019 
00020 #if TIME_WITH_SYS_TIME
00021 #include <sys/time.h>
00022 #include <time.h>
00023 #else
00024 #if HAVE_SYS_TIME_H
00025 #include <sys/time.h>
00026 #else
00027 #include <time.h>
00028 #endif
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #endif
00036 
00037 #include "db_int.h"
00038 
00039 int      db_checkpoint_main __P((int, char *[]));
00040 int      db_checkpoint_usage __P((void));
00041 int      db_checkpoint_version_check __P((void));
00042 
00043 const char *progname;
00044 
00045 int
00046 db_checkpoint(args)
00047         char *args;
00048 {
00049         int argc;
00050         char **argv;
00051 
00052         __db_util_arg("db_checkpoint", args, &argc, &argv);
00053         return (db_checkpoint_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
00054 }
00055 
00056 #include <stdio.h>
00057 #define ERROR_RETURN    ERROR
00058 
00059 int
00060 db_checkpoint_main(argc, argv)
00061         int argc;
00062         char *argv[];
00063 {
00064         extern char *optarg;
00065         extern int optind, __db_getopt_reset;
00066         DB_ENV  *dbenv;
00067         time_t now;
00068         long argval;
00069         u_int32_t flags, kbytes, minutes, seconds;
00070         int ch, exitval, once, ret, verbose;
00071         char *home, *logfile, *passwd;
00072 
00073         if ((progname = strrchr(argv[0], '/')) == NULL)
00074                 progname = argv[0];
00075         else
00076                 ++progname;
00077 
00078         if ((ret = db_checkpoint_version_check()) != 0)
00079                 return (ret);
00080 
00081         /*
00082          * !!!
00083          * Don't allow a fully unsigned 32-bit number, some compilers get
00084          * upset and require it to be specified in hexadecimal and so on.
00085          */
00086 #define MAX_UINT32_T    2147483647
00087 
00088         dbenv = NULL;
00089         kbytes = minutes = 0;
00090         exitval = once = verbose = 0;
00091         flags = 0;
00092         home = logfile = passwd = NULL;
00093         __db_getopt_reset = 1;
00094         while ((ch = getopt(argc, argv, "1h:k:L:P:p:Vv")) != EOF)
00095                 switch (ch) {
00096                 case '1':
00097                         once = 1;
00098                         flags = DB_FORCE;
00099                         break;
00100                 case 'h':
00101                         home = optarg;
00102                         break;
00103                 case 'k':
00104                         if (__db_getlong(NULL, progname,
00105                             optarg, 1, (long)MAX_UINT32_T, &argval))
00106                                 return (EXIT_FAILURE);
00107                         kbytes = (u_int32_t)argval;
00108                         break;
00109                 case 'L':
00110                         logfile = optarg;
00111                         break;
00112                 case 'P':
00113                         passwd = strdup(optarg);
00114                         memset(optarg, 0, strlen(optarg));
00115                         if (passwd == NULL) {
00116                                 fprintf(stderr, "%s: strdup: %s\n",
00117                                     progname, strerror(errno));
00118                                 return (EXIT_FAILURE);
00119                         }
00120                         break;
00121                 case 'p':
00122                         if (__db_getlong(NULL, progname,
00123                             optarg, 1, (long)MAX_UINT32_T, &argval))
00124                                 return (EXIT_FAILURE);
00125                         minutes = (u_int32_t)argval;
00126                         break;
00127                 case 'V':
00128                         printf("%s\n", db_version(NULL, NULL, NULL));
00129                         return (EXIT_SUCCESS);
00130                 case 'v':
00131                         verbose = 1;
00132                         break;
00133                 case '?':
00134                 default:
00135                         return (db_checkpoint_usage());
00136                 }
00137         argc -= optind;
00138         argv += optind;
00139 
00140         if (argc != 0)
00141                 return (db_checkpoint_usage());
00142 
00143         if (once == 0 && kbytes == 0 && minutes == 0) {
00144                 (void)fprintf(stderr,
00145                     "%s: at least one of -1, -k and -p must be specified\n",
00146                     progname);
00147                 return (EXIT_FAILURE);
00148         }
00149 
00150         /* Handle possible interruptions. */
00151         __db_util_siginit();
00152 
00153         /* Log our process ID. */
00154         if (logfile != NULL && __db_util_logset(progname, logfile))
00155                 goto shutdown;
00156 
00157         /*
00158          * Create an environment object and initialize it for error
00159          * reporting.
00160          */
00161         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00162                 fprintf(stderr,
00163                     "%s: db_env_create: %s\n", progname, db_strerror(ret));
00164                 goto shutdown;
00165         }
00166 
00167         dbenv->set_errfile(dbenv, stderr);
00168         dbenv->set_errpfx(dbenv, progname);
00169 
00170         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00171             passwd, DB_ENCRYPT_AES)) != 0) {
00172                 dbenv->err(dbenv, ret, "set_passwd");
00173                 goto shutdown;
00174         }
00175         /* Initialize the environment. */
00176         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0) {
00177                 dbenv->err(dbenv, ret, "open");
00178                 goto shutdown;
00179         }
00180 
00181         /*
00182          * If we have only a time delay, then we'll sleep the right amount
00183          * to wake up when a checkpoint is necessary.  If we have a "kbytes"
00184          * field set, then we'll check every 30 seconds.
00185          */
00186         seconds = kbytes != 0 ? 30 : minutes * 60;
00187         while (!__db_util_interrupted()) {
00188                 if (verbose) {
00189                         (void)time(&now);
00190                         dbenv->errx(dbenv, "checkpoint begin: %s", ctime(&now));
00191                 }
00192 
00193                 if ((ret = dbenv->txn_checkpoint(dbenv,
00194                     kbytes, minutes, flags)) != 0) {
00195                         dbenv->err(dbenv, ret, "txn_checkpoint");
00196                         goto shutdown;
00197                 }
00198 
00199                 if (verbose) {
00200                         (void)time(&now);
00201                         dbenv->errx(dbenv,
00202                             "checkpoint complete: %s", ctime(&now));
00203                 }
00204 
00205                 if (once)
00206                         break;
00207 
00208                 __os_sleep(dbenv, seconds, 0);
00209         }
00210 
00211         if (0) {
00212 shutdown:       exitval = 1;
00213         }
00214 
00215         /* Clean up the logfile. */
00216         if (logfile != NULL)
00217                 (void)remove(logfile);
00218 
00219         /* Clean up the environment. */
00220         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00221                 exitval = 1;
00222                 fprintf(stderr,
00223                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00224         }
00225 
00226         if (passwd != NULL)
00227                 free(passwd);
00228 
00229         /* Resend any caught signal. */
00230         __db_util_sigresend();
00231 
00232         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00233 }
00234 
00235 int
00236 db_checkpoint_usage()
00237 {
00238         (void)fprintf(stderr, "usage: %s [-1Vv]\n\t%s\n", progname,
00239             "[-h home] [-k kbytes] [-L file] [-P password] [-p min]");
00240         return (EXIT_FAILURE);
00241 }
00242 
00243 int
00244 db_checkpoint_version_check()
00245 {
00246         int v_major, v_minor, v_patch;
00247 
00248         /* Make sure we're loaded with the right version of the DB library. */
00249         (void)db_version(&v_major, &v_minor, &v_patch);
00250         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00251                 fprintf(stderr,
00252         "%s: version %d.%d doesn't match library version %d.%d\n",
00253                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00254                     v_major, v_minor);
00255                 return (EXIT_FAILURE);
00256         }
00257         return (0);
00258 }

Generated on Sun Dec 25 12:14:15 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2