Environment_VX.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. // Environment_VX.cpp
  2. //
  3. // $Id: //poco/1.4/Foundation/src/Environment_VX.cpp#3 $
  4. //
  5. // Library: Foundation
  6. // Package: Core
  7. // Module: Environment
  8. //
  9. // Copyright (c) 2004-2011, Applied Informatics Software Engineering GmbH.
  10. // and Contributors.
  11. //
  12. // SPDX-License-Identifier: BSL-1.0
  13. //
  14. #include "Poco/Environment_VX.h"
  15. #include "Poco/Exception.h"
  16. #include "Poco/Buffer.h"
  17. #include <VxWorks.h>
  18. #include <envLib.h>
  19. #include <hostLib.h>
  20. #include <ifLib.h>
  21. #include <sockLib.h>
  22. #include <ioLib.h>
  23. #include <version.h>
  24. #include <cstring>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <sys/ioctl.h>
  28. #include <sys/socket.h>
  29. #include <sys/types.h>
  30. #include <netinet/in.h>
  31. #include <net/if.h>
  32. #include <arpa/inet.h>
  33. #include <netinet/if_ether.h>
  34. #include <ifLib.h>
  35. #include <unistd.h>
  36. namespace Poco {
  37. EnvironmentImpl::StringMap EnvironmentImpl::_map;
  38. FastMutex EnvironmentImpl::_mutex;
  39. std::string EnvironmentImpl::getImpl(const std::string& name)
  40. {
  41. FastMutex::ScopedLock lock(_mutex);
  42. const char* val = getenv(name.c_str());
  43. if (val)
  44. return std::string(val);
  45. else
  46. throw NotFoundException(name);
  47. }
  48. bool EnvironmentImpl::hasImpl(const std::string& name)
  49. {
  50. FastMutex::ScopedLock lock(_mutex);
  51. return getenv(name.c_str()) != 0;
  52. }
  53. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  54. {
  55. FastMutex::ScopedLock lock(_mutex);
  56. std::string var = name;
  57. var.append("=");
  58. var.append(value);
  59. _map[name] = var;
  60. if (putenv((char*) _map[name].c_str()))
  61. {
  62. std::string msg = "cannot set environment variable: ";
  63. msg.append(name);
  64. throw SystemException(msg);
  65. }
  66. }
  67. std::string EnvironmentImpl::osNameImpl()
  68. {
  69. return runtimeName;
  70. }
  71. std::string EnvironmentImpl::osDisplayNameImpl()
  72. {
  73. return osNameImpl();
  74. }
  75. std::string EnvironmentImpl::osVersionImpl()
  76. {
  77. return runtimeVersion;
  78. }
  79. std::string EnvironmentImpl::osArchitectureImpl()
  80. {
  81. #if POCO_ARCH == POCO_ARCH_IA32
  82. return "i386";
  83. #elif POCO_ARCH == POCO_ARCH_MIPS
  84. return "mips";
  85. #elif POCO_ARCH == POCO_ARCH_PPC
  86. return "ppc";
  87. #elif POCO_ARCH == POCO_ARCH_ARM
  88. return "arm";
  89. #elif POCO_ARCH == POCO_ARCH_SH
  90. return "sh";
  91. #else
  92. return "unknown";
  93. #endif
  94. }
  95. std::string EnvironmentImpl::nodeNameImpl()
  96. {
  97. char buffer[64];
  98. if (gethostname(buffer, sizeof(buffer)) == OK)
  99. return buffer;
  100. else
  101. return "unknown";
  102. }
  103. unsigned EnvironmentImpl::processorCountImpl()
  104. {
  105. return 1;
  106. }
  107. void EnvironmentImpl::nodeIdImpl(NodeId& id)
  108. {
  109. std::memset(&id, 0, sizeof(id));
  110. int ifIndex = 1;
  111. char ifName[32];
  112. for (;;)
  113. {
  114. if (ifIndexToIfName(ifIndex, ifName) == OK)
  115. {
  116. struct ifnet* pIf = ifunit(ifName);
  117. if (pIf)
  118. {
  119. std::memcpy(&id, ((struct arpcom *) pIf)->ac_enaddr, sizeof(id));
  120. return;
  121. }
  122. }
  123. else break;
  124. ++ifIndex;
  125. }
  126. throw SystemException("cannot get Ethernet hardware address");
  127. }
  128. } // namespace Poco