00001
00002
00003
00004
00005
00006
00007
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 db_deadlock_main __P((int, char *[]));
00041 int db_deadlock_usage __P((void));
00042 int db_deadlock_version_check __P((void));
00043
00044 const char *progname;
00045
00046 int
00047 db_deadlock(args)
00048 char *args;
00049 {
00050 int argc;
00051 char **argv;
00052
00053 __db_util_arg("db_deadlock", args, &argc, &argv);
00054 return (db_deadlock_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS);
00055 }
00056
00057 #include <stdio.h>
00058 #define ERROR_RETURN ERROR
00059
00060 int
00061 db_deadlock_main(argc, argv)
00062 int argc;
00063 char *argv[];
00064 {
00065 extern char *optarg;
00066 extern int optind, __db_getopt_reset;
00067 DB_ENV *dbenv;
00068 u_int32_t atype;
00069 time_t now;
00070 u_long secs, usecs;
00071 int ch, exitval, ret, verbose;
00072 char *home, *logfile, *passwd, *str;
00073
00074 if ((progname = strrchr(argv[0], '/')) == NULL)
00075 progname = argv[0];
00076 else
00077 ++progname;
00078
00079 if ((ret = db_deadlock_version_check()) != 0)
00080 return (ret);
00081
00082 dbenv = NULL;
00083 atype = DB_LOCK_DEFAULT;
00084 home = logfile = passwd = NULL;
00085 secs = usecs = 0;
00086 exitval = verbose = 0;
00087 __db_getopt_reset = 1;
00088 while ((ch = getopt(argc, argv, "a:h:L:P:t:Vvw")) != EOF)
00089 switch (ch) {
00090 case 'a':
00091 switch (optarg[0]) {
00092 case 'e':
00093 atype = DB_LOCK_EXPIRE;
00094 break;
00095 case 'm':
00096 atype = DB_LOCK_MAXLOCKS;
00097 break;
00098 case 'n':
00099 atype = DB_LOCK_MINLOCKS;
00100 break;
00101 case 'o':
00102 atype = DB_LOCK_OLDEST;
00103 break;
00104 case 'W':
00105 atype = DB_LOCK_MAXWRITE;
00106 break;
00107 case 'w':
00108 atype = DB_LOCK_MINWRITE;
00109 break;
00110 case 'y':
00111 atype = DB_LOCK_YOUNGEST;
00112 break;
00113 default:
00114 return (db_deadlock_usage());
00115
00116 }
00117 if (optarg[1] != '\0')
00118 return (db_deadlock_usage());
00119 break;
00120 case 'h':
00121 home = optarg;
00122 break;
00123 case 'L':
00124 logfile = optarg;
00125 break;
00126 case 'P':
00127 passwd = strdup(optarg);
00128 memset(optarg, 0, strlen(optarg));
00129 if (passwd == NULL) {
00130 fprintf(stderr, "%s: strdup: %s\n",
00131 progname, strerror(errno));
00132 return (EXIT_FAILURE);
00133 }
00134 case 't':
00135 if ((str = strchr(optarg, '.')) != NULL) {
00136 *str++ = '\0';
00137 if (*str != '\0' && __db_getulong(
00138 NULL, progname, str, 0, LONG_MAX, &usecs))
00139 return (EXIT_FAILURE);
00140 }
00141 if (*optarg != '\0' && __db_getulong(
00142 NULL, progname, optarg, 0, LONG_MAX, &secs))
00143 return (EXIT_FAILURE);
00144 if (secs == 0 && usecs == 0)
00145 return (db_deadlock_usage());
00146
00147 break;
00148
00149 case 'V':
00150 printf("%s\n", db_version(NULL, NULL, NULL));
00151 return (EXIT_SUCCESS);
00152 case 'v':
00153 verbose = 1;
00154 break;
00155 case 'w':
00156
00157 secs = 0;
00158 usecs = 100000;
00159 break;
00160 case '?':
00161 default:
00162 return (db_deadlock_usage());
00163 }
00164 argc -= optind;
00165 argv += optind;
00166
00167 if (argc != 0)
00168 return (db_deadlock_usage());
00169
00170
00171 __db_util_siginit();
00172
00173
00174 if (logfile != NULL && __db_util_logset(progname, logfile))
00175 goto shutdown;
00176
00177
00178
00179
00180
00181 if ((ret = db_env_create(&dbenv, 0)) != 0) {
00182 fprintf(stderr,
00183 "%s: db_env_create: %s\n", progname, db_strerror(ret));
00184 goto shutdown;
00185 }
00186
00187 dbenv->set_errfile(dbenv, stderr);
00188 dbenv->set_errpfx(dbenv, progname);
00189
00190 if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv,
00191 passwd, DB_ENCRYPT_AES)) != 0) {
00192 dbenv->err(dbenv, ret, "set_passwd");
00193 goto shutdown;
00194 }
00195
00196 if (verbose) {
00197 (void)dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1);
00198 (void)dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1);
00199 }
00200
00201
00202 if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0) {
00203 dbenv->err(dbenv, ret, "open");
00204 goto shutdown;
00205 }
00206
00207 while (!__db_util_interrupted()) {
00208 if (verbose) {
00209 (void)time(&now);
00210 dbenv->errx(dbenv, "running at %.24s", ctime(&now));
00211 }
00212
00213 if ((ret = dbenv->lock_detect(dbenv, 0, atype, NULL)) != 0) {
00214 dbenv->err(dbenv, ret, "DB_ENV->lock_detect");
00215 goto shutdown;
00216 }
00217
00218
00219 if (secs == 0 && usecs == 0)
00220 break;
00221 __os_sleep(dbenv, secs, usecs);
00222 }
00223
00224 if (0) {
00225 shutdown: exitval = 1;
00226 }
00227
00228
00229 if (logfile != NULL)
00230 (void)remove(logfile);
00231
00232
00233 if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) {
00234 exitval = 1;
00235 fprintf(stderr,
00236 "%s: dbenv->close: %s\n", progname, db_strerror(ret));
00237 }
00238
00239 if (passwd != NULL)
00240 free(passwd);
00241
00242
00243 __db_util_sigresend();
00244
00245 return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
00246 }
00247
00248 int
00249 db_deadlock_usage()
00250 {
00251 (void)fprintf(stderr,
00252 "usage: %s [-Vv] [-a e | m | n | o | W | w | y]\n\t%s\n", progname,
00253 "[-h home] [-L file] [-P password] [-t sec.usec]");
00254 return (EXIT_FAILURE);
00255 }
00256
00257 int
00258 db_deadlock_version_check()
00259 {
00260 int v_major, v_minor, v_patch;
00261
00262
00263 (void)db_version(&v_major, &v_minor, &v_patch);
00264 if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) {
00265 fprintf(stderr,
00266 "%s: version %d.%d doesn't match library version %d.%d\n",
00267 progname, DB_VERSION_MAJOR, DB_VERSION_MINOR,
00268 v_major, v_minor);
00269 return (EXIT_FAILURE);
00270 }
00271 return (0);
00272 }