Skip to content

Commit

Permalink
Finish port to rust
Browse files Browse the repository at this point in the history
  • Loading branch information
aljen committed Oct 16, 2024
1 parent a723fd0 commit 95e054f
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 566 deletions.
21 changes: 18 additions & 3 deletions rinit-protos/protos/rinit.proto
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ syntax = "proto2";

package rinit.api;

message Error { required string message = 1; }
message Error {
required uint32 code = 1;
required string message = 2;
}

message Env {
required bytes name = 1;
Expand All @@ -23,6 +26,11 @@ message Rfd {
};
}

enum IfKind {
IFKIND_VPN = 0;
IFKIND_INET = 1;
}

message Request {
required uint64 request_id = 1;
oneof command {
Expand Down Expand Up @@ -52,6 +60,7 @@ message Response {
SyncFsResponse sync_fs = 9;
NetCtlResponse net_ctl = 10;
NetHostResponse net_host = 11;
ProcessDiedNotification process_died = 12;

Error error = 99;
}
Expand All @@ -70,7 +79,7 @@ message RunProcessRequest {
optional bool is_entrypoint = 8;
}

message KillProcessRequest { required uint64 pid = 1; }
message KillProcessRequest { required uint64 process_id = 1; }

message MountVolumeRequest {
required bytes tag = 1;
Expand Down Expand Up @@ -98,7 +107,7 @@ message NetCtlRequest {
required bytes mask = 2;
required bytes gateway = 3;
required bytes if_addr = 4;
required uint32 if_kind = 5;
required IfKind if_kind = 5;
required uint32 flags = 6;
}

Expand Down Expand Up @@ -126,3 +135,9 @@ message SyncFsResponse {}
message NetCtlResponse {}

message NetHostResponse {}

message ProcessDiedNotification {
required uint64 pid = 1;
required uint32 exit_status = 2;
required uint32 reason_type = 3;
}
50 changes: 50 additions & 0 deletions rinit/src/fs.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::{
fs::File,
io::{BufRead, BufReader},
os::unix::fs::{MetadataExt, PermissionsExt},
path::{Path, PathBuf},
};
Expand Down Expand Up @@ -344,3 +346,51 @@ pub fn mount_sysroot() -> std::io::Result<()> {

Ok(())
}

pub fn find_device_major(name: &str) -> std::io::Result<i32> {
let file = File::open("/proc/devices")?;
let reader = BufReader::new(file);

let mut major = -1;
let mut in_character_devices = false;

for line in reader.lines() {
let line = line?;
if line == "Character devices:" {
in_character_devices = true;
} else if line.is_empty() || line == "Block devices:" {
if in_character_devices {
break;
}
} else if in_character_devices {
let parts: Vec<&str> = line.split_whitespace().collect();
if parts.len() == 2 {
if let (Ok(entry_major), entry_name) = (parts[0].parse::<i32>(), parts[1]) {
if entry_name == name {
major = entry_major;
break;
}
}
}
}
}

Ok(major)
}

pub fn nvidia_gpu_count() -> i32 {
let mut counter = 0;

for i in 0..256 {
let path = format!("/sys/class/drm/card{}", i);
let path = Path::new(&path);

if !path.exists() {
break;
}

counter = i + 1;
}

counter
}
Loading

0 comments on commit 95e054f

Please sign in to comment.