2019-10-04 18:44:25 +02:00
|
|
|
/* Read an image and check that file handles are being closed on minimise.
|
|
|
|
*
|
2022-11-27 16:43:35 +01:00
|
|
|
* This will only work on linux: we signal success and do nothing if
|
|
|
|
* /proc/self/fd does not exist.
|
2019-10-04 18:44:25 +02:00
|
|
|
*/
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
#include <stdio.h>
|
2019-10-04 18:44:25 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
#define _GNU_SOURCE
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2019-10-04 18:44:25 +02:00
|
|
|
#include <vips/vips.h>
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
/**
|
|
|
|
* get_open_files:
|
|
|
|
*
|
|
|
|
* Get a list of open files for this process.
|
|
|
|
*
|
|
|
|
* Returns (transfer full) (nullable): a new #GSList, or %NULL
|
2019-10-04 18:44:25 +02:00
|
|
|
*/
|
2022-11-27 16:43:35 +01:00
|
|
|
static GSList *
|
|
|
|
get_open_files()
|
2019-10-04 18:44:25 +02:00
|
|
|
{
|
2022-11-27 16:43:35 +01:00
|
|
|
GSList *list = NULL;
|
2019-10-04 18:44:25 +02:00
|
|
|
GDir *dir;
|
2022-11-27 16:43:35 +01:00
|
|
|
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 );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
list = g_slist_prepend( list, realpath( fullname, NULL ) );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
g_free( fullname );
|
|
|
|
}
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
g_dir_close( dir );
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
return( list );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
2019-10-04 18:44:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
main( int argc, char **argv )
|
|
|
|
{
|
2019-12-29 22:40:21 +01:00
|
|
|
VipsSource *source;
|
2019-10-04 18:44:25 +02:00
|
|
|
VipsImage *image, *x;
|
2022-11-27 16:43:35 +01:00
|
|
|
GSList *list;
|
2019-10-04 18:44:25 +02:00
|
|
|
double average;
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
if( VIPS_INIT( argv[0] ) )
|
|
|
|
vips_error_exit( "unable to start" );
|
2019-10-04 18:44:25 +02:00
|
|
|
|
2019-10-06 11:55:19 +02:00
|
|
|
if( argc != 2 )
|
|
|
|
vips_error_exit( "usage: %s test-image", argv[0] );
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
list = get_open_files();
|
|
|
|
if( list == NULL )
|
2019-10-04 18:44:25 +02:00
|
|
|
/* Probably not linux, silent success.
|
|
|
|
*/
|
|
|
|
return( 0 );
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
/* 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.
|
2019-10-04 18:44:25 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Opening an image should read the header, then close the fd.
|
|
|
|
*/
|
2021-09-07 12:14:45 +02:00
|
|
|
printf( "** rand open ..\n" );
|
2019-12-29 22:40:21 +01:00
|
|
|
if( !(source = vips_source_new_from_file( argv[1] )) )
|
2022-11-27 16:43:35 +01:00
|
|
|
goto error;
|
2019-12-29 22:40:21 +01:00
|
|
|
if( !(image = vips_image_new_from_source( source, "",
|
2021-09-07 12:14:45 +02:00
|
|
|
"access", VIPS_ACCESS_RANDOM,
|
2019-10-04 18:44:25 +02:00
|
|
|
NULL )) )
|
2022-11-27 16:43:35 +01:00
|
|
|
goto error;
|
|
|
|
if( !fd_check( "header read", list ) )
|
|
|
|
goto error;
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* We should be able to read a chunk near the top, then have the fd
|
|
|
|
* closed again.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** crop1, avg ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
if( vips_crop( image, &x, 0, 0, image->Xsize, 10, NULL ) ||
|
|
|
|
vips_avg( x, &average, NULL ) )
|
2022-11-27 16:43:35 +01:00
|
|
|
goto error;
|
|
|
|
|
2019-10-04 18:44:25 +02:00
|
|
|
g_object_unref( x );
|
2022-11-27 16:43:35 +01:00
|
|
|
if( !fd_check( "first read", list ) )
|
|
|
|
goto error;
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* We should be able to read again, a little further down, and have
|
|
|
|
* the input restarted and closed again.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** crop2, avg ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
if( vips_crop( image, &x, 0, 20, image->Xsize, 10, NULL ) ||
|
|
|
|
vips_avg( x, &average, NULL ) )
|
2022-11-27 16:43:35 +01:00
|
|
|
goto error;
|
|
|
|
|
2019-10-04 18:44:25 +02:00
|
|
|
g_object_unref( x );
|
2022-11-27 16:43:35 +01:00
|
|
|
if( !fd_check( "second read", list ) )
|
|
|
|
goto error;
|
2019-10-04 18:44:25 +02:00
|
|
|
|
|
|
|
/* Clean up, and we should still just have three open.
|
|
|
|
*/
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** unref ..\n" );
|
2019-10-06 08:25:09 +02:00
|
|
|
g_object_unref( image );
|
2019-12-29 22:40:21 +01:00
|
|
|
g_object_unref( source );
|
2019-10-27 19:19:43 +01:00
|
|
|
printf( "** shutdown ..\n" );
|
2019-10-04 18:44:25 +02:00
|
|
|
vips_shutdown();
|
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
if( !fd_check( "shutdown", list ) )
|
|
|
|
goto error;
|
2019-10-04 18:44:25 +02:00
|
|
|
|
2022-11-27 16:43:35 +01:00
|
|
|
g_slist_free_full( list, g_free );
|
2019-10-04 18:44:25 +02:00
|
|
|
return( 0 );
|
2022-11-27 16:43:35 +01:00
|
|
|
|
|
|
|
error:
|
|
|
|
g_slist_free_full( list, g_free );
|
|
|
|
return( 1 );
|
2019-10-04 18:44:25 +02:00
|
|
|
}
|