Get a sysctl value as an integer.
(name)
| 288 | _sysctl_cache = {} |
| 289 | |
| 290 | def _get_sysctl(name): |
| 291 | """Get a sysctl value as an integer.""" |
| 292 | try: |
| 293 | return _sysctl_cache[name] |
| 294 | except KeyError: |
| 295 | pass |
| 296 | |
| 297 | # At least Linux and FreeBSD support the "-n" option |
| 298 | cmd = ['sysctl', '-n', name] |
| 299 | proc = subprocess.run(cmd, |
| 300 | stdout=subprocess.PIPE, |
| 301 | stderr=subprocess.STDOUT, |
| 302 | text=True) |
| 303 | if proc.returncode: |
| 304 | support.print_warning(f'{' '.join(cmd)!r} command failed with ' |
| 305 | f'exit code {proc.returncode}') |
| 306 | # cache the error to only log the warning once |
| 307 | _sysctl_cache[name] = None |
| 308 | return None |
| 309 | output = proc.stdout |
| 310 | |
| 311 | # Parse '0\n' to get '0' |
| 312 | try: |
| 313 | value = int(output.strip()) |
| 314 | except Exception as exc: |
| 315 | support.print_warning(f'Failed to parse {' '.join(cmd)!r} ' |
| 316 | f'command output {output!r}: {exc!r}') |
| 317 | # cache the error to only log the warning once |
| 318 | _sysctl_cache[name] = None |
| 319 | return None |
| 320 | |
| 321 | _sysctl_cache[name] = value |
| 322 | return value |
| 323 | |
| 324 | |
| 325 | def tcp_blackhole(): |
no test coverage detected
searching dependent graphs…