websocat 4.0.0-alpha2

Command-line client for web sockets, like netcat/curl/socat for ws://.
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
use std::{ffi::OsString, io::ErrorKind, pin::Pin, task::Poll};

use crate::scenario_executor::utils1::TaskHandleExt2;
use rhai::{Engine, FnPtr, NativeCallContext};
use tokio::{
    io::AsyncWrite,
    process::{Child, ChildStdin, Command},
};
use tracing::{debug, warn};

use crate::scenario_executor::{
    scenario::{callback_and_continue, ScenarioAccess},
    types::{Handle, StreamRead, StreamSocket, StreamWrite, Task},
};

use super::{
    types::Hangup,
    utils1::{ExtractHandleOrFail, HandleExt, RhResult, SimpleErr},
};

//@ Prepare subprocess, setting up executable name. See `Command::` methods for further steps.
fn subprocess_new(program_name: String) -> Handle<Command> {
    Some(Command::new(program_name)).wrap()
}

//@ Prepare subprocess, setting up possibly non-UTF8 executable name.  See `Command::` methods for further steps.
fn subprocess_new_osstr(program_name: OsString) -> Handle<Command> {
    Some(Command::new(program_name)).wrap()
}

//@ Add one command line argument to the array
fn subprocess_arg(ctx: NativeCallContext, cmd: &mut Handle<Command>, arg: String) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;
    c.arg(arg);
    cmd.put(c);
    Ok(())
}

//@ Add one possibly non-UTF8 command line argument to the array
fn subprocess_arg_osstr(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    arg: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;
    c.arg(arg);
    cmd.put(c);
    Ok(())
}

//@ Configure what to do with subprocess's stdin, stdout and stderr
//@
//@ Each numeric argument accepts the following values:
//@ * `0` meaning the fd will be /dev/null-ed.
//@ * `1` meaning leave it connected to Websocat's fds.
//@ * `2` meaning we can capture process's input or output.
fn subprocess_configure_fds(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    stdin: i64,
    stdout: i64,
    stderr: i64,
) -> RhResult<()> {
    use std::process::Stdio;
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;
    let gets = |x: i64| -> RhResult<Stdio> {
        Ok(match x {
            0 => Stdio::null(),
            1 => Stdio::inherit(),
            2 => Stdio::piped(),
            _ => return Err(ctx.err("Invalid value for subprocess_configure_fds argument")),
        })
    };
    let (si, so, se) = (gets(stdin)?, gets(stdout)?, gets(stderr)?);

    c.stdin(si).stdout(so).stderr(se);

    cmd.put(c);
    Ok(())
}

//@ Execute the prepared subprocess and wait for its exit code
//@ Callback receives exit code or `-1` meaning that starting failed
//@ or `-2` meaning the process exited because of signal
fn subprocess_execute_for_status(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    continuation: FnPtr,
) -> RhResult<Handle<Task>> {
    let mut c = ctx.lutbarm(cmd)?;
    let the_scenario = ctx.get_scenario()?;
    Ok(async move {
        debug!("starting subprocess");

        let s = c.status().await;

        let ret = match s {
            Ok(x) => match x.code() {
                Some(x) => x.into(),
                None => -2,
            },
            Err(e) => {
                warn!("Failed to execute subprocess: {e}");
                -1
            }
        };

        callback_and_continue::<(i64,)>(the_scenario, continuation, (ret,)).await;
        Ok(())
    }
    .wrap())
}

//@ Execute the prepared subprocess and wait for its exit, storing
//@ output of stdout and stderr in memory.
//@ Status code the callback receives follows similar rules as in `subprocess_execute_for_status`.
//@ Second and third arguments of the callback are stdout and stderr respectively.
fn subprocess_execute_for_output(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    continuation: FnPtr,
) -> RhResult<Handle<Task>> {
    let mut c = ctx.lutbarm(cmd)?;
    let the_scenario = ctx.get_scenario()?;
    Ok(async move {
        debug!("starting subprocess");

        let o = c.output().await;

        let (code, stdout, stderr) = match o {
            Ok(x) => {
                let code = match x.status.code() {
                    Some(x) => x.into(),
                    None => -2,
                };
                (code, x.stdout, x.stderr)
            }
            Err(e) => {
                warn!("Failed to execute subprocess: {e}");
                (-1, vec![], vec![])
            }
        };

        callback_and_continue::<(i64, Vec<u8>, Vec<u8>)>(
            the_scenario,
            continuation,
            (code, stdout, stderr),
        )
        .await;
        Ok(())
    }
    .wrap())
}

struct StdinWrapper(Option<ChildStdin>);

