build: Make module build also depend on header file
[netsniff-ng.git] / proc.c
blob39d661e927ecff6b1866f1ff24ae0896ccd830a3
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;