strata-ui-platform 0.1.1

Native Hardware Abstraction Layer for Android, iOS, macOS, Windows, Linux, and Web
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
//! Single-Instance Mutex and Inter-Process Communication (IPC) Window Focusing.
//!
//! Prevents duplicate application processes from opening when an executable is launched
//! repeatedly, automatically forwarding CLI arguments to the primary instance and requesting
//! window focusing / un-minimization.
//!
//! Unix hosts use a domain socket in `$XDG_RUNTIME_DIR` (falling back to `/tmp`). Windows
//! hosts use a named pipe at `\\.\pipe\<sanitized-app-id>`. Both transports forward
//! newline-separated CLI arguments to the primary instance.

use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

#[cfg(unix)]
use std::path::PathBuf;

/// Status result when attempting to acquire a single-instance application lock.
#[derive(Debug)]
pub enum SingleInstanceStatus {
    /// This process is the primary instance and acquired the exclusive mutex.
    Primary(SingleInstanceLock),
    /// A primary instance is already running; CLI arguments were forwarded and this process should exit.
    SecondaryExited,
}

/// Serializes a slice of arguments into length-prefixed binary wire format.
///
/// Wire format:
/// - 4-byte little-endian u32: argument count `N`
/// - For each argument:
///   - 4-byte little-endian u32: byte length `L`
///   - `L` UTF-8 payload bytes
pub fn encode_args(args: &[String]) -> Vec<u8> {
    let mut buf = Vec::new();
    buf.extend_from_slice(&(args.len() as u32).to_le_bytes());
    for arg in args {
        let bytes = arg.as_bytes();
        buf.extend_from_slice(&(bytes.len() as u32).to_le_bytes());
        buf.extend_from_slice(bytes);
    }
    buf
}

/// Decodes arguments from the length-prefixed binary wire format.
///
/// Provides graceful backwards-compatible fallback for null-separated and newline-separated
/// payloads if length-prefixed framing is absent.
pub fn decode_args(payload: &[u8]) -> Vec<String> {
    if payload.len() >= 4 {
        let count = u32::from_le_bytes(payload[0..4].try_into().unwrap()) as usize;
        let mut offset = 4;
        let mut args = Vec::with_capacity(count.min(1024));
        let mut ok = true;
        for _ in 0..count {
            if offset + 4 > payload.len() {
                ok = false;
                break;
            }
            let len = u32::from_le_bytes(payload[offset..offset + 4].try_into().unwrap()) as usize;
            offset += 4;
            if offset + len > payload.len() {
                ok = false;
                break;
            }
            if let Ok(s) = std::str::from_utf8(&payload[offset..offset + len]) {
                args.push(s.to_string());
            } else {
                args.push(String::from_utf8_lossy(&payload[offset..offset + len]).into_owned());
            }
            offset += len;
        }
        if ok && offset == payload.len() {
            return args;
        }
    }

    // Backwards-compatible fallback: null-byte or newline delimited payload
    if let Ok(raw) = std::str::from_utf8(payload) {
        if raw.contains('\0') {
            return raw.split('\0').map(|s| s.to_string()).collect();
        }
        return raw
            .lines()
            .map(|s| s.to_string())
            .filter(|s| !s.is_empty())
            .collect();
    }

    Vec::new()
}

/// Mutex handle holding the single-instance lock and IPC listener thread.
#[derive(Debug)]
pub struct SingleInstanceLock {
    app_id: String,
    running: Arc<AtomicBool>,
    #[cfg(unix)]
    socket_path: PathBuf,
    #[cfg(windows)]
    pipe_handle: usize,
    listener_thread: Option<std::thread::JoinHandle<()>>,
}

impl SingleInstanceLock {
    /// Attempts to acquire an exclusive single-instance application lock for the given application ID.
    ///
    /// If another instance is already running, forwards `args` to it and returns
    /// `SingleInstanceStatus::SecondaryExited`.
    ///
    /// If this is the first instance, binds the IPC channel and invokes `on_secondary_args` whenever
    /// subsequent processes launch.
    pub fn acquire<F>(
        app_id: &str,
        args: &[String],
        on_secondary_args: F,
    ) -> Result<SingleInstanceStatus, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        #[cfg(unix)]
        {
            let runtime_dir = match std::env::var_os("XDG_RUNTIME_DIR") {
                Some(p) => PathBuf::from(p),
                None => {
                    extern "C" {
                        fn getuid() -> u32;
                    }
                    let uid = unsafe { getuid() };
                    let dir = PathBuf::from(format!("/tmp/strata-runtime-{}", uid));
                    use std::os::unix::fs::{DirBuilderExt, MetadataExt, PermissionsExt};

                    if let Ok(meta) = std::fs::symlink_metadata(&dir) {
                        if meta.file_type().is_symlink() {
                            return Err(std::io::Error::new(
                                std::io::ErrorKind::PermissionDenied,
                                format!("Security violation: runtime directory {:?} is an insecure symlink (CWE-59)", dir),
                            ));
                        }
                        if meta.uid() != uid {
                            return Err(std::io::Error::new(
                                std::io::ErrorKind::PermissionDenied,
                                format!("Security violation: runtime directory {:?} is not owned by current user (uid {})", dir, uid),
                            ));
                        }
                        if meta.permissions().mode() & 0o777 != 0o700 {
                            let _ = std::fs::set_permissions(
                                &dir,
                                std::fs::Permissions::from_mode(0o700),
                            );
                        }
                    } else {
                        let mut builder = std::fs::DirBuilder::new();
                        builder.mode(0o700);
                        if let Err(e) = builder.create(&dir) {
                            if e.kind() != std::io::ErrorKind::AlreadyExists {
                                return Err(e);
                            }
                        }
                        let meta = std::fs::symlink_metadata(&dir)?;
                        if meta.file_type().is_symlink() || meta.uid() != uid {
                            return Err(std::io::Error::new(
                                std::io::ErrorKind::PermissionDenied,
                                format!("Security violation: created runtime directory {:?} failed verification", dir),
                            ));
                        }
                        let _ =
                            std::fs::set_permissions(&dir, std::fs::Permissions::from_mode(0o700));
                    }
                    dir
                }
            };
            Self::acquire_in_dir(app_id, args, &runtime_dir, on_secondary_args)
        }
        #[cfg(windows)]
        {
            Self::acquire_windows(app_id, args, on_secondary_args)
        }
        #[cfg(not(any(unix, windows)))]
        {
            let _ = (args, on_secondary_args);
            Ok(SingleInstanceStatus::Primary(Self::handle_only(app_id)))
        }
    }

    /// Acquires the lock inside an explicit runtime directory.
    ///
    /// This is the testable form of [`SingleInstanceLock::acquire`]: callers can
    /// point it at a private directory instead of mutating process environment
    /// variables. On Windows the runtime directory is ignored because the named
    /// pipe namespace is global.
    pub fn acquire_in_dir<F>(
        app_id: &str,
        args: &[String],
        runtime_dir: &std::path::Path,
        on_secondary_args: F,
    ) -> Result<SingleInstanceStatus, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        #[cfg(unix)]
        {
            Self::acquire_unix(app_id, args, runtime_dir, on_secondary_args)
        }
        #[cfg(windows)]
        {
            let _ = runtime_dir;
            Self::acquire_windows(app_id, args, on_secondary_args)
        }
        #[cfg(not(any(unix, windows)))]
        {
            let _ = (runtime_dir, args, on_secondary_args);
            Ok(SingleInstanceStatus::Primary(Self::handle_only(app_id)))
        }
    }

    #[cfg(not(any(unix, windows)))]
    fn handle_only(app_id: &str) -> Self {
        Self {
            app_id: app_id.to_string(),
            running: Arc::new(AtomicBool::new(true)),
            listener_thread: None,
        }
    }

    #[cfg(unix)]
    fn acquire_unix<F>(
        app_id: &str,
        args: &[String],
        runtime_dir: &std::path::Path,
        on_secondary_args: F,
    ) -> Result<SingleInstanceStatus, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        use std::io::Write;
        use std::os::unix::net::{UnixListener, UnixStream};

        let socket_name = format!("{}.sock", app_id.replace('/', "_"));
        let socket_path = runtime_dir.join(socket_name);

        // Security check: remove any untrusted symlink before socket binding (CWE-59)
        if let Ok(meta) = std::fs::symlink_metadata(&socket_path) {
            if meta.file_type().is_symlink() {
                let _ = std::fs::remove_file(&socket_path);
            }
        }

        // Try binding the socket as the primary instance.
        match UnixListener::bind(&socket_path) {
            Ok(listener) => {
                use std::os::unix::fs::PermissionsExt;
                let _ =
                    std::fs::set_permissions(&socket_path, std::fs::Permissions::from_mode(0o600));
                Ok(SingleInstanceStatus::Primary(Self::establish_unix(
                    app_id,
                    listener,
                    socket_path,
                    on_secondary_args,
                )?))
            }
            Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {
                // The path exists. Connect to determine whether the owner is live.
                match UnixStream::connect(&socket_path) {
                    Ok(mut stream) => {
                        // Primary is running: forward CLI arguments and exit cleanly.
                        let payload = encode_args(args);
                        let _ = stream.write_all(&payload);
                        let _ = stream.flush();
                        let _ = stream.shutdown(std::net::Shutdown::Write);
                        Ok(SingleInstanceStatus::SecondaryExited)
                    }
                    Err(_) => {
                        // Stale socket left by a crashed primary. Remove and rebind.
                        let _ = std::fs::remove_file(&socket_path);
                        let listener = UnixListener::bind(&socket_path)?;
                        use std::os::unix::fs::PermissionsExt;
                        let _ = std::fs::set_permissions(
                            &socket_path,
                            std::fs::Permissions::from_mode(0o600),
                        );
                        Ok(SingleInstanceStatus::Primary(Self::establish_unix(
                            app_id,
                            listener,
                            socket_path,
                            on_secondary_args,
                        )?))
                    }
                }
            }
            Err(e) => Err(e),
        }
    }

    #[cfg(unix)]
    fn establish_unix<F>(
        app_id: &str,
        listener: std::os::unix::net::UnixListener,
        socket_path: PathBuf,
        on_secondary_args: F,
    ) -> Result<Self, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        let running = Arc::new(AtomicBool::new(true));
        let on_args = Arc::new(on_secondary_args);
        let thread = Self::spawn_unix_listener(listener, Arc::clone(&running), on_args)?;
        Ok(Self {
            app_id: app_id.to_string(),
            running,
            socket_path,
            listener_thread: Some(thread),
        })
    }

    #[cfg(unix)]
    fn spawn_unix_listener<F>(
        listener: std::os::unix::net::UnixListener,
        running: Arc<AtomicBool>,
        on_args: Arc<F>,
    ) -> Result<std::thread::JoinHandle<()>, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        use std::io::Read;

        listener.set_nonblocking(true)?;

        let handle = std::thread::Builder::new()
            .name("strata-single-instance-listener".to_string())
            .spawn(move || {
                while running.load(Ordering::Relaxed) {
                    match listener.accept() {
                        Ok((mut stream, _)) => {
                            let _ = stream.set_nonblocking(false);
                            let mut payload = Vec::new();
                            if stream.read_to_end(&mut payload).is_ok() {
                                let parsed_args = decode_args(&payload);
                                on_args(parsed_args);
                            }
                        }
                        Err(ref e) if e.kind() == std::io::ErrorKind::WouldBlock => {
                            std::thread::sleep(std::time::Duration::from_millis(50));
                        }
                        Err(_) => break,
                    }
                }
            })?;
        Ok(handle)
    }

    #[cfg(windows)]
    fn acquire_windows<F>(
        app_id: &str,
        args: &[String],
        on_secondary_args: F,
    ) -> Result<SingleInstanceStatus, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        // If a primary already owns the pipe, forward arguments and exit.
        if windows_ipc::forward_to_primary(app_id, args) {
            return Ok(SingleInstanceStatus::SecondaryExited);
        }

        let handle = match windows_ipc::create_server(app_id) {
            Some(handle) => handle,
            None => {
                // Another process won the first-instance race after our initial
                // connect attempt; forward to it instead of failing.
                let create_error = std::io::Error::last_os_error();
                if windows_ipc::forward_to_primary(app_id, args) {
                    return Ok(SingleInstanceStatus::SecondaryExited);
                }
                return Err(create_error);
            }
        };

        let running = Arc::new(AtomicBool::new(true));
        let on_args = Arc::new(on_secondary_args);
        let thread = match windows_ipc::spawn_server_listener(handle, Arc::clone(&running), on_args)
        {
            Ok(t) => t,
            Err(error) => {
                windows_ipc::close(handle);
                return Err(error);
            }
        };

        Ok(SingleInstanceStatus::Primary(Self {
            app_id: app_id.to_string(),
            running,
            pipe_handle: handle,
            listener_thread: Some(thread),
        }))
    }

    /// Returns the unique application identifier for this lock.
    pub fn app_id(&self) -> &str {
        &self.app_id
    }
}

