summary refs log tree commit diff
path: root/src/wl.rs
blob: 333be871f31c0637c266a59a5aee772e82e44668 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// SPDX-License-Identifier: BSD-3-Clause

use devices::virtio::{
    BincodeRequest, BincodeResponse, InterruptProxy, InterruptProxyEvent, MsgOnSocketRequest,
    MsgOnSocketResponse, VirtioDevice, Wl,
};
use msg_socket::MsgSocket;
use poly_msg_socket::PolyMsgSocket;
use std::collections::BTreeMap;
use std::fs::remove_file;
use sys_util::{error, net::UnixSeqpacketListener, warn, GuestMemory};

#[cfg(any(target_arch = "arm", target_arch = "aarch64"))]
pub use aarch64::{arch_memory_regions, MemoryParams};
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
pub use x86_64::{arch_memory_regions, MemoryParams};

type Socket =
    PolyMsgSocket<MsgOnSocketResponse, MsgOnSocketRequest, BincodeResponse, BincodeRequest>;

fn main() {
    eprintln!("hello world");

    // Create and display the socket.
    let mut path = std::env::var("XDG_RUNTIME_DIR").expect("XDG_RUNTIME_DIR missing");
    path.push_str("/crosvm-wl.sock");
    let _ = remove_file(&path);
    let server = UnixSeqpacketListener::bind(&path).expect("failed to create control socket");
    println!("{}", path);

    // Receive connection from crosvm.
    let conn = server.accept().expect("accept failed");
    let msg_socket: Socket = PolyMsgSocket::new(conn);

    let vm_socket = match msg_socket.recv() {
        Ok(poly_msg_socket::Value::MsgOnSocket(MsgOnSocketRequest::Create { vm_socket })) => {
            MsgSocket::new(vm_socket.owned())
        }

        Ok(msg) => {
            panic!("received unexpected message: {:?}", msg);
        }

        Err(e) => {
            panic!("recv error: {}", e);
        }
    };

    let mut wayland_paths = BTreeMap::new();
    wayland_paths.insert("".into(), "/run/user/1000/wayland-0".into());

    let mut wl = Some(Wl::new(wayland_paths, vm_socket, None).unwrap());

    loop {
        match msg_socket.recv() {
            Ok(poly_msg_socket::Value::MsgOnSocket(MsgOnSocketRequest::Kill)) => {
                if let Some(wl) = wl.take() {
                    // Will block until worker shuts down.
                    drop(wl);

                    if let Err(e) = msg_socket.send(MsgOnSocketResponse::Kill) {
                        error!("failed to send Response::Kill: {}", e);
                        break;
                    }
                } else {
                    warn!("received Kill but no Wl is running");
                }
            }

            Ok(poly_msg_socket::Value::MsgOnSocket(MsgOnSocketRequest::Activate {
                shm,
                interrupt,
                interrupt_resample_evt,
                in_queue,
                out_queue,
                in_queue_evt,
                out_queue_evt,
            })) => {
                let shm = shm.owned();

                let regions = arch_memory_regions(MemoryParams {
                    size: shm.size(),
                    has_bios: false,
                });
                let mem =
                    GuestMemory::with_memfd(&regions, shm).expect("GuestMemory::with_memfd failed");

                let interrupt: MsgSocket<InterruptProxyEvent, ()> =
                    MsgSocket::new(interrupt.owned());

                wl.as_mut().unwrap().activate(
                    mem,
                    Box::new(InterruptProxy::new(
                        interrupt,
                        interrupt_resample_evt.owned(),
                    )),
                    vec![in_queue, out_queue],
                    vec![in_queue_evt.owned(), out_queue_evt.owned()],
                );

                println!("activated Wl");
            }

            Ok(msg) => panic!("unexpected message {:?}", msg),

            Err(e) => panic!("recv failed: {}", e),
        }
    }
}