~ubuntu-branches/ubuntu/lucid/codelite/lucid

« back to all changes in this revision

Viewing changes to sdk/codelite_indexer/network/np_connections_server.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Chow Loong Jin
  • Date: 2009-02-10 02:27:55 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090210022755-m5692nfc1t5uf1w9
Tags: 1.0.2759+dfsg-0ubuntu1
* New upstream release (LP: #327216).
* debian/patches/series, debian/patches/00_fix-ia64-build.patch:
  + Dropped, applied upstream already.
* debian/patches/02_fix-desktop.patch,
  debian/patches/03_fix-sh.patch:
  + Refreshed to patch cleanly.
* debian/rules:
  + Make get-orig-source honour UPSTREAM_VERSION if set.
* debian/ctags-le.1,
  debian/codelite_indexer.1,
  debian/codelite.manpages:
  + Dropped ctags-le manpage, since ctags-le was replaced by
    codelite_indexer.
  + Added codelite_indexer manpage.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "np_connections_server.h"
 
2
#include "named_pipe_server.h"
 
3
 
 
4
#ifndef __WXMSW__
 
5
#  include <sys/types.h>
 
6
#  include <sys/socket.h>
 
7
#  include <sys/un.h>
 
8
#  include <stdio.h>
 
9
#endif
 
10
 
 
11
#ifdef __WXMSW__
 
12
static PIPE_HANDLE createNamedPipe(const char* pipeName, SECURITY_ATTRIBUTES sa)
 
13
{
 
14
        return CreateNamedPipe( pipeName,
 
15
                                PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED,
 
16
                                PIPE_TYPE_BYTE | PIPE_WAIT | PIPE_READMODE_BYTE,
 
17
                                PIPE_UNLIMITED_INSTANCES,
 
18
                                8192, 8192, 0,
 
19
                                &sa);
 
20
}
 
21
 
 
22
static PACL prepareNamedPipeAcl(SECURITY_DESCRIPTOR* sd, SECURITY_ATTRIBUTES* sa)
 
23
{
 
24
        DWORD req_acl_size;
 
25
        char everyone_buf[32], owner_buf[32];
 
26
        PSID sid_everyone, sid_owner;
 
27
        SID_IDENTIFIER_AUTHORITY
 
28
        siaWorld = SECURITY_WORLD_SID_AUTHORITY,
 
29
                   siaCreator = SECURITY_CREATOR_SID_AUTHORITY;
 
30
        PACL acl;
 
31
 
 
32
        sid_everyone = (PSID)&everyone_buf;
 
33
        sid_owner = (PSID)&owner_buf;
 
34
 
 
35
        req_acl_size = sizeof(ACL) +
 
36
                       (2 * ((sizeof(ACCESS_ALLOWED_ACE) - sizeof(DWORD)) + GetSidLengthRequired(1)));
 
37
 
 
38
        acl = (PACL) malloc(req_acl_size);
 
39
 
 
40
        if (acl == NULL) {
 
41
                return NULL;
 
42
        }
 
43
 
 
44
        if (!InitializeSid(sid_everyone, &siaWorld, 1)) {
 
45
                goto out_fail;
 
46
        }
 
47
        *GetSidSubAuthority(sid_everyone, 0) = SECURITY_WORLD_RID;
 
48
 
 
49
        if (!InitializeSid(sid_owner, &siaCreator, 1)) {
 
50
                goto out_fail;
 
51
        }
 
52
        *GetSidSubAuthority(sid_owner, 0) = SECURITY_CREATOR_OWNER_RID;
 
53
 
 
54
        if (!InitializeAcl(acl, req_acl_size, ACL_REVISION)) {
 
55
                goto out_fail;
 
56
        }
 
57
 
 
58
        if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_GENERIC_READ | FILE_GENERIC_WRITE, sid_everyone)) {
 
59
                goto out_fail;
 
60
        }
 
61
 
 
62
        if (!AddAccessAllowedAce(acl, ACL_REVISION, FILE_ALL_ACCESS, sid_owner)) {
 
63
                goto out_fail;
 
64
        }
 
65
 
 
66
        if (!InitializeSecurityDescriptor(sd, SECURITY_DESCRIPTOR_REVISION)) {
 
67
                goto out_fail;
 
68
        }
 
69
 
 
70
        if (!SetSecurityDescriptorDacl(sd, TRUE, acl, FALSE)) {
 
71
                goto out_fail;
 
72
        }
 
73
 
 
74
        sa->lpSecurityDescriptor = sd;
 
75
 
 
76
        return acl;
 
77
 
 
78
out_fail:
 
79
        free(acl);
 
80
        return NULL;
 
81
 
 
82
}
 
83
#endif
 
84
 
 
85
clNamedPipeConnectionsServer::clNamedPipeConnectionsServer(const char* pipeName)
 
86
: _listenHandle(INVALID_PIPE_HANDLE)
 
87
{
 
88
        _pipePath = strdup(pipeName);
 
89
}
 
90
 
 
91
clNamedPipeConnectionsServer::~clNamedPipeConnectionsServer()
 
92
{
 
93
        if(_pipePath) {
 
94
                free(_pipePath);
 
95
                _pipePath = NULL;       
 
96
        }
 
97
        _listenHandle = INVALID_PIPE_HANDLE;
 
98
}
 
