45 lines · cpp
1//===---------- Linux implementation of the shm_unlink function -----------===//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 "src/sys/mman/shm_unlink.h"10 11#include "hdr/fcntl_macros.h"12#include "src/__support/OSUtil/syscall.h" // For internal syscall function.13#include "src/__support/libc_errno.h" // For internal errno.14#include "src/__support/macros/config.h"15#include "src/sys/mman/linux/shm_common.h"16#include <sys/syscall.h> // For SYS_unlink, SYS_unlinkat17 18namespace LIBC_NAMESPACE_DECL {19 20// TODO: move the unlink syscall to a shared utility.21 22LLVM_LIBC_FUNCTION(int, shm_unlink, (const char *name)) {23 auto path_result = shm_common::translate_name(name);24 if (!path_result.has_value()) {25 libc_errno = path_result.error();26 return -1;27 }28#ifdef SYS_unlink29 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_unlink, path_result->data());30#elif defined(SYS_unlinkat)31 int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_unlinkat, AT_FDCWD,32 path_result->data(), 0);33#else34#error "unlink and unlinkat syscalls not available."35#endif36 37 if (ret < 0) {38 libc_errno = -ret;39 return -1;40 }41 return ret;42}43 44} // namespace LIBC_NAMESPACE_DECL45