build: Handle libnetfilter-conntrack with pkg-config
[netsniff-ng.git] / proc.c
blob672f00d64e1f4ce651abe3f0d1f8eb91fa2c2ef9
1 #define _GNU_SOURCE
2 #include <sched.h>
3 #include <sys/types.h>
4 #include <sys/resource.h>
5 #include <unistd.h>
6 #include <string.h>
7 #include <errno.h>
9 #include "proc.h"
10 #include "die.h"
12 void cpu_affinity(int cpu)
14 int ret;
15 cpu_set_t cpu_bitmask;
17 CPU_ZERO(&cpu_bitmask);
18 CPU_SET(cpu, &cpu_bitmask);
20 ret = sched_setaffinity(getpid(), sizeof(cpu_bitmask),
21 &cpu_bitmask);
22 if (ret)
23 panic("Can't set this cpu affinity!\n");
26 int set_proc_prio(int priority)
28 int ret = setpriority(PRIO_PROCESS, getpid(), priority);
29 if (ret)
30 panic("Can't set nice val to %i!\n", priority);
32 return 0;
35 int set_sched_status(int policy, int priority)
37 int ret, min_prio, max_prio;
38 struct sched_param sp;
40 max_prio = sched_get_priority_max(policy);
41 min_prio = sched_get_priority_min(policy);
43 if (max_prio == -1 || min_prio == -1)
44 printf("Cannot determine scheduler prio limits!\n");
45 else if (priority < min_prio)
46 priority = min_prio;
47 else if (priority > max_prio)
48 priority = max_prio;
50 memset(&sp, 0, sizeof(sp));
51 sp.sched_priority = priority;
53 ret = sched_setscheduler(getpid(), policy, &sp);
54 if (ret) {
55 printf("Cannot set scheduler policy!\n");
56 return -EINVAL;
59 ret = sched_setparam(getpid(), &sp);
60 if (ret) {
61 printf("Cannot set scheduler prio!\n");
62 return -EINVAL;
65 return 0;
68 ssize_t proc_get_cmdline(unsigned int pid, char *cmdline, size_t len)
70 ssize_t ret;
71 char path[1024];
73 snprintf(path, sizeof(path), "/proc/%u/exe", pid);
74 ret = readlink(path, cmdline, len - 1);
75 if (ret < 0)
76 cmdline[0] = '\0';
77 else
78 cmdline[ret] = '\0';
80 return ret;