armybox 0.3.0

A memory-safe #[no_std] BusyBox/Toybox clone in Rust - 299 Unix utilities in ~500KB
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
//! rfkill - tool for enabling and disabling wireless devices
//!
//! Query and change the state of rfkill switches.

extern crate alloc;

use alloc::vec::Vec;
use crate::io;
use crate::sys;
use crate::applets::get_arg;

/// rfkill - tool for enabling and disabling wireless devices
///
/// # Synopsis
/// ```text
/// rfkill [list|block|unblock] [type|id]
/// ```
///
/// # Description
/// Tool for enabling and disabling wireless devices.
///
/// # Commands
/// - list: List the current state of rfkill switches
/// - block [type|id]: Disable a type of wireless device or specific device
/// - unblock [type|id]: Enable a type of wireless device or specific device
///
/// # Types
/// - wifi, wlan: Wireless LAN
/// - bluetooth: Bluetooth
/// - uwb: Ultra-Wideband
/// - wimax: Worldwide Interoperability for Microwave Access
/// - wwan: Wireless Wide Area Network
/// - gps: GPS
/// - fm: FM
/// - nfc: Near Field Communication
/// - all: All devices
///
/// # Exit Status
/// - 0: Success
/// - 1: Error
pub fn rfkill(argc: i32, argv: *const *const u8) -> i32 {
    if argc < 2 {
        // Default to list
        return rfkill_list();
    }

    let cmd = match unsafe { get_arg(argv, 1) } {
        Some(c) => c,
        None => return rfkill_list(),
    };

    if cmd == b"list" {
        rfkill_list()
    } else if cmd == b"block" {
        if argc < 3 {
            io::write_str(2, b"rfkill: missing type/id\n");
            return 1;
        }
        let type_arg = unsafe { get_arg(argv, 2).unwrap() };
        rfkill_set(type_arg, true)
    } else if cmd == b"unblock" {
        if argc < 3 {
            io::write_str(2, b"rfkill: missing type/id\n");
            return 1;
        }
        let type_arg = unsafe { get_arg(argv, 2).unwrap() };
        rfkill_set(type_arg, false)
    } else if cmd == b"-h" || cmd == b"--help" {
        print_help();
        0
    } else {
        io::write_str(2, b"rfkill: unknown command: ");
        io::write_all(2, cmd);
        io::write_str(2, b"\n");
        1
    }
}

fn print_help() {
    io::write_str(1, b"Usage: rfkill [command] [type|id]\n\n");
    io::write_str(1, b"Commands:\n");
    io::write_str(1, b"  list           List all devices\n");
    io::write_str(1, b"  block TYPE     Block devices of TYPE\n");
    io::write_str(1, b"  unblock TYPE   Unblock devices of TYPE\n\n");
    io::write_str(1, b"Types: wifi, bluetooth, wwan, gps, nfc, all\n");
}

