1、__io_canceled在lib.h中定义,
/* IO cancelation */
extern volatile sig_atomic_t __io_canceled;
__io_canceled变量影响下列函数,
static inline int read_n(int fd, char *buf, int len)
{
register int t=0, w;
while (!__io_canceled && len > 0)
{
if( (w = read(fd, buf, len)) < 0 )
{
if( errno == EINTR || errno == EAGAIN )
continue;
return -1;
}
if( !w )
return 0;
len -= w; buf += w; t += w;
}
return t;
}
static inline int write_n(int fd, char *buf, int len)
{
register int t=0, w;
while (!__io_canceled && len > 0)
{
if( (w = write(fd, buf, len)) < 0 )
{
if( errno == EINTR || errno == EAGAIN )
continue;
return -1;
}
if( !w ) return 0;
len -= w; buf += w; t += w;
}
return t;
}
2、下面的函数改变__io_canceled变量,
static inline void io_init(void)
{
__io_canceled = 0;
}
static inline void io_cancel(void)
{
__io_canceled = 1;
}
3、
而io_cancel函数在下面函数中调用,
static void sig_term(int sig)
{
vtun_syslog(LOG_INFO, "Closing connection");
io_cancel();
linker_term = VTUN_SIG_TERM;
}
static void sig_hup(int sig)
{
vtun_syslog(LOG_INFO, "Reestablishing connection");
io_cancel();
linker_term = VTUN_SIG_HUP;
}
当内核发出退出或挂起等信号时,会调用sig_term或sig_hup函数,从而改变__io_canceled的值,从而停止read_n write_n.