| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ui/ozone/platform/drm/host/drm_device_handle.h" | 5 #include "ui/ozone/platform/drm/host/drm_device_handle.h" |
| 6 | 6 |
| 7 #include <fcntl.h> | 7 #include <fcntl.h> |
| 8 #include <xf86drm.h> | 8 #include <xf86drm.h> |
| 9 #include <xf86drmMode.h> | 9 #include <xf86drmMode.h> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
| 13 #include "base/posix/eintr_wrapper.h" | 13 #include "base/posix/eintr_wrapper.h" |
| 14 #include "base/threading/thread_restrictions.h" | 14 #include "base/threading/thread_restrictions.h" |
| 15 #include "base/time/time.h" |
| 15 | 16 |
| 16 namespace ui { | 17 namespace ui { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 21 // Sleep this many milliseconds before retrying after authentication fails. |
| 22 const int kAuthFailSleepMs = 100; |
| 23 |
| 24 // Log a warning after failing to authenticate for this many milliseconds. |
| 25 const int kLogAuthFailDelayMs = 1000; |
| 26 |
| 20 bool Authenticate(int fd) { | 27 bool Authenticate(int fd) { |
| 21 drm_magic_t magic; | 28 drm_magic_t magic; |
| 22 memset(&magic, 0, sizeof(magic)); | 29 memset(&magic, 0, sizeof(magic)); |
| 23 // We need to make sure the DRM device has enough privilege. Use the DRM | 30 // We need to make sure the DRM device has enough privilege. Use the DRM |
| 24 // authentication logic to figure out if the device has enough permissions. | 31 // authentication logic to figure out if the device has enough permissions. |
| 25 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); | 32 return !drmGetMagic(fd, &magic) && !drmAuthMagic(fd, magic); |
| 26 } | 33 } |
| 27 | 34 |
| 28 } // namespace | 35 } // namespace |
| 29 | 36 |
| 30 DrmDeviceHandle::DrmDeviceHandle() { | 37 DrmDeviceHandle::DrmDeviceHandle() { |
| 31 } | 38 } |
| 32 | 39 |
| 33 DrmDeviceHandle::~DrmDeviceHandle() { | 40 DrmDeviceHandle::~DrmDeviceHandle() { |
| 34 if (file_.is_valid()) | 41 if (file_.is_valid()) |
| 35 base::ThreadRestrictions::AssertIOAllowed(); | 42 base::ThreadRestrictions::AssertIOAllowed(); |
| 36 } | 43 } |
| 37 | 44 |
| 38 bool DrmDeviceHandle::Initialize(const base::FilePath& dev_path, | 45 bool DrmDeviceHandle::Initialize(const base::FilePath& dev_path, |
| 39 const base::FilePath& sys_path) { | 46 const base::FilePath& sys_path) { |
| 40 // Security folks have requested that we assert the graphics device has the | 47 // Security folks have requested that we assert the graphics device has the |
| 41 // expected path, so use a CHECK instead of a DCHECK. The sys_path is only | 48 // expected path, so use a CHECK instead of a DCHECK. The sys_path is only |
| 42 // used a label and is otherwise unvalidated. | 49 // used a label and is otherwise unvalidated. |
| 43 CHECK(dev_path.DirName() == base::FilePath("/dev/dri")); | 50 CHECK(dev_path.DirName() == base::FilePath("/dev/dri")); |
| 44 base::ThreadRestrictions::AssertIOAllowed(); | 51 base::ThreadRestrictions::AssertIOAllowed(); |
| 45 bool print_warning = true; | 52 |
| 53 int num_auth_attempts = 0; |
| 54 bool logged_warning = false; |
| 55 const base::TimeTicks start_time = base::TimeTicks::Now(); |
| 46 while (true) { | 56 while (true) { |
| 47 file_.reset(); | 57 file_.reset(); |
| 48 int fd = HANDLE_EINTR(open(dev_path.value().c_str(), O_RDWR | O_CLOEXEC)); | 58 int fd = HANDLE_EINTR(open(dev_path.value().c_str(), O_RDWR | O_CLOEXEC)); |
| 49 if (fd < 0) { | 59 if (fd < 0) { |
| 50 PLOG(ERROR) << "Failed to open " << dev_path.value(); | 60 PLOG(ERROR) << "Failed to open " << dev_path.value(); |
| 51 return false; | 61 return false; |
| 52 } | 62 } |
| 53 | 63 |
| 54 file_.reset(fd); | 64 file_.reset(fd); |
| 55 sys_path_ = sys_path; | 65 sys_path_ = sys_path; |
| 56 | 66 |
| 67 num_auth_attempts++; |
| 57 if (Authenticate(file_.get())) | 68 if (Authenticate(file_.get())) |
| 58 break; | 69 break; |
| 59 | 70 |
| 60 LOG_IF(WARNING, print_warning) << "Failed to authenticate " | 71 // To avoid spamming the logs, hold off before logging a warning (some |
| 61 << dev_path.value(); | 72 // failures are expected at first) and only log a single message. |
| 62 print_warning = false; | 73 if (!logged_warning && |
| 63 usleep(100000); | 74 (base::TimeTicks::Now() - start_time).InMilliseconds() >= |
| 75 kLogAuthFailDelayMs) { |
| 76 LOG(WARNING) << "Failed to authenticate " << dev_path.value() |
| 77 << " within " << kLogAuthFailDelayMs << " ms"; |
| 78 logged_warning = true; |
| 79 } |
| 80 usleep(kAuthFailSleepMs * 1000); |
| 64 } | 81 } |
| 65 | 82 |
| 66 VLOG(1) << "Succeeded authenticating " << dev_path.value(); | 83 VLOG(1) << "Succeeded authenticating " << dev_path.value() << " in " |
| 84 << (base::TimeTicks::Now() - start_time).InMilliseconds() << " ms " |
| 85 << "with " << num_auth_attempts << " attempt(s)"; |
| 67 return true; | 86 return true; |
| 68 } | 87 } |
| 69 | 88 |
| 70 bool DrmDeviceHandle::IsValid() const { | 89 bool DrmDeviceHandle::IsValid() const { |
| 71 return file_.is_valid(); | 90 return file_.is_valid(); |
| 72 } | 91 } |
| 73 | 92 |
| 74 base::ScopedFD DrmDeviceHandle::PassFD() { | 93 base::ScopedFD DrmDeviceHandle::PassFD() { |
| 75 return std::move(file_); | 94 return std::move(file_); |
| 76 } | 95 } |
| 77 | 96 |
| 78 } // namespace ui | 97 } // namespace ui |
| OLD | NEW |