timer_subr.c revision 224144
1224006Shrs/* $FreeBSD: head/usr.sbin/rtadvd/timer_subr.c 224144 2011-07-17 19:24:54Z hrs $ */ 2224006Shrs/* $KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $ */ 3224006Shrs 4224006Shrs/* 5224006Shrs * Copyright (C) 1998 WIDE Project. 6224006Shrs * All rights reserved. 7224006Shrs * 8224006Shrs * Redistribution and use in source and binary forms, with or without 9224006Shrs * modification, are permitted provided that the following conditions 10224006Shrs * are met: 11224006Shrs * 1. Redistributions of source code must retain the above copyright 12224006Shrs * notice, this list of conditions and the following disclaimer. 13224006Shrs * 2. Redistributions in binary form must reproduce the above copyright 14224006Shrs * notice, this list of conditions and the following disclaimer in the 15224006Shrs * documentation and/or other materials provided with the distribution. 16224006Shrs * 3. Neither the name of the project nor the names of its contributors 17224006Shrs * may be used to endorse or promote products derived from this software 18224006Shrs * without specific prior written permission. 19224006Shrs * 20224006Shrs * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 21224006Shrs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22224006Shrs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23224006Shrs * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 24224006Shrs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25224006Shrs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26224006Shrs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27224006Shrs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28224006Shrs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29224006Shrs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30224006Shrs * SUCH DAMAGE. 31224006Shrs */ 32224006Shrs 33224006Shrs#include <sys/time.h> 34224006Shrs#include <sys/queue.h> 35224006Shrs#include <sys/socket.h> 36224006Shrs#include <syslog.h> 37224006Shrs#include <stdio.h> 38224144Shrs#include <inttypes.h> 39224006Shrs 40224006Shrs#include "timer.h" 41224006Shrs#include "timer_subr.h" 42224006Shrs 43224006Shrsstruct timeval * 44224006Shrsrtadvd_timer_rest(struct rtadvd_timer *rat) 45224006Shrs{ 46224006Shrs static struct timeval returnval, now; 47224006Shrs 48224006Shrs gettimeofday(&now, NULL); 49224006Shrs if (TIMEVAL_LEQ(&rat->rat_tm, &now)) { 50224006Shrs syslog(LOG_DEBUG, 51224006Shrs "<%s> a timer must be expired, but not yet", 52224006Shrs __func__); 53224006Shrs returnval.tv_sec = returnval.tv_usec = 0; 54224006Shrs } 55224006Shrs else 56224006Shrs TIMEVAL_SUB(&rat->rat_tm, &now, &returnval); 57224006Shrs 58224006Shrs return (&returnval); 59224006Shrs} 60224006Shrs 61224006Shrs/* result = a + b */ 62224006Shrsvoid 63224006ShrsTIMEVAL_ADD(struct timeval *a, struct timeval *b, struct timeval *result) 64224006Shrs{ 65224006Shrs long l; 66224006Shrs 67224006Shrs if ((l = a->tv_usec + b->tv_usec) < MILLION) { 68224006Shrs result->tv_usec = l; 69224006Shrs result->tv_sec = a->tv_sec + b->tv_sec; 70224006Shrs } 71224006Shrs else { 72224006Shrs result->tv_usec = l - MILLION; 73224006Shrs result->tv_sec = a->tv_sec + b->tv_sec + 1; 74224006Shrs } 75224006Shrs} 76224006Shrs 77224006Shrs/* 78224006Shrs * result = a - b 79224006Shrs * XXX: this function assumes that a >= b. 80224006Shrs */ 81224006Shrsvoid 82224006ShrsTIMEVAL_SUB(struct timeval *a, struct timeval *b, struct timeval *result) 83224006Shrs{ 84224006Shrs long l; 85224006Shrs 86224006Shrs if ((l = a->tv_usec - b->tv_usec) >= 0) { 87224006Shrs result->tv_usec = l; 88224006Shrs result->tv_sec = a->tv_sec - b->tv_sec; 89224006Shrs } 90224006Shrs else { 91224006Shrs result->tv_usec = MILLION + l; 92224006Shrs result->tv_sec = a->tv_sec - b->tv_sec - 1; 93224006Shrs } 94224006Shrs} 95224006Shrs 96224006Shrschar * 97224144Shrssec2str(uint32_t s, char *buf) 98224006Shrs{ 99224144Shrs uint32_t day; 100224144Shrs uint32_t hour; 101224144Shrs uint32_t min; 102224144Shrs uint32_t sec; 103224006Shrs char *p; 104224144Shrs 105224006Shrs min = s / 60; 106224006Shrs sec = s % 60; 107224006Shrs 108224006Shrs hour = min / 60; 109224006Shrs min = min % 60; 110224006Shrs 111224006Shrs day = hour / 24; 112224006Shrs hour = hour % 24; 113224006Shrs 114224006Shrs p = buf; 115224006Shrs if (day > 0) 116224144Shrs p += sprintf(p, "%" PRIu32 "d", day); 117224006Shrs if (hour > 0) 118224144Shrs p += sprintf(p, "%" PRIu32 "h", hour); 119224006Shrs if (min > 0) 120224144Shrs p += sprintf(p, "%" PRIu32 "m", min); 121224006Shrs 122224144Shrs if ((p == buf) || (sec > 0 && p > buf)) 123224144Shrs sprintf(p, "%" PRIu32 "s", sec); 124224006Shrs 125224006Shrs return (buf); 126224006Shrs} 127