49 lines · c
1//===---------- Shared implementations for shm_open/shm_unlink ------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "hdr/errno_macros.h"10#include "src/__support/CPP/array.h"11#include "src/__support/CPP/string_view.h"12#include "src/__support/error_or.h"13#include "src/__support/macros/config.h"14#include "src/string/memory_utils/inline_memcpy.h"15 16// TODO: Get PATH_MAX via https://github.com/llvm/llvm-project/issues/8512117#include <linux/limits.h>18 19namespace LIBC_NAMESPACE_DECL {20 21namespace shm_common {22 23LIBC_INLINE_VAR constexpr cpp::string_view SHM_PREFIX = "/dev/shm/";24using SHMPath = cpp::array<char, NAME_MAX + SHM_PREFIX.size() + 1>;25 26LIBC_INLINE ErrorOr<SHMPath> translate_name(cpp::string_view name) {27 // trim leading slashes28 size_t offset = name.find_first_not_of('/');29 if (offset == cpp::string_view::npos)30 return Error(EINVAL);31 name = name.substr(offset);32 33 // check the name34 if (name.size() > NAME_MAX)35 return Error(ENAMETOOLONG);36 if (name == "." || name == ".." || name.contains('/'))37 return Error(EINVAL);38 39 // prepend the prefix40 SHMPath buffer;41 inline_memcpy(buffer.data(), SHM_PREFIX.data(), SHM_PREFIX.size());42 inline_memcpy(buffer.data() + SHM_PREFIX.size(), name.data(), name.size());43 buffer[SHM_PREFIX.size() + name.size()] = '\0';44 return buffer;45}46} // namespace shm_common47 48} // namespace LIBC_NAMESPACE_DECL49