impl Drop for SingleInstanceLock {
    fn drop(&mut self) {
        self.running.store(false, Ordering::Relaxed);
        #[cfg(unix)]
        {
            let _ = std::fs::remove_file(&self.socket_path);
        }
        #[cfg(windows)]
        {
            windows_ipc::unblock_server(&self.app_id);
            windows_ipc::close(self.pipe_handle);
        }
        if let Some(thread) = self.listener_thread.take() {
            let _ = thread.join();
        }
    }
}

/// Raw `kernel32` named-pipe transport used by the Windows single-instance path.
///
/// The pipe name is `\\.\pipe\<sanitized-app-id>`. The primary creates a duplex
/// byte-mode server instance and listens for newline-separated CLI arguments;
/// a secondary opens the pipe, writes its arguments, and exits. This module is
/// compile-gated to Windows and is not exercised on other hosts.
#[cfg(windows)]
mod windows_ipc {
    use std::ffi::c_void;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    type Handle = *mut c_void;

    const INVALID_HANDLE_VALUE: Handle = -1isize as Handle;
    const PIPE_ACCESS_DUPLEX: u32 = 0x0000_0003;
    const FILE_FLAG_FIRST_PIPE_INSTANCE: u32 = 0x0008_0000;
    const PIPE_TYPE_BYTE: u32 = 0x0000_0000;
    const PIPE_READMODE_BYTE: u32 = 0x0000_0000;
    const PIPE_WAIT: u32 = 0x0000_0000;
    const PIPE_UNLIMITED_INSTANCES: u32 = 255;
    const GENERIC_WRITE: u32 = 0x4000_0000;
    const OPEN_EXISTING: u32 = 3;
    const ERROR_PIPE_BUSY: u32 = 231;
    const ERROR_PIPE_CONNECTED: u32 = 535;
    const BUFFER_SIZE: u32 = 8192;

