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
use glib::translate::*;
use io_lifetimes::{BorrowedFd, OwnedFd};
use std::boxed::Box as Box_;
use std::os::unix::io::{IntoRawFd, RawFd};
use std::ptr;
use crate::{ffi, prelude::*, Pty};
impl Pty {
///
/// # Returns
///
/// the file descriptor of the PTY master in @self. The
/// file descriptor belongs to @self and must not be closed or have
/// its flags changed
#[doc(alias = "vte_pty_get_fd")]
#[doc(alias = "get_fd")]
pub fn fd(&self) -> BorrowedFd<'_> {
unsafe {
let raw_fd = ffi::vte_pty_get_fd(self.to_glib_none().0);
BorrowedFd::borrow_raw(raw_fd)
}
}
/// Creates a new #VtePty for the PTY master @fd.
///
/// No entry will be made in the lastlog, utmp or wtmp system files.
///
/// Note that the newly created #VtePty will take ownership of @fd
/// and close it on finalize.
/// ## `fd`
/// a file descriptor to the PTY
/// ## `cancellable`
/// a #GCancellable, or [`None`]
///
/// # Returns
///
/// a new #VtePty for @fd, or [`None`] on error with @error filled in
#[doc(alias = "vte_pty_new_foreign_sync")]
#[doc(alias = "new_foreign_sync")]
pub fn foreign_sync(
fd: OwnedFd,
cancellable: Option<&impl IsA<gio::Cancellable>>,
) -> Result<Pty, glib::Error> {
assert_initialized_main_thread!();
unsafe {
let mut error = ptr::null_mut();
let ret = ffi::vte_pty_new_foreign_sync(
fd.into_raw_fd(),
cancellable.map(|p| p.as_ref()).to_glib_none().0,
&mut error,
);
if error.is_null() {
Ok(from_glib_full(ret))
} else {
Err(from_glib_full(error))
}
}
}
/// Like vte_pty_spawn_with_fds_async(), except that this function does not
/// allow passing file descriptors to the child process. See vte_pty_spawn_with_fds_async()
/// for more information.
/// ## `working_directory`
/// the name of a directory the command should start
/// in, or [`None`] to use the current working directory
/// ## `argv`
/// child's argument vector
/// ## `envv`
/// a list of environment
/// variables to be added to the environment before starting the process, or [`None`]
/// ## `spawn_flags`
/// flags from #GSpawnFlags
/// ## `child_setup`
/// an extra child setup function to run in the child just before exec(), or [`None`]
/// ## `child_setup_data`
/// user data for @child_setup, or [`None`]
/// ## `child_setup_data_destroy`
/// a #GDestroyNotify for @child_setup_data, or [`None`]
/// ## `timeout`
/// a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #GAsyncReadyCallback, or [`None`]
#[doc(alias = "vte_pty_spawn_async")]
#[allow(clippy::too_many_arguments)]
pub fn spawn_async<P: FnOnce(Result<glib::Pid, glib::Error>) + 'static, Q: Fn() + 'static>(
&self,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
assert_initialized_main_thread!();
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let child_setup_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::new(glib::thread_guard::ThreadGuard::new(child_setup));
unsafe extern "C" fn child_setup_func<Q: Fn() + 'static>(user_data: glib::ffi::gpointer) {
let callback: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback()
}
let child_setup = Some(child_setup_func::<Q> as _);
let callback_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn spawn_async_trampoline<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let mut child_pid = 0;
let _ = ffi::vte_pty_spawn_finish(
_source_object as *mut _,
res,
&mut child_pid,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib(child_pid))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback: P = callback.into_inner();
callback(result);
}
let callback = Some(spawn_async_trampoline::<P> as _);
unsafe extern "C" fn child_setup_data_destroy_func<Q: Fn() + 'static>(
data: glib::ffi::gpointer,
) {
let _callback: Box_<Q> = Box_::from_raw(data as *mut _);
}
let destroy_call8 = Some(child_setup_data_destroy_func::<Q> as _);
let super_callback0: Box_<glib::thread_guard::ThreadGuard<Q>> = child_setup_data;
let super_callback1: Box_<glib::thread_guard::ThreadGuard<P>> = callback_data;
unsafe {
ffi::vte_pty_spawn_async(
self.to_glib_none().0,
working_directory.to_glib_none().0,
argv.to_glib_none().0,
envv.to_glib_none().0,
spawn_flags.into_glib(),
child_setup,
Box_::into_raw(super_callback0) as *mut _,
destroy_call8,
timeout,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
callback,
Box_::into_raw(super_callback1) as *mut _,
);
}
}
#[doc(alias = "vte_pty_spawn_async")]
#[allow(clippy::too_many_arguments)]
pub fn spawn_future<Q: Fn() + 'static>(
&self,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
> {
let working_directory = working_directory.map(ToOwned::to_owned);
let argv: Vec<String> = argv.iter().map(|p| p.to_string()).collect();
let envv: Vec<String> = envv.iter().map(|p| p.to_string()).collect();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
let argv: Vec<&str> = argv.iter().map(|s| s.as_str()).collect();
let envv: Vec<&str> = envv.iter().map(|s| s.as_str()).collect();
obj.spawn_async(
working_directory.as_deref(),
&argv,
&envv,
spawn_flags,
child_setup,
timeout,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
/// # Safety
///
/// The map_fds have to make sense.
// rustdoc-stripper-ignore-next-stop
/// Starts the specified command under the pseudo-terminal @self.
/// The @argv and @envv lists should be [`None`]-terminated.
/// The "TERM" environment variable is automatically set to a default value,
/// but can be overridden from @envv.
/// @pty_flags controls logging the session to the specified system log files.
///
/// Note also that [`glib::SpawnFlags::STDOUT_TO_DEV_NULL`][crate::glib::SpawnFlags::STDOUT_TO_DEV_NULL], [`glib::SpawnFlags::STDERR_TO_DEV_NULL`][crate::glib::SpawnFlags::STDERR_TO_DEV_NULL],
/// and [`glib::SpawnFlags::CHILD_INHERITS_STDIN`][crate::glib::SpawnFlags::CHILD_INHERITS_STDIN] are not supported in @spawn_flags, since
/// stdin, stdout and stderr of the child process will always be connected to
/// the PTY. Also [`glib::SpawnFlags::LEAVE_DESCRIPTORS_OPEN`][crate::glib::SpawnFlags::LEAVE_DESCRIPTORS_OPEN] is not supported; and
/// [`glib::SpawnFlags::DO_NOT_REAP_CHILD`][crate::glib::SpawnFlags::DO_NOT_REAP_CHILD] will always be added to @spawn_flags.
///
/// If @fds is not [`None`], the child process will map the file descriptors from
/// @fds according to @map_fds; @n_map_fds must be less or equal to @n_fds.
/// This function will take ownership of the file descriptors in @fds;
/// you must not use or close them after this call. All file descriptors in @fds
/// must have the FD_CLOEXEC flag set on them; it will be unset in the child process
/// before calling man:execve(2). Note also that no file descriptor may be mapped
/// to stdin, stdout, or stderr (file descriptors 0, 1, or 2), since these will be
/// assigned to the PTY. All open file descriptors apart from those mapped as above
/// will be closed when execve() is called.
///
/// Beginning with 0.60, and on linux only, and unless `VTE_SPAWN_NO_SYSTEMD_SCOPE` is
/// passed in @spawn_flags, the newly created child process will be moved to its own
/// systemd user scope; and if `VTE_SPAWN_REQUIRE_SYSTEMD_SCOPE` is passed, and creation
/// of the systemd user scope fails, the whole spawn will fail.
/// You can override the options used for the systemd user scope by
/// providing a systemd override file for 'vte-spawn-.scope' unit. See man:systemd.unit(5)
/// for further information.
///
/// See vte_pty_new(), and vte_terminal_watch_child() for more information.
/// ## `working_directory`
/// the name of a directory the command should start
/// in, or [`None`] to use the current working directory
/// ## `argv`
/// child's argument vector
/// ## `envv`
/// a list of environment
/// variables to be added to the environment before starting the process, or [`None`]
/// ## `fds`
/// an array of file descriptors, or [`None`]
/// ## `map_fds`
/// an array of integers, or [`None`]
/// ## `spawn_flags`
/// flags from #GSpawnFlags
/// ## `child_setup`
/// an extra child setup function to run in the child just before exec(), or [`None`]
/// ## `child_setup_data`
/// user data for @child_setup, or [`None`]
/// ## `child_setup_data_destroy`
/// a #GDestroyNotify for @child_setup_data, or [`None`]
/// ## `timeout`
/// a timeout value in ms, -1 for the default timeout, or G_MAXINT to wait indefinitely
/// ## `cancellable`
/// a #GCancellable, or [`None`]
/// ## `callback`
/// a #GAsyncReadyCallback, or [`None`]
#[allow(clippy::too_many_arguments)]
pub unsafe fn spawn_with_fds_async<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
Q: Fn() + 'static,
>(
&self,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
cancellable: Option<&impl IsA<gio::Cancellable>>,
callback: P,
) {
assert_initialized_main_thread!();
let main_context = glib::MainContext::ref_thread_default();
let is_main_context_owner = main_context.is_owner();
let has_acquired_main_context = (!is_main_context_owner)
.then(|| main_context.acquire().ok())
.flatten();
assert!(
is_main_context_owner || has_acquired_main_context.is_some(),
"Async operations only allowed if the thread is owning the MainContext"
);
let n_fds = fds.len() as _;
let n_map_fds = map_fds.len() as _;
let child_setup_data: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::new(glib::thread_guard::ThreadGuard::new(child_setup));
unsafe extern "C" fn child_setup_func<Q: Fn() + 'static>(user_data: glib::ffi::gpointer) {
let callback: Box_<glib::thread_guard::ThreadGuard<Q>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback()
}
let child_setup = Some(child_setup_func::<Q> as _);
let callback_data: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::new(glib::thread_guard::ThreadGuard::new(callback));
unsafe extern "C" fn spawn_with_fds_trampoline<
P: FnOnce(Result<glib::Pid, glib::Error>) + 'static,
>(
_source_object: *mut glib::gobject_ffi::GObject,
res: *mut gio::ffi::GAsyncResult,
user_data: glib::ffi::gpointer,
) {
let mut error = ptr::null_mut();
let mut child_pid = 0;
let _ = ffi::vte_pty_spawn_finish(
_source_object as *mut _,
res,
&mut child_pid,
&mut error,
);
let result = if error.is_null() {
Ok(from_glib(child_pid))
} else {
Err(from_glib_full(error))
};
let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
Box_::from_raw(user_data as *mut _);
let callback = callback.into_inner();
callback(result)
}
let callback = Some(spawn_with_fds_trampoline::<P> as _);
unsafe extern "C" fn child_setup_data_destroy_func<Q: Fn() + 'static>(
data: glib::ffi::gpointer,
) {
let _callback: Box_<Q> = Box_::from_raw(data as *mut _);
}
let destroy_call12 = Some(child_setup_data_destroy_func::<Q> as _);
let super_callback0: Box_<glib::thread_guard::ThreadGuard<Q>> = child_setup_data;
let super_callback1: Box_<glib::thread_guard::ThreadGuard<P>> = callback_data;
let fds: Vec<RawFd> = fds.into_iter().map(|x| x.into_raw_fd()).collect();
unsafe {
ffi::vte_pty_spawn_with_fds_async(
self.to_glib_none().0,
working_directory.to_glib_none().0,
argv.to_glib_none().0,
envv.to_glib_none().0,
fds.to_glib_none().0,
n_fds,
map_fds.to_glib_none().0,
n_map_fds,
spawn_flags.into_glib(),
child_setup,
Box_::into_raw(super_callback0) as *mut _,
destroy_call12,
timeout,
cancellable.map(|p| p.as_ref()).to_glib_none().0,
callback,
Box_::into_raw(super_callback1) as *mut _,
);
}
}
// # Safety
//
// The map_fds have to make sense.
#[doc(alias = "vte_pty_spawn_async")]
#[allow(clippy::too_many_arguments)]
pub unsafe fn spawn_with_fds_future<Q: Fn() + 'static>(
&self,
working_directory: Option<&str>,
argv: &[&str],
envv: &[&str],
fds: Vec<OwnedFd>,
map_fds: &[RawFd],
spawn_flags: glib::SpawnFlags,
child_setup: Q,
timeout: i32,
) -> std::pin::Pin<
Box_<dyn std::future::Future<Output = Result<glib::Pid, glib::Error>> + 'static>,
> {
let working_directory = working_directory.map(ToOwned::to_owned);
let argv: Vec<String> = argv.iter().map(|x| x.to_string()).collect();
let envv: Vec<String> = envv.iter().map(|x| x.to_string()).collect();
let map_fds = map_fds.to_vec();
Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
let argv: Vec<&str> = argv.iter().map(|s| s.as_str()).collect();
let envv: Vec<&str> = envv.iter().map(|s| s.as_str()).collect();
obj.spawn_with_fds_async(
working_directory.as_deref(),
&argv,
&envv,
fds,
&map_fds,
spawn_flags,
child_setup,
timeout,
Some(cancellable),
move |res| {
send.resolve(res);
},
);
}))
}
}