[libc-commits] [libc] Implement getpriority, setpriority, their unit tests; add the accompanying macros and type headers. (PR #219573)

Vadim Kotov via libc-commits libc-commits at lists.llvm.org
Fri Aug 28 13:59:29 PDT 2026


https://github.com/vadimkotov created https://github.com/llvm/llvm-project/pull/219573

Testing these is a bit tricky, what I've currently settled on is this.

- For `getpriority`
  - We make sure the call succeeds and then round-trip the highest nice value on Linux (19).
- For `setpriority`
  - We make sure the call succeeds when setting it to the current nice.
- For both
  - Test two failure modes that are easy to stably induce.

Since these calls are effectively glorified syscall wrappers, even though the above tests don't cover every case in which these functions are used, I think this may be sufficient to show that we correctly propagate values to the kernel and get the correct nice back.

>From d0daed415bf01d493cc0289cea25e64973213464 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Thu, 27 Aug 2026 17:00:05 -0700
Subject: [PATCH 1/6] Basic implementation of getprioriry and accompanying
 macros.

---
 libc/hdr/types/CMakeLists.txt                 |  8 +++++
 .../linux/sys-resource-macros.h               |  4 +++
 libc/include/sys/resource.yaml                |  6 ++++
 .../linux/syscall_wrappers/CMakeLists.txt     | 13 +++++++
 .../linux/syscall_wrappers/getpriority.h      | 34 +++++++++++++++++++
 libc/src/sys/resource/CMakeLists.txt          |  7 ++++
 libc/src/sys/resource/getpriority.h           | 26 ++++++++++++++
 libc/src/sys/resource/linux/getpriority.cpp   | 33 ++++++++++++++++++
 8 files changed, 131 insertions(+)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
 create mode 100644 libc/src/sys/resource/getpriority.h
 create mode 100644 libc/src/sys/resource/linux/getpriority.cpp

diff --git a/libc/hdr/types/CMakeLists.txt b/libc/hdr/types/CMakeLists.txt
index 14f5004922606..0d12917200226 100644
--- a/libc/hdr/types/CMakeLists.txt
+++ b/libc/hdr/types/CMakeLists.txt
@@ -1349,3 +1349,11 @@ add_proxy_header_library(
   FULL_BUILD_DEPENDS
     libc.include.llvm-libc-types.tcflag_t
 )
+
+add_proxy_header_library(
+  id_t
+  HDRS
+    id_t.h
+  FULL_BUILD_DEPENDS
+    libc.include.llvm-libc-types.id_t
+)
diff --git a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
index 9f1e04a339c73..4c3ebf3991086 100644
--- a/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
+++ b/libc/include/llvm-libc-macros/linux/sys-resource-macros.h
@@ -34,4 +34,8 @@
 #define RUSAGE_CHILDREN (-1)
 #define RUSAGE_THREAD 1
 
+#define PRIO_PROCESS 0
+#define PRIO_PGRP 1
+#define PRIO_USER 2
+
 #endif // LLVM_LIBC_MACROS_LINUX_SYS_RESOURCE_MACROS_H
diff --git a/libc/include/sys/resource.yaml b/libc/include/sys/resource.yaml
index a26dfb5f77beb..c91835b2565ed 100644
--- a/libc/include/sys/resource.yaml
+++ b/libc/include/sys/resource.yaml
@@ -12,6 +12,12 @@ macros:
     macro_header: sys-resource-macros.h
     standards:
       - gnu
+  - macro_name: PRIO_PROCESS
+    macro_header: sys-resource-macros.h
+  - macro_name: PRIO_PGRP
+    macro_header: sys-resource-macros.h
+  - macro_name: PRIO_USER
+    macro_header: sys-resource-macros.h
 types:
   - type_name: struct_rlimit
   - type_name: rlim_t
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index 7d09f46696308..e1d0ddd1497f0 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1066,3 +1066,16 @@ add_header_library(
     libc.src.__support.macros.config
     libc.include.sys_syscall
 )
+
+add_header_library(
+  getpriority
+  HDRS
+    getpriority.h
+  DEPENDS
+    libc.hdr.types.id_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
new file mode 100644
index 0000000000000..e42e5794bc30a
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for getpriority
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETPRIORITY_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETPRIORITY_H
+
+#include "hdr/types/pid_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> getpriority(int which, id_t who) {
+  return syscall_checked<int>(SYS_getprioriry, which, who);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif  // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETPRIORITY_H
diff --git a/libc/src/sys/resource/CMakeLists.txt b/libc/src/sys/resource/CMakeLists.txt
index ac5f676588994..f05da3e768a5c 100644
--- a/libc/src/sys/resource/CMakeLists.txt
+++ b/libc/src/sys/resource/CMakeLists.txt
@@ -2,6 +2,13 @@ if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
   add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
 endif()
 
+add_entrypoint_object(
+  getpriority
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.getpriority
+)
+
 add_entrypoint_object(
   getrlimit
   ALIAS
diff --git a/libc/src/sys/resource/getpriority.h b/libc/src/sys/resource/getpriority.h
new file mode 100644
index 0000000000000..103ee53ceaadc
--- /dev/null
+++ b/libc/src/sys/resource/getpriority.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of getpriority
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_RESOURCE_GETPRIORITY_H
+#define LLVM_LIBC_SRC_SYS_RESOURCE_GETPRIORITY_H
+
+#include "hdr/types/id_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int getpriority(int which, id_t who);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif  // LLVM_LIBC_SRC_SYS_RESOURCE_GETPRIORITY_H
diff --git a/libc/src/sys/resource/linux/getpriority.cpp b/libc/src/sys/resource/linux/getpriority.cpp
new file mode 100644
index 0000000000000..827ea456207f6
--- /dev/null
+++ b/libc/src/sys/resource/linux/getpriority.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of getpriority.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/resource/getpriority.h"
+
+#include "hdr/types/id_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/getpriority.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, getpriority, (int which, id_t who)) {
+  auto result = linux_syscalls::getpriority(which, who);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL

>From 9790c7f2ce1e917a9bd6beb309425bb44ae2cde3 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Thu, 27 Aug 2026 19:58:47 -0700
Subject: [PATCH 2/6] Fix getpriority implementation, its syscall wrapper; add
 missing type header, entry points and dependencies; implement basic test.

---
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/hdr/types/id_t.h                         | 27 +++++++++++++++++
 libc/include/sys/resource.yaml                |  7 +++++
 .../linux/syscall_wrappers/getpriority.h      |  2 +-
 libc/src/sys/resource/linux/CMakeLists.txt    | 14 +++++++++
 libc/src/sys/resource/linux/getpriority.cpp   |  3 +-
 libc/test/src/sys/resource/CMakeLists.txt     | 13 ++++++++
 .../src/sys/resource/getpriority_test.cpp     | 30 +++++++++++++++++++
 8 files changed, 95 insertions(+), 2 deletions(-)
 create mode 100644 libc/hdr/types/id_t.h
 create mode 100644 libc/test/src/sys/resource/getpriority_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 6a37d8b2b48d6..09d580194b4b4 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -330,6 +330,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.random.getrandom
 
     # sys/resource.h entrypoints
+    libc.src.sys.resource.getpriority
     libc.src.sys.resource.getrlimit
     libc.src.sys.resource.getrusage
     libc.src.sys.resource.setrlimit
diff --git a/libc/hdr/types/id_t.h b/libc/hdr/types/id_t.h
new file mode 100644
index 0000000000000..b478af776b7cb
--- /dev/null
+++ b/libc/hdr/types/id_t.h
@@ -0,0 +1,27 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Proxy for id_t.
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_HDR_TYPES_ID_T_H
+#define LLVM_LIBC_HDR_TYPES_ID_T_H
+
+#ifdef LIBC_FULL_BUILD
+
+#include "include/llvm-libc-types/id_t.h"
+
+#else // Overlay mode
+
+#include <sys/types.h>
+
+#endif // LIBC_FULL_BUILD
+
+#endif // LLVM_LIBC_HDR_TYPES_ID_T_H
diff --git a/libc/include/sys/resource.yaml b/libc/include/sys/resource.yaml
index c91835b2565ed..0e65fa4e1d81b 100644
--- a/libc/include/sys/resource.yaml
+++ b/libc/include/sys/resource.yaml
@@ -25,6 +25,13 @@ types:
 enums: []
 objects: []
 functions:
+  - name: getpriority
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
+      - type: id_t
   - name: getrlimit
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
index e42e5794bc30a..ddd9c14ef7209 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
@@ -25,7 +25,7 @@ namespace LIBC_NAMESPACE_DECL {
 namespace linux_syscalls {
 
 LIBC_INLINE ErrorOr<int> getpriority(int which, id_t who) {
-  return syscall_checked<int>(SYS_getprioriry, which, who);
+  return syscall_checked<int>(SYS_getpriority, which, who);
 }
 
 } // namespace linux_syscalls
diff --git a/libc/src/sys/resource/linux/CMakeLists.txt b/libc/src/sys/resource/linux/CMakeLists.txt
index 2a7d96c2eb365..55af459b4f74f 100644
--- a/libc/src/sys/resource/linux/CMakeLists.txt
+++ b/libc/src/sys/resource/linux/CMakeLists.txt
@@ -1,3 +1,17 @@
+add_entrypoint_object(
+  getpriority
+  SRCS
+    getpriority.cpp
+  HDRS
+    ../getpriority.h
+  DEPENDS
+    libc.hdr.types.id_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.getpriority
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   getrlimit
   SRCS
diff --git a/libc/src/sys/resource/linux/getpriority.cpp b/libc/src/sys/resource/linux/getpriority.cpp
index 827ea456207f6..a8c1225120c48 100644
--- a/libc/src/sys/resource/linux/getpriority.cpp
+++ b/libc/src/sys/resource/linux/getpriority.cpp
@@ -27,7 +27,8 @@ LLVM_LIBC_FUNCTION(int, getpriority, (int which, id_t who)) {
     libc_errno = result.error();
     return -1;
   }
-  return 0;
+  // The syscall returns (20 - nice), but we must return nice itself.
+  return 20 - result.value();
 }
 
 } // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/test/src/sys/resource/CMakeLists.txt b/libc/test/src/sys/resource/CMakeLists.txt
index 5ff3ec8a62ea9..c79bf9974b306 100644
--- a/libc/test/src/sys/resource/CMakeLists.txt
+++ b/libc/test/src/sys/resource/CMakeLists.txt
@@ -1,5 +1,18 @@
 add_custom_target(libc_sys_resource_unittests)
 
+add_libc_test(
+  getpriority_test
+  SUITE
+    libc_sys_resource_unittests
+  SRCS
+    getpriority_test.cpp
+  DEPENDS
+    libc.hdr.sys_resource_macros
+    libc.hdr.types.id_t
+    libc.src.sys.resource.getpriority
+    libc.test.UnitTest.ErrnoCheckingTest
+)
+
 add_libc_test(
   getrlimit_setrlimit_test
   SUITE
diff --git a/libc/test/src/sys/resource/getpriority_test.cpp b/libc/test/src/sys/resource/getpriority_test.cpp
new file mode 100644
index 0000000000000..7a0b24eb48a30
--- /dev/null
+++ b/libc/test/src/sys/resource/getpriority_test.cpp
@@ -0,0 +1,30 @@
+
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for getpriority.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/id_t.h"
+#include "src/sys/resource/getpriority.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/Test.h"
+
+using LlvmLibcGetrusageTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcGetrusageTest, BasicTest) {
+  int nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
+  ASSERT_ERRNO_SUCCESS();
+  ASSERT_GE(nice, -20);
+  ASSERT_LE(nice, 19);
+
+}
+

>From f5fe41de8b990f14b7075d27fd324850f77fd5a2 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Thu, 27 Aug 2026 20:49:05 -0700
Subject: [PATCH 3/6] Implement setpriority, its syscall wrapper and basic unit
 test; fix minor errors.

---
 libc/config/linux/x86_64/entrypoints.txt      |  1 +
 libc/include/sys/resource.yaml                |  8 +++++
 .../linux/syscall_wrappers/CMakeLists.txt     | 13 +++++++
 .../linux/syscall_wrappers/setpriority.h      | 34 +++++++++++++++++++
 libc/src/sys/resource/CMakeLists.txt          |  7 ++++
 libc/src/sys/resource/linux/CMakeLists.txt    | 14 ++++++++
 libc/src/sys/resource/linux/setpriority.cpp   | 33 ++++++++++++++++++
 libc/src/sys/resource/setpriority.h           | 26 ++++++++++++++
 libc/test/src/sys/resource/CMakeLists.txt     | 16 +++++++++
 .../src/sys/resource/getpriority_test.cpp     |  6 ++--
 .../src/sys/resource/setpriority_test.cpp     | 31 +++++++++++++++++
 11 files changed, 185 insertions(+), 4 deletions(-)
 create mode 100644 libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
 create mode 100644 libc/src/sys/resource/linux/setpriority.cpp
 create mode 100644 libc/src/sys/resource/setpriority.h
 create mode 100644 libc/test/src/sys/resource/setpriority_test.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 09d580194b4b4..2e1dc645696ee 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -333,6 +333,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.resource.getpriority
     libc.src.sys.resource.getrlimit
     libc.src.sys.resource.getrusage
+    libc.src.sys.resource.setpriority
     libc.src.sys.resource.setrlimit
 
     # sys/sem.h entrypoints
diff --git a/libc/include/sys/resource.yaml b/libc/include/sys/resource.yaml
index 0e65fa4e1d81b..c4733dead7575 100644
--- a/libc/include/sys/resource.yaml
+++ b/libc/include/sys/resource.yaml
@@ -46,6 +46,14 @@ functions:
     arguments:
       - type: int
       - type: struct rusage *
+  - name: setpriority
+    standards:
+      - posix
+    return_type: int
+    arguments:
+      - type: int
+      - type: id_t
+      - type: int
   - name: setrlimit
     standards:
       - posix
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
index e1d0ddd1497f0..24371d2220921 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/CMakeLists.txt
@@ -1079,3 +1079,16 @@ add_header_library(
     libc.src.__support.macros.config
     libc.include.sys_syscall
 )
+
+add_header_library(
+  setpriority
+  HDRS
+    setpriority.h
+  DEPENDS
+    libc.hdr.types.id_t
+    libc.src.__support.OSUtil.osutil
+    libc.src.__support.common
+    libc.src.__support.error_or
+    libc.src.__support.macros.config
+    libc.include.sys_syscall
+)
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
new file mode 100644
index 0000000000000..e9d169c97c424
--- /dev/null
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
@@ -0,0 +1,34 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Syscall wrapper for setpriority
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SETPRIORITY_H
+#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SETPRIORITY_H
+
+#include "hdr/types/id_t.h"
+#include "src/__support/OSUtil/linux/syscall.h" // For syscall_checked
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/macros/config.h"
+#include <sys/syscall.h> // For syscall numbers
+
+namespace LIBC_NAMESPACE_DECL {
+namespace linux_syscalls {
+
+LIBC_INLINE ErrorOr<int> setpriority(int which, id_t who, int niceval) {
+  return syscall_checked<int>(SYS_setpriority, which, who, niceval);
+}
+
+} // namespace linux_syscalls
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif  // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SETPRIORITY_H
diff --git a/libc/src/sys/resource/CMakeLists.txt b/libc/src/sys/resource/CMakeLists.txt
index f05da3e768a5c..7105eb98e67ac 100644
--- a/libc/src/sys/resource/CMakeLists.txt
+++ b/libc/src/sys/resource/CMakeLists.txt
@@ -23,6 +23,13 @@ add_entrypoint_object(
     .${LIBC_TARGET_OS}.getrusage
 )
 
+add_entrypoint_object(
+  setpriority
+  ALIAS
+  DEPENDS
+    .${LIBC_TARGET_OS}.setpriority
+)
+
 add_entrypoint_object(
   setrlimit
   ALIAS
diff --git a/libc/src/sys/resource/linux/CMakeLists.txt b/libc/src/sys/resource/linux/CMakeLists.txt
index 55af459b4f74f..293acb3337355 100644
--- a/libc/src/sys/resource/linux/CMakeLists.txt
+++ b/libc/src/sys/resource/linux/CMakeLists.txt
@@ -38,6 +38,20 @@ add_entrypoint_object(
     libc.src.errno.errno
 )
 
+add_entrypoint_object(
+  setpriority
+  SRCS
+    setpriority.cpp
+  HDRS
+    ../setpriority.h
+  DEPENDS
+    libc.hdr.types.id_t
+    libc.src.__support.OSUtil.linux.syscall_wrappers.setpriority
+    libc.src.__support.common
+    libc.src.__support.macros.config
+    libc.src.errno.errno
+)
+
 add_entrypoint_object(
   setrlimit
   SRCS
diff --git a/libc/src/sys/resource/linux/setpriority.cpp b/libc/src/sys/resource/linux/setpriority.cpp
new file mode 100644
index 0000000000000..c22802ad0184e
--- /dev/null
+++ b/libc/src/sys/resource/linux/setpriority.cpp
@@ -0,0 +1,33 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of setpriority.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/sys/resource/setpriority.h"
+
+#include "hdr/types/id_t.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/setpriority.h"
+#include "src/__support/common.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+LLVM_LIBC_FUNCTION(int, setpriority, (int which, id_t who, int prio)) {
+  auto result = linux_syscalls::setpriority(which, who, prio);
+  if (!result) {
+    libc_errno = result.error();
+    return -1;
+  }
+  return 0;
+}
+
+} // namespace LIBC_NAMESPACE_DECL
diff --git a/libc/src/sys/resource/setpriority.h b/libc/src/sys/resource/setpriority.h
new file mode 100644
index 0000000000000..b701e3ec29483
--- /dev/null
+++ b/libc/src/sys/resource/setpriority.h
@@ -0,0 +1,26 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Implementation header of setpriority
+///
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_SYS_RESOURCE_SETPRIORITY_H
+#define LLVM_LIBC_SRC_SYS_RESOURCE_SETPRIORITY_H
+
+#include "hdr/types/id_t.h"
+#include "src/__support/macros/config.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+int setpriority(int which, id_t who, int prio);
+
+} // namespace LIBC_NAMESPACE_DECL
+
+#endif  // LLVM_LIBC_SRC_SYS_RESOURCE_SETPRIORITY_H
diff --git a/libc/test/src/sys/resource/CMakeLists.txt b/libc/test/src/sys/resource/CMakeLists.txt
index c79bf9974b306..c7703a6dcb9a9 100644
--- a/libc/test/src/sys/resource/CMakeLists.txt
+++ b/libc/test/src/sys/resource/CMakeLists.txt
@@ -47,3 +47,19 @@ add_libc_test(
     libc.test.UnitTest.ErrnoCheckingTest
     libc.test.UnitTest.ErrnoSetterMatcher
 )
+
+add_libc_test(
+  setpriority_test
+  SUITE
+    libc_sys_resource_unittests
+  SRCS
+    setpriority_test.cpp
+  DEPENDS
+    libc.hdr.sys_resource_macros
+    libc.hdr.types.id_t
+    libc.src.sys.resource.getpriority
+    libc.src.sys.resource.setpriority
+    libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
+)
+
diff --git a/libc/test/src/sys/resource/getpriority_test.cpp b/libc/test/src/sys/resource/getpriority_test.cpp
index 7a0b24eb48a30..46eafb9cc9f71 100644
--- a/libc/test/src/sys/resource/getpriority_test.cpp
+++ b/libc/test/src/sys/resource/getpriority_test.cpp
@@ -1,4 +1,3 @@
-
 //===----------------------------------------------------------------------===//
 //
 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
@@ -18,13 +17,12 @@
 #include "test/UnitTest/ErrnoCheckingTest.h"
 #include "test/UnitTest/Test.h"
 
-using LlvmLibcGetrusageTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+using LlvmLibcGetpriorityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST_F(LlvmLibcGetrusageTest, BasicTest) {
+TEST_F(LlvmLibcGetpriorityTest, BasicTest) {
   int nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
   ASSERT_ERRNO_SUCCESS();
   ASSERT_GE(nice, -20);
   ASSERT_LE(nice, 19);
-
 }
 
diff --git a/libc/test/src/sys/resource/setpriority_test.cpp b/libc/test/src/sys/resource/setpriority_test.cpp
new file mode 100644
index 0000000000000..130364b0fb9b5
--- /dev/null
+++ b/libc/test/src/sys/resource/setpriority_test.cpp
@@ -0,0 +1,31 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Unittests for setpriority.
+///
+//===----------------------------------------------------------------------===//
+
+#include "hdr/sys_resource_macros.h"
+#include "hdr/types/id_t.h"
+#include "src/sys/resource/getpriority.h"
+#include "src/sys/resource/setpriority.h"
+#include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
+#include "test/UnitTest/Test.h"
+
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
+using LlvmLibcSetpriorityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
+
+TEST_F(LlvmLibcSetpriorityTest,  BasicTest) {
+  int nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
+  ASSERT_ERRNO_SUCCESS();
+
+  ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, 0, nice), Succeeds());
+}
+

>From 4773deb6ff79ba098ad13386fa02d049c37d6ad0 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Fri, 28 Aug 2026 13:41:43 -0700
Subject: [PATCH 4/6] Improve getprioriry and setpriority tests; add aarch64
 and riscv entry points.

---
 libc/config/linux/aarch64/entrypoints.txt     |  2 ++
 libc/config/linux/riscv/entrypoints.txt       |  2 ++
 libc/test/src/sys/resource/CMakeLists.txt     |  2 ++
 .../src/sys/resource/getpriority_test.cpp     | 27 ++++++++++++++++---
 .../src/sys/resource/setpriority_test.cpp     |  7 +++++
 5 files changed, 37 insertions(+), 3 deletions(-)

diff --git a/libc/config/linux/aarch64/entrypoints.txt b/libc/config/linux/aarch64/entrypoints.txt
index 567025e62f43d..05bc74fa6932f 100644
--- a/libc/config/linux/aarch64/entrypoints.txt
+++ b/libc/config/linux/aarch64/entrypoints.txt
@@ -299,8 +299,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.random.getrandom
 
     # sys/resource.h entrypoints
+    libc.src.sys.resource.getpriority
     libc.src.sys.resource.getrlimit
     libc.src.sys.resource.getrusage
+    libc.src.sys.resource.setpriority
     libc.src.sys.resource.setrlimit
 
     # sys/sendfile.h entrypoints
diff --git a/libc/config/linux/riscv/entrypoints.txt b/libc/config/linux/riscv/entrypoints.txt
index 40fef13470322..cf28d6d9645e8 100644
--- a/libc/config/linux/riscv/entrypoints.txt
+++ b/libc/config/linux/riscv/entrypoints.txt
@@ -330,8 +330,10 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.sys.random.getrandom
 
     # sys/resource.h entrypoints
+    libc.src.sys.resource.getpriority
     libc.src.sys.resource.getrlimit
     libc.src.sys.resource.getrusage
+    libc.src.sys.resource.setpriority
     libc.src.sys.resource.setrlimit
 
     # sys/sendfile.h entrypoints
diff --git a/libc/test/src/sys/resource/CMakeLists.txt b/libc/test/src/sys/resource/CMakeLists.txt
index c7703a6dcb9a9..7e926a9e2996e 100644
--- a/libc/test/src/sys/resource/CMakeLists.txt
+++ b/libc/test/src/sys/resource/CMakeLists.txt
@@ -10,7 +10,9 @@ add_libc_test(
     libc.hdr.sys_resource_macros
     libc.hdr.types.id_t
     libc.src.sys.resource.getpriority
+    libc.src.sys.resource.setpriority
     libc.test.UnitTest.ErrnoCheckingTest
+    libc.test.UnitTest.ErrnoSetterMatcher
 )
 
 add_libc_test(
diff --git a/libc/test/src/sys/resource/getpriority_test.cpp b/libc/test/src/sys/resource/getpriority_test.cpp
index 46eafb9cc9f71..43ea569299fd2 100644
--- a/libc/test/src/sys/resource/getpriority_test.cpp
+++ b/libc/test/src/sys/resource/getpriority_test.cpp
@@ -14,15 +14,36 @@
 #include "hdr/sys_resource_macros.h"
 #include "hdr/types/id_t.h"
 #include "src/sys/resource/getpriority.h"
+#include "src/sys/resource/setpriority.h"
 #include "test/UnitTest/ErrnoCheckingTest.h"
+#include "test/UnitTest/ErrnoSetterMatcher.h"
 #include "test/UnitTest/Test.h"
 
+using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 using LlvmLibcGetpriorityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
 TEST_F(LlvmLibcGetpriorityTest, BasicTest) {
-  int nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
+  int current_nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
   ASSERT_ERRNO_SUCCESS();
-  ASSERT_GE(nice, -20);
-  ASSERT_LE(nice, 19);
+  ASSERT_GE(current_nice, -20);
+  ASSERT_LE(current_nice, 19);
+
+  // Increase to the max on Linux (i.e. minimal priority, which doesn't require special
+  // privileges), so that we can confirm we get it back correctly.
+  int nice = 19;
+  // Make sure we're not setting it to the same priority by chance
+  // however unlikely it may be.
+  if (nice == current_nice) {
+    nice -= 1;
+  }
+  ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, 0, nice), Succeeds());
+  ASSERT_THAT(LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0), Succeeds(nice));
+}
+
+TEST_F(LlvmLibcGetpriorityTest, TestBadPid) {
+  ASSERT_THAT(LIBC_NAMESPACE::getpriority(PRIO_PROCESS, -1), Fails(ESRCH, -1));
 }
 
