pub struct Library { /* private fields */ }
Expand description
A loaded wslapi.dll
or api-ms-win-wsl-api-l1-1-0.dll
instance
Implementations§
Source§impl Library
impl Library
Sourcepub fn new() -> Result<Self>
pub fn new() -> Result<Self>
Attempt to load wslapi.dll
Examples found in repository?
5fn main() {
6 let ubuntu = "Ubuntu";
7 let nonexistant = "Nonexistant";
8
9 let wsl = Library::new().unwrap();
10 assert!( wsl.is_distribution_registered(ubuntu));
11 assert!(!wsl.is_distribution_registered(nonexistant));
12 assert!(wsl.get_distribution_configuration(nonexistant).is_err());
13
14 let cfg = wsl.get_distribution_configuration(ubuntu).unwrap();
15 assert!(cfg.default_uid == 0 || (1000 ..= 2000).contains(&cfg.default_uid)); // Root or regular UID
16 assert!(cfg.flags & WSL_DISTRIBUTION_FLAGS::DEFAULT == WSL_DISTRIBUTION_FLAGS::DEFAULT);
17 assert!((1..=2).contains(&cfg.version)); // WSL version
18
19 println!("Environment varibales:");
20 for (k,v) in cfg.default_environment_variables.iter() {
21 // XXX: This might be inaccurate / depend on the linux locale? Good enough for a demo though!
22 let k = String::from_utf8_lossy(k);
23 let v = String::from_utf8_lossy(v);
24 println!("{: <10} = {}", k, v);
25 }
26
27 wsl.launch_interactive(ubuntu, "echo testing 123", true).unwrap();
28
29 let stdin = "echo testing 456\necho PATH: ${PATH}\nasdf";
30 let stdout = std::fs::File::create("target/basic.txt").unwrap();
31 let stderr = std::fs::File::create("CON").unwrap();
32 wsl.launch(ubuntu, "sh", true, stdin, stdout, stderr).unwrap().wait().unwrap();
33
34 println!("\rPress ENTER to quit");
35 let _ = std::io::stdin().read_line(&mut String::new());
36}
Sourcepub fn is_distribution_registered(
&self,
distribution_name: impl AsRef<OsStr>,
) -> bool
pub fn is_distribution_registered( &self, distribution_name: impl AsRef<OsStr>, ) -> bool
Determines if a distribution is registered with the Windows Subsystem for Linux (WSL).
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).
§Returns
true
if the supplied distribution is currently registeredfalse
otherwise.
§See Also
- WslIsDistributionRegistered - the underlying API
Examples found in repository?
5fn main() {
6 let ubuntu = "Ubuntu";
7 let nonexistant = "Nonexistant";
8
9 let wsl = Library::new().unwrap();
10 assert!( wsl.is_distribution_registered(ubuntu));
11 assert!(!wsl.is_distribution_registered(nonexistant));
12 assert!(wsl.get_distribution_configuration(nonexistant).is_err());
13
14 let cfg = wsl.get_distribution_configuration(ubuntu).unwrap();
15 assert!(cfg.default_uid == 0 || (1000 ..= 2000).contains(&cfg.default_uid)); // Root or regular UID
16 assert!(cfg.flags & WSL_DISTRIBUTION_FLAGS::DEFAULT == WSL_DISTRIBUTION_FLAGS::DEFAULT);
17 assert!((1..=2).contains(&cfg.version)); // WSL version
18
19 println!("Environment varibales:");
20 for (k,v) in cfg.default_environment_variables.iter() {
21 // XXX: This might be inaccurate / depend on the linux locale? Good enough for a demo though!
22 let k = String::from_utf8_lossy(k);
23 let v = String::from_utf8_lossy(v);
24 println!("{: <10} = {}", k, v);
25 }
26
27 wsl.launch_interactive(ubuntu, "echo testing 123", true).unwrap();
28
29 let stdin = "echo testing 456\necho PATH: ${PATH}\nasdf";
30 let stdout = std::fs::File::create("target/basic.txt").unwrap();
31 let stderr = std::fs::File::create("CON").unwrap();
32 wsl.launch(ubuntu, "sh", true, stdin, stdout, stderr).unwrap().wait().unwrap();
33
34 println!("\rPress ENTER to quit");
35 let _ = std::io::stdin().read_line(&mut String::new());
36}
Sourcepub fn register_distribution(
&self,
distribution_name: impl AsRef<OsStr>,
tar_gz_filename: impl AsRef<Path>,
) -> Result<()>
pub fn register_distribution( &self, distribution_name: impl AsRef<OsStr>, tar_gz_filename: impl AsRef<Path>, ) -> Result<()>
Registers a new distribution with the Windows Subsystem for Linux (WSL).
Consider using wsl --import <Distro> <InstalLocation> <FileName>
instead:
The directory containing the executable will be registered as the BasePath
for rootfs
/ temp
to be placed in.
This odd design choice stems from WslRegisterDistribution itself! Wasn’t that a bad choice as far back as Windows XP?
This also limits you to a single registration per executable!
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).tar_gz_filename
- Full path to a .tar.gz file containing the file system of the distribution to register.
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
already existedErr(Error)
- iftar_gz_filename
contained'\0'
charactersErr(Error)
- iftar_gz_filename
wasn’t an absolute path?Err(Error)
- iftar_gz_filename
wasn’t a valid pathErr(Error)
- if the executable’s directory already contains a registered distributionErr(Error)
- if the executable’s directory wasn’t writable?Err(Error)
- if WslRegisterDistribution otherwise failedOk(())
- otherwise
§See Also
- WslRegisterDistribution - the underlying API
Sourcepub fn unregister_distribution(
&self,
distribution_name: impl AsRef<OsStr>,
) -> Result<()>
pub fn unregister_distribution( &self, distribution_name: impl AsRef<OsStr>, ) -> Result<()>
Unregisters a distribution from the Windows Subsystem for Linux (WSL).
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
didn’t exist?Err(Error)
- if WslUnregisterDistribution failedOk(())
- otherwise
§See Also
- WslUnregisterDistribution - the underlying API
Sourcepub fn configure_distribution(
&self,
distribution_name: impl AsRef<OsStr>,
default_uid: ULONG,
wsl_distribution_flags: WSL_DISTRIBUTION_FLAGS,
) -> Result<()>
pub fn configure_distribution( &self, distribution_name: impl AsRef<OsStr>, default_uid: ULONG, wsl_distribution_flags: WSL_DISTRIBUTION_FLAGS, ) -> Result<()>
Modifies the behavior of a distribution registered with the Windows Subsystem for Linux (WSL).
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).default_uid
- The Linux user ID to use when launching new WSL sessions for this distribution.wsl_distribution_flags
- Flags specifying what behavior to use for this distribution.
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
didn’t exist?Err(Error)
- if WslConfigureDistribution otherwise failed (invalid uid? invalid flags?)Ok(())
- otherwise
§Returns
Sourcepub fn get_distribution_configuration(
&self,
distribution_name: impl AsRef<OsStr>,
) -> Result<Configuration>
pub fn get_distribution_configuration( &self, distribution_name: impl AsRef<OsStr>, ) -> Result<Configuration>
Retrieves the current configuration of a distribution registered with the Windows Subsystem for Linux (WSL).
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
didn’t exist?Err(Error)
- if WslGetDistributionConfiguration failedOk(Configuration { version, default_uid, flags, default_environment_variables })
- otherwise
§See Also
- WslGetDistributionConfiguration - the underlying API
- Configuration - the returned struct
Examples found in repository?
5fn main() {
6 let ubuntu = "Ubuntu";
7 let nonexistant = "Nonexistant";
8
9 let wsl = Library::new().unwrap();
10 assert!( wsl.is_distribution_registered(ubuntu));
11 assert!(!wsl.is_distribution_registered(nonexistant));
12 assert!(wsl.get_distribution_configuration(nonexistant).is_err());
13
14 let cfg = wsl.get_distribution_configuration(ubuntu).unwrap();
15 assert!(cfg.default_uid == 0 || (1000 ..= 2000).contains(&cfg.default_uid)); // Root or regular UID
16 assert!(cfg.flags & WSL_DISTRIBUTION_FLAGS::DEFAULT == WSL_DISTRIBUTION_FLAGS::DEFAULT);
17 assert!((1..=2).contains(&cfg.version)); // WSL version
18
19 println!("Environment varibales:");
20 for (k,v) in cfg.default_environment_variables.iter() {
21 // XXX: This might be inaccurate / depend on the linux locale? Good enough for a demo though!
22 let k = String::from_utf8_lossy(k);
23 let v = String::from_utf8_lossy(v);
24 println!("{: <10} = {}", k, v);
25 }
26
27 wsl.launch_interactive(ubuntu, "echo testing 123", true).unwrap();
28
29 let stdin = "echo testing 456\necho PATH: ${PATH}\nasdf";
30 let stdout = std::fs::File::create("target/basic.txt").unwrap();
31 let stderr = std::fs::File::create("CON").unwrap();
32 wsl.launch(ubuntu, "sh", true, stdin, stdout, stderr).unwrap().wait().unwrap();
33
34 println!("\rPress ENTER to quit");
35 let _ = std::io::stdin().read_line(&mut String::new());
36}
Sourcepub fn launch_interactive(
&self,
distribution_name: impl AsRef<OsStr>,
command: impl AsRef<OsStr>,
use_current_working_directory: bool,
) -> Result<DWORD>
pub fn launch_interactive( &self, distribution_name: impl AsRef<OsStr>, command: impl AsRef<OsStr>, use_current_working_directory: bool, ) -> Result<DWORD>
Launches an interactive Windows Subsystem for Linux (WSL) process in the context of a particular distribution. This differs from Library::launch in that the end user will be able to interact with the newly-created process.
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).command
- Command to execute. If no command is supplied, launches the default shell.use_current_working_directory
- Governs whether or not the launched process should inherit the calling process’s working directory. Iffalse
, the process is started in the WSL default user’s home directory (“~”).
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
didn’t exist?Err(Error)
- ifcommand
contained'\0'
charactersErr(Error)
- if WslLaunchInteractive otherwise failedOk(DWORD)
- the exit code of the process after it exits.
§See Also
- Library::launch - non-interactive, programatic interaction
- WslLaunchInteractive - the underlying API
Examples found in repository?
5fn main() {
6 let ubuntu = "Ubuntu";
7 let nonexistant = "Nonexistant";
8
9 let wsl = Library::new().unwrap();
10 assert!( wsl.is_distribution_registered(ubuntu));
11 assert!(!wsl.is_distribution_registered(nonexistant));
12 assert!(wsl.get_distribution_configuration(nonexistant).is_err());
13
14 let cfg = wsl.get_distribution_configuration(ubuntu).unwrap();
15 assert!(cfg.default_uid == 0 || (1000 ..= 2000).contains(&cfg.default_uid)); // Root or regular UID
16 assert!(cfg.flags & WSL_DISTRIBUTION_FLAGS::DEFAULT == WSL_DISTRIBUTION_FLAGS::DEFAULT);
17 assert!((1..=2).contains(&cfg.version)); // WSL version
18
19 println!("Environment varibales:");
20 for (k,v) in cfg.default_environment_variables.iter() {
21 // XXX: This might be inaccurate / depend on the linux locale? Good enough for a demo though!
22 let k = String::from_utf8_lossy(k);
23 let v = String::from_utf8_lossy(v);
24 println!("{: <10} = {}", k, v);
25 }
26
27 wsl.launch_interactive(ubuntu, "echo testing 123", true).unwrap();
28
29 let stdin = "echo testing 456\necho PATH: ${PATH}\nasdf";
30 let stdout = std::fs::File::create("target/basic.txt").unwrap();
31 let stderr = std::fs::File::create("CON").unwrap();
32 wsl.launch(ubuntu, "sh", true, stdin, stdout, stderr).unwrap().wait().unwrap();
33
34 println!("\rPress ENTER to quit");
35 let _ = std::io::stdin().read_line(&mut String::new());
36}
Sourcepub fn launch<I, O, E>(
&self,
distribution_name: impl AsRef<OsStr>,
command: impl AsRef<OsStr>,
use_current_working_directory: bool,
stdin: I,
stdout: O,
stderr: E,
) -> Result<Process>
pub fn launch<I, O, E>( &self, distribution_name: impl AsRef<OsStr>, command: impl AsRef<OsStr>, use_current_working_directory: bool, stdin: I, stdout: O, stderr: E, ) -> Result<Process>
Launches a Windows Subsystem for Linux (WSL) process in the context of a particular distribution.
§Arguments
distribution_name
- Unique name representing a distribution (for example, “Fabrikam.Distro.10.01”).command
- Command to execute. If no command is supplied, launches the default shell.use_current_working_directory
- Governs whether or not the launched process should inherit the calling process’s working directory. Iffalse
, the process is started in the WSL default user’s home directory (“~”).stdin
- Handle to use for STDIN.stdout
- Handle to use for STDOUT.stderr
- Handle to use for STDERR.
§Returns
Err(Error)
- ifdistribution_name
contained'\0'
charactersErr(Error)
- ifdistribution_name
didn’t exist?Err(Error)
- ifcommand
contained'\0'
charactersErr(Error)
- ifstdin
,stdout
, orstderr
failed to convert to StdioErr(Error)
- ifstdin
,stdout
, orstderr
was an invalid handle for WslLaunchErr(Error)
- if WslLaunch otherwise failedOk(Process)
- if the WSL process that launched successfully
§See Also
- Process
- Library::launch_interactive - interactive, inherits the same console handles
- WslLaunch - the underlying API
Examples found in repository?
5fn main() {
6 let ubuntu = "Ubuntu";
7 let nonexistant = "Nonexistant";
8
9 let wsl = Library::new().unwrap();
10 assert!( wsl.is_distribution_registered(ubuntu));
11 assert!(!wsl.is_distribution_registered(nonexistant));
12 assert!(wsl.get_distribution_configuration(nonexistant).is_err());
13
14 let cfg = wsl.get_distribution_configuration(ubuntu).unwrap();
15 assert!(cfg.default_uid == 0 || (1000 ..= 2000).contains(&cfg.default_uid)); // Root or regular UID
16 assert!(cfg.flags & WSL_DISTRIBUTION_FLAGS::DEFAULT == WSL_DISTRIBUTION_FLAGS::DEFAULT);
17 assert!((1..=2).contains(&cfg.version)); // WSL version
18
19 println!("Environment varibales:");
20 for (k,v) in cfg.default_environment_variables.iter() {
21 // XXX: This might be inaccurate / depend on the linux locale? Good enough for a demo though!
22 let k = String::from_utf8_lossy(k);
23 let v = String::from_utf8_lossy(v);
24 println!("{: <10} = {}", k, v);
25 }
26
27 wsl.launch_interactive(ubuntu, "echo testing 123", true).unwrap();
28
29 let stdin = "echo testing 456\necho PATH: ${PATH}\nasdf";
30 let stdout = std::fs::File::create("target/basic.txt").unwrap();
31 let stderr = std::fs::File::create("CON").unwrap();
32 wsl.launch(ubuntu, "sh", true, stdin, stdout, stderr).unwrap().wait().unwrap();
33
34 println!("\rPress ENTER to quit");
35 let _ = std::io::stdin().read_line(&mut String::new());
36}