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