|
| 1 | +//===--- clock_settime linux implementation ---------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +#include "src/__support/time/clock_settime.h" |
| 10 | +#include "hdr/types/clockid_t.h" |
| 11 | +#include "hdr/types/struct_timespec.h" |
| 12 | +#include "src/__support/OSUtil/syscall.h" |
| 13 | +#include "src/__support/common.h" |
| 14 | +#include "src/__support/error_or.h" |
| 15 | +#include "src/__support/macros/config.h" |
| 16 | +#include <sys/syscall.h> |
| 17 | + |
| 18 | +#if defined(SYS_clock_settime64) |
| 19 | +#include <linux/time_types.h> |
| 20 | +#endif |
| 21 | + |
| 22 | +namespace LIBC_NAMESPACE_DECL { |
| 23 | +namespace internal { |
| 24 | +ErrorOr<int> clock_settime(clockid_t clockid, const timespec *ts) { |
| 25 | + int ret; |
| 26 | +#if defined(SYS_clock_settime) |
| 27 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime, |
| 28 | + static_cast<long>(clockid), |
| 29 | + reinterpret_cast<long>(ts)); |
| 30 | +#elif defined(SYS_clock_settime64) |
| 31 | + static_assert( |
| 32 | + sizeof(time_t) == sizeof(int64_t), |
| 33 | + "SYS_clock_settime64 requires struct timespec with 64-bit members."); |
| 34 | + |
| 35 | + __kernel_timespec ts64{}; |
| 36 | + |
| 37 | + // Populate the 64-bit kernel structure from the user-provided timespec |
| 38 | + ts64.tv_sec = static_cast<decltype(ts64.tv_sec)>(ts->tv_sec); |
| 39 | + ts64.tv_nsec = static_cast<decltype(ts64.tv_nsec)>(ts->tv_nsec); |
| 40 | + |
| 41 | + ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64, |
| 42 | + static_cast<long>(clockid), |
| 43 | + reinterpret_cast<long>(&ts64)); |
| 44 | +#else |
| 45 | +#error "SYS_clock_settime and SYS_clock_settime64 syscalls not available." |
| 46 | +#endif |
| 47 | + if (ret < 0) |
| 48 | + return Error(-ret); |
| 49 | + return ret; |
| 50 | +} |
| 51 | + |
| 52 | +} // namespace internal |
| 53 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments