Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 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 | /** @file lock.c |
| 29 | * Tests various potential failures of the DRM locking mechanisms |
| 30 | */ |
| 31 | |
| 32 | #include <limits.h> |
| 33 | #include "drmtest.h" |
| 34 | |
| 35 | enum auth_event { |
| 36 | SERVER_READY, |
| 37 | CLIENT_MAGIC, |
| 38 | SERVER_LOCKED, |
| 39 | CLIENT_LOCKED, |
| 40 | }; |
| 41 | |
| 42 | int commfd[2]; |
| 43 | unsigned int lock1 = 0x00001111; |
| 44 | unsigned int lock2 = 0x00002222; |
| 45 | |
| 46 | /* return time in milliseconds */ |
| 47 | static unsigned int |
| 48 | get_millis() |
| 49 | { |
| 50 | struct timeval tv; |
| 51 | |
| 52 | gettimeofday(&tv, NULL); |
| 53 | return tv.tv_sec * 1000 + tv.tv_usec / 1000; |
| 54 | } |
| 55 | |
| 56 | static void |
| 57 | wait_event(int pipe, enum auth_event expected_event) |
| 58 | { |
| 59 | int ret; |
| 60 | enum auth_event event; |
| 61 | unsigned char in; |
| 62 | |
| 63 | ret = read(commfd[pipe], &in, 1); |
| 64 | if (ret == -1) |
| 65 | err(1, "read error"); |
| 66 | event = in; |
| 67 | |
| 68 | if (event != expected_event) |
| 69 | errx(1, "unexpected event: %d\n", event); |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | send_event(int pipe, enum auth_event send_event) |
| 74 | { |
| 75 | int ret; |
| 76 | unsigned char event; |
| 77 | |
| 78 | event = send_event; |
| 79 | ret = write(commfd[pipe], &event, 1); |
| 80 | if (ret == -1) |
| 81 | err(1, "failed to send event %d", event); |
| 82 | } |
| 83 | |
| 84 | static void |
| 85 | client_auth(int drmfd) |
| 86 | { |
| 87 | struct drm_auth auth; |
| 88 | int ret; |
| 89 | |
Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 90 | /* Get a client magic number and pass it to the master for auth. */ |
| 91 | ret = ioctl(drmfd, DRM_IOCTL_GET_MAGIC, &auth); |
| 92 | if (ret == -1) |
| 93 | err(1, "Couldn't get client magic"); |
| 94 | send_event(0, CLIENT_MAGIC); |
| 95 | ret = write(commfd[0], &auth.magic, sizeof(auth.magic)); |
| 96 | if (ret == -1) |
| 97 | err(1, "Couldn't write auth data"); |
| 98 | } |
| 99 | |
| 100 | static void |
| 101 | server_auth(int drmfd) |
| 102 | { |
| 103 | struct drm_auth auth; |
| 104 | int ret; |
| 105 | |
| 106 | send_event(1, SERVER_READY); |
| 107 | wait_event(1, CLIENT_MAGIC); |
| 108 | ret = read(commfd[1], &auth.magic, sizeof(auth.magic)); |
| 109 | if (ret == -1) |
| 110 | err(1, "Failure to read client magic"); |
| 111 | |
| 112 | ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth); |
| 113 | if (ret == -1) |
| 114 | err(1, "Failure to authenticate client magic\n"); |
| 115 | } |
| 116 | |
| 117 | /** Tests that locking is successful in normal conditions */ |
| 118 | static void |
| 119 | test_lock_unlock(int drmfd) |
| 120 | { |
| 121 | int ret; |
| 122 | |
| 123 | ret = drmGetLock(drmfd, lock1, 0); |
| 124 | if (ret != 0) |
| 125 | err(1, "Locking failed"); |
| 126 | ret = drmUnlock(drmfd, lock1); |
| 127 | if (ret != 0) |
| 128 | err(1, "Unlocking failed"); |
| 129 | } |
| 130 | |
| 131 | /** Tests that unlocking the lock while it's not held works correctly */ |
| 132 | static void |
| 133 | test_unlock_unlocked(int drmfd) |
| 134 | { |
| 135 | int ret; |
| 136 | |
| 137 | ret = drmUnlock(drmfd, lock1); |
| 138 | if (ret == 0) |
| 139 | err(1, "Unlocking unlocked lock succeeded"); |
| 140 | } |
| 141 | |
| 142 | /** Tests that unlocking a lock held by another context fails appropriately */ |
| 143 | static void |
| 144 | test_unlock_unowned(int drmfd) |
| 145 | { |
| 146 | int ret; |
| 147 | |
| 148 | ret = drmGetLock(drmfd, lock1, 0); |
| 149 | assert(ret == 0); |
| 150 | ret = drmUnlock(drmfd, lock2); |
| 151 | if (ret == 0) |
| 152 | errx(1, "Unlocking other context's lock succeeded"); |
| 153 | ret = drmUnlock(drmfd, lock1); |
| 154 | assert(ret == 0); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Tests that an open/close by the same process doesn't result in the lock |
| 159 | * being dropped. |
| 160 | */ |
| 161 | static void test_open_close_locked(drmfd) |
| 162 | { |
| 163 | int ret, tempfd; |
| 164 | |
| 165 | ret = drmGetLock(drmfd, lock1, 0); |
| 166 | assert(ret == 0); |
| 167 | /* XXX: Need to make sure that this is the same device as drmfd */ |
| 168 | tempfd = drm_open_any(); |
| 169 | close(tempfd); |
| 170 | ret = drmUnlock(drmfd, lock1); |
| 171 | if (ret != 0) |
| 172 | errx(1, "lock lost during open/close by same pid"); |
Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void client() |
| 176 | { |
| 177 | int drmfd, ret; |
| 178 | unsigned int time; |
| 179 | |
Robert Noland | 2c5c18f | 2007-10-17 13:25:31 -0700 | [diff] [blame] | 180 | wait_event(0, SERVER_READY); |
| 181 | |
Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 182 | /* XXX: Should make sure we open the same DRM as the master */ |
| 183 | drmfd = drm_open_any(); |
| 184 | |
| 185 | client_auth(drmfd); |
| 186 | |
| 187 | /* Wait for the server to grab the lock, then grab it ourselves (to |
| 188 | * contest it). Hopefully we hit it within the window of when the |
| 189 | * server locks. |
| 190 | */ |
| 191 | wait_event(0, SERVER_LOCKED); |
| 192 | ret = drmGetLock(drmfd, lock2, 0); |
| 193 | time = get_millis(); |
| 194 | if (ret != 0) |
| 195 | err(1, "Failed to get lock on client\n"); |
| 196 | drmUnlock(drmfd, lock2); |
| 197 | |
| 198 | /* Tell the server that our locking completed, and when it did */ |
| 199 | send_event(0, CLIENT_LOCKED); |
| 200 | ret = write(commfd[0], &time, sizeof(time)); |
| 201 | |
Robert Noland | 2c5c18f | 2007-10-17 13:25:31 -0700 | [diff] [blame] | 202 | close(drmfd); |
Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 203 | exit(0); |
| 204 | } |
| 205 | |
| 206 | static void server() |
| 207 | { |
| 208 | int drmfd, tempfd, ret; |
| 209 | unsigned int client_time, unlock_time; |
| 210 | |
| 211 | drmfd = drm_open_any_master(); |
| 212 | |
| 213 | test_lock_unlock(drmfd); |
| 214 | test_unlock_unlocked(drmfd); |
| 215 | test_unlock_unowned(drmfd); |
| 216 | test_open_close_locked(drmfd); |
| 217 | |
| 218 | /* Perform the authentication sequence with the client. */ |
| 219 | server_auth(drmfd); |
| 220 | |
| 221 | /* Now, test that the client attempting to lock while the server |
| 222 | * holds the lock works correctly. |
| 223 | */ |
| 224 | ret = drmGetLock(drmfd, lock1, 0); |
| 225 | assert(ret == 0); |
| 226 | send_event(1, SERVER_LOCKED); |
| 227 | /* Wait a while for the client to do its thing */ |
| 228 | sleep(1); |
| 229 | ret = drmUnlock(drmfd, lock1); |
| 230 | assert(ret == 0); |
| 231 | unlock_time = get_millis(); |
| 232 | |
| 233 | wait_event(1, CLIENT_LOCKED); |
| 234 | ret = read(commfd[1], &client_time, sizeof(client_time)); |
| 235 | if (ret == -1) |
| 236 | err(1, "Failure to read client magic"); |
| 237 | |
| 238 | if (client_time < unlock_time) |
| 239 | errx(1, "Client took lock before server released it"); |
Robert Noland | 2c5c18f | 2007-10-17 13:25:31 -0700 | [diff] [blame] | 240 | |
| 241 | close(drmfd); |
Eric Anholt | 9254e00 | 2007-08-15 13:41:24 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | int main(int argc, char **argv) |
| 245 | { |
| 246 | int ret; |
| 247 | |
| 248 | |
| 249 | ret = pipe(commfd); |
| 250 | if (ret == -1) |
| 251 | err(1, "Couldn't create pipe"); |
| 252 | |
| 253 | ret = fork(); |
| 254 | if (ret == -1) |
| 255 | err(1, "failure to fork client"); |
| 256 | if (ret == 0) |
| 257 | client(); |
| 258 | else |
| 259 | server(); |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | |