#include #include #include #include #include #include #define TRUE 1 #define FALSE 0 #define CHECK_TIMEOUT 10 typedef void Sigfunc(int); /* Private variables */ static sigjmp_buf timeout; static void connection_timeout(int); Sigfunc *signal(int signo, Sigfunc *func); void set_alarm_handler(void *func); int action(void); void checker(void); static void connection_timeout(int sig) { siglongjmp(timeout, TRUE); } /** * Replace the standard signal() function, with a more reliable * using sigaction. From W. Richard Stevens' "Advanced Programming * in the UNIX Environment" */ Sigfunc *signal(int signo, Sigfunc *func) { struct sigaction act, oact; act.sa_handler = func; sigemptyset(&act.sa_mask); act.sa_flags = 0; if (signo == SIGALRM) { #ifdef SA_INTERRUPT act.sa_flags |= SA_INTERRUPT; /* SunOS */ #endif } else { #ifdef SA_RESTART act.sa_flags |= SA_RESTART; /* SVR4, 44BSD */ #endif } if (sigaction(signo, &act, &oact) < 0) return(SIG_ERR); return(oact.sa_handler); } /** * Set a handler for the alarm signal, * SIGALRM using sigaction. */ void set_alarm_handler(void *func) { struct sigaction sa; sa.sa_handler= func; sa.sa_flags= 0; sigemptyset(&sa.sa_mask); sigaction(SIGALRM, &sa, NULL); } int action(void) { int i; int fd; char buf[1025]; fd= open("/home/chopp/isos/Mandrake82-cd1-inst.i586.iso", O_RDONLY); if ( fd < 0 ) { return -1; } for ( i = 0; i < 1024000; i ++ ) { if( read(fd, buf, 1024) != 1024 ) { return -1; } } close(fd); return 1; } void checker(void) { int action_return = 0; if ( sigsetjmp(timeout, TRUE) ) { goto label_timeout; } /* Set a timeout handler and activate the timer */ set_alarm_handler(connection_timeout); alarm(CHECK_TIMEOUT); action_return = action(); alarm(0); if ( action_return ) { printf("Exiting successfully!\n"); } else { printf("Exiting non successfully!\n"); } return; label_timeout: printf("Timeout occured!\n"); return; } int main () { checker(); }