fn rfkill_list() -> i32 {
    // Read from /sys/class/rfkill
    let dir = io::opendir(b"/sys/class/rfkill");
    if dir.is_null() {
        io::write_str(1, b"No rfkill devices found\n");
        return 0;
    }

    io::write_str(1, b"ID TYPE      DEVICE           SOFT      HARD\n");

    loop {
        let entry = io::readdir(dir);
        if entry.is_null() {
            break;
        }

        let d_name = unsafe { &(*entry).d_name };
        let name_len = d_name.iter().position(|&c| c == 0).unwrap_or(d_name.len());
        let name = &d_name[..name_len];
        let name_u8: &[u8] = unsafe { core::mem::transmute(name) };

        // Skip . and ..
        if name_u8 == b"." || name_u8 == b".." {
            continue;
        }

        // Build base path
        let mut base_path = [0u8; 256];
        let mut pi = 0;
        for &c in b"/sys/class/rfkill/" {
            base_path[pi] = c;
            pi += 1;
        }
        for &c in name_u8 {
            base_path[pi] = c;
            pi += 1;
        }
        let base_len = pi;

        // Read type
        base_path[base_len] = b'/';
        for (i, &c) in b"type".iter().enumerate() {
            base_path[base_len + 1 + i] = c;
        }
        let type_str = read_sysfs_string(&base_path[..base_len + 5]);

        // Read name/device
        for (i, &c) in b"name".iter().enumerate() {
            base_path[base_len + 1 + i] = c;
        }
        let device_str = read_sysfs_string(&base_path[..base_len + 5]);

        // Read soft block state
        for (i, &c) in b"soft".iter().enumerate() {
            base_path[base_len + 1 + i] = c;
        }
        let soft_str = read_sysfs_string(&base_path[..base_len + 5]);
        let soft_blocked = soft_str.first() == Some(&b'1');

        // Read hard block state
        for (i, &c) in b"hard".iter().enumerate() {
            base_path[base_len + 1 + i] = c;
        }
        let hard_str = read_sysfs_string(&base_path[..base_len + 5]);
        let hard_blocked = hard_str.first() == Some(&b'1');

        // Print entry: ID TYPE DEVICE SOFT HARD
        // Extract ID number from name (e.g., "rfkill0" -> "0")
        let id = if name_u8.starts_with(b"rfkill") {
            &name_u8[6..]
        } else {
            name_u8
        };

        // ID (right-aligned in 2 chars)
        if id.len() < 2 {
            io::write_str(1, b" ");
        }
        io::write_all(1, id);
        io::write_str(1, b" ");

        // Type (left-aligned, 9 chars)
        io::write_all(1, &type_str);
        for _ in type_str.len()..10 {
            io::write_str(1, b" ");
        }

        // Device name (left-aligned, 16 chars)
        let dev_name = if device_str.is_empty() { name_u8 } else { &device_str };
        io::write_all(1, dev_name);
        for _ in dev_name.len()..17 {
            io::write_str(1, b" ");
        }

        // Soft block state
        let soft_state: &[u8] = if soft_blocked { b"blocked" } else { b"unblocked" };
        io::write_all(1, soft_state);
        for _ in soft_state.len()..10 {
            io::write_str(1, b" ");
        }

        // Hard block state
        let hard_state: &[u8] = if hard_blocked { b"blocked" } else { b"unblocked" };
        io::write_all(1, hard_state);

        io::write_str(1, b"\n");
    }

    io::closedir(dir);
    0
}

/// Read a sysfs string file (strips trailing newline)
fn read_sysfs_string(path: &[u8]) -> Vec<u8> {
    let fd = io::open(path, libc::O_RDONLY, 0);
    if fd < 0 {
        return Vec::new();
    }

    let mut buf = [0u8; 64];
    let n = io::read(fd, &mut buf);
    io::close(fd);

    if n <= 0 {
        return Vec::new();
    }

    let len = if n > 0 && buf[n as usize - 1] == b'\n' {
        n as usize - 1
    } else {
        n as usize
    };

    buf[..len].to_vec()
}

