static void
do_wait(globalstate *gstate)

{
    struct timeval wait;

    double2tv(&wait, gstate->delay);
    select(0, NULL, NULL, NULL, &wait);
}
示例#2
0
/* start timer in special interval */
int start_timer( double interval )
{
    struct itimerval itv;

    if( interval <= EPSINON )
    {
        msg_log( L_ERR,
                 "%s: timer interval is zero\n",
                 __func__);
        return -1;
    }

    double2tv( &itv.it_interval, interval );
    bcopy( &itv.it_interval, &itv.it_value, sizeof( struct timeval ) );

    //msg_log( L_ERR, "%d.%d\n", sec, usec );

    /*
     * start a ITIMER_REAL timer.
     * ITIMER_VIRTUAL and ITIMER_PROF timer is not update its value
     * when this process is not run by CPU,
     * and if we call resvmsg then CPU queue this process in block-state,
     * until packet come and change to running -state.
     * so ITIMER_VIRTUAL and ITIMER_PROF timer will not expire when we expected
     */
    if( setitimer( ITIMER_REAL, &itv, NULL ) == -1 )
    {
        msg_log( L_ERR,
                 "%s: %s\n",
                 __func__,
                 strerror(errno) );
        return -1;
    }

    return 0;
}
static void
do_command(globalstate *gstate)

{
    int status;
    struct timeval wait = {0, 0};
    struct timeval now;
    fd_set readfds;
    unsigned char ch;

    /* calculate new refresh time */
    gstate->refresh = gstate->now;
    double2tv(&now, gstate->delay);
    timeradd(&now, &gstate->refresh, &gstate->refresh);
    time_get(&now);

    /* loop waiting for time to expire */
    do {
	/* calculate time to wait */
	if (gstate->delay > 0)
	{
	    wait = gstate->refresh;
	    wait.tv_usec -= now.tv_usec;
	    if (wait.tv_usec < 0)
	    {
		wait.tv_usec += 1000000;
		wait.tv_sec--;
	    }
	    wait.tv_sec -= now.tv_sec;
	}

	/* set up arguments for select on stdin (0) */
	FD_ZERO(&readfds);
	FD_SET(STDIN_FILENO, &readfds);

	/* wait for something to read or time out */
	if (select(32, &readfds, NULL, NULL, &wait) > 0)
	{
	    /* read it */
	    if (read(STDIN_FILENO, &ch, 1) != 1)
	    {
		/* read error */
		message_error(" Read error on stdin");
		quit(EX_DATAERR);
		/*NOTREACHED*/
	    }

	    /* mark pending messages as old */
	    message_mark();

	    /* dispatch */
	    status = command_process(gstate, (int)ch);
	    switch(status)
	    {
	    case CMD_ERROR:
		quit(EX_SOFTWARE);
		/*NOTREACHED*/
		
	    case CMD_REFRESH:
		return;

	    case CMD_UNKNOWN:
		message_error(" Unknown command");
		break;

	    case CMD_NA:
		message_error(" Command not available");
	    }
	}

	/* get new time */
	time_get(&now);
    } while (timercmp(&now, &(gstate->refresh), < ));
}