Back to index

libdrm  2.4.37
gem_flink.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2008 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  *
00023  * Authors:
00024  *    Eric Anholt <eric@anholt.net>
00025  *
00026  */
00027 
00028 #include <stdlib.h>
00029 #include <stdio.h>
00030 #include <string.h>
00031 #include <assert.h>
00032 #include <fcntl.h>
00033 #include <inttypes.h>
00034 #include <errno.h>
00035 #include <sys/stat.h>
00036 #include "drm.h"
00037 #include "i915_drm.h"
00038 
00039 static void
00040 test_flink(int fd)
00041 {
00042        struct drm_i915_gem_create create;
00043        struct drm_gem_flink flink;
00044        struct drm_gem_open open;
00045        int ret;
00046 
00047        printf("Testing flink and open.\n");
00048 
00049        memset(&create, 0, sizeof(create));
00050        create.size = 16 * 1024;
00051        ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
00052        assert(ret == 0);
00053 
00054        flink.handle = create.handle;
00055        ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
00056        assert(ret == 0);
00057 
00058        open.name = flink.name;
00059        ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
00060        assert(ret == 0);
00061        assert(open.handle != 0);
00062 }
00063 
00064 static void
00065 test_double_flink(int fd)
00066 {
00067        struct drm_i915_gem_create create;
00068        struct drm_gem_flink flink;
00069        struct drm_gem_flink flink2;
00070        int ret;
00071 
00072        printf("Testing repeated flink.\n");
00073 
00074        memset(&create, 0, sizeof(create));
00075        create.size = 16 * 1024;
00076        ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
00077        assert(ret == 0);
00078 
00079        flink.handle = create.handle;
00080        ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
00081        assert(ret == 0);
00082 
00083        flink2.handle = create.handle;
00084        ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2);
00085        assert(ret == 0);
00086        assert(flink2.name == flink.name);
00087 }
00088 
00089 static void
00090 test_bad_flink(int fd)
00091 {
00092        struct drm_gem_flink flink;
00093        int ret;
00094 
00095        printf("Testing error return on bad flink ioctl.\n");
00096 
00097        flink.handle = 0x10101010;
00098        ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
00099        assert(ret == -1 && errno == ENOENT);
00100 }
00101 
00102 static void
00103 test_bad_open(int fd)
00104 {
00105        struct drm_gem_open open;
00106        int ret;
00107 
00108        printf("Testing error return on bad open ioctl.\n");
00109 
00110        open.name = 0x10101010;
00111        ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
00112 
00113        assert(ret == -1 && errno == ENOENT);
00114 }
00115 
00116 int main(int argc, char **argv)
00117 {
00118        int fd;
00119 
00120        if (geteuid()) {
00121               fprintf(stderr, "requires root privileges, skipping\n");
00122               return 77;
00123        }
00124 
00125        fd = drm_open_matching("8086:*", 0);
00126        if (fd < 0) {
00127               fprintf(stderr, "failed to open intel drm device, skipping\n");
00128               return 77;
00129        }
00130 
00131        test_flink(fd);
00132        test_double_flink(fd);
00133        test_bad_flink(fd);
00134        test_bad_open(fd);
00135 
00136        return 0;
00137 }