2020-01-18 11:38:21 +03:00
|
|
|
/*
|
2021-01-10 17:55:54 +03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-01-18 11:38:21 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
#include <AK/ScopeGuard.h>
|
2020-07-31 01:13:15 +03:00
|
|
|
#include <AK/String.h>
|
2021-11-23 13:32:25 +03:00
|
|
|
#include <LibCore/MappedFile.h>
|
2021-11-23 13:36:00 +03:00
|
|
|
#include <LibCore/System.h>
|
2021-03-12 19:29:37 +03:00
|
|
|
#include <errno.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <fcntl.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/stat.h>
|
2019-04-03 14:51:49 +03:00
|
|
|
#include <unistd.h>
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-11-23 13:32:25 +03:00
|
|
|
namespace Core {
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-11-07 02:37:07 +03:00
|
|
|
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map(String const& path)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
2021-01-12 21:21:59 +03:00
|
|
|
int fd = open(path.characters(), O_RDONLY | O_CLOEXEC, 0);
|
2021-01-10 17:55:54 +03:00
|
|
|
if (fd < 0)
|
2021-11-07 02:37:07 +03:00
|
|
|
return Error::from_errno(errno);
|
2019-05-28 12:53:16 +03:00
|
|
|
|
2021-08-06 04:47:55 +03:00
|
|
|
return map_from_fd_and_close(fd, path);
|
|
|
|
}
|
|
|
|
|
2021-11-07 02:37:07 +03:00
|
|
|
ErrorOr<NonnullRefPtr<MappedFile>> MappedFile::map_from_fd_and_close(int fd, [[maybe_unused]] String const& path)
|
2021-08-06 04:47:55 +03:00
|
|
|
{
|
2021-11-23 13:36:00 +03:00
|
|
|
TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
|
2021-08-06 04:47:55 +03:00
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
ScopeGuard fd_close_guard = [fd] {
|
|
|
|
close(fd);
|
|
|
|
};
|
2018-10-10 12:53:07 +03:00
|
|
|
|
2021-11-23 13:36:00 +03:00
|
|
|
auto stat = TRY(Core::System::fstat(fd));
|
|
|
|
auto size = stat.st_size;
|
2019-08-05 15:26:56 +03:00
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
auto* ptr = mmap(nullptr, size, PROT_READ, MAP_SHARED, fd, 0);
|
2019-08-05 15:26:56 +03:00
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
if (ptr == MAP_FAILED)
|
2021-11-07 02:37:07 +03:00
|
|
|
return Error::from_errno(errno);
|
2019-04-03 14:51:49 +03:00
|
|
|
|
2021-07-29 16:00:26 +03:00
|
|
|
#ifdef __serenity__
|
|
|
|
if (set_mmap_name(ptr, size, path.characters()) < 0) {
|
|
|
|
perror("set_mmap_name");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-23 17:46:57 +03:00
|
|
|
return adopt_ref(*new MappedFile(ptr, size));
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
MappedFile::MappedFile(void* ptr, size_t size)
|
|
|
|
: m_data(ptr)
|
|
|
|
, m_size(size)
|
2018-10-10 12:53:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-10 17:55:54 +03:00
|
|
|
MappedFile::~MappedFile()
|
2019-04-03 14:51:49 +03:00
|
|
|
{
|
2021-01-10 17:55:54 +03:00
|
|
|
auto rc = munmap(m_data, m_size);
|
2021-02-23 22:42:32 +03:00
|
|
|
VERIFY(rc == 0);
|
2019-04-03 14:51:49 +03:00
|
|
|
}
|
|
|
|
|
2018-10-10 12:53:07 +03:00
|
|
|
}
|