Repository: firecracker-microvm/firecracker
File: src/jailer/src/env.rs (Rust)

Code before:
        );

        // Trying to create again the same device node is not allowed.
        assert_eq!(
            format!(
                "{}",
                env.mknod_and_own_dev(dev_path, major, minor).unwrap_err()
            ),
            format!(
                "Failed to create {} via mknod inside the jail: File exists (os error 17)",
                dev_path.to_str().unwrap()
            )
        );
    }

    #[test]
    fn test_mknod_and_own_dev() {
        let mut mock_cgroups = MockCgroupFs::new().unwrap();
        mock_cgroups.add_v1_mounts().unwrap();
        let env = create_env(mock_cgroups.proc_mounts_path.as_str());

        let mock_dev_dir_name = format!(
            "/tmp/firecracker/test/{}/jailer/dev",
            rand::rand_alphanumerics(4).into_string().unwrap()
        );
        let mock_dev_dir = Path::new(mock_dev_dir_name.as_str());

        // Ensure device nodes are created with correct major/minor numbers and permissions.
        let mut dev_infos: Vec<(PathBuf, u32, u32)> = vec![
            (
                mock_dev_dir.join("net/tun-test"),
                DEV_NET_TUN_MAJOR,
                DEV_NET_TUN_MINOR,
            ),
            (mock_dev_dir.join("kvm-test"), DEV_KVM_MAJOR, DEV_KVM_MINOR),
        ];

        if let Some(uffd_dev_minor) = env.uffd_dev_minor {
            dev_infos.push((
                mock_dev_dir.join("userfaultfd-test"),
                DEV_UFFD_MAJOR,
                uffd_dev_minor,
            ));
        }

        for (dev, major, minor) in dev_infos {
            // Ensure the folder where we are creating the node exists
            fs::create_dir_all(dev.parent().unwrap()).unwrap();
            let dev_path = dev.to_str().map(CString::new).unwrap().unwrap();
            ensure_mknod_and_own_dev(&env, &dev_path, major, minor);
        }

Review comment (suggestion):
`vmm_sys_util::TempDir` should do exactly this for you + handle the cleanup

Code after:
            libc::S_IFCHR | libc::S_IRUSR | libc::S_IWUSR
        );

        // Trying to create again the same device node is not allowed.
        assert_eq!(
            format!(
                "{}",
                env.mknod_and_own_dev(dev_path, major, minor).unwrap_err()
            ),
            format!(
                "Failed to create {} via mknod inside the jail: File exists (os error 17)",
                dev_path.to_str().unwrap()
            )
        );
    }

    #[test]
    fn test_mknod_and_own_dev() {
        let mut mock_cgroups = MockCgroupFs::new().unwrap();
        mock_cgroups.add_v1_mounts().unwrap();
        let env = create_env(&mock_cgroups.proc_mounts_path);

        let mock_dev_dir = TempDir::new().unwrap();

        // Ensure device nodes are created with correct major/minor numbers and permissions.
        let mut dev_infos: Vec<(PathBuf, u32, u32)> = vec![
            (
                mock_dev_dir.as_path().join("net/tun-test"),
                DEV_NET_TUN_MAJOR,
                DEV_NET_TUN_MINOR,
            ),
            (
                mock_dev_dir.as_path().join("kvm-test"),
                DEV_KVM_MAJOR,
                DEV_KVM_MINOR,
            ),
        ];

        if let Some(uffd_dev_minor) = env.uffd_dev_minor {
            dev_infos.push((
                mock_dev_dir.as_path().join("userfaultfd-test"),
                DEV_UFFD_MAJOR,
                uffd_dev_minor,
            ));
        }

        for (dev, major, minor) in dev_infos {
            // Ensure the folder where we are creating the node exists
            fs::create_dir_all(dev.parent().unwrap()).unwrap();
            let dev_path = dev.to_str().map(CString::new).unwrap().unwrap();
            ensure_mknod_and_own_dev(&env, &dev_path, major, minor);