8 #include <sys/resource.h>
17 void cpu_affinity(int cpu
)
20 cpu_set_t cpu_bitmask
;
22 CPU_ZERO(&cpu_bitmask
);
23 CPU_SET(cpu
, &cpu_bitmask
);
25 ret
= sched_setaffinity(getpid(), sizeof(cpu_bitmask
),
28 panic("Can't set this cpu affinity!\n");
31 int set_proc_prio(int priority
)
33 int ret
= setpriority(PRIO_PROCESS
, getpid(), priority
);
35 panic("Can't set nice val to %i!\n", priority
);
40 int set_sched_status(int policy
, int priority
)
42 int ret
, min_prio
, max_prio
;
43 struct sched_param sp
;
45 max_prio
= sched_get_priority_max(policy
);
46 min_prio
= sched_get_priority_min(policy
);
48 if (max_prio
== -1 || min_prio
== -1)
49 printf("Cannot determine scheduler prio limits!\n");
50 else if (priority
< min_prio
)
52 else if (priority
> max_prio
)
55 memset(&sp
, 0, sizeof(sp
));
56 sp
.sched_priority
= priority
;
58 ret
= sched_setscheduler(getpid(), policy
, &sp
);
60 printf("Cannot set scheduler policy!\n");
64 ret
= sched_setparam(getpid(), &sp
);
66 printf("Cannot set scheduler prio!\n");
73 ssize_t
proc_get_cmdline(unsigned int pid
, char *cmdline
, size_t len
)
78 snprintf(path
, sizeof(path
), "/proc/%u/exe", pid
);
79 ret
= readlink(path
, cmdline
, len
- 1);
88 static int match_pid_by_inode(pid_t pid
, ino_t ino
)
94 if (snprintf(path
, sizeof(path
), "/proc/%u/fd", pid
) == -1)
95 panic("giant process name! %u\n", pid
);
101 while ((ent
= readdir(dir
))) {
104 if (snprintf(path
, sizeof(path
), "/proc/%u/fd/%s",
105 pid
, ent
->d_name
) < 0)
108 if (stat(path
, &statbuf
) < 0)
111 if (S_ISSOCK(statbuf
.st_mode
) && ino
== statbuf
.st_ino
) {
121 int proc_find_by_inode(ino_t ino
, char *cmdline
, size_t len
, pid_t
*pid
)
131 dir
= opendir("/proc");
133 panic("Cannot open /proc: %s\n", strerror(errno
));
135 while ((ent
= readdir(dir
))) {
138 const char *name
= ent
->d_name
;
139 pid_t cur_pid
= strtoul(name
, &end
, 10);
142 if (cur_pid
== 0 && end
== name
)
145 ret
= match_pid_by_inode(cur_pid
, ino
);
147 ret
= proc_get_cmdline(cur_pid
, cmdline
, len
);
149 panic("Failed to get process cmdline: %s\n", strerror(errno
));
161 int proc_exec(const char *proc
, char *const argv
[])
170 } else if (pid
== 0) {
171 if (execvp(proc
, argv
) < 0)
172 fprintf(stderr
, "Failed to exec: %s\n", proc
);
176 if (waitpid(pid
, &status
, 0) < 0) {
181 if (!WIFEXITED(status
))
182 return -WEXITSTATUS(status
);
187 bool proc_exists(pid_t pid
)
192 if (snprintf(path
, sizeof(path
), "/proc/%u", pid
) < 0)
195 return stat(path
, &statbuf
) == 0;