-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargusd.c
313 lines (254 loc) · 7.14 KB
/
argusd.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
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include "argus.h"
#define SIZE 1024
#define DEFAULT 100
int ter=0;
int exe=0;
int tempoExec=DEFAULT;
int tempoInat=DEFAULT;
int minhaVez=0;
int bytesEscritos=0;
char** execucao=NULL;
//handler para retirar uma tarefa da lista de tarefas em execucao
void hdl(int sig, siginfo_t * siginfo, void *context){
int j=0;
char * pidString = malloc(sizeof(char*));
int value = siginfo->si_pid;
snprintf(pidString, 10, "%d", value);
for(j;j<exe;j++){
if(strstr(execucao[j],pidString) != NULL) {
execucao[j]='\0';
break;
}
}
for(int i=j;i<exe;i++){
execucao[i]=execucao[i+1];
}
exe--;
}
//handler para o processo pai fazer wait pelo filho
void signalHandler(int signal)
{
wait(NULL);
}
//handler para o processo "filho" ignorar o SIGCHILD do processo "neto"
void signalIGNORA(int signal)
{
}
int main(int argc, char * argv[]){
struct sigaction act;
memset (&act, '\0', sizeof(act));
write(1,"BEM VINDO\n",10);
act.sa_sigaction = &hdl;
act.sa_flags = SA_SIGINFO;
if(sigaction(SIGUSR1, &act,NULL)<0){
perror("signal error");
exit(3);
}
if(signal(SIGCHLD, signalHandler)==SIG_ERR){
perror("signal SIGCHLDERRO");
exit(3);
}
execucao=malloc(24*sizeof(char*));
int fifo_fd=-1;
int fd2;
int fd;
//fifo CLIENTE->SEVIDOR
mkfifo("FIFO",0666);
//fifo SERVIDOR->CLIENTE
mkfifo("FIFO2",0666);
int r;
char buffer[1024];
int bytes=0;
int servpid = getpid();
int fw=open("logs.txt",O_WRONLY|O_CREAT|O_TRUNC,0600);
int fr=open("logs.txt",O_RDONLY,0600);
char *aux;
char *str1;
while(1) {
fd = open("FIFO",O_RDONLY);
while((r = read(fd,buffer,SIZE)) > 0) {
char * guarda = malloc(sizeof(strlen(buffer))-1);
int bytesaff = strlen(buffer);
if(buffer[0]=='m'){
strcpy(guarda,buffer+1);
tempoExec=atoi(guarda);
write(1,"tempo de execucao definido\n",28);
}
else if(buffer[0]=='i'){
strcpy(guarda,buffer+1);
tempoInat=atoi(guarda);
write(1,"tempo de Inatividade entre pipes anonimos definido\n",51);
}
int pipe_paiFilho[2];
if(pipe(pipe_paiFilho)<0){
perror("pipe");
exit(1);
}
if(fork()==0){
if(signal(SIGCHLD, signalIGNORA)==SIG_ERR){
perror("signal sigalarm");
exit(3);
}
close(pipe_paiFilho[0]);
int pid;
int status;
switch(buffer[0]){
//executar tarefa
case('e'):
strcpy(guarda,buffer+1);
char TAREFAPID[1024];
strcpy(TAREFAPID,guarda);
strcat(TAREFAPID, " " );
strcat(TAREFAPID, "PID-");
strcat(guarda, "\n");
if((pid=fork())==0){
write(1,"tarefa recebida\n",16);
char * bufferAA = malloc(sizeof(char*));
int value = getpid();
snprintf(bufferAA, 10, "%d", value);
strcat(TAREFAPID,bufferAA);
write(pipe_paiFilho[1],TAREFAPID,strlen(TAREFAPID));
close(pipe_paiFilho[1]);
int z= executa(buffer+1, tempoExec, tempoInat,servpid);
kill(servpid,SIGUSR1);
if(z==0){ write(1,"Tarefa concluida com sucesso\n",29); _exit(1);} //normal
if(z==2){ write(1,"Tempo de inatividade atingido\n",30); _exit(2);} //pipes anonumos tempo
_exit(0); //tempo de exec acabou
}
//pai do processo que vai executar a tarefa
else{
close(pipe_paiFilho[1]);
int status;
wait(&status);
int escrever = WEXITSTATUS(status);
while(minhaVez==1){
sleep(1);
}
minhaVez=1;
if(escrever==2){
char esc[bytesaff + 22];
strcpy(esc,"TEMPO DE INATIVIDADE: ");
strcat(esc,guarda);
bytesEscritos=write(fw,esc,strlen(esc));
}
if(escrever==0){
char esc[bytesaff + 19];
strcpy(esc,"TEMPO DE EXECUCAO: ");
strcat(esc,guarda);
bytesEscritos=write(fw,esc,strlen(esc));
}
if(escrever==1){
char esc[bytesaff + 23];
strcpy(esc,"CONCLUIDO COM SUCESSO: ");
strcat(esc,guarda);
bytesEscritos=write(fw,esc,strlen(esc));
}
minhaVez=0;
}
break;
//historico
case('r'):
close(pipe_paiFilho[1]);
if(ter==0){
if((fd2=open("FIFO2",O_WRONLY))<0){
perror("open");
}
aux="Nao existem registos de tarefas concluidas\n";
write(fd2,aux,strlen(aux)+1);
}
else{
char * res = (char*) malloc(sizeof(100));
int ler;
if((fd2=open("FIFO2",O_WRONLY))<0){
perror("open");
}
while((ler=read(fr,res,1000))>0){
write(fd2,res,ler);
}
close(fd2);
lseek(fr,0,SEEK_SET);
}
break;
case('m'):
close(pipe_paiFilho[1]);
break;
case('i'):
close(pipe_paiFilho[1]);
break;
//listar tarefas em execucao
case('l'):
close(pipe_paiFilho[1]);
if(exe==0){
aux="Nao existem tarefas em execucao\n";
if((fd2=open("FIFO2",O_WRONLY))<0){
perror("open");
}
write(fd2,aux,strlen(aux)+1);
}
else{
while(minhaVez==1){
sleep(1);
}
for(int i=0;i<exe;i++){
int length = snprintf( NULL, 0, "%d", i+1 );
str1 = malloc( length + 1 );
snprintf(str1, length + 1, "%d", i+1 );
strcat(str1,": ");
strcat(str1,execucao[i]);
strcat(str1,"\n");
if((fd2=open("FIFO2",O_WRONLY))<0){
perror("open");
}
write(fd2,str1,strlen(str1)+1);
close(fd2);
}
}
break;
//terminar tarefa em execucao
case('t'):
close(pipe_paiFilho[1]);
kill(atoi(guarda),SIGUSR1);
break;
default:
close(pipe_paiFilho[1]);
aux="nao percebi o comando\n";
if((fd2=open("FIFO2",O_WRONLY))<0){
perror("open");
}
write(fd2,aux,strlen(aux)+1);
break;
}
_exit(0);
}
close(pipe_paiFilho[1]);
if(buffer[0]=='e'){
char * tarefa = (char*) malloc(100) ;
char b;
int i=0;
while((r = read(pipe_paiFilho[0],&b,1)) > 0){
tarefa[i]=b;
i++;
}
char final[i];
strcpy(final,tarefa);
execucao[exe]=strdup(final);
exe++;
close(pipe_paiFilho[0]);
}
else{
close(pipe_paiFilho[0]);
}
ter++;
}
}
return 0;
}