RSC  0.17.1
PosixUtilities.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project
4  *
5  * Copyright (C) 2014 Jan Moringen
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 <assert.h>
30 #include <errno.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <sys/utsname.h>
34 
35 #include <stdexcept>
36 #include <sstream>
37 
38 #include <boost/algorithm/string.hpp>
39 #include <boost/format.hpp>
40 #include <boost/function.hpp>
41 
42 namespace rsc {
43 namespace os {
44 
45 // {Machine,Software} {Type,Version}
46 
47 std::string callWithUtsname(const std::string& context,
48  boost::function1<std::string, const utsname&> thunk) {
49  utsname info;
50  if (uname(&info) == -1) {
51  throw std::runtime_error(boost::str(boost::format("Could not determine %1% because"
52  " uname(2) failed: %2%")
53  % context % strerror(errno)));
54  }
55  return thunk(info);
56 }
57 
59  std::string operator()(const utsname& info) const {
60  if (std::string(info.machine) == "i686") {
61  return "x86";
62  } else {
63  return info.machine;
64  }
65  }
66 };
67 
68 std::string currentMachineType() {
69  return callWithUtsname("machine type", GetMachineType());
70 }
71 
72 struct GetSysname {
73  std::string operator()(const utsname& info) const {
74  std::string result(info.sysname);
75  boost::algorithm::to_lower(result);
76  return result;
77  }
78 };
79 
80 std::string currentSoftwareType() {
81  return callWithUtsname("software type", GetSysname());
82 }
83 
84 struct GetRelease {
85  std::string operator()(const utsname& info) const {
86  return info.release;
87  }
88 };
89 
90 std::string currentSoftwareVersion() {
91  return callWithUtsname("software version", GetRelease());
92 }
93 
94 // Hostname
95 
96 const unsigned int HOSTNAME_MAX_LENGTH = 1024;
97 
98 std::string currentHostname() {
99  char buffer[HOSTNAME_MAX_LENGTH];
100  if (gethostname(buffer, HOSTNAME_MAX_LENGTH) == 0) {
101  std::string maybeQualifiedHostname(buffer);
102  std::vector<std::string> components;
103  boost::algorithm::split(components, maybeQualifiedHostname,
104  boost::algorithm::is_any_of("."));
105  assert(!components.empty());
106  return components[0];
107  } else {
108  throw std::runtime_error(boost::str(boost::format("gethostname failed: %1%")
109  % strerror(errno)));
110  }
111 }
112 
113 }
114 }
std::string operator()(const utsname &info) const
std::string callWithUtsname(const std::string &context, boost::function1< std::string, const utsname & > thunk)
std::string operator()(const utsname &info) const
std::string operator()(const utsname &info) const
RSC_EXPORT std::string currentHostname()
Determine and return the hostname of the local machine.
RSC_EXPORT std::string currentSoftwareVersion()
Determine and return the version of the operating system within its type, usually the kernel version ...
RSC_EXPORT std::string currentSoftwareType()
Determine and return the type of the operating system, usually the kernel name, running on the local ...
const unsigned int HOSTNAME_MAX_LENGTH
RSC_EXPORT std::string currentMachineType()
Determine and return the machine type, usually CPU architecture, of the local machine.