use std::os::raw::c_char;
#[cfg(test)]
mod find_lib;
pub const KRUN_LOG_TARGET_DEFAULT: i32 = -1;
pub const KRUN_LOG_TARGET_STDOUT: i32 = 1;
pub const KRUN_LOG_TARGET_STDERR: i32 = 2;
pub const KRUN_LOG_LEVEL_OFF: u32 = 0;
pub const KRUN_LOG_LEVEL_ERROR: u32 = 1;
pub const KRUN_LOG_LEVEL_WARN: u32 = 2;
pub const KRUN_LOG_LEVEL_INFO: u32 = 3;
pub const KRUN_LOG_LEVEL_DEBUG: u32 = 4;
pub const KRUN_LOG_LEVEL_TRACE: u32 = 5;
pub const KRUN_LOG_STYLE_AUTO: u32 = 0;
pub const KRUN_LOG_STYLE_ALWAYS: u32 = 1;
pub const KRUN_LOG_STYLE_NEVER: u32 = 2;
pub const KRUN_LOG_OPTION_NO_ENV: u32 = 1;
pub const KRUN_DISK_FORMAT_RAW: u32 = 0;
pub const KRUN_DISK_FORMAT_QCOW2: u32 = 1;
pub const KRUN_KERNEL_FORMAT_RAW: u32 = 0;
pub const KRUN_KERNEL_FORMAT_ELF: u32 = 1;
pub const KRUN_KERNEL_FORMAT_PE_GZ: u32 = 2;
pub const KRUN_KERNEL_FORMAT_IMAGE_BZ2: u32 = 3;
pub const KRUN_KERNEL_FORMAT_IMAGE_GZ: u32 = 4;
pub const KRUN_KERNEL_FORMAT_IMAGE_ZSTD: u32 = 5;
pub const KRUN_TSI_HIJACK_INET: u32 = 1 << 0;
pub const KRUN_TSI_HIJACK_UNIX: u32 = 1 << 1;
extern "C" {
pub fn krun_init_log(target: i32, level: u32, style: u32, flags: u32) -> i32;
pub fn krun_set_log_level(level: u32) -> i32;
pub fn krun_create_ctx() -> i32;
pub fn krun_free_ctx(ctx_id: u32) -> i32;
pub fn krun_set_vm_config(ctx_id: u32, num_vcpus: u8, ram_mib: u32) -> i32;
pub fn krun_set_root(ctx_id: u32, root_path: *const c_char) -> i32;
pub fn krun_add_virtiofs(
ctx_id: u32,
mount_tag: *const c_char,
host_path: *const c_char,
) -> i32;
pub fn krun_set_kernel(
ctx_id: u32,
kernel_path: *const c_char,
kernel_format: u32,
initramfs: *const c_char,
cmdline: *const c_char,
) -> i32;
pub fn krun_set_exec(
ctx_id: u32,
exec_path: *const c_char,
argv: *const *const c_char,
envp: *const *const c_char,
) -> i32;
pub fn krun_set_env(ctx_id: u32, envp: *const *const c_char) -> i32;
pub fn krun_set_workdir(ctx_id: u32, workdir_path: *const c_char) -> i32;
pub fn krun_split_irqchip(ctx_id: u32, enable: bool) -> i32;
pub fn krun_set_nested_virt(ctx_id: u32, enabled: bool) -> i32;
pub fn krun_set_gpu_options(ctx_id: u32, virgl_flags: u32) -> i32;
pub fn krun_set_rlimits(ctx_id: u32, rlimits: *const *const c_char) -> i32;
pub fn krun_set_port_map(ctx_id: u32, port_map: *const *const c_char) -> i32;
pub fn krun_add_disk(
ctx_id: u32,
block_id: *const c_char,
disk_path: *const c_char,
read_only: bool,
) -> i32;
pub fn krun_start_enter(ctx_id: u32) -> i32;
pub fn krun_set_console_output(ctx_id: u32, filepath: *const c_char) -> i32;
pub fn krun_disable_implicit_console(ctx_id: u32) -> i32;
pub fn krun_disable_implicit_vsock(ctx_id: u32) -> i32;
pub fn krun_set_kernel_console(ctx_id: u32, console_id: *const c_char) -> i32;
pub fn krun_add_serial_console_default(
ctx_id: u32,
input_fd: std::os::raw::c_int,
output_fd: std::os::raw::c_int,
) -> i32;
pub fn krun_add_virtio_console_default(
ctx_id: u32,
input_fd: std::os::raw::c_int,
output_fd: std::os::raw::c_int,
err_fd: std::os::raw::c_int,
) -> i32;
pub fn krun_add_virtio_console_multiport(ctx_id: u32) -> i32;
pub fn krun_add_console_port_tty(
ctx_id: u32,
console_id: u32,
name: *const c_char,
tty_fd: std::os::raw::c_int,
) -> i32;
pub fn krun_add_console_port_inout(
ctx_id: u32,
console_id: u32,
name: *const c_char,
input_fd: std::os::raw::c_int,
output_fd: std::os::raw::c_int,
) -> i32;
pub fn krun_add_vsock(ctx_id: u32, tsi_features: u32) -> i32;
pub fn krun_get_shutdown_eventfd(ctx_id: u32) -> i32;
}
#[cfg(not(target_os = "windows"))]
extern "C" {
pub fn krun_setuid(ctx_id: u32, uid: libc::uid_t) -> i32;
pub fn krun_setgid(ctx_id: u32, gid: libc::gid_t) -> i32;
}
#[cfg(target_os = "windows")]
extern "C" {
pub fn krun_setuid(ctx_id: u32, uid: u32) -> i32;
pub fn krun_setgid(ctx_id: u32, gid: u32) -> i32;
}
#[cfg(not(target_os = "windows"))]
extern "C" {
pub fn krun_add_vsock_port2(
ctx_id: u32,
port: u32,
filepath: *const c_char,
listen: bool,
) -> i32;
pub fn krun_add_disk2(
ctx_id: u32,
block_id: *const c_char,
disk_path: *const c_char,
disk_format: u32,
read_only: bool,
) -> i32;
pub fn krun_add_net_unixstream(
ctx_id: u32,
c_path: *const c_char,
fd: i32,
c_mac: *const u8,
features: u32,
flags: u32,
) -> i32;
pub fn krun_add_net_unixgram(
ctx_id: u32,
c_path: *const c_char,
fd: i32,
c_mac: *const u8,
features: u32,
flags: u32,
) -> i32;
pub fn krun_set_root_disk_remount(
ctx_id: u32,
device: *const c_char,
fstype: *const c_char,
options: *const c_char,
) -> i32;
}
#[cfg(target_os = "windows")]
extern "C" {
pub fn krun_add_net_tcp(
ctx_id: u32,
c_iface_id: *const c_char,
c_mac: *const u8,
c_tcp_addr: *const c_char,
) -> i32;
pub fn krun_add_vsock_port_windows(ctx_id: u32, port: u32, c_pipe_name: *const c_char) -> i32;
}
#[cfg(target_os = "linux")]
pub unsafe fn krun_set_tee_config_file(ctx_id: u32, filepath: *const c_char) -> i32 {
type Func = unsafe extern "C" fn(u32, *const c_char) -> i32;
static FUNC: std::sync::OnceLock<Option<Func>> = std::sync::OnceLock::new();
let func = FUNC.get_or_init(|| {
let sym = b"krun_set_tee_config_file\0";
let ptr = libc::dlsym(libc::RTLD_DEFAULT, sym.as_ptr() as *const _);
if ptr.is_null() {
None
} else {
Some(std::mem::transmute::<*mut libc::c_void, Func>(ptr))
}
});
match func {
Some(f) => f(ctx_id, filepath),
None => -libc::ENOSYS,
}
}