~gnaservicesinc/tpad-project/tpad-project

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
/********************************************************************************* 
 *     COPYRIGHT NOTICE:
 *     Copyright © 2014 Andrew Smith (GNA SERVICES INC) <Andrew@GNAServicesInc.com>
 *     All Rights Reserved.
 *
 *   This file, tpad_tree.c , is part of tpad.
 *
 *   tpad 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.
 *
 *   tpad 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 tpad.  If not, see <http://www.gnu.org/licenses/>.
 ********************************************************************************/
#include "tpad_headers.h"
#include <pthread.h>
pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex1b = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2b = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex1c = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2c = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex3 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex4 = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t count_mutex     = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond_got_request  = PTHREAD_COND_INITIALIZER;
extern int tpad_wach_thread_set;
extern unsigned char M_HASH_SLOT_ONE[];
extern unsigned char M_HASH_SLOT_TWO[];
extern gboolean disable_file_watch;
extern int iexit;
//extern  pthread_t thread1, thread2;
extern  pthread_t rCurrent;
extern int modBalance;
extern int itog;
extern int iiit;
extern GtkSourceBuffer *mBuff;
extern gboolean save_locked;
extern GtkWidget *window;
extern gchar* origfile;
extern int madetemp;

int itog=0;
int iiit=1;
pthread_t rCurrent;
int iexit=0;
unsigned char M_HASH_SLOT_ONE[64];
unsigned char M_HASH_SLOT_TWO[64];
static void tpad_watch_copy_two_to_one();
static void tpad_watch_copy_one_to_two();
static void *tpad_watch_sig(void *arg);
static unsigned char* tpad_watch_file_hash_get(char file[]);
static void *tpad_watch_threadproc(void *arg);
static void tpad_watch_file_to_one(char* fpath);
static void tpad_watch_free_two_and_one();
static void tpad_watch_file_to_two(char* fpath);
static void tpad_watch_file_to_one(char* fpath);
static void tpad_watch_free_one();
static void tpad_watch_free_two();
static void copy_to_one(void* sString);
static void copy_to_two(void* sString);
static void tpad_watch_copy_thid(pthread_t ThisThread);


static void tpad_watch_copy_thid(pthread_t ThisThread){
		pthread_mutex_lock( &mutex1c );
	 while ( pthread_mutex_trylock(&mutex2c) ){
		pthread_mutex_unlock(&mutex1c); 
		pthread_yield();
		sleep(WATCH_STALL_TIME);
		pthread_mutex_lock(&mutex1c);
	}
	memcpy((void*) &rCurrent, (void*) &ThisThread,sizeof(pthread_t) );
	pthread_mutex_unlock(&mutex1c);
    	pthread_mutex_unlock(&mutex2c);

}
static void copy_to_one(void* sString){
		pthread_mutex_lock( &mutex1 );
	 while ( pthread_mutex_trylock(&mutex2) ){
		pthread_mutex_unlock(&mutex1); 
		pthread_yield();
		sleep(WATCH_STALL_TIME);
		pthread_mutex_lock(&mutex1);
	}
	memcpy( (void*) M_HASH_SLOT_ONE, (void*) sString, 64);
	pthread_mutex_unlock(&mutex1);
    	pthread_mutex_unlock(&mutex2);
}
static void copy_to_two(void* sString){
		pthread_mutex_lock( &mutex1b );
	 while ( pthread_mutex_trylock(&mutex2b) ){
		pthread_mutex_unlock(&mutex1b); 
		pthread_yield();
		sleep(WATCH_STALL_TIME);
		pthread_mutex_lock(&mutex1b);
	}
	memcpy( (void*) M_HASH_SLOT_TWO, (void*) sString, 64);
	pthread_mutex_unlock(&mutex1b);
    	pthread_mutex_unlock(&mutex2b);
}
static void tpad_watch_file_to_one(char* fpath){
	if( fpath != NULL && strlen(fpath) > 2) copy_to_one((void*)tpad_watch_file_hash_get(fpath));
}
static void tpad_watch_file_to_two(char* fpath){
	if( fpath != NULL && strlen(fpath) > 2) copy_to_two((void*)tpad_watch_file_hash_get(fpath));
}
static void tpad_watch_free_one(){
	pthread_mutex_lock( &mutex1 );
	 while ( pthread_mutex_trylock(&mutex2) ){
		pthread_mutex_unlock(&mutex1);
		pthread_yield(); 
		sleep(WATCH_STALL_TIME);
		pthread_mutex_lock(&mutex1);
	}
	memset( (void*) M_HASH_SLOT_ONE, 0, 64);
	pthread_mutex_unlock(&mutex1);
    	pthread_mutex_unlock(&mutex2);
}
static void tpad_watch_free_two(){
	pthread_mutex_lock( &mutex1b );
	 while ( pthread_mutex_trylock(&mutex2b) ){
		pthread_mutex_unlock(&mutex1b); 
		pthread_yield();
		sleep(WATCH_STALL_TIME);
		pthread_mutex_lock(&mutex1b);
	}
	memset( (void*) M_HASH_SLOT_TWO, 0, 64);
	pthread_mutex_unlock(&mutex1b);
    	pthread_mutex_unlock(&mutex2b);
}

