aboutsummaryrefslogtreecommitdiff
path: root/libc/src/sys/mman/linux/shm_common.h
blob: 9ba8fd1ea100c14f2a4c4ab26e7f2a5cec5203d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//===---------- 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 "hdr/errno_macros.h"
#include "src/__support/CPP/array.h"
#include "src/__support/CPP/string_view.h"
#include "src/__support/error_or.h"
#include "src/__support/macros/config.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>

namespace LIBC_NAMESPACE_DECL {

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>;

LIBC_INLINE ErrorOr<SHMPath> translate_name(cpp::string_view name) {
  // trim leading slashes
  size_t offset = name.find_first_not_of('/');
  if (offset == cpp::string_view::npos)
    return Error(EINVAL);
  name = name.substr(offset);

  // check the name
  if (name.size() > NAME_MAX)
    return Error(ENAMETOOLONG);
  if (name == "." || name == ".." || name.contains('/'))
    return Error(EINVAL);

  // 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_DECL