1//===-- ConnectionSharedMemory.cpp ----------------------------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "lldb/Core/ConnectionSharedMemory.h"
11
12// C Includes
13#include <errno.h>
14#include <pthread.h>
15#include <stdlib.h>
16#include <sys/file.h>
17#include <sys/mman.h>
18#include <sys/stat.h>
19#include <sys/types.h>
20
21// C++ Includes
22// Other libraries and framework includes
23// Project includes
24#include "lldb/lldb-private-log.h"
25#include "lldb/Core/Communication.h"
26#include "lldb/Core/Log.h"
27
28using namespace lldb;
29using namespace lldb_private;
30
31ConnectionSharedMemory::ConnectionSharedMemory () :
32    Connection(),
33    m_name(),
34    m_fd (-1),
35    m_mmap()
36{
37}
38
39ConnectionSharedMemory::~ConnectionSharedMemory ()
40{
41    Disconnect (NULL);
42}
43
44bool
45ConnectionSharedMemory::IsConnected () const
46{
47    return m_fd >= 0;
48}
49
50ConnectionStatus
51ConnectionSharedMemory::Connect (const char *s, Error *error_ptr)
52{
53//    if (s && s[0])
54//    {
55//        if (strstr(s, "shm-create://"))
56//        {
57//        }
58//        else if (strstr(s, "shm-connect://"))
59//        {
60//        }
61//        if (error_ptr)
62//            error_ptr->SetErrorStringWithFormat ("unsupported connection URL: '%s'", s);
63//        return eConnectionStatusError;
64//    }
65    if (error_ptr)
66        error_ptr->SetErrorString("invalid connect arguments");
67    return eConnectionStatusError;
68}
69
70ConnectionStatus
71ConnectionSharedMemory::Disconnect (Error *error_ptr)
72{
73    m_mmap.Clear();
74    if (!m_name.empty())
75    {
76        shm_unlink (m_name.c_str());
77        m_name.clear();
78    }
79    return eConnectionStatusSuccess;
80}
81
82size_t
83ConnectionSharedMemory::Read (void *dst,
84                              size_t dst_len,
85                              uint32_t timeout_usec,
86                              ConnectionStatus &status,
87                              Error *error_ptr)
88{
89    status = eConnectionStatusSuccess;
90    return 0;
91}
92
93size_t
94ConnectionSharedMemory::Write (const void *src, size_t src_len, ConnectionStatus &status, Error *error_ptr)
95{
96    status = eConnectionStatusSuccess;
97    return 0;
98}
99
100ConnectionStatus
101ConnectionSharedMemory::BytesAvailable (uint32_t timeout_usec, Error *error_ptr)
102{
103    return eConnectionStatusLostConnection;
104}
105
106ConnectionStatus
107ConnectionSharedMemory::Open (bool create, const char *name, size_t size, Error *error_ptr)
108{
109    if (m_fd != -1)
110    {
111        if (error_ptr)
112            error_ptr->SetErrorString("already open");
113        return eConnectionStatusError;
114    }
115
116    m_name.assign (name);
117    int oflag = O_RDWR;
118    if (create)
119        oflag |= O_CREAT;
120    m_fd = ::shm_open (m_name.c_str(), oflag, S_IRUSR|S_IWUSR);
121
122    if (create)
123        ::ftruncate (m_fd, size);
124
125    if (m_mmap.MemoryMapFromFileDescriptor(m_fd, 0, size, true, false) == size)
126        return eConnectionStatusSuccess;
127
128    Disconnect(NULL);
129    return eConnectionStatusError;
130}
131
132