... | ... |
@@ -127,36 +127,6 @@ int mcomm_read(mcomm *c, fd_set *fds){ |
127 | 127 |
return(0); |
128 | 128 |
} |
129 | 129 |
|
130 |
-int mcomm_fdset(mcomm *c, fd_set *rfds, fd_set *wfds) |
|
131 |
-{ |
|
132 |
- int i; |
|
133 |
- |
|
134 |
- /*----- listen socket -----*/ |
|
135 |
- if(moption.lisocket != -1){ |
|
136 |
- FD_SET(moption.lisocket, rfds); |
|
137 |
- } |
|
138 |
- |
|
139 |
- /*----- connect socket -----*/ |
|
140 |
- for(i=0;i<MAX_COMM;i++){ |
|
141 |
- if(c[i].fd[0] != -1){ |
|
142 |
- FD_SET(c[i].fd[0], rfds); |
|
143 |
- if(c[i].working){ |
|
144 |
- FD_SET(c[i].fd[0], wfds); |
|
145 |
- } |
|
146 |
- } |
|
147 |
- if(c[i].fd[1] != -1){ |
|
148 |
- FD_SET(c[i].fd[1], rfds); |
|
149 |
- }else{ |
|
150 |
- if(c[i].cpid){ |
|
151 |
- if(waitpid(c[i].cpid, NULL, WNOHANG) == c[i].cpid){ |
|
152 |
- c[i].cpid = 0; |
|
153 |
- } |
|
154 |
- } |
|
155 |
- } |
|
156 |
- } |
|
157 |
- return(0); |
|
158 |
-} |
|
159 |
- |
|
160 | 130 |
int mfdirchk(mfile *d){ |
161 | 131 |
mfile *m; |
162 | 132 |
int len = strlen(d->fn); |
... | ... |
@@ -223,6 +193,11 @@ int is_send(mfile *m) |
223 | 223 |
return(1); |
224 | 224 |
} |
225 | 225 |
|
226 |
+void rfdset(int s, fd_set *fds) |
|
227 |
+{ |
|
228 |
+ FD_SET(s, fds); |
|
229 |
+} |
|
230 |
+ |
|
226 | 231 |
void wfdset(int s, fd_set *fds) |
227 | 232 |
{ |
228 | 233 |
mfile *m; |
... | ... |
@@ -234,6 +209,35 @@ void wfdset(int s, fd_set *fds) |
234 | 234 |
} |
235 | 235 |
} |
236 | 236 |
|
237 |
+void cfdset(mcomm *c, fd_set *rfds, fd_set *wfds) |
|
238 |
+{ |
|
239 |
+ int i; |
|
240 |
+ |
|
241 |
+ /*----- listen socket -----*/ |
|
242 |
+ if(moption.lisocket != -1){ |
|
243 |
+ FD_SET(moption.lisocket, rfds); |
|
244 |
+ } |
|
245 |
+ |
|
246 |
+ /*----- connect socket -----*/ |
|
247 |
+ for(i=0;i<MAX_COMM;i++){ |
|
248 |
+ if(c[i].fd[0] != -1){ |
|
249 |
+ FD_SET(c[i].fd[0], rfds); |
|
250 |
+ if(c[i].working){ |
|
251 |
+ FD_SET(c[i].fd[0], wfds); |
|
252 |
+ } |
|
253 |
+ } |
|
254 |
+ if(c[i].fd[1] != -1){ |
|
255 |
+ FD_SET(c[i].fd[1], rfds); |
|
256 |
+ }else{ |
|
257 |
+ if(c[i].cpid){ |
|
258 |
+ if(waitpid(c[i].cpid, NULL, WNOHANG) == c[i].cpid){ |
|
259 |
+ c[i].cpid = 0; |
|
260 |
+ } |
|
261 |
+ } |
|
262 |
+ } |
|
263 |
+ } |
|
264 |
+} |
|
265 |
+ |
|
237 | 266 |
int do_select(fd_set *rfds, fd_set *wfds) |
238 | 267 |
{ |
239 | 268 |
struct timeval tv; |
... | ... |
@@ -242,7 +246,6 @@ int do_select(fd_set *rfds, fd_set *wfds) |
242 | 242 |
if(select(1024, rfds, wfds, NULL, &tv) <= 0){ |
243 | 243 |
gettimeofday(&curtime, NULL); |
244 | 244 |
moption.sendready = 0; |
245 |
- mrecv_gc(); |
|
246 | 245 |
return(-1); |
247 | 246 |
} |
248 | 247 |
gettimeofday(&curtime, NULL); |
... | ... |
@@ -257,6 +260,11 @@ void do_pong() |
257 | 257 |
} |
258 | 258 |
} |
259 | 259 |
|
260 |
+void do_free() |
|
261 |
+{ |
|
262 |
+ mrecv_gc(); |
|
263 |
+} |
|
264 |
+ |
|
260 | 265 |
void do_recv() |
261 | 266 |
{ |
262 | 267 |
while(mrecv()); |
... | ... |
@@ -295,19 +303,20 @@ void mloop() |
295 | 295 |
while(loop_flag){ |
296 | 296 |
FD_ZERO(&rfds); |
297 | 297 |
FD_ZERO(&wfds); |
298 |
- FD_SET(moption.mcsocket, &rfds); |
|
298 |
+ rfdset(moption.mcsocket, &wfds); |
|
299 | 299 |
wfdset(moption.mcsocket, &wfds); |
300 |
- mcomm_fdset(moption.comm, &rfds, &wfds); |
|
300 |
+ cfdset(moption.comm, &rfds, &wfds); |
|
301 | 301 |
if(do_select(&rfds, &wfds)){ |
302 | 302 |
do_pong(); |
303 |
- continue; |
|
303 |
+ do_free(); |
|
304 |
+ }else{ |
|
305 |
+ do_pong(); |
|
306 |
+ do_recv(); |
|
307 |
+ do_send(); |
|
308 |
+ mcomm_check(moption.comm); /* exec check */ |
|
309 |
+ mcomm_accept(moption.comm, &rfds); /* new console */ |
|
310 |
+ mcomm_read(moption.comm, &rfds); /* command exec */ |
|
304 | 311 |
} |
305 |
- do_pong(); |
|
306 |
- do_recv(); |
|
307 |
- do_send(); |
|
308 |
- mcomm_check(moption.comm); /* exec check */ |
|
309 |
- mcomm_accept(moption.comm, &rfds); /* new console */ |
|
310 |
- mcomm_read(moption.comm, &rfds); /* command exec */ |
|
311 | 312 |
} |
312 | 313 |
} |
313 | 314 |
|
... | ... |
@@ -271,7 +271,7 @@ int mexec_close(mcomm *c, int n) |
271 | 271 |
while(c->exclude){ |
272 | 272 |
mexec_exclude_del(c, c->exclude); |
273 | 273 |
} |
274 |
- for(m=mftop[0];m;m=m->next){ |
|
274 |
+ for(m=mftop[MFSEND];m;m=m->next){ |
|
275 | 275 |
if(m->comm == c){ |
276 | 276 |
m->comm = NULL; |
277 | 277 |
lprintf(3, "%s: cancel %s\n", __func__, m->fn); |
... | ... |
@@ -503,7 +503,6 @@ int mexec_check(mcomm *c, int n) |
503 | 503 |
{ |
504 | 504 |
int e; |
505 | 505 |
int i; |
506 |
- int r; |
|
507 | 506 |
ssize_t size; |
508 | 507 |
char *argv[9]; |
509 | 508 |
char *fn = NULL; |
... | ... |
@@ -593,19 +592,10 @@ int mexec_check(mcomm *c, int n) |
593 | 593 |
/*----- md5 -----*/ |
594 | 594 |
h = (mhash *)m->mdata.data; |
595 | 595 |
h->fnlen = strlen(m->fn); |
596 |
- r = md5sum(m->fd, h->hash); |
|
597 |
- close(m->fd); |
|
598 |
- m->fd = -1; |
|
599 |
- if(r == -1){ |
|
600 |
- e = errno; |
|
601 |
- lprintf(0, "[error] %s: %s %s\n", __func__, strerror(e), m->fn); |
|
602 |
- cprintf(0, c, "error: %s %s\n", strerror(e), m->fn); |
|
603 |
- mfdel(m); |
|
604 |
- return(0); |
|
605 |
- } |
|
606 | 596 |
memcpy(h->filename, m->fn, h->fnlen); |
607 | 597 |
m->mdata.head.szdata = sizeof(mhash) + h->fnlen; |
608 | 598 |
h->fnlen = htons(h->fnlen); |
599 |
+ MD5_Init(&(m->md5)); |
|
609 | 600 |
|
610 | 601 |
/*----- sendto address -----*/ |
611 | 602 |
if(t){ |
... | ... |
@@ -874,41 +874,27 @@ static void mrecv_req_send(mdata *data, struct sockaddr_in *addr) |
874 | 874 |
|
875 | 875 |
static void mrecv_req_md5_open(mfile *m, mdata *data, struct sockaddr_in *addr) |
876 | 876 |
{ |
877 |
- int r; |
|
878 |
- int l; |
|
877 |
+ int l; |
|
879 | 878 |
mhash *h; |
880 | 879 |
|
881 | 880 |
if(!m){ |
882 | 881 |
m = mfadd(MFRECV); |
883 |
- if(!m){ |
|
884 |
- lprintf(0,"[error] %s: out of memory\n", __func__); |
|
885 |
- return; |
|
886 |
- } |
|
887 | 882 |
memcpy(&(m->addr), addr, sizeof(m->addr)); |
888 | 883 |
memcpy(&(m->mdata.head), &(data->head), sizeof(m->mdata.head)); |
889 | 884 |
h = (mhash *)(data->data); |
890 | 885 |
l = ntohs(h->fnlen); |
891 | 886 |
memcpy(m->fn, h->filename, l); |
892 | 887 |
m->fn[l] = 0; |
888 |
+ memcpy(m->mdata.data, h->hash, 16); |
|
893 | 889 |
m->fd = open(m->fn, O_RDONLY); |
894 | 890 |
if(m->fd == -1){ |
895 | 891 |
m->mdata.head.error = errno; |
896 | 892 |
m->mdata.head.nstate = MAKUO_RECVSTATE_OPENERROR; |
897 | 893 |
}else{ |
898 |
- r = md5sum(m->fd, m->mdata.data); |
|
899 |
- close(m->fd); |
|
900 |
- m->fd = -1; |
|
901 |
- if(r == -1){ |
|
902 |
- lprintf(0, "%s: file read error %s\n", __func__, m->fn); |
|
903 |
- m->mdata.head.error = errno; |
|
904 |
- m->mdata.head.nstate = MAKUO_RECVSTATE_READERROR; |
|
905 |
- }else{ |
|
906 |
- if(!memcmp(m->mdata.data, data->data, 16)){ |
|
907 |
- m->mdata.head.nstate = MAKUO_RECVSTATE_MD5OK; |
|
908 |
- }else{ |
|
909 |
- m->mdata.head.nstate = MAKUO_RECVSTATE_MD5NG; |
|
910 |
- } |
|
911 |
- } |
|
894 |
+ m->mdata.head.nstate = MAKUO_RECVSTATE_OPEN; |
|
895 |
+ m->link = mkack(&(m->mdata), &(m->addr), MAKUO_RECVSTATE_NONE); |
|
896 |
+ m->link->link = m; |
|
897 |
+ MD5_Init(&(m->md5)); |
|
912 | 898 |
} |
913 | 899 |
} |
914 | 900 |
mtimeget(&(m->lastrecv)); |
... | ... |
@@ -917,15 +903,20 @@ static void mrecv_req_md5_open(mfile *m, mdata *data, struct sockaddr_in *addr) |
917 | 917 |
|
918 | 918 |
static void mrecv_req_md5_close(mfile *m, mdata *data, struct sockaddr_in *addr) |
919 | 919 |
{ |
920 |
+ if(m){ |
|
921 |
+ if(m->link){ |
|
922 |
+ close(m->fd); |
|
923 |
+ m->fd = -1; |
|
924 |
+ MD5_Final(m->mdata.data, &(m->md5)); |
|
925 |
+ m->link->mdata.head.nstate = MAKUO_RECVSTATE_CLOSE; |
|
926 |
+ m->link->link = NULL; |
|
927 |
+ m->link = NULL; |
|
928 |
+ } |
|
929 |
+ mrecv_mfdel(m); |
|
930 |
+ } |
|
920 | 931 |
msend(mkack(data, addr, MAKUO_RECVSTATE_CLOSE)); |
921 |
- mrecv_mfdel(m); |
|
922 | 932 |
} |
923 | 933 |
|
924 |
-/* |
|
925 |
- * md5チェック要求を受け取ったときの処理 |
|
926 |
- * mfileオブジェクトを生成して |
|
927 |
- * 対象ファイルのmd5を取得する |
|
928 |
- */ |
|
929 | 934 |
static void mrecv_req_md5(mdata *data, struct sockaddr_in *addr) |
930 | 935 |
{ |
931 | 936 |
mfile *m = mrecv_req_search(data, addr); |
... | ... |
@@ -201,6 +201,41 @@ static void msend_ack_send(int s, mfile *m) |
201 | 201 |
|
202 | 202 |
static void msend_ack_md5(int s, mfile *m) |
203 | 203 |
{ |
204 |
+ int r; |
|
205 |
+ char hash[16]; |
|
206 |
+ char buff[8192]; |
|
207 |
+ mfile *d = m->link; |
|
208 |
+ if(!d){ |
|
209 |
+ msend_shot(s, m); |
|
210 |
+ return; |
|
211 |
+ } |
|
212 |
+ r = read(d->fd, buff, sizeof(buff)); |
|
213 |
+ if(r > 0){ |
|
214 |
+ MD5_Update(&(d->md5), buff, r); |
|
215 |
+ return; |
|
216 |
+ } |
|
217 |
+ if(r == -1){ |
|
218 |
+ if(errno == EINTR){ |
|
219 |
+ return; |
|
220 |
+ } |
|
221 |
+ d->mdata.head.error = errno; |
|
222 |
+ d->mdata.head.nstate = MAKUO_RECVSTATE_READERROR; |
|
223 |
+ lprintf(0, "[error] %s: file read error %s\n", __func__, d->fn); |
|
224 |
+ MD5_Final(hash, &(d->md5)); |
|
225 |
+ }else{ |
|
226 |
+ MD5_Final(hash, &(d->md5)); |
|
227 |
+ if(!memcmp(hash, d->mdata.data, 16)){ |
|
228 |
+ d->mdata.head.nstate = MAKUO_RECVSTATE_MD5OK; |
|
229 |
+ }else{ |
|
230 |
+ d->mdata.head.nstate = MAKUO_RECVSTATE_MD5NG; |
|
231 |
+ } |
|
232 |
+ } |
|
233 |
+ m->mdata.head.error = d->mdata.head.error; |
|
234 |
+ m->mdata.head.nstate = d->mdata.head.nstate; |
|
235 |
+ close(d->fd); |
|
236 |
+ d->fd = -1; |
|
237 |
+ d->link = NULL; |
|
238 |
+ m->link = NULL; |
|
204 | 239 |
msend_shot(s, m); |
205 | 240 |
} |
206 | 241 |
|
... | ... |
@@ -674,6 +709,33 @@ static void msend_req_send(int s, mfile *m) |
674 | 674 |
|
675 | 675 |
static void msend_req_md5_open_init(int s, mfile *m) |
676 | 676 |
{ |
677 |
+ int e; |
|
678 |
+ int r; |
|
679 |
+ mhash *h; |
|
680 |
+ char buff[8192]; |
|
681 |
+ |
|
682 |
+ h = (mhash *)m->mdata.data; |
|
683 |
+ r = read(m->fd, buff, sizeof(buff)); |
|
684 |
+ if(r > 0){ |
|
685 |
+ MD5_Update(&(m->md5), buff, r); |
|
686 |
+ return; |
|
687 |
+ } |
|
688 |
+ e = errno; |
|
689 |
+ if(r == -1){ |
|
690 |
+ if(e == EINTR){ |
|
691 |
+ return; |
|
692 |
+ } |
|
693 |
+ lprintf(0, "[error] %s: file read error(%s) %s\n", __func__, strerror(e), m->fn); |
|
694 |
+ cprintf(0, m->comm, "error: file read error(%s) %s\n", strerror(e), m->fn); |
|
695 |
+ MD5_Final(h->hash, &(m->md5)); |
|
696 |
+ close(m->fd); |
|
697 |
+ m->fd = -1; |
|
698 |
+ msend_mfdel(m); |
|
699 |
+ return; |
|
700 |
+ } |
|
701 |
+ MD5_Final(h->hash, &(m->md5)); |
|
702 |
+ close(m->fd); |
|
703 |
+ m->fd = -1; |
|
677 | 704 |
m->sendwait = 1; |
678 | 705 |
m->initstate = 0; |
679 | 706 |
ack_clear(m, -1); |
... | ... |
@@ -691,7 +753,14 @@ static void msend_req_md5_open(int s, mfile *m) |
691 | 691 |
msend_packet(s, &(m->mdata), &(m->addr)); |
692 | 692 |
return; |
693 | 693 |
} |
694 |
+ if(ack_check(m, MAKUO_RECVSTATE_OPEN) == 1){ |
|
695 |
+ m->initstate = 0; |
|
696 |
+ m->sendwait = 1; |
|
697 |
+ ack_clear(m, MAKUO_RECVSTATE_OPEN); |
|
698 |
+ return; |
|
699 |
+ } |
|
694 | 700 |
m->initstate = 1; |
701 |
+ m->sendwait = 0; |
|
695 | 702 |
m->mdata.head.nstate = MAKUO_SENDSTATE_CLOSE; |
696 | 703 |
} |
697 | 704 |
|
... | ... |
@@ -721,6 +790,20 @@ static void msend_req_md5_close(int s, mfile *m) |
721 | 721 |
/*----- md5 -----*/ |
722 | 722 |
static void msend_req_md5(int s, mfile *m) |
723 | 723 |
{ |
724 |
+ if(!m->comm){ |
|
725 |
+ if(m->mdata.head.nstate == MAKUO_SENDSTATE_OPEN){ |
|
726 |
+ if(m->initstate){ |
|
727 |
+ MD5_Final(m->mdata.data, &(m->md5)); |
|
728 |
+ close(m->fd); |
|
729 |
+ m->fd = -1; |
|
730 |
+ msend_mfdel(m); |
|
731 |
+ return; |
|
732 |
+ }else{ |
|
733 |
+ m->initstate = 1; |
|
734 |
+ m->mdata.head.nstate = MAKUO_SENDSTATE_CLOSE; |
|
735 |
+ } |
|
736 |
+ } |
|
737 |
+ } |
|
724 | 738 |
switch(m->mdata.head.nstate){ |
725 | 739 |
case MAKUO_SENDSTATE_OPEN: |
726 | 740 |
msend_req_md5_open(s, m); |