Skip to content

Commit

Permalink
get_pid_max: Return maximum value for pid_t type when reading fails
Browse files Browse the repository at this point in the history
  • Loading branch information
HiGarfield committed Jan 12, 2025
1 parent b5a5772 commit d8c104b
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d8c104b

Please sign in to comment.