Environment_UNIX.cpp 6.3 KB

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