aria2-core 0.3.11

aria2-rust core download engine: multi-protocol segmented downloads, rate limiting, configuration, session persistence, and BitTorrent seeding
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
//! OS-backed waiting for an arbitrary process to exit.
//!
//! The public shutdown watcher lives in `halt_watchers.rs`; this module keeps
//! platform-specific process handles out of that policy code. Native process
//! events are used wherever the target exposes them:
//!
//! - Windows: process handle plus a cancellation event
//! - Linux: `pidfd_open` registered with Tokio's `AsyncFd`
//! - macOS/BSD/Haiku: `kqueue` with `EVFILT_PROC | NOTE_EXIT`
//! - other targets: a low-frequency compatibility check, because no portable
//!   arbitrary-PID exit event is available there

use std::time::Duration;

#[cfg(unix)]
use std::io;
#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
use std::os::fd::{AsRawFd, RawFd};

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
use tracing::debug;
use tracing::warn;

use super::engine_command::EngineCommandSender;

const FALLBACK_PROCESS_POLL_INTERVAL: Duration = Duration::from_secs(1);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProcessWaitResult {
    Exited,
    ChannelClosed,
}

/// Wait for a process-exit event, falling back only where the target OS has no
/// usable event primitive for an arbitrary PID.
#[cfg(windows)]
pub(crate) async fn wait_for_process_exit(
    pid: u32,
    cmd_tx: &EngineCommandSender,
) -> ProcessWaitResult {
    use windows_sys::Win32::System::Threading::CreateEventW;

    // The cancellation event lets the blocking wait be interrupted when the
    // engine shuts down. Aborting a `spawn_blocking` task would otherwise leave
    // a worker thread parked until the watched process exits.
    let cancel_handle = unsafe { CreateEventW(std::ptr::null(), 1, 0, std::ptr::null()) as usize };
    if cancel_handle == 0 {
        warn!(pid, "Failed to create process watcher cancellation event");
        return wait_for_process_exit_by_polling(pid, cmd_tx).await;
    }

    let mut cancel_signal = WindowsCancelSignal::new(cancel_handle);
    let mut wait_task =
        tokio::task::spawn_blocking(move || wait_for_process_exit_windows(pid, cancel_handle));

    let result = tokio::select! {
        wait_result = &mut wait_task => wait_result.unwrap_or(ProcessWaitResult::Exited),
        _ = cmd_tx.closed() => {
            cancel_signal.signal();
            wait_task.await.unwrap_or(ProcessWaitResult::ChannelClosed);
            ProcessWaitResult::ChannelClosed
        }
    };

    // The blocking waiter owns and closes the event after returning. Disarm
    // the guard so a completed wait is not signalled through a stale handle.
    cancel_signal.disarm();
    result
}

#[cfg(windows)]
struct WindowsCancelSignal {
    handle: usize,
}

#[cfg(windows)]
impl WindowsCancelSignal {
    fn new(handle: usize) -> Self {
        Self { handle }
    }

    fn signal(&self) {
        use windows_sys::Win32::Foundation::HANDLE;
        use windows_sys::Win32::System::Threading::SetEvent;

        // SAFETY: the blocking waiter owns the handle and only closes it after
        // observing the event or the process exit.
        unsafe {
            SetEvent(self.handle as HANDLE);
        }
    }

    fn disarm(&mut self) {
        self.handle = 0;
    }
}

#[cfg(windows)]
impl Drop for WindowsCancelSignal {
    fn drop(&mut self) {
        if self.handle != 0 {
            self.signal();
        }
    }
}

