diff --git a/src/util.c b/src/util.c index 82e9784b..099656cf 100644 --- a/src/util.c +++ b/src/util.c @@ -94,22 +94,26 @@ int get_ncpu(void) return cached_ncpu; } +#define MAX_INT_TYPE(type) \ + ((type) ~((type)1 << (sizeof(type) * 8 - 1))) + pid_t get_pid_max(void) { #if defined(__linux__) - long pid_max = -1; - FILE *fd; - if ((fd = fopen("/proc/sys/kernel/pid_max", "r")) == NULL) + long pid_max; + FILE *fp; + if ((fp = fopen("/proc/sys/kernel/pid_max", "r")) == NULL) { fprintf(stderr, "Fail to open /proc/sys/kernel/pid_max\n"); - return (pid_t)-1; + return MAX_INT_TYPE(pid_t); } - if (fscanf(fd, "%ld", &pid_max) != 1) + if (fscanf(fp, "%ld", &pid_max) != 1) { fprintf(stderr, "Fail to read /proc/sys/kernel/pid_max\n"); - pid_max = -1; + fclose(fp); + return MAX_INT_TYPE(pid_t); } - fclose(fd); + fclose(fp); return (pid_t)pid_max; #elif defined(__FreeBSD__) return (pid_t)99998;