aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorJakob Unterwurzacher2024-09-03 16:00:24 +0200
committerJakob Unterwurzacher2024-09-03 16:01:46 +0200
commit2c01b1ab3774ace723b2700fc539dda5f0350633 (patch)
tree1a6eb88a9559ab2062db379c48e9cbd61f9c812e /internal
parent40abf968eddd54b6c03ac064d6e42cc6f1083146 (diff)
ctlsock: delete colliding orphaned socket file
Detect and delete an orphaned socket file that collides with the ctlsock we want to create. Fixes https://github.com/rfjakob/gocryptfs/issues/776
Diffstat (limited to 'internal')
-rw-r--r--internal/ctlsocksrv/ctlsock_listen.go35
1 files changed, 35 insertions, 0 deletions
diff --git a/internal/ctlsocksrv/ctlsock_listen.go b/internal/ctlsocksrv/ctlsock_listen.go
index 1db9cbc..94ffca4 100644
--- a/internal/ctlsocksrv/ctlsock_listen.go
+++ b/internal/ctlsocksrv/ctlsock_listen.go
@@ -1,9 +1,44 @@
package ctlsocksrv
import (
+ "errors"
+ "io/fs"
"net"
+ "os"
+ "syscall"
+ "time"
+
+ "github.com/rfjakob/gocryptfs/v2/internal/tlog"
)
+// cleanupOrphanedSocket deletes an orphaned socket file at `path`.
+// The file at `path` will only be deleted if:
+// 1) It is a socket file
+// 2) Connecting to it results in ECONNREFUSED
+func cleanupOrphanedSocket(path string) {
+ fi, err := os.Stat(path)
+ if err != nil {
+ return
+ }
+ if fi.Mode().Type() != fs.ModeSocket {
+ return
+ }
+ conn, err := net.DialTimeout("unix", path, time.Second)
+ if err == nil {
+ // This socket file is still active. Don't delete it.
+ conn.Close()
+ return
+ }
+ if errors.Is(err, syscall.ECONNREFUSED) {
+ tlog.Info.Printf("ctlsock: deleting orphaned socket file %q\n", path)
+ err = os.Remove(path)
+ if err != nil {
+ tlog.Warn.Printf("ctlsock: deleting socket file failed: %v", path)
+ }
+ }
+}
+
func Listen(path string) (net.Listener, error) {
+ cleanupOrphanedSocket(path)
return net.Listen("unix", path)
}