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 uint32_t HostInfoNetBSD::GetMaxThreadNameLength() {
25   return PTHREAD_MAX_NAMELEN_NP;
26 }
27 
28 bool HostInfoNetBSD::GetOSVersion(uint32_t &major, uint32_t &minor,
29                                   uint32_t &update) {
30   struct utsname un;
31 
32   ::memset(&un, 0, sizeof(un));
33   if (::uname(&un) < 0)
34     return false;
35 
36   /* Accept versions like 7.99.21 and 6.1_STABLE */
37   int status = ::sscanf(un.release, "%" PRIu32 ".%" PRIu32 ".%" PRIu32, &major,
38                         &minor, &update);
39   switch (status) {
40   case 0:
41     return false;
42   case 1:
43     minor = 0;
44   /* FALLTHROUGH */
45   case 2:
46     update = 0;
47   /* FALLTHROUGH */
48   case 3:
49   default:
50     return true;
51   }
52 }
53 
54 bool HostInfoNetBSD::GetOSBuildString(std::string &s) {
55   int mib[2] = {CTL_KERN, KERN_OSREV};
56   char osrev_str[12];
57   int osrev = 0;
58   size_t osrev_len = sizeof(osrev);
59 
60   if (::sysctl(mib, 2, &osrev, &osrev_len, NULL, 0) == 0) {
61     ::snprintf(osrev_str, sizeof(osrev_str), "%-10.10d", osrev);
62     s.assign(osrev_str);
63     return true;
64   }
65 
66   s.clear();
67   return false;
68 }
69 
70 bool HostInfoNetBSD::GetOSKernelDescription(std::string &s) {
71   struct utsname un;
72 
73   ::memset(&un, 0, sizeof(un));
74   s.clear();
75 
76   if (::uname(&un) < 0)
77     return false;
78 
79   s.assign(un.version);
80 
81   return true;
82 }
83 
84 FileSpec HostInfoNetBSD::GetProgramFileSpec() {
85   static FileSpec g_program_filespec;
86 
87   if (!g_program_filespec) {
88     ssize_t len;
89     static char buf[PATH_MAX];
90     char name[PATH_MAX];
91 
92     ::snprintf(name, PATH_MAX, "/proc/%d/exe", ::getpid());
93     len = ::readlink(name, buf, PATH_MAX - 1);
94     if (len != -1) {
95       buf[len] = '\0';
96       g_program_filespec.SetFile(buf, false);
97     }
98   }
99   return g_program_filespec;
100 }
101