186bde5adSSchrodinger ZHU Yifan //===---------- Linux implementation of the prctl function ----------------===// 286bde5adSSchrodinger ZHU Yifan // 386bde5adSSchrodinger ZHU Yifan // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 486bde5adSSchrodinger ZHU Yifan // See https://llvm.org/LICENSE.txt for license information. 586bde5adSSchrodinger ZHU Yifan // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 686bde5adSSchrodinger ZHU Yifan // 786bde5adSSchrodinger ZHU Yifan //===----------------------------------------------------------------------===// 886bde5adSSchrodinger ZHU Yifan 986bde5adSSchrodinger ZHU Yifan #include "src/sys/prctl/prctl.h" 1086bde5adSSchrodinger ZHU Yifan 1186bde5adSSchrodinger ZHU Yifan #include "src/__support/OSUtil/syscall.h" // For internal syscall function. 1286bde5adSSchrodinger ZHU Yifan 13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h" 1486bde5adSSchrodinger ZHU Yifan #include "src/errno/libc_errno.h" 1586bde5adSSchrodinger ZHU Yifan #include <sys/syscall.h> // For syscall numbers. 1686bde5adSSchrodinger ZHU Yifan 17*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL { 1886bde5adSSchrodinger ZHU Yifan 1986bde5adSSchrodinger ZHU Yifan LLVM_LIBC_FUNCTION(int, prctl, 2086bde5adSSchrodinger ZHU Yifan (int option, unsigned long arg2, unsigned long arg3, 2186bde5adSSchrodinger ZHU Yifan unsigned long arg4, unsigned long arg5)) { 2286bde5adSSchrodinger ZHU Yifan long ret = 2386bde5adSSchrodinger ZHU Yifan LIBC_NAMESPACE::syscall_impl(SYS_prctl, option, arg2, arg3, arg4, arg5); 2486bde5adSSchrodinger ZHU Yifan // The manpage states that "... return the nonnegative values described 2586bde5adSSchrodinger ZHU Yifan // above. All other option values return 0 on success. On error, 2686bde5adSSchrodinger ZHU Yifan // -1 is returned, and errno is set to indicate the error." 2786bde5adSSchrodinger ZHU Yifan // According to the kernel implementation 2886bde5adSSchrodinger ZHU Yifan // (https://github.com/torvalds/linux/blob/bee0e7762ad2c6025b9f5245c040fcc36ef2bde8/kernel/sys.c#L2442), 2986bde5adSSchrodinger ZHU Yifan // return value from the syscall is set to 0 on default so we do not need to 3086bde5adSSchrodinger ZHU Yifan // set the value on success manually. 3186bde5adSSchrodinger ZHU Yifan if (ret < 0) { 3286bde5adSSchrodinger ZHU Yifan libc_errno = static_cast<int>(-ret); 3386bde5adSSchrodinger ZHU Yifan return -1; 3486bde5adSSchrodinger ZHU Yifan } 3586bde5adSSchrodinger ZHU Yifan return static_cast<int>(ret); 3686bde5adSSchrodinger ZHU Yifan } 3786bde5adSSchrodinger ZHU Yifan 38*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL 39