Changeset 2650 for trunk/src


Ignore:
Timestamp:
Sep 9, 2012, 6:06:32 PM (13 years ago)
Author:
bird
Message:

kash/shfile.c: Fixed console inherit proble on windows. Fixed CWD slashes on windows and OS/2.

File:
1 edited

Legend:

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

    r2647 r2650  
    570570    {
    571571#ifdef SHFILE_IN_USE
     572        /* Get CWD with unix slashes. */
    572573        char buf[SHFILE_MAX_PATH];
    573574        if (getcwd(buf, sizeof(buf)))
    574575        {
     576# if K_OS == K_OS_WINDOWS || K_OS == K_OS_OS2
     577            char *pszSlash = strchr(buf, '\\');
     578            while (pszSlash)
     579            {
     580                *pszSlash = '/';
     581                pszSlash = strchr(pszSlash + 1, '\\');
     582            }
     583# endif
     584
    575585            pfdtab->cwd = sh_strdup(NULL, buf);
    576586            if (!inherit)
     
    764774
    765775/**
     776 * Changes the inheritability of a file descriptro, taking console handles into
     777 * account.
     778 *
     779 * @note    This MAY change the native handle for the entry.
     780 *
     781 * @returns The native handle.
     782 * @param   pfd     The file descriptor to change.
     783 * @param   set     If set, make child processes inherit the handle, if clear
     784 *                  make them not inherit it.
     785 */
     786static HANDLE shfile_set_inherit_win(shfile *pfd, int set)
     787{
     788    HANDLE hFile = (HANDLE)pfd->native;
     789    if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, set ? HANDLE_FLAG_INHERIT : 0))
     790    {
     791        /* SetHandleInformation doesn't work for console handles,
     792           so we have to duplicate the handle to change the
     793           inheritability. */
     794        DWORD err = GetLastError();
     795        if (   err == ERROR_INVALID_PARAMETER
     796            && DuplicateHandle(GetCurrentProcess(),
     797                               hFile,
     798                               GetCurrentProcess(),
     799                               &hFile,
     800                               0,
     801                               set ? TRUE : FALSE /* bInheritHandle */,
     802                               DUPLICATE_SAME_ACCESS))
     803        {
     804            TRACE2((NULL, "shfile_set_inherit_win: %p -> %p (set=%d)\n", pfd->native, hFile, set));
     805            if (!CloseHandle((HANDLE)pfd->native))
     806                assert(0);
     807            pfd->native = (intptr_t)hFile;
     808        }
     809        else
     810        {
     811            err = GetLastError();
     812            assert(0);
     813            hFile = (HANDLE)pfd->native;
     814        }
     815    }
     816    return hFile;
     817}
     818
     819/**
    766820 * Helper for shfork.
    767821 *
     
    775829    shmtxtmp tmp;
    776830    unsigned i;
    777     DWORD fFlag = set ? HANDLE_FLAG_INHERIT : 0;
    778831
    779832    shmtx_enter(&pfdtab->mtx, &tmp);
     
    785838        if (pfdtab->tab[i].fd == i)
    786839        {
    787             HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
     840            shfile_set_inherit_win(&pfdtab->tab[i], set);
    788841            if (set)
    789842                TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
    790                         i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
    791             if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, fFlag))
    792             {
    793 #if 0  /* Seems to happen for console handles, ignore it. */
    794                 DWORD err = GetLastError();
    795                 assert(0);
    796 #endif
    797             }
     843                        i, pfdtab->tab[i].native, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
    798844        }
    799845    }
     
    861907                &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
    862908            {
    863                 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
     909                HANDLE hFile = shfile_set_inherit_win(&pfdtab->tab[i], 1);
    864910                TRACE2((NULL, "  #%d: native=%#x oflags=%#x shflags=%#x\n",
    865911                        i, hFile, pfdtab->tab[i].oflags, pfdtab->tab[i].shflags));
    866 
    867                 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, HANDLE_FLAG_INHERIT))
    868                 {
    869 #if 0 /* Seems to fail for console handles, ignore. */
    870                     DWORD err = GetLastError();
    871                     assert(0);
    872 #endif
    873                 }
    874912                paf[i] = FOPEN;
    875913                if (pfdtab->tab[i].oflags & _O_APPEND)
     
    914952            if (    pfdtab->tab[i].fd == i
    915953                &&  !(pfdtab->tab[i].shflags & SHFILE_FLAGS_CLOSE_ON_EXEC))
    916             {
    917                 HANDLE hFile = (HANDLE)pfdtab->tab[i].native;
    918                 if (!SetHandleInformation(hFile, HANDLE_FLAG_INHERIT, 0))
    919                 {
    920                     DWORD err = GetLastError();
    921                     assert(0);
    922                 }
    923             }
     954                shfile_set_inherit_win(&pfdtab->tab[i], 0);
    924955        }
    925956        pvRet = NULL;
Note: See TracChangeset for help on using the changeset viewer.