ztmux 3.7.20

A Rust port of tmux — the full terminal multiplexer, server and client
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
// Copyright (c) 2019 Nicholas Marriott <nicholas.marriott@gmail.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
// IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
// OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
use crate::compat::{closefrom, fdforkpty::fdforkpty};
use crate::libc::{
    _exit, SIG_BLOCK, SIG_SETMASK, STDERR_FILENO, STDIN_FILENO, TCSANOW, VERASE, chdir, close,
    execl, execvp, sigfillset, sigprocmask, strrchr, tcgetattr, tcsetattr,
};
#[cfg(feature = "utempter")]
use crate::utempter::utempter_add_record;
use crate::*;
use crate::options_::*;

/// C `vendor/tmux/spawn.c:54`: `static void spawn_log(const char *from, struct spawn_context *sc)`
pub unsafe fn spawn_log(from: &str, sc: *mut spawn_context) {
    unsafe {
        let s = (*sc).s;
        let wl = (*sc).wl;
        let wp0 = (*sc).wp0;
        let name = cmdq_get_name((*sc).item);
        type tmp_type = [u8; 128];
        let mut tmp = MaybeUninit::<tmp_type>::uninit();

        log_debug!("{}: {}, flags={:#x}", from, _s(name), (*sc).flags);

        if !wl.is_null() && !wp0.is_null() {
            _ = xsnprintf_!(
                tmp.as_mut_ptr().cast(),
                size_of::<tmp_type>(),
                "wl={} wp0=%{}",
                (*wl).idx,
                (*wp0).id,
            );
        } else if !wl.is_null() {
            _ = xsnprintf_!(
                tmp.as_mut_ptr().cast(),
                size_of::<tmp_type>(),
                "wl={} wp0=none",
                (*wl).idx,
            );
        } else if !wp0.is_null() {
            _ = xsnprintf_!(
                tmp.as_mut_ptr().cast(),
                size_of::<tmp_type>(),
                "wl=none wp0=%{}",
                (*wp0).id,
            );
        } else {
            _ = xsnprintf_!(
                tmp.as_mut_ptr().cast(),
                size_of::<tmp_type>(),
                "wl=none wp0=none",
            );
        }
        log_debug!(
            "{}: s=${} {} idx={}",
            from,
            (*s).id,
            _s(tmp.as_ptr().cast::<i8>()),
            (*sc).idx
        );
        log_debug!(
            "{}: name={}",
            from,
            _s(if (*sc).name.is_null() {
                c!("none")
            } else {
                (*sc).name
            }),
        );
    }
}

