Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
// Copyright 2021 Red Hat, Inc.
// SPDX-License-Identifier: Apache-2.0

use libc::c_char;

#[link(name = "krun")]
extern "C" {
pub fn krun_set_log_level(level: u32) -> i32;
pub fn krun_create_ctx() -> i32;
pub fn krun_free_ctx(ctx: u32) -> i32;
pub fn krun_set_vm_config(ctx: u32, num_vcpus: u8, ram_mib: u32) -> i32;
pub fn krun_set_root(ctx: u32, root_path: *const c_char) -> i32;
pub fn krun_set_mapped_volumes(ctx: u32, mapped_volumes: *const *const c_char) -> i32;
pub fn krun_set_port_map(ctx: u32, port_map: *const *const c_char) -> i32;
pub fn krun_set_workdir(ctx: u32, workdir_path: *const c_char) -> i32;
pub fn krun_set_root(ctx: u32, root_path: *const i8) -> i32;
pub fn krun_set_mapped_volumes(ctx: u32, mapped_volumes: *const *const i8) -> i32;
pub fn krun_set_port_map(ctx: u32, port_map: *const *const i8) -> i32;
pub fn krun_set_workdir(ctx: u32, workdir_path: *const i8) -> i32;
pub fn krun_set_exec(
ctx: u32,
exec_path: *const c_char,
argv: *const *const c_char,
envp: *const *const c_char,
exec_path: *const i8,
argv: *const *const i8,
envp: *const *const i8,
) -> i32;
pub fn krun_set_env(ctx: u32, envp: *const *const c_char) -> i32;
pub fn krun_set_env(ctx: u32, envp: *const *const i8) -> i32;
pub fn krun_start_enter(ctx: u32) -> i32;
}
21 changes: 15 additions & 6 deletions src/start.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
}

let c_rootfs = CString::new(rootfs).unwrap();
let ret = bindings::krun_set_root(ctx, c_rootfs.as_ptr());
let ret = bindings::krun_set_root(ctx, c_rootfs.as_ptr() as *const i8);
if ret < 0 {
println!("Error setting VM rootfs");
std::process::exit(-1);
Expand All @@ -93,7 +93,7 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C
}
let mut ps: Vec<*const i8> = Vec::new();
for port in ports.iter() {
ps.push(port.as_ptr());
ps.push(port.as_ptr() as *const i8);
}
ps.push(std::ptr::null());
let ret = bindings::krun_set_port_map(ctx, ps.as_ptr());
Expand All @@ -104,7 +104,7 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C

if !vmcfg.workdir.is_empty() {
let c_workdir = CString::new(vmcfg.workdir.clone()).unwrap();
let ret = bindings::krun_set_workdir(ctx, c_workdir.as_ptr());
let ret = bindings::krun_set_workdir(ctx, c_workdir.as_ptr() as *const i8);
if ret < 0 {
println!("Error setting VM workdir");
std::process::exit(-1);
Expand All @@ -113,17 +113,26 @@ unsafe fn exec_vm(vmcfg: &VmConfig, rootfs: &str, cmd: Option<&str>, args: Vec<C

let hostname = CString::new(format!("HOSTNAME={}", vmcfg.name)).unwrap();
let home = CString::new("HOME=/root").unwrap();
let env: [*const i8; 3] = [hostname.as_ptr(), home.as_ptr(), std::ptr::null()];
let env: [*const i8; 3] = [
hostname.as_ptr() as *const i8,
home.as_ptr() as *const i8,
std::ptr::null(),
];

if let Some(cmd) = cmd {
let mut argv: Vec<*const i8> = Vec::new();
for a in args.iter() {
argv.push(a.as_ptr());
argv.push(a.as_ptr() as *const i8);
}
argv.push(std::ptr::null());

let c_cmd = CString::new(cmd).unwrap();
let ret = bindings::krun_set_exec(ctx, c_cmd.as_ptr(), argv.as_ptr(), env.as_ptr());
let ret = bindings::krun_set_exec(
ctx,
c_cmd.as_ptr() as *const i8,
argv.as_ptr(),
env.as_ptr(),
);
if ret < 0 {
println!("Error setting VM config");
std::process::exit(-1);
Expand Down