socket-activate printer

Previous to this commit, the printer needs to continuously run in the
background, even if it doesn't do much most of the time, to keep the
socket open and handle incoming connections. This commit hands over
handling the open socket to systemd and the printer-code gets started by
systemd only when there is something incoming on the socket. The printer
itself then takes over the socket from systemd, handles the incoming
data and exits afterwards, returning socket management to systemd again.

This reduces running processes as well as the memory footprint of the
running system.
This commit is contained in:
Jonas Große Sundrup 2020-12-27 15:34:58 +01:00
parent 1e2627c837
commit d1fe37662a
No known key found for this signature in database
GPG key ID: 5B587ADC0531D442
5 changed files with 32 additions and 26 deletions

View file

@ -24,14 +24,15 @@ install: printer.arm
ssh -o AddKeysToAgent=yes root@$(host) systemctl stop printer || true
scp printer.arm root@$(host):
scp printer.service root@$(host):/etc/systemd/system
scp printer.socket root@$(host):/etc/systemd/system
ssh root@$(host) systemctl daemon-reload
ssh root@$(host) systemctl enable printer
ssh root@$(host) systemctl restart printer
ssh root@$(host) systemctl enable printer.socket
ssh root@$(host) systemctl restart printer.socket
.PHONY: release
release: printer.arm printer.x86
rm -f release.zip
zip release.zip printer.arm printer.x86 printer.service -r
zip release.zip printer.arm printer.x86 printer.service printer.socket -r
.PHONY: install_config
install_config:

View file

@ -3,6 +3,7 @@ cd /home/root/bin
wget -O release.zip http://github.com/evidlo/remarkable_printer/releases/latest/download/release.zip
unzip -o release.zip
mv printer.service /etc/systemd/system
mv printer.socket /etc/systemd/system
systemctl daemon-reload
systemctl enable --now printer.service
systemctl enable --now printer.socket
rm printer.x86 release.zip

34
main.go
View file

@ -60,32 +60,30 @@ func main() {
// ----- Listen for connections -----
// Listen for incoming connections.
l, err := net.Listen("tcp", *CONN_HOST + ":" + *CONN_PORT)
l, err := net.FileListener(os.NewFile(3, "systemd-socket"))
check(err)
defer l.Close()
// Close the listener when the application closes.
fmt.Println("Listening on " + *CONN_HOST + ":" + *CONN_PORT)
for {
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
handleRequest(conn)
// Restart xochitl
if *restart {
stdout, err := exec.Command("systemctl", "restart", "xochitl").CombinedOutput()
if err != nil {
fmt.Println("xochitl restart failed with message:", string(stdout))
}
}
// Listen for an incoming connection.
conn, err := l.Accept()
if err != nil {
fmt.Println("Error accepting: ", err.Error())
os.Exit(1)
}
}
handleRequest(conn)
// Restart xochitl
if *restart {
stdout, err := exec.Command("systemctl", "restart", "xochitl").CombinedOutput()
if err != nil {
fmt.Println("xochitl restart failed with message:", string(stdout))
}
}
}
func debug(msg ...string) {
if LOG_LEVEL == "debug" {

View file

@ -1,9 +1,6 @@
[Unit]
Description=Native printing to reMarkable
Requires=printer.socket
[Service]
ExecStart=/home/root/printer.arm -restart -debug
Restart=always
[Install]
WantedBy=multi-user.target

9
printer.socket Normal file
View file

@ -0,0 +1,9 @@
[Unit]
Description=Socket for native printing to reMarkable
After=multi-user.target
[Socket]
ListenStream=10.11.99.1:9100
[Install]
WantedBy=multi-user.target