This commit is contained in:
Ilya Zlobintsev 2020-10-17 07:38:28 +03:00
commit c482961c49
10 changed files with 251 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

101
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,101 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in library 'daemon'",
"cargo": {
"args": [
"test",
"--no-run",
"--lib",
"--package=daemon"
],
"filter": {
"name": "daemon",
"kind": "lib"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'daemon'",
"cargo": {
"args": [
"build",
"--bin=daemon",
"--package=daemon"
],
"filter": {
"name": "daemon",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'daemon'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=daemon",
"--package=daemon"
],
"filter": {
"name": "daemon",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug executable 'cli'",
"cargo": {
"args": [
"build",
"--bin=cli",
"--package=cli"
],
"filter": {
"name": "cli",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
},
{
"type": "lldb",
"request": "launch",
"name": "Debug unit tests in executable 'cli'",
"cargo": {
"args": [
"test",
"--no-run",
"--bin=cli",
"--package=cli"
],
"filter": {
"name": "cli",
"kind": "bin"
}
},
"args": [],
"cwd": "${workspaceFolder}"
}
]
}

2
Cargo.toml Normal file
View File

@ -0,0 +1,2 @@
[workspace]
members = ["daemon", "cli"]

11
cli/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "cli"
version = "0.1.0"
authors = ["Ilya Zlobintsev <ilya.zl@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
daemon = { path = "../daemon" }
structopt = "0.3"

15
cli/src/main.rs Normal file
View File

@ -0,0 +1,15 @@
use structopt::StructOpt;
#[derive(StructOpt)]
#[structopt(rename_all = "lower")]
enum Opt {
///Get information about the GPU.
Info,
}
fn main() {
let opt = Opt::from_args();
match opt {
Opt::Info => daemon::get_info(),
}
}

11
daemon/Cargo.toml Normal file
View File

@ -0,0 +1,11 @@
[package]
name = "daemon"
version = "0.1.0"
authors = ["Ilya Zlobintsev <ilya.zl@protonmail.com>"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
bincode = "1.3"
serde = { version = "1.0", features = ["derive"] }

67
daemon/src/daemon.rs Normal file
View File

@ -0,0 +1,67 @@
use std::os::unix::net::{UnixStream, UnixListener};
use std::io::{Read, Write};
use std::process::Command;
use std::thread;
use std::fs;
use serde::{Serialize, Deserialize};
use crate::SOCK_PATH;
use crate::fan_controller::FanController;
#[derive(Clone)]
pub struct Daemon {
fan_controller: FanController,
}
#[derive(Serialize, Deserialize, Debug)]
pub enum Action {
GetInfo,
GetStats,
}
impl Daemon {
pub fn new(fan_controller: FanController) -> Daemon {
Daemon {
fan_controller,
}
}
pub fn run(self) {
if fs::metadata(SOCK_PATH).is_ok() {
fs::remove_file(SOCK_PATH).expect("Failed to take control over socket");
}
let listener = UnixListener::bind(SOCK_PATH).unwrap();
Command::new("chmod").arg("666").arg(SOCK_PATH).output().expect("Failed to chmod");
for stream in listener.incoming() {
match stream {
Ok(stream) => {
thread::spawn(|| Daemon::handle_connection(stream));
}
Err(err) => {
println!("Error: {}", err);
break;
}
}
}
}
fn handle_connection(mut stream: UnixStream) {
let mut buffer = Vec::<u8>::new();
stream.read_to_end(&mut buffer).unwrap();
println!("finished reading");
let action: Action = bincode::deserialize(&buffer).unwrap();
let response = match action {
Action::GetInfo => "gpu information",
Action::GetStats => {
"gpu stats"
},
};
stream.write_all(response.as_bytes()).unwrap();
}
}

View File

@ -0,0 +1,10 @@
#[derive(Clone)]
pub struct FanController {
hwmon_path: String,
}
impl FanController {
pub fn new(hwmon_path: &str) -> FanController {
FanController { hwmon_path: hwmon_path.to_string() }
}
}

24
daemon/src/lib.rs Normal file
View File

@ -0,0 +1,24 @@
use std::os::unix::net::UnixStream;
use std::io::{Read, Write};
pub mod daemon;
pub mod fan_controller;
pub const SOCK_PATH: &str = "/tmp/amdgpu-configurator.sock";
pub fn get_info() {
let mut stream = UnixStream::connect(SOCK_PATH).expect("Failed to connect to daemon");
stream.write_all(&bincode::serialize(&daemon::Action::GetInfo).unwrap()).unwrap();
stream.shutdown(std::net::Shutdown::Write).expect("Could not shut down");
let mut response = String::new();
stream.read_to_string(&mut response).unwrap();
println!("{}", response);
}
pub struct DaemonConnection {
}
impl DaemonConnection {
}

9
daemon/src/main.rs Normal file
View File

@ -0,0 +1,9 @@
use daemon::daemon::Daemon;
use daemon::fan_controller::FanController;
fn main() {
let fan_controller = FanController::new("afsadfasdfa");
let d = Daemon::new(fan_controller);
d.run();
}