    #[link(name = "kernel32")]
    extern "system" {
        fn CreateNamedPipeW(
            name: *const u16,
            open_mode: u32,
            pipe_mode: u32,
            max_instances: u32,
            out_buffer_size: u32,
            in_buffer_size: u32,
            default_timeout: u32,
            security_attributes: *mut c_void,
        ) -> Handle;
        fn ConnectNamedPipe(pipe: Handle, overlapped: *mut c_void) -> i32;
        fn DisconnectNamedPipe(pipe: Handle) -> i32;
        fn ReadFile(
            file: Handle,
            buffer: *mut u8,
            bytes_to_read: u32,
            bytes_read: *mut u32,
            overlapped: *mut c_void,
        ) -> i32;
        fn WriteFile(
            file: Handle,
            buffer: *const u8,
            bytes_to_write: u32,
            bytes_written: *mut u32,
            overlapped: *mut c_void,
        ) -> i32;
        fn CreateFileW(
            name: *const u16,
            desired_access: u32,
            share_mode: u32,
            security_attributes: *mut c_void,
            creation_disposition: u32,
            flags_and_attributes: u32,
            template_file: Handle,
        ) -> Handle;
        fn CloseHandle(object: Handle) -> i32;
        fn GetLastError() -> u32;
        fn LocalFree(h_mem: *mut c_void) -> *mut c_void;
    }

    #[link(name = "advapi32")]
    extern "system" {
        fn ConvertStringSecurityDescriptorToSecurityDescriptorW(
            string_security_descriptor: *const u16,
            string_sd_revision: u32,
            security_descriptor: *mut *mut c_void,
            security_descriptor_size: *mut u32,
        ) -> i32;
    }

    fn pipe_name(app_id: &str) -> Vec<u16> {
        let sanitized: String = app_id
            .chars()
            .map(|ch| {
                if ch.is_ascii_alphanumeric() || ch == '.' || ch == '-' || ch == '_' {
                    ch
                } else {
                    '-'
                }
            })
            .collect();
        format!("\\\\.\\pipe\\{}", sanitized)
            .encode_utf16()
            .chain(std::iter::once(0))
            .collect()
    }

    fn to_handle(value: usize) -> Handle {
        value as Handle
    }

    #[repr(C)]
    struct SecurityAttributes {
        n_length: u32,
        lp_security_descriptor: *mut c_void,
        b_inherit_handle: i32,
    }

    pub(super) fn forward_to_primary(app_id: &str, args: &[String]) -> bool {
        let name = pipe_name(app_id);
        let bytes = super::encode_args(args);

        for _ in 0..40 {
            let handle = unsafe {
                CreateFileW(
                    name.as_ptr(),
                    GENERIC_WRITE,
                    0,
                    std::ptr::null_mut(),
                    OPEN_EXISTING,
                    0,
                    std::ptr::null_mut(),
                )
            };
            if handle != INVALID_HANDLE_VALUE {
                let mut written = 0u32;
                let ok = unsafe {
                    WriteFile(
                        handle,
                        bytes.as_ptr(),
                        bytes.len() as u32,
                        &mut written,
                        std::ptr::null_mut(),
                    )
                };
                unsafe {
                    CloseHandle(handle);
                }
                return ok != 0;
            }
            let error = unsafe { GetLastError() };
            if error != ERROR_PIPE_BUSY {
                return false;
            }
            std::thread::sleep(std::time::Duration::from_millis(25));
        }
        false
    }

