process-util: add helper get_process_threads()

Let's add a proper helper for querying the number of threads in a
process.
This commit is contained in:
Lennart Poettering
2023-02-17 14:27:58 +01:00
parent ceecf3d45d
commit 6aa90884a0
6 changed files with 86 additions and 9 deletions
+1 -3
View File
@@ -277,7 +277,6 @@ static _used_ const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX
#if VALGRIND
_destructor_ static void cleanup_pools(void) {
_cleanup_free_ char *t = NULL;
int r;
/* Be nice to valgrind */
@@ -291,8 +290,7 @@ _destructor_ static void cleanup_pools(void) {
if (getpid() != gettid())
return;
r = get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t);
if (r < 0 || !streq(t, "1"))
if (get_process_threads(0) != 1)
return;
mempool_drop(&hashmap_pool);
+25
View File
@@ -1549,6 +1549,31 @@ _noreturn_ void freeze(void) {
pause();
}
int get_process_threads(pid_t pid) {
_cleanup_free_ char *t = NULL;
const char *p;
int n, r;
if (pid < 0)
return -EINVAL;
p = procfs_file_alloca(pid, "status");
r = get_proc_field(p, "Threads", WHITESPACE, &t);
if (r == -ENOENT)
return proc_mounted() == 0 ? -ENOSYS : -ESRCH;
if (r < 0)
return r;
r = safe_atoi(t, &n);
if (r < 0)
return r;
if (n < 0)
return -EINVAL;
return n;
}
static const char *const sigchld_code_table[] = {
[CLD_EXITED] = "exited",
[CLD_KILLED] = "killed",
+2
View File
@@ -189,3 +189,5 @@ int pidfd_verify_pid(int pidfd, pid_t pid);
int setpriority_closest(int priority);
_noreturn_ void freeze(void);
int get_process_threads(pid_t pid);