| 
 | 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 | +  ret = LIBC_NAMESPACE::syscall_impl<int>(SYS_clock_settime64,  | 
 | 38 | +                                          static_cast<long>(clockid),  | 
 | 39 | +                                          reinterpret_cast<long>(&ts64));  | 
 | 40 | +  if (ret == 0) {  | 
 | 41 | +    ts->tv_sec = static_cast<decltype(ts->tv_sec)>(ts64.tv_sec);  | 
 | 42 | +    ts->tv_nsec = static_cast<decltype(ts->tv_nsec)>(ts64.tv_nsec);  | 
 | 43 | +  }  | 
 | 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