Mir

Merge lp:~afrantzis/mir/drm-device-fd-close-on-exec into lp:mir

Proposed by Alexandros Frantzis
Status: Merged
Approved by: Daniel van Vugt
Approved revision: no longer in the source branch.
Merged at revision: 1431
Proposed branch: lp:~afrantzis/mir/drm-device-fd-close-on-exec
Merge into: lp:mir
Diff against target: 221 lines (+89/-10)
7 files modified
include/test/mir_test_doubles/mock_drm.h (+1/-0)
src/platform/graphics/mesa/display_helpers.cpp (+8/-1)
tests/mir_test_doubles/mock_drm.cpp (+7/-4)
tests/unit-tests/graphics/mesa/CMakeLists.txt (+1/-0)
tests/unit-tests/graphics/mesa/test_display.cpp (+1/-1)
tests/unit-tests/graphics/mesa/test_drm_helper.cpp (+65/-0)
tests/unit-tests/graphics/mesa/test_platform.cpp (+6/-4)
To merge this branch: bzr merge lp:~afrantzis/mir/drm-device-fd-close-on-exec
Reviewer Review Type Date Requested Status
Daniel van Vugt Approve
PS Jenkins bot (community) continuous-integration Approve
Alan Griffiths Approve
Review via email: mp+208349@code.launchpad.net

Commit message

mesa: Ensure we close drm device file descriptors on exec

Description of the change

mesa: Ensure we close drm device file descriptors on exec

This is needed for https://bugs.launchpad.net/mir/+bug/1284081 , but it's not a complete fix, because Mesa internally dup()s our drm fd without setting the close-on-exec flag.

To post a comment you must log in.
Revision history for this message
Alan Griffiths (alan-griffiths) wrote :

Looks sensible

review: Approve
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Daniel van Vugt (vanvugt) wrote :

This looks like a potential mismatch:

3 parameters mocked:
8 + MOCK_METHOD3(open, int(char const* path, int flags, mode_t mode));

2 parameters used:
35 + tmp_fd = open(device.devnode(), O_RDWR | O_CLOEXEC);

Maybe it mocks correctly still? I guess the expectations would fail otherwise. The real function is:
/usr/include/fcntl.h:extern int open (const char *__file, int __oflag, ...) __nonnull ((1));

