summary refs log tree commit diff
path: root/src/wl.rs
blob: b35af8b12550a68ee68513def4e94235b34d6c4d (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
// SPDX-License-Identifier: BSD-3-Clause

use devices::virtio::{InterruptProxy, InterruptProxyEvent, SingleFd, Worker};
use msg_socket::{MsgReceiver, MsgSocket};
use std::collections::BTreeMap;
use std::fs::remove_file;
use sys_util::{net::UnixSeqpacketListener, GuestMemory};
use vm_control::VmMemoryControlRequestSocket;

#[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 = MsgSocket<(), SingleFd>;

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 = MsgSocket::new(conn);

    loop {
        match msg_socket.recv() {
            Ok(SingleFd {
                shm,
                interrupt,
                interrupt_resample_evt,
                in_queue,
                out_queue,
                vm_socket,
                use_transition_flags,
                in_queue_evt,
                out_queue_evt,
                kill_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());
                let vm_socket: VmMemoryControlRequestSocket = MsgSocket::new(vm_socket.owned());

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

                let mut worker = Worker::new(
                    mem,
                    Box::new(InterruptProxy::new(
                        interrupt,
                        interrupt_resample_evt.owned(),
                    )),
                    in_queue,
                    out_queue,
                    wayland_paths,
                    vm_socket,
                    use_transition_flags,
                    None,
                );

                worker.run(
                    vec![in_queue_evt.owned(), out_queue_evt.owned()],
                    kill_evt.owned(),
                );
            }

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