-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
310 lines (280 loc) · 10.1 KB
/
main.cpp
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* Sigstoped
* Copyright (C) 2020 Carl Klemm
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 3 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define __USE_POSIX
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <X11/Xlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <limits.h>
#include <fstream>
#include <string>
#include <vector>
#include <list>
#include <signal.h>
#include <sys/stat.h>
#include <cstring>
#include <filesystem>
#include "xinstance.h"
#include "process.h"
#include "split.h"
#include "debug.h"
#include "argpopt.h"
#include "CppTimer.h"
Window intraCommesWindow;
volatile bool configStale = false;
XInstance xinstance;
constexpr char configPrefix[] = "/.config/sigstoped/";
constexpr char STOP_EVENT = 65;
constexpr char PROC_STOP_EVENT = 1;
void sigTerm(int dummy)
{
XClientMessageEvent event;
memset(&event, 0, sizeof(XClientMessageEvent));
event.type = ClientMessage;
event.window = intraCommesWindow;
event.format = 8;
event.data.b[0] = STOP_EVENT;
XLockDisplay(xinstance.display);
XSendEvent(xinstance.display, intraCommesWindow, 0, 0, (XEvent*)&event);
XUnlockDisplay(xinstance.display);
xinstance.flush();
}
void sigUser1(int dummy)
{
configStale = true;
}
std::string getConfdir()
{
const char* homeDir = getenv("HOME");
if(homeDir == nullptr)
{
std::cerr<<"HOME enviroment variable must be set.\n";
return std::string();
}
const std::string configDir(std::string(homeDir)+configPrefix);
if (std::filesystem::exists(configDir))
{
return std::string(homeDir)+configPrefix;
}
else if(!std::filesystem::create_directory(configDir, homeDir))
{
std::cout<<"Can't create "<<configDir<<'\n';
return std::string();
}
else return std::string(homeDir)+configPrefix;
}
std::vector<std::string> getApplicationlist(const std::string& fileName)
{
std::fstream blacklistFile;
blacklistFile.open(fileName, std::fstream::in);
std::string blacklistString;
if(!blacklistFile.is_open())
{
std::cout<<fileName+" dose not exist\n";
blacklistFile.open(fileName, std::fstream::out);
if(blacklistFile.is_open()) blacklistFile.close();
else
{
std::cout<<"Can't create "<<fileName<<'\n';
return std::vector<std::string>();
}
}
else
{
blacklistString.assign((std::istreambuf_iterator<char>(blacklistFile)),
std::istreambuf_iterator<char>());
}
return split(blacklistString);
}
bool createPidFile(const std::string& fileName)
{
if(std::filesystem::exists(fileName))
{
std::cerr<<fileName<<" pid file exsists, only one instance may run at once\n";
std::string sigstopedName = Process(getpid()).getName();
if(Process::byName(sigstopedName).size() > 1) return false;
else
{
std::cerr<<"Only one "
<<sigstopedName
<<" process exists, either sigstoped died or you have several diferently named binarys\n";
std::filesystem::remove(fileName);
return createPidFile(fileName);
}
}
else
{
std::fstream pidFile;
pidFile.open(fileName, std::fstream::out);
if(!pidFile.is_open())
{
std::cerr<<"Can not create "<<fileName<<std::endl;
return false;
}
else
{
pidFile<<getpid();
pidFile.close();
return true;
}
}
}
void sendEventProcStop()
{
XClientMessageEvent event;
memset(&event, 0, sizeof(XClientMessageEvent));
event.type = ClientMessage;
event.window = intraCommesWindow;
event.format = 8;
event.data.b[0] = PROC_STOP_EVENT;
XLockDisplay(xinstance.display);
XSendEvent(xinstance.display, intraCommesWindow, 0, 0, (XEvent*)&event);
XUnlockDisplay(xinstance.display);
xinstance.flush();
}
bool stopProcess(Process process, XInstance* xinstance)
{
bool hasTopLevelWindow = false;
std::vector<Window> tlWindows = xinstance->getTopLevelWindows();
for(auto& window : tlWindows)
{
if(xinstance->getPid(window) == process.getPid()) hasTopLevelWindow = true;
}
if(hasTopLevelWindow)
{
process.stop(true);
std::cout<<"Stoping pid: "+std::to_string(process.getPid())+" name: "+process.getName()<<'\n';
return true;
}
else
{
std::cout<<"not Stoping pid: "+std::to_string(process.getPid())+" name: "+process.getName()<<'\n';
return false;
}
}
int main(int argc, char* argv[])
{
char* xDisplayName = std::getenv( "DISPLAY" );
if(xDisplayName == nullptr)
{
std::cerr<<"DISPLAY enviroment variable must be set.\n";
return 1;
}
if(!xinstance.open(xDisplayName)) exit(1);
Config config;
argp_parse(&argp, argc, argv, 0, 0, &config);
if(config.ignoreClientMachine)
{
std::cout<<"WARNING: Ignoring WM_CLIENT_MACHINE is dangerous and may cause sigstoped to stop random pids if remote windows are present"<<std::endl;
XInstance::ignoreClientMachine = true;
}
std::string confDir = getConfdir();
if(confDir.size() == 0) return 1;
if(!createPidFile(confDir+"pidfile")) return 1;
std::vector<std::string> applicationNames = getApplicationlist(confDir+"blacklist");
if(applicationNames.size() == 0) std::cout<<"WARNIG: no application names configured.\n";
if( !std::filesystem::exists("/proc") )
{
std::cerr<<"proc must be mounted!\n";
return 1;
}
std::list<Process> stoppedProcs;
intraCommesWindow = XCreateSimpleWindow(xinstance.display,
RootWindow(xinstance.display, xinstance.screen),
10, 10, 10, 10, 0, 0, 0);
XSelectInput(xinstance.display, intraCommesWindow, StructureNotifyMask);
XSelectInput(xinstance.display, RootWindow(xinstance.display, xinstance.screen), PropertyChangeMask | StructureNotifyMask);
XEvent event;
Process prevProcess;
Process qeuedToStop;
Window prevWindow = 0;
signal(SIGINT, sigTerm);
signal(SIGTERM, sigTerm);
signal(SIGHUP, sigTerm);
signal(SIGUSR1, sigUser1);
CppTimer timer;
while(true)
{
XNextEvent(xinstance.display, &event);
if (event.type == DestroyNotify) break;
else if (event.type == PropertyNotify && event.xproperty.atom == xinstance.atoms.netActiveWindow)
{
Window wid = xinstance.getActiveWindow();
if(wid != 0 && wid != prevWindow)
{
pid_t windowPid = xinstance.getPid(wid);
Process process(windowPid);
std::cout<<"Active window: "<<wid<<" pid: "<<process.getPid()<<" name: "<<process.getName()<<'\n';
if(process != prevProcess)
{
if(configStale)
{
applicationNames = getApplicationlist(confDir+"blacklist");
configStale = false;
for(auto& process : stoppedProcs) process.resume(true);
stoppedProcs.clear();
}
for(size_t i = 0; i < applicationNames.size(); ++i)
{
if(process.getName() == applicationNames[i] && wid != 0 && process.getPid() > 0 && process.getName() != "")
{
if(process == qeuedToStop)
{
std::cout<<"Canceling stop of wid: "+std::to_string(wid)+" pid: "+std::to_string(process.getPid())+" name: "+process.getName()<<'\n';
timer.stop();
qeuedToStop = Process();
}
process.resume(true);
stoppedProcs.remove(process);
std::cout<<"Resumeing wid: "+std::to_string(wid)+" pid: "+std::to_string(process.getPid())+" name: "+process.getName()<<'\n';
}
else if(prevProcess.getName() == applicationNames[i] &&
prevWindow != 0 &&
prevProcess.getName() != "" &&
prevProcess.getPid() > 0)
{
timer.block();
std::cout<<"Will stop pid: "<<prevProcess.getPid()<<" name: "<<prevProcess.getName()<<'\n';
qeuedToStop = prevProcess;
timer.start(config.timeoutSecs, 0, sendEventProcStop, CppTimer::ONESHOT);
stoppedProcs.push_back(prevProcess);
}
}
}
prevProcess = process;
prevWindow = wid;
}
}
else if (event.type == ClientMessage && ((XClientMessageEvent*)&event)->window == intraCommesWindow)
{
XClientMessageEvent* clientEvent = (XClientMessageEvent*)&event;
if (clientEvent->data.b[0] == STOP_EVENT) break;
if (clientEvent->data.b[0] == PROC_STOP_EVENT)
{
stopProcess(qeuedToStop, &xinstance);
qeuedToStop = Process();
}
}
}
for(auto& process : stoppedProcs) process.resume(true);
std::filesystem::remove(confDir+"pidfile");
return 0;
}