Clean up the time(2) implementation.
This is also slightly faster for the no VDSO case (56ns vs 66ns).
Bug: N/A
Test: ran tests, benchmarks
Change-Id: I2b0edd06ee6942eb57c32678279278a53ca5ee9b
diff --git a/libc/Android.bp b/libc/Android.bp
index 9da4e1d..3ff5c03 100644
--- a/libc/Android.bp
+++ b/libc/Android.bp
@@ -402,7 +402,6 @@
"upstream-openbsd/lib/libc/gen/getprogname.c",
"upstream-openbsd/lib/libc/gen/isctype.c",
"upstream-openbsd/lib/libc/gen/setprogname.c",
- "upstream-openbsd/lib/libc/gen/time.c",
"upstream-openbsd/lib/libc/gen/tolower_.c",
"upstream-openbsd/lib/libc/gen/toupper_.c",
"upstream-openbsd/lib/libc/gen/verr.c",
diff --git a/libc/bionic/vdso.cpp b/libc/bionic/vdso.cpp
index 44899e7..c926a58 100644
--- a/libc/bionic/vdso.cpp
+++ b/libc/bionic/vdso.cpp
@@ -61,12 +61,16 @@
}
time_t time(time_t* t) {
- auto vdso_time = reinterpret_cast<decltype(&time)>(
- __libc_globals->vdso[VDSO_TIME].fn);
+ auto vdso_time = reinterpret_cast<decltype(&time)>(__libc_globals->vdso[VDSO_TIME].fn);
if (__predict_true(vdso_time)) {
return vdso_time(t);
}
- return __time(t);
+
+ // We can't fallback to the time(2) system call because it doesn't exist for most architectures.
+ timeval tv;
+ if (gettimeofday(&tv, nullptr) == -1) return -1;
+ if (t) *t = tv.tv_sec;
+ return tv.tv_sec;
}
void __libc_init_vdso(libc_globals* globals, KernelArgumentBlock& args) {
diff --git a/libc/private/bionic_vdso.h b/libc/private/bionic_vdso.h
index 8c4d8d2..da19b29 100644
--- a/libc/private/bionic_vdso.h
+++ b/libc/private/bionic_vdso.h
@@ -46,7 +46,6 @@
extern "C" int __clock_gettime(int, timespec*);
extern "C" int __clock_getres(int, timespec*);
extern "C" int __gettimeofday(timeval*, struct timezone*);
-extern "C" time_t __time(time_t*);
struct vdso_entry {
const char* name;
diff --git a/libc/upstream-openbsd/lib/libc/gen/time.c b/libc/upstream-openbsd/lib/libc/gen/time.c
deleted file mode 100644
index afe463b..0000000
--- a/libc/upstream-openbsd/lib/libc/gen/time.c
+++ /dev/null
@@ -1,45 +0,0 @@
-/* $OpenBSD: time.c,v 1.7 2015/10/29 03:58:55 mmcc Exp $ */
-/*
- * Copyright (c) 1983, 1993
- * The Regents of the University of California. All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * 3. Neither the name of the University nor the names of its contributors
- * may be used to endorse or promote products derived from this software
- * without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- */
-
-#include <sys/cdefs.h>
-#include <sys/time.h>
-#include <time.h>
-
-__LIBC_HIDDEN__ time_t
-__time(time_t *t)
-{
- struct timeval tt;
-
- if (gettimeofday(&tt, NULL) < 0)
- return (-1);
- if (t)
- *t = (time_t)tt.tv_sec;
- return (tt.tv_sec);
-}