1 //===-- HostInfoNetBSD.cpp -------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "lldb/Host/netbsd/HostInfoNetBSD.h"
11 
12 #include <inttypes.h>
13 #include <limits.h>
14 #include <pthread.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <sys/sysctl.h>
18 #include <sys/types.h>
19 #include <sys/utsname.h>
20 #include <unistd.h>
21 
22 using namespace lldb_private;
23 
24 llvm::VersionTuple HostInfoNetBSD::GetOSVersion() {
25   struct utsname un;
26 
27   ::memset(&un, 0, sizeof(un));
28   if (::uname(&un) < 0)
29     return llvm::VersionTuple();
30 
31   /* Accept versions like 7.99.21 and 6.1_STABLE */
32   uint32_t major, minor, update;
33   int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
34                         &minor, &update);
35   switch (status) {
36   case 1:
37     return llvm::VersionTuple(major);
38   case 2:
39     return llvm::VersionTuple(major, minor);
40   case 3:
41     return llvm::VersionTuple(major, minor, update);
42   }
43   return llvm::VersionTuple();
44 }
45 
46 bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
47   int mib[2] = {CTL_KERN, KERN_OSREV};
48   char osrev_str[12];
49   int osrev = 0;
50   size_t osrev_len = sizeof(osrev);
51 
52   if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
53     ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
54     s.assign(osrev_str);
55     return true;
56   }
57 
58   s.clear();
59   return false;
60 }
61 
62 bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
63   struct utsname un;
64 
65   ::memset(&un, 0, sizeof(un));
66   s.clear();
67 
68   if (::uname(&un) < 0)
69     return false;
70 
71   s.assign(un.version);
72 
73   return true;
74 }
75 
76 FileSpec HostInfoNetBSD::GetProgramFileSpec() {
77   static FileSpec g_program_filespec;
78 
79   if (!g_program_filespec) {
80     static const int name[] = {
81         CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME,
82     };
83     char path[MAXPATHLEN];
84     size_t len;
85 
86     len = sizeof(path);
87     if (sysctl(name, __arraycount(name), path, &len, NULL, 0) != -1) {
88       g_program_filespec.SetFile(path, FileSpec::Style::native);
89     }
90   }
91   return g_program_filespec;
92 }
93