/// C `vendor/tmux/spawn.c:75`: `struct winlink *spawn_window(struct spawn_context *sc, char **cause)`
pub unsafe fn spawn_window(sc: *mut spawn_context, cause: *mut *mut u8) -> *mut winlink {
    unsafe {
        let item = (*sc).item;
        let c = cmdq_get_client(item);
        let s = (*sc).s;
        let mut idx = (*sc).idx;
        let mut w: *mut window;
        let mut wp = null_mut();

        spawn_log("spawn_window", sc);

        // If the window already exists, we are respawning, so destroy all the
        // panes except one.
        if (*sc).flags.intersects(spawn_flags::SPAWN_RESPAWN) {
            w = (*(*sc).wl).window;
            if !(*sc).flags.intersects(SPAWN_KILL) {
                // C `TAILQ_FOREACH` leaves `wp == NULL` when no `break` fires;
                // Rust's `for` would leave `wp` = last pane, so only assign `wp`
                // when a pane with `fd != -1` is actually found (`wp` is still
                // NULL here from its declaration above).
                for wp_ in tailq_foreach::<_, discr_entry>(&raw mut (*w).panes).map(NonNull::as_ptr)
                {
                    if (*wp_).fd != -1 {
                        wp = wp_;
                        break;
                    }
                }
                if !wp.is_null() {
                    *cause = format_nul!("window {}:{} still active", (*s).name, (*(*sc).wl).idx,);
                    return null_mut();
                }
            }

            (*sc).wp0 = tailq_first(&raw mut (*w).panes);
            tailq_remove::<_, discr_entry>(&raw mut (*w).panes, (*sc).wp0);

            layout_free(w, 0);
            window_destroy_panes(w);

            tailq_insert_head::<_, discr_entry>(&raw mut (*w).panes, (*sc).wp0);
            window_pane_resize((*sc).wp0, (*w).sx, (*w).sy);

            layout_init(w, (*sc).wp0);
            (*w).active = null_mut();
            window_set_active_pane(w, (*sc).wp0, 0);
        }

        // Otherwise we have no window so we will need to create one. First
        // check if the given index already exists and destroy it if so.
        if !(*sc).flags.intersects(SPAWN_RESPAWN) && idx != -1 {
            let wl = winlink_find_by_index(&raw mut (*s).windows, idx);
            if !wl.is_null() && !(*sc).flags.intersects(SPAWN_KILL) {
                *cause = format_nul!("index {} in use", idx);
                return null_mut();
            }
            if !wl.is_null() {
                // Can't use session_detach as it will destroy session
                // if this makes it empty.
                (*wl).flags &= !WINLINK_ALERTFLAGS;
                notify_session_window(c"window-unlinked", s, (*wl).window);
                winlink_stack_remove(&raw mut (*s).lastw, wl);
                winlink_remove(&raw mut (*s).windows, wl);

                if (*s).curw == wl {
                    (*s).curw = null_mut();
                    (*sc).flags &= !SPAWN_DETACHED;
                }
            }
        }

        // Then create a window if needed.
        if !(*sc).flags.intersects(SPAWN_RESPAWN) {
            if idx == -1 {
                idx = -1 - options_get_number_((*s).options, "base-index") as i32;
            }
            (*sc).wl = winlink_add(&raw mut (*s).windows, idx);
            if (*sc).wl.is_null() {
                *cause = format_nul!("couldn't add window {}", idx);
                return null_mut();
            }
            let mut sx = 0u32;
            let mut sy = 0u32;
            let mut xpixel = 0u32;
            let mut ypixel = 0u32;
            default_window_size(
                (*sc).tc,
                s,
                null_mut(),
                &raw mut sx,
                &raw mut sy,
                &raw mut xpixel,
                &raw mut ypixel,
                None,
            );
            w = window_create(sx, sy, xpixel, ypixel);
            if w.is_null() {
                winlink_remove(&raw mut (*s).windows, (*sc).wl);
                *cause = format_nul!("couldn't create window {idx}");
                return null_mut();
            }
            if (*s).curw.is_null() {
                (*s).curw = (*sc).wl;
            }
            (*(*sc).wl).session = s;
            (*w).latest = (*sc).tc.cast();
            winlink_set_window((*sc).wl, w);
        } else {
            w = null_mut();
        }
        (*sc).flags |= SPAWN_NONOTIFY;

        // Spawn the pane.
        wp = spawn_pane(sc, cause);
        if wp.is_null() {
            if !(*sc).flags.intersects(SPAWN_RESPAWN) {
                winlink_remove(&raw mut (*s).windows, (*sc).wl);
            }
            return null_mut();
        }

        // Set the name of the new window.
        if !(*sc).flags.intersects(SPAWN_RESPAWN) {
            // Assigning drops the old name CString — no manual free.
            if (*sc).name.is_null() || *(*sc).name == 0 {
                (*w).name = Some(CString::new(default_window_name(w)).unwrap());
            } else {
                // Adopt the owned format_single result.
                (*w).name = Some(CString::from_raw(
                    format_single(item, cstr_to_str((*sc).name), c, s, null_mut(), null_mut())
                        .cast(),
                ));
                options_set_number((*w).options, "automatic-rename", 0);
            }
        }

        // Switch to the new window if required.
        if !(*sc).flags.intersects(SPAWN_DETACHED) {
            session_select(s, (*(*sc).wl).idx);
        }

        // Fire notification if new window.
        if !(*sc).flags.intersects(SPAWN_RESPAWN) {
            notify_session_window(c"window-linked", s, w);
        }

        session_group_synchronize_from(s);
        (*sc).wl
    }
}

