Debian Codegraph @

src/utils/proc_status.c

raw
1 #include "utils/proc_status.h"
2 #include "main.h"
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <sys/types.h>
9
10 #define SECCOMP "Seccomp:"
11 // the sizeof simply including appending \t or space
12 #define SECCOMP_OFFSET (sizeof (SECCOMP))
13 #define KTHREAD "Kthread:"
14 #define KTHREAD_OFFSET (sizeof (KTHREAD))
15 #define TRACER_PID "TracerPid:"
16 #define TRACER_PID_OFFSET (sizeof (TRACER_PID))
17
18 static long
19 access_proc_with_key (pid_t pid, const char *comparator, size_t strsize)
20 {
21 long value = PROCFS_ERROR;
22 char buf[0x40];
23 char *end;
24 snprintf (buf, 0x40, "/proc/%d/status", pid);
25
26 FILE *f = fopen (buf, "r");
27 if (f == NULL)
28 return PROCFS_ERROR;
29
30 while (fgets (buf, 0x40, f))
31 if (STARTWITH (buf, comparator))
32 {
33 value = strtoull (buf + strsize, &end, 10);
34 if (end == buf + strsize)
35 value = PROCFS_ERROR;
36 break;
37 }
38
39 fclose (f);
40 return value;
41 }
42
43 seccomp_mode
44 get_proc_seccomp (pid_t pid)
45 {
46 return access_proc_with_key (pid, SECCOMP, SECCOMP_OFFSET);
47 }
48
49 kthread_mode
50 is_proc_kthread (pid_t pid)
51 {
52 return access_proc_with_key (pid, KTHREAD, KTHREAD_OFFSET);
53 }
54
55 pid_t
56 get_tracer_pid (pid_t pid)
57 {
58 return access_proc_with_key (pid, TRACER_PID, TRACER_PID_OFFSET);
59 }