Skip to content

Commit

Permalink
pillar/handlentp: move cleanup procedures close to the error path
Browse files Browse the repository at this point in the history
Make error path of the `dialUnixWithChronyd()` cleaner by
moving socket cleanup directly into the function.

Also remove socket in case of `chmod()` error.

Signed-off-by: Roman Penyaev <r.peniaev@gmail.com>
  • Loading branch information
rouming committed Jul 4, 2024
1 parent c66d1bd commit 6c55cc7
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions pkg/pillar/cmd/zedagent/handlentp.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,10 +247,14 @@ func dialUnixWithChronyd(address string) (*chronyConn, error) {
&net.UnixAddr{Name: address, Net: "unixgram"},
)
if err != nil {
// Even there was an error, net.DialUnix() leaves trash behind.
// What a shame.
os.Remove(local)
return nil, err
}
if err := os.Chmod(local, 0600); err != nil {
conn.Close()
os.Remove(local)
return nil, err
}
return &chronyConn{Conn: conn, local: local}, nil
Expand All @@ -261,9 +265,6 @@ func dialUnixWithChronyd(address string) (*chronyConn, error) {
func getNTPSourcesInfo(ctx *zedagentContext) *info.ZInfoNTPSources {
conn, err := dialUnixWithChronyd(unixChronydPath)
if err != nil {
// Even there was an error, net.DialUnix() leaves trash behind.
// What a shame.
os.Remove(conn.local)
log.Errorf("getNTPSourcesInfo: can't connect to chronyd: %v", err)
return nil
}
Expand Down

0 comments on commit 6c55cc7

Please sign in to comment.