egl: Ignore certain environment variables when setuid/setgid.
[mesa.git] / src / egl / main / egldriver.c
1 /**
2 * Functions for choosing and opening/loading device drivers.
3 */
4
5
6 #include <assert.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include "eglconfig.h"
11 #include "eglcontext.h"
12 #include "egldefines.h"
13 #include "egldisplay.h"
14 #include "egldriver.h"
15 #include "eglglobals.h"
16 #include "egllog.h"
17 #include "eglmisc.h"
18 #include "eglmode.h"
19 #include "eglscreen.h"
20 #include "eglstring.h"
21 #include "eglsurface.h"
22 #include "eglimage.h"
23
24 #if defined(_EGL_PLATFORM_POSIX)
25 #include <dlfcn.h>
26 #include <sys/types.h>
27 #include <dirent.h>
28 #include <unistd.h>
29 #endif
30
31
32 /**
33 * Wrappers for dlopen/dlclose()
34 */
35 #if defined(_EGL_PLATFORM_WINDOWS)
36
37
38 /* XXX Need to decide how to do dynamic name lookup on Windows */
39 static const char DefaultDriverName[] = "TBD";
40
41 typedef HMODULE lib_handle;
42
43 static HMODULE
44 open_library(const char *filename)
45 {
46 return LoadLibrary(filename);
47 }
48
49 static void
50 close_library(HMODULE lib)
51 {
52 FreeLibrary(lib);
53 }
54
55
56 static const char *
57 library_suffix(void)
58 {
59 return ".dll";
60 }
61
62
63 #elif defined(_EGL_PLATFORM_POSIX)
64
65
66 static const char DefaultDriverName[] = "egl_glx";
67
68 typedef void * lib_handle;
69
70 static void *
71 open_library(const char *filename)
72 {
73 return dlopen(filename, RTLD_LAZY);
74 }
75
76 static void
77 close_library(void *lib)
78 {
79 dlclose(lib);
80 }
81
82
83 static const char *
84 library_suffix(void)
85 {
86 return ".so";
87 }
88
89
90 #else /* _EGL_PLATFORM_NO_OS */
91
92
93 static const char DefaultDriverName[] = "builtin";
94
95 typedef void *lib_handle;
96
97 static INLINE void *
98 open_library(const char *filename)
99 {
100 return (void *) filename;
101 }
102
103 static INLINE void
104 close_library(void *lib)
105 {
106 }
107
108
109 static const char *
110 library_suffix(void)
111 {
112 return NULL;
113 }
114
115
116 #endif
117
118
119 #define NUM_PROBE_CACHE_SLOTS 8
120 static struct {
121 EGLint keys[NUM_PROBE_CACHE_SLOTS];
122 const void *values[NUM_PROBE_CACHE_SLOTS];
123 } _eglProbeCache;
124
125
126 /**
127 * Open the named driver and find its bootstrap function: _eglMain().
128 */
129 static _EGLMain_t
130 _eglOpenLibrary(const char *driverPath, lib_handle *handle)
131 {
132 lib_handle lib;
133 _EGLMain_t mainFunc = NULL;
134 const char *error = "unknown error";
135
136 assert(driverPath);
137
138 _eglLog(_EGL_DEBUG, "dlopen(%s)", driverPath);
139 lib = open_library(driverPath);
140
141 #if defined(_EGL_PLATFORM_WINDOWS)
142 /* XXX untested */
143 if (lib)
144 mainFunc = (_EGLMain_t) GetProcAddress(lib, "_eglMain");
145 #elif defined(_EGL_PLATFORM_POSIX)
146 if (lib) {
147 mainFunc = (_EGLMain_t) dlsym(lib, "_eglMain");
148 if (!mainFunc)
149 error = dlerror();
150 }
151 else {
152 error = dlerror();
153 }
154 #else /* _EGL_PLATFORM_NO_OS */
155 /* must be the default driver name */
156 if (strcmp(driverPath, DefaultDriverName) == 0)
157 mainFunc = (_EGLMain_t) _eglMain;
158 else
159 error = "not builtin driver";
160 #endif
161
162 if (!lib) {
163 _eglLog(_EGL_WARNING, "Could not open driver %s (%s)",
164 driverPath, error);
165 if (!getenv("EGL_DRIVER"))
166 _eglLog(_EGL_WARNING,
167 "The driver can be overridden by setting EGL_DRIVER");
168 return NULL;
169 }
170
171 if (!mainFunc) {
172 _eglLog(_EGL_WARNING, "_eglMain not found in %s (%s)",
173 driverPath, error);
174 if (lib)
175 close_library(lib);
176 return NULL;
177 }
178
179 *handle = lib;
180 return mainFunc;
181 }
182
183
184 /**
185 * Load the named driver.
186 */
187 static _EGLDriver *
188 _eglLoadDriver(const char *path, const char *args)
189 {
190 _EGLMain_t mainFunc;
191 lib_handle lib;
192 _EGLDriver *drv = NULL;
193
194 mainFunc = _eglOpenLibrary(path, &lib);
195 if (!mainFunc)
196 return NULL;
197
198 drv = mainFunc(args);
199 if (!drv) {
200 if (lib)
201 close_library(lib);
202 return NULL;
203 }
204
205 if (!drv->Name) {
206 _eglLog(_EGL_WARNING, "Driver loaded from %s has no name", path);
207 drv->Name = "UNNAMED";
208 }
209
210 drv->Path = _eglstrdup(path);
211 drv->Args = (args) ? _eglstrdup(args) : NULL;
212 if (!drv->Path || (args && !drv->Args)) {
213 if (drv->Path)
214 free((char *) drv->Path);
215 if (drv->Args)
216 free((char *) drv->Args);
217 drv->Unload(drv);
218 if (lib)
219 close_library(lib);
220 return NULL;
221 }
222
223 drv->LibHandle = lib;
224
225 return drv;
226 }
227
228
229 /**
230 * Match a display to a preloaded driver.
231 *
232 * The matching is done by finding the driver with the highest score.
233 */
234 _EGLDriver *
235 _eglMatchDriver(_EGLDisplay *dpy)
236 {
237 _EGLDriver *best_drv = NULL;
238 EGLint best_score = -1, i;
239
240 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
241 _EGLDriver *drv = _eglGlobal.Drivers[i];
242 EGLint score;
243
244 score = (drv->Probe) ? drv->Probe(drv, dpy) : 0;
245 if (score > best_score) {
246 if (best_drv) {
247 _eglLog(_EGL_DEBUG, "driver %s has higher score than %s",
248 drv->Name, best_drv->Name);
249 }
250
251 best_drv = drv;
252 best_score = score;
253 /* perfect match */
254 if (score >= 100)
255 break;
256 }
257 }
258
259 return best_drv;
260 }
261
262
263 /**
264 * A preload function for use with _eglPreloadForEach. The preload data is the
265 * filename of the driver. This function stops on the first valid driver.
266 */
267 static EGLBoolean
268 _eglPreloadFile(const char *dir, size_t len, void *preload_data)
269 {
270 _EGLDriver *drv;
271 char path[1024];
272 const char *filename = (const char *) preload_data;
273 size_t flen = strlen(filename);
274
275 /* make a full path */
276 if (len + flen + 2 > sizeof(path))
277 return EGL_TRUE;
278 if (len) {
279 memcpy(path, dir, len);
280 path[len++] = '/';
281 }
282 memcpy(path + len, filename, flen);
283 len += flen;
284 path[len] = '\0';
285
286 drv = _eglLoadDriver(path, NULL);
287 /* fix the path and load again */
288 if (!drv && library_suffix()) {
289 const char *suffix = library_suffix();
290 size_t slen = strlen(suffix);
291 const char *p;
292 EGLBoolean need_suffix;
293
294 p = filename + flen - slen;
295 need_suffix = (p < filename || strcmp(p, suffix) != 0);
296 if (need_suffix && len + slen + 1 <= sizeof(path)) {
297 strcpy(path + len, suffix);
298 drv = _eglLoadDriver(path, NULL);
299 }
300 }
301 if (!drv)
302 return EGL_TRUE;
303
304 /* remember the driver and stop */
305 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
306 return EGL_FALSE;
307 }
308
309
310 /**
311 * A preload function for use with _eglPreloadForEach. The preload data is the
312 * pattern (prefix) of the files to look for.
313 */
314 static EGLBoolean
315 _eglPreloadPattern(const char *dir, size_t len, void *preload_data)
316 {
317 #if defined(_EGL_PLATFORM_POSIX)
318 const char *prefix, *suffix;
319 size_t prefix_len, suffix_len;
320 DIR *dirp;
321 struct dirent *dirent;
322 char path[1024];
323
324 if (len + 2 > sizeof(path))
325 return EGL_TRUE;
326 if (len) {
327 memcpy(path, dir, len);
328 path[len++] = '/';
329 }
330 path[len] = '\0';
331
332 dirp = opendir(path);
333 if (!dirp)
334 return EGL_TRUE;
335
336 prefix = (const char *) preload_data;
337 prefix_len = strlen(prefix);
338 suffix = library_suffix();
339 suffix_len = (suffix) ? strlen(suffix) : 0;
340
341 while ((dirent = readdir(dirp))) {
342 _EGLDriver *drv;
343 size_t dirent_len = strlen(dirent->d_name);
344 const char *p;
345
346 /* match the prefix */
347 if (strncmp(dirent->d_name, prefix, prefix_len) != 0)
348 continue;
349 /* match the suffix */
350 p = dirent->d_name + dirent_len - suffix_len;
351 if (p < dirent->d_name || strcmp(p, suffix) != 0)
352 continue;
353
354 /* make a full path and load the driver */
355 if (len + dirent_len + 1 <= sizeof(path)) {
356 strcpy(path + len, dirent->d_name);
357 drv = _eglLoadDriver(path, NULL);
358 if (drv)
359 _eglGlobal.Drivers[_eglGlobal.NumDrivers++] = drv;
360 }
361 }
362
363 closedir(dirp);
364
365 return EGL_TRUE;
366 #else /* _EGL_PLATFORM_POSIX */
367 /* stop immediately */
368 return EGL_FALSE;
369 #endif
370 }
371
372
373 /**
374 * Run the preload function on each driver directory and return the number of
375 * drivers loaded.
376 *
377 * The process may end prematurely if the callback function returns false.
378 */
379 static EGLint
380 _eglPreloadForEach(const char *search_path,
381 EGLBoolean (*preload)(const char *, size_t, void *),
382 void *preload_data)
383 {
384 const char *cur, *next;
385 size_t len;
386 EGLint num_drivers = _eglGlobal.NumDrivers;
387
388 cur = search_path;
389 while (cur) {
390 next = strchr(cur, ':');
391 len = (next) ? next - cur : strlen(cur);
392
393 if (!preload(cur, len, preload_data))
394 break;
395
396 cur = (next) ? next + 1 : NULL;
397 }
398
399 return (_eglGlobal.NumDrivers - num_drivers);
400 }
401
402
403 /**
404 * Return a list of colon-separated driver directories.
405 */
406 static const char *
407 _eglGetSearchPath(void)
408 {
409 static const char *search_path;
410
411 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
412 if (!search_path) {
413 static char buffer[1024];
414 const char *p;
415 int ret;
416
417 p = getenv("EGL_DRIVERS_PATH");
418 #if defined(_EGL_PLATFORM_POSIX)
419 if (p && (geteuid() != getuid() || getegid() != getgid())) {
420 _eglLog(_EGL_DEBUG,
421 "ignore EGL_DRIVERS_PATH for setuid/setgid binaries");
422 p = NULL;
423 }
424 #endif /* _EGL_PLATFORM_POSIX */
425
426 if (p) {
427 ret = snprintf(buffer, sizeof(buffer),
428 "%s:%s", p, _EGL_DRIVER_SEARCH_DIR);
429 if (ret > 0 && ret < sizeof(buffer))
430 search_path = buffer;
431 }
432 }
433 if (!search_path)
434 search_path = _EGL_DRIVER_SEARCH_DIR;
435 #else
436 search_path = "";
437 #endif
438
439 return search_path;
440 }
441
442
443 /**
444 * Preload a user driver.
445 *
446 * A user driver can be specified by EGL_DRIVER.
447 */
448 static EGLBoolean
449 _eglPreloadUserDriver(void)
450 {
451 #if defined(_EGL_PLATFORM_POSIX) || defined(_EGL_PLATFORM_WINDOWS)
452 const char *search_path = _eglGetSearchPath();
453 char *env;
454
455 env = getenv("EGL_DRIVER");
456 #if defined(_EGL_PLATFORM_POSIX)
457 if (env && strchr(env, '/')) {
458 search_path = "";
459 if ((geteuid() != getuid() || getegid() != getgid())) {
460 _eglLog(_EGL_DEBUG,
461 "ignore EGL_DRIVER for setuid/setgid binaries");
462 env = NULL;
463 }
464 }
465 #endif /* _EGL_PLATFORM_POSIX */
466 if (!env)
467 return EGL_FALSE;
468
469 if (!_eglPreloadForEach(search_path, _eglPreloadFile, (void *) env)) {
470 _eglLog(_EGL_WARNING, "EGL_DRIVER is set to an invalid driver");
471 return EGL_FALSE;
472 }
473
474 return EGL_TRUE;
475 #else /* _EGL_PLATFORM_POSIX || _EGL_PLATFORM_WINDOWS */
476 return EGL_FALSE;
477 #endif
478 }
479
480
481 /**
482 * Preload display drivers.
483 *
484 * Display drivers are a set of drivers that support a certain display system.
485 * The display system may be specified by EGL_DISPLAY.
486 *
487 * FIXME This makes libEGL a memory hog if an user driver is not specified and
488 * there are many display drivers.
489 */
490 static EGLBoolean
491 _eglPreloadDisplayDrivers(void)
492 {
493 #if defined(_EGL_PLATFORM_POSIX)
494 const char *dpy;
495 char prefix[32];
496 int ret;
497
498 dpy = getenv("EGL_DISPLAY");
499 if (!dpy || !dpy[0])
500 dpy = _EGL_DEFAULT_DISPLAY;
501 if (!dpy || !dpy[0])
502 return EGL_FALSE;
503
504 ret = snprintf(prefix, sizeof(prefix), "egl_%s_", dpy);
505 if (ret < 0 || ret >= sizeof(prefix))
506 return EGL_FALSE;
507
508 return (_eglPreloadForEach(_eglGetSearchPath(),
509 _eglPreloadPattern, (void *) prefix) > 0);
510 #else /* _EGL_PLATFORM_POSIX */
511 return EGL_FALSE;
512 #endif
513 }
514
515
516 /**
517 * Preload the default driver.
518 */
519 static EGLBoolean
520 _eglPreloadDefaultDriver(void)
521 {
522 return (_eglPreloadForEach(_eglGetSearchPath(),
523 _eglPreloadFile, (void *) DefaultDriverName) > 0);
524 }
525
526
527 /**
528 * Preload drivers.
529 *
530 * This function loads the driver modules and creates the corresponding
531 * _EGLDriver objects.
532 */
533 EGLBoolean
534 _eglPreloadDrivers(void)
535 {
536 EGLBoolean loaded;
537
538 /* already preloaded */
539 if (_eglGlobal.NumDrivers)
540 return EGL_TRUE;
541
542 loaded = (_eglPreloadUserDriver() ||
543 _eglPreloadDisplayDrivers() ||
544 _eglPreloadDefaultDriver());
545
546 return loaded;
547 }
548
549
550 /**
551 * Unload preloaded drivers.
552 */
553 void
554 _eglUnloadDrivers(void)
555 {
556 EGLint i;
557 for (i = 0; i < _eglGlobal.NumDrivers; i++) {
558 _EGLDriver *drv = _eglGlobal.Drivers[i];
559 lib_handle handle = drv->LibHandle;
560
561 if (drv->Path)
562 free((char *) drv->Path);
563 if (drv->Args)
564 free((char *) drv->Args);
565
566 /* destroy driver */
567 if (drv->Unload)
568 drv->Unload(drv);
569
570 if (handle)
571 close_library(handle);
572 _eglGlobal.Drivers[i] = NULL;
573 }
574
575 _eglGlobal.NumDrivers = 0;
576 }
577
578
579 /**
580 * Plug all the available fallback routines into the given driver's
581 * dispatch table.
582 */
583 void
584 _eglInitDriverFallbacks(_EGLDriver *drv)
585 {
586 /* If a pointer is set to NULL, then the device driver _really_ has
587 * to implement it.
588 */
589 drv->API.Initialize = NULL;
590 drv->API.Terminate = NULL;
591
592 drv->API.GetConfigs = _eglGetConfigs;
593 drv->API.ChooseConfig = _eglChooseConfig;
594 drv->API.GetConfigAttrib = _eglGetConfigAttrib;
595
596 drv->API.CreateContext = _eglCreateContext;
597 drv->API.DestroyContext = _eglDestroyContext;
598 drv->API.MakeCurrent = _eglMakeCurrent;
599 drv->API.QueryContext = _eglQueryContext;
600
601 drv->API.CreateWindowSurface = _eglCreateWindowSurface;
602 drv->API.CreatePixmapSurface = _eglCreatePixmapSurface;
603 drv->API.CreatePbufferSurface = _eglCreatePbufferSurface;
604 drv->API.DestroySurface = _eglDestroySurface;
605 drv->API.QuerySurface = _eglQuerySurface;
606 drv->API.SurfaceAttrib = _eglSurfaceAttrib;
607 drv->API.BindTexImage = _eglBindTexImage;
608 drv->API.ReleaseTexImage = _eglReleaseTexImage;
609 drv->API.SwapInterval = _eglSwapInterval;
610 drv->API.SwapBuffers = _eglSwapBuffers;
611 drv->API.CopyBuffers = _eglCopyBuffers;
612
613 drv->API.QueryString = _eglQueryString;
614 drv->API.WaitClient = _eglWaitClient;
615 drv->API.WaitNative = _eglWaitNative;
616
617 #ifdef EGL_MESA_screen_surface
618 drv->API.ChooseModeMESA = _eglChooseModeMESA;
619 drv->API.GetModesMESA = _eglGetModesMESA;
620 drv->API.GetModeAttribMESA = _eglGetModeAttribMESA;
621 drv->API.GetScreensMESA = _eglGetScreensMESA;
622 drv->API.CreateScreenSurfaceMESA = _eglCreateScreenSurfaceMESA;
623 drv->API.ShowScreenSurfaceMESA = _eglShowScreenSurfaceMESA;
624 drv->API.ScreenPositionMESA = _eglScreenPositionMESA;
625 drv->API.QueryScreenMESA = _eglQueryScreenMESA;
626 drv->API.QueryScreenSurfaceMESA = _eglQueryScreenSurfaceMESA;
627 drv->API.QueryScreenModeMESA = _eglQueryScreenModeMESA;
628 drv->API.QueryModeStringMESA = _eglQueryModeStringMESA;
629 #endif /* EGL_MESA_screen_surface */
630
631 #ifdef EGL_VERSION_1_2
632 drv->API.CreatePbufferFromClientBuffer = _eglCreatePbufferFromClientBuffer;
633 #endif /* EGL_VERSION_1_2 */
634
635 #ifdef EGL_KHR_image_base
636 drv->API.CreateImageKHR = _eglCreateImageKHR;
637 drv->API.DestroyImageKHR = _eglDestroyImageKHR;
638 #endif /* EGL_KHR_image_base */
639 }
640
641
642 /**
643 * Set the probe cache at the given key.
644 *
645 * A key, instead of a _EGLDriver, is used to allow the probe cache to be share
646 * by multiple drivers.
647 */
648 void
649 _eglSetProbeCache(EGLint key, const void *val)
650 {
651 EGLint idx;
652
653 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
654 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
655 break;
656 }
657 assert(key > 0);
658 assert(idx < NUM_PROBE_CACHE_SLOTS);
659
660 _eglProbeCache.keys[idx] = key;
661 _eglProbeCache.values[idx] = val;
662 }
663
664
665 /**
666 * Return the probe cache at the given key.
667 */
668 const void *
669 _eglGetProbeCache(EGLint key)
670 {
671 EGLint idx;
672
673 for (idx = 0; idx < NUM_PROBE_CACHE_SLOTS; idx++) {
674 if (!_eglProbeCache.keys[idx] || _eglProbeCache.keys[idx] == key)
675 break;
676 }
677
678 return (idx < NUM_PROBE_CACHE_SLOTS && _eglProbeCache.keys[idx] == key) ?
679 _eglProbeCache.values[idx] : NULL;
680 }