99
 
 
100
PIPE_HANDLE clNamedPipeConnectionsServer::initNewInstance()
 
101
{
 
102
#ifdef __WXMSW__
 
103
        PACL acl;
 
104
        SECURITY_DESCRIPTOR  sd = {0};
 
105
 
 
106
        SECURITY_ATTRIBUTES  sa = {0};
 
107
        memset(&sa, 0, sizeof(sa));
 
108
        sa.nLength = sizeof(sa);
 
109
        sa.bInheritHandle = TRUE;
 
110
        sa.lpSecurityDescriptor = NULL;
 
111
 
 
112
        HANDLE hPipe = createNamedPipe(_pipePath, sa);
 
113
        if (!hPipe) {
 
114
                this->setLastError(NP_SERVER_UNKNOWN_ERROR);
 
115
                return INVALID_PIPE_HANDLE;
 
116
        }
 
117
        return hPipe;
 
118
#else
 
119
        if(_listenHandle == INVALID_PIPE_HANDLE) {
 
120
                
 
121
                unlink(_pipePath);
 
122
                struct sockaddr_un server;
 
123
                
 
124
                _listenHandle = socket(AF_UNIX, SOCK_STREAM, 0);
 
125
                if (_listenHandle < 0) {
 
126
                        perror("ERROR: socket");
 
127
                        return INVALID_PIPE_HANDLE;
 
128
                }
 
129
                
 
130
                server.sun_family = AF_UNIX;
 
131
                strcpy(server.sun_path, _pipePath);
 
132
                if (bind(_listenHandle, (struct sockaddr *) &server, sizeof(struct sockaddr_un))) {
 
133
                        perror("ERROR: binding stream socket");
 
134
                        return INVALID_PIPE_HANDLE;
 
135
                }       
 
136
        }
 
137
        listen(_listenHandle, 10);
 
138
        return _listenHandle;
 
139
#endif
 
140
}
 
141
 
 
142
bool clNamedPipeConnectionsServer::shutdown()
 
143
{
 
144
        if(_pipePath){
 
145
                free(_pipePath);
 
146
                _pipePath = NULL;       
 
147
        }
 
148
 
 
149
#ifndef __WXMSW__
 
150
        close(_listenHandle);
 
151
#endif
 
152
 
 
153
        _listenHandle = INVALID_PIPE_HANDLE;
 
154
        return true;
 
155
}
 
156
 
 
157
clNamedPipe *clNamedPipeConnectionsServer::waitForNewConnection( int timeout )
 
158
{
 
159
        PIPE_HANDLE hConn = this->initNewInstance();
 
160
 
 
161
#ifdef __WXMSW__
 
162
        OVERLAPPED ov = {0};
 
163
        HANDLE ev = CreateEvent(NULL, TRUE, TRUE, NULL);
 
164
        ov.hEvent = ev;
 
165
 
 
166
        bool fConnected = ConnectNamedPipe(hConn, &ov);
 
167
        if (fConnected != 0) {
 
168
                this->setLastError(NP_SERVER_UNKNOWN_ERROR);
 
169
                return NULL;
 
170
        }
 
171
 
 
172
        switch (GetLastError()) {
 
173
 
 
174
                // The overlapped connection in progress.
 
175
        case ERROR_IO_PENDING: {
 
176
                DWORD res = WaitForSingleObject(ov.hEvent, timeout) ;
 
177
                switch (res) {
 
178
                case WAIT_OBJECT_0 : {
 
179
                        clNamedPipeServer *conn = new clNamedPipeServer(_pipePath);
 
180
                        conn->setHandle(hConn);
 
181
                        return conn;
 
182
                }
 
183
                case WAIT_TIMEOUT : {
 
184
                        this->setLastError(NP_SERVER_TIMEOUT);
 
185
                        return NULL;
 
186
                }
 
187
                default: {
 
188
                        this->setLastError(NP_SERVER_UNKNOWN_ERROR);
 
189
                        return NULL;
 
190
                }
 
191
 
 
192
                }
 
193
        }
 
194
 
 
195
        case ERROR_PIPE_CONNECTED: {
 
196
                clNamedPipeServer *conn = new clNamedPipeServer(_pipePath);
 
197
                conn->setHandle(hConn);
 
198
                return conn;
 
199
        }
 
200
        // If an error occurs during the connect operation...
 
201
        default: {
 
202
                this->setLastError(NP_SERVER_UNKNOWN_ERROR);
 
203
                return NULL;
 
204
        }
 
205
        }
 
206
#else
 
207
        // accept new connection
 
208
        if(hConn != INVALID_PIPE_HANDLE){
 
209
                PIPE_HANDLE fd = ::accept(hConn, 0, 0);
 
210
                if(fd > 0){
 
211
                        clNamedPipeServer *conn = new clNamedPipeServer(_pipePath);
 
212
                        conn->setHandle(fd);
 
213
                        return conn;
 
214
                } else {
 
215
                        perror("ERROR: accept");
 
216
                        return NULL;
 
217
                }
 
218
                
 
219
        }
 
220
        return NULL;
 
221
#endif
 
222
}