From 0e7272f3896788bbc53614de1a1b30b082a988d0 Mon Sep 17 00:00:00 2001 From: Angelo Marletta Date: Mon, 2 Jul 2012 12:06:11 +0100 Subject: added function get_pid_max() to detect the maximum pid value --- src/cpulimit.c | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/cpulimit.c b/src/cpulimit.c index e7d5309..f35a954 100644 --- a/src/cpulimit.c +++ b/src/cpulimit.c @@ -147,6 +147,26 @@ static int get_ncpu() { return ncpu; } +int get_pid_max() +{ +#ifdef __linux__ + //read /proc/sys/kernel/pid_max + static char buffer[1024]; + FILE *fd = fopen("/proc/sys/kernel/pid_max", "r"); + if (fd==NULL) return -1; + if (fgets(buffer, sizeof(buffer), fd)==NULL) { + fclose(fd); + return -1; + } + fclose(fd); + return atoi(buffer); +#elif defined __FreeBSD__ + return 99998; +#elif defined __APPLE__ + return 99998; +#endif +} + void limit_process(pid_t pid, double limit, int include_children) { //slice of the slot in which the process is allowed to run @@ -347,7 +367,7 @@ int main(int argc, char **argv) { } } while(next_option != -1); - if (pid_ok && (pid <= 1 || pid >= 65536)) { + if (pid_ok && (pid <= 1 || pid >= get_pid_max())) { fprintf(stderr,"Error: Invalid value for argument PID\n"); print_usage(stderr, 1); exit(1); -- cgit v1.2.3