dlclose.c 2.57 KB
Newer Older
1
#include <dirent.h>
Aaron Plattner's avatar
Aaron Plattner committed
2
3
4
5
6
7
8
9
10
11
#include <dlfcn.h>
#include <stdio.h>
#include <vdpau/vdpau.h>
#include <vdpau/vdpau_x11.h>
#include <X11/Xlib.h>

#define PASS 0
#define FAIL 1
#define SKIP 77

12
13
14
15
16
17
18
19
20
21
22
static int countOpenFDs(void)
{
    DIR *dir = opendir("/proc/self/fd");
    int count = 0;

    if (!dir) {
        fprintf(stderr, "Couldn't open /proc/self/fd; skipping file descriptor "
                "leak test\n");
        return 0;
    }

23
    while (readdir(dir) != NULL) {
24
25
26
27
28
29
30
        count++;
    }

    closedir(dir);
    return count;
}

31
32
33
34
35
36
#if defined(__sun)
static const char libXext_so[] = "libXext.so.0";
#else
static const char libXext_so[] = "libXext.so.6";
#endif

37
int main(void)
Aaron Plattner's avatar
Aaron Plattner committed
38
39
40
41
{
    // Work around a bug in libXext: dlclosing it after it has registered the
    // Generic Event Extension causes an identical bug to the one this program
    // is trying to test for.
42
    int nOpenFDs = countOpenFDs();
43
    void *libXext = dlopen(libXext_so, RTLD_LAZY);
44
    void *libvdpau = dlopen("src/libvdpau.so", RTLD_LAZY);
Aaron Plattner's avatar
Aaron Plattner committed
45
46
47
48
49
50
51
    Display *dpy = XOpenDisplay(NULL);
    VdpDeviceCreateX11 *pvdp_device_create_x11;
    VdpDevice device;
    VdpGetProcAddress *get_proc_address;
    VdpStatus status;

    if (!libXext) {
52
        fprintf(stderr, "Failed to open %s: %s", libXext_so, dlerror());
Aaron Plattner's avatar
Aaron Plattner committed
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
        return SKIP;
    }

    if (!libvdpau) {
        fprintf(stderr, "Failed to open libvdpau.so: %s", dlerror());
        return FAIL;
    }

    if (!dpy) {
        fprintf(stderr, "Failed to connect to X display %s\n", XDisplayName(NULL));
        return SKIP;
    }

    pvdp_device_create_x11 = dlsym(libvdpau, "vdp_device_create_x11");
    if (!pvdp_device_create_x11) {
        fprintf(stderr, "Failed to find the symbol vdp_device_create_x11\n");
        return FAIL;
    }

    status = pvdp_device_create_x11(dpy, 0, &device, &get_proc_address);
    if (status == VDP_STATUS_OK) {
        // It's okay if creating the device fails.  This will still install the
        // DRI2 extension in libX11 and trigger the bug.
        VdpDeviceDestroy *pvdp_device_destroy;

        status = get_proc_address(device, VDP_FUNC_ID_DEVICE_DESTROY, (void**)&pvdp_device_destroy);
        if (status != VDP_STATUS_OK) {
            fprintf(stderr, "Failed to find the VdpDeviceDestroy function: %d\n", status);
            return FAIL;
        }

        pvdp_device_destroy(device);
    }

    dlclose(libvdpau);
    XCloseDisplay(dpy);

90
91
92
93
94
95
    // Make sure no file descriptors were leaked.
    if (countOpenFDs() != nOpenFDs) {
        fprintf(stderr, "Mismatch in the number of open file descriptors!\n");
        return FAIL;
    }

Aaron Plattner's avatar
Aaron Plattner committed
96
97
    return PASS;
}