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 <limits.h>
29 #include <sys/ioctl.h>
30 #include "drmtest.h"
31
32 enum auth_event {
33 SERVER_READY,
34 CLIENT_MAGIC,
35 CLIENT_DONE,
36 };
37
38 int commfd[2];
39
wait_event(int pipe,enum auth_event expected_event)40 static void wait_event(int pipe, enum auth_event expected_event)
41 {
42 int ret;
43 enum auth_event event;
44 unsigned char in;
45
46 ret = read(commfd[pipe], &in, 1);
47 if (ret == -1)
48 err(1, "read error");
49 event = in;
50
51 if (event != expected_event)
52 errx(1, "unexpected event: %d\n", event);
53 }
54
55 static void
send_event(int pipe,enum auth_event send_event)56 send_event(int pipe, enum auth_event send_event)
57 {
58 int ret;
59 unsigned char event;
60
61 event = send_event;
62 ret = write(commfd[pipe], &event, 1);
63 if (ret == -1)
64 err(1, "failed to send event %d", event);
65 }
66
client()67 static void client()
68 {
69 struct drm_auth auth;
70 int drmfd, ret;
71
72 /* XXX: Should make sure we open the same DRM as the master */
73 wait_event(0, SERVER_READY);
74
75 drmfd = drm_open_any();
76
77 /* Get a client magic number and pass it to the master for auth. */
78 auth.magic = 0; /* Quiet valgrind */
79 ret = ioctl(drmfd, DRM_IOCTL_GET_MAGIC, &auth);
80 if (ret == -1)
81 err(1, "Couldn't get client magic");
82 send_event(0, CLIENT_MAGIC);
83 ret = write(commfd[0], &auth.magic, sizeof(auth.magic));
84 if (ret == -1)
85 err(1, "Couldn't write auth data");
86
87 /* Signal that the client is completely done. */
88 send_event(0, CLIENT_DONE);
89 }
90
server()91 static void server()
92 {
93 int drmfd, ret;
94 struct drm_auth auth;
95
96 drmfd = drm_open_any_master();
97
98 auth.magic = 0xd0d0d0d0;
99 ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
100 if (ret != -1 || errno != EINVAL)
101 errx(1, "Authenticating bad magic succeeded\n");
102
103 send_event(1, SERVER_READY);
104
105 wait_event(1, CLIENT_MAGIC);
106 ret = read(commfd[1], &auth.magic, sizeof(auth.magic));
107 if (ret == -1)
108 err(1, "Failure to read client magic");
109
110 ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
111 if (ret == -1)
112 err(1, "Failure to authenticate client magic\n");
113
114 wait_event(1, CLIENT_DONE);
115 }
116
117 /**
118 * Checks DRM authentication mechanisms.
119 */
main(int argc,char ** argv)120 int main(int argc, char **argv)
121 {
122 int ret;
123
124 ret = pipe(commfd);
125 if (ret == -1)
126 err(1, "Couldn't create pipe");
127
128 ret = fork();
129 if (ret == -1)
130 err(1, "failure to fork client");
131 if (ret == 0)
132 client();
133 else
134 server();
135
136 return 0;
137 }
138
139