[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[win-pv-devel] [PATCH 08/20] Add XenIfaceItf



Abstract the IOCTL calls in a CDevice subclass. Handles translation 
to/from method parameters to/from IOCTL buffers

Signed-off-by: Owen Smith <owen.smith@xxxxxxxxxx>
---
 src/liteagent/DeviceList.cpp       |  10 ++-
 src/liteagent/DeviceList.h         |   2 +-
 src/liteagent/LiteAgent.cpp        |   6 +-
 src/liteagent/LiteAgent.h          |   3 +-
 src/liteagent/XenIfaceItf.cpp      | 145 +++++++++++++++++++++++++++++++++++++
 src/liteagent/XenIfaceItf.h        |  60 +++++++++++++++
 vs2012/liteagent/LiteAgent.vcxproj |   2 +
 vs2013/liteagent/LiteAgent.vcxproj |   2 +
 8 files changed, 223 insertions(+), 7 deletions(-)
 create mode 100644 src/liteagent/XenIfaceItf.cpp
 create mode 100644 src/liteagent/XenIfaceItf.h

diff --git a/src/liteagent/DeviceList.cpp b/src/liteagent/DeviceList.cpp
index 9033407..3fc9a85 100644
--- a/src/liteagent/DeviceList.cpp
+++ b/src/liteagent/DeviceList.cpp
@@ -101,12 +101,18 @@ const wchar_t* CDevice::Path() const
     return m_path.c_str();
 }
 
