xref: /dflybsd-src/usr.sbin/rtadvd/timer_subr.c (revision c17e6018cab90fba83aa80d4974cfbeb9a5e75ca)
1*c17e6018SMatthew Dillon /*	$FreeBSD: stable/10/usr.sbin/rtadvd/timer_subr.c 253970 2013-08-05 20:13:02Z hrs $	*/
2*c17e6018SMatthew Dillon /*	$KAME: timer.c,v 1.9 2002/06/10 19:59:47 itojun Exp $	*/
3*c17e6018SMatthew Dillon 
4*c17e6018SMatthew Dillon /*
5*c17e6018SMatthew Dillon  * Copyright (C) 1998 WIDE Project.
6*c17e6018SMatthew Dillon  * All rights reserved.
7*c17e6018SMatthew Dillon  *
8*c17e6018SMatthew Dillon  * Redistribution and use in source and binary forms, with or without
9*c17e6018SMatthew Dillon  * modification, are permitted provided that the following conditions
10*c17e6018SMatthew Dillon  * are met:
11*c17e6018SMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12*c17e6018SMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13*c17e6018SMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14*c17e6018SMatthew Dillon  *    notice, this list of conditions and the following disclaimer in the
15*c17e6018SMatthew Dillon  *    documentation and/or other materials provided with the distribution.
16*c17e6018SMatthew Dillon  * 3. Neither the name of the project nor the names of its contributors
17*c17e6018SMatthew Dillon  *    may be used to endorse or promote products derived from this software
18*c17e6018SMatthew Dillon  *    without specific prior written permission.
19*c17e6018SMatthew Dillon  *
20*c17e6018SMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21*c17e6018SMatthew Dillon  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22*c17e6018SMatthew Dillon  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23*c17e6018SMatthew Dillon  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24*c17e6018SMatthew Dillon  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25*c17e6018SMatthew Dillon  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26*c17e6018SMatthew Dillon  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*c17e6018SMatthew Dillon  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28*c17e6018SMatthew Dillon  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29*c17e6018SMatthew Dillon  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30*c17e6018SMatthew Dillon  * SUCH DAMAGE.
31*c17e6018SMatthew Dillon  */
32*c17e6018SMatthew Dillon 
33*c17e6018SMatthew Dillon #include <sys/queue.h>
34*c17e6018SMatthew Dillon #include <sys/socket.h>
35*c17e6018SMatthew Dillon #include <syslog.h>
36*c17e6018SMatthew Dillon #include <stdio.h>
37*c17e6018SMatthew Dillon #include <inttypes.h>
38*c17e6018SMatthew Dillon #include <time.h>
39*c17e6018SMatthew Dillon 
40*c17e6018SMatthew Dillon #include "timer.h"
41*c17e6018SMatthew Dillon #include "timer_subr.h"
42*c17e6018SMatthew Dillon 
43*c17e6018SMatthew Dillon struct timespec *
rtadvd_timer_rest(struct rtadvd_timer * rat)44*c17e6018SMatthew Dillon rtadvd_timer_rest(struct rtadvd_timer *rat)
45*c17e6018SMatthew Dillon {
46*c17e6018SMatthew Dillon 	static struct timespec returnval, now;
47*c17e6018SMatthew Dillon 
48*c17e6018SMatthew Dillon 	clock_gettime(CLOCK_MONOTONIC_FAST, &now);
49*c17e6018SMatthew Dillon 	if (TS_CMP(&rat->rat_tm, &now, <=)) {
50*c17e6018SMatthew Dillon 		syslog(LOG_DEBUG,
51*c17e6018SMatthew Dillon 		    "<%s> a timer must be expired, but not yet",
52*c17e6018SMatthew Dillon 		    __func__);
53*c17e6018SMatthew Dillon 		returnval.tv_sec = returnval.tv_nsec = 0;
54*c17e6018SMatthew Dillon 	}
55*c17e6018SMatthew Dillon 	else
56*c17e6018SMatthew Dillon 		TS_SUB(&rat->rat_tm, &now, &returnval);
57*c17e6018SMatthew Dillon 
58*c17e6018SMatthew Dillon 	return (&returnval);
59*c17e6018SMatthew Dillon }
60*c17e6018SMatthew Dillon 
61*c17e6018SMatthew Dillon char *
sec2str(uint32_t s,char * buf)62*c17e6018SMatthew Dillon sec2str(uint32_t s, char *buf)
63*c17e6018SMatthew Dillon {
64*c17e6018SMatthew Dillon 	uint32_t day;
65*c17e6018SMatthew Dillon 	uint32_t hour;
66*c17e6018SMatthew Dillon 	uint32_t min;
67*c17e6018SMatthew Dillon 	uint32_t sec;
68*c17e6018SMatthew Dillon 	char *p;
69*c17e6018SMatthew Dillon 
70*c17e6018SMatthew Dillon 	min = s / 60;
71*c17e6018SMatthew Dillon 	sec = s % 60;
72*c17e6018SMatthew Dillon 
73*c17e6018SMatthew Dillon 	hour = min / 60;
74*c17e6018SMatthew Dillon 	min = min % 60;
75*c17e6018SMatthew Dillon 
76*c17e6018SMatthew Dillon 	day = hour / 24;
77*c17e6018SMatthew Dillon 	hour = hour % 24;
78*c17e6018SMatthew Dillon 
79*c17e6018SMatthew Dillon 	p = buf;
80*c17e6018SMatthew Dillon 	if (day > 0)
81*c17e6018SMatthew Dillon 		p += sprintf(p, "%" PRIu32 "d", day);
82*c17e6018SMatthew Dillon 	if (hour > 0)
83*c17e6018SMatthew Dillon 		p += sprintf(p, "%" PRIu32 "h", hour);
84*c17e6018SMatthew Dillon 	if (min > 0)
85*c17e6018SMatthew Dillon 		p += sprintf(p, "%" PRIu32 "m", min);
86*c17e6018SMatthew Dillon 
87*c17e6018SMatthew Dillon 	if ((p == buf) || (sec > 0 && p > buf))
88*c17e6018SMatthew Dillon 		sprintf(p, "%" PRIu32 "s", sec);
89*c17e6018SMatthew Dillon 
90*c17e6018SMatthew Dillon 	return (buf);
91*c17e6018SMatthew Dillon }
92