impl AsyncWrite for StdinWrapper {
    fn poll_write(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &[u8],
    ) -> Poll<Result<usize, std::io::Error>> {
        if let Some(ref mut x) = self.get_mut().0 {
            Pin::new(x).poll_write(cx, buf)
        } else {
            Poll::Ready(Err(ErrorKind::BrokenPipe.into()))
        }
    }

    fn poll_flush(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<(), std::io::Error>> {
        if let Some(ref mut x) = self.get_mut().0 {
            Pin::new(x).poll_flush(cx)
        } else {
            Poll::Ready(Err(ErrorKind::BrokenPipe.into()))
        }
    }

    fn poll_shutdown(
        self: std::pin::Pin<&mut Self>,
        _cx: &mut std::task::Context<'_>,
    ) -> Poll<Result<(), std::io::Error>> {
        if let Some(x) = self.get_mut().0.take() {
            drop(x);
            Poll::Ready(Ok(()))
        } else {
            Poll::Ready(Err(ErrorKind::BrokenPipe.into()))
        }
    }

    fn poll_write_vectored(
        self: Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        bufs: &[std::io::IoSlice<'_>],
    ) -> Poll<Result<usize, std::io::Error>> {
        if let Some(ref mut x) = self.get_mut().0 {
            Pin::new(x).poll_write_vectored(cx, bufs)
        } else {
            Poll::Ready(Err(ErrorKind::BrokenPipe.into()))
        }
    }

    fn is_write_vectored(&self) -> bool {
        if let Some(ref x) = self.0 {
            x.is_write_vectored()
        } else {
            true
        }
    }
}

//@ Convert the child process handle to a Stream Socket of its stdin and stdout (but not stderr).
//@ In case of non-piped (`2`) fds, the resulting socket would be incomplete.
fn child_socket(
    ctx: NativeCallContext,
    chld: &mut Handle<Child>,
) -> RhResult<Handle<StreamSocket>> {
    let (mut c, chld) = ctx.lutbar2m(chld)?;
    let s = StreamSocket {
        read: c.stdout.take().map(|x| StreamRead {
            reader: Box::pin(x),
            prefix: bytes::BytesMut::new(),
        }),
        write: c.stdin.take().map(|x| StreamWrite {
            writer: Box::pin(StdinWrapper(Some(x))),
        }),
        close: None,
        fd: None,
    };

    debug!(s=?s, "subprocess socket");

    chld.put(c);
    Ok(Some(s).wrap())
}

//@ Obtain a Hangup handle that resovles when child process terminates.
//@ `Child` instance cannot be used after this.
fn child_wait(ctx: NativeCallContext, chld: &mut Handle<Child>) -> RhResult<Handle<Hangup>> {
    let mut c = ctx.lutbarm(chld)?;
    let s: Hangup = Box::pin(async move {
        match c.wait().await {
            Ok(x) => {
                debug!("child process exited with status {x}")
            }
            Err(e) => {
                warn!("Failed to wait for a child process: {e}")
            }
        }
    });

    Ok(Some(s).wrap())
}

//@ Simplified function to just execute a command line
fn simplified_exec(ctx: NativeCallContext, cmdline: &str) -> RhResult<Handle<Hangup>> {
    let mut cmd: Command;
    #[cfg(windows)]
    {
        cmd = Command::new("cmd");
        cmd.arg("/C");
        cmd.raw_arg(cmdline);
    }
    #[cfg(not(windows))]
    {
        cmd = Command::new("sh");
        cmd.arg("-c");
        cmd.arg(cmdline);
    }

    let mut c = match cmd.spawn() {
        Ok(x) => {
            debug!("spawned subprocess");
            x
        }
        Err(e) => {
            warn!("Process spawning failed: {e}");
            return Err(ctx.err("Failed to spawn the process"));
        }
    };

    let s: Hangup = Box::pin(async move {
        match c.wait().await {
            Ok(x) => {
                debug!("child process exited with status {x}")
            }
            Err(e) => {
                warn!("Failed to wait for a child process: {e}")
            }
        }
    });

    Ok(Some(s).wrap())
}

//@ Terminate a child process.
//@ `Child` instance cannot be used after this.
fn child_kill(ctx: NativeCallContext, chld: &mut Handle<Child>) -> RhResult<Handle<Hangup>> {
    let mut c = ctx.lutbarm(chld)?;
    let s: Hangup = Box::pin(async move {
        match c.kill().await {
            Ok(()) => {
                debug!("child process terminated")
            }
            Err(e) => {
                warn!("Failed to terminate a child process: {e}")
            }
        }
    });

    Ok(Some(s).wrap())
}

//@ Take stderr handle as a Stream Reader (i.e. half-socket).
//@ In case of non-piped (`2`) fds, the handle would be null
fn child_take_stderr(
    ctx: NativeCallContext,
    chld: &mut Handle<Child>,
) -> RhResult<Handle<StreamRead>> {
    let (mut c, chld) = ctx.lutbar2m(chld)?;

    let s = c.stderr.take().map(|x| StreamRead {
        reader: Box::pin(x),
        prefix: bytes::BytesMut::new(),
    });

    chld.put(c);
    Ok(s.wrap())
}

//@ Spawn the prepared subprocess. What happens next depends on used `Child::` methods.
fn subprocess_spawn(ctx: NativeCallContext, cmd: &mut Handle<Command>) -> RhResult<Handle<Child>> {
    let mut c = ctx.lutbarm(cmd)?;
    match c.spawn() {
        Ok(x) => {
            debug!("spawned subprocess");
            Ok(Some(x).wrap())
        }
        Err(e) => {
            warn!("Process spawning failed: {e}");
            Err(ctx.err("Failed to spawn the process"))
        }
    }
}

//@ Substitude Websocat process with the prepared command, abandoning other connections if they exist.
#[cfg(unix)]
fn subprocess_execve(ctx: NativeCallContext, cmd: &mut Handle<Command>) -> RhResult<Handle<Child>> {
    use std::os::unix::process::CommandExt;

    use tracing::error;
    let c = ctx.lutbarm(cmd)?;
    let mut c = c.into_std();
    debug!("Substituting Websocat process with a command");
    let e = c.exec();
    error!("Failed to execve: {e}. Exiting anyway");
    std::process::exit(127)
}

//@ Add literal, unescaped text to Windows's command line
#[allow(unused)]
fn subprocess_raw_windows_arg(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    arg: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    #[cfg(not(windows))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(windows)]
    {
        c.raw_arg(arg);
    }

    cmd.put(c);
    Ok(())
}

//@ Add or set environtment variable for the subprocess
fn subprocess_env(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    key: String,
    value: String,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.env(key, value);

    cmd.put(c);
    Ok(())
}

//@ Add or set environtment variable for the subprocess (possibly non-UTF8)
fn subprocess_env_osstr(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    key: OsString,
    value: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.env(key, value);

    cmd.put(c);
    Ok(())
}

//@ Add or set environtment variable for the subprocess.
fn subprocess_env_remove(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    key: String,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.env_remove(key);

    cmd.put(c);
    Ok(())
}

//@ Add or set environtment variable for the subprocess.
fn subprocess_env_remove_osstr(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    key: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.env_remove(key);

    cmd.put(c);
    Ok(())
}

//@ Clear all environment variables for the subprocess.
fn subprocess_env_clear(ctx: NativeCallContext, cmd: &mut Handle<Command>) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.env_clear();

    cmd.put(c);
    Ok(())
}

//@ Change current directory for the subprocess.
fn subprocess_chdir(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    dir: String,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.current_dir(dir);

    cmd.put(c);
    Ok(())
}

//@ Change current directory for the subprocess.
fn subprocess_chdir_osstr(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    dir: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    c.current_dir(dir);

    cmd.put(c);
    Ok(())
}

//@ Set Windows's process creation flags.
#[allow(unused)]
fn subprocess_windows_creation_flags(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    flags: i64,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    let flags: u32 = flags as u32;
    #[cfg(not(windows))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(windows)]
    {
        c.creation_flags(flags);
    }

    cmd.put(c);
    Ok(())
}

//@ Set subprocess's uid on Unix.
#[allow(unused)]
fn subprocess_uid(ctx: NativeCallContext, cmd: &mut Handle<Command>, uid: i64) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    let x: u32 = uid as u32;
    #[cfg(not(unix))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(unix)]
    {
        c.uid(x);
    }

