Skip to content

Commit

Permalink
Add a Close method to DBusClient and use it in GNMI server
Browse files Browse the repository at this point in the history
Add a Close method to DBusClient and use it in GNMI server
  • Loading branch information
hdwhdw authored Jan 31, 2025
2 parents a023991 + 0c6099f commit 34b97ce
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 1 deletion.
7 changes: 7 additions & 0 deletions gnmi_server/gnoi.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func ReadFileStat(path string) (*gnoi_file_pb.StatInfo, error) {
if err != nil {
return nil, err
}
defer sc.Close()

log.V(2).Infof("Reading file stat at path %s...", path)
data, err := sc.GetFileStat(path)
Expand Down Expand Up @@ -100,6 +101,8 @@ func KillOrRestartProcess(restart bool, serviceName string) error {
if err != nil {
return err
}
defer sc.Close()

if restart {
log.V(2).Infof("Restarting service %s...", serviceName)
err = sc.RestartService(serviceName)
Expand Down Expand Up @@ -129,6 +132,7 @@ func (srv *OSServer) Verify(ctx context.Context, req *gnoi_os_pb.VerifyRequest)
log.V(2).Infof("Failed to create dbus client: %v", err)
return nil, err
}
defer dbus.Close()

image_json, err := dbus.ListImages()
if err != nil {
Expand Down Expand Up @@ -186,6 +190,7 @@ func HaltSystem() error {
if err != nil {
return err
}
defer sc.Close()

log.V(2).Infof("Halting the system..")
err = sc.HaltSystem()
Expand All @@ -201,6 +206,8 @@ func RebootSystem(fileName string) error {
if err != nil {
return err
}
defer sc.Close()

err = sc.ConfigReload(fileName)
return err
}
Expand Down
16 changes: 15 additions & 1 deletion sonic_service_client/dbus_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import (
)

type Service interface {
// Close the connection to the D-Bus
Close() error

// SONiC Host Service D-Bus API
ConfigReload(fileName string) error
ConfigSave(fileName string) error
ApplyPatchYang(fileName string) error
Expand All @@ -34,9 +38,10 @@ type DbusClient struct {
}

func NewDbusClient() (Service, error) {
log.Infof("DbusClient: NewDbusClient")

var client DbusClient
var err error

client.busNamePrefix = "org.SONiC.HostService."
client.busPathPrefix = "/org/SONiC/HostService/"
client.intNamePrefix = "org.SONiC.HostService."
Expand All @@ -45,6 +50,15 @@ func NewDbusClient() (Service, error) {
return &client, err
}

// Close the connection to the D-Bus.
func (c *DbusClient) Close() error {
log.Infof("DbusClient: Close")
if c.channel != nil {
close(c.channel)
}
return nil
}

func DbusApi(busName string, busPath string, intName string, timeout int, args ...interface{}) (interface{}, error) {
common_utils.IncCounter(common_utils.DBUS)
conn, err := dbus.SystemBus()
Expand Down
40 changes: 40 additions & 0 deletions sonic_service_client/dbus_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,46 @@ import (
"github.com/godbus/dbus/v5"
)

func TestNewDbusClient(t *testing.T) {
client, err := NewDbusClient()
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
if client == nil {
t.Errorf("NewDbusClient failed: %v", client)
}
}

func TestCloseClient(t *testing.T) {
client, err := NewDbusClient()
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
err = client.Close()
if err != nil {
t.Errorf("Close should pass: %v", err)
}
}

func TestCloseClientWithChannel(t *testing.T) {
client, err := NewDbusClient()
if err != nil {
t.Errorf("NewDbusClient failed: %v", err)
}
client.(*DbusClient).channel = make(chan struct{})
err = client.Close()
if err != nil {
t.Errorf("Close should pass: %v", err)
}

select {
case <-client.(*DbusClient).channel:
// Channel is closed
default:
t.Errorf("Channel should be closed")
}
}

func TestSystemBusNegative(t *testing.T) {
client, err := NewDbusClient()
if err != nil {
Expand Down

0 comments on commit 34b97ce

Please sign in to comment.