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