| 1 | /* $Id: k32SetEnvironment.cpp,v 1.2 2001-07-10 16:39:17 bird Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * k32SetEnvironment - Sets the Odin32 environment for a process. | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (c) 2001 knut st. osmundsen (kosmunds@csc.no) | 
|---|
| 6 | * | 
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | /******************************************************************************* | 
|---|
| 13 | *   Defined Constants And Macros                                               * | 
|---|
| 14 | *******************************************************************************/ | 
|---|
| 15 | #define INCL_DOSERRORS | 
|---|
| 16 | #define INCL_OS2KRNL_TK | 
|---|
| 17 | #define INCL_OS2KRNL_PTDA | 
|---|
| 18 | #define INCL_OS2KRNL_SEM | 
|---|
| 19 | #define INCL_OS2KRNL_LDR | 
|---|
| 20 | #define NO_WIN32K_LIB_FUNCTIONS | 
|---|
| 21 |  | 
|---|
| 22 |  | 
|---|
| 23 | /******************************************************************************* | 
|---|
| 24 | *   Header Files                                                               * | 
|---|
| 25 | *******************************************************************************/ | 
|---|
| 26 | #include <os2.h> | 
|---|
| 27 | #include <memory.h> | 
|---|
| 28 | #include "devSegDf.h"                   /* Win32k segment definitions. */ | 
|---|
| 29 | #include "OS2Krnl.h" | 
|---|
| 30 | #include "win32k.h" | 
|---|
| 31 | #include "k32.h" | 
|---|
| 32 | #include "options.h" | 
|---|
| 33 | #include "dev32.h" | 
|---|
| 34 | #include "dev32hlp.h" | 
|---|
| 35 | #include "log.h" | 
|---|
| 36 | #include "macros.h" | 
|---|
| 37 | #include "avl.h" | 
|---|
| 38 | #include "PerTaskW32kData.h" | 
|---|
| 39 |  | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | /** | 
|---|
| 43 | * Set the Odin32 environment pointer for a process. | 
|---|
| 44 | * @returns NO_ERROR on success. | 
|---|
| 45 | *          Appropriate error code on failure. | 
|---|
| 46 | * @param   pszzEnvironment Pointer to environment block for the process identified by pid. | 
|---|
| 47 | * @param   cchEnvironment  Size of the environment block. | 
|---|
| 48 | * @param   pid             Process Id to set environment for. | 
|---|
| 49 | *                          Currently limited to the current process (if pszzEnvironment != NULL). | 
|---|
| 50 | * @status  Completely implemented. | 
|---|
| 51 | * @author  knut st. osmundsen (knut.stange.osmundsen@mynd.no) | 
|---|
| 52 | * @remark  This is called by the kernel32 initterm proc. | 
|---|
| 53 | *          It should be called by any environment replacement later... | 
|---|
| 54 | */ | 
|---|
| 55 | APIRET k32SetEnvironment(PSZ pszzEnvironment, ULONG cchEnvironment, PID pid) | 
|---|
| 56 | { | 
|---|
| 57 | APIRET  rc; | 
|---|
| 58 | PPTD    pptd; | 
|---|
| 59 |  | 
|---|
| 60 | /* | 
|---|
| 61 | * Validate pid a little bit... | 
|---|
| 62 | */ | 
|---|
| 63 | if (pid >= 0x10000) | 
|---|
| 64 | { | 
|---|
| 65 | kprintf(("k32SetEnvironment: invalid pid=%x\n", pid)); | 
|---|
| 66 | return ERROR_INVALID_PARAMETER; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | if (pid != 0) | 
|---|
| 70 | { | 
|---|
| 71 | kprintf(("k32SetEnvironment: currently only supported for current pid. pid=%x\n", pid)); | 
|---|
| 72 | return ERROR_INVALID_PARAMETER; | 
|---|
| 73 | } | 
|---|
| 74 |  | 
|---|
| 75 |  | 
|---|
| 76 | /* | 
|---|
| 77 | * Take Loader semaphore as that currently protects everything in this driver... | 
|---|
| 78 | */ | 
|---|
| 79 | rc = LDRRequestSem(); | 
|---|
| 80 | if (rc != NO_ERROR) | 
|---|
| 81 | { | 
|---|
| 82 | kprintf(("k32SetEnvironment: LDRRequestSem failed with rc = %d\n", rc)); | 
|---|
| 83 | return rc; | 
|---|
| 84 | } | 
|---|
| 85 |  | 
|---|
| 86 |  | 
|---|
| 87 | /* | 
|---|
| 88 | * Get Per Task Data and try set next environment pointer. | 
|---|
| 89 | */ | 
|---|
| 90 | pptd = GetTaskData(ptdaGetCur(), TRUE); | 
|---|
| 91 | if (pptd) | 
|---|
| 92 | { | 
|---|
| 93 | if (*(PULONG)&pptd->lockOdin32Env) | 
|---|
| 94 | { | 
|---|
| 95 | D32Hlp_VMUnLock(&pptd->lockOdin32Env); | 
|---|
| 96 | memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env)); | 
|---|
| 97 | } | 
|---|
| 98 | pptd->pszzOdin32Env = NULL; | 
|---|
| 99 | if (pptd->cUsage != 0 && pszzEnvironment != NULL && cchEnvironment != 0) | 
|---|
| 100 | { | 
|---|
| 101 | /* | 
|---|
| 102 | * Lock the memory (so we don't block or trap during environment searchs). | 
|---|
| 103 | */ | 
|---|
| 104 | rc = D32Hlp_VMLock2(pszzEnvironment, cchEnvironment, | 
|---|
| 105 | VMDHL_LONG | VMDHL_WRITE, &pptd->lockOdin32Env); | 
|---|
| 106 | if (rc == NO_ERROR) | 
|---|
| 107 | pptd->pszzOdin32Env = pszzEnvironment; | 
|---|
| 108 | else | 
|---|
| 109 | { | 
|---|
| 110 | kprintf(("k32SetEnvironment: VMLock2 failed with rc=%d\n", rc)); | 
|---|
| 111 | memset(&pptd->lockOdin32Env, 0, sizeof(pptd->lockOdin32Env)); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 | else | 
|---|
| 115 | rc = NO_ERROR; | 
|---|
| 116 | } | 
|---|
| 117 | else | 
|---|
| 118 | rc = ERROR_NOT_ENOUGH_MEMORY; | 
|---|
| 119 |  | 
|---|
| 120 | LDRClearSem(); | 
|---|
| 121 | return rc; | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|