+TEST_F(LlvmLibcGetpriorityTest, TestBadWho) {
+  ASSERT_THAT(LIBC_NAMESPACE::getpriority(99, 0), Fails(EINVAL, -1));
+}
diff --git a/libc/test/src/sys/resource/setpriority_test.cpp b/libc/test/src/sys/resource/setpriority_test.cpp
index 130364b0fb9b5..3bf75dbc34780 100644
--- a/libc/test/src/sys/resource/setpriority_test.cpp
+++ b/libc/test/src/sys/resource/setpriority_test.cpp
@@ -29,3 +29,10 @@ TEST_F(LlvmLibcSetpriorityTest,  BasicTest) {
   ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, 0, nice), Succeeds());
 }
 
+TEST_F(LlvmLibcSetpriorityTest, TestBadPid) {
+  ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, -1, 19), Fails(ESRCH, -1));
+}
+
+TEST_F(LlvmLibcSetpriorityTest, TestBadWho) {
+  ASSERT_THAT(LIBC_NAMESPACE::setpriority(99, 0, 19), Fails(EINVAL, -1));
+}

>From 3fbaced6debef5a261a10fb57780a8779e10efb0 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Fri, 28 Aug 2026 13:46:37 -0700
Subject: [PATCH 5/6] Fix clang formatting

