1 /*
2  * Copyright © 2008 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  *
23  * Authors:
24  *    Eric Anholt <eric@anholt.net>
25  *
26  */
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <assert.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include "drm.h"
37 #include "i915_drm.h"
38 
39 static void
test_flink(int fd)40 test_flink(int fd)
41 {
42 	struct drm_i915_gem_create create;
43 	struct drm_gem_flink flink;
44 	struct drm_gem_open open;
45 	int ret;
46 
47 	printf("Testing flink and open.\n");
48 
49 	memset(&create, 0, sizeof(create));
50 	create.size = 16 * 1024;
51 	ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
52 	assert(ret == 0);
53 
54 	flink.handle = create.handle;
55 	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
56 	assert(ret == 0);
57 
58 	open.name = flink.name;
59 	ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
60 	assert(ret == 0);
61 	assert(open.handle != 0);
62 }
63 
64 static void
test_double_flink(int fd)65 test_double_flink(int fd)
66 {
67 	struct drm_i915_gem_create create;
68 	struct drm_gem_flink flink;
69 	struct drm_gem_flink flink2;
70 	int ret;
71 
72 	printf("Testing repeated flink.\n");
73 
74 	memset(&create, 0, sizeof(create));
75 	create.size = 16 * 1024;
76 	ret = ioctl(fd, DRM_IOCTL_I915_GEM_CREATE, &create);
77 	assert(ret == 0);
78 
79 	flink.handle = create.handle;
80 	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
81 	assert(ret == 0);
82 
83 	flink2.handle = create.handle;
84 	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink2);
85 	assert(ret == 0);
86 	assert(flink2.name == flink.name);
87 }
88 
89 static void
test_bad_flink(int fd)90 test_bad_flink(int fd)
91 {
92 	struct drm_gem_flink flink;
93 	int ret;
94 
95 	printf("Testing error return on bad flink ioctl.\n");
96 
97 	flink.handle = 0x10101010;
98 	ret = ioctl(fd, DRM_IOCTL_GEM_FLINK, &flink);
99 	assert(ret == -1 && errno == ENOENT);
100 }
101 
102 static void
test_bad_open(int fd)103 test_bad_open(int fd)
104 {
105 	struct drm_gem_open open;
106 	int ret;
107 
108 	printf("Testing error return on bad open ioctl.\n");
109 
110 	open.name = 0x10101010;
111 	ret = ioctl(fd, DRM_IOCTL_GEM_OPEN, &open);
112 
113 	assert(ret == -1 && errno == ENOENT);
114 }
115 
main(int argc,char ** argv)116 int main(int argc, char **argv)
117 {
118 	int fd;
119 
120 	if (geteuid()) {
121 		fprintf(stderr, "requires root privileges, skipping\n");
122 		return 77;
123 	}
124 
125 	fd = drm_open_matching("8086:*", 0);
126 	if (fd < 0) {
127 		fprintf(stderr, "failed to open intel drm device, skipping\n");
128 		return 77;
129 	}
130 
131 	test_flink(fd);
132 	test_double_flink(fd);
133 	test_bad_flink(fd);
134 	test_bad_open(fd);
135 
136 	return 0;
137 }
138