// Copyright 2018 Google Inc.
//
// 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.

// This is the VDSO for sandboxed binaries. This file just contains the entry
// points to the VDSO. All of the real work is done in vdso_time.cc

#define _DEFAULT_SOURCE  // ensure glibc provides struct timezone.
#include <sys/time.h>
#include <time.h>

#include "vdso/syscalls.h"
#include "vdso/vdso_time.h"

namespace vdso {

// __vdso_clock_gettime() implements clock_gettime()
extern "C" int __vdso_clock_gettime(clockid_t clock, struct timespec* ts) {
  int ret;

  switch (clock) {
    case CLOCK_REALTIME:
      ret = ClockRealtime(ts);
      break;

    case CLOCK_MONOTONIC:
      ret = ClockMonotonic(ts);
      break;

    default:
      ret = sys_clock_gettime(clock, ts);
      break;
  }

  return ret;
}
extern "C" int clock_gettime(clockid_t clock, struct timespec* ts)
    __attribute__((weak, alias("__vdso_clock_gettime")));

// __vdso_gettimeofday() implements gettimeofday()
extern "C" int __vdso_gettimeofday(struct timeval* tv, struct timezone* tz) {
  if (tv) {
    struct timespec ts;
    int ret = ClockRealtime(&ts);
    if (ret) {
      return ret;
    }
    tv->tv_sec = ts.tv_sec;
    tv->tv_usec = ts.tv_nsec / 1000;
  }

  // Nobody should be calling gettimeofday() with a non-NULL
  // timezone pointer. If they do then they will get zeros.
  if (tz) {
    tz->tz_minuteswest = 0;
    tz->tz_dsttime = 0;
  }

  return 0;
}
extern "C" int gettimeofday(struct timeval* tv, struct timezone* tz)
    __attribute__((weak, alias("__vdso_gettimeofday")));

// __vdso_time() implements time()
extern "C" time_t __vdso_time(time_t* t) {
  struct timespec ts;
  ClockRealtime(&ts);
  if (t) {
    *t = ts.tv_sec;
  }
  return ts.tv_sec;
}
extern "C" time_t time(time_t* t) __attribute__((weak, alias("__vdso_time")));

// __vdso_getcpu() implements getcpu()
extern "C" long __vdso_getcpu(unsigned* cpu, unsigned* node,
                              struct getcpu_cache* cache) {
  // No optimizations yet, just make the real system call.
  return sys_getcpu(cpu, node, cache);
}
extern "C" long getcpu(unsigned* cpu, unsigned* node,
                       struct getcpu_cache* cache)
    __attribute__((weak, alias("__vdso_getcpu")));

}  // namespace vdso