Created
May 12, 2022 12:06
-
-
Save zuzzas/d32a34ae99ae23ad594c348204f07361 to your computer and use it in GitHub Desktop.
Testing inhibitor locks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
cat <<EOF > /etc/systemd/system/inhibit.service | |
[Unit] | |
Description=heh | |
After=network.target | |
[Service] | |
Type=simple | |
ExecStart=/home/ubuntu/inhibit | |
KillMode=none | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
systemctl daemon-reload | |
systemctl start inhibit.service | |
sed -i 's/#InhibitDelayMaxSec=5/InhibitDelayMaxSec=600/g' /etc/systemd/logind.conf | |
apt purge unattended-upgrades -y | |
systemctl reload systemd-logind |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// thanks to https://trstringer.com/systemd-inhibitor-locks/ | |
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/godbus/dbus/v5" | |
) | |
func main() { | |
fmt.Println("Starting dbus example") | |
// Get a handle on the system bus. There are two types | |
// of buses: system and session. The system bus is for | |
// handling system-wide operations (like in this case, | |
// shutdown). The session bus is a per-user bus. | |
conn, err := dbus.SystemBus() | |
if err != nil { | |
fmt.Printf("error getting system bus: %v\n", err) | |
os.Exit(1) | |
} | |
defer conn.Close() | |
// Call the Inhibit method so that this process register | |
// an inhibitor lock. This returns a file descriptor so | |
// that after a shutdown signal this process can signal | |
// back to systemd that it is complete by closing the | |
// file descriptor. | |
// | |
// The parameters that are passed to Inhibit dictate the | |
// state change. In this case, that is "shutdown". The | |
// mode can either be "delay" or "block". Delay will halt | |
// the state change for the InhibitDelayMaxSec setting, | |
// which defaults to 5 seconds. Block will indefinitely | |
// block the operation and should be used with caution. | |
var fd int | |
err = conn.Object( | |
"org.freedesktop.login1", | |
dbus.ObjectPath("/org/freedesktop/login1"), | |
).Call( | |
"org.freedesktop.login1.Manager.Inhibit", // Method | |
0, // Flags | |
"shutdown", // What | |
"Inhibitor Test", // Who | |
"Testing systemd inhibitors from Go", // Why | |
"delay", // Mode | |
).Store(&fd) | |
if err != nil { | |
fmt.Printf("error storing file descriptor: %v\n", err) | |
os.Exit(1) | |
} | |
fmt.Printf("Inhibitor file descriptor: %d\n", fd) | |
// Call AddMatch so that this process will be notified for | |
// the PrepareForShutdown signal. This will allow us to do | |
// custom logic when the machine is getting ready to shutdown. | |
err = conn.AddMatchSignal( | |
dbus.WithMatchInterface("org.freedesktop.login1.Manager"), | |
dbus.WithMatchObjectPath("/org/freedesktop/login1"), | |
dbus.WithMatchMember("PrepareForShutdown"), | |
) | |
if err != nil { | |
fmt.Printf("error adding match signal: %v\n", err) | |
os.Exit(1) | |
} | |
fmt.Println("Beginning to sleep forever") | |
select {} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment