view src/main.rs @ 5:ef1358b993e1

mmm.....
author AnaTofuZ <anatofuz@gmail.com>
date Tue, 27 Oct 2020 17:14:39 +0900
parents 668a91cd7be8
children 08247e54d78d
line wrap: on
line source

use nix;
use clap::{Arg, App, Clap};
use std::process::Command;

#[derive(Clap)]
struct Opts {
    #[clap(subcommand)]
    subcmd: SubCommand,
}

#[derive(Clap)]
enum SubCommand {
    define(Define),
    shutdown(Shutdown),
}

#[derive(Clap)]
struct Define {
    name: String,
}

#[derive(Clap)]
struct Shutdown {
    name: String,
}


struct VM {
    id: u32,
    name: String,
    is_vm_running: bool,
}


fn main() {
    let uid = getuid();
    let gid = getgid();
    let user_name = getlogin(uid);
    println!("uid: {} gid: {} name: {}", uid, gid, user_name);

    let matches = App::new("ie-virsh")
            .version("1.0")
            .author("AnatofuZ <anatofuz@cr.ie.u-ryukyu.ac.jp>")
            .about("virsh wrapper for ie members")
            .get_matches();

            /*
    let opts: Opts = Opts::parse();

    match opts.subcmd {
        SubCommand::define(name) => {
        },
        SubCommand::shutdown(name) => {

        }
    }
    */

    //set_root_id();
    //list_command(user_name);
}

fn getlogin(uid: u32) -> &'static str {
    use std::ffi::CStr;
    let user_passwd = unsafe { nix::libc::getpwuid(uid) };
    let c_str = unsafe { CStr::from_ptr((*user_passwd).pw_name) };
    return c_str.to_str().unwrap();
}

fn getuid() -> u32 {
    let uid_struct = nix::unistd::getuid();
    return uid_struct.into();
}

fn getgid() -> u32 {
    let gid_struct = nix::unistd::getgid();
    return gid_struct.into();
}

fn list_command(user_name: &'static str) {
    let output = Command::new("virsh")
        .arg("list")
        .arg("--all")
        .output()
        .expect("failed to virsh");
    let virsh_list = String::from_utf8_lossy(&output.stdout);
    let mut virsh_list = virsh_list.split("\n");
    let info_msg = virsh_list.next().unwrap();
    let border_line = virsh_list.next().unwrap();

    let virsh_emit_vm_list = virsh_list.filter(|&x| x.contains(user_name));

    let mut vm_list : Vec<VM> = Vec::new();

    println!("{}\n{}", info_msg, border_line);
    for emit_vm in virsh_emit_vm_list {
        let mut vm_infos = emit_vm.split(" ");
        let vm_status = vm_infos.next().unwrap();
        let is_vm_running = if vm_status != "-" { true } else { false };
        let name = vm_infos.next().unwrap();
        let  vm = VM{ id: 0, is_vm_running: is_vm_running, name: String::from(name)};
        vm_list.push(vm);
    }
}


fn set_root_id() {
    let id = 0;
    match nix::unistd::seteuid(nix::unistd::Uid::from_raw(id)) {
        Err(err) => panic!("failed seteuid {}", err),
        Ok(_) => {}
    }
    match nix::unistd::setegid(nix::unistd::Gid::from_raw(id)) {
        Err(err) => panic!("failed setedid{}", err),
        Ok(_) => {}
    }

    match nix::unistd::setuid(nix::unistd::Uid::from_raw(id)) {
        Err(err) => panic!("failed setuid {}", err),
        Ok(_) => {}
    }

    match nix::unistd::setgid(nix::unistd::Gid::from_raw(id)) {
        Err(err) => panic!("failed setdid{}", err),
        Ok(_) => {}
    }
}


fn create_new_vm(user_name: &'static str, vm_name: &'static str, debug_kernel:bool) {

}