    pub(super) fn create_server(app_id: &str) -> Option<usize> {
        let name = pipe_name(app_id);

        // Enforce explicit Win32 DACL: Allow Generic All strictly to Principal Self (PS) / Owner
        let sddl = "D:(A;;GA;;;PS)\0".encode_utf16().collect::<Vec<u16>>();
        let mut p_sd: *mut c_void = std::ptr::null_mut();
        let mut sd_size = 0u32;
        let has_sd = unsafe {
            ConvertStringSecurityDescriptorToSecurityDescriptorW(
                sddl.as_ptr(),
                1, // SDDL_REVISION_1
                &mut p_sd,
                &mut sd_size,
            )
        };

        let mut sa = SecurityAttributes {
            n_length: std::mem::size_of::<SecurityAttributes>() as u32,
            lp_security_descriptor: if has_sd != 0 {
                p_sd
            } else {
                std::ptr::null_mut()
            },
            b_inherit_handle: 0,
        };

        let handle = unsafe {
            CreateNamedPipeW(
                name.as_ptr(),
                PIPE_ACCESS_DUPLEX | FILE_FLAG_FIRST_PIPE_INSTANCE,
                PIPE_TYPE_BYTE | PIPE_READMODE_BYTE | PIPE_WAIT,
                PIPE_UNLIMITED_INSTANCES,
                BUFFER_SIZE,
                BUFFER_SIZE,
                0,
                &mut sa as *mut _ as *mut c_void,
            )
        };

        if !p_sd.is_null() {
            unsafe {
                LocalFree(p_sd);
            }
        }

        if handle == INVALID_HANDLE_VALUE {
            None
        } else {
            Some(handle as usize)
        }
    }

    pub(super) fn unblock_server(app_id: &str) {
        let name = pipe_name(app_id);
        let handle = unsafe {
            CreateFileW(
                name.as_ptr(),
                GENERIC_WRITE,
                0,
                std::ptr::null_mut(),
                OPEN_EXISTING,
                0,
                std::ptr::null_mut(),
            )
        };
        if handle != INVALID_HANDLE_VALUE {
            unsafe {
                CloseHandle(handle);
            }
        }
    }

    pub(super) fn spawn_server_listener<F>(
        handle: usize,
        running: Arc<AtomicBool>,
        on_args: Arc<F>,
    ) -> Result<std::thread::JoinHandle<()>, std::io::Error>
    where
        F: Fn(Vec<String>) + Send + Sync + 'static,
    {
        std::thread::Builder::new()
            .name("strata-single-instance-pipe".to_string())
            .spawn(move || {
                let pipe = to_handle(handle);
                let mut buffer = vec![0u8; BUFFER_SIZE as usize];

                while running.load(Ordering::Relaxed) {
                    let connected = unsafe { ConnectNamedPipe(pipe, std::ptr::null_mut()) };
                    if connected == 0 {
                        let error = unsafe { GetLastError() };
                        if error != ERROR_PIPE_CONNECTED {
                            break;
                        }
                    }

                    if !running.load(Ordering::Relaxed) {
                        unsafe {
                            DisconnectNamedPipe(pipe);
                        }
                        break;
                    }

                    let mut payload = Vec::new();
                    loop {
                        let mut bytes_read = 0u32;
                        let ok = unsafe {
                            ReadFile(
                                pipe,
                                buffer.as_mut_ptr(),
                                buffer.len() as u32,
                                &mut bytes_read,
                                std::ptr::null_mut(),
                            )
                        };
                        if ok == 0 || bytes_read == 0 {
                            break;
                        }
                        payload.extend_from_slice(&buffer[..bytes_read as usize]);
                        if (bytes_read as usize) < buffer.len() {
                            break;
                        }
                    }

                    if !payload.is_empty() {
                        let parsed = super::decode_args(&payload);
                        on_args(parsed);
                    }

                    unsafe {
                        DisconnectNamedPipe(pipe);
                    }
                }
            })
    }

    pub(super) fn close(handle: usize) {
        if handle != 0 {
            unsafe {
                CloseHandle(to_handle(handle));
            }
        }
    }
}