Blame lib/timer_settime.c

Packit 0b5880
/*
Packit 0b5880
 * Check: a unit test framework for C
Packit 0b5880
 * Copyright (C) 2001, 2002 Arien Malec
Packit 0b5880
 *
Packit 0b5880
 * This library is free software; you can redistribute it and/or
Packit 0b5880
 * modify it under the terms of the GNU Lesser General Public
Packit 0b5880
 * License as published by the Free Software Foundation; either
Packit 0b5880
 * version 2.1 of the License, or (at your option) any later version.
Packit 0b5880
 *
Packit 0b5880
 * This library is distributed in the hope that it will be useful,
Packit 0b5880
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0b5880
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 0b5880
 * Lesser General Public License for more details.
Packit 0b5880
 *
Packit 0b5880
 * You should have received a copy of the GNU Lesser General Public
Packit 0b5880
 * License along with this library; if not, write to the
Packit 0b5880
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
Packit 0b5880
 * MA 02110-1301, USA.
Packit 0b5880
 */
Packit 0b5880
Packit 0b5880
#include "libcompat.h"
Packit 0b5880
Packit 0b5880
int timer_settime(timer_t timerid CK_ATTRIBUTE_UNUSED,
Packit 0b5880
                  int flags CK_ATTRIBUTE_UNUSED,
Packit 0b5880
                  const struct itimerspec *new_value,
Packit 0b5880
                  struct itimerspec *old_value CK_ATTRIBUTE_UNUSED)
Packit 0b5880
{
Packit 0b5880
#ifdef HAVE_SETITIMER
Packit 0b5880
    /*
Packit 0b5880
     * If the system does not have timer_settime() but does have
Packit 0b5880
     * setitimer() use that instead of alarm().
Packit 0b5880
     */
Packit 0b5880
    struct itimerval interval;
Packit 0b5880
Packit 0b5880
    interval.it_value.tv_sec = new_value->it_value.tv_sec;
Packit 0b5880
    interval.it_value.tv_usec = new_value->it_value.tv_nsec / 1000;
Packit 0b5880
    interval.it_interval.tv_sec = new_value->it_interval.tv_sec;
Packit 0b5880
    interval.it_interval.tv_usec = new_value->it_interval.tv_nsec / 1000;
Packit 0b5880
Packit 0b5880
    return setitimer(ITIMER_REAL, &interval, NULL);
Packit 0b5880
#else
Packit 0b5880
    int seconds = new_value->it_value.tv_sec;
Packit 0b5880
Packit 0b5880
    /* 
Packit 0b5880
     * As the alarm() call has only second precision, if the caller
Packit 0b5880
     * specifies partial seconds, we round up to the nearest second.
Packit 0b5880
     */
Packit 0b5880
    if(new_value->it_value.tv_nsec > 0)
Packit 0b5880
    {
Packit 0b5880
        seconds += 1;
Packit 0b5880
    }
Packit 0b5880
Packit 0b5880
    alarm(seconds);
Packit 0b5880
Packit 0b5880
    return 0;
Packit 0b5880
#endif
Packit 0b5880
}