-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc] Add shm_open/shm_unlink
#84974
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2dd6975
628c04b
62d9543
31ef3dd
91b8430
3e3650f
329ea45
1929661
8a8e964
7504b21
ec47dc4
e4f434b
edde037
8a255c3
1eaa414
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
//===---------- Shared implementations for shm_open/shm_unlink ------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/__support/CPP/array.h" | ||
#include "src/__support/CPP/optional.h" | ||
#include "src/__support/CPP/string_view.h" | ||
#include "src/errno/libc_errno.h" | ||
#include "src/string/memory_utils/inline_memcpy.h" | ||
|
||
// TODO: Get PATH_MAX via https://github.com/llvm/llvm-project/issues/85121 | ||
#include <linux/limits.h> | ||
SchrodingerZhu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
namespace shm_common { | ||
|
||
LIBC_INLINE_VAR constexpr cpp::string_view SHM_PREFIX = "/dev/shm/"; | ||
using SHMPath = cpp::array<char, NAME_MAX + SHM_PREFIX.size() + 1>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider moving this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is a little bit annoying that I actually use the type in the function signature. I surround the shared code in a |
||
|
||
LIBC_INLINE cpp::optional<SHMPath> translate_name(cpp::string_view name) { | ||
// trim leading slashes | ||
size_t offset = name.find_first_not_of('/'); | ||
if (offset == cpp::string_view::npos) { | ||
libc_errno = EINVAL; | ||
return cpp::nullopt; | ||
} | ||
name = name.substr(offset); | ||
|
||
// check the name | ||
if (name.size() > NAME_MAX) { | ||
libc_errno = ENAMETOOLONG; | ||
return cpp::nullopt; | ||
} | ||
if (name == "." || name == ".." || name.contains('/')) { | ||
libc_errno = EINVAL; | ||
return cpp::nullopt; | ||
} | ||
|
||
// prepend the prefix | ||
SHMPath buffer; | ||
inline_memcpy(buffer.data(), SHM_PREFIX.data(), SHM_PREFIX.size()); | ||
inline_memcpy(buffer.data() + SHM_PREFIX.size(), name.data(), name.size()); | ||
buffer[SHM_PREFIX.size() + name.size()] = '\0'; | ||
return buffer; | ||
} | ||
} // namespace shm_common | ||
|
||
} // namespace LIBC_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
//===---------- Linux implementation of the shm_open function -------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/sys/mman/shm_open.h" | ||
#include "llvm-libc-macros/fcntl-macros.h" | ||
#include "src/fcntl/open.h" | ||
#include "src/sys/mman/linux/shm_common.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
static constexpr int DEFAULT_OFLAGS = O_NOFOLLOW | O_CLOEXEC | O_NONBLOCK; | ||
|
||
LLVM_LIBC_FUNCTION(int, shm_open, (const char *name, int oflags, mode_t mode)) { | ||
using namespace shm_common; | ||
if (cpp::optional<SHMPath> buffer = translate_name(name)) | ||
return open(buffer->data(), oflags | DEFAULT_OFLAGS, mode); | ||
return -1; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
//===---------- Linux implementation of the shm_unlink function -----------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "src/sys/mman/shm_unlink.h" | ||
#include "src/sys/mman/linux/shm_common.h" | ||
#include "src/unistd/unlink.h" | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
LLVM_LIBC_FUNCTION(int, shm_unlink, (const char *name)) { | ||
using namespace shm_common; | ||
if (cpp::optional<SHMPath> buffer = translate_name(name)) | ||
return unlink(buffer->data()); | ||
return -1; | ||
} | ||
|
||
} // namespace LIBC_NAMESPACE |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
//===-- Implementation header for shm_open function -------------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_SYS_MMAN_SHM_OPEN_H | ||
#define LLVM_LIBC_SRC_SYS_MMAN_SHM_OPEN_H | ||
|
||
#include <llvm-libc-types/mode_t.h> | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int shm_open(const char *name, int oflag, mode_t mode); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_MMAN_SHM_OPEN_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
//===-- Implementation header for shm_unlink function ------------*- C++-*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_LIBC_SRC_SYS_MMAN_SHM_UNLINK_H | ||
#define LLVM_LIBC_SRC_SYS_MMAN_SHM_UNLINK_H | ||
|
||
namespace LIBC_NAMESPACE { | ||
|
||
int shm_unlink(const char *name); | ||
|
||
} // namespace LIBC_NAMESPACE | ||
|
||
#endif // LLVM_LIBC_SRC_SYS_MMAN_SHM_UNLINK_H |
Uh oh!
There was an error while loading. Please reload this page.