105532289SSiva Chandra Reddy //===-- Linux implementation of chmod -------------------------------------===// 205532289SSiva Chandra Reddy // 305532289SSiva Chandra Reddy // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 405532289SSiva Chandra Reddy // See https://llvm.org/LICENSE.txt for license information. 505532289SSiva Chandra Reddy // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 605532289SSiva Chandra Reddy // 705532289SSiva Chandra Reddy //===----------------------------------------------------------------------===// 805532289SSiva Chandra Reddy 905532289SSiva Chandra Reddy #include "src/sys/stat/chmod.h" 1005532289SSiva Chandra Reddy 1105532289SSiva Chandra Reddy #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 1205532289SSiva Chandra Reddy #include "src/__support/common.h" 1305532289SSiva Chandra Reddy 14*7b663bd9SJob Henandez Lara #include "hdr/fcntl_macros.h" 15abc49cc1SJob Henandez Lara #include "hdr/types/mode_t.h" 165ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 17204587a3SSiva Chandra Reddy #include "src/errno/libc_errno.h" 1805532289SSiva Chandra Reddy #include <sys/stat.h> 1905532289SSiva Chandra Reddy #include <sys/syscall.h> // For syscall numbers. 2005532289SSiva Chandra Reddy 215ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 2205532289SSiva Chandra Reddy 2305532289SSiva Chandra Reddy LLVM_LIBC_FUNCTION(int, chmod, (const char *path, mode_t mode)) { 2405532289SSiva Chandra Reddy #ifdef SYS_chmod 25b6bc9d72SGuillaume Chatelet int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_chmod, path, mode); 2641821208SDavid CARLIER #elif defined(SYS_fchmodat2) 2741821208SDavid CARLIER int ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_fchmodat2, AT_FDCWD, path, 2841821208SDavid CARLIER mode, 0, AT_SYMLINK_NOFOLLOW); 29d00e97dfSSiva Chandra #elif defined(SYS_fchmodat) 30b6bc9d72SGuillaume Chatelet int ret = 3141821208SDavid CARLIER LIBC_NAMESPACE::syscall_impl<int>(SYS_fchmodat, AT_FDCWD, path, mode, 0); 3205532289SSiva Chandra Reddy #else 3341821208SDavid CARLIER #error "chmod, fchmodat and fchmodat2 syscalls not available." 3405532289SSiva Chandra Reddy #endif 3505532289SSiva Chandra Reddy 3605532289SSiva Chandra Reddy if (ret < 0) { 37204587a3SSiva Chandra Reddy libc_errno = -ret; 3805532289SSiva Chandra Reddy return -1; 3905532289SSiva Chandra Reddy } 4005532289SSiva Chandra Reddy return 0; 4105532289SSiva Chandra Reddy } 4205532289SSiva Chandra Reddy 435ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 44