-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyncing.c
127 lines (117 loc) · 3.11 KB
/
syncing.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
// Every time a new .id file is created the main mirror_client proccess
// executes this program. Here the two children that are meant to do the syncing are created.
// Also this is where the SIGUSR signals are handled.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <sys/stat.h>
#include <signal.h>
#include <sys/wait.h>
#include "my_functions.h"
volatile sig_atomic_t retry = 1;
void readinterrupt(int signo){
printf("Caught %d, because the receiver child couldn't read from the pipe\n", signo);
}
void childerror(int signo){
retry++;
printf("Caught %d, because there was an error in a child process.\n", signo);
if (retry < 4)
{
printf("Attempting to retry the connection.\n");
}
else
{
printf("The connection is impossible, aborting.\n");
}
}
// Usage: common, id, new file, buffer, input, logfile, mirror
int main(int argc, char **argv){
pid_t sender, receiver, pid;
int status;
static struct sigaction act;
char message[100];
// Setting up the signal handler
sigfillset(&(act.sa_mask));
act.sa_flags = SA_RESTART;
act.sa_handler = childerror;
sigaction(SIGUSR1, &act, NULL);
act.sa_handler = readinterrupt;
sigaction(SIGUSR2, &act, NULL);
do {
// Two children are created to deal with the transfers
sender = fork();
if (sender < 0)
{
perror("Sender Fork Failed");
exit(2);
}
if (sender == 0)
{
execl("./sender", "sender", argv[1], argv[2], argv[3], argv[4], argv[5], argv[6], NULL);
perror("exec failed");
exit(2);
}
receiver = fork();
if (receiver < 0)
{
perror("Receiver Fork Failed");
exit(2);
}
if (receiver == 0)
{
execl("./receiver", "receiver", argv[1], argv[2], argv[3], argv[4], argv[7], argv[6], NULL);
perror("exec failed");
exit(2);
}
while ((pid = wait(&status)) > 0)
{
// Seeing how each child exited
if (pid == receiver)
{
if (WIFEXITED(status))
{
// They return 12 if everything went normally
if (WEXITSTATUS(status) == 12)
{
printf("Transfer from %s to %s.id is complete.\n", argv[3], argv[2]);
retry = 1;
}
// Check if the receiver had to wait for 30 seconds to receive any input
else if (WEXITSTATUS(status) == 3)
{
sprintf(message, "30_SEC_WAIT %s\n", argv[3]);
write_to_logfile(argv[6], message);
kill(sender, SIGKILL);
retry = 1;
}
else
{
kill(sender, SIGKILL);
}
}
}
else
{
if (WIFEXITED(status))
{
// They return 12 if everything went normally
if (WEXITSTATUS(status) == 12)
{
printf("Transfer from %s.id to %s is complete.\n", argv[2], argv[3]);
retry = 1;
}
else
{
kill(receiver, SIGKILL);
}
}
}
}
} while ((retry > 1) && (retry < 4));
retry = 1;
return 0;
}