OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ui/ozone/platform/drm/host/drm_device_handle.h" |
| 6 |
| 7 #include <fcntl.h> |
| 8 #include <sys/stat.h> |
| 9 #include <unistd.h> |
| 10 #include <xf86drm.h> |
| 11 #include <xf86drmMode.h> |
| 12 |
| 13 #include "base/files/file_path.h" |
| 14 #include "base/posix/eintr_wrapper.h" |
| 15 #include "base/threading/thread_restrictions.h" |
| 16 |
| 17 namespace ui { |
| 18 |
| 19 namespace { |
| 20 |
| 21 bool Authenticate(int fd) { |
| 22 drm_magic_t magic; |
| 23 memset(&magic, 0, sizeof(magic)); |
| 24 // We need to make sure the DRM device has enough privilege. Use the DRM |
| 25 // authentication logic to figure out if the device has enough permissions. |
| 26 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); |
| 27 } |
| 28 |
| 29 } // namespace |
| 30 |
| 31 DrmDeviceHandle::DrmDeviceHandle() { |
| 32 } |
| 33 |
| 34 DrmDeviceHandle::~DrmDeviceHandle() { |
| 35 base::ThreadRestrictions::AssertIOAllowed(); |
| 36 } |
| 37 |
| 38 bool DrmDeviceHandle::Initialize(const base::FilePath& path) { |
| 39 CHECK(path.DirName() == base::FilePath("/dev/dri")); |
| 40 base::ThreadRestrictions::AssertIOAllowed(); |
| 41 bool print_warning = true; |
| 42 while (true) { |
| 43 file_.reset(); |
| 44 int fd = HANDLE_EINTR(open(path.value().c_str(), O_RDWR | O_CLOEXEC)); |
| 45 if (fd < 0) { |
| 46 PLOG(ERROR) << "Failed to open " << path.value(); |
| 47 return false; |
| 48 } |
| 49 |
| 50 file_.reset(fd); |
| 51 struct stat64 file_info; |
| 52 if (fstat64(fd, &file_info)) { |
| 53 PLOG(ERROR) << "Failed to get file info " << path.value(); |
| 54 continue; |
| 55 } |
| 56 |
| 57 CHECK(!S_ISDIR(file_info.st_mode)); |
| 58 |
| 59 if (Authenticate(file_.get())) |
| 60 break; |
| 61 |
| 62 LOG_IF(WARNING, print_warning) << "Failed to authenticate " << path.value(); |
| 63 print_warning = false; |
| 64 usleep(100000); |
| 65 } |
| 66 |
| 67 VLOG(1) << "Succeeded authenticating " << path.value(); |
| 68 return true; |
| 69 } |
| 70 |
| 71 bool DrmDeviceHandle::IsValid() const { |
| 72 return file_.is_valid(); |
| 73 } |
| 74 |
| 75 base::ScopedFD DrmDeviceHandle::Duplicate() { |
| 76 DCHECK(file_.is_valid()); |
| 77 int fd = dup(file_.get()); |
| 78 if (fd < 0) { |
| 79 PLOG(ERROR) << "Failed to dup"; |
| 80 return base::ScopedFD(); |
| 81 } |
| 82 |
| 83 return base::ScopedFD(fd); |
| 84 } |
| 85 |
| 86 } // namespace ui |
OLD | NEW |