Check file sharing permissions based on the file inode instead of the
[wine] / dlls / ole32 / git.c
1 /*
2  * Implementation of the StdGlobalInterfaceTable object
3  *
4  * The GlobalInterfaceTable (GIT) object is used to marshal interfaces between
5  * threading apartments (contexts). When you want to pass an interface but not
6  * as a parameter, it wouldn't get marshalled automatically, so you can use this
7  * object to insert the interface into a table, and you get back a cookie.
8  * Then when it's retrieved, it'll be unmarshalled into the right apartment.
9  *
10  * Copyright 2003 Mike Hearn <mike@theoretic.com>
11  *
12  * This library is free software; you can redistribute it and/or
13  * modify it under the terms of the GNU Lesser General Public
14  * License as published by the Free Software Foundation; either
15  * version 2.1 of the License, or (at your option) any later version.
16  *
17  * This library is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  * Lesser General Public License for more details.
21  *
22  * You should have received a copy of the GNU Lesser General Public
23  * License along with this library; if not, write to the Free Software
24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25  */
26
27 #include "config.h"
28
29 #define NONAMELESSUNION
30 #define NONAMELESSSTRUCT
31 #include <assert.h>
32 #include <stdlib.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <string.h>
36
37 #include "windef.h"
38 #include "winbase.h"
39 #include "winuser.h"
40 #include "objbase.h"
41 #include "ole2.h"
42 #include "winerror.h"
43 #include "winreg.h"
44 #include "winternl.h"
45
46 #include "compobj_private.h" 
47
48 #include "wine/debug.h"
49
50 WINE_DEFAULT_DEBUG_CHANNEL(ole);
51
52 /****************************************************************************
53  * StdGlobalInterfaceTable definition
54  *
55  * This class implements IGlobalInterfaceTable and is a process-wide singleton
56  * used for marshalling interfaces between threading apartments using cookies.
57  */
58
59 /* Each entry in the linked list of GIT entries */
60 typedef struct StdGITEntry
61 {
62   DWORD cookie;
63   IID iid;         /* IID of the interface */
64   IStream* stream; /* Holds the marshalled interface */
65
66   struct StdGITEntry* next;
67   struct StdGITEntry* prev;  
68 } StdGITEntry;
69
70 /* Class data */
71 typedef struct StdGlobalInterfaceTableImpl
72 {
73   ICOM_VFIELD(IGlobalInterfaceTable);
74
75   ULONG ref;
76   struct StdGITEntry* firstEntry;
77   struct StdGITEntry* lastEntry;
78   ULONG nextCookie;
79   
80 } StdGlobalInterfaceTableImpl;
81
82 void* StdGlobalInterfaceTableInstance;
83
84
85 /* IUnknown */
86 static HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject);
87 static ULONG   WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface);
88 static ULONG   WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface);
89 /* IGlobalInterfaceTable */
90 static HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie);
91 static HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie);
92 static HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv);
93
94 /* Virtual function table */
95 static ICOM_VTABLE(IGlobalInterfaceTable) StdGlobalInterfaceTableImpl_Vtbl =
96 {
97   ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
98   StdGlobalInterfaceTable_QueryInterface,
99   StdGlobalInterfaceTable_AddRef,
100   StdGlobalInterfaceTable_Release,
101   StdGlobalInterfaceTable_RegisterInterfaceInGlobal,
102   StdGlobalInterfaceTable_RevokeInterfaceFromGlobal,
103   StdGlobalInterfaceTable_GetInterfaceFromGlobal
104 };
105
106 static CRITICAL_SECTION git_section;
107 static CRITICAL_SECTION_DEBUG critsect_debug =
108 {
109     0, 0, &git_section,
110     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
111       0, 0, { 0, (DWORD)(__FILE__ ": global interface table") }
112 };
113 static CRITICAL_SECTION git_section = { &critsect_debug, -1, 0, 0, 0, 0 };
114
115
116 /***
117  * Let's go! Here is the constructor and destructor for the class.
118  *
119  */
120
121 /** This function constructs the GIT. It should only be called once **/
122 void* StdGlobalInterfaceTable_Construct() {
123   StdGlobalInterfaceTableImpl* newGIT;
124
125   newGIT = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGlobalInterfaceTableImpl));
126   if (newGIT == 0) return newGIT;
127
128   newGIT->lpVtbl = &StdGlobalInterfaceTableImpl_Vtbl;
129   newGIT->ref = 1;      /* Initialise the reference count */
130   newGIT->firstEntry = NULL; /* we start with an empty table   */
131   newGIT->lastEntry  = NULL;
132   newGIT->nextCookie = 0xf100; /* that's where windows starts, so that's where we start */
133   TRACE("Created the GIT at %p\n", newGIT);
134
135   return (void*)newGIT;
136 }
137
138 /** This destroys it again. It should revoke all the held interfaces first **/
139 void StdGlobalInterfaceTable_Destroy(void* self) {
140   TRACE("(%p)\n", self);
141   FIXME("Revoke held interfaces here\n");
142   
143   HeapFree(GetProcessHeap(), 0, self);
144   StdGlobalInterfaceTableInstance = NULL;
145 }
146
147 /***
148  * A helper function to traverse the list and find the entry that matches the cookie.
149  * Returns NULL if not found
150  */
151 StdGITEntry* StdGlobalInterfaceTable_FindEntry(IGlobalInterfaceTable* iface, DWORD cookie) {
152   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
153   StdGITEntry* e;
154
155   TRACE("iface=%p, cookie=0x%x\n", iface, (UINT)cookie);
156
157   EnterCriticalSection(&git_section);
158   e = self->firstEntry;
159   while (e != NULL) {
160     if (e->cookie == cookie) {
161       LeaveCriticalSection(&git_section);
162       return e;
163     }
164     e = e->next;
165   }
166   LeaveCriticalSection(&git_section);
167   
168   TRACE("Entry not found\n");
169   return NULL;
170 }
171
172 /***
173  * Here's the boring boilerplate stuff for IUnknown
174  */
175
176 HRESULT WINAPI StdGlobalInterfaceTable_QueryInterface(IGlobalInterfaceTable* iface, REFIID riid, void** ppvObject) {
177   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
178
179   /* Make sure silly coders can't crash us */
180   if (ppvObject == 0) return E_INVALIDARG;
181
182   *ppvObject = 0; /* assume we don't have the interface */
183
184   /* Do we implement that interface? */
185   if (IsEqualIID(&IID_IUnknown, riid)) {
186     *ppvObject = (IGlobalInterfaceTable*) self;
187   } else if (IsEqualIID(&IID_IGlobalInterfaceTable, riid)) {
188     *ppvObject = (IGlobalInterfaceTable*) self;
189   } else return E_NOINTERFACE;
190
191   /* Now inc the refcount */
192   StdGlobalInterfaceTable_AddRef(iface);
193   return S_OK;
194 }
195
196 ULONG WINAPI StdGlobalInterfaceTable_AddRef(IGlobalInterfaceTable* iface) {
197   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
198
199   /* InterlockedIncrement(&self->ref); */
200   return self->ref;
201 }
202
203 ULONG WINAPI StdGlobalInterfaceTable_Release(IGlobalInterfaceTable* iface) {
204   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
205
206   /* InterlockedDecrement(&self->ref); */
207   if (self->ref == 0) {
208     /* Hey ho, it's time to go, so long again 'till next weeks show! */
209     StdGlobalInterfaceTable_Destroy(self);
210     return 0;
211   }
212
213   return self->ref;
214 }
215
216 /***
217  * Now implement the actual IGlobalInterfaceTable interface
218  */
219
220 HRESULT WINAPI StdGlobalInterfaceTable_RegisterInterfaceInGlobal(IGlobalInterfaceTable* iface, IUnknown* pUnk, REFIID riid, DWORD* pdwCookie) {
221   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
222   IStream* stream = NULL;
223   HRESULT hres;
224   StdGITEntry* entry;
225
226   TRACE("iface=%p, pUnk=%p, riid=%s, pdwCookie=0x%p\n", iface, pUnk, debugstr_guid(riid), pdwCookie);
227
228   if (pUnk == NULL) return E_INVALIDARG;
229   
230   /* marshal the interface */
231   TRACE("About to marshal the interface\n");
232   hres = CoMarshalInterThreadInterfaceInStream(riid, pUnk, &stream);
233   if (hres) return hres;
234   entry = HeapAlloc(GetProcessHeap(), 0, sizeof(StdGITEntry));
235   if (entry == NULL) return E_OUTOFMEMORY;
236
237   EnterCriticalSection(&git_section);
238   
239   entry->iid = *riid;
240   entry->stream = stream;
241   entry->cookie = self->nextCookie;
242   self->nextCookie++; /* inc the cookie count */
243
244   /* insert the new entry at the end of the list */
245   entry->next = NULL;
246   entry->prev = self->lastEntry;
247   if (entry->prev) entry->prev->next = entry;
248   else self->firstEntry = entry;
249   self->lastEntry = entry;
250
251   /* and return the cookie */
252   *pdwCookie = entry->cookie;
253   
254   LeaveCriticalSection(&git_section);
255   
256   TRACE("Cookie is 0x%lx\n", entry->cookie);
257   return S_OK;
258 }
259
260 HRESULT WINAPI StdGlobalInterfaceTable_RevokeInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie) {
261   StdGlobalInterfaceTableImpl* const self = (StdGlobalInterfaceTableImpl*) iface;
262   StdGITEntry* entry;
263
264   TRACE("iface=%p, dwCookie=0x%x\n", iface, (UINT)dwCookie);
265   
266   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
267   if (entry == NULL) {
268     TRACE("Entry not found\n");
269     return E_INVALIDARG; /* not found */
270   }
271   
272   /* Free the stream */
273   IStream_Release(entry->stream);
274                     
275   /* chop entry out of the list, and free the memory */
276   EnterCriticalSection(&git_section);
277   if (entry->prev) entry->prev->next = entry->next;
278   else self->firstEntry = entry->next;
279   if (entry->next) entry->next->prev = entry->prev;
280   else self->lastEntry = entry->prev;
281   LeaveCriticalSection(&git_section);
282
283   HeapFree(GetProcessHeap(), 0, entry);
284   return S_OK;
285 }
286
287 HRESULT WINAPI StdGlobalInterfaceTable_GetInterfaceFromGlobal(IGlobalInterfaceTable* iface, DWORD dwCookie, REFIID riid, void **ppv) {
288   StdGITEntry* entry;
289   HRESULT hres;
290   LARGE_INTEGER move;
291   LPUNKNOWN lpUnk;
292   
293   TRACE("dwCookie=0x%lx, riid=%s, ppv=%p\n", dwCookie, debugstr_guid(riid), ppv);
294   
295   entry = StdGlobalInterfaceTable_FindEntry(iface, dwCookie);
296   if (entry == NULL) return E_INVALIDARG;
297
298   if (!IsEqualIID(&entry->iid, riid)) {
299     WARN("entry->iid (%s) != riid\n", debugstr_guid(&entry->iid));
300     return E_INVALIDARG;
301   }
302   TRACE("entry=%p\n", entry);
303   
304   /* unmarshal the interface */
305   hres = CoUnmarshalInterface(entry->stream, riid, ppv);
306   if (hres) {
307     WARN("Failed to unmarshal stream\n");
308     return hres;
309   }
310   
311   /* rewind stream, in case it's used again */
312   move.u.LowPart = 0;
313   move.u.HighPart = 0;
314   IStream_Seek(entry->stream, move, STREAM_SEEK_SET, NULL);
315
316   /* addref it */
317   lpUnk = *ppv;
318   IUnknown_AddRef(lpUnk);
319   TRACE("ppv=%p\n", *ppv);
320   return S_OK;
321 }
322
323 /* Classfactory definition - despite what MSDN says, some programs need this */
324
325 static HRESULT WINAPI GITCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid, LPVOID *ppv) {
326   *ppv = NULL;
327   if (IsEqualIID(riid,&IID_IUnknown) || IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
328     *ppv = (LPVOID)iface;
329     return S_OK;
330   }
331   return E_NOINTERFACE;
332 }
333 static ULONG WINAPI GITCF_AddRef(LPCLASSFACTORY iface) { return 2; }
334 static ULONG WINAPI GITCF_Release(LPCLASSFACTORY iface) { return 1; }
335
336 static HRESULT WINAPI GITCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pUnk, REFIID riid, LPVOID *ppv) {
337   if (IsEqualIID(riid,&IID_IGlobalInterfaceTable)) {
338     if (StdGlobalInterfaceTableInstance == NULL) 
339       StdGlobalInterfaceTableInstance = StdGlobalInterfaceTable_Construct();
340     return IGlobalInterfaceTable_QueryInterface( (IGlobalInterfaceTable*) StdGlobalInterfaceTableInstance, riid, ppv);
341   }
342
343   FIXME("(%s), not supported.\n",debugstr_guid(riid));
344   return E_NOINTERFACE;
345 }
346
347 static HRESULT WINAPI GITCF_LockServer(LPCLASSFACTORY iface, BOOL fLock) {
348     FIXME("(%d), stub!\n",fLock);
349     return S_OK;
350 }
351
352 static ICOM_VTABLE(IClassFactory) GITClassFactoryVtbl = {
353     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
354     GITCF_QueryInterface,
355     GITCF_AddRef,
356     GITCF_Release,
357     GITCF_CreateInstance,
358     GITCF_LockServer
359 };
360 static ICOM_VTABLE(IClassFactory) *PGITClassFactoryVtbl = &GITClassFactoryVtbl;
361
362 HRESULT StdGlobalInterfaceTable_GetFactory(LPVOID *ppv) {
363   *ppv = &PGITClassFactoryVtbl;
364   TRACE("Returning GIT classfactory\n");
365   return S_OK;
366 }