-bool CDevice::Ioctl(DWORD ioctl, void* in, DWORD insz, void* out, DWORD outsz, 
DWORD* bytes)
+bool CDevice::Ioctl(DWORD ioctl, void* in, DWORD insz, void* out, DWORD outsz, 
DWORD* bytes /*= NULL*/)
 {
     if (m_handle == INVALID_HANDLE_VALUE)
         return false;
 
-    if (!DeviceIoControl(m_handle, ioctl, in, insz, out, outsz, bytes, NULL))
+    DWORD   _bytes;
+    if (!DeviceIoControl(m_handle,
+                        ioctl,
+                        in, insz,
+                        out, outsz,
+                        (bytes == NULL) ? &_bytes : bytes,
+                        NULL))
         return false;
 
     return true;
diff --git a/src/liteagent/DeviceList.h b/src/liteagent/DeviceList.h
index 457a099..6a1b0b4 100644
--- a/src/liteagent/DeviceList.h
+++ b/src/liteagent/DeviceList.h
@@ -49,7 +49,7 @@ public:
     const wchar_t* Path() const;
 
 protected:
-    bool Ioctl(DWORD ioctl, void* in, DWORD insz, void* out, DWORD outsz, 
DWORD* bytes);
+    bool Ioctl(DWORD ioctl, void* in, DWORD insz, void* out, DWORD outsz, 
DWORD* bytes = NULL);
 
 private:
     std::wstring    m_path;
diff --git a/src/liteagent/LiteAgent.cpp b/src/liteagent/LiteAgent.cpp
index a7987dc..908c0c9 100644
--- a/src/liteagent/LiteAgent.cpp
+++ b/src/liteagent/LiteAgent.cpp
@@ -137,14 +137,14 @@ CLiteAgent::~CLiteAgent()
 /*virtual*/ CDevice* CLiteAgent::Create(const wchar_t* path)
 {
     // create subclass of CDevice
-    return new CDevice(path);
+    return new CXenIfaceItf(path);
 }
 
 /*virtual*/ void CLiteAgent::OnDeviceAdded(CDevice* dev)
 {
     CLiteAgent::Log("OnDeviceAdded(%ws)\n", dev->Path());
     if (m_dev == NULL) {
-        m_dev = dev;
+        m_dev = (CXenIfaceItf*)dev;
         // setting active device
     }
 }
@@ -152,7 +152,7 @@ CLiteAgent::~CLiteAgent()
 /*virtual*/ void CLiteAgent::OnDeviceRemoved(CDevice* dev)
 {
     CLiteAgent::Log("OnDeviceRemoved(%ws)\n", dev->Path());
-    if (dev == m_dev) {
+    if ((CXenIfaceItf*)dev == m_dev) {
         m_dev = NULL;
         // active device
     }
diff --git a/src/liteagent/LiteAgent.h b/src/liteagent/LiteAgent.h
index 261e737..3680afd 100644
--- a/src/liteagent/LiteAgent.h
+++ b/src/liteagent/LiteAgent.h
@@ -39,6 +39,7 @@
 #define SVC_DESC "Monitors and provides various metrics to XenStore"
 
 #include "DeviceList.h"
+#include "XenIfaceItf.h"
 
 class CLiteAgent : public IDeviceCreator
 {
@@ -77,7 +78,7 @@ private: // service support
     HANDLE                  m_svc_stop;
     
     CDeviceList             m_devs;
-    CDevice*                m_dev;
+    CXenIfaceItf*           m_dev;
 };
 
 #endif
diff --git a/src/liteagent/XenIfaceItf.cpp b/src/liteagent/XenIfaceItf.cpp
new file mode 100644
index 0000000..7e5fadf
--- /dev/null
+++ b/src/liteagent/XenIfaceItf.cpp
@@ -0,0 +1,145 @@
+/* Copyright (c) Citrix Systems Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, 
+ * with or without modification, are permitted provided 
+ * that the following conditions are met:
+ *
+ * *   Redistributions of source code must retain the above 
+ *     copyright notice, this list of conditions and the 
+ *     following disclaimer.
+ * *   Redistributions in binary form must reproduce the above 
+ *     copyright notice, this list of conditions and the 
+ *     following disclaimer in the documentation and/or other 
+ *     materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
+ * SUCH DAMAGE.
+ */
+
+#include <windows.h>
+#include "XenIfaceItf.h"
+#include "DeviceList.h"
+#include "xeniface_ioctls.h"
+
+CXenIfaceItf::CXenIfaceItf(const wchar_t* path) : CDevice(path)
+{}
+
+/*virtual*/ CXenIfaceItf::~CXenIfaceItf()
+{}
+
+// store interface
+bool CXenIfaceItf::StoreRead(const std::string& path, std::string& value)
+{
+    DWORD   bytes(0);
+    char*   buffer;
+    bool    result;
+
+    Ioctl(IOCTL_XENIFACE_STORE_READ,
+          (void*)path.c_str(), (DWORD)path.length() + 1,
+          NULL, 0,
+          &bytes);
+
+    buffer = new char[bytes + 1];
+    if (buffer == NULL)
+        return false;
+
+    result = Ioctl(IOCTL_XENIFACE_STORE_READ,
+                   (void*)path.c_str(), (DWORD)path.length() + 1,
+                   buffer, bytes);
+
+    buffer[bytes] = 0;
+    if (result)
+        value = buffer;
+
+    delete [] buffer;
+    return result;
+}
+
+bool CXenIfaceItf::StoreWrite(const std::string& path, const std::string& 
value)
+{
+    bool   result;
+    size_t length = path.length() + 1 + value.length() + 1 + 1;
+    char*  buffer = new char[length];
+    if (buffer == NULL)
+        return false;
+
+    memcpy(buffer, path.c_str(), path.length());
+    buffer[path.length()] = 0;
+
+    memcpy(buffer + path.length() + 1, value.c_str(), value.length());
+    buffer[path.length() + 1 + value.length()] = 0;
+    buffer[length - 1] = 0;
+
+    result = Ioctl(IOCTL_XENIFACE_STORE_WRITE, buffer, (DWORD)length, NULL, 0);
+    delete [] buffer;
+    return result;
+}
+
+bool CXenIfaceItf::StoreRemove(const std::string& path)
+{
+    return Ioctl(IOCTL_XENIFACE_STORE_REMOVE,
+                 (void*)path.c_str(), (DWORD)path.length() + 1,
+                 NULL, 0);
+}
+
+bool CXenIfaceItf::StoreAddWatch(const std::string& path, HANDLE evt, void** 
ctxt)
+{
+    XENIFACE_STORE_ADD_WATCH_IN  in  = { (PCHAR)path.c_str(), 
(DWORD)path.length() + 1, evt };
+    XENIFACE_STORE_ADD_WATCH_OUT out = { NULL };
+    if (!Ioctl(IOCTL_XENIFACE_STORE_ADD_WATCH,
+               &in, (DWORD)sizeof(in),
+               &out, (DWORD)sizeof(out)))
+        return false;
+    *ctxt = out.Context;
+    return true;
+}
+
+bool CXenIfaceItf::StoreRemoveWatch(void* ctxt)
+{
+    XENIFACE_STORE_REMOVE_WATCH_IN in = { ctxt };
+    return Ioctl(IOCTL_XENIFACE_STORE_REMOVE_WATCH,
+                 &in, (DWORD)sizeof(in),
+                 NULL, 0);
+}
+
+// suspend interface
+bool CXenIfaceItf::SuspendRegister(HANDLE evt, void** ctxt)
+{
+    XENIFACE_SUSPEND_REGISTER_IN  in  = { evt };
+    XENIFACE_SUSPEND_REGISTER_OUT out = { NULL };
+    if (!Ioctl(IOCTL_XENIFACE_SUSPEND_REGISTER,
+               &in, (DWORD)sizeof(in),
+               &out, (DWORD)sizeof(out)))
+        return false;
+    *ctxt = out.Context;
+    return true;
+}
+
+bool CXenIfaceItf::SuspendDeregister(void* ctxt)
+{
+    XENIFACE_SUSPEND_REGISTER_OUT in = { ctxt };
+    return Ioctl(IOCTL_XENIFACE_SUSPEND_DEREGISTER,
+                 &in, (DWORD)sizeof(in),
+                 NULL, 0);
+}
+
+// sharedinfo interface
+bool CXenIfaceItf::SharedInfoGetTime(FILETIME* time)
+{
+    return Ioctl(IOCTL_XENIFACE_SHAREDINFO_GET_TIME,
+                 NULL, 0,
+                 time, sizeof(FILETIME));
+}
\ No newline at end of file
diff --git a/src/liteagent/XenIfaceItf.h b/src/liteagent/XenIfaceItf.h
new file mode 100644
index 0000000..e36f189
--- /dev/null
+++ b/src/liteagent/XenIfaceItf.h
@@ -0,0 +1,60 @@
+/* Copyright (c) Citrix Systems Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, 
+ * with or without modification, are permitted provided 
+ * that the following conditions are met:
+ *
+ * *   Redistributions of source code must retain the above 
+ *     copyright notice, this list of conditions and the 
+ *     following disclaimer.
+ * *   Redistributions in binary form must reproduce the above 
+ *     copyright notice, this list of conditions and the 
+ *     following disclaimer in the documentation and/or other 
+ *     materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
+ * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
+ * SUCH DAMAGE.
+ */
+
+#ifndef _XENIFACEITF_H_
+#define _XENIFACEITF_H_
+
+#include <windows.h>
+#include "DeviceList.h"
+#include <string>
+
+class CXenIfaceItf : public CDevice
+{
+public:
+    CXenIfaceItf(const wchar_t* path);
+    virtual ~CXenIfaceItf();
+
+public: // store interface
+    bool StoreRead(const std::string& path, std::string& value);
+    bool StoreWrite(const std::string& path, const std::string& value);
+    bool StoreRemove(const std::string& path);
+    bool StoreAddWatch(const std::string& path, HANDLE evt, void** ctxt);
+    bool StoreRemoveWatch(void* ctxt);
+
+public: // suspend interface
+    bool SuspendRegister(HANDLE evt, void** ctxt);
+    bool SuspendDeregister(void* ctxt);
+
+public: // sharedinfo interface
+    bool SharedInfoGetTime(FILETIME* time);
+};
+
+#endif
diff --git a/vs2012/liteagent/LiteAgent.vcxproj 
b/vs2012/liteagent/LiteAgent.vcxproj
index 84b5113..4863061 100644
--- a/vs2012/liteagent/LiteAgent.vcxproj
+++ b/vs2012/liteagent/LiteAgent.vcxproj
@@ -195,10 +195,12 @@
   <ItemGroup>
     <ClCompile Include="..\..\src\liteagent\LiteAgent.cpp" />
     <ClCompile Include="..\..\src\liteagent\DeviceList.cpp" />
+    <ClCompile Include="..\..\src\liteagent\XenIfaceItf.cpp" />
        </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\liteagent\LiteAgent.h" />
     <ClInclude Include="..\..\src\liteagent\DeviceList.h" />
+    <ClInclude Include="..\..\src\liteagent\XenIfaceItf.h" />
        </ItemGroup>
   <ItemGroup>
     <CustomBuild Include="..\..\src\liteagent\messages.mc">
diff --git a/vs2013/liteagent/LiteAgent.vcxproj 
b/vs2013/liteagent/LiteAgent.vcxproj
index 6a3c787..a7d10c0 100644
--- a/vs2013/liteagent/LiteAgent.vcxproj
+++ b/vs2013/liteagent/LiteAgent.vcxproj
@@ -199,10 +199,12 @@
   <ItemGroup>
     <ClCompile Include="..\..\src\liteagent\LiteAgent.cpp" />
     <ClCompile Include="..\..\src\liteagent\DeviceList.cpp" />
+    <ClCompile Include="..\..\src\liteagent\XenIfaceItf.cpp" />
   </ItemGroup>
   <ItemGroup>
     <ClInclude Include="..\..\src\liteagent\LiteAgent.h" />
     <ClInclude Include="..\..\src\liteagent\DeviceList.h" />
+    <ClInclude Include="..\..\src\liteagent\XenIfaceItf.h" />
   </ItemGroup>
   <ItemGroup>
     <CustomBuild Include="..\..\src\liteagent\messages.mc">
-- 
1.9.4.msysgit.1


_______________________________________________
win-pv-devel mailing list
win-pv-devel@xxxxxxxxxxxxxxxxxxxx
http://lists.xenproject.org/cgi-bin/mailman/listinfo/win-pv-devel

 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.