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 <fcntl.h>
29 #include <fnmatch.h>
30 #include <sys/stat.h>
31 #include "drmtest.h"
32 
33 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
34 #include <libudev.h>
35 
is_master(int fd)36 static int is_master(int fd)
37 {
38 	drm_client_t client;
39 	int ret;
40 
41 	/* Check that we're the only opener and authed. */
42 	client.idx = 0;
43 	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
44 	assert (ret == 0);
45 	if (!client.auth)
46 		return 0;
47 	client.idx = 1;
48 	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
49 	if (ret != -1 || errno != EINVAL)
50 		return 0;
51 
52 	return 1;
53 }
54 
55 /** Open the first DRM device matching the criteria */
drm_open_matching(const char * pci_glob,int flags)56 int drm_open_matching(const char *pci_glob, int flags)
57 {
58 	struct udev *udev;
59 	struct udev_enumerate *e;
60 	struct udev_device *device, *parent;
61         struct udev_list_entry *entry;
62 	const char *pci_id, *path;
63 	int i, fd;
64 
65 	udev = udev_new();
66 	if (udev == NULL) {
67 		fprintf(stderr, "failed to initialize udev context\n");
68 		abort();
69 	}
70 
71 	fd = -1;
72 	e = udev_enumerate_new(udev);
73 	udev_enumerate_add_match_subsystem(e, "drm");
74         udev_enumerate_scan_devices(e);
75         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
76 		path = udev_list_entry_get_name(entry);
77 		device = udev_device_new_from_syspath(udev, path);
78 		parent = udev_device_get_parent(device);
79 		/* Filter out KMS output devices. */
80 		if (strcmp(udev_device_get_subsystem(parent), "pci") != 0)
81 			continue;
82 		pci_id = udev_device_get_property_value(parent, "PCI_ID");
83 		if (fnmatch(pci_glob, pci_id, 0) != 0)
84 			continue;
85 		fd = open(udev_device_get_devnode(device), O_RDWR);
86 		if (fd < 0)
87 			continue;
88 		if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
89 			close(fd);
90 			fd = -1;
91 			continue;
92 		}
93 
94 		break;
95 	}
96         udev_enumerate_unref(e);
97 	udev_unref(udev);
98 
99 	return fd;
100 }
101 
drm_open_any(void)102 int drm_open_any(void)
103 {
104 	int fd = drm_open_matching("*:*", 0);
105 
106 	if (fd < 0) {
107 		fprintf(stderr, "failed to open any drm device\n");
108 		abort();
109 	}
110 
111 	return fd;
112 }
113 
114 /**
115  * Open the first DRM device we can find where we end up being the master.
116  */
drm_open_any_master(void)117 int drm_open_any_master(void)
118 {
119 	int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
120 
121 	if (fd < 0) {
122 		fprintf(stderr, "failed to open any drm device\n");
123 		abort();
124 	}
125 
126 	return fd;
127 
128 }
129