Merge pull request #119 from koitococo/master

Write unshare pid into file for scripting purposes
This commit is contained in:
Birk Blechschmidt 2024-06-05 18:11:18 +02:00 committed by GitHub
commit 4243057fbf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 2 deletions

View file

@ -32,6 +32,11 @@ pub struct Args {
#[arg(long)] #[arg(long)]
pub unshare: bool, pub unshare: bool,
/// Create a pidfile of `unshare` process when using `--unshare`.
#[cfg(target_os = "linux")]
#[arg(long)]
pub unshare_pidfile: Option<String>,
/// File descriptor for UNIX datagram socket meant to transfer /// File descriptor for UNIX datagram socket meant to transfer
/// network sockets from global namespace to the new one. /// network sockets from global namespace to the new one.
/// See `unshare(1)`, `namespaces(7)`, `sendmsg(2)`, `unix(7)`. /// See `unshare(1)`, `namespaces(7)`, `sendmsg(2)`, `unix(7)`.
@ -103,6 +108,8 @@ impl Default for Args {
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
unshare: false, unshare: false,
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
unshare_pidfile: None,
#[cfg(target_os = "linux")]
socket_transfer_fd: None, socket_transfer_fd: None,
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
admin_command: Vec::new(), admin_command: Vec::new(),

View file

@ -72,6 +72,7 @@ async fn namespace_proxy_main(
child => child?, child => child?,
}; };
let unshare_pid = child.id().unwrap_or(0);
log::info!("The tun proxy is running in unprivileged mode. See `namespaces(7)`."); log::info!("The tun proxy is running in unprivileged mode. See `namespaces(7)`.");
log::info!(""); log::info!("");
log::info!("If you need to run a process that relies on root-like capabilities (e.g. `openvpn`)"); log::info!("If you need to run a process that relies on root-like capabilities (e.g. `openvpn`)");
@ -80,10 +81,13 @@ async fn namespace_proxy_main(
log::info!("To run a new process in the created namespace (e.g. a flatpak app)"); log::info!("To run a new process in the created namespace (e.g. a flatpak app)");
log::info!( log::info!(
"Use `nsenter --preserve-credentials --user --net --mount --target {} /bin/sh`", "Use `nsenter --preserve-credentials --user --net --mount --target {} /bin/sh`",
child.id().unwrap_or(0) unshare_pid
); );
log::info!(""); log::info!("");
if let Some(pidfile) = _args.unshare_pidfile.as_ref() {
log::info!("Writing unshare pid to {}", pidfile);
std::fs::write(pidfile, unshare_pid.to_string()).ok();
}
tokio::spawn(async move { tun2proxy::socket_transfer::process_socket_requests(&socket).await }); tokio::spawn(async move { tun2proxy::socket_transfer::process_socket_requests(&socket).await });
Ok(child.wait().await?) Ok(child.wait().await?)