---
 .../__support/OSUtil/linux/syscall_wrappers/getpriority.h    | 2 +-
 .../__support/OSUtil/linux/syscall_wrappers/setpriority.h    | 2 +-
 libc/src/sys/resource/getpriority.h                          | 2 +-
 libc/src/sys/resource/setpriority.h                          | 2 +-
 libc/test/src/sys/resource/getpriority_test.cpp              | 4 ++--
 libc/test/src/sys/resource/setpriority_test.cpp              | 5 +++--
 6 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
index ddd9c14ef7209..d4939568528cf 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/getpriority.h
@@ -31,4 +31,4 @@ LIBC_INLINE ErrorOr<int> getpriority(int which, id_t who) {
 } // namespace linux_syscalls
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif  // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETPRIORITY_H
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_GETPRIORITY_H
diff --git a/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h b/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
index e9d169c97c424..0d3b7ede81f8a 100644
--- a/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
+++ b/libc/src/__support/OSUtil/linux/syscall_wrappers/setpriority.h
@@ -31,4 +31,4 @@ LIBC_INLINE ErrorOr<int> setpriority(int which, id_t who, int niceval) {
 } // namespace linux_syscalls
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif  // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SETPRIORITY_H
+#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_LINUX_SYSCALL_WRAPPERS_SETPRIORITY_H
diff --git a/libc/src/sys/resource/getpriority.h b/libc/src/sys/resource/getpriority.h
index 103ee53ceaadc..ca89889f0a190 100644
--- a/libc/src/sys/resource/getpriority.h
+++ b/libc/src/sys/resource/getpriority.h
@@ -23,4 +23,4 @@ int getpriority(int which, id_t who);
 
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif  // LLVM_LIBC_SRC_SYS_RESOURCE_GETPRIORITY_H
+#endif // LLVM_LIBC_SRC_SYS_RESOURCE_GETPRIORITY_H
diff --git a/libc/src/sys/resource/setpriority.h b/libc/src/sys/resource/setpriority.h
index b701e3ec29483..bcfd2a422882a 100644
--- a/libc/src/sys/resource/setpriority.h
+++ b/libc/src/sys/resource/setpriority.h
@@ -23,4 +23,4 @@ int setpriority(int which, id_t who, int prio);
 
 } // namespace LIBC_NAMESPACE_DECL
 
