Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 1 | /* |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 2 | * (C) 2016 by sysmocom - s.f.m.c. GmbH <info@sysmocom.de> |
| 3 | * Authors: Neels Hofmeyr <nhofmeyr@sysmocom.de> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 4 | * All Rights Reserved |
| 5 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | /*! \addtogroup timer |
| 25 | * @{ |
Pau Espin Pedrol | 8d82780 | 2018-02-28 18:28:53 +0100 | [diff] [blame] | 26 | * \file timer_gettimeofday.c |
| 27 | * Overriding Time: osmo_gettimeofday() |
| 28 | * - Useful to write and reproduce tests that depend on specific time |
| 29 | * factors. This API allows to fake the timeval provided by `gettimeofday()` |
| 30 | * by using a small shim osmo_gettimeofday(). |
| 31 | * - If the clock override is disabled (default) for a given clock, |
| 32 | * osmo_gettimeofday() will do the same as regular `gettimeofday()`. |
| 33 | * - If you want osmo_gettimeofday() to provide a specific time, you must |
| 34 | * enable time override by setting the global variable |
| 35 | * osmo_gettimeofday_override (`osmo_gettimeofday_override = true`), then |
| 36 | * set the global struct timeval osmo_gettimeofday_override_time wih the |
| 37 | * desired value. Next time osmo_gettimeofday() is called, it will return |
| 38 | * the values previously set. |
| 39 | * - A helper osmo_gettimeofday_override_add() is provided to easily |
| 40 | * increment osmo_gettimeofday_override_time with a specific amount of |
| 41 | * time. |
| 42 | */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 43 | |
| 44 | #include <stdbool.h> |
| 45 | #include <sys/time.h> |
Pau Espin Pedrol | 648f878 | 2017-06-18 11:41:32 +0200 | [diff] [blame] | 46 | #include <osmocom/core/timer_compat.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 47 | |
| 48 | bool osmo_gettimeofday_override = false; |
| 49 | struct timeval osmo_gettimeofday_override_time = { 23, 424242 }; |
| 50 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 51 | /*! shim around gettimeofday to be able to set the time manually. |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 52 | * To override, set osmo_gettimeofday_override == true and set the desired |
Max | 1fa8dfb | 2017-10-20 12:48:04 +0200 | [diff] [blame] | 53 | * current time in osmo_gettimeofday_override_time. |
| 54 | * |
| 55 | * N. B: gettimeofday() is affected by discontinuous jumps in the system time |
| 56 | * (e.g., if the system administrator manually changes the system time). |
| 57 | * Hence this should NEVER be used for elapsed time computation. |
Pau Espin Pedrol | 87fade8 | 2018-02-26 19:42:22 +0100 | [diff] [blame] | 58 | * Instead, osmo_clock_gettime() with CLOCK_MONOTONIC should be used for that. |
Max | 1fa8dfb | 2017-10-20 12:48:04 +0200 | [diff] [blame] | 59 | */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 60 | int osmo_gettimeofday(struct timeval *tv, struct timezone *tz) |
| 61 | { |
| 62 | if (osmo_gettimeofday_override) { |
| 63 | *tv = osmo_gettimeofday_override_time; |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | return gettimeofday(tv, tz); |
| 68 | } |
| 69 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 70 | /*! convenience function to advance the fake time. |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 71 | * Add the given values to osmo_gettimeofday_override_time. */ |
| 72 | void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs) |
| 73 | { |
| 74 | struct timeval val = { secs, usecs }; |
| 75 | timeradd(&osmo_gettimeofday_override_time, &val, |
| 76 | &osmo_gettimeofday_override_time); |
| 77 | } |
| 78 | |
| 79 | /*! @} */ |