This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
atropos.c
365 lines (326 loc) · 12.4 KB
/
atropos.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
357
358
359
360
361
362
363
364
365
/* Mode: C;
* Atropos frederic Bourgeois https://gitlab.com/fredbcode
*/
#include <sys/time.h>
#include <time.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <sys/dir.h>
/* local include */
#include "vrrpd.h"
#ifdef use_linux_libc5
#endif
typedef u_int32_t u32;
typedef u_int16_t u16;
typedef u_int8_t u8;
#if defined(SIOCGPARAMS) && SIOCGPARAMS != SIOCDEVPRIVATE+3
#error Changed definition for SIOCGPARAMS
#else
#define SIOCGPARAMS (SIOCDEVPRIVATE+3) /* Read operational parameters. */
#define SIOCSPARAMS (SIOCDEVPRIVATE+4) /* Set operational parameters. */
#endif
#define DATA_MAX 10000
int max_monitor = 9;
char version1[10] = "1.0";
char buff[80];
char data[DATA_MAX];
char pidend[6] = ".pid";
char separetepath[2] = "/";
int monitor;
int pid;
int ix = 0;
int max_mac = 6;
// Five loop with twice SIGNAL
int max_count = 5;
int nb;
int retval;
static char PidDir[FILENAME_MAX+1];
char namepid[FILENAME_MAX+1]="vrrpd_";
char temp[FILENAME_MAX+1];
char finalfile[FILENAME_MAX+1];
DIR *currentDir;
struct dirent *fichier;
const char shortopts[] = "aA:bB:fF:Mn:sS:hH:p:rRsvVw?";
struct option longopts[] = {
/* { name has_arg *flag val } */
{"state", 0, 0, 's'},
{"help", 0, 0, 'h'},
{"backup", 0, 0, 'b'},
{"reduce", 0, 0, 'r'},
{"increase", 0, 0, 'i'},
{"master", 0, 0, 'm'},
{"version", 0, 0, 'v'},
{"error", 0, 0, '?'},
{ 0, 0, 0, 0 }
};
char ligne[80];
int cpt = 0;
char buf[80];
int s, i;
FILE * filestate;
FILE *f;
unsigned int opt_a = 0, /* Show-all-interfaces flag. */ /* Verbose flag. */
state = 0,
version = 0,
backup = 0,
master = 0,
increase = 0,
reduce = 0;
void fonctinfo() {
FILE * child_process = popen("ps -e |grep vrrpd | wc -l", "r");
fgets(buf, sizeof(buf), child_process);
pclose(child_process);
printf("############################################################\n\n");
if (child_process == NULL)
fprintf(stdout,"Could not open pipe:\n");
if ( !strncmp(buf, "0", 1 )) {
fprintf(stdout, "\n");
fprintf(stdout,"VRRPD RUN: 0\n");
exit(0);
}
snprintf( PidDir, sizeof(PidDir), "%s", VRRP_PIDDIR_DFL );
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(finalfile,PidDir);
strcat(finalfile,separetepath);
strcat(finalfile,fichier->d_name);
if ((f = fopen(finalfile, "rb")) != NULL){
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
kill(pid,SIGUSR1);
char vrrp_tmp[FILENAME_MAX] = VRRP_PIDDIR_DFL;
sleep(1);
snprintf(temp, sizeof(temp), "/.vrrpstate%d", pid);
strcat(vrrp_tmp, temp);
if ((f = fopen(vrrp_tmp, "r")) != NULL){
while (fgets(data, DATA_MAX, f) != NULL)
{
printf("%s", data);
}
} else {
fprintf(stdout, "VRRP PID %d File %s STATE NOT FOUND \n", pid, vrrp_tmp);
}
}
finalfile[0]='\0';
printf("\n");
printf("############################################################\n\n");
}
}
}
// End Loop //
FILE *filestate = popen("ps -fC vrrpd", "r");
if (filestate != NULL){
while (fgets (ligne, sizeof ligne, filestate) != NULL){
cpt++;
printf ("%s",ligne);
}
fclose (filestate);
} else {
printf ("Erreur d'ouverture du fichier\n");
}
printf("\n");
for (ix=0; ix < max_mac; ix++) {
struct ifreq devea;
int ether = ix;
s = socket(AF_INET, SOCK_DGRAM, 0);
if (s < 0)
{
perror("socket");
exit(1);
}
sprintf(devea.ifr_name, "eth%d", ether);
if (ioctl(s, SIOCGIFHWADDR, &devea) == 0)
{
printf("eth%d Current MAC is: ",ether);
for (i = 0; i < max_mac; i++)
{
if (i < (max_mac - 1))
printf("%2.2x:", i[devea.ifr_hwaddr.sa_data] & 0xff);
if (i == (max_mac - 1))
printf("%2.2x", i[devea.ifr_hwaddr.sa_data] & 0xff);
}
printf("\n");
}
}
printf("\n");
char command[FILENAME_MAX+50];
snprintf(command,sizeof command,"grep -sh state %s/.*vrrpstate*",PidDir);
system(command);
snprintf(command,sizeof command,"rm %s/.*vrrpstate*",PidDir);
system(command);
printf("\n");
fprintf(stdout, "Be careful, Atropos doesn't show virtual mac address of vlan interface");
printf("\n");
fprintf(stdout, "Take a look at syslog for more informations");
printf("\n");
}
static void signal_end( int nosig )
{
fprintf(stdout,"\n");
signal( SIGINT, signal_end );
return;
}
int main(int argc, char **argv)
{
signal( SIGINT, signal_end );
int c, errflag = 0;
while ((c = getopt_long(argc, argv, shortopts, longopts, 0)) != EOF)
switch (c) {
case 's': state++; break;
case 'v': version++; break;
case 'b': backup++; break;
case 'r': reduce++; break;
case 'i': increase++; break;
case 'm': master++; break;
case '?': errflag++; break;
}
if (state) {
fonctinfo();
return 0;
};
if (version) {
fprintf(stdout,"Atropos %s\n", version1);
return 0;
};
if (reduce) {
snprintf( PidDir, sizeof(PidDir), "%s", VRRP_PIDDIR_DFL );
fprintf(stdout,"Please wait\n");
sleep (1);
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(finalfile,PidDir);
strcat(finalfile,separetepath);
strcat(finalfile,fichier->d_name);
if ((f = fopen(finalfile, "rb")) != NULL){
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
kill(pid,SIGTTIN);
finalfile[0]='\0';
}
}
}
}
fprintf(stdout,"DONE\n");
return 0;
};
if (increase) {
snprintf( PidDir, sizeof(PidDir), "%s", VRRP_PIDDIR_DFL );
fprintf(stdout,"Please wait\n");
sleep (1);
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(finalfile,PidDir);
strcat(finalfile,separetepath);
strcat(finalfile,fichier->d_name);
if ((f = fopen(finalfile, "rb")) != NULL){
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
kill(pid,SIGTTOU);
finalfile[0]='\0';
}
}
}
}
fprintf(stdout,"DONE\n");
return 0;
};
if (backup) {
snprintf( PidDir, sizeof(PidDir), "%s", VRRP_PIDDIR_DFL );
fprintf(stdout,"VRRPD BACKUP\n");
fprintf(stdout,"PLEASE WAIT\n");
for (ix = 0; ix <= max_count; ix++) {
fprintf(stderr, "\n");
fprintf(stderr,"Stage: %d|%d: ", ix, max_count);
sleep (1);
fprintf(stderr,"\b");
fprintf(stderr,"|");
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(finalfile,PidDir);
strcat(finalfile,separetepath);
strcat(finalfile,fichier->d_name);
if ((f = fopen(finalfile, "rb")) != NULL){
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
kill(pid,SIGUSR2);
finalfile[0]='\0';
}
}
}
}
sleep(1);
fprintf(stderr,"\b");
fprintf(stderr,"/");
sleep (1);
fprintf(stderr,"\b");
fprintf(stderr,"-");
sleep (1);
fprintf(stderr,"\b");
fprintf(stderr,"DONE");
if( NULL == ( currentDir = opendir(PidDir))) {
perror( "opendir()" );
} else {
while( NULL != ( fichier = readdir( currentDir ))) {
if(!strncmp(namepid, fichier->d_name, 6) ){
strcpy(finalfile,PidDir);
strcat(finalfile,separetepath);
strcat(finalfile,fichier->d_name);
if ((f = fopen(finalfile, "rb")) != NULL){
fgets(buff, sizeof(buff), f);
fclose(f);
int pid = atoi(buff);
kill(pid,SIGUSR2);
finalfile[0]='\0';
}
}
}
}
}
fprintf(stdout,"\n");
fprintf(stdout,"OK \n");
fonctinfo();
return 0;
}
else {
fprintf(stdout,"\n");
fprintf(stdout,"Atropos %s frederic Bourgeois https://gitlab.com/fredbcode\n", version1);
fprintf(stdout,"\n");
fprintf(stdout,"atropos --backup Be backup (caution: Do not use with priority !)\n");
fprintf(stdout,"atropos --reduce Reduce priority dynamically priority -10\n");
fprintf(stdout," If vrrpd run with -z : Set the priority after SIGTTIN (not decrement as default)\n");
fprintf(stdout,"atropos --increase Increase priority dynamically +10 \n");
fprintf(stdout," If vrrpd run with -x : Set the priority after SIGTTOU (not increment as default)\n");
fprintf(stdout,"atropos --help This Page\n");
fprintf(stdout,"atropos --state Status \n");
fprintf(stdout,"atropos --version version \n");
fprintf(stdout,"It requires to be run as root \n");
return 0;
}
}