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      main __P((int, char *[]));
00040 int      usage __P((void));
00041 int      version_check __P((void));
00042 
00043 const char *progname;
00044 
00045 int
00046 main(argc, argv)
00047         int argc;
00048         char *argv[];
00049 {
00050         extern char *optarg;
00051         extern int optind;
00052         DB_ENV  *dbenv;
00053         time_t now;
00054         long argval;
00055         u_int32_t flags, kbytes, minutes, seconds;
00056         int ch, exitval, once, ret, verbose;
00057         char *home, *logfile, *passwd;
00058 
00059         if ((progname = strrchr(argv[0], '/')) == NULL)
00060                 progname = argv[0];
00061         else
00062                 ++progname;
00063 
00064         if ((ret = version_check()) != 0)
00065                 return (ret);
00066 
00067         /*
00068          * !!!
00069          * Don't allow a fully unsigned 32-bit number, some compilers get
00070          * upset and require it to be specified in hexadecimal and so on.
00071          */
00072 #define MAX_UINT32_T    2147483647
00073 
00074         dbenv = NULL;
00075         kbytes = minutes = 0;
00076         exitval = once = verbose = 0;
00077         flags = 0;
00078         home = logfile = passwd = NULL;
00079         while ((ch = getopt(argc, argv, "1h:k:L:P:p:Vv")) != EOF)
00080                 switch (ch) {
00081                 case '1':
00082                         once = 1;
00083                         flags = DB_FORCE;
00084                         break;
00085                 case 'h':
00086                         home = optarg;
00087                         break;
00088                 case 'k':
00089                         if (__db_getlong(NULL, progname,
00090                             optarg, 1, (long)MAX_UINT32_T, &argval))
00091                                 return (EXIT_FAILURE);
00092                         kbytes = (u_int32_t)argval;
00093                         break;
00094                 case 'L':
00095                         logfile = optarg;
00096                         break;
00097                 case 'P':
00098                         passwd = strdup(optarg);
00099                         memset(optarg, 0, strlen(optarg));
00100                         if (passwd == NULL) {
00101                                 fprintf(stderr, "%s: strdup: %s\n",
00102                                     progname, strerror(errno));
00103                                 return (EXIT_FAILURE);
00104                         }
00105                         break;
00106                 case 'p':
00107                         if (__db_getlong(NULL, progname,
00108                             optarg, 1, (long)MAX_UINT32_T, &argval))
00109                                 return (EXIT_FAILURE);
00110                         minutes = (u_int32_t)argval;
00111                         break;
00112                 case 'V':
00113                         printf("%s\n", db_version(NULL, NULL, NULL));
00114                         return (EXIT_SUCCESS);
00115                 case 'v':
00116                         verbose = 1;
00117                         break;
00118                 case '?':
00119                 default:
00120                         return (usage());
00121                 }
00122         argc -= optind;
00123         argv += optind;
00124 
00125         if (argc != 0)
00126                 return (usage());
00127 
00128         if (once == 0 && kbytes == 0 && minutes == 0) {
00129                 (void)fprintf(stderr,
00130                     "%s: at least one of -1, -k and -p must be specified\n",
00131                     progname);
00132                 return (EXIT_FAILURE);
00133         }
00134 
00135         /* Handle possible interruptions. */
00136         __db_util_siginit();
00137 
00138         /* Log our process ID. */
00139         if (logfile != NULL && __db_util_logset(progname, logfile))
00140                 goto shutdown;
00141 
00142         /*
00143          * Create an environment object and initialize it for error
00144          * reporting.
00145          */
00146         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00147                 fprintf(stderr,
00148                     "%s: db_env_create: %s\n", progname, db_strerror(ret));
00149                 goto shutdown;
00150         }
00151 
00152         dbenv->set_errfile(dbenv, stderr);
00153         dbenv->set_errpfx(dbenv, progname);
00154 
00155         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00156             passwd, DB_ENCRYPT_AES)) != 0) {
00157                 dbenv->err(dbenv, ret, "set_passwd");
00158                 goto shutdown;
00159         }
00160         /* Initialize the environment. */
00161         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0) {
00162                 dbenv->err(dbenv, ret, "open");
00163                 goto shutdown;
00164         }
00165 
00166         /*
00167          * If we have only a time delay, then we'll sleep the right amount
00168          * to wake up when a checkpoint is necessary.  If we have a "kbytes"
00169          * field set, then we'll check every 30 seconds.
00170          */
00171         seconds = kbytes != 0 ? 30 : minutes * 60;
00172         while (!__db_util_interrupted()) {
00173                 if (verbose) {
00174                         (void)time(&now);
00175                         dbenv->errx(dbenv, "checkpoint begin: %s", ctime(&now));
00176                 }
00177 
00178                 if ((ret = dbenv->txn_checkpoint(dbenv,
00179                     kbytes, minutes, flags)) != 0) {
00180                         dbenv->err(dbenv, ret, "txn_checkpoint");
00181                         goto shutdown;
00182                 }
00183 
00184                 if (verbose) {
00185                         (void)time(&now);
00186                         dbenv->errx(dbenv,
00187                             "checkpoint complete: %s", ctime(&now));
00188                 }
00189 
00190                 if (once)
00191                         break;
00192 
00193                 __os_sleep(dbenv, seconds, 0);
00194         }
00195 
00196         if (0) {
00197 shutdown:       exitval = 1;
00198         }
00199 
00200         /* Clean up the logfile. */
00201         if (logfile != NULL)
00202                 (void)remove(logfile);
00203 
00204         /* Clean up the environment. */
00205         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00206                 exitval = 1;
00207                 fprintf(stderr,
00208                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00209         }
00210 
00211         if (passwd != NULL)
00212                 free(passwd);
00213 
00214         /* Resend any caught signal. */
00215         __db_util_sigresend();
00216 
00217         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00218 }
00219 
00220 int
00221 usage()
00222 {
00223         (void)fprintf(stderr, "usage: %s [-1Vv]\n\t%s\n", progname,
00224             "[-h home] [-k kbytes] [-L file] [-P password] [-p min]");
00225         return (EXIT_FAILURE);
00226 }
00227 
00228 int
00229 version_check()
00230 {
00231         int v_major, v_minor, v_patch;
00232 
00233         /* Make sure we're loaded with the right version of the DB library. */
00234         (void)db_version(&v_major, &v_minor, &v_patch);
00235         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00236                 fprintf(stderr,
00237         "%s: version %d.%d doesn't match library version %d.%d\n",
00238                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00239                     v_major, v_minor);
00240                 return (EXIT_FAILURE);
00241         }
00242         return (0);
00243 }

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