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

db_deadlock.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_deadlock.c,v 12.4 2005/10/03 16:00:16 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 <limits.h>
00032 #include <stdio.h>
00033 #include <stdlib.h>
00034 #include <string.h>
00035 #include <unistd.h>
00036 #endif
00037 
00038 #include "db_int.h"
00039 
00040 int main __P((int, char *[]));
00041 int usage __P((void));
00042 int version_check __P((void));
00043 
00044 const char *progname;
00045 
00046 int
00047 main(argc, argv)
00048         int argc;
00049         char *argv[];
00050 {
00051         extern char *optarg;
00052         extern int optind;
00053         DB_ENV  *dbenv;
00054         u_int32_t atype;
00055         time_t now;
00056         u_long secs, usecs;
00057         int ch, exitval, ret, verbose;
00058         char *home, *logfile, *passwd, *str;
00059 
00060         if ((progname = strrchr(argv[0], '/')) == NULL)
00061                 progname = argv[0];
00062         else
00063                 ++progname;
00064 
00065         if ((ret = version_check()) != 0)
00066                 return (ret);
00067 
00068         dbenv = NULL;
00069         atype = DB_LOCK_DEFAULT;
00070         home = logfile = passwd = NULL;
00071         secs = usecs = 0;
00072         exitval = verbose = 0;
00073         while ((ch = getopt(argc, argv, "a:h:L:P:t:Vvw")) != EOF)
00074                 switch (ch) {
00075                 case 'a':
00076                         switch (optarg[0]) {
00077                         case 'e':
00078                                 atype = DB_LOCK_EXPIRE;
00079                                 break;
00080                         case 'm':
00081                                 atype = DB_LOCK_MAXLOCKS;
00082                                 break;
00083                         case 'n':
00084                                 atype = DB_LOCK_MINLOCKS;
00085                                 break;
00086                         case 'o':
00087                                 atype = DB_LOCK_OLDEST;
00088                                 break;
00089                         case 'W':
00090                                 atype = DB_LOCK_MAXWRITE;
00091                                 break;
00092                         case 'w':
00093                                 atype = DB_LOCK_MINWRITE;
00094                                 break;
00095                         case 'y':
00096                                 atype = DB_LOCK_YOUNGEST;
00097                                 break;
00098                         default:
00099                                 return (usage());
00100                                 /* NOTREACHED */
00101                         }
00102                         if (optarg[1] != '\0')
00103                                 return (usage());
00104                         break;
00105                 case 'h':
00106                         home = optarg;
00107                         break;
00108                 case 'L':
00109                         logfile = optarg;
00110                         break;
00111                 case 'P':
00112                         passwd = strdup(optarg);
00113                         memset(optarg, 0, strlen(optarg));
00114                         if (passwd == NULL) {
00115                                 fprintf(stderr, "%s: strdup: %s\n",
00116                                     progname, strerror(errno));
00117                                 return (EXIT_FAILURE);
00118                         }
00119                 case 't':
00120                         if ((str = strchr(optarg, '.')) != NULL) {
00121                                 *str++ = '\0';
00122                                 if (*str != '\0' && __db_getulong(
00123                                     NULL, progname, str, 0, LONG_MAX, &usecs))
00124                                         return (EXIT_FAILURE);
00125                         }
00126                         if (*optarg != '\0' && __db_getulong(
00127                             NULL, progname, optarg, 0, LONG_MAX, &secs))
00128                                 return (EXIT_FAILURE);
00129                         if (secs == 0 && usecs == 0)
00130                                 return (usage());
00131 
00132                         break;
00133 
00134                 case 'V':
00135                         printf("%s\n", db_version(NULL, NULL, NULL));
00136                         return (EXIT_SUCCESS);
00137                 case 'v':
00138                         verbose = 1;
00139                         break;
00140                 case 'w':                       /* Undocumented. */
00141                         /* Detect every 100ms (100000 us) when polling. */
00142                         secs = 0;
00143                         usecs = 100000;
00144                         break;
00145                 case '?':
00146                 default:
00147                         return (usage());
00148                 }
00149         argc -= optind;
00150         argv += optind;
00151 
00152         if (argc != 0)
00153                 return (usage());
00154 
00155         /* Handle possible interruptions. */
00156         __db_util_siginit();
00157 
00158         /* Log our process ID. */
00159         if (logfile != NULL && __db_util_logset(progname, logfile))
00160                 goto shutdown;
00161 
00162         /*
00163          * Create an environment object and initialize it for error
00164          * reporting.
00165          */
00166         if ((ret = db_env_create(&dbenv, 0)) != 0) {
00167                 fprintf(stderr,
00168                     "%s: db_env_create: %s\n", progname, db_strerror(ret));
00169                 goto shutdown;
00170         }
00171 
00172         dbenv->set_errfile(dbenv, stderr);
00173         dbenv->set_errpfx(dbenv, progname);
00174 
00175         if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00176             passwd, DB_ENCRYPT_AES)) != 0) {
00177                 dbenv->err(dbenv, ret, "set_passwd");
00178                 goto shutdown;
00179         }
00180 
00181         if (verbose) {
00182                 (void)dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
00183                 (void)dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1);
00184         }
00185 
00186         /* An environment is required. */
00187         if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0) {
00188                 dbenv->err(dbenv, ret, "open");
00189                 goto shutdown;
00190         }
00191 
00192         while (!__db_util_interrupted()) {
00193                 if (verbose) {
00194                         (void)time(&now);
00195                         dbenv->errx(dbenv, "running at %.24s", ctime(&now));
00196                 }
00197 
00198                 if ((ret = dbenv->lock_detect(dbenv, 0, atype, NULL)) != 0) {
00199                         dbenv->err(dbenv, ret, "DB_ENV->lock_detect");
00200                         goto shutdown;
00201                 }
00202 
00203                 /* Make a pass every "secs" secs and "usecs" usecs. */
00204                 if (secs == 0 && usecs == 0)
00205                         break;
00206                 __os_sleep(dbenv, secs, usecs);
00207         }
00208 
00209         if (0) {
00210 shutdown:       exitval = 1;
00211         }
00212 
00213         /* Clean up the logfile. */
00214         if (logfile != NULL)
00215                 (void)remove(logfile);
00216 
00217         /* Clean up the environment. */
00218         if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00219                 exitval = 1;
00220                 fprintf(stderr,
00221                     "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00222         }
00223 
00224         if (passwd != NULL)
00225                 free(passwd);
00226 
00227         /* Resend any caught signal. */
00228         __db_util_sigresend();
00229 
00230         return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00231 }
00232 
00233 int
00234 usage()
00235 {
00236         (void)fprintf(stderr,
00237             "usage: %s [-Vv] [-a e | m | n | o | W | w | y]\n\t%s\n", progname,
00238             "[-h home] [-L file] [-P password] [-t sec.usec]");
00239         return (EXIT_FAILURE);
00240 }
00241 
00242 int
00243 version_check()
00244 {
00245         int v_major, v_minor, v_patch;
00246 
00247         /* Make sure we're loaded with the right version of the DB library. */
00248         (void)db_version(&v_major, &v_minor, &v_patch);
00249         if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00250                 fprintf(stderr,
00251         "%s: version %d.%d doesn't match library version %d.%d\n",
00252                     progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00253                     v_major, v_minor);
00254                 return (EXIT_FAILURE);
00255         }
00256         return (0);
00257 }

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