-
Notifications
You must be signed in to change notification settings - Fork 2
/
synexec_master_slaveset.c
246 lines (225 loc) · 6.58 KB
/
synexec_master_slaveset.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
/*
* ------------------------------------
* synexec - Synchronised Executioner
* ------------------------------------
* synexec_master_slaveset.c
* ---------------------------
* Copyright 2014 (c) Citrix
*
* This program 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, version only.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*
* Read the README file for the changelog and information on how to
* compile and use this program.
*/
// Header files
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/time.h>
#include <unistd.h>
#include "synexec_master_slaveset.h"
#include "synexec_master_comm.h"
#include "synexec_common.h"
// Global variables
extern int verbose;
/*
* static int
* slave_remove(slaveset_t *slaveset, slave_t *slave_aux);
* -------------------------------------------------------
* This function removes 'slave_aux' from 'slaveset', if present. It will also
* free() all associated memory.
*
* Mandatory params: slaveset, slave_aux
* Optional params :
*
* Return values:
* 0 'slave_aux' not present
* 1 'slave_aux' successfully removed
*/
static int
slave_remove(slaveset_t *slaveset, slave_t *slave_aux){
// Local variables
int ret = 0; // Return code
slave_t *slave_aux_a = NULL; // Auxiliary slave_t
slave_t *slave_aux_b = NULL; // Auxiliary slave_t
// Check if its the first slave
if (!memcmp(slaveset->slave, slave_aux, sizeof(*slave_aux))){
slave_aux_a = slave_aux->next;
free(slave_aux);
slaveset->slave = slave_aux_a;
ret = 1;
goto out;
}
// Go through the list, aux_b is always the parent
slave_aux_b = slaveset->slave;
slave_aux_a = slaveset->slave->next;
while(slave_aux_a){
if (!memcmp(slave_aux_a, slave_aux, sizeof(*slave_aux))){
slave_aux_b->next = slave_aux_a->next;
free(slave_aux);
ret = 1;
goto out;
}
slave_aux_b = slave_aux_a;
slave_aux_a = slave_aux_a->next;
}
out:
// Return
return(ret);
}
/*
* int
* slaveset_probe(slaveset_t *slaveset);
* -------------------------------------
* This function pings all slaves in slaveset and removes unresponding
* slaves from the list. It returns the number of slaves that responded.
*
* Mandatory params: slaveset
* Optional params :
*
* Return values:
* Number of slaves alive in set
*/
int
slaveset_probe(slaveset_t *slaveset){
// Local variables
slave_t *slave_aux; // Auxiliary slave_t
slave_t *slave_aux_b; // Auxiliary slave_t
// Run through all the slaves in the set, counting
slaveset->active = 0;
if (verbose > 1){
printf("%s: Validating current slaveset.\n", __FUNCTION__);
fflush(stdout);
}
slave_aux = slaveset->slave;
while(slave_aux){
if (slave_probe(slave_aux) < 0){
// Close its socket
if (slave_aux->slave_fd >= 0){
(void)close(slave_aux->slave_fd);
slave_aux->slave_fd = -1;
}
slave_aux_b = slave_aux->next;
//printf("Removing dead slave: '%s:%hu'\n", inet_ntoa(slave_aux->slave_addr.sin_addr), ntohs(slave_aux->slave_addr.sin_port));
slave_remove(slaveset, slave_aux);
slave_aux = slave_aux_b;
}else{
slaveset->active++;
//printf("Worker alive: '%s:%hu'\n", inet_ntoa(slave_aux->slave_addr.sin_addr), ntohs(slave_aux->slave_addr.sin_port));
slave_aux = slave_aux->next;
}
}
// Return number of slaves alive
return(slaveset->active);
}
/*
* int
* slave_in_list(slaveset_t *slaveset, struct sockaddr_in *slave_addr);
* --------------------------------------------------------------------
* This function checks if 'slave_addr' is present in 'slaveset'.
*
* Mandatory params: slaveset, slave_addr
* Optional params :
*
* Return values:
* 1 'slave_addr' is in 'slaveset'
* 0 'slave_addr' is not in 'slaveset'
*/
int
slave_in_list(slaveset_t *slaveset, struct sockaddr_in *slave_addr){
// Local variables
slave_t *slave_aux;
int ret = 0;
// Run through the list returning if we found the guy
slave_aux = slaveset->slave;
while(slave_aux){
if (!memcmp(&(slave_addr->sin_addr), &(slave_aux->slave_addr.sin_addr), sizeof(struct sockaddr_in))){
// Found
ret = 1;
break;
}
slave_aux = slave_aux->next;
}
// Return
return(ret);
}
/*
* int
* slave_add(slaveset_t *slaveset, struct sockaddr_in *slave_addr,
* int slave_sock);
* ---------------------------------------------------------------
* This function adds 'slave_addr' and 'slave_sock' to 'slaveset'.
*
* Mandatory params: slaveset, slave_addr, slave_sock
* Optional params :
*
* Return values:
* -1 Error
* 0 Already in list
* 1 Inserted
*/
int
slave_add(slaveset_t *slaveset, struct sockaddr_in *slave_addr, int slave_sock){
// Local variables
slave_t *slave_aux = NULL; // Auxiliary slave_t
int err = 0; // Return code
// Check if already in list
if (slave_in_list(slaveset, slave_addr)){
if (verbose > 1){
printf("%s: Slave (%s) already in list.\n", __FUNCTION__, inet_ntoa(slave_addr->sin_addr));
fflush(stdout);
}
goto out;
}
// Allocate new slave_t
if ((slave_aux = (slave_t *)calloc(1, sizeof(slave_t))) == NULL){
perror("calloc");
fprintf(stderr, "%s: Error allocating new slave.\n", __FUNCTION__);
goto err;
}
// Fill slave contents
memcpy(&(slave_aux->slave_addr), slave_addr, sizeof(struct sockaddr_in));
slave_aux->slave_fd = slave_sock;
// Insert it into list
slave_aux->next = slaveset->slave;
slaveset->slave = slave_aux;
err = 1;
out:
// Return
return(err);
err:
if (slave_aux){
free(slave_aux);
}
if (slave_sock >= 0){
(void)close(slave_sock);
}
err = -1;
goto out;
}
void
slave_times(slaveset_t *slaveset){
slave_t *slave;
slave = slaveset->slave;
while (slave){
printf("Slave %s:%hu, %ld.%ld -> %ld.%ld\n",
inet_ntoa(slave->slave_addr.sin_addr), ntohs(slave->slave_addr.sin_port),
slave->slave_time[0].tv_sec, slave->slave_time[0].tv_usec,
slave->slave_time[1].tv_sec, slave->slave_time[1].tv_usec);
fflush(stdout);
slave = slave->next;
}
}