fn rfkill_set(type_arg: &[u8], block: bool) -> i32 {
    // Normalize type name
    let target_type: Option<&[u8]> = match type_arg {
        b"wifi" | b"wlan" => Some(b"wlan"),
        b"bluetooth" => Some(b"bluetooth"),
        b"wwan" => Some(b"wwan"),
        b"gps" => Some(b"gps"),
        b"nfc" => Some(b"nfc"),
        b"uwb" => Some(b"uwb"),
        b"wimax" => Some(b"wimax"),
        b"fm" => Some(b"fm"),
        b"all" => None, // Match all devices
        _ => {
            // Could be a numeric ID, try that
            if sys::parse_u64(type_arg).is_some() {
                // Treat as device ID
                return set_device_by_id(type_arg, block);
            }
            io::write_str(2, b"rfkill: unknown type: ");
            io::write_all(2, type_arg);
            io::write_str(2, b"\n");
            return 1;
        }
    };

    // Iterate over all rfkill devices
    let dir = io::opendir(b"/sys/class/rfkill");
    if dir.is_null() {
        io::write_str(2, b"rfkill: cannot access /sys/class/rfkill\n");
        return 1;
    }

    let mut found = false;
    let value = if block { b"1" } else { b"0" };

    loop {
        let entry = io::readdir(dir);
        if entry.is_null() {
            break;
        }

        let d_name = unsafe { &(*entry).d_name };
        let name_len = d_name.iter().position(|&c| c == 0).unwrap_or(d_name.len());
        let name = &d_name[..name_len];
        let name_u8: &[u8] = unsafe { core::mem::transmute(name) };

        if name_u8 == b"." || name_u8 == b".." {
            continue;
        }

        // Build path to type file
        let mut type_path = [0u8; 256];
        let mut pi = 0;
        for &c in b"/sys/class/rfkill/" {
            type_path[pi] = c;
            pi += 1;
        }
        for &c in name_u8 {
            type_path[pi] = c;
            pi += 1;
        }
        let base_len = pi;
        type_path[base_len] = b'/';
        for (i, &c) in b"type".iter().enumerate() {
            type_path[base_len + 1 + i] = c;
        }

        // Read device type
        let device_type = read_sysfs_string(&type_path[..base_len + 5]);

        // Check if we should operate on this device
        let should_set = match target_type {
            Some(t) => device_type == t,
            None => true, // "all" matches everything
        };

        if should_set {
            // Write to soft file
            for (i, &c) in b"soft".iter().enumerate() {
                type_path[base_len + 1 + i] = c;
            }

            let fd = io::open(&type_path[..base_len + 5], libc::O_WRONLY, 0);
            if fd >= 0 {
                io::write_all(fd, value);
                io::close(fd);
                found = true;
            }
        }
    }

    io::closedir(dir);

    if !found && target_type.is_some() {
        io::write_str(2, b"rfkill: no devices of type ");
        io::write_all(2, type_arg);
        io::write_str(2, b" found\n");
        return 1;
    }

    0
}

fn set_device_by_id(id: &[u8], block: bool) -> i32 {
    let mut path = [0u8; 256];
    let mut pi = 0;

    for &c in b"/sys/class/rfkill/rfkill" {
        path[pi] = c;
        pi += 1;
    }
    for &c in id {
        path[pi] = c;
        pi += 1;
    }
    for &c in b"/soft" {
        path[pi] = c;
        pi += 1;
    }

    let fd = io::open(&path[..pi], libc::O_WRONLY, 0);
    if fd < 0 {
        io::write_str(2, b"rfkill: device ");
        io::write_all(2, id);
        io::write_str(2, b" not found\n");
        return 1;
    }

    let value = if block { b"1" } else { b"0" };
    io::write_all(fd, value);
    io::close(fd);

    0
}

#[cfg(test)]
mod tests {
    extern crate std;
    use std::process::Command;
    use std::path::PathBuf;

    fn get_armybox_path() -> PathBuf {
        if let Ok(path) = std::env::var("ARMYBOX_PATH") {
            return PathBuf::from(path);
        }
        let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
            .map(PathBuf::from)
            .unwrap_or_else(|_| std::env::current_dir().unwrap());
        let release = manifest_dir.join("target/release/armybox");
        if release.exists() { return release; }
        manifest_dir.join("target/debug/armybox")
    }

    #[test]
    fn test_rfkill_list() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["rfkill", "list"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
        let stdout = std::string::String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("TYPE") || stdout.contains("No rfkill"));
    }

    #[test]
    fn test_rfkill_no_args() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["rfkill"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
    }

    #[test]
    fn test_rfkill_help() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

        let output = Command::new(&armybox)
            .args(["rfkill", "--help"])
            .output()
            .unwrap();

        assert_eq!(output.status.code(), Some(0));
        let stdout = std::string::String::from_utf8_lossy(&output.stdout);
        assert!(stdout.contains("Usage"));
    }
}