Changeset 1052


Ignore:
Timestamp:
Jan 25, 2004, 3:17:26 AM (21 years ago)
Author:
bird
Message:

Debugged and fixed posix_spawn implementation.

Location:
trunk/src/emx/src/lib/lgpl
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • TabularUnified trunk/src/emx/src/lib/lgpl/posix/spawn_int.h

    • Property cvs2svn:cvs-rev changed from 1.2 to 1.3
    r1051 r1052  
    2828    } open_action;
    2929  } action;
    30 
    31 #ifdef __EMX__
    32   /* reserve space for saving the handle which we're closing / replacing. */
    33   int fd_saved;                         /* handle to the saved file. -1 if N/A. */
    34   int fd_saved_coe;                     /* original close on exec flag. */
    35 #endif
    3630};
    3731
  • TabularUnified trunk/src/emx/src/lib/lgpl/sysdeps/os2/__spawni.c

    • Property cvs2svn:cvs-rev changed from 1.1 to 1.2
    r1051 r1052  
    2626
    2727
     28/*******************************************************************************
     29*   Header Files                                                               *
     30*******************************************************************************/
    2831#include <stdlib.h>
    2932#include <string.h>
     
    3437#include <process.h>
    3538#include <emx/io.h>
     39#include <emx/umalloc.h>
     40#ifdef DEBUG
     41#include <assert.h>
     42#endif
    3643#include "../../posix/spawn_int.h"
     44
     45/*******************************************************************************
     46*   Structures and Typedefs                                                    *
     47*******************************************************************************/
     48typedef struct RestoreAction
     49{
     50    int fdClose;                        /* handle to close before restoring fdRestore. */
     51    int fdRestore;                      /* fd which fdSaved is a saved version of. */
     52    int fdSaved;                        /* close-on-exec dup of fdRestore. */
     53    int fSavedCoE;                      /* the original close-on-exec flag. */
     54} RESTOREACTION, *PRESTOREACTION;
    3755
    3856/**
     
    4967 *                      Set to -1 on failure.
    5068 */
    51 static int save_handle(int fdToSave, int *pfdMin, int *pfdSaved, int *pfCoE)
     69static int save_handle(int fdToSave, int *pfdMin, PRESTOREACTION pRAction)
    5270{
    53     int fCoE;
    5471    int fdSaved;
    55 
    56     *pfCoE = *pfdSaved = -1;
     72    int fSavedCoE;
     73
     74    pRAction->fdRestore = - 1;
     75    pRAction->fdSaved = -1;
     76    pRAction->fSavedCoE = -1;
     77    if (!__libc_FH(fdToSave))
     78        return 0;                       /* no such handle. */
    5779
    5880    /* Get close on exec flag. */
    59     fCoE = fcntl(fdToSave, F_GETFD);
    60     if (fCoE < 0)
     81    fSavedCoE = fcntl(fdToSave, F_GETFD);
     82    if (fSavedCoE < 0)
    6183        return -1;
    6284
    6385    /* find free handle and duplicate fdToSave onto it */
    64     while (!__libc_FH(*pfdMin))
     86    while (__libc_FH(*pfdMin))
    6587        (*pfdMin)++;
    66     if (*pfdMin >= 65536)
     88    if (*pfdMin >= 0x10000)
    6789        return -1;
    6890    fdSaved = dup2(fdToSave, *pfdMin);
     
    7193
    7294    /* marke it close on exit. */
    73     if (    fCoE
     95    if (    fSavedCoE
    7496        &&  fcntl(fdSaved, F_SETFD, FD_CLOEXEC) < 0)
    7597    {
     
    78100        return -1;
    79101    }
    80     *pfCoE      = fCoE;
    81     *pfdSaved   = fdSaved;
     102    pRAction->fdRestore = fdToSave;
     103    pRAction->fdSaved   = fdSaved;
     104    pRAction->fSavedCoE = fSavedCoE;
    82105    return 0;
    83106}
     
    88111 *
    89112 * @param   fdToRestore The original handle which is to be restored.
    90  * @param   pfdSaved    Pointer to the save handle.
    91  *                      This will be set to -1.
    92  * @param   pfCoE       Pointer to the
    93  *                      This will be set to -1.
     113 * @param   pRAction    Restore action structure.
    94114 */
    95 static void restore_handle(int fdToRestore, int *pfdSaved, int *pfCoE)
     115static void restore_handle(PRESTOREACTION pRAction)
    96116{
    97     int saved_errno;
    98     if (*pfdSaved == -1)
    99         return;
    100     saved_errno = errno;
    101 
    102     dup2(*pfdSaved, fdToRestore);
    103     close(*pfdSaved);
    104     fcntl(fdToRestore, F_SETFD, *pfCoE);
    105     *pfdSaved = -1;
    106     *pfCoE = -1;
     117    int saved_errno = errno;
     118
     119    if (pRAction->fdClose >= 0)
     120    {
     121#ifndef DEBUG
     122        close(pRAction->fdClose);
     123#else
     124        if (close(pRAction->fdClose))
     125            assert(!"__spawni: restore close() failed");
     126#endif
     127        pRAction->fdClose = -1;
     128    }
     129
     130    if (pRAction->fdSaved >= 0)
     131    {
     132#ifndef DEBUG
     133        dup2(pRAction->fdSaved, pRAction->fdRestore);
     134        close(pRAction->fdSaved);
     135        fcntl(pRAction->fdRestore, F_SETFD, pRAction->fSavedCoE);
     136#else
     137        if (dup2(pRAction->fdSaved, pRAction->fdRestore) < 0)
     138            assert(!"__spawni: restore dup2() failed");
     139        if (close(pRAction->fdSaved) < 0)
     140            assert(!"__spawni: restore close(saved) failed");
     141        if (fcntl(pRAction->fdRestore, F_SETFD, pRAction->fSavedCoE) < 0)
     142            assert(!"__spawni: restore fcntl() failed");
     143#endif
     144        pRAction->fdSaved = -1;
     145        pRAction->fSavedCoE = -1;
     146    }
    107147
    108148    errno = saved_errno;
     
    120160    int use_path)
    121161{
    122     int         iFileAction = 0;        /* where to start cleaning up file actions from.  */
    123     int         rc;                     /* result. */
    124     pid_t       pidChild;
     162    int             iFileAction = 0;    /* where to start cleaning up file actions from.  */
     163    PRESTOREACTION  paRestore = NULL;
     164    int             rc;                 /* result. */
     165    pid_t           pidChild;
    125166#if 0 /* requires some process management from LIBC */
    126167    unsigned    fFlags = attrp ? attrp->__flags : 0;
     
    170211
    171212        /* Verify handles and predict what open() and dup2() will return so we can
    172            find the min save handle number. */
     213           find the min save handle number.
     214           Note. we're counting too many opens here... but who cares. */
    173215        for (i = cOpens = 0; !rc && i < file_actions->__used; ++i)
    174216        {
    175217            struct __spawn_action  *pAction = (struct __spawn_action *)&file_actions->__actions[i]; /* nasty - const */
    176             pAction->fd_saved = pAction->fd_saved_coe = -1;
    177218            switch (pAction->tag)
    178219            {
     
    199240        }
    200241
    201         for (fdMin = 0; cOpens >= 0; fdMin++)
     242        paRestore = _hmalloc(sizeof(RESTOREACTION) * file_actions->__used);
     243        if (!paRestore)
     244        {
     245            /* TODO: release the file handle table lock. */
     246            return -1;
     247        }
     248        memset(paRestore, -1, sizeof(RESTOREACTION) * file_actions->__used);
     249
     250        for (fdMin = 0; cOpens >= 0 && fdMin < 0x10000; fdMin++)
    202251        {
    203252            int fClosed;
     
    217266                        break;
    218267                    case spawn_do_open:
     268                        if (pAction->action.open_action.fd == fdMin)
     269                            fDuped = 1;
    219270                        break;
    220271                }
     
    225276                continue;
    226277            /* if not closed or not being freed we cannot use it. */
    227             if (!fClosed || __libc_FH(fdMin))
     278            if (!fClosed && __libc_FH(fdMin))
    228279                continue;
    229280            /* it's either closed or free */
     
    235286        {
    236287            struct __spawn_action  *pAction = (struct __spawn_action *)&file_actions->__actions[i]; /* nasty - const */
     288            PRESTOREACTION          pRAction = &paRestore[i];
    237289
    238290            switch (pAction->tag)
    239291            {
    240292                case spawn_do_close:
    241                     rc = save_handle(pAction->action.close_action.fd, &fdMin, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
     293                    rc = save_handle(pAction->action.close_action.fd, &fdMin, pRAction);
    242294                    if (rc)
    243295                        break;
    244296                    if (close(pAction->action.close_action.fd))
    245297                    {
    246                         restore_handle(pAction->action.close_action.fd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
     298                        restore_handle(pRAction);
    247299                        rc = -1;
    248300                    }
     
    250302
    251303                case spawn_do_open:
    252                     pAction->fd_saved = open(pAction->action.open_action.path,
    253                                              pAction->action.open_action.mode,
    254                                              pAction->action.open_action.oflag);
    255                     if (    pAction->fd_saved < 0
    256                         ||  pAction->fd_saved != pAction->action.open_action.fd)
    257                         rc = -1;
     304                    pRAction->fdClose = open(pAction->action.open_action.path,
     305                                             pAction->action.open_action.oflag,
     306                                             pAction->action.open_action.mode);
     307                    if (pRAction->fdClose < 0)
     308                        rc = -1;
     309                    else if (   pRAction->fdClose != pAction->action.open_action.fd
     310                             && pAction->action.open_action.fd != -1   /* -1 is not standard */)
     311                    {
     312                        int fd = pRAction->fdClose;
     313                        pRAction->fdClose = -1;
     314                        rc = save_handle(pAction->action.open_action.fd, &fdMin, pRAction);
     315                        if (!rc)
     316                        {
     317                            if (dup2(fd, pAction->action.open_action.fd) < 0)
     318                            {
     319                                restore_handle(pRAction);
     320                                rc = -1;
     321                            }
     322                        }
     323                        close(fd);
     324                    }
    258325                    break;
    259326
     
    261328                    if (pAction->action.dup2_action.newfd != -1)
    262329                    {
    263                         rc = save_handle(pAction->action.dup2_action.newfd, &fdMin, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
     330                        rc = save_handle(pAction->action.dup2_action.newfd, &fdMin, pRAction);
    264331                        if (rc)
    265332                            break;
    266333                    }
    267334
    268                     if (dup2(pAction->action.dup2_action.fd, pAction->action.dup2_action.newfd) < 0)
     335                    pRAction->fdClose = dup2(pAction->action.dup2_action.fd, pAction->action.dup2_action.newfd);
     336                    if (pRAction->fdClose < 0)
    269337                    {
    270                         restore_handle(pAction->action.dup2_action.newfd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
     338                        restore_handle(pRAction);
    271339                        rc = -1;
    272340                    }
     
    305373     * Restore the file handles.
    306374     */
    307     if (file_actions)
     375    if (paRestore)
    308376    {
    309377        int save_errno = errno;
    310378        while (iFileAction-- > 0)
    311         {
    312             struct __spawn_action  *pAction = (struct __spawn_action *)&file_actions->__actions[iFileAction]; /* nasty - const */
    313             switch (pAction->tag)
    314             {
    315                 case spawn_do_close:
    316                     restore_handle(pAction->action.close_action.fd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
    317                     break;
    318                 case spawn_do_open:
    319                     close(pAction->fd_saved);
    320                     break;
    321                 case spawn_do_dup2:
    322                     restore_handle(pAction->action.dup2_action.newfd, &pAction->fd_saved_coe, &pAction->fd_saved_coe);
    323                     break;
    324             }
    325         }
     379            restore_handle(&paRestore[iFileAction]);
     380        free(paRestore);
    326381        errno = save_errno;
    327382    }
Note: See TracChangeset for help on using the changeset viewer.