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 | * @{ |
Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 26 | * \file timer_gettimeofday.c */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 27 | |
| 28 | #include <stdbool.h> |
| 29 | #include <sys/time.h> |
Pau Espin Pedrol | 648f878 | 2017-06-18 11:41:32 +0200 | [diff] [blame] | 30 | #include <osmocom/core/timer_compat.h> |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 31 | |
| 32 | bool osmo_gettimeofday_override = false; |
| 33 | struct timeval osmo_gettimeofday_override_time = { 23, 424242 }; |
| 34 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 35 | /*! shim around gettimeofday to be able to set the time manually. |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 36 | * To override, set osmo_gettimeofday_override == true and set the desired |
Max | 1fa8dfb | 2017-10-20 12:48:04 +0200 | [diff] [blame] | 37 | * current time in osmo_gettimeofday_override_time. |
| 38 | * |
| 39 | * N. B: gettimeofday() is affected by discontinuous jumps in the system time |
| 40 | * (e.g., if the system administrator manually changes the system time). |
| 41 | * Hence this should NEVER be used for elapsed time computation. |
| 42 | * Instead, clock_gettime(CLOCK_MONOTONIC, ..) should be used for that (with similar shim if necessary). |
| 43 | */ |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 44 | int osmo_gettimeofday(struct timeval *tv, struct timezone *tz) |
| 45 | { |
| 46 | if (osmo_gettimeofday_override) { |
| 47 | *tv = osmo_gettimeofday_override_time; |
| 48 | return 0; |
| 49 | } |
| 50 | |
| 51 | return gettimeofday(tv, tz); |
| 52 | } |
| 53 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 54 | /*! convenience function to advance the fake time. |
Neels Hofmeyr | 8e2f7e8 | 2016-09-22 03:58:13 +0200 | [diff] [blame] | 55 | * Add the given values to osmo_gettimeofday_override_time. */ |
| 56 | void osmo_gettimeofday_override_add(time_t secs, suseconds_t usecs) |
| 57 | { |
| 58 | struct timeval val = { secs, usecs }; |
| 59 | timeradd(&osmo_gettimeofday_override_time, &val, |
| 60 | &osmo_gettimeofday_override_time); |
| 61 | } |
| 62 | |
| 63 | /*! @} */ |