#[cfg(windows)]
fn wait_for_process_exit_windows(pid: u32, cancel_handle: usize) -> ProcessWaitResult {
    use windows_sys::Win32::Foundation::{CloseHandle, HANDLE, WAIT_OBJECT_0};
    use windows_sys::Win32::System::Threading::{
        INFINITE, OpenProcess, PROCESS_SYNCHRONIZE, WaitForMultipleObjects,
    };

    // SAFETY: both handles are closed before returning. The event handle is
    // owned by this blocking waiter so an aborted async parent cannot leave it
    // dangling or close it while `WaitForMultipleObjects` is using it.
    unsafe {
        let process_handle = OpenProcess(PROCESS_SYNCHRONIZE, 0, pid);
        if process_handle.is_null() {
            CloseHandle(cancel_handle as HANDLE);
            return ProcessWaitResult::Exited;
        }

        let handles = [process_handle, cancel_handle as HANDLE];
        let wait_result = WaitForMultipleObjects(2, handles.as_ptr(), 0, INFINITE);
        CloseHandle(process_handle);
        CloseHandle(cancel_handle as HANDLE);

        if wait_result == WAIT_OBJECT_0 {
            ProcessWaitResult::Exited
        } else if wait_result == WAIT_OBJECT_0 + 1 {
            ProcessWaitResult::ChannelClosed
        } else {
            // A failed wait matches the old behaviour of treating an
            // inaccessible process as gone rather than hanging forever.
            ProcessWaitResult::Exited
        }
    }
}

#[cfg(target_os = "linux")]
struct PidFd(RawFd);

#[cfg(target_os = "linux")]
impl AsRawFd for PidFd {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

#[cfg(target_os = "linux")]
impl Drop for PidFd {
    fn drop(&mut self) {
        // SAFETY: this descriptor is owned by the wrapper and is closed once.
        unsafe {
            libc::close(self.0);
        }
    }
}

#[cfg(target_os = "linux")]
fn open_pidfd(pid: u32) -> io::Result<PidFd> {
    if pid == 0 || pid > libc::pid_t::MAX as u32 {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "PID is outside the platform range",
        ));
    }

    // `pidfd_open` is exposed through libc's syscall constants so this still
    // builds against libc versions that predate the typed wrapper.
    // SAFETY: the syscall arguments are plain integer values.
    let fd = unsafe { libc::syscall(libc::SYS_pidfd_open, pid as libc::pid_t, 0) };
    if fd < 0 {
        Err(io::Error::last_os_error())
    } else {
        Ok(PidFd(fd as RawFd))
    }
}

