aboutsummaryrefslogtreecommitdiffstats
path: root/libtest/libtest_fd_tracking.c
blob: 81bd9cdd6cb53788c6333e6498b7a8226d3c6941 (plain) (blame)
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
161
162
163
164
165
/* See LICENSE file for copyright and license details. */
#include "common.h"
#ifndef TEST


struct fd {
	int name;
	int leakable;
};

static size_t nopened = 0u;
static struct fd *opened = NULL;

static atomic_flag spinlock = ATOMIC_FLAG_INIT;
static int tracking_state = -1;


static int
cmp_fd(const void *av, const void *bv)
{
	const struct fd *a = av;
	const struct fd *b = bv;
	return a->name - b->name;
}


static char *
get_path(int fd)
{
	char path[sizeof("/dev/fd/-") + 3u * sizeof(int)];
	int pathlen;
	char *ret = NULL;
	size_t size = 0;
	ssize_t r;

	pathlen = sprintf(path, "/dev/fd/%i", fd);
	assert(pathlen > (int)sizeof("/dev/fd/") - 1);
	assert((size_t)pathlen < sizeof(path));

	do {
		ret = realloc(ret, size += 128u);
		assert(ret);
		r = readlink(path, ret, size - 1u);
		assert(r >= 0);
		if (r < 0)
			return NULL;
	} while ((size_t)r >= size - 2u);

	ret[r] = '\0';
	return ret;

}


int
libtest_fd_tracking(int action)
{
	size_t new_nopened = 0u;
	struct fd *new_opened = NULL;
	DIR *dir;
	struct dirent *f;
	int ret = 1, dfd, name, digit;
	size_t i, j;
	char *path;
	int old_tracking_state;

	SPINLOCK(spinlock);
	old_tracking_state = tracking_state;
	tracking_state = action;
	SPINUNLOCK(spinlock);
	if (old_tracking_state == action)
		return 1;

	libtest_malloc_internal_usage++;

	dir = opendir("/dev/fd/");
	assert(dir != NULL);
	dfd = dirfd(dir);
next:
	while ((f = readdir(dir))) {
		name = 0;
		for (i = 0u; f->d_name[i]; i++) {
			if ('0' > f->d_name[i] || f->d_name[i] > '9')
				goto next;
			digit = (int)f->d_name[i] - (int)'0';
			if (name > (INT_MAX - digit) / 10)
				goto next;
			name = name * 10 + digit;
		}
		if (name == dfd)
			continue;

		new_opened = realloc(new_opened, (new_nopened + 1u) * sizeof(*new_opened));
		assert(new_opened);
		new_opened[new_nopened].name = name;
		new_opened[new_nopened].leakable = action;
		new_nopened += 1u;
	}
	closedir(dir);

	qsort(new_opened, new_nopened, sizeof(*new_opened), &cmp_fd);

	for (i = j = 0u; i < new_nopened || j < nopened;) {
		if (i == new_nopened) {
		old_unique:
			/* the file has been closed, that's great! */
			j++;
		} else if (j == nopened) {
		new_unique:
			if (action < 0) {
				ret = 0;
				path = get_path(new_opened[i].name);
				if (path)
					fprintf(stderr, "File descriptor leak: %i (%s)\n", new_opened[i].name, path);
				else
					fprintf(stderr, "File descriptor leak: %i\n", new_opened[i].name);
				free(path);
			}
			i++;
		} else if (new_opened[i].name < opened[j].name) {
			goto new_unique;
		} else if (new_opened[i].name > opened[j].name) {
			goto old_unique;
		} else {
			new_opened[i].leakable = opened[j].leakable;
			i++;
			j++;
		}
	}

	if (new_nopened == 0u || action < 0) {
		free(new_opened);
		new_opened = NULL;
		new_nopened = 0u;
	}

	free(opened);
	if (action >= 0) {
		opened = new_opened;
		nopened = new_nopened;
	} else {
		opened = NULL;
		nopened = 0u;
	}

	libtest_malloc_internal_usage--;
	return ret;
}


#else


int
main(void)
{
	SET_UP_ALARM();

	return 0;
}


#endif
/* TODO maybe test */
/* TODO maybe attempt to tracking where files are opened */