static void tpad_watch_copy_two_to_one(){
	copy_to_one(M_HASH_SLOT_TWO);
}	
static void tpad_watch_copy_one_to_two(){
	copy_to_two(M_HASH_SLOT_ONE);
}
static void tpad_watch_free_two_and_one(){
	tpad_watch_free_one();
	tpad_watch_free_two();
}
static void tpad_watch_check_file_init(char* fpath){
	tpad_watch_free_two_and_one();
	tpad_watch_file_to_one( (char*) fpath);
	tpad_watch_copy_one_to_two();
	
}
static unsigned char* tpad_watch_file_hash_get(char file[]){

	if(file == NULL) return("--\n");	
	if(strlen(file) <= 2 || strlen(file) > FILENAME_MAX ) return("--\n");	
	if(access(file, R_OK ) == -1) return("--\n");
	
	return ((unsigned char*) str2sha512(tpad_hash_read_in_file(file)));

}

static  void *tpad_watch_threadproc(void *arg){
	tpad_watch_copy_thid((pthread_t) pthread_self() );
	if (disable_file_watch) return(NULL); 
	
	if(iexit){
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	}
 
	char* fpath = (char*) arg;
	tpad_watch_check_file_init(fpath);
if(iexit){
		tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
}

do{ 
	if(iexit){
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	}
	pthread_mutex_lock( &mutex3 );
		if(iexit){
		tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
		pthread_mutex_unlock(&mutex3);
		 return(NULL);
		}
	 while ( pthread_mutex_trylock(&mutex4) ){
		pthread_mutex_unlock(&mutex3);
		
		if(iexit) {
			tpad_modbal_set_zero();
			tpad_watch_free_two_and_one();
			return(NULL);
			}
		else pthread_yield();
		
		struct timespec asleeprqtp;
		asleeprqtp.tv_sec  = 0;
		asleeprqtp.tv_nsec = 500000000L;
		if(nanosleep(&asleeprqtp , NULL) != 0 ) sleep(WATCH_PROC_SLEEP_LENGTH);
		if(iexit) {
			tpad_modbal_set_zero();
			tpad_watch_free_two_and_one();
			return(NULL);
			}
		else pthread_yield();
	
		pthread_mutex_lock(&mutex3);
	}

		if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}

 
	if(!tpad_modbal_check_armed()){

	if (memcmp( (void*) M_HASH_SLOT_ONE, (void*) tpad_watch_file_hash_get(fpath), 64) != 0){
		fprintf(stdout,"FILE CHANGED!\n");
		fflush(stdout);	
		if(iexit) {
		tpad_modbal_set_zero();
		tpad_watch_free_two_and_one();
		pthread_mutex_unlock(&mutex3);
    		pthread_mutex_unlock(&mutex4);
		return(NULL);  
		}
		else tpad_modbal_set_one();

		tpad_watch_check_file_init(fpath);
	  }else pthread_yield();
	}else pthread_yield();
		if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}
	//else {
	//IS Armed 
		 
	//}
	struct timespec lsleeprqtp;
			if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}
	lsleeprqtp.tv_sec  = 0;
			if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}
	lsleeprqtp.tv_nsec = 500000000L;
			if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}
	if(nanosleep(&lsleeprqtp , NULL) != 0 ) sleep(WATCH_PROC_SLEEP_LENGTH);
			if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	return(NULL);  
	}
	pthread_mutex_unlock(&mutex3);
    	pthread_mutex_unlock(&mutex4);
	if(iexit) {
	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
	return(NULL);  
	} else pthread_yield();
   } while(!iexit);

	tpad_modbal_set_zero();
	tpad_watch_free_two_and_one();
return(NULL);  

}

void tpad_watch_exit(){

// Disabled for now //
/*
	iexit=1;
	if(rCurrent && tpad_wach_thread_set) pthread_join((pthread_t)rCurrent,NULL);
	tpad_wach_thread_set=FALSE;
	rCurrent=(pthread_t) NULL;
*/
}

void tpad_watch_fname(){
// Disabled for now //
/*
	pthread_t thread;
	tpad_wach_thread_set=TRUE;
	pthread_create( &thread, NULL, &tpad_watch_threadproc, (void*) tpad_fp_get_current());
	*/
  	
}