-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ctlsock: delete colliding orphaned socket file
Detect and delete an orphaned socket file that collides with the ctlsock we want to create. Fixes #776
- Loading branch information
Showing
2 changed files
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters