summaryrefslogtreecommitdiffstats
path: root/private/ntos/ps/pscid.c
blob: 671849934a820fb949cb7e4c69d1577e364ced9d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    pscid.c

Abstract:

    This module implements the Client ID related services.


Author:

    Mark Lucovsky (markl) 25-Apr-1989
    Jim Kelly (JimK) 2-August-1990

Revision History:

--*/

#include "psp.h"

NTSTATUS
PsLookupProcessThreadByCid(
    IN PCLIENT_ID Cid,
    OUT PEPROCESS *Process OPTIONAL,
    OUT PETHREAD *Thread
    )

/*++

Routine Description:

    This function accepts The Client ID of a thread, and returns a
    referenced pointer to the thread, and possibly a referenced pointer
    to the process.

Arguments:

    Cid - Specifies the Client ID of the thread.

    Process - If specified, returns a referenced pointer to the process
        specified in the Cid.

    Thread - Returns a referenced pointer to the thread specified in the
        Cid.

Return Value:

    STATUS_SUCCESS - A process and thread were located based on the contents
        of the Cid.

    STATUS_INVALID_CID - The specified Cid is invalid.

--*/

{

    PHANDLE_ENTRY CidEntry;
    PETHREAD lThread;
    NTSTATUS Status;

    CidEntry = ExMapHandleToPointer(PspCidTable, Cid->UniqueThread, TRUE);
    Status = STATUS_INVALID_CID;
    if (CidEntry != NULL) {
        lThread = (PETHREAD)CidEntry->Object;
        if ((lThread != (PETHREAD)PSP_INVALID_ID) &&
            (lThread->Cid.UniqueProcess == Cid->UniqueProcess)) {
            if (ARGUMENT_PRESENT(Process)) {
                *Process = THREAD_TO_PROCESS(lThread);
                ObReferenceObject(*Process);
            }

            ObReferenceObject(lThread);
            *Thread = lThread;
            Status = STATUS_SUCCESS;
        }

        ExUnlockHandleTableShared(PspCidTable);
    }

    return Status;
}


NTSTATUS
PsLookupProcessByProcessId(
    IN HANDLE ProcessId,
    OUT PEPROCESS *Process
    )

/*++

Routine Description:

    This function accepts the process id of a process and returns a
    referenced pointer to the process.

Arguments:

    ProcessId - Specifies the Process ID of the process.

    Process - Returns a referenced pointer to the process specified by the
        process id.

Return Value:

    STATUS_SUCCESS - A process was located based on the contents of
        the process id.

    STATUS_INVALID_PARAMETER - The process was not found.

--*/

{

    PHANDLE_ENTRY CidEntry;
    PEPROCESS lProcess;
    NTSTATUS Status;

    CidEntry = ExMapHandleToPointer(PspCidTable, ProcessId, TRUE);
    Status = STATUS_INVALID_PARAMETER;
    if (CidEntry != NULL) {
        lProcess = (PEPROCESS)CidEntry->Object;
        if (lProcess != (PEPROCESS)PSP_INVALID_ID) {
            ObReferenceObject(lProcess);
            *Process = lProcess;
            Status = STATUS_SUCCESS;
        }

        ExUnlockHandleTableShared(PspCidTable);
    }

    return Status;
}


NTSTATUS
PsLookupThreadByThreadId(
    IN HANDLE ThreadId,
    OUT PETHREAD *Thread
    )

/*++

Routine Description:

    This function accepts the thread id of a thread and returns a
    referenced pointer to the thread.

Arguments:

    ThreadId - Specifies the Thread ID of the thread.

    Thread - Returns a referenced pointer to the thread specified by the
        thread id.

Return Value:

    STATUS_SUCCESS - A thread was located based on the contents of
        the thread id.

    STATUS_INVALID_PARAMETER - The thread was not found.

--*/

{

    PHANDLE_ENTRY CidEntry;
    PETHREAD lThread;
    NTSTATUS Status;

    CidEntry = ExMapHandleToPointer(PspCidTable, ThreadId, TRUE);
    Status = STATUS_INVALID_PARAMETER;
    if (CidEntry != NULL) {
        lThread = (PETHREAD)CidEntry->Object;
        if (lThread != (PETHREAD)PSP_INVALID_ID) {
            ObReferenceObject(lThread);
            *Thread = lThread;
            Status = STATUS_SUCCESS;
        }

        ExUnlockHandleTableShared(PspCidTable);
    }

    return Status;
}