Back to index

libdrm  2.4.37
drmtest.c
Go to the documentation of this file.
00001 /*
00002  * Copyright © 2007 Intel Corporation
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice (including the next
00012  * paragraph) shall be included in all copies or substantial portions of the
00013  * Software.
00014  *
00015  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
00018  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00020  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
00021  * IN THE SOFTWARE.
00022  *
00023  * Authors:
00024  *    Eric Anholt <eric@anholt.net>
00025  *
00026  */
00027 
00028 #include <string.h>
00029 #include <fcntl.h>
00030 #include <fnmatch.h>
00031 #include <sys/stat.h>
00032 #include <sys/ioctl.h>
00033 #include "drmtest.h"
00034 
00035 #define LIBUDEV_I_KNOW_THE_API_IS_SUBJECT_TO_CHANGE
00036 #include <libudev.h>
00037 
00038 static int is_master(int fd)
00039 {
00040        drm_client_t client;
00041        int ret;
00042 
00043        /* Check that we're the only opener and authed. */
00044        client.idx = 0;
00045        ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
00046        assert (ret == 0);
00047        if (!client.auth)
00048               return 0;
00049        client.idx = 1;
00050        ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
00051        if (ret != -1 || errno != EINVAL)
00052               return 0;
00053 
00054        return 1;
00055 }
00056 
00058 int drm_open_matching(const char *pci_glob, int flags)
00059 {
00060        struct udev *udev;
00061        struct udev_enumerate *e;
00062        struct udev_device *device, *parent;
00063         struct udev_list_entry *entry;
00064        const char *pci_id, *path;
00065        const char *usub, *dnode;
00066        int fd;
00067 
00068        udev = udev_new();
00069        if (udev == NULL) {
00070               fprintf(stderr, "failed to initialize udev context\n");
00071               abort();
00072        }
00073 
00074        fd = -1;
00075        e = udev_enumerate_new(udev);
00076        udev_enumerate_add_match_subsystem(e, "drm");
00077         udev_enumerate_scan_devices(e);
00078         udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
00079               path = udev_list_entry_get_name(entry);
00080               device = udev_device_new_from_syspath(udev, path);
00081               parent = udev_device_get_parent(device);
00082               usub = udev_device_get_subsystem(parent);
00083               /* Filter out KMS output devices. */
00084               if (!usub || (strcmp(usub, "pci") != 0))
00085                      continue;
00086               pci_id = udev_device_get_property_value(parent, "PCI_ID");
00087               if (fnmatch(pci_glob, pci_id, 0) != 0)
00088                      continue;
00089               dnode = udev_device_get_devnode(device);
00090               if (strstr(dnode, "control"))
00091                      continue;
00092               fd = open(dnode, O_RDWR);
00093               if (fd < 0)
00094                      continue;
00095               if ((flags & DRM_TEST_MASTER) && !is_master(fd)) {
00096                      close(fd);
00097                      fd = -1;
00098                      continue;
00099               }
00100 
00101               break;
00102        }
00103         udev_enumerate_unref(e);
00104        udev_unref(udev);
00105 
00106        return fd;
00107 }
00108 
00109 int drm_open_any(void)
00110 {
00111        int fd = drm_open_matching("*:*", 0);
00112 
00113        if (fd < 0) {
00114               fprintf(stderr, "failed to open any drm device\n");
00115               exit(0);
00116        }
00117 
00118        return fd;
00119 }
00120 
00124 int drm_open_any_master(void)
00125 {
00126        int fd = drm_open_matching("*:*", DRM_TEST_MASTER);
00127 
00128        if (fd < 0) {
00129               fprintf(stderr, "failed to open any drm device\n");
00130               exit(0);
00131        }
00132 
00133        return fd;
00134 
00135 }