|
1 | 1 | use display_info::DisplayInfo; |
2 | 2 | use lazy_static::lazy_static; |
3 | 3 | use regex::Regex; |
4 | | -use std::{env, fs::read_to_string, process::Command}; |
| 4 | +use std::{env, fs::{read_to_string, read_link, metadata}, process::Command}; |
5 | 5 | use which::which; |
6 | 6 |
|
7 | 7 | lazy_static! { |
@@ -46,6 +46,39 @@ pub fn get_kernel_version() -> String { |
46 | 46 | kernel_result_full[2].to_string() |
47 | 47 | } |
48 | 48 |
|
| 49 | +/// Gets the system init system |
| 50 | +/// # Returns |
| 51 | +/// |
| 52 | +/// Returns a `String` containing the init system name (e.g., "runit-init"). |
| 53 | +/// |
| 54 | +/// # Panics |
| 55 | +/// |
| 56 | +/// A specific panic setting was not added, it could be because it cannot find file if it is panic. |
| 57 | +/// |
| 58 | +/// # Examples |
| 59 | +/// |
| 60 | +/// ```rust |
| 61 | +/// use ffetch::get_init_system(); |
| 62 | +/// let init_system = get_init_system(); |
| 63 | +/// println!("Init: {}", init_system); |
| 64 | +/// // Output: Init: runit-init |
| 65 | +/// ``` |
| 66 | +pub fn get_init_system() -> String { |
| 67 | + let path = read_link("/sbin/init").unwrap(); |
| 68 | + let init : &str = path.to_str().unwrap(); |
| 69 | + |
| 70 | + if metadata("/sbin/openrc").is_ok() { |
| 71 | + return "openrc".to_string(); |
| 72 | + } |
| 73 | + |
| 74 | + match init { |
| 75 | + "runit-init" => "runit".to_string(), |
| 76 | + "/lib/systemd/systemd" => "systemd".to_string(), |
| 77 | + _ => init.to_string(), |
| 78 | + } |
| 79 | + |
| 80 | +} |
| 81 | + |
49 | 82 | /// Gets the system locale from environment variables. |
50 | 83 | /// |
51 | 84 | /// Checks `LC_ALL` first, then `LANG`, and defaults to "C" if neither is set. |
|
0 commit comments