Really add adjtimex(2), and add clock_adjtime(2) too.

Change-Id: I81fde2ec9fdf787bb19a784ad13df92d33a4f852
diff --git a/libc/SYSCALLS.TXT b/libc/SYSCALLS.TXT
index e045049..d5dd206 100644
--- a/libc/SYSCALLS.TXT
+++ b/libc/SYSCALLS.TXT
@@ -224,6 +224,7 @@
 int           timerfd_settime(int, int, const struct itimerspec*, struct itimerspec*)   all
 int           timerfd_gettime(int, struct itimerspec*)   all
 int           adjtimex(struct timex*)   all
+int           clock_adjtime(clockid_t, struct timex*)   all
 
 # signals
 int     __sigaction:sigaction(int, const struct sigaction*, struct sigaction*)  arm,mips,x86
diff --git a/libc/arch-arm/syscalls/clock_adjtime.S b/libc/arch-arm/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..fa778e1
--- /dev/null
+++ b/libc/arch-arm/syscalls/clock_adjtime.S
@@ -0,0 +1,14 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    mov     ip, r7
+    ldr     r7, =__NR_clock_adjtime
+    swi     #0
+    mov     r7, ip
+    cmn     r0, #(MAX_ERRNO + 1)
+    bxls    lr
+    neg     r0, r0
+    b       __set_errno_internal
+END(clock_adjtime)
diff --git a/libc/arch-arm64/syscalls/clock_adjtime.S b/libc/arch-arm64/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..c2c191e
--- /dev/null
+++ b/libc/arch-arm64/syscalls/clock_adjtime.S
@@ -0,0 +1,14 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    mov     x8, __NR_clock_adjtime
+    svc     #0
+
+    cmn     x0, #(MAX_ERRNO + 1)
+    cneg    x0, x0, hi
+    b.hi    __set_errno_internal
+
+    ret
+END(clock_adjtime)
diff --git a/libc/arch-mips/syscalls/clock_adjtime.S b/libc/arch-mips/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..f8a4ce2
--- /dev/null
+++ b/libc/arch-mips/syscalls/clock_adjtime.S
@@ -0,0 +1,19 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    .set noreorder
+    .cpload t9
+    li v0, __NR_clock_adjtime
+    syscall
+    bnez a3, 1f
+    move a0, v0
+    j ra
+    nop
+1:
+    la t9,__set_errno_internal
+    j t9
+    nop
+    .set reorder
+END(clock_adjtime)
diff --git a/libc/arch-mips64/syscalls/clock_adjtime.S b/libc/arch-mips64/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..206e9fd
--- /dev/null
+++ b/libc/arch-mips64/syscalls/clock_adjtime.S
@@ -0,0 +1,25 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    .set push
+    .set noreorder
+    li v0, __NR_clock_adjtime
+    syscall
+    bnez a3, 1f
+    move a0, v0
+    j ra
+    nop
+1:
+    move t0, ra
+    bal     2f
+    nop
+2:
+    .cpsetup ra, t1, 2b
+    LA t9,__set_errno_internal
+    .cpreturn
+    j t9
+    move ra, t0
+    .set pop
+END(clock_adjtime)
diff --git a/libc/arch-x86/syscalls/clock_adjtime.S b/libc/arch-x86/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..b6e0ac4
--- /dev/null
+++ b/libc/arch-x86/syscalls/clock_adjtime.S
@@ -0,0 +1,26 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    pushl   %ebx
+    .cfi_def_cfa_offset 8
+    .cfi_rel_offset ebx, 0
+    pushl   %ecx
+    .cfi_adjust_cfa_offset 4
+    .cfi_rel_offset ecx, 0
+    mov     12(%esp), %ebx
+    mov     16(%esp), %ecx
+    movl    $__NR_clock_adjtime, %eax
+    int     $0x80
+    cmpl    $-MAX_ERRNO, %eax
+    jb      1f
+    negl    %eax
+    pushl   %eax
+    call    __set_errno_internal
+    addl    $4, %esp
+1:
+    popl    %ecx
+    popl    %ebx
+    ret
+END(clock_adjtime)
diff --git a/libc/arch-x86_64/syscalls/clock_adjtime.S b/libc/arch-x86_64/syscalls/clock_adjtime.S
new file mode 100644
index 0000000..0601930
--- /dev/null
+++ b/libc/arch-x86_64/syscalls/clock_adjtime.S
@@ -0,0 +1,15 @@
+/* Generated by gensyscalls.py. Do not edit. */
+
+#include <private/bionic_asm.h>
+
+ENTRY(clock_adjtime)
+    movl    $__NR_clock_adjtime, %eax
+    syscall
+    cmpq    $-MAX_ERRNO, %rax
+    jb      1f
+    negl    %eax
+    movl    %eax, %edi
+    call    __set_errno_internal
+1:
+    ret
+END(clock_adjtime)
diff --git a/libc/include/sys/timex.h b/libc/include/sys/timex.h
index 6138ac4..fade5c3 100644
--- a/libc/include/sys/timex.h
+++ b/libc/include/sys/timex.h
@@ -30,11 +30,13 @@
 #define _SYS_TIMEX_H_
 
 #include <sys/cdefs.h>
