RSC  0.16.0
MacHostInfo.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2014 Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "HostInfo.h"
28 
29 #include <errno.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35 
36 #include <stdexcept>
37 #include <sstream>
38 
39 #include <boost/format.hpp>
40 
41 #include <boost/date_time/posix_time/posix_time.hpp>
42 
43 namespace rsc {
44 namespace os {
45 
46 // {Machine,Software} {Type,Version}
47 
48 std::string currentMachineVersion () {
49  char buffer[256];
50  size_t size = 256;
51  if (sysctlbyname("machdep.cpu.brand_string", &buffer, &size, 0, 0) != 0) {
52  throw std::runtime_error(boost::str(boost::format("sysctlbyname failed: %1%")
53  % strerror(errno)));
54  }
55  return std::string(buffer);
56 }
57 
58 // Host ID
59 
60 std::string currentHostId() {
61  uuid_t id;
62  struct timespec wait;
63  wait.tv_sec = 0;
64  wait.tv_nsec = 0;
65  if (gethostuuid(id, &wait) != 0) {
66  throw std::runtime_error(boost::str(boost::format("gethostuuid failed: %1%")
67  % strerror(errno)));
68  }
69  std::stringstream stream;
70  for (unsigned int i = 0; i < 16; ++i) {
71  stream << std::hex << std::setw(2) << std::setfill('0') << (int) id[i];
72  }
73  return stream.str();
74 }
75 
76 // Boot time
77 
78 boost::posix_time::ptime currentBootTime() {
79  int mib[] = { CTL_KERN, KERN_BOOTTIME };
80  struct timeval t;
81  size_t len = (size_t) sizeof(t);
82  if (sysctl(mib, 2, &t, &len, NULL, 0) != 0) {
83  throw std::runtime_error(boost::str(boost::format("Could not determine"
84  " computer boot time:"
85  " %1%")
86  % strerror(errno)));
87  }
88  return boost::posix_time::from_time_t(t.tv_sec) +
89  boost::posix_time::microseconds(t.tv_usec);
90 }
91 
92 }
93 }
RSC_EXPORT std::string currentHostId()
Determine and return a unique id string of the local machine.
RSC_EXPORT boost::posix_time::ptime currentBootTime()
Return the boot time of the local machine.
RSC_EXPORT std::string currentMachineVersion()
Determine and return the version within its type, usually the CPU identification string, of the local machine.