2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
11 static const char copyright
[] =
12 "@(#) Copyright (c) 1997\n\
13 Sleepycat Software Inc. All rights reserved.\n";
14 static const char sccsid
[] = "@(#)db_deadlock.c 10.15 (Sleepycat) 9/4/97";
17 #ifndef NO_SYSTEM_INCLUDES
18 #include <sys/types.h>
31 #include "common_ext.h"
33 #define BAD_KILLID 0xffffffff
35 DB_ENV
*db_init
__P((char *, int));
36 int logpid
__P((char *, int));
37 int main
__P((int, char *[]));
38 void onint
__P((int));
39 void siginit
__P((void));
40 void usage
__P((void));
44 *progname
= "db_deadlock"; /* Program name. */
57 int ch
, flags
, verbose
;
60 atype
= DB_LOCK_DEFAULT
;
61 home
= logfile
= NULL
;
64 while ((ch
= getopt(argc
, argv
, "a:h:L:t:vw")) != EOF
)
69 atype
= DB_LOCK_OLDEST
;
72 atype
= DB_LOCK_YOUNGEST
;
78 if (optarg
[1] != '\0')
88 get_long(optarg
, 1, LONG_MAX
, &seconds
);
94 LF_SET(DB_LOCK_CONFLICT
);
106 if (seconds
== 0 && !LF_ISSET(DB_LOCK_CONFLICT
)) {
107 warnx("at least one of -t and -w must be specified");
112 * We detect every second when we're running in DB_LOCK_CONFLICT mode.
117 /* Initialize the deadlock detector by opening the lock manager. */
118 dbenv
= db_init(home
, verbose
);
120 if (logfile
!= NULL
&& logpid(logfile
, 1)) {
121 (void)db_appexit(dbenv
);
125 while (!interrupted
) {
126 if (dbenv
->db_verbose
!= 0) {
128 __db_err(dbenv
, "Running at %s", ctime(&now
));
131 if ((errno
= lock_detect(dbenv
->lk_info
, flags
, atype
)) != 0)
134 /* Make a pass every "seconds" seconds. */
135 (void)__db_sleep(seconds
, 0);
139 (void)logpid(logfile
, 0);
142 (void)signal(interrupted
, SIG_DFL
);
143 (void)raise(interrupted
);
147 return (db_appexit(dbenv
));
151 db_init(home
, verbose
)
157 if ((dbenv
= (DB_ENV
*)calloc(sizeof(DB_ENV
), 1)) == NULL
) {
161 dbenv
->db_errfile
= stderr
;
162 dbenv
->db_errpfx
= progname
;
163 dbenv
->db_verbose
= verbose
;
165 if ((errno
= db_appinit(home
,
166 NULL
, dbenv
, DB_INIT_LOCK
| DB_USE_ENVIRON
)) != 0)
167 err(1, "db_appinit");
176 * Log that we're running.
179 logpid(fname
, is_open
)
187 if ((fp
= fopen(fname
, "w")) == NULL
) {
193 "%s: %lu %s", progname
, (u_long
)getpid(), ctime(&now
));
202 * Initialize the set of signals for which we want to clean up.
203 * Generally, we try not to leave the shared regions locked if
210 (void)signal(SIGHUP
, onint
);
212 (void)signal(SIGINT
, onint
);
214 (void)signal(SIGKILL
, onint
);
216 (void)signal(SIGTERM
, onint
);
221 * Interrupt signal handler.
227 if ((interrupted
= signo
) == 0)
228 interrupted
= SIGINT
;
234 (void)fprintf(stderr
,
235 "usage: db_deadlock [-vw] [-a m | o | y] [-h home] [-L file] [-t sec]\n");