    cmd.put(c);
    Ok(())
}

//@ Set subprocess's uid on Unix.
#[allow(unused)]
fn subprocess_gid(ctx: NativeCallContext, cmd: &mut Handle<Command>, gid: i64) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    let x: u32 = gid as u32;
    #[cfg(not(unix))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(unix)]
    {
        c.gid(x);
    }

    cmd.put(c);
    Ok(())
}

//@ Override process's name / zeroeth command line argument on Unix.
#[allow(unused)]
fn subprocess_arg0(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    arg0: String,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;
    #[cfg(not(unix))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(unix)]
    {
        c.arg0(arg0);
    }

    cmd.put(c);
    Ok(())
}

//@ Override process's name / zeroeth command line argument on Unix.
#[allow(unused)]
fn subprocess_arg0_osstr(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    arg0: OsString,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;
    #[cfg(not(unix))]
    {
        return Err(ctx.err("This function is not available on this platform"));
    }

    #[cfg(unix)]
    {
        c.arg0(arg0);
    }

    cmd.put(c);
    Ok(())
}

//@ `dup2` specified file descriptor over specified file descriptor numbers in the executed process
#[cfg(unix)]
fn subprocess_dup2(
    ctx: NativeCallContext,
    cmd: &mut Handle<Command>,
    source_fd: i64,
    destination_fds: rhai::Dynamic,
    set_to_blocking: bool,
) -> RhResult<()> {
    let (mut c, cmd) = ctx.lutbar2m(cmd)?;

    use libc::{c_int, dup2, fcntl, FD_CLOEXEC, F_GETFD, F_GETFL, F_SETFD, F_SETFL, O_NONBLOCK};

    let src_fd = source_fd as c_int;

    const MAX_DEST_FDS: usize = 16;

    let mut dest_fds: [c_int; MAX_DEST_FDS] = [-1; MAX_DEST_FDS];

    let destination_fds = destination_fds.as_array_ref()?;

    if destination_fds.len() > MAX_DEST_FDS {
        return Err(ctx.err("Too many destination file descriptors in subprocess_dup2"));
    }

    for (a, b) in destination_fds.iter().zip(dest_fds.iter_mut()) {
        let x = a.as_int()?;
        *b = x as c_int;
    }

    // # Safety
    // No attempt to check if supplied source or target file descriptors are OK is made,
    // it is responsibility of end user
    unsafe {
        c.pre_exec(move || {
            let mut ok = true;
            for x in dest_fds {
                if x == -1 {
                    continue;
                }

                if x != src_fd {
                    if -1 == dup2(src_fd, x) {
                        ok = false;
                        break;
                    }
                } else {
                    // Force file descriptor that is already in the needed slot to be inheritable
                    let mut flags = fcntl(x, F_GETFD, 0);
                    if flags == -1 {
                        ok = false;
                        break;
                    }
                    flags &= !FD_CLOEXEC;
                    if -1 == fcntl(x, F_SETFD, flags) {
                        ok = false;
                        break;
                    }
                }

                if set_to_blocking {
                    let mut flags = fcntl(x, F_GETFL, 0);
                    if flags == -1 {
                        ok = false;
                        break;
                    }
                    flags &= !O_NONBLOCK;
                    if -1 == fcntl(x, F_SETFL, flags) {
                        ok = false;
                        break;
                    }
                }
            }
            if !ok {
                Err(std::io::ErrorKind::Other.into())
            } else {
                Ok(())
            }
        })
    };
    cmd.put(c);

    Ok(())
}

