No empty .Rs/.Re
[netbsd-mini2440.git] / sys / external / bsd / drm / dist / tests / auth.c
blob9b6fca9482e19293ea53ad3f93bacb21f54aff5c
1 /*
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
13 * Software.
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.
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
28 #include <limits.h>
29 #include "drmtest.h"
31 enum auth_event {
32 SERVER_READY,
33 CLIENT_MAGIC,
34 CLIENT_DONE,
37 int commfd[2];
39 static void wait_event(int pipe, enum auth_event expected_event)
41 int ret;
42 enum auth_event event;
43 unsigned char in;
45 ret = read(commfd[pipe], &in, 1);
46 if (ret == -1)
47 err(1, "read error");
48 event = in;
50 if (event != expected_event)
51 errx(1, "unexpected event: %d\n", event);
54 static void
55 send_event(int pipe, enum auth_event send_event)
57 int ret;
58 unsigned char event;
60 event = send_event;
61 ret = write(commfd[pipe], &event, 1);
62 if (ret == -1)
63 err(1, "failed to send event %d", event);
66 static void client()
68 struct drm_auth auth;
69 int drmfd, ret;
71 /* XXX: Should make sure we open the same DRM as the master */
72 wait_event(0, SERVER_READY);
74 drmfd = drm_open_any();
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");
86 /* Signal that the client is completely done. */
87 send_event(0, CLIENT_DONE);
90 static void server()
92 int drmfd, ret;
93 struct drm_auth auth;
95 drmfd = drm_open_any_master();
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");
102 send_event(1, SERVER_READY);
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");
109 ret = ioctl(drmfd, DRM_IOCTL_AUTH_MAGIC, &auth);
110 if (ret == -1)
111 err(1, "Failure to authenticate client magic\n");
113 wait_event(1, CLIENT_DONE);
117 * Checks DRM authentication mechanisms.
119 int main(int argc, char **argv)
121 int ret;
123 ret = pipe(commfd);
124 if (ret == -1)
125 err(1, "Couldn't create pipe");
127 ret = fork();
128 if (ret == -1)
129 err(1, "failure to fork client");
130 if (ret == 0)
131 client();
132 else
133 server();
135 return 0;