File: test_descriptors.c

package info (click to toggle)
vips 8.17.3-2
  • links: PTS
  • area: main
  • in suites: sid
  • size: 52,228 kB
  • sloc: ansic: 169,684; cpp: 12,156; python: 4,887; sh: 733; perl: 40; makefile: 25; javascript: 6
file content (171 lines) | stat: -rw-r--r-- 3,845 bytes parent folder | download | duplicates (3)
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
166
167
168
169
170
171
/* Read an image and check that file handles are being closed on minimise.
 *
 * This will only work on linux: we signal success and do nothing if
 * /proc/self/fd does not exist.
 */

#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /*HAVE_UNISTD_H*/

#define _GNU_SOURCE
#include <stdlib.h>

#include <vips/vips.h>

/**
 * get_open_files:
 *
 * Get a list of open files for this process.
 *
 * Returns (transfer full) (nullable): a new #GSList, or %NULL
 */
static GSList *
get_open_files()
{
#ifdef G_OS_WIN32
	/* realpath() and /proc/self/fd is not available on Windows.
	 */
	return NULL;
#else
	GSList *list = NULL;
	GDir *dir;
	const char *name;

	if (!(dir = g_dir_open("/proc/self/fd", 0, NULL)))
		return NULL;

	while ((name = g_dir_read_name(dir))) {
		char *fullname = g_build_filename("/proc/self/fd", name, NULL);

		list = g_slist_prepend(list, realpath(fullname, NULL));

		g_free(fullname);
	}

	g_dir_close(dir);

	return list;
#endif
}

/**
 * fd_check:
 * @stage: the originating stage for the error message
 * @fds: a #GSList of file descriptors to check against
 *
 * Check for a leak by comparing the currently open files for this
 * process with the file descriptors in @fds. If there's a leak,
 * print an error message and return %FALSE.
 *
 * See also: get_open_files().
 *
 * Returns: %TRUE if there are no leaks; %FALSE otherwise
 */
static gboolean
fd_check(const char *stage, GSList *fds)
{
	GSList *unique_list = NULL, *list, *iter;

	list = get_open_files();

	for (iter = list; iter; iter = iter->next)
		if (!g_slist_find_custom(fds, iter->data,
				(GCompareFunc) g_strcmp0))
			unique_list = g_slist_prepend(unique_list, iter->data);

	if (unique_list == NULL) {
		g_slist_free_full(list, g_free);
		return TRUE;
	}

	fprintf(stderr, "%s: file descriptors not closed after %s:\n",
		vips_get_prgname(), stage);
	for (iter = unique_list; iter; iter = iter->next)
		fprintf(stderr, "%s\n", (char *) iter->data);

	g_slist_free(unique_list);
	g_slist_free_full(list, g_free);

	return FALSE;
}

int
main(int argc, char **argv)
{
	VipsSource *source;
	VipsImage *image, *x;
	GSList *list;
	double average;

	if (VIPS_INIT(argv[0]))
		vips_error_exit("unable to start");

	if (argc != 2)
		vips_error_exit("usage: %s test-image", argv[0]);

	list = get_open_files();
	if (list == NULL)
		/* Probably not *nix, skip test with return code 77.
		 */
		return 77;

	/* This is usually a list of 4 files. stdout / stdin / stderr plus one
	 * more made for us by glib, I think, doing what I don't know.
	 */

	/* Opening an image should read the header, then close the fd.
	 */
	printf("** rand open ..\n");
	if (!(source = vips_source_new_from_file(argv[1])))
		goto error;
	if (!(image = vips_image_new_from_source(source, "",
			  "access", VIPS_ACCESS_RANDOM,
			  NULL)))
		goto error;
	if (!fd_check("header read", list))
		goto error;

	/* We should be able to read a chunk near the top, then have the fd
	 * closed again.
	 */
	printf("** crop1, avg ..\n");
	if (vips_crop(image, &x, 0, 0, image->Xsize, 10, NULL) ||
		vips_avg(x, &average, NULL))
		goto error;

	g_object_unref(x);
	if (!fd_check("first read", list))
		goto error;

	/* We should be able to read again, a little further down, and have
	 * the input restarted and closed again.
	 */
	printf("** crop2, avg ..\n");
	if (vips_crop(image, &x, 0, 20, image->Xsize, 10, NULL) ||
		vips_avg(x, &average, NULL))
		goto error;

	g_object_unref(x);
	if (!fd_check("second read", list))
		goto error;

	/* Clean up, and we should still just have three open.
	 */
	printf("** unref ..\n");
	g_object_unref(image);
	g_object_unref(source);
	printf("** shutdown ..\n");
	vips_shutdown();

	if (!fd_check("shutdown", list))
		goto error;

	g_slist_free_full(list, g_free);
	return 0;

error:
	g_slist_free_full(list, g_free);
	return 1;
}