summaryrefslogtreecommitdiffstats
path: root/ctdb/utils/tdb/tdb_mutex_check.c
blob: 440bd48e3fa06b069a8f40ff19db740af7b14268 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
   Check the mutex lock information in tdb database

   Copyright (C) Amitay Isaacs 2015-2021

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <pthread.h>
#include <errno.h>

#ifndef USE_TDB_MUTEX_LOCKING
#define USE_TDB_MUTEX_LOCKING  1
#endif

#include "lib/tdb/common/tdb_private.h"
#include "lib/tdb/common/mutex.c"

static uint8_t *hex_decode(const char *hex_in, size_t *plen)
{
	size_t i;
	int num;
	uint8_t *buffer;
	size_t len;

	len = strlen(hex_in) / 2;
	if (len == 0) {
		return NULL;
	}

	buffer = malloc(len);
	if (buffer == NULL) {
		return NULL;
	}

	for (i = 0; i < len; i++) {
		sscanf(&hex_in[i*2], "%02X", &num);
		buffer[i] = (uint8_t)num;
	}

	*plen = len;

	return buffer;
}

static int get_hash_chain(struct tdb_context *tdb, const char *hex_key)
{
	TDB_DATA key = {
		.dsize = 0,
	};
	unsigned int hash;

	key.dptr = hex_decode(hex_key, &key.dsize);
	if (key.dptr == NULL || key.dsize == 0) {
		return -1;
	}
	hash = tdb_jenkins_hash(&key);
	free(key.dptr);

	return hash % tdb_hash_size(tdb);
}

static void check_one(struct tdb_mutexes *mutexes, int chain)
{
	pthread_mutex_t *m;
	int ret;
	int pthread_mutex_consistent_np(pthread_mutex_t *);

	m = &mutexes->hashchains[chain+1];
	ret = pthread_mutex_trylock(m);
	if (ret == 0) {
		pthread_mutex_unlock(m);
		return;
	}
	if (ret == EOWNERDEAD) {
		ret = pthread_mutex_consistent_np(m);
		if (ret != 0) {
			printf("[%6d] consistent failed (%d)\n", chain, ret);
			return;
		}
		ret = pthread_mutex_unlock(m);
		if (ret != 0) {
			printf("[%6d] unlock failed (%d)\n", chain, ret);
			return;
		}
		printf("[%6d] cleaned\n", chain);
		return;
	}
	if (ret == EBUSY) {
		printf("[%6d] pid=%d\n", chain, m->__data.__owner);
		return;
	}
	printf("[%6d] trylock failed (%d)\n", chain, ret);
}

static void check_all(struct tdb_mutexes *mutexes, unsigned int hash_size)
{
	unsigned int i;

	for (i=0; i<hash_size; i++) {
		check_one(mutexes, i);
	}
}

int main(int argc, char **argv)
{
	const char *tdb_file;
	TDB_CONTEXT *tdb;
	uint32_t tdb_flags;
	int chain, i;

	if (argc < 2) {
		printf("Usage %s <tdb file> [<key1> <key2>]\n", argv[0]);
		exit(1);
	}

	tdb_file = argv[1];

	tdb_flags = TDB_MUTEX_LOCKING | TDB_INCOMPATIBLE_HASH |
		    TDB_CLEAR_IF_FIRST;
	tdb = tdb_open(tdb_file, 0, tdb_flags, O_RDWR, 0);
	if (tdb == NULL) {
		printf("Error opening %s\n", tdb_file);
		exit(1);
	}

	if (tdb->mutexes == NULL) {
		printf("Mutexes are not mmapped\n");
		exit(1);
	}

	if (argc == 2) {
		check_all(tdb->mutexes, tdb_hash_size(tdb));
	} else {
		for (i=2; i<argc; i++) {
			chain = get_hash_chain(tdb, argv[i]);
			if (chain == -1) {
				continue;
			}
			check_one(tdb->mutexes, chain);
		}
	}

	tdb_close(tdb);
	return 0;
}