Ignore:
Timestamp:
Dec 18, 1999, 10:46:19 PM (26 years ago)
Author:
sandervl
Message:

Added token handlemanager class + HMHandleGetUserData export

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kernel32/HandleManager.cpp

    r2062 r2129  
    1 /* $Id: HandleManager.cpp,v 1.30 1999-12-12 14:57:14 phaller Exp $ */
     1/* $Id: HandleManager.cpp,v 1.31 1999-12-18 21:45:53 sandervl Exp $ */
    22
    33/*
     
    5656#include "HMMMap.h"
    5757#include "HMComm.h"
     58#include "HMToken.h"
    5859#include <winconst.h>
    5960
     
    123124  HMDeviceHandler        *pHMFileMapping;  /* static instances of subsystems */
    124125  HMDeviceHandler        *pHMComm;                   /* serial communication */
     126  HMDeviceHandler        *pHMToken;         /* security tokens */
    125127
    126128  ULONG         ulHandleLast;                   /* index of last used handle */
     
    209211
    210212/*****************************************************************************
     213 * Name      : HMHandleGetUserData
     214 * Purpose   : Get the dwUserData dword for a specific handle
     215 * Parameters: HANDLE hHandle
     216 * Variables :
     217 * Result    : -1 or dwUserData
     218 * Remark    :
     219 * Status    :
     220 *
     221 * Author    : SvL
     222 *****************************************************************************/
     223DWORD HMHandleGetUserData(ULONG  hHandle)
     224{
     225  if (hHandle > MAX_OS2_HMHANDLES)                  /* check the table range */
     226    return (-1);
     227                                                   /* Oops, invalid handle ! */
     228  if (INVALID_HANDLE_VALUE == TabWin32Handles[hHandle].hmHandleData.hHMHandle)
     229    return (-1);              /* nope, ERROR_INVALID_HANDLE */
     230
     231  return TabWin32Handles[hHandle].hmHandleData.dwUserData;
     232}
     233
     234
     235/*****************************************************************************
    211236 * Name      : static int _HMHandleQuery
    212237 * Purpose   : gets the index of handle table entry as fast as possible from
     
    322347    HMGlobals.pHMFileMapping= new HMDeviceMemMapClass("\\\\MEMMAP\\");
    323348    HMGlobals.pHMComm       = new HMDeviceCommClass("\\\\COM\\");
     349    HMGlobals.pHMToken      = new HMDeviceTokenClass("\\\\TOKEN\\");
    324350  }
    325351  return (NO_ERROR);
     
    392418  ulHandle = HMGlobals.ulHandleLast;                      /* get free handle */
    393419
     420  if(ulHandle == 0) {
     421        ulHandle = 1; //SvL: Start searching from index 1
     422  }
    394423  do
    395424  {
     
    24892518
    24902519  // allocate array for handle table
    2491   pArrayOfHandles = (PHANDLE)malloc(cObjects * sizeof(HANDLE));
     2520  pArrayOfHandles = (PHANDLE)alloca(cObjects * sizeof(HANDLE));
    24922521  if (pArrayOfHandles == NULL)
    24932522  {
    2494     O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
    2495     return WAIT_FAILED;
    2496   }
    2497   else
    2498     pLoop2 = pArrayOfHandles;
     2523        dprintf(("ERROR: HMWaitForMultipleObjects: alloca failed to allocate %d handles", cObjects));
     2524        O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     2525        return WAIT_FAILED;
     2526  }
     2527  else  pLoop2 = pArrayOfHandles;
    24992528
    25002529  // convert array to odin handles
     
    25122541    if (rc != NO_ERROR)
    25132542    {
    2514       free (pArrayOfHandles);             // free memory
    25152543      O32_SetLastError(ERROR_INVALID_HANDLE);
    25162544      return (WAIT_FAILED);
     
    25262554                                  dwTimeout);
    25272555
    2528   free(pArrayOfHandles);                  // free memory
    25292556  return (rc);                            // OK, done
    25302557}
     
    25562583}
    25572584
     2585/*****************************************************************************
     2586 * Name      : HMMsgWaitForMultipleObjects
     2587 * Purpose   : router function for MsgWaitForMultipleObjects
     2588 * Parameters:
     2589 * Variables :
     2590 * Result    :
     2591 * Remark    : Open32 doesn't implement this properly! (doesn't check dwWakeMask)
     2592 * Status    :
     2593 *
     2594 * Author    : Patrick Haller [Wed, 1999/06/17 20:44]
     2595 *****************************************************************************/
     2596
     2597DWORD  HMMsgWaitForMultipleObjects  (DWORD                      nCount,
     2598                                     LPHANDLE                   pHandles,
     2599                                     BOOL                       fWaitAll,
     2600                                     DWORD                      dwMilliseconds,
     2601                                     DWORD                      dwWakeMask)
     2602{
     2603  ULONG   ulIndex;
     2604  PHANDLE pArrayOfHandles;
     2605  PHANDLE pLoop1 = pHandles;
     2606  PHANDLE pLoop2;
     2607  DWORD   rc;
     2608
     2609  // allocate array for handle table
     2610  pArrayOfHandles = (PHANDLE)alloca(nCount * sizeof(HANDLE));
     2611  if (pArrayOfHandles == NULL)
     2612  {
     2613        dprintf(("ERROR: HMMsgWaitForMultipleObjects: alloca failed to allocate %d handles", nCount));
     2614        O32_SetLastError(ERROR_NOT_ENOUGH_MEMORY);
     2615        return WAIT_FAILED;
     2616  }
     2617  else
     2618    pLoop2 = pArrayOfHandles;
     2619
     2620  // convert array to odin handles
     2621  for (ulIndex = 0;
     2622
     2623       ulIndex < nCount;
     2624
     2625       ulIndex++,
     2626       pLoop1++,
     2627       pLoop2++)
     2628  {
     2629    rc = HMHandleTranslateToOS2 (*pLoop1, // translate handle
     2630                                 pLoop2);
     2631
     2632    if (rc != NO_ERROR)
     2633    {
     2634      O32_SetLastError(ERROR_INVALID_HANDLE);
     2635      return (WAIT_FAILED);
     2636    }
     2637  }
     2638
     2639  // OK, now forward to Open32.
     2640  // @@@PH: Note this will fail on handles that do NOT belong to Open32
     2641  //        but to i.e. the console subsystem!
     2642  rc = O32_MsgWaitForMultipleObjects(nCount,
     2643                                     pArrayOfHandles,
     2644                                     fWaitAll, dwMilliseconds,
     2645                                     dwWakeMask);
     2646
     2647  return (rc);                            // OK, done
     2648}
    25582649/*****************************************************************************
    25592650 * Name      : HMDeviceIoControl
     
    26622753  return (bResult);                                  /* deliver return code */
    26632754}
     2755
     2756/*****************************************************************************
     2757 * Name      : HMOpenThreadToken
     2758 * Purpose   : router function for NtOpenThreadToken
     2759 * Parameters:
     2760 * Variables :
     2761 * Result    :
     2762 * Remark    :
     2763 * Status    :
     2764 *
     2765 * Author    : SvL
     2766 *****************************************************************************/
     2767
     2768DWORD HMOpenThreadToken(HANDLE  ThreadHandle,
     2769                        DWORD   DesiredAccess,
     2770                        BOOLEAN OpenAsSelf,
     2771                        DWORD   dwUserData,
     2772                        HANDLE *TokenHandle)
     2773{
     2774  int             iIndex;                     /* index into the handle table */
     2775  int             iIndexNew;                  /* index into the handle table */
     2776  HMDeviceHandler *pDeviceHandler;         /* device handler for this handle */
     2777  PHMHANDLEDATA   pHMHandleData;
     2778  DWORD           rc;                                     /* API return code */
     2779
     2780  pDeviceHandler = HMGlobals.pHMToken;         /* device is predefined */
     2781
     2782  iIndexNew = _HMHandleGetFree();                         /* get free handle */
     2783  if (-1 == iIndexNew)                            /* oops, no free handles ! */
     2784  {
     2785    SetLastError(ERROR_NOT_ENOUGH_MEMORY);      /* use this as error message */
     2786    return ERROR_NOT_ENOUGH_MEMORY;
     2787  }
     2788
     2789  /* initialize the complete HMHANDLEDATA structure */
     2790  pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
     2791  pHMHandleData->dwType     = FILE_TYPE_UNKNOWN;      /* unknown handle type */
     2792  pHMHandleData->dwAccess   = DesiredAccess;
     2793  pHMHandleData->dwShare    = 0;
     2794  pHMHandleData->dwCreation = 0;
     2795  pHMHandleData->dwFlags    = 0;
     2796  pHMHandleData->lpHandlerData = NULL;
     2797
     2798
     2799  /* we've got to mark the handle as occupied here, since another device */
     2800  /* could be created within the device handler -> deadlock */
     2801
     2802  /* write appropriate entry into the handle table if open succeeded */
     2803  TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
     2804  TabWin32Handles[iIndexNew].pDeviceHandler         = pDeviceHandler;
     2805
     2806                                                  /* call the device handler */
     2807
     2808  // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
     2809  //              a valid HandleManager-internal handle!
     2810  rc = pDeviceHandler->OpenThreadToken(&TabWin32Handles[iIndexNew].hmHandleData,
     2811                                       dwUserData,
     2812                                       ThreadHandle,
     2813                                       OpenAsSelf);
     2814
     2815  if (rc != NO_ERROR)     /* oops, creation failed within the device handler */
     2816  {
     2817        TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     2818        return (rc);                                           /* signal error */
     2819  }
     2820  else
     2821    SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
     2822
     2823  *TokenHandle = iIndexNew;                                   /* return valid handle */
     2824  return STATUS_SUCCESS;
     2825}
     2826
     2827/*****************************************************************************
     2828 * Name      : HMOpenProcessToken
     2829 * Purpose   : router function for NtOpenProcessToken
     2830 * Parameters:
     2831 * Variables :
     2832 * Result    :
     2833 * Remark    :
     2834 * Status    :
     2835 *
     2836 * Author    : SvL
     2837 *****************************************************************************/
     2838DWORD HMOpenProcessToken(HANDLE  ProcessHandle,
     2839                         DWORD   DesiredAccess,
     2840                         DWORD   dwUserData,
     2841                         HANDLE *TokenHandle)
     2842{
     2843  int             iIndex;                     /* index into the handle table */
     2844  int             iIndexNew;                  /* index into the handle table */
     2845  HMDeviceHandler *pDeviceHandler;         /* device handler for this handle */
     2846  PHMHANDLEDATA   pHMHandleData;
     2847  DWORD           rc;                                     /* API return code */
     2848
     2849  pDeviceHandler = HMGlobals.pHMToken;         /* device is predefined */
     2850
     2851  iIndexNew = _HMHandleGetFree();                         /* get free handle */
     2852  if (-1 == iIndexNew)                            /* oops, no free handles ! */
     2853  {
     2854    SetLastError(ERROR_NOT_ENOUGH_MEMORY);      /* use this as error message */
     2855    return ERROR_NOT_ENOUGH_MEMORY;
     2856  }
     2857
     2858  /* initialize the complete HMHANDLEDATA structure */
     2859  pHMHandleData = &TabWin32Handles[iIndexNew].hmHandleData;
     2860  pHMHandleData->dwType     = FILE_TYPE_UNKNOWN;      /* unknown handle type */
     2861  pHMHandleData->dwAccess   = DesiredAccess;
     2862  pHMHandleData->dwShare    = 0;
     2863  pHMHandleData->dwCreation = 0;
     2864  pHMHandleData->dwFlags    = 0;
     2865  pHMHandleData->lpHandlerData = NULL;
     2866
     2867
     2868  /* we've got to mark the handle as occupied here, since another device */
     2869  /* could be created within the device handler -> deadlock */
     2870
     2871  /* write appropriate entry into the handle table if open succeeded */
     2872  TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = iIndexNew;
     2873  TabWin32Handles[iIndexNew].pDeviceHandler         = pDeviceHandler;
     2874
     2875                                                  /* call the device handler */
     2876
     2877  // @@@PH: Note: hFile is a Win32-style handle, it's not (yet) converted to
     2878  //              a valid HandleManager-internal handle!
     2879  rc = pDeviceHandler->OpenProcessToken(&TabWin32Handles[iIndexNew].hmHandleData,
     2880                                        dwUserData,
     2881                                        ProcessHandle);
     2882
     2883  if (rc != NO_ERROR)     /* oops, creation failed within the device handler */
     2884  {
     2885        TabWin32Handles[iIndexNew].hmHandleData.hHMHandle = INVALID_HANDLE_VALUE;
     2886        return (rc);                                           /* signal error */
     2887  }
     2888  else
     2889    SetLastError(ERROR_SUCCESS); //@@@PH 1999/10/27 rc5desg requires this?
     2890
     2891  *TokenHandle = iIndexNew;                                   /* return valid handle */
     2892  return STATUS_SUCCESS;
     2893}
     2894
Note: See TracChangeset for help on using the changeset viewer.