summaryrefslogtreecommitdiffstats
path: root/src/libnetdata/os/get_pid_max.c
blob: 70372a74380f433fa05110c5bb9377cd819a819e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// SPDX-License-Identifier: GPL-3.0-or-later

#include "../libnetdata.h"

pid_t pid_max = 4194304;

pid_t os_get_system_pid_max(void) {
    static bool read = false;
    if(read) return pid_max;
    read = true;

#if defined(OS_MACOS)
    int mib[2];
    int maxproc;
    size_t len = sizeof(maxproc);

    mib[0] = CTL_KERN;
    mib[1] = KERN_MAXPROC;

    if (sysctl(mib, 2, &maxproc, &len, NULL, 0) == -1) {
        pid_max = 99999; // Fallback value
        nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot find system max pid. Assuming %d.", pid_max);
    }
    else pid_max = (pid_t)maxproc;

    return pid_max;

#elif defined(OS_FREEBSD)

    int32_t tmp_pid_max;

    if (unlikely(GETSYSCTL_BY_NAME("kern.pid_max", tmp_pid_max))) {
        pid_max = 99999;
        nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot get system max pid. Assuming %d.", pid_max);
    }
    else
        pid_max = tmp_pid_max;

    return pid_max;

#elif defined(OS_LINUX)

    char filename[FILENAME_MAX + 1];
    snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix?netdata_configured_host_prefix:"");

    unsigned long long max = 0;
    if(read_single_number_file(filename, &max) != 0) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
        return pid_max;
    }

    if(!max) {
        nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
        return pid_max;
    }

    pid_max = (pid_t) max;
    return pid_max;

#elif defined(OS_WINDOWS)

    pid_max = (pid_t)0x7FFFFFFF;
    return pid_max;

#else

    // return the default
    return pid_max;

#endif
}