Skip to content

Commit

Permalink
fix: Pidfile handling and socket docs
Browse files Browse the repository at this point in the history
Signed-off-by: Shubhranshu153 <[email protected]>
  • Loading branch information
Shubhranshu153 committed Nov 19, 2024
1 parent 9cc615a commit 7b20b3b
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
45 changes: 27 additions & 18 deletions cmd/finch-daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,32 +104,41 @@ func getListener(options *DaemonOptions) (net.Listener, error) {
return listener, nil
}

func handlePidFileOptions(options *DaemonOptions) error {
if options.pidFile != "" {
if err := os.MkdirAll(filepath.Dir(options.pidFile), 0o640); err != nil {
return fmt.Errorf("failed to create pidfile directory %s", err)
}
if err := pidfile.Write(options.pidFile, os.Getpid()); err != nil {
return fmt.Errorf("failed to start daemon, ensure finch daemon is not running or delete %s %w", options.pidFile, err)
}
}
return nil
}

func run(options *DaemonOptions) error {
// This sets the log level of the dependencies that use logrus (e.g., containerd library).
if options.debug {
logrus.SetLevel(logrus.DebugLevel)
}

defer func() {
if err := os.Remove(options.pidFile); err != nil {
logrus.Errorf("failed to remove pidfile %s", options.pidFile)
if options.pidFile != "" {
if err := os.MkdirAll(filepath.Dir(options.pidFile), 0o600); err != nil {
return fmt.Errorf("failed to create pidfile directory %s", err)
}
}()

if err := handlePidFileOptions(options); err != nil {
return err
pidFile, err := os.OpenFile(options.pidFile, os.O_RDWR|os.O_CREATE, 0o600)
if err != nil {
return fmt.Errorf("failed to open or create PID file: %w", err)
}

// Lock the PID file to prevent concurrent access
if err := syscall.Flock(int(pidFile.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
pidFile.Close()
return fmt.Errorf("failed to acquire lock on PID file; ensure only one instance is running")
}

if err := pidfile.Write(options.pidFile, os.Getpid()); err != nil {
return fmt.Errorf("failed to start daemon, ensure finch daemon is not running or delete %s %w", options.pidFile, err)
}

syscall.Flock(int(pidFile.Fd()), syscall.LOCK_UN)
pidFile.Close()

// 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
defer func() {
if err := os.Remove(options.pidFile); err != nil {
logrus.Errorf("failed to remove pidfile %s", options.pidFile)
}
}()
}

logger := flog.NewLogrus()
Expand Down
16 changes: 16 additions & 0 deletions docs/sample-service-files/finch-socket-activation.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[Unit]
Description=finch daemon
Documentation=https://runfinch.com
After=network.target local-fs.target containerd.service
Wants=network.target containerd.service
Requires=finch.socket

[Service]
ExecStart=/usr/local/bin/finch-daemon --debug --socket-addr fd://
Type=notify
Delegate=yes
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target
11 changes: 11 additions & 0 deletions docs/sample-service-files/finch-socket-activation.socket
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[Unit]
Description=Socket for finch daemon

[Socket]
ListenStream=/run/finch.sock
SocketMode=0660
SocketUser=root
SocketGroup=root

[Install]
WantedBy=sockets.target

0 comments on commit 7b20b3b

Please sign in to comment.