diff --git a/qemu/qmp.go b/qemu/qmp.go index 1e57aaa9e0..fccc0f56ae 100644 --- a/qemu/qmp.go +++ b/qemu/qmp.go @@ -963,3 +963,26 @@ func (q *QMP) ExecuteGetFD(ctx context.Context, fdname string, fd *os.File) erro _, err := q.executeCommandWithResponse(ctx, "getfd", args, oob, nil) return err } + +// ExecuteCharDevUnixSocketAdd adds a character device using as backend a unix socket, +// id is an identifier for the device, path specifies the local path of the unix socket, +// wait is to block waiting for a client to connect, server specifies that the socket is a listening socket. +func (q *QMP) ExecuteCharDevUnixSocketAdd(ctx context.Context, id, path string, wait, server bool) error { + args := map[string]interface{}{ + "id": id, + "backend": map[string]interface{}{ + "type": "socket", + "data": map[string]interface{}{ + "wait": wait, + "server": server, + "addr": map[string]interface{}{ + "type": "unix", + "data": map[string]interface{}{ + "path": path, + }, + }, + }, + }, + } + return q.executeCommand(ctx, "chardev-add", args, nil) +} diff --git a/qemu/qmp_test.go b/qemu/qmp_test.go index 0ffdf5bc32..2f60db1abd 100644 --- a/qemu/qmp_test.go +++ b/qemu/qmp_test.go @@ -1040,3 +1040,20 @@ func TestExecuteGetFdD(t *testing.T) { q.Shutdown() <-disconnectedCh } + +// Checks chardev-add unix socket +func TestExecuteCharDevUnixSocketAdd(t *testing.T) { + connectedCh := make(chan *QMPVersion) + disconnectedCh := make(chan struct{}) + buf := newQMPTestCommandBuffer(t) + buf.AddCommand("chardev-add", nil, "return", nil) + cfg := QMPConfig{Logger: qmpTestLogger{}} + q := startQMPLoop(buf, cfg, connectedCh, disconnectedCh) + checkVersion(t, connectedCh) + err := q.ExecuteCharDevUnixSocketAdd(context.Background(), "foo", "foo.sock", false, true) + if err != nil { + t.Fatalf("Unexpected error %v", err) + } + q.Shutdown() + <-disconnectedCh +}