1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
// Environment_UNIX.cpp
//
// Library: Foundation
// Package: Core
// Module: Environment
//
// Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/Environment_UNIX.h"
#include "Poco/Exception.h"
#include "Poco/Buffer.h"
#include <cstring>
#include <unistd.h>
#include <stdlib.h>
#include <sys/utsname.h>
#include <sys/param.h>
#if defined(POCO_OS_FAMILY_BSD)
#include <sys/sysctl.h>
#elif POCO_OS == POCO_OS_HPUX
#include <pthread.h>
#endif
namespace Poco {
EnvironmentImpl::StringMap EnvironmentImpl::_map;
FastMutex EnvironmentImpl::_mutex;
std::string EnvironmentImpl::getImpl(const std::string& name)
{
FastMutex::ScopedLock lock(_mutex);
const char* val = getenv(name.c_str());
if (val)
return std::string(val);
else
throw NotFoundException(name);
}
bool EnvironmentImpl::hasImpl(const std::string& name)
{
FastMutex::ScopedLock lock(_mutex);
return getenv(name.c_str()) != 0;
}
void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
{
FastMutex::ScopedLock lock(_mutex);
std::string var = name;
var.append("=");
var.append(value);
_map[name] = var;
if (putenv((char*) _map[name].c_str()))
{
std::string msg = "cannot set environment variable: ";
msg.append(name);
throw SystemException(msg);
}
}
std::string EnvironmentImpl::osNameImpl()
{
struct utsname uts;
uname(&uts);
return uts.sysname;
}
std::string EnvironmentImpl::osDisplayNameImpl()
{
return osNameImpl();
}
std::string EnvironmentImpl::osVersionImpl()
{
struct utsname uts;
uname(&uts);
return uts.release;
}
std::string EnvironmentImpl::osArchitectureImpl()
{
struct utsname uts;
uname(&uts);
return uts.machine;
}
std::string EnvironmentImpl::nodeNameImpl()
{
struct utsname uts;
uname(&uts);
return uts.nodename;
}
unsigned EnvironmentImpl::processorCountImpl()
{
#if defined(_SC_NPROCESSORS_ONLN)
int count = sysconf(_SC_NPROCESSORS_ONLN);
if (count <= 0) count = 1;
return static_cast<int>(count);
#elif defined(POCO_OS_FAMILY_BSD)
unsigned count;
std::size_t size = sizeof(count);
if (sysctlbyname("hw.ncpu", &count, &size, 0, 0))
return 1;
else
return count;
#elif POCO_OS == POCO_OS_HPUX
return pthread_num_processors_np();
#else
return 1;
#endif
}
} // namespace Poco
//
// nodeIdImpl
//
#if defined(POCO_OS_FAMILY_BSD) || POCO_OS == POCO_OS_QNX
//
// BSD variants
//
#include <sys/types.h>
#include <sys/socket.h>
#include <ifaddrs.h>
#include <net/if_dl.h>
namespace Poco {
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
std::memset(&id, 0, sizeof(id));
struct ifaddrs* ifaphead;
int rc = getifaddrs(&ifaphead);
if (rc) return;
for (struct ifaddrs* ifap = ifaphead; ifap; ifap = ifap->ifa_next)
{
if (ifap->ifa_addr && ifap->ifa_addr->sa_family == AF_LINK)
{
struct sockaddr_dl* sdl = reinterpret_cast<struct sockaddr_dl*>(ifap->ifa_addr);
caddr_t ap = LLADDR(sdl);
int alen = sdl->sdl_alen;
if (ap && alen > 0)
{
std::memcpy(&id, ap, sizeof(id));
break;
}
}
}
freeifaddrs(ifaphead);
}
} // namespace Poco
#elif defined(__CYGWIN__) || POCO_OS == POCO_OS_LINUX || POCO_OS == POCO_OS_ANDROID
//
// Linux, Cygwin
//
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <net/if.h>
#ifndef __CYGWIN__
#include <net/if_arp.h>
#else // workaround for Cygwin, which does not have if_arp.h
#define ARPHRD_ETHER 1 /* Ethernet 10Mbps */
#endif
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <cstdio>
namespace Poco {
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
std::memset(&id, 0, sizeof(id));
// ideally, the following code should be rewritten
// to use netlink
// first try to obtain the MAC address of eth0 using /sys/class/net
int fd = open("/sys/class/net/eth0/address", O_RDONLY);
if (fd >= 0)
{
char buffer[18];
int n = read(fd, buffer, 17);
close(fd);
if (n == 17)
{
buffer[n] = 0;
if (std::sscanf(buffer, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx", &id[0], &id[1], &id[2], &id[3], &id[4], &id[5]) == 6)
return;
}
}
// if that did not work, search active interfaces
int sock = socket(PF_INET, SOCK_DGRAM, 0);
if (sock == -1) return;
// the following code is loosely based
// on W. Richard Stevens, UNIX Network Programming, pp 434ff.
int lastlen = 0;
int len = 100*sizeof(struct ifreq);
struct ifconf ifc;
char* buf = 0;
for (;;)
{
buf = new char[len];
ifc.ifc_len = len;
ifc.ifc_buf = buf;
if (::ioctl(sock, SIOCGIFCONF, &ifc) < 0)
{
if (errno != EINVAL || lastlen != 0)
{
close(sock);
delete [] buf;
return;
}
}
else
{
if (ifc.ifc_len == lastlen)
break;
lastlen = ifc.ifc_len;
}
len += 10*sizeof(struct ifreq);
delete [] buf;
}
for (const char* ptr = buf; ptr < buf + ifc.ifc_len;)
{
const struct ifreq* ifr = reinterpret_cast<const struct ifreq*>(ptr);
int rc = ioctl(sock, SIOCGIFHWADDR, ifr);
if (rc != -1)
{
const struct sockaddr* sa = reinterpret_cast<const struct sockaddr*>(&ifr->ifr_hwaddr);
if (sa->sa_family == ARPHRD_ETHER)
{
std::memcpy(&id, sa->sa_data, sizeof(id));
break;
}
}
ptr += sizeof(struct ifreq);
}
close(sock);
delete [] buf;
}
} // namespace Poco
#elif defined(__GNU__)
//
// GNU Hurd
//
#include <sys/ioctl.h>
#include <net/if.h>
#include <unistd.h>
#include <netinet/in.h>
namespace Poco {
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
std::memset(&id, 0, sizeof(id));
struct ifreq ifr;
struct ifconf ifc;
char buf[1024];
int sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
if (sock == -1) return;
ifc.ifc_len = sizeof(buf);
ifc.ifc_buf = buf;
if (ioctl(sock, SIOCGIFCONF, &ifc) == -1) return;
struct ifreq* it = ifc.ifc_req;
const struct ifreq* const end = it + (ifc.ifc_len / sizeof(struct ifreq));
for (; it != end; ++it) {
std::strcpy(ifr.ifr_name, it->ifr_name);
if (ioctl(sock, SIOCGIFFLAGS, &ifr) == 0) {
if (! (ifr.ifr_flags & IFF_LOOPBACK)) { // don't count loopback
if (ioctl(sock, SIOCGIFHWADDR, &ifr) == 0) {
std::memcpy(&id, ifr.ifr_hwaddr.sa_data, sizeof(id));
break;
}
}
}
else return;
}
}
} // namespace Poco
#elif defined(POCO_OS_FAMILY_UNIX)
//
// General Unix
//
#include <sys/ioctl.h>
#if defined(sun) || defined(__sun)
#include <sys/sockio.h>
#endif
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <net/if.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <net/if.h>
#include <net/if_arp.h>
#include <unistd.h>
namespace Poco {
void EnvironmentImpl::nodeIdImpl(NodeId& id)
{
std::memset(&id, 0, sizeof(id));
char name[MAXHOSTNAMELEN];
if (gethostname(name, sizeof(name)))
return;
struct hostent* pHost = gethostbyname(name);
if (!pHost) return;
int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (s == -1) return;
struct arpreq ar;
std::memset(&ar, 0, sizeof(ar));
struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
pAddr->sin_family = AF_INET;
std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
int rc = ioctl(s, SIOCGARP, &ar);
close(s);
if (rc < 0) return;
std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
}
} // namespace Poco
#endif
|