@@ -659,15 +659,15 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
659
659
// connect to monitor
660
660
conn , err := net .Dial ("unix" , d .monitorPath ())
661
661
if err != nil {
662
- return nil , err
662
+ return nil , errors . Wrap ( err , "connect" )
663
663
}
664
664
defer conn .Close ()
665
665
666
666
// initial QMP response
667
667
var buf [1024 ]byte
668
668
nr , err := conn .Read (buf [:])
669
669
if err != nil {
670
- return nil , err
670
+ return nil , errors . Wrap ( err , "read initial resp" )
671
671
}
672
672
type qmpInitialResponse struct {
673
673
QMP struct {
@@ -684,9 +684,8 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
684
684
}
685
685
686
686
var initialResponse qmpInitialResponse
687
- err = json .Unmarshal (buf [:nr ], & initialResponse )
688
- if err != nil {
689
- return nil , err
687
+ if err := json .Unmarshal (buf [:nr ], & initialResponse ); err != nil {
688
+ return nil , errors .Wrap (err , "unmarshal initial resp" )
690
689
}
691
690
692
691
// run 'qmp_capabilities' to switch to command mode
@@ -696,22 +695,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
696
695
}
697
696
jsonCommand , err := json .Marshal (qmpCommand {Command : "qmp_capabilities" })
698
697
if err != nil {
699
- return nil , err
698
+ return nil , errors . Wrap ( err , "marshal qmp_capabilities" )
700
699
}
701
700
if _ , err := conn .Write (jsonCommand ); err != nil {
702
- return nil , err
701
+ return nil , errors . Wrap ( err , "write qmp_capabilities" )
703
702
}
704
703
nr , err = conn .Read (buf [:])
705
704
if err != nil {
706
- return nil , err
705
+ return nil , errors . Wrap ( err , "read qmp_capabilities resp" )
707
706
}
708
707
type qmpResponse struct {
709
708
Return map [string ]interface {} `json:"return"`
710
709
}
711
710
var response qmpResponse
712
- err = json .Unmarshal (buf [:nr ], & response )
713
- if err != nil {
714
- return nil , err
711
+ if err := json .Unmarshal (buf [:nr ], & response ); err != nil {
712
+ return nil , errors .Wrap (err , "unmarshal qmp_capabilities resp" )
715
713
}
716
714
// expecting empty response
717
715
if len (response .Return ) != 0 {
@@ -721,18 +719,21 @@ func (d *Driver) RunQMPCommand(command string) (map[string]interface{}, error) {
721
719
// { "execute": command }
722
720
jsonCommand , err = json .Marshal (qmpCommand {Command : command })
723
721
if err != nil {
724
- return nil , err
722
+ return nil , errors . Wrap ( err , "marshal command" )
725
723
}
726
724
if _ , err := conn .Write (jsonCommand ); err != nil {
727
- return nil , err
725
+ return nil , errors . Wrap ( err , "write command" )
728
726
}
729
727
nr , err = conn .Read (buf [:])
730
728
if err != nil {
731
- return nil , err
729
+ return nil , errors . Wrap ( err , "read command resp" )
732
730
}
733
- err = json .Unmarshal (buf [:nr ], & response )
734
- if err != nil {
735
- return nil , err
731
+
732
+ // Sometimes QEMU returns two JSON objects with the first object being the command response
733
+ // and the second object being an event log (unimportant)
734
+ firstRespObj := strings .Split (string (buf [:nr ]), "\n " )[0 ]
735
+ if err := json .Unmarshal ([]byte (firstRespObj ), & response ); err != nil {
736
+ return nil , errors .Wrap (err , "unmarshal command resp" )
736
737
}
737
738
if strings .HasPrefix (command , "query-" ) {
738
739
return response .Return , nil
0 commit comments