Environment_VMS.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // Environment_VMS.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/Environment_VMS.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Core
  8. // Module: Environment
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/Environment_VMS.h"
  16. #include "Poco/Exception.h"
  17. #include <stdlib.h>
  18. #include <starlet.h>
  19. #include <descrip.h>
  20. #include <ssdef.h>
  21. #include <syidef.h>
  22. #include <iledef.h>
  23. #include <lnmdef.h>
  24. #include <ioctl.h>
  25. #include <sys/socket.h>
  26. #include <sys/types.h>
  27. #include <netinet/in.h>
  28. #include <net/if.h>
  29. #include <inet.h>
  30. #include <netdb.h>
  31. #include <net/if.h>
  32. #include <net/if_arp.h>
  33. #include <unistd.h>
  34. #define MAXHOSTNAMELEN 64
  35. namespace Poco {
  36. FastMutex EnvironmentImpl::_mutex;
  37. std::string EnvironmentImpl::getImpl(const std::string& name)
  38. {
  39. FastMutex::ScopedLock lock(_mutex);
  40. const char* val = getenv(name.c_str());
  41. if (val)
  42. return std::string(val);
  43. else
  44. throw NotFoundException(name);
  45. }
  46. bool EnvironmentImpl::hasImpl(const std::string& name)
  47. {
  48. FastMutex::ScopedLock lock(_mutex);
  49. return getenv(name.c_str()) != 0;
  50. }
  51. void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
  52. {
  53. FastMutex::ScopedLock lock(_mutex);
  54. if (setenv(name.c_str(), value.c_str(), 1))
  55. {
  56. std::string msg = "cannot set environment variable: ";
  57. msg.append(name);
  58. throw SystemException(msg);
  59. }
  60. }
  61. std::string EnvironmentImpl::osNameImpl()
  62. {
  63. return getsyi(SYI$_NODE_SWTYPE);
  64. }
  65. std::string EnvironmentImpl::osDisplayNameImpl()
  66. {
  67. return osNameImpl();
  68. }
  69. std::string EnvironmentImpl::osVersionImpl()
  70. {
  71. return getsyi(SYI$_VERSION);
  72. }
  73. std::string EnvironmentImpl::osArchitectureImpl()
  74. {
  75. return getsyi(SYI$_ARCH_NAME);
  76. }
  77. std::string EnvironmentImpl::nodeNameImpl()
  78. {
  79. return getsyi(SYI$_NODENAME);
  80. }
  81. void EnvironmentImpl::nodeIdImpl(NodeId& id)
  82. {
  83. std::memset(&id, 0, sizeof(id));
  84. char name[MAXHOSTNAMELEN];
  85. if (gethostname(name, sizeof(name)))
  86. return;
  87. struct hostent* pHost = gethostbyname(name);
  88. if (!pHost) return;
  89. int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
  90. if (s == -1) return;
  91. struct arpreq ar;
  92. std::memset(&ar, 0, sizeof(ar));
  93. struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
  94. pAddr->sin_family = AF_INET;
  95. std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
  96. int rc = ioctl(s, SIOCGARP, &ar);
  97. close(s);
  98. if (rc < 0) return;
  99. std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
  100. }
  101. unsigned EnvironmentImpl::processorCountImpl()
  102. {
  103. #pragma pointer_size save
  104. #pragma pointer_size 32
  105. Poco::UInt32 count;
  106. unsigned short length;
  107. ILE3 items[2];
  108. items[0].ile3$w_code = SYI$_ACTIVECPU_CNT;
  109. items[0].ile3$w_length = sizeof(count);
  110. items[0].ile3$ps_bufaddr = &count;
  111. items[0].ile3$ps_retlen_addr = &length;
  112. items[1].ile3$w_code = 0;
  113. items[1].ile3$w_length = 0;
  114. if (sys$getsyiw(0, 0, 0, items, 0, 0, 0) == 1)
  115. return count;
  116. else
  117. throw SystemException("$GETSYI failed");
  118. #pragma pointer_size restore
  119. }
  120. std::string EnvironmentImpl::getsyi(unsigned short code)
  121. {
  122. #pragma pointer_size save
  123. #pragma pointer_size 32
  124. unsigned char result[16];
  125. unsigned short length;
  126. ILE3 items[2];
  127. items[0].ile3$w_code = code;
  128. items[0].ile3$w_length = sizeof(result);
  129. items[0].ile3$ps_bufaddr = result;
  130. items[0].ile3$ps_retlen_addr = &length;
  131. items[1].ile3$w_code = 0;
  132. items[1].ile3$w_length = 0;
  133. if (sys$getsyiw(0, 0, 0, items, 0, 0, 0) == 1)
  134. return std::string((char*) result, length);
  135. else
  136. throw SystemException("$GETSYI failed");
  137. #pragma pointer_size restore
  138. }
  139. std::string EnvironmentImpl::trnlnm(const std::string& name)
  140. {
  141. #pragma pointer_size save
  142. #pragma pointer_size 32
  143. unsigned char result[LNM$C_NAMLENGTH];
  144. unsigned short length;
  145. ILE3 items[2];
  146. items[0].ile3$w_code = LNM$_STRING;
  147. items[0].ile3$w_length = sizeof(result);
  148. items[0].ile3$ps_bufaddr = result;
  149. items[0].ile3$ps_retlen_addr = &length;
  150. items[1].ile3$w_code = 0;
  151. items[1].ile3$w_length = 0;
  152. #pragma pointer_size restore
  153. unsigned int trnAttr = LNM$M_CASE_BLIND;
  154. POCO_DESCRIPTOR_LITERAL(tableDsc, "LNM$FILE_DEV");
  155. POCO_DESCRIPTOR_STRING(nameDsc, name);
  156. if (sys$trnlnm(&trnAttr, &tableDsc, &nameDsc, 0, &items) == 1)
  157. {
  158. if (result[0] == 0x1B)
  159. return std::string((char*) result + 4, length - 4);
  160. else
  161. return std::string((char*) result, length);
  162. }
  163. else
  164. {
  165. return std::string();
  166. }
  167. }
  168. } // namespace Poco