pub fn register(engine: &mut Engine) {
    engine.register_fn("subprocess_new", subprocess_new);
    engine.register_fn("subprocess_new_osstr", subprocess_new_osstr);
    engine.register_fn("arg", subprocess_arg);
    engine.register_fn("arg_osstr", subprocess_arg_osstr);
    engine.register_fn("configure_fds", subprocess_configure_fds);
    engine.register_fn("execute_for_status", subprocess_execute_for_status);
    engine.register_fn("execute_for_output", subprocess_execute_for_output);
    engine.register_fn("execute", subprocess_spawn);
    #[cfg(unix)]
    engine.register_fn("execve", subprocess_execve);
    engine.register_fn("socket", child_socket);
    engine.register_fn("take_stderr", child_take_stderr);
    engine.register_fn("wait", child_wait);
    engine.register_fn("kill", child_kill);
    engine.register_fn("raw_windows_arg", subprocess_raw_windows_arg);

    engine.register_fn("env", subprocess_env);
    engine.register_fn("env_osstr", subprocess_env_osstr);
    engine.register_fn("env_remove", subprocess_env_remove);
    engine.register_fn("env_remove_osstr", subprocess_env_remove_osstr);
    engine.register_fn("env_clear", subprocess_env_clear);
    engine.register_fn("chdir", subprocess_chdir);
    engine.register_fn("chdir_osstr", subprocess_chdir_osstr);
    engine.register_fn("windows_creation_flags", subprocess_windows_creation_flags);
    engine.register_fn("uid", subprocess_uid);
    engine.register_fn("gid", subprocess_gid);
    engine.register_fn("arg0", subprocess_arg0);
    engine.register_fn("arg0_osstr", subprocess_arg0_osstr);

    #[cfg(unix)]
    engine.register_fn("dup2", subprocess_dup2);

    engine.register_fn("system", simplified_exec);
}