diff --git a/internal/api/unix_socket.go b/internal/api/unix_socket.go index cae7f8d0..51d0e331 100644 --- a/internal/api/unix_socket.go +++ b/internal/api/unix_socket.go @@ -5,6 +5,8 @@ import ( "fmt" "net" "os" + "os/signal" + "syscall" "github.com/edulinq/autograder/internal/api/core" "github.com/edulinq/autograder/internal/common" @@ -15,26 +17,21 @@ import ( func startExclusiveUnixServer() error { var socketPath = config.UNIX_SOCKET_PATH.Get() - os.Remove(socketPath) - fmt.Println("socketPath: ", socketPath) + unixSocket, err := net.Listen("unix", socketPath) if err != nil { log.Fatal("Failed to listen on a Unix socket.", err) } defer os.Remove(socketPath) - // sigc := make(chan os.Signal, 1) - // signal.Notify(sigc, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP) - // go func(c chan os.Signal) { - // // Wait for a SIGINT or SIGKILL: - // sig := <-c - // fmt.Println("Caught signal %s: shutting down.", sig) - // // Stop listening (and unlink the socket if unix type): - // unixSocket.Close() - // os.Remove(socketPath) - // // And we're done: - // os.Exit(0) - // }(sigc) + defer unixSocket.Close() + c := make(chan os.Signal, 1) + signal.Notify(c, os.Interrupt, syscall.SIGTERM) + go func() { + <-c + os.Remove(socketPath) + unixSocket.Close() + }() log.Info("Unix Server Started", log.NewAttr("unix_socket", socketPath)) diff --git a/internal/config/options.go b/internal/config/options.go index 9b56cf0f..df0adbc8 100644 --- a/internal/config/options.go +++ b/internal/config/options.go @@ -43,8 +43,8 @@ var ( // Server WEB_PORT = MustNewIntOption("web.port", 8080, "The port for the web interface to serve on.") WEB_MAX_FILE_SIZE_KB = MustNewIntOption("web.maxsizekb", 2*1024, "The maximum allowed file size (in KB) submitted via POST request. The default is 2048 KB (2 MB).") - UNIX_SOCKET_PATH = MustNewStringOption("unix.socket", GetUnixSocketPath(), "The socket for CMD requests to serve on.") - PID_PATH = MustNewStringOption("pid.path", GetPidPath(), "The pid file to ensure only one instance of a server runs at a time.") + UNIX_SOCKET_PATH = MustNewStringOption("unix.socket", "/tmp/autograder.sock", "The socket for CMD requests to serve on.") + PID_PATH = MustNewStringOption("pid.path", "/tmp/autograder.pid", "The pid file to ensure only one instance of a server runs at a time.") BUFFER_SIZE = MustNewIntOption("buffer.size", 8, "The length of the byte slice for the unix socket to read the size of the request.") BIT_SIZE = MustNewIntOption("bit.size", 64, "The number of bits to generate a random hexidecimal string.") diff --git a/internal/procedures/server/start.go b/internal/procedures/server/start.go index 808fb94f..71ad2231 100644 --- a/internal/procedures/server/start.go +++ b/internal/procedures/server/start.go @@ -46,7 +46,6 @@ func Start() error { defer os.Remove(pidFilePath) - // Remove the unix socket file when the program terminates abruptly. c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt, syscall.SIGTERM) go func() {