-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.c
More file actions
255 lines (219 loc) · 6.94 KB
/
manager.c
File metadata and controls
255 lines (219 loc) · 6.94 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include "ADTQueue.h"
#include "ADTList.h"
#define READ 0
#define WRITE 1
#define BUFFSIZE 16384
char* get_pipeName(pid_t workerId);
void listener(char* args[]);
char* get_filename(char* buffer);
int* create_int(int value) {
int* p = malloc(sizeof(int));
*p = value;
return p;
}
/* Global variables for sigint handler */
int end = 1;
/* SIGINT Handler */
void sig_int(int signo){
pid_t stoppedWorker;
int status;
end = 0;
/* Find every stopped process and kill it */
while((stoppedWorker = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){
kill(SIGKILL, stoppedWorker);
}
}
/* Global variable for sigchld handler */
int workerAvailable = 0;
/* SIGCHLD Handler */
void sig_chld(int signo){
signal(SIGCHLD, sig_chld);
workerAvailable = 1;
}
int main(int argc, char* argv[]){
/* Signals */
static struct sigaction act;
act.sa_handler = sig_chld;
act.sa_flags = 0;
act.sa_flags |= SA_RESTART;
sigaction(SIGCHLD, &act, NULL);
static struct sigaction act_int;
act_int.sa_handler = sig_int;
sigaction(SIGINT, &act_int, NULL);
/* Get path to monitor */
char* mpath;
if(argc > 1){
mpath = argv[2];
}
else{
mpath = ".";
}
int fd[2];
/* Create the pipe */
if(pipe(fd) == -1){
perror("pipe call");
exit(1);
}
/* Create child process */
pid_t pid = fork();
if(pid < 0){
perror("fork call");
exit(2);
}
/* Listener */
if(pid == 0){
dup2(fd[WRITE], STDOUT_FILENO);
close(fd[READ]); // listener is writing
char* args[] = {"inotifywait", mpath, "-m", "-e", "create", "-e", "moved_to", NULL};
listener(args);
}
/* Priority Queue for workers */
Queue q = q_create(free);
pid_t pq_pid;
int readfd, writefd, md;
/* Manager is reading */
char buffer[BUFFSIZE];
pid_t pid2, workerId=0, stoppedWorker;
char* pipeName;
char* pipeNumber, *buf;
int* max, length, status, *pip;
while(end){
if(workerAvailable){
/* Get stopped processes */
while((stoppedWorker = waitpid(-1, &status, WNOHANG|WUNTRACED)) > 0){
q_insert(q, create_int(stoppedWorker));
}
}
ssize_t bytes = read(fd[READ], buffer, BUFFSIZE);
if(bytes > 0){
/* Get the filename */
buf = strndup(buffer, bytes);
printf("\n\n%s", buf);
char* filename = get_filename(buf);
/* If there are no available workers we fork */
if(q_size(q) == 0){
pid2 = fork();
stoppedWorker = 0;
/* Create Worker */
if(pid2 < 0){
perror("fork call");
exit(2);
}
else if(pid2 != 0){
/* Create named pipe */
workerId = pid2;
pipeName = get_pipeName(workerId);
printf("Created named pipe %s \n", pipeName);
if((mkfifo(pipeName, 0666) < 0) && (errno != EEXIST)){
perror("Cant create fifo");
}
/* Parent writes in pipe */
printf("Manager writing in pipe %s \n", pipeName);
if((writefd = open(pipeName, O_WRONLY)) < 0){
perror("Manager cant open write fifo");
exit(1);
}
if(write(writefd, filename, strlen(filename)) != strlen(filename)){
perror("write error");
exit(4);
}
free(pipeName);
close(writefd);
}
if(pid2 == 0){
/* Get named pipe name */
workerId = getpid();
pipeName = get_pipeName(workerId);
printf("Calling worker with pipeName : %s \n", pipeName);
char* args[] = {"./worker", pipeName};
execvp(args[0], args);
}
}
/* If there are available workers in queue we SIGCONT the first one and remove
him from queue */
else{
/* Get named pipe name */
pip = q_first(q);
workerId = *pip;
pipeName = get_pipeName(workerId);
printf("Child to be continued is %d \n", workerId);
/* Continue stopped worker */
kill(workerId, SIGCONT);
/* Parent writes in pipe */
printf("Second manager writing in pipe %s \n", pipeName);
writefd = (open(pipeName, O_WRONLY));
if((writefd = (open(pipeName, O_WRONLY))) < 0){
perror("Manager cant open write fifo");
exit(1);
}
if(write(writefd, filename, strlen(filename)) != strlen(filename)){
perror("Manager write error");
exit(4);
}
close(writefd);
/* Remove it from queue */
q_remove_first(q);
free(pipeName);
}
free(filename);
free(buf);
}
else if(bytes == 0){
break;
}
}
/* Free Memory */
q_destroy(q);
return 1;
}
char* get_filename(char* buffer){
int counter = 0;
char* token, *filename;
char* p = malloc(strlen(buffer)+1);
strcpy(p, buffer);
token = strtok(p, " ");
char* path = malloc((strlen(token)+1));
strcpy(path, token);
while(token != NULL){
token = strtok(NULL, " ");
counter++;
if(counter == 2){
filename = malloc(strlen(token)+strlen(path)+2);
strcpy(filename, path);
strcat(filename, token);
}
}
/* inotifywait output is like "./ CREATE <filename>\n" so i remove the \n*/
filename[strlen(filename)-1] = '\0';
char* newLine;
int index;
if((newLine = strchr(filename, '\n')) != NULL){
index = (int)(newLine - filename);
filename[index] = '\0';
}
free(p); free(path);
return filename;
}
char* get_pipeName(pid_t workerId){
int length;
char* pipeName, *pipeNumber;
/* Get length of worker id number */
length = snprintf(NULL, 0, "%d", workerId) + 1;
pipeNumber = malloc(length);
snprintf(pipeNumber, length+1, "%d", workerId);
/* Name will be of type "/tmp/fifo.(workerId)" */
pipeName = malloc(length+13);
strcpy(pipeName, "/tmp/fifo.");
strcat(pipeName, pipeNumber);
free(pipeNumber);
return pipeName;
}