xref: /freebsd-src/tools/regression/sockets/unix_cmsg/uc_check_time.c (revision 1d386b48a555f61cb7325543adbbb5c3f3407a66)
179847968SMaxim Sobolev /*-
279847968SMaxim Sobolev  * Copyright (c) 2016 Maksym Sobolyev <sobomax@FreeBSD.org>
379847968SMaxim Sobolev  * All rights reserved.
479847968SMaxim Sobolev  *
579847968SMaxim Sobolev  * Redistribution and use in source and binary forms, with or without
679847968SMaxim Sobolev  * modification, are permitted provided that the following conditions
779847968SMaxim Sobolev  * are met:
879847968SMaxim Sobolev  * 1. Redistributions of source code must retain the above copyright
979847968SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer.
1079847968SMaxim Sobolev  * 2. Redistributions in binary form must reproduce the above copyright
1179847968SMaxim Sobolev  *    notice, this list of conditions and the following disclaimer in the
1279847968SMaxim Sobolev  *    documentation and/or other materials provided with the distribution.
1379847968SMaxim Sobolev  *
1479847968SMaxim Sobolev  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1579847968SMaxim Sobolev  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1679847968SMaxim Sobolev  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1779847968SMaxim Sobolev  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1879847968SMaxim Sobolev  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1979847968SMaxim Sobolev  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2079847968SMaxim Sobolev  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2179847968SMaxim Sobolev  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2279847968SMaxim Sobolev  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2379847968SMaxim Sobolev  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2479847968SMaxim Sobolev  * SUCH DAMAGE.
2579847968SMaxim Sobolev  */
2679847968SMaxim Sobolev 
2779847968SMaxim Sobolev #include <sys/cdefs.h>
2879847968SMaxim Sobolev #include <sys/time.h>
2979847968SMaxim Sobolev #include <time.h>
3079847968SMaxim Sobolev 
3179847968SMaxim Sobolev #include "uc_check_time.h"
3279847968SMaxim Sobolev 
3379847968SMaxim Sobolev static const struct timeval max_diff_tv = {.tv_sec = 1, .tv_usec = 0};
3479847968SMaxim Sobolev static const struct timespec max_diff_ts = {.tv_sec = 1, .tv_nsec = 0};
3579847968SMaxim Sobolev 
3679847968SMaxim Sobolev int
uc_check_bintime(const struct bintime * mt)3779847968SMaxim Sobolev uc_check_bintime(const struct bintime *mt)
3879847968SMaxim Sobolev {
3979847968SMaxim Sobolev 	struct timespec bt;
4079847968SMaxim Sobolev 
4179847968SMaxim Sobolev 	bintime2timespec(mt, &bt);
4279847968SMaxim Sobolev 	return (uc_check_timespec_real(&bt));
4379847968SMaxim Sobolev }
4479847968SMaxim Sobolev 
4579847968SMaxim Sobolev int
uc_check_timeval(const struct timeval * bt)4679847968SMaxim Sobolev uc_check_timeval(const struct timeval *bt)
4779847968SMaxim Sobolev {
4879847968SMaxim Sobolev 	struct timeval ct, dt;
4979847968SMaxim Sobolev 
5079847968SMaxim Sobolev 	if (gettimeofday(&ct, NULL) < 0)
5179847968SMaxim Sobolev 		return (-1);
5279847968SMaxim Sobolev 	timersub(&ct, bt, &dt);
5379847968SMaxim Sobolev 	if (!timercmp(&dt, &max_diff_tv, <))
5479847968SMaxim Sobolev 		return (-1);
5579847968SMaxim Sobolev 
5679847968SMaxim Sobolev 	return (0);
5779847968SMaxim Sobolev }
5879847968SMaxim Sobolev 
5979847968SMaxim Sobolev int
uc_check_timespec_real(const struct timespec * bt)6079847968SMaxim Sobolev uc_check_timespec_real(const struct timespec *bt)
6179847968SMaxim Sobolev {
6279847968SMaxim Sobolev 	struct timespec ct;
6379847968SMaxim Sobolev 
6479847968SMaxim Sobolev 	if (clock_gettime(CLOCK_REALTIME, &ct) < 0)
6579847968SMaxim Sobolev 		return (-1);
66*6040822cSAlan Somers 	timespecsub(&ct, bt, &ct);
6779847968SMaxim Sobolev 	if (!timespeccmp(&ct, &max_diff_ts, <))
6879847968SMaxim Sobolev 		return (-1);
6979847968SMaxim Sobolev 
7079847968SMaxim Sobolev 	return (0);
7179847968SMaxim Sobolev }
7279847968SMaxim Sobolev 
7379847968SMaxim Sobolev int
uc_check_timespec_mono(const struct timespec * bt)7479847968SMaxim Sobolev uc_check_timespec_mono(const struct timespec *bt)
7579847968SMaxim Sobolev {
7679847968SMaxim Sobolev 	struct timespec ct;
7779847968SMaxim Sobolev 
7879847968SMaxim Sobolev 	if (clock_gettime(CLOCK_MONOTONIC, &ct) < 0)
7979847968SMaxim Sobolev 		return (-1);
80*6040822cSAlan Somers 	timespecsub(&ct, bt, &ct);
8179847968SMaxim Sobolev 	if (!timespeccmp(&ct, &max_diff_ts, <))
8279847968SMaxim Sobolev 		return (-1);
8379847968SMaxim Sobolev 
8479847968SMaxim Sobolev 	return (0);
8579847968SMaxim Sobolev }
86