2 * Copyright © 2007 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
29 * Tests various potential failures of the DRM locking mechanisms
43 unsigned int lock1 = 0x00001111;
44 unsigned int lock2 = 0x00002222;
46 /* return time in milliseconds */
52 gettimeofday(&tv, NULL);
53 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
57 wait_event(int pipe, enum auth_event expected_event)
60 enum auth_event event;
63 ret = read(commfd[pipe], &in, 1);
68 if (event != expected_event)
69 errx(1, "unexpected event: %d\n", event);
73 send_event(int pipe, enum auth_event send_event)
79 ret = write(commfd[pipe], &event, 1);
81 err(1, "failed to send event %d", event);
85 client_auth(int drmfd)
90 wait_event(0, SERVER_READY);
92 /* Get a client magic number and pass it to the master for auth. */
93 ret = ioctl(drmfd, DRM_IOCTL_GET_MAGIC, &auth);
95 err(1, "Couldn't get client magic");
96 send_event(0, CLIENT_MAGIC);
97 ret = write(commfd[0], &auth.magic, sizeof(auth.magic));
99 err(1, "Couldn't write auth data");
103 server_auth(int drmfd)
105 struct drm_auth auth;
108 send_event(1, SERVER_READY);
109 wait_event(1, CLIENT_MAGIC);
110 ret = read(commfd[1], &auth.magic, sizeof(auth.magic));
112 err(1, "Failure to read client magic");
114 ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
116 err(1, "Failure to authenticate client magic\n");
119 /** Tests that locking is successful in normal conditions */
121 test_lock_unlock(int drmfd)
125 ret = drmGetLock(drmfd, lock1, 0);
127 err(1, "Locking failed");
128 ret = drmUnlock(drmfd, lock1);
130 err(1, "Unlocking failed");
133 /** Tests that unlocking the lock while it's not held works correctly */
135 test_unlock_unlocked(int drmfd)
139 ret = drmUnlock(drmfd, lock1);
141 err(1, "Unlocking unlocked lock succeeded");
144 /** Tests that unlocking a lock held by another context fails appropriately */
146 test_unlock_unowned(int drmfd)
150 ret = drmGetLock(drmfd, lock1, 0);
152 ret = drmUnlock(drmfd, lock2);
154 errx(1, "Unlocking other context's lock succeeded");
155 ret = drmUnlock(drmfd, lock1);
160 * Tests that an open/close by the same process doesn't result in the lock
163 static void test_open_close_locked(drmfd)
167 ret = drmGetLock(drmfd, lock1, 0);
169 /* XXX: Need to make sure that this is the same device as drmfd */
170 tempfd = drm_open_any();
172 ret = drmUnlock(drmfd, lock1);
174 errx(1, "lock lost during open/close by same pid");
184 /* XXX: Should make sure we open the same DRM as the master */
185 drmfd = drm_open_any();
189 /* Wait for the server to grab the lock, then grab it ourselves (to
190 * contest it). Hopefully we hit it within the window of when the
193 wait_event(0, SERVER_LOCKED);
194 ret = drmGetLock(drmfd, lock2, 0);
197 err(1, "Failed to get lock on client\n");
198 drmUnlock(drmfd, lock2);
200 /* Tell the server that our locking completed, and when it did */
201 send_event(0, CLIENT_LOCKED);
202 ret = write(commfd[0], &time, sizeof(time));
209 int drmfd, tempfd, ret;
210 unsigned int client_time, unlock_time;
212 drmfd = drm_open_any_master();
214 test_lock_unlock(drmfd);
215 test_unlock_unlocked(drmfd);
216 test_unlock_unowned(drmfd);
217 test_open_close_locked(drmfd);
219 /* Perform the authentication sequence with the client. */
222 /* Now, test that the client attempting to lock while the server
223 * holds the lock works correctly.
225 ret = drmGetLock(drmfd, lock1, 0);
227 send_event(1, SERVER_LOCKED);
228 /* Wait a while for the client to do its thing */
230 ret = drmUnlock(drmfd, lock1);
232 unlock_time = get_millis();
234 wait_event(1, CLIENT_LOCKED);
235 ret = read(commfd[1], &client_time, sizeof(client_time));
237 err(1, "Failure to read client magic");
239 if (client_time < unlock_time)
240 errx(1, "Client took lock before server released it");
243 int main(int argc, char **argv)
250 err(1, "Couldn't create pipe");
254 err(1, "failure to fork client");