Skip to main content

closemn

Function closemn 

Source
pub fn closemn(mfds: &mut [Option<Box<multio>>; 10], fd: i32, type_: i32)
Expand description

Port of static void closemn(struct multio **mfds, int fd, int type) from Src/exec.c:2273.

C body (abridged — the meat is the fork-into-tee-or-cat child):

if (fd >= 0 && mfds[fd] && mfds[fd]->ct >= 2) {
    struct multio *mn = mfds[fd];
    char buf[TCBUFSIZE]; int len, i;
    pid_t pid; struct timespec bgtime;
    child_block();
    if ((pid = zfork(&bgtime))) {
        for (i = 0; i < mn->ct; i++) zclose(mn->fds[i]);
        zclose(mn->pipe);
        if (pid == -1) { mfds[fd] = NULL; child_unblock(); return; }
        mn->ct = 1; mn->fds[0] = fd;
        addproc(pid, NULL, 1, &bgtime, -1, -1);
        child_unblock(); return;
    }
    /* pid == 0 (child) */
    opts[INTERACTIVE] = 0;
    dont_queue_signals();
    child_unblock();
    closeallelse(mn);
    if (mn->rflag) {
        /* tee process: read mn->pipe, write each mn->fds[i] */
    } else {
        /* cat process: read each mn->fds[i], write mn->pipe */
    }
    _exit(0);
} else if (fd >= 0 && type == REDIR_CLOSE)
    mfds[fd] = NULL;

Success-path close of a multio. For ct>=2 (multiple-output redirection), forks a tee/cat child that proxies bytes between the original fd and the per-output fds. Single-output multios (ct=1) skip the fork entirely and just clear the slot.

c:2299 — addproc(pid, NULL, 1, &bgtime, -1, -1) records the tee/cat child in the current job’s auxprocs.