Add pread/pwrite ioctls to mmfs.
[platform/upstream/libdrm.git] / tests / drmtest.c
1 /*
2  * Copyright © 2007 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 <fcntl.h>
29 #include <sys/stat.h>
30 #include "drmtest.h"
31 #include "mmfs.h"
32
33 /** Open the first DRM device we can find, searching up to 16 device nodes */
34 int drm_open_any(void)
35 {
36         char name[20];
37         int i, fd;
38
39         for (i = 0; i < 16; i++) {
40                 sprintf(name, "/dev/dri/card%d", i);
41                 fd = open(name, O_RDWR);
42                 if (fd != -1)
43                         return fd;
44         }
45         abort();
46 }
47
48
49 /**
50  * Open the first DRM device we can find where we end up being the master.
51  */
52 int drm_open_any_master(void)
53 {
54         char name[20];
55         int i, fd;
56
57         for (i = 0; i < 16; i++) {
58                 drm_client_t client;
59                 int ret;
60
61                 sprintf(name, "/dev/dri/card%d", i);
62                 fd = open(name, O_RDWR);
63                 if (fd == -1)
64                         continue;
65
66                 /* Check that we're the only opener and authed. */
67                 client.idx = 0;
68                 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
69                 assert (ret == 0);
70                 if (!client.auth) {
71                         close(fd);
72                         continue;
73                 }
74                 client.idx = 1;
75                 ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
76                 if (ret != -1 || errno != EINVAL) {
77                         close(fd);
78                         continue;
79                 }
80                 return fd;
81         }
82         fprintf(stderr, "Couldn't find an un-controlled DRM device\n");
83         abort();
84 }
85
86 static void
87 create_mmfs_device()
88 {
89         struct stat sb;
90         int ret;
91
92         ret = stat(MMFS_DEVICE_PATH, &sb);
93
94         if (ret == 0)
95                 return;
96
97         ret = mknod(MMFS_DEVICE_PATH, S_IFCHR | S_IRUSR | S_IWUSR,
98                     makedev(MMFS_DEVICE_MAJOR, 0));
99
100         if (ret != 0)
101                 errx(1, "mknod()");
102 }
103
104 int
105 open_mmfs_device()
106 {
107         int fd;
108
109         create_mmfs_device();
110
111         fd = open(MMFS_DEVICE_PATH, O_RDWR);
112         if (fd == -1)
113                 errx(1, "open()");
114
115         return fd;
116 }