+#include <sys/types.h>
 #include <linux/timex.h>
 
 __BEGIN_DECLS
 
-extern int adjtimex(struct timex *buf);
+int adjtimex(struct timex*);
+int clock_adjtime(clockid_t, struct timex*);
 
 __END_DECLS
 
diff --git a/libc/libc.arm.brillo.map b/libc/libc.arm.brillo.map
index 9251de2..01541a9 100644
--- a/libc/libc.arm.brillo.map
+++ b/libc/libc.arm.brillo.map
@@ -1224,6 +1224,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.arm.map b/libc/libc.arm.map
index f5ddfd3..25f53c9 100644
--- a/libc/libc.arm.map
+++ b/libc/libc.arm.map
@@ -1224,6 +1224,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.arm64.map b/libc/libc.arm64.map
index 271460f..93bd94c 100644
--- a/libc/libc.arm64.map
+++ b/libc/libc.arm64.map
@@ -1147,6 +1147,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.map.txt b/libc/libc.map.txt
index c859d81..dd0b9c0 100644
--- a/libc/libc.map.txt
+++ b/libc/libc.map.txt
@@ -1250,6 +1250,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.mips.brillo.map b/libc/libc.mips.brillo.map
index e5376be..287c214 100644
--- a/libc/libc.mips.brillo.map
+++ b/libc/libc.mips.brillo.map
@@ -1208,6 +1208,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.mips.map b/libc/libc.mips.map
index 4902f57..5c15a42 100644
--- a/libc/libc.mips.map
+++ b/libc/libc.mips.map
@@ -1208,6 +1208,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.mips64.map b/libc/libc.mips64.map
index 271460f..93bd94c 100644
--- a/libc/libc.mips64.map
+++ b/libc/libc.mips64.map
@@ -1147,6 +1147,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.x86.brillo.map b/libc/libc.x86.brillo.map
index bbf9d57..95fd7b8 100644
--- a/libc/libc.x86.brillo.map
+++ b/libc/libc.x86.brillo.map
@@ -1207,6 +1207,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.x86.map b/libc/libc.x86.map
index fb0f109..6a1acba 100644
--- a/libc/libc.x86.map
+++ b/libc/libc.x86.map
@@ -1207,6 +1207,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/libc/libc.x86_64.map b/libc/libc.x86_64.map
index 271460f..93bd94c 100644
--- a/libc/libc.x86_64.map
+++ b/libc/libc.x86_64.map
@@ -1147,6 +1147,8 @@
     __pwrite_chk;
     __pwrite64_chk;
     __write_chk;
+    adjtimex;
+    clock_adjtime;
     fgetpos64;
     fileno_unlocked;
     fopen64;
diff --git a/tests/Android.mk b/tests/Android.mk
index fb3c254..aba0871 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -106,6 +106,7 @@
     sys_sysinfo_test.cpp \
     sys_sysmacros_test.cpp \
     sys_time_test.cpp \
+    sys_timex_test.cpp \
     sys_types_test.cpp \
     sys_uio_test.cpp \
     sys_vfs_test.cpp \
diff --git a/tests/sys_timex_test.cpp b/tests/sys_timex_test.cpp
new file mode 100644
index 0000000..1340ea4
--- /dev/null
+++ b/tests/sys_timex_test.cpp
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2016 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <sys/timex.h>
+
+#include <errno.h>
+
+#include <gtest/gtest.h>
+
+TEST(sys_timex, adjtimex_smoke) {
+  timex t;
+  memset(&t, 0, sizeof(t));
+  // adjtimex/clock_adjtime return the clock state on success, -1 on failure.
+  ASSERT_NE(-1, adjtimex(&t));
+}
+
+TEST(sys_timex, adjtimex_EFAULT) {
+  errno = 0;
+  ASSERT_EQ(-1, adjtimex(nullptr));
+  ASSERT_EQ(EFAULT, errno);
+}
+
+TEST(sys_timex, clock_adjtime_smoke) {
+  timex t;
+  memset(&t, 0, sizeof(t));
+  // adjtimex/clock_adjtime return the clock state on success, -1 on failure.
+  ASSERT_NE(-1, clock_adjtime(CLOCK_REALTIME, &t));
+}
+
+TEST(sys_timex, clock_adjtime_EFAULT) {
+  errno = 0;
+  ASSERT_EQ(-1, clock_adjtime(CLOCK_REALTIME, nullptr));
+  ASSERT_EQ(EFAULT, errno);
+}