Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server: Cleanup UNIX Socket file #27886

Merged
merged 5 commits into from
Sep 16, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
}

if s.cfg.Socket != "" {

err := cleanupStaleSocket(s.cfg.Socket)
if err != nil {
return nil, errors.Trace(err)
}

if s.socket, err = net.Listen("unix", s.cfg.Socket); err != nil {
return nil, errors.Trace(err)
}
Expand Down Expand Up @@ -295,6 +301,30 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
return s, nil
}

func cleanupStaleSocket(socket string) error {
sockStat, err := os.Stat(socket)
if err == nil {
if sockStat.Mode().Type() != os.ModeSocket {
return fmt.Errorf(
"the specified socket file %s is a %s instead of a socket file",
socket, sockStat.Mode().String())
}

_, err = net.Dial("unix", socket)
if err != nil {
logutil.BgLogger().Warn("Unix socket exists and is nonfunctional, removing it",
zap.String("socket", socket), zap.Error(err))
err = os.Remove(socket)
if err != nil {
return fmt.Errorf("failed to remove socket file %s", socket)
}
} else {
return fmt.Errorf("unix socket %s exists and is functional, not removing it", socket)
}
}
return nil
}

func setSSLVariable(ca, key, cert string) {
variable.SetSysVar("have_openssl", "YES")
variable.SetSysVar("have_ssl", "YES")
Expand Down