-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.c
356 lines (261 loc) · 9.59 KB
/
main.c
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
Copyright (C) 2011 Jay Satiro <[email protected]>
All rights reserved.
This file is part of GetHooks.
GetHooks is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
GetHooks 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 GetHooks. If not, see <https://www.gnu.org/licenses/>.
*/
/**
This file contains main() and various supporting functions.
-
print_version()
Print the program version.
-
-
print_license()
Print the GPL license and copyright.
-
-
gethooks()
Initialize and process the snapshot store(s), and print the HOOK info to stdout.
-
-
main()
Create and initialize the global stores, print the license and version and then run gethooks().
-
*/
#include <stdio.h>
#include "util.h"
#include "snapshot.h"
#include "diff.h"
#include "test.h"
/* the global stores */
#include "global.h"
/* this program's version */
#define VERSION_MAJOR 1
#define VERSION_MINOR 1
/* print_version()
Print the program version.
If you've made any modifications to this program add a line that says "Modifications by" as shown.
*/
void print_version( void )
{
printf( "\ngethooks v%u.%.*u", VERSION_MAJOR, ( ( VERSION_MINOR ) ? 2 : 1 ), VERSION_MINOR );
printf( " - " );
printf( "Built on " __DATE__ " at " __TIME__ "\n" );
printf( "The original gethooks source can be found at https://jay.github.io/gethooks/\n" );
printf( "For usage use --help\n" );
/* Example modification notice. Leave this example intact. Copy it below to use as a template.
printf( "\n****This version has been modified by Your Name <your@email>\n" );
printf( "The modification made is super fast lightning if executed outside gravity.\n" );
printf( "Source code: https://your.website/yourgethooksmods\n" );
*/
return;
}
/* print_license()
Print the GPL license and copyright.
If you've made substantial modifications to this program add an additional copyright with your name.
*/
void print_license( void )
{
printf( "-\n" );
/* Example copyright notice. Leave this example intact. Copy it below to use as a template.
printf( "Copyright (C) 2011 Your Name <your@email> \n" );
*/
printf(
"Copyright (C) 2011 Jay Satiro <[email protected]> \n"
"All rights reserved. License GPLv3+: GNU GPL version 3 or later \n"
"<https://www.gnu.org/licenses/gpl.html>. \n"
"This is free software: you are free to change and redistribute it. \n"
"There is NO WARRANTY, to the extent permitted by law. \n"
);
printf( "-\n" );
return;
}
/* gethooks()
Initialize and process the snapshot store(s), and print the HOOK info to stdout.
Each snapshot store is a snapshot of the state of the system, specifically thread information and
HOOK information at that point in time. This function calls init_snapshot_store() which takes a
snapshot and then matches the HOOKs to their threads. This function then prints the results.
If monitoring/polling is enabled then snapshots are taken continuously with each current snapshot
compared to the previous one for differences. The results are printed for each difference.
returns nonzero on success (a single snapshot was taken and its results printed to stdout).
if polling is enabled this function will loop continuously and never return.
*/
int gethooks()
{
const char *const objname = "GetHooks";
struct desktop_hook_item *dh = NULL;
struct snapshot *previous = NULL;
struct snapshot *current = NULL;
struct snapshot *temp = NULL;
int ret = 0;
FAIL_IF( !G ); // The global store must exist.
FAIL_IF( !G->prog->init_time ); // The program store must be initialized.
FAIL_IF( !G->config->init_time ); // The configuration store must be initialized.
FAIL_IF( !G->desktops->init_time ); // The desktop store must be initialized.
if( G->config->verbose >= 5 )
PRINT_HASHSEP_BEGIN( objname );
/* allocate the memory needed to take a snapshot */
create_snapshot_store( ¤t );
/* take a snapshot */
ret = init_snapshot_store( current );
if( G->config->verbose >= 8 )
print_snapshot_store( current );
if( !ret )
{
MSG_FATAL( "The snapshot store failed to initialize." );
exit( 1 );
}
/* print the HOOKs found in the snapshot */
print_initial_desktop_hook_list( current->desktop_hooks );
printf( "\n" );
/* for each desktop in the snapshot */
for( dh = current->desktop_hooks->head; dh; dh = dh->next )
{
unsigned i = 0, hook_ignore_count = 0;
/* for each hook info in the desktop's array of hook info structs */
for( i = 0; i < dh->hook_count; ++i )
{
if( dh->hook[ i ].ignore ) // the hook was ignored
++hook_ignore_count;
}
/* print some statistics if the user requested verbosity */
if( G->config->verbose >= 1 )
{
printf( "\nDesktop '%ls':\nFound %u, Ignored %u, Printed %u hooks.\n",
dh->desktop->pwszDesktopName,
dh->hook_count,
hook_ignore_count,
( dh->hook_count - hook_ignore_count )
);
}
}
/* if polling is disabled then the user did not request monitor mode so we're done */
if( G->config->polling < POLLING_MIN )
goto cleanup;
printf( "\nMonitor mode enabled. Checking for changes every %d seconds...\n",
G->config->polling
);
fflush( stdout );
/* allocate the memory needed to take another snapshot */
create_snapshot_store( &previous );
for( ;; )
{
Sleep( G->config->polling * 1000 );
/* swap pointers to previous and current snapshot stores.
this is better than continually freeing and creating the stores.
the current snapshot becomes the previous, and the former previous is set
to be reinitialized with new info and become the current.
*/
temp = previous;
previous = current;
current = temp;
/* take a snapshot */
ret = init_snapshot_store( current );
if( G->config->verbose >= 8 )
print_snapshot_store( current );
if( !ret )
{
MSG_FATAL( "The snapshot store failed to initialize." );
exit( 1 );
}
/* Print the HOOKs that have been added/removed/modified since the last snapshot */
print_diff_desktop_hook_lists( previous->desktop_hooks, current->desktop_hooks );
}
cleanup:
/* free the stores and all their descendants */
free_snapshot_store( &previous );
free_snapshot_store( ¤t );
if( G->config->verbose >= 5 )
PRINT_HASHSEP_END( objname );
return TRUE;
}
#ifdef _MSC_VER
#pragma optimize( "g", off ) /* disable global optimizations */
#pragma auto_inline( off ) /* disable automatic inlining */
#endif
static void pause( void )
{
system( "pause" );
return;
}
#ifdef _MSC_VER
#pragma auto_inline( on ) /* revert automatic inlining setting */
#pragma optimize( "g", on ) /* revert global optimizations setting */
#endif
/* main()
Create and initialize the global stores, print the license and version and then run gethooks().
returns zero on success
*/
int main( int argc, char **argv )
{
/* If the program is started in its own window then pause before exit
(eg user clicks on gethooks.exe in explorer, or vs debugger initiated program)
*/
{
HANDLE hOutput = GetStdHandle( STD_OUTPUT_HANDLE );
CONSOLE_SCREEN_BUFFER_INFO csbi;
ZeroMemory( &csbi, sizeof( csbi ) );
if( ( hOutput != INVALID_HANDLE_VALUE )
&& ( GetFileType( hOutput ) == FILE_TYPE_CHAR )
&& GetConsoleScreenBufferInfo( hOutput, &csbi )
&& !csbi.dwCursorPosition.X
&& !csbi.dwCursorPosition.Y
&& ( csbi.dwSize.X > 0 )
&& ( csbi.dwSize.Y > 0 )
)
{
// increase console buffer size to 10000 lines and pause before exit
if( csbi.dwSize.Y < 10000 )
csbi.dwSize.Y = 10000;
SetConsoleScreenBufferSize( hOutput, csbi.dwSize );
atexit( pause );
}
}
//_set_printf_count_output( 1 ); // enable support for %n.
/* print version and license */
print_version();
printf( "\n" );
print_license();
printf( "\n\n" );
/* Create the global store 'G' and its descendants or die.
The global store holds all the stores that must be available globally.
*/
create_global_store();
/* G and its descendants have been created */
/* Initialize the global program store 'G->prog', a descendant of the global store.
The global program store holds basic info like command line arguments and system version.
'G' must be created before initializing the global program store.
*/
init_global_prog_store( argc, argv );
/* G->prog has been initialized */
/* Initialize the global configuration store 'G->config', a descendant of the global store.
The global configuration store holds the user-specified command line configuration.
'G->prog' must be initialized before initializing the global configuration store.
*/
init_global_config_store();
/* G->config has been initialized */
/* Initialize the global desktop store 'G->desktops', a descendant of the global store.
The global desktop store holds a linked list of attached to desktops and their heaps.
'G->config' must be initialized before initializing the global desktop store.
*/
init_global_desktop_store();
/* G->desktops has been initialized */
/* The global store is initialized */
if( G->config->verbose >= 5 )
print_global_store();
/* If the testlist is initialized the user requested testmode to run tests.
Else run gethooks() to take snapshots and print differences.
testmode() and gethooks() return nonzero on success, but main should return zero on success.
*/
return ( ( G->config->testlist->init_time ) ? !testmode() : !gethooks() );
}