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