VirtualBox

Changeset 3444 in kBuild


Ignore:
Timestamp:
Sep 10, 2020 2:08:18 PM (5 years ago)
Author:
bird
Message:

kash: when duplicateing a fdtab, don't heed close-on-exec on windows or in non-forked mode. Close files in sh_execve before waiting, but leave the tracefd open.

Location:
trunk/src/kash
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kash/shfile.c

    r3439 r3444  
    240240    if (fdMin >= SHFILE_MAX)
    241241    {
     242        TRACE2((NULL, "shfile_insert: fdMin=%d is out of bounds; native=%p\n", fdMin, native));
     243        shfile_native_close(native, oflags);
    242244        errno = EMFILE;
    243245        return -1;
     
    247249    {
    248250        int e = errno;
     251        TRACE2((NULL, "shfile_insert: F_SETFD failed %d; native=%p\n", e, native));
    249252        close((int)native);
    250253        errno = e;
     
    277280        pfdtab->tab[fd].shflags = shflags;
    278281        pfdtab->tab[fd].native = native;
     282        TRACE2((NULL, "shfile_insert: #%d: native=%p oflags=%#x shflags=%#x\n", fd, native, oflags, shflags));
    279283    }
    280284    else
     
    847851                    else
    848852                    {
    849 # ifdef SH_FORKED_MODE
     853# if K_OS == K_OS_WINDOWS
     854#  ifdef SH_FORKED_MODE
    850855                        KBOOL const cox = !!(src->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC);
    851 # else
    852                         KBOOL const cox = !!(src->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC); /// @todo K_TRUE
     856#  else
     857                        KBOOL const cox = K_TRUE;
     858#  endif
    853859# endif
    854860                        *dst = *src;
     
    859865                                            (HANDLE *)&dst->native,
    860866                                            0,
    861                                             cox ? FALSE : TRUE /* bInheritHandle */,
     867                                            FALSE /* bInheritHandle */,
    862868                                            DUPLICATE_SAME_ACCESS))
    863869                            TRACE2((NULL, "shfile_init: %d (%#x, %#x) %p (was %p)\n",
     
    10541060 * @returns Pointer to CRT data if prepare is 1, NULL if prepare is 0.
    10551061 * @param   pfdtab  The file descriptor table.
    1056  * @param   prepare Which call, 1 if before and 0 if after.
     1062 * @param   prepare Which call, 1 if before, 0 if after and success, -1 if after on failure.
    10571063 * @param   sizep   Where to store the size of the returned data.
    10581064 * @param   hndls   Where to store the three standard handles.
     
    10741080        count--;
    10751081
    1076     if (prepare)
     1082    if (prepare > 0)
    10771083    {
    10781084        size_t      cbData = sizeof(int) + count * (1 + sizeof(HANDLE));
     
    11271133    else
    11281134    {
     1135        shfile *file = pfdtab->tab;
    11291136        assert(!hndls);
    11301137        assert(!sizep);
    11311138        i = count;
    1132         while (i-- > 0)
    1133         {
    1134             if (    pfdtab->tab[i].fd == i
    1135                 &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
    1136                 shfile_set_inherit_win(&pfdtab->tab[i], 0);
    1137         }
     1139        if (prepare == 0)
     1140            for (i = 0; i < count; i++, file++)
     1141            {
     1142                if (   file->fd == i
     1143                    && !(file->shflags & SHFILE_FLAGS_TRACE))
     1144                {
     1145                    shfile_native_close(file->native, file->oflags);
     1146
     1147                    file->fd = -1;
     1148                    file->oflags = 0;
     1149                    file->shflags = 0;
     1150                    file->native = -1;
     1151                }
     1152            }
     1153        else
     1154            for (i = 0; i < count; i++, file++)
     1155                if (    file->fd == i
     1156                    &&  !(file->shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
     1157                    shfile_set_inherit_win(file, 0);
    11381158        pvRet = NULL;
    11391159    }
     
    19451965
    19461966    TRACE2((NULL, "shfile_cloexec(%d, %d) -> %d [%d]\n", fd, closeit, rc, errno));
     1967    return rc;
     1968}
     1969
     1970/**
     1971 * Sets the SHFILE_FLAGS_TRACE flag.
     1972 */
     1973int shfile_set_trace(shfdtab *pfdtab, int fd)
     1974{
     1975    int         rc;
     1976#ifdef SHFILE_IN_USE
     1977    shmtxtmp    tmp;
     1978    shfile     *file = shfile_get(pfdtab, fd, &tmp);
     1979    if (file)
     1980    {
     1981        file->shflags |= SHFILE_FLAGS_TRACE;
     1982        shfile_put(pfdtab, file, &tmp);
     1983        rc = 0;
     1984    }
     1985    else
     1986        rc = -1;
     1987#else
     1988    rc = 0;
     1989#endif
     1990
     1991    TRACE2((NULL, "shfile_set_trace(%d) -> %d\n", fd, rc));
    19471992    return rc;
    19481993}
  • trunk/src/kash/shfile.h

    r3439 r3444  
    112112 */
    113113#define SHFILE_FLAGS_CLOSE_ON_EXEC      0x0001
     114#define SHFILE_FLAGS_TRACE              0x0002  /**< The 'trace' file, keep open after execve. */
    114115#define SHFILE_FLAGS_TYPE_MASK          0x00f0
    115116#define SHFILE_FLAGS_FILE               0x0000
     
    154155int shfile_isatty(shfdtab *, int);
    155156int shfile_cloexec(shfdtab *, int, int);
     157int shfile_set_trace(shfdtab *, int);
    156158int shfile_ioctl(shfdtab *, int, unsigned long, void *);
    157159#if defined(_MSC_VER) || defined(__OS2__)
  • trunk/src/kash/shinstance.c

    r3442 r3444  
    4444#include "error.h"
    4545#include "memalloc.h"
     46#include "redir.h"
    4647#include "shell.h"
    4748#include "trap.h"
     
    18411842                           &ProcInfo);
    18421843
    1843         shfile_exec_win(&psh->fdtab, 0 /* done */, NULL, NULL);
     1844        shfile_exec_win(&psh->fdtab, rc ? 0 /* done */ : -1 /* done but failed */, NULL, NULL);
    18441845#ifndef SH_FORKED_MODE
    18451846        shmtx_leave(&g_sh_exec_mtx, &tmp);
  • trunk/src/kash/show.c

    r3438 r3444  
    522522                }
    523523                shfile_cloexec(&psh->fdtab, psh->tracefd, 1 /* close it */);
     524                shfile_set_trace(&psh->fdtab, psh->tracefd);
    524525        }
    525526        if (psh->tracefd == -1) {
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette