Path: blob/main/tools/regression/sockets/unix_cmsg/uc_check_time.c
96317 views
/*-1* Copyright (c) 2016 Maksym Sobolyev <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526#include <sys/cdefs.h>27#include <sys/time.h>28#include <time.h>2930#include "uc_check_time.h"3132static const struct timeval max_diff_tv = {.tv_sec = 1, .tv_usec = 0};33static const struct timespec max_diff_ts = {.tv_sec = 1, .tv_nsec = 0};3435int36uc_check_bintime(const struct bintime *mt)37{38struct timespec bt;3940bintime2timespec(mt, &bt);41return (uc_check_timespec_real(&bt));42}4344int45uc_check_timeval(const struct timeval *bt)46{47struct timeval ct, dt;4849if (gettimeofday(&ct, NULL) < 0)50return (-1);51timersub(&ct, bt, &dt);52if (!timercmp(&dt, &max_diff_tv, <))53return (-1);5455return (0);56}5758int59uc_check_timespec_real(const struct timespec *bt)60{61struct timespec ct;6263if (clock_gettime(CLOCK_REALTIME, &ct) < 0)64return (-1);65timespecsub(&ct, bt, &ct);66if (!timespeccmp(&ct, &max_diff_ts, <))67return (-1);6869return (0);70}7172int73uc_check_timespec_mono(const struct timespec *bt)74{75struct timespec ct;7677if (clock_gettime(CLOCK_MONOTONIC, &ct) < 0)78return (-1);79timespecsub(&ct, bt, &ct);80if (!timespeccmp(&ct, &max_diff_ts, <))81return (-1);8283return (0);84}858687