clean-up sockfile on exit

This commit is contained in:
andrzej 2024-11-04 16:46:56 +01:00
parent a4b75bb90d
commit c46ab056f5
2 changed files with 14 additions and 2 deletions

12
main.go
View File

@ -8,7 +8,13 @@ import (
var config Config
const sockfile = "/tmp/gopaper.sock"
func main() {
//TODO:kill existing processes
log.SetFlags(log.LstdFlags | log.Lshortfile)
err := config.load()
if err != nil {
log.Println("failed to load config", err)
@ -20,6 +26,7 @@ func main() {
var waitGroup sync.WaitGroup
waitGroup.Add(1)
slideshowDir := make(chan string)
go server(slideshowDir)
go slideshow(slideshowDir)
@ -34,3 +41,8 @@ func main() {
waitGroup.Wait()
}
func cleanExit(code int) {
os.Remove(sockfile)
os.Exit(code)
}

View File

@ -11,7 +11,8 @@ import (
func server(slideshowDir chan string) {
sockfile := "/tmp/bg-go.sock"
defer os.Remove(sockfile)
//Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", sockfile)
if err != nil {
@ -27,7 +28,6 @@ func server(slideshowDir chan string) {
os.Remove(sockfile)
os.Exit(1)
}()
defer os.Remove(sockfile)
for {
//Accept incoming