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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! losetup - set up and control loop devices
//!
//! Associate loop devices with regular files.

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

// Loop device ioctl commands
#[cfg(target_os = "linux")]
const LOOP_SET_FD: crate::io::IoctlReq = 0x4C00u32 as crate::io::IoctlReq;
#[cfg(target_os = "linux")]
const LOOP_CLR_FD: crate::io::IoctlReq = 0x4C01u32 as crate::io::IoctlReq;
#[cfg(target_os = "linux")]
const LOOP_SET_STATUS64: crate::io::IoctlReq = 0x4C04u32 as crate::io::IoctlReq;
#[cfg(target_os = "linux")]
const LOOP_GET_STATUS64: crate::io::IoctlReq = 0x4C05u32 as crate::io::IoctlReq;
#[cfg(target_os = "linux")]
const LOOP_CTL_GET_FREE: crate::io::IoctlReq = 0x4C82u32 as crate::io::IoctlReq;

// Loop flags
#[cfg(target_os = "linux")]
const LO_FLAGS_READ_ONLY: u32 = 1;
#[cfg(target_os = "linux")]
const LO_FLAGS_AUTOCLEAR: u32 = 4;
#[cfg(target_os = "linux")]
const LO_FLAGS_PARTSCAN: u32 = 8;

/// Loop device info structure
#[cfg(target_os = "linux")]
#[repr(C)]
struct LoopInfo64 {
    lo_device: u64,
    lo_inode: u64,
    lo_rdevice: u64,
    lo_offset: u64,
    lo_sizelimit: u64,
    lo_number: u32,
    lo_encrypt_type: u32,
    lo_encrypt_key_size: u32,
    lo_flags: u32,
    lo_file_name: [u8; 64],
    lo_crypt_name: [u8; 64],
    lo_encrypt_key: [u8; 32],
    lo_init: [u64; 2],
}

/// losetup - set up and control loop devices
///
/// # Synopsis
/// ```text
/// losetup [-a|-d|-f] [-o OFFSET] [-r] [LOOPDEV] [FILE]
/// ```
///
/// # Description
/// Associate loop devices with regular files, or show/detach loop devices.
///
/// # Options
/// - `-a`: Show all loop devices
/// - `-d LOOPDEV`: Detach loop device
/// - `-f`: Find and show next free loop device
/// - `-o OFFSET`: Start at offset into file
/// - `-r`: Read-only
/// - `-P`: Force kernel to scan for partitions
///
/// # Exit Status
/// - 0: Success
/// - >0: An error occurred
#[cfg(target_os = "linux")]
pub fn losetup(argc: i32, argv: *const *const u8) -> i32 {
    let mut show_all = false;
    let mut detach: Option<&[u8]> = None;
    let mut find_free = false;
    let mut offset: u64 = 0;
    let mut read_only = false;
    let mut partscan = false;
    let mut loopdev: Option<&[u8]> = None;
    let mut file: Option<&[u8]> = None;

    // Parse arguments
    let mut i = 1;
    while i < argc as usize {
        let arg = match unsafe { get_arg(argv, i as i32) } {
            Some(a) => a,
            None => break,
        };

        if arg == b"-a" || arg == b"--all" {
            show_all = true;
        } else if arg == b"-d" || arg == b"--detach" {
            i += 1;
            if i < argc as usize {
                detach = unsafe { get_arg(argv, i as i32) };
            }
        } else if arg == b"-f" || arg == b"--find" {
            find_free = true;
        } else if arg == b"-o" || arg == b"--offset" {
            i += 1;
            if i < argc as usize {
                if let Some(o) = unsafe { get_arg(argv, i as i32) } {
                    offset = sys::parse_u64(o).unwrap_or(0);
                }
            }
        } else if arg == b"-r" || arg == b"--read-only" {
            read_only = true;
        } else if arg == b"-P" || arg == b"--partscan" {
            partscan = true;
        } else if arg == b"-h" || arg == b"--help" {
            print_usage();
            return 0;
        } else if !arg.starts_with(b"-") {
            if loopdev.is_none() {
                loopdev = Some(arg);
            } else if file.is_none() {
                file = Some(arg);
            }
        }
        i += 1;
    }

    // Handle detach
    if let Some(dev) = detach {
        return detach_loop(dev);
    }

    // Handle find free
    if find_free && file.is_none() && loopdev.is_none() {
        return find_free_loop();
    }

    // Handle show all
    if show_all || (loopdev.is_none() && file.is_none() && !find_free) {
        return show_loops();
    }

    // Handle setup: either losetup -f file or losetup loopdev file
    if let Some(f) = file {
        let dev = if find_free {
            None
        } else {
            loopdev
        };
        return setup_loop(dev, f, offset, read_only, partscan);
    } else if let Some(dev) = loopdev {
        // Just show info for this device
        return show_loop_info(dev);
    }

    print_usage();
    1
}

