aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/dot_local/bin/executable_zellij-inhibit-watcher
blob: 6af70329cd396ca8c4935d1c8cf4abb7f4191001 (plain) (blame)
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
#!/bin/sh
# Block while at least one zellij server process was spawned from an
# SSH context, exit cleanly once none remain.
#
# Rationale: a zellij session started locally (e.g. from a sway terminal)
# is the user actively sitting in front of the laptop — that should NOT
# inhibit suspend. Only zellij sessions started while SSH'd in deserve
# the lock, so the host stays awake across detach + disconnect but
# normal local-attended suspend still works.
#
# Detection: zellij's daemonised server is exec'd by the client and
# inherits the client's environment. Linux preserves that exec-time
# environment in /proc/<pid>/environ for the life of the process, even
# after the original SSH session is gone. So an "ssh-spawned" zellij is
# one whose environ contains SSH_CONNECTION=.
#
# This script is the ExecStart payload of zellij-inhibit-suspend.service,
# which wraps it in systemd-inhibit. When this script exits, the lock is
# released. The .path unit re-fires the service on the next zellij socket
# transition.
set -eu

poll=${ZELLIJ_INHIBIT_POLL:-15}

has_ssh_zellij() {
  pids=$(pgrep -x zellij 2>/dev/null) || return 1
  for pid in $pids; do
    [ -r "/proc/$pid/environ" ] || continue
    if tr '\0' '\n' <"/proc/$pid/environ" 2>/dev/null |
      grep -q '^SSH_CONNECTION='; then
      return 0
    fi
  done
  return 1
}

while has_ssh_zellij; do
  sleep "$poll"
done