Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

userManagerOom.ts

22 lines1.3 KB

1/**
2 * Run after `systemctl --user daemon-reload`. The user manager's -.slice shares the cgroup of
3 * user@<uid>.service and reports it to systemd-oomd as `auto` on reload, which drops the system
4 * manager's `kill` watch (systemd/systemd#43174). Mirroring that policy onto -.slice restores the
5 * watch and keeps it through later reloads. Best effort: exits 0 when there is nothing to mirror.
6 */
7export const RESTORE_OOMD_WATCH_SCRIPT = [
8 "unit=$(systemctl --user show --property=ControlGroup --value -- -.slice) || exit 0",
9 "unit=${unit##*/}",
10 "case $unit in user@*.service) ;; *) exit 0 ;; esac",
11 'show() { systemctl show --property="$1" --value "$unit"; }',
12 '[ "$(show ManagedOOMMemoryPressure)" = kill ] || exit 0',
13 "limit=$(show ManagedOOMMemoryPressureLimit)",
14 "case $limit in '' | *[!0-9]*) exit 0 ;; esac",
15 // The limit is a fraction of 2^32; set-property takes a percentage.
16 "p=$(( (limit * 10000 + 2147483648) / 4294967296 ))",
17 'set -- ManagedOOMMemoryPressure=kill "ManagedOOMMemoryPressureLimit=$((p / 100)).$(printf %02d $((p % 100)))%"',
18 "duration=$(show ManagedOOMMemoryPressureDurationUSec)",
19 'case $duration in "" | infinity | "[not set]") ;; *) set -- "$@" "ManagedOOMMemoryPressureDurationSec=$duration" ;; esac',
20 'exec systemctl --user set-property --runtime -- -.slice "$@"',
21 "",
22].join("\n");