Seems to work.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'include/test/mir_test_doubles/mock_drm.h'
--- include/test/mir_test_doubles/mock_drm.h 2013-08-28 03:41:48 +0000
+++ include/test/mir_test_doubles/mock_drm.h 2014-02-26 12:30:46 +0000
@@ -88,6 +88,7 @@
88 MockDRM();88 MockDRM();
89 ~MockDRM() noexcept;89 ~MockDRM() noexcept;
9090
91 MOCK_METHOD3(open, int(char const* path, int flags, mode_t mode));
91 MOCK_METHOD2(drmOpen, int(const char *name, const char *busid));92 MOCK_METHOD2(drmOpen, int(const char *name, const char *busid));
92 MOCK_METHOD1(drmClose, int(int fd));93 MOCK_METHOD1(drmClose, int(int fd));
93 MOCK_METHOD3(drmIoctl, int(int fd, unsigned long request, void *arg));94 MOCK_METHOD3(drmIoctl, int(int fd, unsigned long request, void *arg));
9495
=== modified file 'src/platform/graphics/mesa/display_helpers.cpp'
--- src/platform/graphics/mesa/display_helpers.cpp 2014-01-22 08:32:55 +0000
+++ src/platform/graphics/mesa/display_helpers.cpp 2014-02-26 12:30:46 +0000
@@ -65,6 +65,13 @@
65 BOOST_THROW_EXCEPTION(65 BOOST_THROW_EXCEPTION(
66 std::runtime_error("Failed to open DRM device for authenticated fd"));66 std::runtime_error("Failed to open DRM device for authenticated fd"));
6767
68 if (fcntl(auth_fd, F_SETFD, fcntl(auth_fd, F_GETFD) | FD_CLOEXEC) == -1)
69 {
70 BOOST_THROW_EXCEPTION(
71 boost::enable_error_info(
72 std::runtime_error("Failed to set FD_CLOEXEC for authenticated drm fd")));
73 }
74
68 drm_magic_t magic;75 drm_magic_t magic;
69 int ret = -1;76 int ret = -1;
70 if ((ret = drmGetMagic(auth_fd, &magic)) < 0)77 if ((ret = drmGetMagic(auth_fd, &magic)) < 0)
@@ -198,7 +205,7 @@
198 continue;205 continue;
199206
200 // If directly opening the DRM device is good enough for X it's good enough for us!207 // If directly opening the DRM device is good enough for X it's good enough for us!
201 tmp_fd = open(device.devnode(), O_RDWR, O_CLOEXEC);208 tmp_fd = open(device.devnode(), O_RDWR | O_CLOEXEC);
202 if (tmp_fd < 0)209 if (tmp_fd < 0)
203 {210 {
204 error = errno;211 error = errno;
205212
=== modified file 'tests/mir_test_doubles/mock_drm.cpp'
--- tests/mir_test_doubles/mock_drm.cpp 2014-01-13 06:12:33 +0000
+++ tests/mir_test_doubles/mock_drm.cpp 2014-02-26 12:30:46 +0000
@@ -229,6 +229,9 @@
229229
230 global_mock = this;230 global_mock = this;
231231
232 ON_CALL(*this, open(_,_,_))
233 .WillByDefault(Return(fake_drm.fd()));
234
232 ON_CALL(*this, drmOpen(_,_))235 ON_CALL(*this, drmOpen(_,_))
233 .WillByDefault(Return(fake_drm.fd()));236 .WillByDefault(Return(fake_drm.fd()));
234237
@@ -405,7 +408,7 @@
405{408{
406 char const* drm_prefix = "/dev/dri/";409 char const* drm_prefix = "/dev/dri/";
407 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))410 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))
408 return global_mock->drmOpen("i915", NULL);411 return global_mock->open(path, flags, mode);
409412
410 int (*real_open)(char const *path, int flags, mode_t mode);413 int (*real_open)(char const *path, int flags, mode_t mode);
411 *(void **)(&real_open) = dlsym(RTLD_NEXT, "open");414 *(void **)(&real_open) = dlsym(RTLD_NEXT, "open");
@@ -417,7 +420,7 @@
417{420{
418 char const* drm_prefix = "/dev/dri/";421 char const* drm_prefix = "/dev/dri/";
419 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))422 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))
420 return global_mock->drmOpen("i915", NULL);423 return global_mock->open(path, flags, mode);
421424
422 int (*real_open64)(char const *path, int flags, mode_t mode);425 int (*real_open64)(char const *path, int flags, mode_t mode);
423 *(void **)(&real_open64) = dlsym(RTLD_NEXT, "open64");426 *(void **)(&real_open64) = dlsym(RTLD_NEXT, "open64");
@@ -429,7 +432,7 @@
429{432{
430 char const* drm_prefix = "/dev/dri/";433 char const* drm_prefix = "/dev/dri/";
431 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))434 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))
432 return global_mock->drmOpen("i915", NULL);435 return global_mock->open(path, flags, mode);
433436
434 int (*real_open)(char const *path, int flags, mode_t mode);437 int (*real_open)(char const *path, int flags, mode_t mode);
435 *(void **)(&real_open) = dlsym(RTLD_NEXT, "__open");438 *(void **)(&real_open) = dlsym(RTLD_NEXT, "__open");
@@ -441,7 +444,7 @@
441{444{
442 char const* drm_prefix = "/dev/dri/";445 char const* drm_prefix = "/dev/dri/";
443 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))446 if (!strncmp(path, drm_prefix, strlen(drm_prefix)))
444 return global_mock->drmOpen("i915", NULL);447 return global_mock->open(path, flags, mode);
445448
446 int (*real_open64)(char const *path, int flags, mode_t mode);449 int (*real_open64)(char const *path, int flags, mode_t mode);
447 *(void **)(&real_open64) = dlsym(RTLD_NEXT, "__open64");450 *(void **)(&real_open64) = dlsym(RTLD_NEXT, "__open64");
448451
=== modified file 'tests/unit-tests/graphics/mesa/CMakeLists.txt'
--- tests/unit-tests/graphics/mesa/CMakeLists.txt 2014-01-22 08:32:55 +0000
+++ tests/unit-tests/graphics/mesa/CMakeLists.txt 2014-02-26 12:30:46 +0000
@@ -17,6 +17,7 @@
17 ${CMAKE_CURRENT_SOURCE_DIR}/test_native_platform.cpp17 ${CMAKE_CURRENT_SOURCE_DIR}/test_native_platform.cpp
18 ${CMAKE_CURRENT_SOURCE_DIR}/test_anonymous_shm_file.cpp18 ${CMAKE_CURRENT_SOURCE_DIR}/test_anonymous_shm_file.cpp
19 ${CMAKE_CURRENT_SOURCE_DIR}/test_shm_buffer.cpp19 ${CMAKE_CURRENT_SOURCE_DIR}/test_shm_buffer.cpp
20 ${CMAKE_CURRENT_SOURCE_DIR}/test_drm_helper.cpp
20)21)
2122
22set(UNIT_TEST_SOURCES ${UNIT_TEST_SOURCES} PARENT_SCOPE)23set(UNIT_TEST_SOURCES ${UNIT_TEST_SOURCES} PARENT_SCOPE)
2324
=== modified file 'tests/unit-tests/graphics/mesa/test_display.cpp'
--- tests/unit-tests/graphics/mesa/test_display.cpp 2014-02-21 15:32:30 +0000
+++ tests/unit-tests/graphics/mesa/test_display.cpp 2014-02-26 12:30:46 +0000
@@ -332,7 +332,7 @@
332{332{
333 using namespace testing;333 using namespace testing;
334334
335 EXPECT_CALL(mock_drm, drmOpen(_,_))335 EXPECT_CALL(mock_drm, open(_,_,_))
336 .Times(AtLeast(1))336 .Times(AtLeast(1))
337 .WillRepeatedly(Return(-1));337 .WillRepeatedly(Return(-1));
338338
339339
=== added file 'tests/unit-tests/graphics/mesa/test_drm_helper.cpp'
--- tests/unit-tests/graphics/mesa/test_drm_helper.cpp 1970-01-01 00:00:00 +0000
+++ tests/unit-tests/graphics/mesa/test_drm_helper.cpp 2014-02-26 12:30:46 +0000
@@ -0,0 +1,65 @@
1/*
2 * Copyright © 2014 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 3 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 * Authored by: Alexandros Frantzis <alexandros.frantzis@canonical.com>
17 */
18
19#include "src/platform/graphics/mesa/display_helpers.h"
20#include "mir/udev/wrapper.h"
21
22#include "mir_test_framework/udev_environment.h"
23#include "mir_test_doubles/mock_drm.h"
24
25#include <fcntl.h>
26
27#include <gtest/gtest.h>
28#include <gmock/gmock.h>
29
30namespace mgm = mir::graphics::mesa;
31namespace mtf = mir::mir_test_framework;
32namespace mtd = mir::test::doubles;
33
34namespace
35{
36
37MATCHER_P(FlagSet, flag, "")
38{
39 return (arg & flag);
40}
41
42class DRMHelperTest : public ::testing::Test
43{
44public:
45 DRMHelperTest()
46 {
47 fake_devices.add_standard_device("standard-drm-devices");
48 }
49
50protected:
51 ::testing::NiceMock<mtd::MockDRM> mock_drm;
52 mtf::UdevEnvironment fake_devices;
53};
54
55}
56
57TEST_F(DRMHelperTest, closes_drm_fd_on_exec)
58{
59 using namespace testing;
60
61 EXPECT_CALL(mock_drm, open(_, FlagSet(O_CLOEXEC), _));
62
63 mgm::helpers::DRMHelper drm_helper;
64 drm_helper.setup(std::make_shared<mir::udev::Context>());
65}
066
=== modified file 'tests/unit-tests/graphics/mesa/test_platform.cpp'
--- tests/unit-tests/graphics/mesa/test_platform.cpp 2014-02-19 14:01:37 +0000
+++ tests/unit-tests/graphics/mesa/test_platform.cpp 2014-02-26 12:30:46 +0000
@@ -29,6 +29,7 @@
29#include <gtest/gtest.h>29#include <gtest/gtest.h>
3030
31#include "mir_test_framework/udev_environment.h"31#include "mir_test_framework/udev_environment.h"
32#include "mir_test/pipe.h"
3233
33#include "mir_test_doubles/mock_drm.h"34#include "mir_test_doubles/mock_drm.h"
34#include "mir_test_doubles/mock_gbm.h"35#include "mir_test_doubles/mock_gbm.h"
@@ -76,12 +77,13 @@
76TEST_F(MesaGraphicsPlatform, get_ipc_package)77TEST_F(MesaGraphicsPlatform, get_ipc_package)
77{78{
78 using namespace testing;79 using namespace testing;
79 const int auth_fd{66};80 mir::test::Pipe auth_pipe;
81 int const auth_fd{auth_pipe.read_fd()};
8082
81 /* First time for master DRM fd, second for authenticated fd */83 /* First time for master DRM fd, second for authenticated fd */
84 EXPECT_CALL(mock_drm, open(_,_,_))
85 .WillOnce(Return(mock_drm.fake_drm.fd()));
82 EXPECT_CALL(mock_drm, drmOpen(_,_))86 EXPECT_CALL(mock_drm, drmOpen(_,_))
83 .Times(2)
84 .WillOnce(Return(mock_drm.fake_drm.fd()))
85 .WillOnce(Return(auth_fd));87 .WillOnce(Return(auth_fd));
8688
87 /* Expect proper authorization */89 /* Expect proper authorization */
@@ -107,7 +109,7 @@
107{109{
108 using namespace ::testing;110 using namespace ::testing;
109111
110 EXPECT_CALL(mock_drm, drmOpen(_,_))112 EXPECT_CALL(mock_drm, open(_,_,_))
111 .WillRepeatedly(Return(-1));113 .WillRepeatedly(Return(-1));
112114
113 try115 try

Subscribers

People subscribed via source and target branches