xref: /aosp_15_r20/external/llvm-libc/src/sys/prctl/linux/prctl.cpp (revision 71db0c75aadcf003ffe3238005f61d7618a3fead)
1 //===---------- Linux implementation of the prctl 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-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "src/sys/prctl/prctl.h"
10 
11 #include "src/__support/OSUtil/syscall.h" // For internal syscall function.
12 
13 #include "src/__support/macros/config.h"
14 #include "src/errno/libc_errno.h"
15 #include <sys/syscall.h> // For syscall numbers.
16 
17 namespace LIBC_NAMESPACE_DECL {
18 
19 LLVM_LIBC_FUNCTION(int, prctl,
20                    (int option, unsigned long arg2, unsigned long arg3,
21                     unsigned long arg4, unsigned long arg5)) {
22   long ret =
23       LIBC_NAMESPACE::syscall_impl(SYS_prctl, option, arg2, arg3, arg4, arg5);
24   // The manpage states that "... return the nonnegative values described
25   // above. All other option values return 0 on success. On error,
26   // -1 is returned, and errno is set to indicate the error."
27   // According to the kernel implementation
28   // (https://github.com/torvalds/linux/blob/bee0e7762ad2c6025b9f5245c040fcc36ef2bde8/kernel/sys.c#L2442),
29   // return value from the syscall is set to 0 on default so we do not need to
30   // set the value on success manually.
31   if (ret < 0) {
32     libc_errno = static_cast<int>(-ret);
33     return -1;
34   }
35   return static_cast<int>(ret);
36 }
37 
38 } // namespace LIBC_NAMESPACE_DECL
39