-#endif  // LLVM_LIBC_SRC_SYS_RESOURCE_SETPRIORITY_H
+#endif // LLVM_LIBC_SRC_SYS_RESOURCE_SETPRIORITY_H
diff --git a/libc/test/src/sys/resource/getpriority_test.cpp b/libc/test/src/sys/resource/getpriority_test.cpp
index 43ea569299fd2..98a5eb5a601ca 100644
--- a/libc/test/src/sys/resource/getpriority_test.cpp
+++ b/libc/test/src/sys/resource/getpriority_test.cpp
@@ -28,8 +28,8 @@ TEST_F(LlvmLibcGetpriorityTest, BasicTest) {
   ASSERT_GE(current_nice, -20);
   ASSERT_LE(current_nice, 19);
 
-  // Increase to the max on Linux (i.e. minimal priority, which doesn't require special
-  // privileges), so that we can confirm we get it back correctly.
+  // Increase to the max on Linux (i.e. minimal priority, which doesn't require
+  // special privileges), so that we can confirm we get it back correctly.
   int nice = 19;
   // Make sure we're not setting it to the same priority by chance
   // however unlikely it may be.
diff --git a/libc/test/src/sys/resource/setpriority_test.cpp b/libc/test/src/sys/resource/setpriority_test.cpp
index 3bf75dbc34780..0483eac9665ee 100644
--- a/libc/test/src/sys/resource/setpriority_test.cpp
+++ b/libc/test/src/sys/resource/setpriority_test.cpp
@@ -22,7 +22,7 @@
 using namespace LIBC_NAMESPACE::testing::ErrnoSetterMatcher;
 using LlvmLibcSetpriorityTest = LIBC_NAMESPACE::testing::ErrnoCheckingTest;
 
-TEST_F(LlvmLibcSetpriorityTest,  BasicTest) {
+TEST_F(LlvmLibcSetpriorityTest, BasicTest) {
   int nice = LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0);
   ASSERT_ERRNO_SUCCESS();
 
@@ -30,7 +30,8 @@ TEST_F(LlvmLibcSetpriorityTest,  BasicTest) {
 }
 
 TEST_F(LlvmLibcSetpriorityTest, TestBadPid) {
-  ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, -1, 19), Fails(ESRCH, -1));
+  ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, -1, 19),
+              Fails(ESRCH, -1));
 }
 
 TEST_F(LlvmLibcSetpriorityTest, TestBadWho) {

>From 4f03e58c7f51b68017e2af2d7f21ed59d0152de8 Mon Sep 17 00:00:00 2001
From: Vadim Kotov <kotov at google.com>
Date: Fri, 28 Aug 2026 13:54:27 -0700
Subject: [PATCH 6/6] Fix getpriority test.

---
 libc/test/src/sys/resource/getpriority_test.cpp | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/libc/test/src/sys/resource/getpriority_test.cpp b/libc/test/src/sys/resource/getpriority_test.cpp
index 98a5eb5a601ca..567a344c1bb03 100644
--- a/libc/test/src/sys/resource/getpriority_test.cpp
+++ b/libc/test/src/sys/resource/getpriority_test.cpp
@@ -31,11 +31,10 @@ TEST_F(LlvmLibcGetpriorityTest, BasicTest) {
   // Increase to the max on Linux (i.e. minimal priority, which doesn't require
   // special privileges), so that we can confirm we get it back correctly.
   int nice = 19;
-  // Make sure we're not setting it to the same priority by chance
-  // however unlikely it may be.
-  if (nice == current_nice) {
-    nice -= 1;
-  }
+  // Make sure we're not setting it to the same priority by chance. If the below
+  // assertion ever fails, we'll need to re-think this test.
+  ASSERT_GT(nice, current_nice);
+
   ASSERT_THAT(LIBC_NAMESPACE::setpriority(PRIO_PROCESS, 0, nice), Succeeds());
   ASSERT_THAT(LIBC_NAMESPACE::getpriority(PRIO_PROCESS, 0), Succeeds(nice));
 }



More information about the libc-commits mailing list