19189Sroland//===-- ConnectionSharedMemory.cpp ----------------------------*- C++ -*-===//
210307Sctornqvi//
39189Sroland//                     The LLVM Compiler Infrastructure
49189Sroland//
59189Sroland// This file is distributed under the University of Illinois Open Source
69189Sroland// License. See LICENSE.TXT for details.
79189Sroland//
89189Sroland//===----------------------------------------------------------------------===//
99189Sroland
109189Sroland#include "lldb/Core/ConnectionSharedMemory.h"
119189Sroland
129189Sroland// C Includes
139189Sroland#include <errno.h>
149189Sroland#include <stdlib.h>
159189Sroland#ifdef _WIN32
169189Sroland#include "lldb/Host/windows/windows.h"
179189Sroland#else
189189Sroland#include <sys/file.h>
199189Sroland#include <sys/mman.h>
209189Sroland#include <sys/stat.h>
219189Sroland#include <sys/types.h>
229189Sroland#endif
239189Sroland
249189Sroland// C++ Includes
259189Sroland// Other libraries and framework includes
269189Sroland// Project includes
279189Sroland#include "lldb/lldb-private-log.h"
2812290Salanb#include "lldb/Core/Communication.h"
299189Sroland#include "lldb/Core/Log.h"
3011707Stpivovarova
3111707Stpivovarovausing namespace lldb;
3211707Stpivovarovausing namespace lldb_private;
3311707Stpivovarova
3411707StpivovarovaConnectionSharedMemory::ConnectionSharedMemory () :
359189Sroland    Connection(),
369189Sroland    m_name(),
3711707Stpivovarova    m_fd (-1),
3811707Stpivovarova    m_mmap()
399475Sneliasso{
409189Sroland}
4111707Stpivovarova
4211707StpivovarovaConnectionSharedMemory::~ConnectionSharedMemory ()
439189Sroland{
449189Sroland    Disconnect (NULL);
459189Sroland}
469189Sroland
479189Srolandbool
489189SrolandConnectionSharedMemory::IsConnected () const
499189Sroland{
509189Sroland    return m_fd >= 0;
519189Sroland}
529189Sroland
539189SrolandConnectionStatus
549189SrolandConnectionSharedMemory::Connect (const char *s, Error *error_ptr)
559189Sroland{
569189Sroland//    if (s && s[0])
579189Sroland//    {
589189Sroland//        if (strstr(s, "shm-create://"))
599189Sroland//        {
609189Sroland//        }
619189Sroland//        else if (strstr(s, "shm-connect://"))
629189Sroland//        {
639189Sroland//        }
649189Sroland//        if (error_ptr)
659189Sroland//            error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
669189Sroland//        return eConnectionStatusError;
679189Sroland//    }
689189Sroland    if (error_ptr)
699189Sroland        error_ptr->SetErrorString("invalid connect arguments");
709189Sroland    return eConnectionStatusError;
719189Sroland}
729189Sroland
739189SrolandConnectionStatus
749189SrolandConnectionSharedMemory::Disconnect (Error *error_ptr)
759189Sroland{
769189Sroland    m_mmap.Clear();
779189Sroland    if (!m_name.empty())
789189Sroland    {
799189Sroland#ifdef _WIN32
809189Sroland        close(m_fd);
819189Sroland        m_fd = -1;
829189Sroland#else
839189Sroland        shm_unlink (m_name.c_str());
849189Sroland#endif
859189Sroland        m_name.clear();
869189Sroland    }
879189Sroland    return eConnectionStatusSuccess;
889189Sroland}
899189Sroland
909189Srolandsize_t
919189SrolandConnectionSharedMemory::Read (void *dst,
929189Sroland                              size_t dst_len,
939189Sroland                              uint32_t timeout_usec,
949189Sroland                              ConnectionStatus &status,
959189Sroland                              Error *error_ptr)
969189Sroland{
979189Sroland    status = eConnectionStatusSuccess;
989189Sroland    return 0;
999189Sroland}
1009189Sroland
1019189Srolandsize_t
1029189SrolandConnectionSharedMemory::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
1039189Sroland{
1049189Sroland    status = eConnectionStatusSuccess;
1059189Sroland    return 0;
1069189Sroland}
1079189Sroland
1089189SrolandConnectionStatus
1099189SrolandConnectionSharedMemory::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
1109189Sroland{
1119189Sroland    return eConnectionStatusLostConnection;
1129189Sroland}
1139189Sroland
1149189SrolandConnectionStatus
1159189SrolandConnectionSharedMemory::Open (bool create, const char *name, size_t size, Error *error_ptr)
1169189Sroland{
1179189Sroland    if (m_fd != -1)
1189189Sroland    {
1199189Sroland        if (error_ptr)
1209189Sroland            error_ptr->SetErrorString("already open");
1219189Sroland        return eConnectionStatusError;
1229189Sroland    }
1239189Sroland
1249189Sroland    m_name.assign (name);
1259189Sroland
1269189Sroland#ifdef _WIN32
1279189Sroland    HANDLE handle;
1289189Sroland    if (create)
129        handle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, (DWORD)(size >> 32), (DWORD)(size), name);
130    else
131        handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, name);
132
133    m_fd = _open_osfhandle((intptr_t)handle, 0);
134#else
135    int oflag = O_RDWR;
136    if (create)
137        oflag |= O_CREAT;
138    m_fd = ::shm_open (m_name.c_str(), oflag, S_IRUSR|S_IWUSR);
139
140    if (create)
141        ::ftruncate (m_fd, size);
142#endif
143
144    if (m_mmap.MemoryMapFromFileDescriptor(m_fd, 0, size, true, false) == size)
145        return eConnectionStatusSuccess;
146
147    Disconnect(NULL);
148    return eConnectionStatusError;
149}
150
151