#[cfg(target_os = "linux")]
pub(crate) async fn wait_for_process_exit(
    pid: u32,
    cmd_tx: &EngineCommandSender,
) -> ProcessWaitResult {
    let pidfd = match open_pidfd(pid) {
        Ok(pidfd) => pidfd,
        Err(error) if error.raw_os_error() == Some(libc::ESRCH) => {
            return ProcessWaitResult::Exited;
        }
        Err(error) => {
            debug!(pid, error = %error, "pidfd_open unavailable; using compatibility watcher");
            return wait_for_process_exit_by_polling(pid, cmd_tx).await;
        }
    };

    let async_fd = match tokio::io::unix::AsyncFd::new(pidfd) {
        Ok(async_fd) => async_fd,
        Err(error) => {
            debug!(pid, error = %error, "Failed to register pidfd; using compatibility watcher");
            return wait_for_process_exit_by_polling(pid, cmd_tx).await;
        }
    };

    tokio::select! {
        result = async_fd.readable() => {
            if let Err(error) = result {
                warn!(pid, error = %error, "pidfd readiness failed; treating process as gone");
            }
            ProcessWaitResult::Exited
        }
        _ = cmd_tx.closed() => ProcessWaitResult::ChannelClosed,
    }
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
struct KqueueFd(RawFd);

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
impl AsRawFd for KqueueFd {
    fn as_raw_fd(&self) -> RawFd {
        self.0
    }
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
impl Drop for KqueueFd {
    fn drop(&mut self) {
        // SAFETY: this descriptor is owned by the wrapper and is closed once.
        unsafe {
            libc::close(self.0);
        }
    }
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
fn open_process_kqueue(pid: u32) -> io::Result<KqueueFd> {
    if pid == 0 || pid > libc::pid_t::MAX as u32 {
        return Err(io::Error::new(
            io::ErrorKind::NotFound,
            "PID is outside the platform range",
        ));
    }

    // SAFETY: `kqueue` takes no arguments and returns an owned descriptor.
    let kqueue_fd = unsafe { libc::kqueue() };
    if kqueue_fd < 0 {
        return Err(io::Error::last_os_error());
    }

    let close_on_error = |error| {
        // SAFETY: this is the newly-created descriptor and no other owner exists.
        unsafe { libc::close(kqueue_fd) };
        Err(error)
    };

    // Tokio's `AsyncFd` requires a non-blocking descriptor.
    // SAFETY: `fcntl` operates on the descriptor created above.
    let flags = unsafe { libc::fcntl(kqueue_fd, libc::F_GETFL) };
    if flags < 0 {
        return close_on_error(io::Error::last_os_error());
    }
    // SAFETY: same descriptor, with the non-blocking flag added.
    if unsafe { libc::fcntl(kqueue_fd, libc::F_SETFL, flags | libc::O_NONBLOCK) } < 0 {
        return close_on_error(io::Error::last_os_error());
    }

    // SAFETY: zero-initialization is the portable way to handle the extra
    // fields present in newer FreeBSD `kevent` layouts.
    let mut event: libc::kevent = unsafe { std::mem::zeroed() };
    event.ident = pid as libc::uintptr_t;
    event.filter = libc::EVFILT_PROC as _;
    event.flags = (libc::EV_ADD | libc::EV_ONESHOT) as _;
    event.fflags = libc::NOTE_EXIT as _;

    // SAFETY: all pointers reference valid storage for the duration of the call.
    let result = unsafe {
        libc::kevent(
            kqueue_fd,
            &event,
            1,
            std::ptr::null_mut(),
            0,
            std::ptr::null(),
        )
    };
    if result < 0 {
        return close_on_error(io::Error::last_os_error());
    }

    Ok(KqueueFd(kqueue_fd))
}

#[cfg(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
))]
pub(crate) async fn wait_for_process_exit(
    pid: u32,
    cmd_tx: &EngineCommandSender,
) -> ProcessWaitResult {
    let kqueue = match open_process_kqueue(pid) {
        Ok(kqueue) => kqueue,
        Err(error) if error.raw_os_error() == Some(libc::ESRCH) => {
            return ProcessWaitResult::Exited;
        }
        Err(error) => {
            debug!(pid, error = %error, "kqueue process watch unavailable; using compatibility watcher");
            return wait_for_process_exit_by_polling(pid, cmd_tx).await;
        }
    };

    let async_fd = match tokio::io::unix::AsyncFd::new(kqueue) {
        Ok(async_fd) => async_fd,
        Err(error) => {
            debug!(pid, error = %error, "Failed to register kqueue; using compatibility watcher");
            return wait_for_process_exit_by_polling(pid, cmd_tx).await;
        }
    };

    tokio::select! {
        result = async_fd.readable() => {
            if let Err(error) = result {
                warn!(pid, error = %error, "kqueue readiness failed; treating process as gone");
            }
            ProcessWaitResult::Exited
        }
        _ = cmd_tx.closed() => ProcessWaitResult::ChannelClosed,
    }
}

#[cfg(not(any(
    target_os = "dragonfly",
    target_os = "freebsd",
    target_os = "haiku",
    target_os = "ios",
    target_os = "linux",
    target_os = "macos",
    target_os = "netbsd",
    target_os = "openbsd",
    windows,
)))]
pub(crate) async fn wait_for_process_exit(
    pid: u32,
    cmd_tx: &EngineCommandSender,
) -> ProcessWaitResult {
    wait_for_process_exit_by_polling(pid, cmd_tx).await
}

async fn wait_for_process_exit_by_polling(
    pid: u32,
    cmd_tx: &EngineCommandSender,
) -> ProcessWaitResult {
    loop {
        if !super::halt_watchers::is_process_alive(pid) {
            return ProcessWaitResult::Exited;
        }

        tokio::select! {
            _ = tokio::time::sleep(FALLBACK_PROCESS_POLL_INTERVAL) => {}
            _ = cmd_tx.closed() => return ProcessWaitResult::ChannelClosed,
        }
    }
}