Skip to content

Commit 5c2e99f

Browse files
fix: Pidfile handling and socket docs (runfinch#101)
Signed-off-by: Shubhranshu153 <[email protected]>
1 parent 0469999 commit 5c2e99f

File tree

5 files changed

+56
-18
lines changed

5 files changed

+56
-18
lines changed

cmd/finch-daemon/main.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424

2525
"github.com/coreos/go-systemd/v22/activation"
2626
"github.com/coreos/go-systemd/v22/daemon"
27+
"github.com/gofrs/flock"
2728
"github.com/moby/moby/pkg/pidfile"
2829
"github.com/runfinch/finch-daemon/api/router"
2930
"github.com/runfinch/finch-daemon/pkg/flog"
@@ -104,32 +105,39 @@ func getListener(options *DaemonOptions) (net.Listener, error) {
104105
return listener, nil
105106
}
106107

107-
func handlePidFileOptions(options *DaemonOptions) error {
108-
if options.pidFile != "" {
109-
if err := os.MkdirAll(filepath.Dir(options.pidFile), 0o640); err != nil {
110-
return fmt.Errorf("failed to create pidfile directory %s", err)
111-
}
112-
if err := pidfile.Write(options.pidFile, os.Getpid()); err != nil {
113-
return fmt.Errorf("failed to start daemon, ensure finch daemon is not running or delete %s %w", options.pidFile, err)
114-
}
115-
}
116-
return nil
117-
}
118-
119108
func run(options *DaemonOptions) error {
120109
// This sets the log level of the dependencies that use logrus (e.g., containerd library).
121110
if options.debug {
122111
logrus.SetLevel(logrus.DebugLevel)
123112
}
124113

125-
defer func() {
126-
if err := os.Remove(options.pidFile); err != nil {
127-
logrus.Errorf("failed to remove pidfile %s", options.pidFile)
114+
if options.pidFile != "" {
115+
if err := os.MkdirAll(filepath.Dir(options.pidFile), 0o600); err != nil {
116+
return fmt.Errorf("failed to create pidfile directory %s", err)
128117
}
129-
}()
130118

131-
if err := handlePidFileOptions(options); err != nil {
132-
return err
119+
pidFileLock := flock.New(options.pidFile)
120+
121+
defer func() {
122+
pidFileLock.Unlock()
123+
}()
124+
125+
if isLocked, err := pidFileLock.TryLock(); err != nil || !isLocked {
126+
return fmt.Errorf("failed to acquire lock on PID file (%s); ensure only one instance is using the PID file path", options.pidFile)
127+
}
128+
129+
if err := pidfile.Write(options.pidFile, os.Getpid()); err != nil {
130+
return fmt.Errorf("failed to start daemon, ensure finch daemon is not running or delete %s %w", options.pidFile, err)
131+
}
132+
133+
pidFileLock.Unlock()
134+
135+
// Defer is at the end of trying to write to PID file so that it doesn't remove the pidfile created by another process when daemon fails to start
136+
defer func() {
137+
if err := os.Remove(options.pidFile); err != nil {
138+
logrus.Errorf("failed to remove pidfile %s", options.pidFile)
139+
}
140+
}()
133141
}
134142

135143
logger := flog.NewLogrus()
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[Unit]
2+
Description=finch daemon
3+
Documentation=https://runfinch.com
4+
After=network.target local-fs.target containerd.service finch.socket
5+
Wants=network.target containerd.service
6+
Requires=finch.socket
7+
8+
[Service]
9+
ExecStart=/usr/local/bin/finch-daemon --debug --socket-addr fd://
10+
Type=notify
11+
Delegate=yes
12+
Restart=always
13+
RestartSec=5
14+
15+
[Install]
16+
WantedBy=multi-user.target
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[Unit]
2+
Description=Socket for finch daemon
3+
4+
[Socket]
5+
ListenStream=/run/finch.sock
6+
SocketMode=0660
7+
SocketUser=root
8+
SocketGroup=root
9+
10+
[Install]
11+
WantedBy=sockets.target

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ require (
8484
github.com/go-logr/stdr v1.2.2 // indirect
8585
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
8686
github.com/godbus/dbus/v5 v5.1.0 // indirect
87+
github.com/gofrs/flock v0.12.1 // indirect
8788
github.com/gogo/protobuf v1.3.2 // indirect
8889
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
8990
github.com/golang/protobuf v1.5.4 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ github.com/go-task/slim-sprig/v3 v3.0.0/go.mod h1:W848ghGpv3Qj3dhTPRyJypKRiqCdHZ
130130
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
131131
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
132132
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
133+
github.com/gofrs/flock v0.12.1 h1:MTLVXXHf8ekldpJk3AKicLij9MdwOWkZ+a/jHHZby9E=
134+
github.com/gofrs/flock v0.12.1/go.mod h1:9zxTsyu5xtJ9DK+1tFZyibEV7y3uwDxPPfbxeeHCoD0=
133135
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
134136
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
135137
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=

0 commit comments

Comments
 (0)