| 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_deadlock_main __P((int, char *[])); |
| 19 |
int db_deadlock_usage __P((void)); |
| 20 |
int db_deadlock_version_check __P((void)); |
| 21 |
|
| 22 |
const char *progname; |
| 23 |
|
| 24 |
int |
| 25 |
db_deadlock(args) |
| 26 |
char *args; |
| 27 |
{ |
| 28 |
int argc; |
| 29 |
char **argv; |
| 30 |
|
| 31 |
__db_util_arg("db_deadlock", args, &argc, &argv); |
| 32 |
return (db_deadlock_main(argc, argv) ? EXIT_FAILURE : EXIT_SUCCESS); |
| 33 |
} |
| 34 |
|
| 35 |
#include <stdio.h> |
| 36 |
#define ERROR_RETURN ERROR |
| 37 |
|
| 38 |
int |
| 39 |
db_deadlock_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 |
u_int32_t atype; |
| 47 |
time_t now; |
| 48 |
u_long secs, usecs; |
| 49 |
int rejected, ch, exitval, ret, verbose; |
| 50 |
char *home, *logfile, *passwd, *str, 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_deadlock_version_check()) != 0) |
| 58 |
return (ret); |
| 59 |
|
| 60 |
dbenv = NULL; |
| 61 |
atype = DB_LOCK_DEFAULT; |
| 62 |
home = logfile = passwd = NULL; |
| 63 |
secs = usecs = 0; |
| 64 |
exitval = verbose = 0; |
| 65 |
__db_getopt_reset = 1; |
| 66 |
while ((ch = getopt(argc, argv, "a:h:L:P:t:Vv")) != EOF) |
| 67 |
switch (ch) { |
| 68 |
case 'a': |
| 69 |
switch (optarg[0]) { |
| 70 |
case 'e': |
| 71 |
atype = DB_LOCK_EXPIRE; |
| 72 |
break; |
| 73 |
case 'm': |
| 74 |
atype = DB_LOCK_MAXLOCKS; |
| 75 |
break; |
| 76 |
case 'n': |
| 77 |
atype = DB_LOCK_MINLOCKS; |
| 78 |
break; |
| 79 |
case 'o': |
| 80 |
atype = DB_LOCK_OLDEST; |
| 81 |
break; |
| 82 |
case 'W': |
| 83 |
atype = DB_LOCK_MAXWRITE; |
| 84 |
break; |
| 85 |
case 'w': |
| 86 |
atype = DB_LOCK_MINWRITE; |
| 87 |
break; |
| 88 |
case 'y': |
| 89 |
atype = DB_LOCK_YOUNGEST; |
| 90 |
break; |
| 91 |
default: |
| 92 |
return (db_deadlock_usage()); |
| 93 |
/* NOTREACHED */ |
| 94 |
} |
| 95 |
if (optarg[1] != '\0') |
| 96 |
return (db_deadlock_usage()); |
| 97 |
break; |
| 98 |
case 'h': |
| 99 |
home = optarg; |
| 100 |
break; |
| 101 |
case 'L': |
| 102 |
logfile = optarg; |
| 103 |
break; |
| 104 |
case 'P': |
| 105 |
if (passwd != NULL) { |
| 106 |
fprintf(stderr, DB_STR("5136", |
| 107 |
"Password may not be specified twice")); |
| 108 |
free(passwd); |
| 109 |
return (EXIT_FAILURE); |
| 110 |
} |
| 111 |
passwd = strdup(optarg); |
| 112 |
memset(optarg, 0, strlen(optarg)); |
| 113 |
if (passwd == NULL) { |
| 114 |
fprintf(stderr, DB_STR_A("5100", |
| 115 |
"%s: strdup: %s\n", "%s %s\n"), |
| 116 |
progname, strerror(errno)); |
| 117 |
return (EXIT_FAILURE); |
| 118 |
} |
| 119 |
break; |
| 120 |
case 't': |
| 121 |
if ((str = strchr(optarg, '.')) != NULL) { |
| 122 |
*str++ = '\0'; |
| 123 |
if (*str != '\0' && __db_getulong( |
| 124 |
NULL, progname, str, 0, LONG_MAX, &usecs)) |
| 125 |
return (EXIT_FAILURE); |
| 126 |
} |
| 127 |
if (*optarg != '\0' && __db_getulong( |
| 128 |
NULL, progname, optarg, 0, LONG_MAX, &secs)) |
| 129 |
return (EXIT_FAILURE); |
| 130 |
if (secs == 0 && usecs == 0) |
| 131 |
return (db_deadlock_usage()); |
| 132 |
|
| 133 |
break; |
| 134 |
case 'V': |
| 135 |
printf("%s\n", db_version(NULL, NULL, NULL)); |
| 136 |
return (EXIT_SUCCESS); |
| 137 |
case 'v': |
| 138 |
verbose = 1; |
| 139 |
break; |
| 140 |
case '?': |
| 141 |
default: |
| 142 |
return (db_deadlock_usage()); |
| 143 |
} |
| 144 |
argc -= optind; |
| 145 |
argv += optind; |
| 146 |
|
| 147 |
if (argc != 0) |
| 148 |
return (db_deadlock_usage()); |
| 149 |
|
| 150 |
/* Handle possible interruptions. */ |
| 151 |
__db_util_siginit(); |
| 152 |
|
| 153 |
/* Log our process ID. */ |
| 154 |
if (logfile != NULL && __db_util_logset(progname, logfile)) |
| 155 |
goto err; |
| 156 |
|
| 157 |
/* |
| 158 |
* Create an environment object and initialize it for error |
| 159 |
* reporting. |
| 160 |
*/ |
| 161 |
if ((ret = db_env_create(&dbenv, 0)) != 0) { |
| 162 |
fprintf(stderr, |
| 163 |
"%s: db_env_create: %s\n", progname, db_strerror(ret)); |
| 164 |
goto err; |
| 165 |
} |
| 166 |
|
| 167 |
dbenv->set_errfile(dbenv, stderr); |
| 168 |
dbenv->set_errpfx(dbenv, progname); |
| 169 |
|
| 170 |
if (passwd != NULL && (ret = dbenv->set_encrypt(dbenv, |
| 171 |
passwd, DB_ENCRYPT_AES)) != 0) { |
| 172 |
dbenv->err(dbenv, ret, "set_passwd"); |
| 173 |
goto err; |
| 174 |
} |
| 175 |
|
| 176 |
if (verbose) { |
| 177 |
(void)dbenv->set_verbose(dbenv, DB_VERB_DEADLOCK, 1); |
| 178 |
(void)dbenv->set_verbose(dbenv, DB_VERB_WAITSFOR, 1); |
| 179 |
} |
| 180 |
|
| 181 |
/* An environment is required. */ |
| 182 |
if ((ret = dbenv->open(dbenv, home, DB_USE_ENVIRON, 0)) != 0) { |
| 183 |
dbenv->err(dbenv, ret, DB_STR("5101", "open")); |
| 184 |
goto err; |
| 185 |
} |
| 186 |
|
| 187 |
while (!__db_util_interrupted()) { |
| 188 |
if (verbose) { |
| 189 |
(void)time(&now); |
| 190 |
dbenv->errx(dbenv, DB_STR_A("5102", |
| 191 |
"running at %.24s", "%.24s"), |
| 192 |
__os_ctime(&now, time_buf)); |
| 193 |
} |
| 194 |
|
| 195 |
if ((ret = |
| 196 |
dbenv->lock_detect(dbenv, 0, atype, &rejected)) != 0) { |
| 197 |
dbenv->err(dbenv, ret, "DB_ENV->lock_detect"); |
| 198 |
goto err; |
| 199 |
} |
| 200 |
if (verbose) |
| 201 |
dbenv->errx(dbenv, DB_STR_A("5103", |
| 202 |
"rejected %d locks", "%d"), rejected); |
| 203 |
|
| 204 |
/* Make a pass every "secs" secs and "usecs" usecs. */ |
| 205 |
if (secs == 0 && usecs == 0) |
| 206 |
break; |
| 207 |
__os_yield(dbenv->env, secs, usecs); |
| 208 |
} |
| 209 |
|
| 210 |
if (0) { |
| 211 |
err: exitval = 1; |
| 212 |
} |
| 213 |
|
| 214 |
/* Clean up the logfile. */ |
| 215 |
if (logfile != NULL) |
| 216 |
(void)remove(logfile); |
| 217 |
|
| 218 |
/* Clean up the environment. */ |
| 219 |
if (dbenv != NULL && (ret = dbenv->close(dbenv, 0)) != 0) { |
| 220 |
exitval = 1; |
| 221 |
fprintf(stderr, |
| 222 |
"%s: dbenv->close: %s\n", progname, db_strerror(ret)); |
| 223 |
} |
| 224 |
|
| 225 |
if (passwd != NULL) |
| 226 |
free(passwd); |
| 227 |
|
| 228 |
/* Resend any caught signal. */ |
| 229 |
__db_util_sigresend(); |
| 230 |
|
| 231 |
return (exitval == 0 ? EXIT_SUCCESS : EXIT_FAILURE); |
| 232 |
} |
| 233 |
|
| 234 |
int |
| 235 |
db_deadlock_usage() |
| 236 |
{ |
| 237 |
(void)fprintf(stderr, |
| 238 |
"usage: %s [-Vv] [-a e | m | n | o | W | w | y]\n\t%s\n", progname, |
| 239 |
"[-h home] [-L file] [-P password] [-t sec.usec]"); |
| 240 |
return (EXIT_FAILURE); |
| 241 |
} |
| 242 |
|
| 243 |
int |
| 244 |
db_deadlock_version_check() |
| 245 |
{ |
| 246 |
int v_major, v_minor, v_patch; |
| 247 |
|
| 248 |
/* Make sure we're loaded with the right version of the DB library. */ |
| 249 |
(void)db_version(&v_major, &v_minor, &v_patch); |
| 250 |
if (v_major != DB_VERSION_MAJOR || v_minor != DB_VERSION_MINOR) { |
| 251 |
fprintf(stderr, DB_STR_A("5104", |
| 252 |
"%s: version %d.%d doesn't match library version %d.%d\n", |
| 253 |
"%s %d %d %d %d\n"), progname, DB_VERSION_MAJOR, |
| 254 |
DB_VERSION_MINOR, v_major, v_minor); |
| 255 |
return (EXIT_FAILURE); |
| 256 |
} |
| 257 |
return (0); |
| 258 |
} |