1139826Simp//===-- HostInfoOpenBSD.cpp -----------------------------------------------===//
253541Sshin//
3222488Srwatson// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
453541Sshin// See https://llvm.org/LICENSE.txt for license information.
553541Sshin// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6222488Srwatson//
7222488Srwatson//===----------------------------------------------------------------------===//
8222488Srwatson
953541Sshin#include "lldb/Host/openbsd/HostInfoOpenBSD.h"
1053541Sshin#include "lldb/Host/FileSystem.h"
1153541Sshin
1253541Sshin#include <cstdio>
1353541Sshin#include <cstring>
1453541Sshin#include <unistd.h>
1553541Sshin#include <optional>
1653541Sshin#include <sys/sysctl.h>
1753541Sshin#include <sys/types.h>
1853541Sshin#include <sys/utsname.h>
1953541Sshin
2053541Sshinusing namespace lldb_private;
2153541Sshin
2253541Sshinllvm::VersionTuple HostInfoOpenBSD::GetOSVersion() {
2353541Sshin  struct utsname un;
2453541Sshin
2553541Sshin  ::memset(&un, 0, sizeof(un));
2653541Sshin  if (::uname(&un) < 0)
2753541Sshin    return llvm::VersionTuple();
2853541Sshin
2953541Sshin  uint32_t major, minor;
3053541Sshin  int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32, &major, &minor);
3153541Sshin  switch (status) {
3253541Sshin  case 1:
33174510Sobrien    return llvm::VersionTuple(major);
3453541Sshin  case 2:
3553541Sshin    return llvm::VersionTuple(major, minor);
36139826Simp  }
3753541Sshin  return llvm::VersionTuple();
3853541Sshin}
3953541Sshin
4053541Sshinstd::optional<std::string> HostInfoOpenBSD::GetOSBuildString() {
4153541Sshin  int mib[2] = {CTL_KERN, KERN_OSREV};
4253541Sshin  uint32_t osrev = 0;
4353541Sshin  size_t osrev_len = sizeof(osrev);
4453541Sshin
4553541Sshin  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
4653541Sshin    return llvm::formatv("{0,8:8}", osrev).str();
4753541Sshin
4853541Sshin  return std::nullopt;
4953541Sshin}
5053541Sshin
5153541SshinFileSpec HostInfoOpenBSD::GetProgramFileSpec() {
5253541Sshin  static FileSpec g_program_filespec;
5353541Sshin  return g_program_filespec;
5453541Sshin}
5553541Sshin
5653541Sshinbool HostInfoOpenBSD::ComputeSupportExeDirectory(FileSpec &file_spec) {
5753541Sshin  if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
5853541Sshin      file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
5953541Sshin    return true;
6053541Sshin
6153541Sshin  file_spec.SetDirectory("/usr/bin");
6253541Sshin  return true;
6353541Sshin}
6453541Sshin