1//===-- HostInfoOpenBSD.cpp -----------------------------------------------===//
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 "lldb/Host/openbsd/HostInfoOpenBSD.h"
10#include "lldb/Host/FileSystem.h"
11
12#include <cstdio>
13#include <cstring>
14#include <unistd.h>
15#include <optional>
16#include <sys/sysctl.h>
17#include <sys/types.h>
18#include <sys/utsname.h>
19
20using namespace lldb_private;
21
22llvm::VersionTuple HostInfoOpenBSD::GetOSVersion() {
23  struct utsname un;
24
25  ::memset(&un, 0, sizeof(un));
26  if (::uname(&un) < 0)
27    return llvm::VersionTuple();
28
29  uint32_t major, minor;
30  int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32, &major, &minor);
31  switch (status) {
32  case 1:
33    return llvm::VersionTuple(major);
34  case 2:
35    return llvm::VersionTuple(major, minor);
36  }
37  return llvm::VersionTuple();
38}
39
40std::optional<std::string> HostInfoOpenBSD::GetOSBuildString() {
41  int mib[2] = {CTL_KERN, KERN_OSREV};
42  uint32_t osrev = 0;
43  size_t osrev_len = sizeof(osrev);
44
45  if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0)
46    return llvm::formatv("{0,8:8}", osrev).str();
47
48  return std::nullopt;
49}
50
51FileSpec HostInfoOpenBSD::GetProgramFileSpec() {
52  static FileSpec g_program_filespec;
53  return g_program_filespec;
54}
55
56bool HostInfoOpenBSD::ComputeSupportExeDirectory(FileSpec &file_spec) {
57  if (HostInfoPosix::ComputeSupportExeDirectory(file_spec) &&
58      file_spec.IsAbsolute() && FileSystem::Instance().Exists(file_spec))
59    return true;
60
61  file_spec.SetDirectory("/usr/bin");
62  return true;
63}
64