/// C `vendor/tmux/spawn.c:201`: `struct window_pane *spawn_pane(struct spawn_context *sc, char **cause)`
pub unsafe fn spawn_pane(sc: *mut spawn_context, cause: *mut *mut u8) -> *mut window_pane {
    unsafe {
        let item = (*sc).item;
        let s = (*sc).s;
        // C `spawn.c:223`: item may be NULL (e.g. editor-spawn path), in which
        // case the target session is `sc->s` and the client is `sc->tc`.
        let (ts, c) = if item.is_null() {
            (s, (*sc).tc)
        } else {
            ((*cmdq_get_target(item)).s, cmdq_get_client(item))
        };
        let w = (*(*sc).wl).window;
        let new_wp: *mut window_pane;
        let child: *mut environ;
        let ee: *mut environ_entry;
        let argv: *mut *mut u8;
        let argvp: *mut *mut u8;
        let argv0: *mut u8;
        let mut cwd: *mut u8;
        let new_cwd: *mut u8;
        let mut cmd: *const u8;
        let argc;
        let mut idx: u32 = 0;
        let mut now: libc::termios = zeroed();
        let hlimit: u32;
        let mut ws: libc::winsize = zeroed();
        let mut set: libc::sigset_t = zeroed();
        let mut oldset: libc::sigset_t = zeroed();
        let key: key_code;

        'complete: {
            spawn_log("spawn_pane", sc);

            if !(*sc).cwd.is_null() {
                cwd = format_single(item, cstr_to_str((*sc).cwd), c, ts, null_mut(), null_mut());
                if *cwd != b'/' {
                    new_cwd =
                        format_nul!("{}/{}", _s(server_client_get_cwd(c, ts)), _s(cwd));
                    free_(cwd);
                    cwd = new_cwd;
                }
            } else if !(*sc).flags.intersects(SPAWN_RESPAWN) {
                cwd = xstrdup(server_client_get_cwd(c, ts)).as_ptr();
            } else {
                cwd = null_mut();
            }

            // If we are respawning then get rid of the old process. Otherwise
            // either create a new cell or assign to the one we are given.
            hlimit = options_get_number_((*s).options, "history-limit") as u32;
            if (*sc).flags.intersects(SPAWN_RESPAWN) {
                if (*(*sc).wp0).fd != -1 && !(*sc).flags.intersects(SPAWN_KILL) {
                    window_pane_index((*sc).wp0, &raw mut idx);
                    *cause = format_nul!(
                        "pane {}:{}.{} still active",
                        (*s).name,
                        (*(*sc).wl).idx,
                        idx
                    );
                    free_(cwd);
                    return null_mut();
                }
                if (*(*sc).wp0).fd != -1 {
                    bufferevent_free((*(*sc).wp0).event);
                    close((*(*sc).wp0).fd);
                }
                window_pane_reset_mode_all((*sc).wp0);
                screen_reinit(&raw mut (*(*sc).wp0).base);
                input_free((*(*sc).wp0).ictx);
                (*(*sc).wp0).ictx = null_mut();
                new_wp = (*sc).wp0;
                (*new_wp).flags &=
                    !(window_pane_flags::PANE_STATUSREADY | window_pane_flags::PANE_STATUSDRAWN);
            } else if (*sc).lc.is_null() {
                new_wp = window_add_pane(w, null_mut(), hlimit, (*sc).flags);
                layout_init(w, new_wp);
            } else {
                new_wp = window_add_pane(w, (*sc).wp0, hlimit, (*sc).flags);
                if (*sc).flags.intersects(SPAWN_ZOOM) {
                    layout_assign_pane((*sc).lc, new_wp, 1);
                } else {
                    layout_assign_pane((*sc).lc, new_wp, 0);
                }
            }
            if (*sc).flags.intersects(SPAWN_FLOATING) {
                (*(*new_wp).layout_cell).flags |= LAYOUT_CELL_FLOATING;
            }

            // If the window is currently zoomed, window_set_active_pane calls
            // window_unzoom which copies back the saved layout cell.
            if (*w).flags.intersects(window_flag::ZOOMED) {
                (*new_wp).saved_layout_cell = (*new_wp).layout_cell;
            }

            // Now we have a pane with nothing running in it ready for the new
            // process. Work out the command and arguments and store the working
            // directory.
            if (*sc).argc == 0 && !(*sc).flags.intersects(SPAWN_RESPAWN) {
                cmd = options_get_string_((*s).options, "default-command");
                if !cmd.is_null() && *cmd != b'\0' {
                    argc = 1;
                    argv = &raw mut cmd as *mut *mut u8;
                } else {
                    argc = 0;
                    argv = null_mut();
                }
            } else {
                argc = (*sc).argc;
                argv = (*sc).argv;
            }
            if !cwd.is_null() {
                // Adopt the owned cwd string; assigning drops the old one.
                (*new_wp).cwd = Some(std::ffi::CString::from_raw(cwd.cast()));
            }

            // Replace the stored arguments if there are new ones. If not, the
            // existing ones will be used (they will only exist for respawn).
            if argc > 0 {
                cmd_free_argv((*new_wp).argc, (*new_wp).argv);
                (*new_wp).argc = argc;
                (*new_wp).argv = cmd_copy_argv(argc, argv);
            }

            // Create an environment for this pane.
            child = environ_for_session(s, 0);
            if !(*sc).environ.is_null() {
                environ_copy((*sc).environ, child);
            }
            environ_set!(
                child,
                c!("TMUX_PANE"),
                environ_flags::empty(),
                "%{}",
                (*new_wp).id,
            );

            // Then the PATH environment variable. The session one is replaced from
            // the client if there is one because otherwise running "tmux new
            // myprogram" wouldn't work if myprogram isn't in the session's path.
            if !c.is_null() && (*c).session.is_null() {
                // only unattached clients
                ee = environ_find((*c).environ, c!("PATH"));
                if !ee.is_null() {
                    environ_set!(
                        child,
                        c!("PATH"),
                        environ_flags::empty(),
                        "{}",
                        _s((*ee).value_ptr())
                    );
                }
            }
            if environ_find(child, c!("PATH")).is_null() {
                environ_set!(
                    child,
                    c!("PATH"),
                    environ_flags::empty(),
                    "{}",
                    _s(_PATH_DEFPATH)
                );
            }

            // Then the shell. If respawning, use the old one.
            if !(*sc).flags.intersects(SPAWN_RESPAWN) {
                let mut tmp = options_get_string_((*s).options, "default-shell");
                if !checkshell_(tmp) {
                    tmp = _PATH_BSHELL;
                }
                // Assigning drops the old shell CString — no manual free.
                (*new_wp).shell = Some(std::ffi::CStr::from_ptr(tmp.cast()).to_owned());
            }
            environ_set!(
                child,
                c!("SHELL"),
                environ_flags::empty(),
                "{}",
                _s((*new_wp).shell_ptr())
            );

            // Log the arguments we are going to use.
            log_debug!("spawn_pane: shell={}", _s((*new_wp).shell_ptr()));
            if (*new_wp).argc != 0 {
                let cp = cmd_stringify_argv((*new_wp).argc, (*new_wp).argv);
                log_debug!("spawn_pane: cmd={}", cp);
            }
            log_debug!("spawn_pane: cwd={}", _s((*new_wp).cwd_ptr()));
            cmd_log_argv!((*new_wp).argc, (*new_wp).argv, "spawn_pan");
            environ_log!(child, "spawn_pan: environment ");

            // Initialize the window size.
            memset0(&raw mut ws);
            ws.ws_col = screen_size_x(&raw mut (*new_wp).base) as u16;
            ws.ws_row = screen_size_y(&raw mut (*new_wp).base) as u16;
            ws.ws_xpixel = ((*w).xpixel * ws.ws_col as u32) as u16;
            ws.ws_ypixel = ((*w).ypixel * ws.ws_row as u32) as u16;

            // Resolve $HOME before fork: find_home() reads the environment
            // under std's ENV_LOCK, which is not safe to touch in the forked
            // child (see job.rs). The child chdir()s to this if cwd fails.
            let home: *const u8 = find_home().map_or(null(), |h| h.as_ptr().cast());

            // Block signals until fork has completed.
            sigfillset(&raw mut set);
            sigprocmask(SIG_BLOCK, &raw mut set, &raw mut oldset);

            // If the command is empty, don't fork a child process.
            if (*sc).flags.intersects(SPAWN_EMPTY) {
                (*new_wp).flags |= window_pane_flags::PANE_EMPTY;
                (*new_wp).base.mode &= !mode_flag::MODE_CURSOR;
                (*new_wp).base.mode |= mode_flag::MODE_CRLF;
                break 'complete;
            }

            // Fork the new process.
            (*new_wp).pid = fdforkpty(
                PTM_FD,
                &raw mut (*new_wp).fd,
                (*new_wp).tty.as_mut_ptr(),
                null_mut(),
                &raw mut ws,
            );
            if (*new_wp).pid == -1 {
                *cause = format_nul!("fork failed: {}", strerror(errno!()));
                (*new_wp).fd = -1;
                if !(*sc).flags.intersects(SPAWN_RESPAWN) {
                    server_client_remove_pane(new_wp);
                    layout_close_pane(new_wp);
                    window_remove_pane(w, new_wp);
                }
                sigprocmask(SIG_SETMASK, &raw mut oldset, null_mut());
                environ_free(child);
                return null_mut();
            }

            // In the parent process, everything is done now.
            if (*new_wp).pid != 0 {
                #[cfg(all(feature = "systemd", feature = "cgroups"))]
                {
                    // Move the child process into a new cgroup for systemd-oomd
                    // isolation.
                    if (systemd_move_pid_to_new_cgroup((*new_wp).pid, cause) < 0) {
                        log_debug!(
                            "{}: moving pane to new cgroup failed: {}",
                            _s(__func__),
                            _s(*cause)
                        );
                        free_(*cause);
                    }
                }
                break 'complete;
            }

            // Child process. Change to the working directory or home if that
            // fails.
            // Async-signal-safe chdir only (libc chdir, not
            // std::env::set_current_dir which allocates); home was resolved in
            // the parent before fork.
            if chdir((*new_wp).cwd_ptr().cast()) == 0 {
                environ_set!(
                    child,
                    c!("PWD"),
                    environ_flags::empty(),
                    "{}",
                    _s((*new_wp).cwd_ptr())
                );
            } else if !home.is_null() && chdir(home.cast()) == 0 {
                environ_set!(
                    child,
                    c!("PWD"),
                    environ_flags::empty(),
                    "{}",
                    _s(home)
                );
            } else if chdir(c!("/").cast()) == 0 {
                environ_set!(child, c!("PWD"), environ_flags::empty(), "/");
            } else {
                fatal("chdir failed");
            }

            // Update terminal escape characters from the session if available and
            // force VERASE to tmux's backspace.
            if tcgetattr(STDIN_FILENO, &raw mut now) != 0 {
                _exit(1);
            }
            if !(*s).tio.is_null() {
                memcpy__(now.c_cc.as_mut_ptr(), (*(*s).tio).c_cc.as_ptr());
            }
            key = options_get_number_(GLOBAL_OPTIONS, "backspace") as u64;
            if key >= 0x7f {
                now.c_cc[VERASE] = b'\x7f';
            } else {
                now.c_cc[VERASE] = key as u8;
            }
            #[cfg(feature = "iutf8")]
            {
                now.c_iflag |= IUTF8;
            }
            if tcsetattr(STDIN_FILENO, TCSANOW, &now) != 0 {
                _exit(1);
            }

            // Clean up file descriptors and signals and update the environment.
            proc_clear_signals(SERVER_PROC, 1);
            closefrom(STDERR_FILENO + 1);
            sigprocmask(SIG_SETMASK, &raw mut oldset, null_mut());
            log_close();
            environ_push(child);

            // If given multiple arguments, use execvp(). Copy the arguments to
            // ensure they end in a NULL.
            if (*new_wp).argc != 0 && (*new_wp).argc != 1 {
                argvp = cmd_copy_argv((*new_wp).argc, (*new_wp).argv);
                execvp((*argvp).cast(), argvp.cast());
                _exit(1);
            }

            // If one argument, pass it to $SHELL -c. Otherwise create a login
            // shell.
            let cp = strrchr((*new_wp).shell_ptr(), b'/' as i32);
            if (*new_wp).argc == 1 {
                let tmp = *(*new_wp).argv;
                argv0 = if !cp.is_null() && *cp.add(1) != b'\0' {
                    format_nul!("{}", _s(cp.add(1)))
                } else {
                    format_nul!("{}", _s((*new_wp).shell_ptr()))
                };
                execl(
                    (*new_wp).shell_ptr().cast(),
                    argv0.cast(),
                    c!("-c"),
                    tmp,
                    null_mut::<u8>(),
                );
                _exit(1);
            }
            argv0 = if !cp.is_null() && *cp.add(1) != b'\0' {
                format_nul!("-{}", _s(cp.add(1)))
            } else {
                format_nul!("-{}", _s((*new_wp).shell_ptr()))
            };
            execl((*new_wp).shell_ptr().cast(), argv0.cast(), null_mut::<u8>());
            _exit(1);
        }

        // complete:
        #[cfg(feature = "utempter")]
        {
            if !(*new_wp).flags.intersects(window_pane_flags::PANE_EMPTY) {
                let cp = format_nul!("tmux({}).%{}", std::process::id() as c_long, (*new_wp).id);
                utempter_add_record((*new_wp).fd, cp);
                kill(std::process::id() as i32, SIGCHLD);
                free_(cp);
            }
        }

        (*new_wp).flags &= !window_pane_flags::PANE_EXITED;

        sigprocmask(SIG_SETMASK, &raw mut oldset, null_mut());
        window_pane_set_event(new_wp);

        environ_free(child);

        if (*sc).flags.intersects(SPAWN_RESPAWN) {
            return new_wp;
        }
        if !(*sc).flags.intersects(SPAWN_DETACHED) || (*w).active.is_null() {
            if (*sc).flags.intersects(SPAWN_NONOTIFY) {
                window_set_active_pane(w, new_wp, 0);
            } else {
                window_set_active_pane(w, new_wp, 1);
            }
        }
        if !(*sc).flags.intersects(SPAWN_NONOTIFY) {
            notify_window(c"window-layout-changed", w);
        }

        new_wp
    }
}