Environment_VMS.cpp
Go to the documentation of this file.
00001 //
00002 // Environment_VMS.cpp
00003 //
00004 // $Id: //poco/1.3/Foundation/src/Environment_VMS.cpp#2 $
00005 //
00006 // Library: Foundation
00007 // Package: Core
00008 // Module:  Environment
00009 //
00010 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
00011 // and Contributors.
00012 //
00013 // Permission is hereby granted, free of charge, to any person or organization
00014 // obtaining a copy of the software and accompanying documentation covered by
00015 // this license (the "Software") to use, reproduce, display, distribute,
00016 // execute, and transmit the Software, and to prepare derivative works of the
00017 // Software, and to permit third-parties to whom the Software is furnished to
00018 // do so, all subject to the following:
00019 // 
00020 // The copyright notices in the Software and this entire statement, including
00021 // the above license grant, this restriction and the following disclaimer,
00022 // must be included in all copies of the Software, in whole or in part, and
00023 // all derivative works of the Software, unless such copies or derivative
00024 // works are solely in the form of machine-executable object code generated by
00025 // a source language processor.
00026 // 
00027 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00028 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00029 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
00030 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
00031 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
00032 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00033 // DEALINGS IN THE SOFTWARE.
00034 //
00035 
00036 
00037 #include "Poco/Environment_VMS.h"
00038 #include "Poco/Exception.h"
00039 #include <stdlib.h>
00040 #include <starlet.h>
00041 #include <descrip.h>
00042 #include <ssdef.h>
00043 #include <syidef.h>
00044 #include <iledef.h>
00045 #include <lnmdef.h>
00046 #include <ioctl.h>
00047 #include <sys/socket.h>
00048 #include <sys/types.h>
00049 #include <netinet/in.h>
00050 #include <net/if.h>
00051 #include <inet.h>
00052 #include <netdb.h>
00053 #include <net/if.h>
00054 #include <net/if_arp.h>
00055 #include <unistd.h>
00056 
00057 
00058 #define MAXHOSTNAMELEN 64
00059 
00060 
00061 namespace Poco {
00062 
00063 
00064 FastMutex EnvironmentImpl::_mutex;
00065 
00066 
00067 std::string EnvironmentImpl::getImpl(const std::string& name)
00068 {
00069         FastMutex::ScopedLock lock(_mutex);
00070         
00071         const char* val = getenv(name.c_str());
00072         if (val)
00073                 return std::string(val);
00074         else
00075                 throw NotFoundException(name);
00076 }
00077 
00078 
00079 bool EnvironmentImpl::hasImpl(const std::string& name)
00080 {
00081         FastMutex::ScopedLock lock(_mutex);
00082 
00083         return getenv(name.c_str()) != 0;
00084 }
00085 
00086 
00087 void EnvironmentImpl::setImpl(const std::string& name, const std::string& value)
00088 {
00089         FastMutex::ScopedLock lock(_mutex);
00090         
00091         if (setenv(name.c_str(), value.c_str(), 1))
00092         {
00093                 std::string msg = "cannot set environment variable: ";
00094                 msg.append(name);
00095                 throw SystemException(msg);
00096         }
00097 }
00098 
00099 
00100 std::string EnvironmentImpl::osNameImpl()
00101 {
00102         return getsyi(SYI$_NODE_SWTYPE);
00103 }
00104 
00105 
00106 std::string EnvironmentImpl::osVersionImpl()
00107 {
00108         return getsyi(SYI$_VERSION);
00109 }
00110 
00111 
00112 std::string EnvironmentImpl::osArchitectureImpl()
00113 {
00114         return getsyi(SYI$_ARCH_NAME);
00115 }
00116 
00117 
00118 std::string EnvironmentImpl::nodeNameImpl()
00119 {
00120         return getsyi(SYI$_NODENAME);
00121 }
00122 
00123 
00124 void EnvironmentImpl::nodeIdImpl(NodeId& id)
00125 {
00126         char name[MAXHOSTNAMELEN];
00127         if (gethostname(name, sizeof(name)))
00128                 throw SystemException("cannot get host name");
00129 
00130         struct hostent* pHost = gethostbyname(name);
00131         if (!pHost) throw SystemException("cannot get host IP address");
00132 
00133         int s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00134         if (s == -1) throw SystemException("cannot open socket");
00135 
00136         struct arpreq ar;
00137         std::memset(&ar, 0, sizeof(ar));
00138         struct sockaddr_in* pAddr = reinterpret_cast<struct sockaddr_in*>(&ar.arp_pa);
00139         pAddr->sin_family = AF_INET;
00140         std::memcpy(&pAddr->sin_addr, *pHost->h_addr_list, sizeof(struct in_addr));
00141         int rc = ioctl(s, SIOCGARP, &ar);
00142         close(s);
00143         if (rc < 0) throw SystemException("cannot get MAC address");
00144         std::memcpy(&id, ar.arp_ha.sa_data, sizeof(id));
00145 }
00146 
00147 
00148 std::string EnvironmentImpl::getsyi(unsigned short code)
00149 {
00150         #pragma pointer_size save
00151         #pragma pointer_size 32
00152 
00153         unsigned char result[16];
00154         unsigned short length;
00155 
00156         ILE3 items[2];
00157         items[0].ile3$w_code         = code;
00158         items[0].ile3$w_length       = sizeof(result);
00159         items[0].ile3$ps_bufaddr     = result;
00160         items[0].ile3$ps_retlen_addr = &length;
00161         items[1].ile3$w_code         = 0;
00162         items[1].ile3$w_length       = 0;
00163 
00164         if (sys$getsyiw(0, 0, 0, items, 0, 0, 0) == 1)
00165                 return std::string((char*) result, length);
00166         else
00167                 throw SystemException("$GETSYI failed");
00168 
00169         #pragma pointer_size restore
00170 }
00171 
00172 
00173 std::string EnvironmentImpl::trnlnm(const std::string& name)
00174 {
00175         #pragma pointer_size save
00176         #pragma pointer_size 32
00177 
00178         unsigned char result[LNM$C_NAMLENGTH];
00179         unsigned short length;
00180         
00181         ILE3 items[2];
00182         items[0].ile3$w_code         = LNM$_STRING;
00183         items[0].ile3$w_length       = sizeof(result);
00184         items[0].ile3$ps_bufaddr     = result;
00185         items[0].ile3$ps_retlen_addr = &length;
00186         items[1].ile3$w_code         = 0;
00187         items[1].ile3$w_length       = 0;
00188 
00189         #pragma pointer_size restore
00190 
00191         unsigned int trnAttr = LNM$M_CASE_BLIND;
00192         POCO_DESCRIPTOR_LITERAL(tableDsc, "LNM$FILE_DEV");
00193         POCO_DESCRIPTOR_STRING(nameDsc, name);
00194         if (sys$trnlnm(&trnAttr, &tableDsc, &nameDsc, 0, &items) == 1)
00195         {
00196                 if (result[0] == 0x1B)
00197                         return std::string((char*) result + 4, length - 4);
00198                 else
00199                         return std::string((char*) result, length);
00200         }
00201         else
00202         {
00203                 return std::string();
00204         }
00205 }
00206 
00207 
00208 } // namespace Poco


pluginlib
Author(s): Tully Foote and Eitan Marder-Eppstein
autogenerated on Sat Dec 28 2013 17:20:19