Process Hacker
svcmain.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * server
4  *
5  * Copyright (C) 2011 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <phapp.h>
24 #include <phsvc.h>
25 
28 
29 NTSTATUS PhSvcMain(
30  _In_opt_ PUNICODE_STRING PortName,
31  _In_opt_ PLARGE_INTEGER Timeout,
32  _Inout_opt_ PPHSVC_STOP Stop
33  )
34 {
35  NTSTATUS status;
36  UNICODE_STRING portName;
37  LARGE_INTEGER timeout;
38 
39  if (!PortName)
40  {
43  else
45 
46  PortName = &portName;
47  }
48 
49  if (!Timeout)
50  {
51  timeout.QuadPart = -15 * PH_TIMEOUT_SEC;
52  Timeout = &timeout;
53  }
54 
55  if (!NT_SUCCESS(status = PhSvcClientInitialization()))
56  return status;
57  if (!NT_SUCCESS(status = PhSvcApiInitialization()))
58  return status;
59  if (!NT_SUCCESS(status = PhSvcApiPortInitialization(PortName)))
60  return status;
61 
62  if (!NT_SUCCESS(status = NtCreateEvent(&PhSvcTimeoutStandbyEventHandle, EVENT_ALL_ACCESS, NULL, SynchronizationEvent, TRUE)))
63  return status;
64 
65  if (!NT_SUCCESS(status = NtCreateEvent(&PhSvcTimeoutCancelEventHandle, EVENT_ALL_ACCESS, NULL, SynchronizationEvent, FALSE)))
66  {
68  return status;
69  }
70 
71  if (Stop)
72  {
73  Stop->Event1 = PhSvcTimeoutStandbyEventHandle;
74  Stop->Event2 = PhSvcTimeoutCancelEventHandle;
75  MemoryBarrier();
76 
77  if (Stop->Stop)
78  return STATUS_SUCCESS;
79  }
80 
81  while (TRUE)
82  {
83  NtWaitForSingleObject(PhSvcTimeoutStandbyEventHandle, FALSE, NULL);
84 
85  if (Stop && Stop->Stop)
86  break;
87 
88  status = NtWaitForSingleObject(PhSvcTimeoutCancelEventHandle, FALSE, Timeout);
89 
90  if (Stop && Stop->Stop)
91  break;
92  if (status == STATUS_TIMEOUT)
93  break;
94 
95  // A client connected, so we wait on the standby event again.
96  }
97 
98  return status;
99 }
100 
102  _Inout_ PPHSVC_STOP Stop
103  )
104 {
105  Stop->Stop = TRUE;
106 
107  if (Stop->Event1)
108  NtSetEvent(Stop->Event1, NULL);
109  if (Stop->Event2)
110  NtSetEvent(Stop->Event2, NULL);
111 }