summaryrefslogtreecommitdiff
path: root/sysutils/libdrm/patches/patch-xf86drm_c
blob: 55c362fdedcf6ac7c7b611092b39aa34290c1731 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
Index: xf86drm.c
--- xf86drm.c.orig
+++ xf86drm.c
@@ -100,6 +100,10 @@
 #define DRM_MAJOR 226 /* Linux */
 #endif
 
+#ifdef __OpenBSD__
+#define X_PRIVSEP
+#endif
+
 #if defined(__OpenBSD__) || defined(__DragonFly__)
 struct drm_pciinfo {
 	uint16_t	domain;
@@ -805,7 +809,7 @@ static int drmMatchBusID(const char *id1, const char *
  * If any other failure happened then it will output error message using
  * drmMsg() call.
  */
-#if !UDEV
+#if !UDEV && !defined(__OpenBSD__)
 static int chown_check_return(const char *path, uid_t owner, gid_t group)
 {
         int rv;
@@ -834,6 +838,18 @@ static const char *drmGetDeviceName(int type)
     return NULL;
 }
 
+#ifdef X_PRIVSEP
+static int
+_priv_open_device(const char *path)
+{
+	drmMsg("_priv_open_device\n");
+	return open(path, O_RDWR);
+}
+
+drm_public int priv_open_device(const char *)
+	__attribute__((weak, alias ("_priv_open_device")));
+#endif
+
 /**
  * Open the DRM device, creating it if necessary.
  *
@@ -855,7 +871,7 @@ static int drmOpenDevice(dev_t dev, int minor, int typ
     int             fd;
     mode_t          devmode = DRM_DEV_MODE, serv_mode;
     gid_t           serv_group;
-#if !UDEV
+#if !UDEV && !defined(__OpenBSD__)
     int             isroot  = !geteuid();
     uid_t           user    = DRM_DEV_UID;
     gid_t           group   = DRM_DEV_GID;
@@ -873,6 +889,7 @@ static int drmOpenDevice(dev_t dev, int minor, int typ
         devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
     }
 
+#ifndef __OpenBSD__
 #if !UDEV
     if (stat(DRM_DIR_NAME, &st)) {
         if (!isroot)
@@ -919,14 +936,19 @@ wait_for_udev:
         }
     }
 #endif
+#endif /* __OpenBSD__ */
 
+#ifndef X_PRIVSEP
     fd = open(buf, O_RDWR | O_CLOEXEC);
+#else
+    fd = priv_open_device(buf);
+#endif
     drmMsg("drmOpenDevice: open result is %d, (%s)\n",
            fd, fd < 0 ? strerror(errno) : "OK");
     if (fd >= 0)
         return fd;
 
-#if !UDEV
+#if !UDEV && !defined(__OpenBSD__)
     /* Check if the device node is not what we expect it to be, and recreate it
      * and try again if so.
      */
@@ -978,8 +1000,13 @@ static int drmOpenMinor(int minor, int create, int typ
         return -EINVAL;
 
     sprintf(buf, dev_name, DRM_DIR_NAME, minor);
-    if ((fd = open(buf, O_RDWR | O_CLOEXEC)) >= 0)
-        return fd;
+#ifndef X_PRIVSEP
+    fd = open(buf, O_RDWR | O_CLOEXEC);
+#else
+    fd = priv_open_device(buf);
+#endif
+    if (fd >= 0)
+	return fd;
     return -errno;
 }
 
@@ -3504,7 +3531,11 @@ static char *drmGetMinorNameForFD(int fd, int type)
     const char *dev_name = drmGetDeviceName(type);
     unsigned int maj, min;
     int n;
+    int base = drmGetMinorBase(type);
 
+    if (base < 0)
+        return NULL;
+
     if (fstat(fd, &sbuf))
         return NULL;
 
@@ -3517,7 +3548,7 @@ static char *drmGetMinorNameForFD(int fd, int type)
     if (!dev_name)
         return NULL;
 
-    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
+    n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, base + min);
     if (n == -1 || n >= sizeof(buf))
         return NULL;
 
@@ -4583,60 +4614,6 @@ drm_device_has_rdev(drmDevicePtr device, dev_t find_rd
  */
 drm_public int drmGetDeviceFromDevId(dev_t find_rdev, uint32_t flags, drmDevicePtr *device)
 {
-#ifdef __OpenBSD__
-    /*
-     * DRI device nodes on OpenBSD are not in their own directory, they reside
-     * in /dev along with a large number of statically generated /dev nodes.
-     * Avoid stat'ing all of /dev needlessly by implementing this custom path.
-     */
-    drmDevicePtr     d;
-    char             node[PATH_MAX + 1];
-    const char      *dev_name;
-    int              node_type, subsystem_type;
-    int              maj, min, n, ret;
-    const int        max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
-    struct stat      sbuf;
-
-    if (device == NULL)
-        return -EINVAL;
-
-    maj = major(find_rdev);
-    min = minor(find_rdev);
-
-    if (!drmNodeIsDRM(maj, min))
-        return -EINVAL;
-
-    node_type = drmGetMinorType(maj, min);
-    if (node_type == -1)
-        return -ENODEV;
-
-    dev_name = drmGetDeviceName(node_type);
-    if (!dev_name)
-        return -EINVAL;
-
-    /* anything longer than this will be truncated in drmDeviceAlloc.
-     * Account for NULL byte
-     */
-    n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
-    if (n == -1 || n >= PATH_MAX)
-      return -errno;
-    if (n + 1 > max_node_length)
-        return -EINVAL;
-    if (stat(node, &sbuf))
-        return -EINVAL;
-
-    subsystem_type = drmParseSubsystemType(maj, min);
-    if (subsystem_type != DRM_BUS_PCI)
-        return -ENODEV;
-
-    ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
-    if (ret)
-        return ret;
-
-    *device = d;
-
-    return 0;
-#else
     drmDevicePtr local_devices[MAX_DRM_NODES];
     drmDevicePtr d;
     DIR *sysdir;
@@ -4700,7 +4677,6 @@ drm_public int drmGetDeviceFromDevId(dev_t find_rdev, 
     if (*device == NULL)
         return -ENODEV;
     return 0;
-#endif
 }
 
 drm_public int drmGetNodeTypeFromDevId(dev_t devid)