master
c 71 lines 1.67 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 #include "../libnetdata.h"
4
5 pid_t pid_max = 4194304;
6
7 pid_t os_get_system_pid_max(void) {
8 static bool read = false;
9 if(read) return pid_max;
10 read = true;
11
12 #if defined(OS_MACOS)
13 int mib[2];
14 int maxproc;
15 size_t len = sizeof(maxproc);
16
17 mib[0] = CTL_KERN;
18 mib[1] = KERN_MAXPROC;
19
20 if (sysctl(mib, 2, &maxproc, &len, NULL, 0) == -1) {
21 pid_max = 99999; // Fallback value
22 nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot find system max pid. Assuming %d.", pid_max);
23 }
24 else pid_max = (pid_t)maxproc;
25
26 return pid_max;
27
28 #elif defined(OS_FREEBSD)
29
30 int32_t tmp_pid_max;
31
32 if (unlikely(GETSYSCTL_BY_NAME("kern.pid_max", tmp_pid_max))) {
33 pid_max = 99999;
34 nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot get system max pid. Assuming %d.", pid_max);
35 }
36 else
37 pid_max = tmp_pid_max;
38
39 return pid_max;
40
41 #elif defined(OS_LINUX)
42
43 char filename[FILENAME_MAX + 1];
44 snprintfz(filename, FILENAME_MAX, "%s/proc/sys/kernel/pid_max", netdata_configured_host_prefix?netdata_configured_host_prefix:"");
45
46 unsigned long long max = 0;
47 if(read_single_number_file(filename, &max) != 0) {
48 nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot open file '%s'. Assuming system supports %d pids.", filename, pid_max);
49 return pid_max;
50 }
51
52 if(!max) {
53 nd_log(NDLS_DAEMON, NDLP_ERR, "Cannot parse file '%s'. Assuming system supports %d pids.", filename, pid_max);
54 return pid_max;
55 }
56
57 pid_max = (pid_t) max;
58 return pid_max;
59
60 #elif defined(OS_WINDOWS)
61
62 pid_max = (pid_t)0x7FFFFFFF;
63 return pid_max;
64
65 #else
66
67 // return the default
68 return pid_max;
69
70 #endif
71 }