#[cfg(not(target_os = "linux"))]
pub fn losetup(_argc: i32, _argv: *const *const u8) -> i32 {
    io::write_str(2, b"losetup: only available on Linux\n");
    1
}

#[cfg(target_os = "linux")]
fn show_loops() -> i32 {
    // Iterate through /dev/loop* and show configured ones
    for n in 0..256 {
        let mut path = Vec::new();
        path.extend_from_slice(b"/dev/loop");
        let mut buf = [0u8; 16];
        path.extend_from_slice(sys::format_u64(n, &mut buf));
        path.push(0);

        let fd = io::open(&path, libc::O_RDONLY, 0);
        if fd >= 0 {
            let mut info: LoopInfo64 = unsafe { core::mem::zeroed() };
            let ret = unsafe { libc::ioctl(fd, LOOP_GET_STATUS64 as crate::io::IoctlReq, &mut info) };
            io::close(fd);

            if ret >= 0 {
                // Print loop info
                path.pop(); // remove null
                io::write_all(1, &path);
                io::write_str(1, b": [");

                // Device and inode
                io::write_all(1, sys::format_u64(info.lo_device, &mut buf));
                io::write_str(1, b"]:");
                io::write_all(1, sys::format_u64(info.lo_inode, &mut buf));
                io::write_str(1, b" (");

                // Filename
                let name_len = info.lo_file_name.iter().position(|&c| c == 0).unwrap_or(64);
                io::write_all(1, &info.lo_file_name[..name_len]);
                io::write_str(1, b")");

                // Offset if non-zero
                if info.lo_offset > 0 {
                    io::write_str(1, b", offset ");
                    io::write_all(1, sys::format_u64(info.lo_offset, &mut buf));
                }

                // Size limit if set
                if info.lo_sizelimit > 0 {
                    io::write_str(1, b", sizelimit ");
                    io::write_all(1, sys::format_u64(info.lo_sizelimit, &mut buf));
                }

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

#[cfg(target_os = "linux")]
fn show_loop_info(device: &[u8]) -> i32 {
    let mut path = Vec::new();
    path.extend_from_slice(device);
    path.push(0);

    let fd = io::open(&path, libc::O_RDONLY, 0);
    if fd < 0 {
        io::write_str(2, b"losetup: ");
        io::write_all(2, device);
        io::write_str(2, b": cannot open\n");
        return 1;
    }

    let mut info: LoopInfo64 = unsafe { core::mem::zeroed() };
    let ret = unsafe { libc::ioctl(fd, LOOP_GET_STATUS64 as crate::io::IoctlReq, &mut info) };
    io::close(fd);

    if ret < 0 {
        io::write_str(2, b"losetup: ");
        io::write_all(2, device);
        io::write_str(2, b": not configured\n");
        return 1;
    }

    let mut buf = [0u8; 16];
    io::write_all(1, device);
    io::write_str(1, b": [");
    io::write_all(1, sys::format_u64(info.lo_device, &mut buf));
    io::write_str(1, b"]:");
    io::write_all(1, sys::format_u64(info.lo_inode, &mut buf));
    io::write_str(1, b" (");
    let name_len = info.lo_file_name.iter().position(|&c| c == 0).unwrap_or(64);
    io::write_all(1, &info.lo_file_name[..name_len]);
    io::write_str(1, b")\n");

    0
}

#[cfg(target_os = "linux")]
fn find_free_loop() -> i32 {
    let fd = io::open(b"/dev/loop-control", libc::O_RDWR, 0);
    if fd < 0 {
        io::write_str(2, b"losetup: cannot open /dev/loop-control\n");
        return 1;
    }

    let num = unsafe { libc::ioctl(fd, LOOP_CTL_GET_FREE as crate::io::IoctlReq) };
    io::close(fd);

    if num < 0 {
        io::write_str(2, b"losetup: cannot find free loop device\n");
        return 1;
    }

    io::write_str(1, b"/dev/loop");
    let mut buf = [0u8; 16];
    io::write_all(1, sys::format_u64(num as u64, &mut buf));
    io::write_str(1, b"\n");

    0
}

#[cfg(target_os = "linux")]
fn setup_loop(device: Option<&[u8]>, file: &[u8], offset: u64, read_only: bool, partscan: bool) -> i32 {
    // Get device path
    let dev_path = if let Some(d) = device {
        let mut p = Vec::new();
        p.extend_from_slice(d);
        p.push(0);
        p
    } else {
        // Find free device
        let ctl_fd = io::open(b"/dev/loop-control", libc::O_RDWR, 0);
        if ctl_fd < 0 {
            io::write_str(2, b"losetup: cannot open /dev/loop-control\n");
            return 1;
        }

        let num = unsafe { libc::ioctl(ctl_fd, LOOP_CTL_GET_FREE as crate::io::IoctlReq) };
        io::close(ctl_fd);

        if num < 0 {
            io::write_str(2, b"losetup: cannot find free loop device\n");
            return 1;
        }

        let mut p = Vec::new();
        p.extend_from_slice(b"/dev/loop");
        let mut buf = [0u8; 16];
        p.extend_from_slice(sys::format_u64(num as u64, &mut buf));
        p.push(0);
        p
    };

    // Open the backing file
    let file_flags = if read_only { libc::O_RDONLY } else { libc::O_RDWR };
    let mut file_path = Vec::new();
    file_path.extend_from_slice(file);
    file_path.push(0);

    let file_fd = io::open(&file_path, file_flags, 0);
    if file_fd < 0 {
        io::write_str(2, b"losetup: cannot open ");
        io::write_all(2, file);
        io::write_str(2, b"\n");
        return 1;
    }

    // Open the loop device
    let loop_flags = if read_only { libc::O_RDONLY } else { libc::O_RDWR };
    let loop_fd = io::open(&dev_path, loop_flags, 0);
    if loop_fd < 0 {
        io::write_str(2, b"losetup: cannot open loop device\n");
        io::close(file_fd);
        return 1;
    }

    // Associate the loop device with the file
    let ret = unsafe { libc::ioctl(loop_fd, LOOP_SET_FD as crate::io::IoctlReq, file_fd) };
    if ret < 0 {
        io::write_str(2, b"losetup: failed to set up loop device\n");
        io::close(file_fd);
        io::close(loop_fd);
        return 1;
    }

    // Set up loop info if needed
    if offset > 0 || read_only || partscan {
        let mut info: LoopInfo64 = unsafe { core::mem::zeroed() };

        // Get current status first
        unsafe { libc::ioctl(loop_fd, LOOP_GET_STATUS64 as crate::io::IoctlReq, &mut info) };

        info.lo_offset = offset;
        if read_only {
            info.lo_flags |= LO_FLAGS_READ_ONLY;
        }
        if partscan {
            info.lo_flags |= LO_FLAGS_PARTSCAN;
        }

        // Copy filename
        let len = core::cmp::min(file.len(), 63);
        info.lo_file_name[..len].copy_from_slice(&file[..len]);

        let ret = unsafe { libc::ioctl(loop_fd, LOOP_SET_STATUS64 as crate::io::IoctlReq, &info) };
        if ret < 0 {
            io::write_str(2, b"losetup: warning: failed to set loop status\n");
        }
    }

    io::close(file_fd);
    io::close(loop_fd);

    // Print the device name if we auto-selected it
    if device.is_none() {
        dev_path[..dev_path.len() - 1].iter().for_each(|&c| {
            io::write_all(1, &[c]);
        });
        io::write_str(1, b"\n");
    }

    0
}

#[cfg(target_os = "linux")]
fn detach_loop(device: &[u8]) -> i32 {
    let mut path = Vec::new();
    path.extend_from_slice(device);
    path.push(0);

    let fd = io::open(&path, libc::O_RDONLY, 0);
    if fd < 0 {
        io::write_str(2, b"losetup: ");
        io::write_all(2, device);
        io::write_str(2, b": cannot open\n");
        return 1;
    }

    let ret = unsafe { libc::ioctl(fd, LOOP_CLR_FD as crate::io::IoctlReq) };
    io::close(fd);

    if ret < 0 {
        io::write_str(2, b"losetup: ");
        io::write_all(2, device);
        io::write_str(2, b": detach failed\n");
        return 1;
    }

    0
}

fn print_usage() {
    io::write_str(1, b"Usage: losetup [OPTIONS] [[LOOPDEV] FILE]\n\n");
    io::write_str(1, b"Set up and control loop devices.\n\n");
    io::write_str(1, b"Options:\n");
    io::write_str(1, b"  -a, --all           Show all loop devices\n");
    io::write_str(1, b"  -d, --detach DEV    Detach loop device\n");
    io::write_str(1, b"  -f, --find          Find and show next free device\n");
    io::write_str(1, b"  -o, --offset N      Start at offset N into file\n");
    io::write_str(1, b"  -r, --read-only     Set up read-only loop\n");
    io::write_str(1, b"  -P, --partscan      Scan for partitions\n");
    io::write_str(1, b"  -h, --help          Show this help\n");
}

#[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_losetup_help() {
        let armybox = get_armybox_path();
        if !armybox.exists() { return; }

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

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

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

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

        // Should succeed even if no loops configured
        assert_eq!(output.status.code(), Some(0));
    }
}