From 7bee4133239b328991c7a46542e8cc2ddeadc9a7 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 24 Sep 2020 16:24:42 +0200 Subject: [PATCH] services: Reorganize services, detect X Detect X autostart a bit better with a separate service. The service relies on a shell script which searches for a socket which X creates on startup and only finishes when it finds the socket succesfully. This makes services which need X to start successfully a bit more robust, and while it can still break if X takes too long, it should be more stable now. Finally, services can restart more quickly when ended since they don't require the 2+ `RestartSec` option to be present anymore to successfully start. --- .../x-started-confirm.service | 1 + services/.config/systemd/user/dirmngr.service | 8 ++++++++ services/.config/systemd/user/dirmngr.socket | 11 ++++++++++ .../systemd/user/dropdown-terminal.service | 4 ++-- .../systemd/user/dropdown-todo.service | 4 ++-- .../.config/systemd/user/flashfocus.service | 4 ++-- .../systemd/user/gpg-agent-browser.socket | 13 ++++++++++++ .../systemd/user/gpg-agent-extra.socket | 13 ++++++++++++ .../.config/systemd/user/gpg-agent-ssh.socket | 13 ++++++++++++ .../.config/systemd/user/gpg-agent.service | 8 ++++++++ .../.config/systemd/user/gpg-agent.socket | 12 +++++++++++ .../flashfocus.service | 1 - .../user/greenclip.service.d/override.conf | 5 +++-- services/.config/systemd/user/mbsync.service | 8 ++++++++ services/.config/systemd/user/mbsync.timer | 10 ++++++++++ .../user/redshift.service.d/override.conf | 5 +++-- .../user/sockets.target.wants/dirmngr.socket | 1 + .../gpg-agent-browser.socket | 1 + .../gpg-agent-extra.socket | 1 + .../sockets.target.wants/gpg-agent-ssh.socket | 1 + .../sockets.target.wants/gpg-agent.socket | 1 + .../user/timers.target.wants/mbsync.timer | 1 + .../systemd/user/x-started-confirm.service | 10 ++++++++++ .../share/services/x-started-confirm.sh | 20 +++++++++++++++++++ 24 files changed, 145 insertions(+), 11 deletions(-) create mode 120000 services/.config/systemd/user/default.target.wants/x-started-confirm.service create mode 100644 services/.config/systemd/user/dirmngr.service create mode 100644 services/.config/systemd/user/dirmngr.socket create mode 100644 services/.config/systemd/user/gpg-agent-browser.socket create mode 100644 services/.config/systemd/user/gpg-agent-extra.socket create mode 100644 services/.config/systemd/user/gpg-agent-ssh.socket create mode 100644 services/.config/systemd/user/gpg-agent.service create mode 100644 services/.config/systemd/user/gpg-agent.socket delete mode 120000 services/.config/systemd/user/graphical-session.target.wants/flashfocus.service create mode 100644 services/.config/systemd/user/mbsync.service create mode 100644 services/.config/systemd/user/mbsync.timer create mode 120000 services/.config/systemd/user/sockets.target.wants/dirmngr.socket create mode 120000 services/.config/systemd/user/sockets.target.wants/gpg-agent-browser.socket create mode 120000 services/.config/systemd/user/sockets.target.wants/gpg-agent-extra.socket create mode 120000 services/.config/systemd/user/sockets.target.wants/gpg-agent-ssh.socket create mode 120000 services/.config/systemd/user/sockets.target.wants/gpg-agent.socket create mode 120000 services/.config/systemd/user/timers.target.wants/mbsync.timer create mode 100644 services/.config/systemd/user/x-started-confirm.service create mode 100755 services/.local/share/services/x-started-confirm.sh diff --git a/services/.config/systemd/user/default.target.wants/x-started-confirm.service b/services/.config/systemd/user/default.target.wants/x-started-confirm.service new file mode 120000 index 0000000..973183e --- /dev/null +++ b/services/.config/systemd/user/default.target.wants/x-started-confirm.service @@ -0,0 +1 @@ +../x-started-confirm.service \ No newline at end of file diff --git a/services/.config/systemd/user/dirmngr.service b/services/.config/systemd/user/dirmngr.service new file mode 100644 index 0000000..3c060cd --- /dev/null +++ b/services/.config/systemd/user/dirmngr.service @@ -0,0 +1,8 @@ +[Unit] +Description=GnuPG network certificate management daemon +Documentation=man:dirmngr(8) +Requires=dirmngr.socket + +[Service] +ExecStart=/usr/bin/dirmngr --supervised +ExecReload=/usr/bin/gpgconf --reload dirmngr diff --git a/services/.config/systemd/user/dirmngr.socket b/services/.config/systemd/user/dirmngr.socket new file mode 100644 index 0000000..ebabf89 --- /dev/null +++ b/services/.config/systemd/user/dirmngr.socket @@ -0,0 +1,11 @@ +[Unit] +Description=GnuPG network certificate management daemon +Documentation=man:dirmngr(8) + +[Socket] +ListenStream=%t/gnupg/S.dirmngr +SocketMode=0600 +DirectoryMode=0700 + +[Install] +WantedBy=sockets.target diff --git a/services/.config/systemd/user/dropdown-terminal.service b/services/.config/systemd/user/dropdown-terminal.service index 8a02ffa..78755de 100644 --- a/services/.config/systemd/user/dropdown-terminal.service +++ b/services/.config/systemd/user/dropdown-terminal.service @@ -1,13 +1,13 @@ [Unit] Description=Alacritty window hidden on i3 scratchpad -After=display-manager.service +Requires=x-started-confirm.service +After=x-started-confirm.service [Service] Type=simple # workaround to allow relative executable invocation (i.e. current users' home dir) ExecStart=/bin/bash -c 'alacritty --title "dropdown-terminal" --class Alacritty,scratchpad' Restart=always -RestartSec=2 [Install] WantedBy=default.target diff --git a/services/.config/systemd/user/dropdown-todo.service b/services/.config/systemd/user/dropdown-todo.service index 8bea35c..28f804d 100644 --- a/services/.config/systemd/user/dropdown-todo.service +++ b/services/.config/systemd/user/dropdown-todo.service @@ -1,13 +1,13 @@ [Unit] Description=Todo.md floating vim instance -After=display-manager.service +Requires=x-started-confirm.service +After=x-started-confirm.service [Service] Type=simple # workaround to allow relative executable invocation (i.e. current users' home dir) ExecStart=/bin/bash -c 'alacritty --title "dropdown-todo" --class "Alacritty,scratchpad" -e nvim -c ":set nonumber norelativenumber noshowmode noruler laststatus=0 noshowcmd shortmess=F | :Limelight" %h/documents/records/todo.md' Restart=always -RestartSec=2 [Install] WantedBy=default.target diff --git a/services/.config/systemd/user/flashfocus.service b/services/.config/systemd/user/flashfocus.service index c407aff..ddd6c02 100644 --- a/services/.config/systemd/user/flashfocus.service +++ b/services/.config/systemd/user/flashfocus.service @@ -1,12 +1,12 @@ [Unit] Description=Perform windows animations on focus PartOf=graphical-session.target -After=graphical-session.target +Requires=x-started-confirm.service +After=x-started-confirm.service [Service] ExecStart=/usr/bin/flashfocus Restart=on-failure -RestartSec=5 [Install] WantedBy=default.target diff --git a/services/.config/systemd/user/gpg-agent-browser.socket b/services/.config/systemd/user/gpg-agent-browser.socket new file mode 100644 index 0000000..bc8d344 --- /dev/null +++ b/services/.config/systemd/user/gpg-agent-browser.socket @@ -0,0 +1,13 @@ +[Unit] +Description=GnuPG cryptographic agent and passphrase cache (access for web browsers) +Documentation=man:gpg-agent(1) + +[Socket] +ListenStream=%t/gnupg/S.gpg-agent.browser +FileDescriptorName=browser +Service=gpg-agent.service +SocketMode=0600 +DirectoryMode=0700 + +[Install] +WantedBy=sockets.target diff --git a/services/.config/systemd/user/gpg-agent-extra.socket b/services/.config/systemd/user/gpg-agent-extra.socket new file mode 100644 index 0000000..5b87d09 --- /dev/null +++ b/services/.config/systemd/user/gpg-agent-extra.socket @@ -0,0 +1,13 @@ +[Unit] +Description=GnuPG cryptographic agent and passphrase cache (restricted) +Documentation=man:gpg-agent(1) + +[Socket] +ListenStream=%t/gnupg/S.gpg-agent.extra +FileDescriptorName=extra +Service=gpg-agent.service +SocketMode=0600 +DirectoryMode=0700 + +[Install] +WantedBy=sockets.target diff --git a/services/.config/systemd/user/gpg-agent-ssh.socket b/services/.config/systemd/user/gpg-agent-ssh.socket new file mode 100644 index 0000000..798c1d9 --- /dev/null +++ b/services/.config/systemd/user/gpg-agent-ssh.socket @@ -0,0 +1,13 @@ +[Unit] +Description=GnuPG cryptographic agent (ssh-agent emulation) +Documentation=man:gpg-agent(1) man:ssh-add(1) man:ssh-agent(1) man:ssh(1) + +[Socket] +ListenStream=%t/gnupg/S.gpg-agent.ssh +FileDescriptorName=ssh +Service=gpg-agent.service +SocketMode=0600 +DirectoryMode=0700 + +[Install] +WantedBy=sockets.target diff --git a/services/.config/systemd/user/gpg-agent.service b/services/.config/systemd/user/gpg-agent.service new file mode 100644 index 0000000..a050fcc --- /dev/null +++ b/services/.config/systemd/user/gpg-agent.service @@ -0,0 +1,8 @@ +[Unit] +Description=GnuPG cryptographic agent and passphrase cache +Documentation=man:gpg-agent(1) +Requires=gpg-agent.socket + +[Service] +ExecStart=/usr/bin/gpg-agent --supervised +ExecReload=/usr/bin/gpgconf --reload gpg-agent diff --git a/services/.config/systemd/user/gpg-agent.socket b/services/.config/systemd/user/gpg-agent.socket new file mode 100644 index 0000000..4257c2c --- /dev/null +++ b/services/.config/systemd/user/gpg-agent.socket @@ -0,0 +1,12 @@ +[Unit] +Description=GnuPG cryptographic agent and passphrase cache +Documentation=man:gpg-agent(1) + +[Socket] +ListenStream=%t/gnupg/S.gpg-agent +FileDescriptorName=std +SocketMode=0600 +DirectoryMode=0700 + +[Install] +WantedBy=sockets.target diff --git a/services/.config/systemd/user/graphical-session.target.wants/flashfocus.service b/services/.config/systemd/user/graphical-session.target.wants/flashfocus.service deleted file mode 120000 index 0916e81..0000000 --- a/services/.config/systemd/user/graphical-session.target.wants/flashfocus.service +++ /dev/null @@ -1 +0,0 @@ -/home/marty/.config/systemd/user/flashfocus.service \ No newline at end of file diff --git a/services/.config/systemd/user/greenclip.service.d/override.conf b/services/.config/systemd/user/greenclip.service.d/override.conf index b7063fd..673779e 100644 --- a/services/.config/systemd/user/greenclip.service.d/override.conf +++ b/services/.config/systemd/user/greenclip.service.d/override.conf @@ -1,2 +1,3 @@ -[Service] -RestartSec=5 +[Unit] +Requires=x-started-confirm.service +After=x-started-confirm.service diff --git a/services/.config/systemd/user/mbsync.service b/services/.config/systemd/user/mbsync.service new file mode 100644 index 0000000..8c6546b --- /dev/null +++ b/services/.config/systemd/user/mbsync.service @@ -0,0 +1,8 @@ +[Unit] +Description=Mailbox synchronization service +Wants=network-online.target gpg-agent.socket +After=network.target network-online.target dbus.socket gpg-agent.socket + +[Service] +Type=oneshot +ExecStart=/home/marty/.local/bin/mail-check diff --git a/services/.config/systemd/user/mbsync.timer b/services/.config/systemd/user/mbsync.timer new file mode 100644 index 0000000..e15db08 --- /dev/null +++ b/services/.config/systemd/user/mbsync.timer @@ -0,0 +1,10 @@ +[Unit] +Description=Mailbox synchronization timer + +[Timer] +OnBootSec=1m +OnUnitActiveSec=1h +Unit=mbsync.service + +[Install] +WantedBy=timers.target diff --git a/services/.config/systemd/user/redshift.service.d/override.conf b/services/.config/systemd/user/redshift.service.d/override.conf index 8611c98..673779e 100644 --- a/services/.config/systemd/user/redshift.service.d/override.conf +++ b/services/.config/systemd/user/redshift.service.d/override.conf @@ -1,2 +1,3 @@ -[Service] -RestartSec=10 +[Unit] +Requires=x-started-confirm.service +After=x-started-confirm.service diff --git a/services/.config/systemd/user/sockets.target.wants/dirmngr.socket b/services/.config/systemd/user/sockets.target.wants/dirmngr.socket new file mode 120000 index 0000000..ce41479 --- /dev/null +++ b/services/.config/systemd/user/sockets.target.wants/dirmngr.socket @@ -0,0 +1 @@ +../dirmngr.socket \ No newline at end of file diff --git a/services/.config/systemd/user/sockets.target.wants/gpg-agent-browser.socket b/services/.config/systemd/user/sockets.target.wants/gpg-agent-browser.socket new file mode 120000 index 0000000..7f3e9b7 --- /dev/null +++ b/services/.config/systemd/user/sockets.target.wants/gpg-agent-browser.socket @@ -0,0 +1 @@ +../gpg-agent-browser.socket \ No newline at end of file diff --git a/services/.config/systemd/user/sockets.target.wants/gpg-agent-extra.socket b/services/.config/systemd/user/sockets.target.wants/gpg-agent-extra.socket new file mode 120000 index 0000000..5a1bed5 --- /dev/null +++ b/services/.config/systemd/user/sockets.target.wants/gpg-agent-extra.socket @@ -0,0 +1 @@ +../gpg-agent-extra.socket \ No newline at end of file diff --git a/services/.config/systemd/user/sockets.target.wants/gpg-agent-ssh.socket b/services/.config/systemd/user/sockets.target.wants/gpg-agent-ssh.socket new file mode 120000 index 0000000..3b40290 --- /dev/null +++ b/services/.config/systemd/user/sockets.target.wants/gpg-agent-ssh.socket @@ -0,0 +1 @@ +../gpg-agent-ssh.socket \ No newline at end of file diff --git a/services/.config/systemd/user/sockets.target.wants/gpg-agent.socket b/services/.config/systemd/user/sockets.target.wants/gpg-agent.socket new file mode 120000 index 0000000..8b9f67c --- /dev/null +++ b/services/.config/systemd/user/sockets.target.wants/gpg-agent.socket @@ -0,0 +1 @@ +../gpg-agent.socket \ No newline at end of file diff --git a/services/.config/systemd/user/timers.target.wants/mbsync.timer b/services/.config/systemd/user/timers.target.wants/mbsync.timer new file mode 120000 index 0000000..fa56ef4 --- /dev/null +++ b/services/.config/systemd/user/timers.target.wants/mbsync.timer @@ -0,0 +1 @@ +../mbsync.timer \ No newline at end of file diff --git a/services/.config/systemd/user/x-started-confirm.service b/services/.config/systemd/user/x-started-confirm.service new file mode 100644 index 0000000..11c74f1 --- /dev/null +++ b/services/.config/systemd/user/x-started-confirm.service @@ -0,0 +1,10 @@ +[Unit] +Description=Monitor status of X server start + +[Service] +Type=oneshot +ExecStart=/bin/bash -c '%h/.local/share/services/x-started-confirm.sh' +RestartSec=2 + +[Install] +WantedBy=default.target diff --git a/services/.local/share/services/x-started-confirm.sh b/services/.local/share/services/x-started-confirm.sh new file mode 100755 index 0000000..cc9f9aa --- /dev/null +++ b/services/.local/share/services/x-started-confirm.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +# from: https://superuser.com/questions/759759/writing-a-service-that-depends-on-xorg/1351489#1351489 + +COUNTER=0 + +while true; do + # Check whether or not socket exists + if [ -S /tmp/.X11-unix/X0 ]; then + exit 0 + fi + + ((++COUNTER)) + + if [ $COUNTER -gt 20 ]; then + printf "X did not start in time\n" + exit 1 + fi + printf "X not yet started, counter at %s\n" "$COUNTER" + sleep 1 +done