1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2016 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include "trio.h"
22 #include "PollVec.h"
23 
operator <(const timeval & a,const timeval & b)24 static inline bool operator<(const timeval& a,const timeval& b)
25 {
26    if(a.tv_sec!=b.tv_sec)
27       return a.tv_sec<b.tv_sec;
28    return a.tv_usec<b.tv_usec;
29 }
30 
AddTimeoutU(unsigned t)31 void PollVec::AddTimeoutU(unsigned t)
32 {
33    struct timeval new_timeout={static_cast<time_t>(t/1000000),static_cast<suseconds_t>(t%1000000)};
34    if(tv_timeout.tv_sec<0 || new_timeout<tv_timeout)
35       SetTimeout(new_timeout);
36 }
37 
AddFD(int fd,int mask)38 void PollVec::AddFD(int fd,int mask)
39 {
40    if(mask&IN)
41       FD_SET(fd,&in);
42    if(mask&OUT)
43       FD_SET(fd,&out);
44    if(nfds<=fd)
45       nfds=fd+1;
46 }
FDReady(int fd,int mask)47 bool PollVec::FDReady(int fd,int mask)
48 {
49    bool res=false;
50    if(mask&IN)
51       res|=(!FD_ISSET(fd,&in_polled) || FD_ISSET(fd,&in_ready));
52    if(mask&OUT)
53       res|=(!FD_ISSET(fd,&out_polled) || FD_ISSET(fd,&out_ready));
54    return res;
55 }
FDSetNotReady(int fd,int mask)56 void PollVec::FDSetNotReady(int fd,int mask)
57 {
58    if(mask&IN)
59       FD_CLR(fd,&in_ready);
60    if(mask&OUT)
61       FD_CLR(fd,&out_ready);
62 }
63 
Block()64 void  PollVec::Block()
65 {
66    if(nfds<1 && tv_timeout.tv_sec<0)
67    {
68       /* dead lock */
69       fprintf(stderr,_("%s: BUG - deadlock detected\n"),"PollVec::Block");
70       tv_timeout.tv_sec=1;
71    }
72 
73    in_ready=in_polled=in;
74    out_ready=out_polled=out;
75    timeval *select_timeout=0;
76    if(tv_timeout.tv_sec!=-1)
77       select_timeout=&tv_timeout;
78    select(nfds,&in_ready,&out_ready,0,select_timeout);
79 }
80