summaryrefslogtreecommitdiff
path: root/win/inspircd_namedpipe.cpp
blob: 76d16037bf721bb51fe1e9286fc2b6fefa2e440d (plain)
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
#include "inspircd.h"
#include "threadengine.h"
#include "inspircd_namedpipe.h"
#include "exitcodes.h"
#include <windows.h>
#include <psapi.h>


IPCThread::IPCThread()
{
	if (!initwmi())
		ServerInstance->Logs->Log("IPC", DEBUG, "Could not initialise WMI. CPU percantage reports will not be available.");
}

IPCThread::~IPCThread()
{
	donewmi();
}

void IPCThread::Run()
{
	LPTSTR Pipename = "\\\\.\\pipe\\InspIRCdStatus";

	while (GetExitFlag() == false)
	{
		Pipe = CreateNamedPipe (Pipename,
					PIPE_ACCESS_DUPLEX, // read/write access
					PIPE_TYPE_MESSAGE | // message type pipe
					PIPE_READMODE_MESSAGE | // message-read mode
					PIPE_WAIT, // blocking mode
					PIPE_UNLIMITED_INSTANCES, // max. instances
					MAXBUF, // output buffer size
					MAXBUF, // input buffer size
					1000, // client time-out
					NULL); // no security attribute

		if (Pipe == INVALID_HANDLE_VALUE)
		{
			SleepEx(10, true);
			continue;
		}

		Connected = ConnectNamedPipe(Pipe, NULL) ? TRUE : (GetLastError() == ERROR_PIPE_CONNECTED);

		if (Connected)
		{
			Success = ReadFile (Pipe, // handle to pipe
				this->status, // buffer to receive data
				1, // size of buffer
				&BytesRead, // number of bytes read
				NULL); // not overlapped I/O

			if (!Success || !BytesRead)
			{
				CloseHandle(Pipe);
				continue;
			}

			const char oldrequest = this->GetStatus();

			/* Wait for main thread to pick up status change */
			while (this->GetStatus())
				SleepEx(10, true);

			std::stringstream stat;
			DWORD Written = 0;
			float kbitpersec_in, kbitpersec_out, kbitpersec_total;

			PROCESS_MEMORY_COUNTERS MemCounters;

			ServerInstance->SE->GetStats(kbitpersec_in, kbitpersec_out, kbitpersec_total);

			bool HaveMemoryStats = GetProcessMemoryInfo(GetCurrentProcess(), &MemCounters, sizeof(MemCounters));

			stat << "name " << ServerInstance->Config->ServerName << std::endl;
			stat << "gecos " << ServerInstance->Config->ServerDesc << std::endl;
			stat << "numlocalusers " << ServerInstance->Users->LocalUserCount() << std::endl;
			stat << "numusers " << ServerInstance->Users->clientlist->size() << std::endl;
			stat << "numchannels " << ServerInstance->chanlist->size() << std::endl;
			stat << "numopers " << ServerInstance->Users->OperCount() << std::endl;
			stat << "timestamp " << ServerInstance->Time() << std::endl;
			stat << "pid " << GetProcessId(GetCurrentProcess()) << std::endl;
			stat << "request " << oldrequest << std::endl;
			stat << "result " << this->GetResult() << std::endl;
			stat << "kbitspersectotal " << kbitpersec_total << std::endl;
			stat << "kbitspersecout " << kbitpersec_out << std::endl;
			stat << "kbitspersecin " << kbitpersec_in << std::endl;
			stat << "uptime " << ServerInstance->Time() - ServerInstance->startup_time << std::endl;
			stat << "cpu " << getcpu() << std::endl;
			if (HaveMemoryStats)
			{
				stat << "workingset " << MemCounters.WorkingSetSize << std::endl;
				stat << "pagefile " << MemCounters.PagefileUsage << std::endl;
				stat << "pagefaults " << MemCounters.PageFaultCount << std::endl;
			}

			stat << "END" << std::endl;

			/* This is a blocking call and will succeed, so long as the client doesnt disconnect */
			Success = WriteFile(Pipe, stat.str().data(), stat.str().length(), &Written, NULL);

			FlushFileBuffers(Pipe);
			DisconnectNamedPipe(Pipe);
		}
		CloseHandle(Pipe);
	}
}

const  char IPCThread::GetStatus()
{
	return *status;
}

void IPCThread::ClearStatus()
{
	*status = '\0';
}

int IPCThread::GetResult()
{
	return result;
}

void IPCThread::SetResult(int newresult)
{
	result = newresult;
}


IPC::IPC()
{
	/* The IPC pipe is threaded */
	thread = new IPCThread();
	ServerInstance->Threads->Start(thread);
}

void IPC::Check()
{
	switch (thread->GetStatus())
	{
		case 'N':
			/* No-Operation */
			thread->SetResult(0);
			thread->ClearStatus();
		break;
		case '1':
			/* Rehash */
			ServerInstance->Rehash("due to rehash command from GUI");
			thread->SetResult(0);
			thread->ClearStatus();
		break;
		case '2':
			/* Shutdown */
			thread->SetResult(0);
			thread->ClearStatus();
			ServerInstance->Exit(EXIT_STATUS_NOERROR);
		break;
		case '3':
			/* Restart */
			thread->SetResult(0);
			thread->ClearStatus();
			ServerInstance->Restart("Restarting due to command from GUI");
		break;
		case '4':
			/* Toggle debug */
			thread->SetResult(0);
			thread->ClearStatus();
			ServerInstance->Config->forcedebug = !ServerInstance->Config->forcedebug;
		break;
	}
}

IPC::~IPC()
{
	thread->SetExitFlag();
	delete thread;
}