53 lines · cpp
1//===-- Implementation file for setitimer ---------------------------------===//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-exception6//7//===----------------------------------------------------------------------===//8#include "src/sys/time/setitimer.h"9#include "hdr/types/struct_itimerval.h"10#include "src/__support/OSUtil/syscall.h"11#include "src/__support/common.h"12#include "src/__support/libc_errno.h"13#include <sys/syscall.h>14 15namespace LIBC_NAMESPACE_DECL {16 17LLVM_LIBC_FUNCTION(int, setitimer,18 (int which, const struct itimerval *new_value,19 struct itimerval *old_value)) {20 long ret = 0;21 if constexpr (sizeof(time_t) > sizeof(long)) {22 // There is no SYS_setitimer_time64 call, so we can't use time_t directly,23 // and need to convert it to long first.24 long new_value32[4] = {static_cast<long>(new_value->it_interval.tv_sec),25 static_cast<long>(new_value->it_interval.tv_usec),26 static_cast<long>(new_value->it_value.tv_sec),27 static_cast<long>(new_value->it_value.tv_usec)};28 long old_value32[4];29 30 ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value32,31 old_value32);32 33 if (!ret && old_value) {34 old_value->it_interval.tv_sec = old_value32[0];35 old_value->it_interval.tv_usec = old_value32[1];36 old_value->it_value.tv_sec = old_value32[2];37 old_value->it_value.tv_usec = old_value32[3];38 }39 } else {40 ret = LIBC_NAMESPACE::syscall_impl<long>(SYS_setitimer, which, new_value,41 old_value);42 }43 44 // On failure, return -1 and set errno.45 if (ret < 0) {46 libc_errno = static_cast<int>(-ret);47 return -1;48 }49 return 0;50}51 52} // namespace LIBC_NAMESPACE_DECL53