File: stubslave.cpp

package info (click to toggle)
tinymux 2.10.1.14-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 6,212 kB
  • ctags: 8,535
  • sloc: cpp: 111,587; sh: 5,867; ansic: 141; makefile: 139
file content (644 lines) | stat: -rw-r--r-- 16,039 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
/*! \file stubslave.cpp
 * \brief This slave hosts modules in a separate process.
 *
 * $Id: stubslave.cpp 5476 2012-02-29 22:45:33Z brazilofmux $
 *
 */

#include "copyright.h"
#include "autoconf.h"
#include "config.h"

#if defined(TINYMUX_MODULES)

#include "libmux.h"
#include "modules.h"

QUEUE_INFO    Queue_In;
QUEUE_INFO    Queue_Out;

#define NUM_CLASSES 1
static MUX_CLASS_INFO stubslave_classes[NUM_CLASSES] =
{
    { CID_StubSlave }
};

bool bStubSlaveShutdown = false;

extern "C" MUX_RESULT DCL_API Stub_PipePump(void)
{
    static UINT8 arg[QUEUE_BLOCK_SIZE];
    size_t nWanted = sizeof(arg);
    while (  Pipe_GetBytes(&Queue_Out, &nWanted, arg)
          && 0 < nWanted)
    {
        if (write(1, arg, nWanted) < 0)
        {
            return MUX_E_FAIL;
        }
        nWanted = sizeof(arg);
    }

    // If we are shutting down, don't wait for any more input from the pipe.
    //
    if (!bStubSlaveShutdown)
    {
        int len = read(0, arg, sizeof(arg));
        if (0 < len)
        {
            Pipe_AppendBytes(&Queue_In, len, arg);
        }
        else if (len < 0)
        {
            return MUX_E_FAIL;
        }
    }
    return MUX_S_OK;
}

void Stub_ShoveChars(void)
{
    QUEUE_INFO Queue_Frame;
    Pipe_InitializeQueueInfo(&Queue_Frame);

    MUX_RESULT mr = MUX_S_OK;
    while (  !bStubSlaveShutdown
          && MUX_SUCCEEDED(mr))
    {
        mr = Stub_PipePump();
        Pipe_DecodeFrames(CHANNEL_INVALID, &Queue_Frame);
    }
    Stub_PipePump();
}

extern "C" MUX_RESULT DCL_API stubslave_GetClassObject(MUX_CID cid, MUX_IID iid, void **ppv)
{
    MUX_RESULT mr = MUX_E_CLASSNOTAVAILABLE;

    if (CID_StubSlave == cid)
    {
        CStubSlaveFactory *pStubSlaveFactory = NULL;
        try
        {
            pStubSlaveFactory = new CStubSlaveFactory;
        }
        catch (...)
        {
            ; // Nothing.
        }

        if (NULL == pStubSlaveFactory)
        {
            return MUX_E_OUTOFMEMORY;
        }

        mr = pStubSlaveFactory->QueryInterface(iid, ppv);
        pStubSlaveFactory->Release();
    }
    return mr;
}

int main(int argc, char *argv[])
{
    Pipe_InitializeQueueInfo(&Queue_In);
    Pipe_InitializeQueueInfo(&Queue_Out);

    MUX_RESULT mr = MUX_S_OK;
    mr = mux_InitModuleLibrary(IsSlaveProcess, Stub_PipePump, &Queue_In, &Queue_Out);
    if (MUX_SUCCEEDED(mr))
    {
        mr = mux_RegisterClassObjects(NUM_CLASSES, stubslave_classes, stubslave_GetClassObject);
        if (MUX_SUCCEEDED(mr))
        {
            Stub_ShoveChars();
            mr = mux_RevokeClassObjects(NUM_CLASSES, stubslave_classes);
        }
        mr = mux_FinalizeModuleLibrary();
    }

    return MUX_RESULT_TO_EXIT_STATUS(mr);
}

// CStubSlave component which is not directly accessible.
//
CStubSlave::CStubSlave(void) : m_cRef(1)
{
}

CStubSlave::~CStubSlave()
{
}

MUX_RESULT CStubSlave::QueryInterface(MUX_IID iid, void **ppv)
{
    if (mux_IID_IUnknown == iid)
    {
        *ppv = static_cast<mux_ISlaveControl *>(this);
    }
    else if (IID_ISlaveControl == iid)
    {
        *ppv = static_cast<mux_ISlaveControl *>(this);
    }
    else if (mux_IID_IMarshal == iid)
    {
        *ppv = static_cast<mux_IMarshal *>(this);
    }
    else
    {
        *ppv = NULL;
        return MUX_E_NOINTERFACE;
    }
    reinterpret_cast<mux_IUnknown *>(*ppv)->AddRef();
    return MUX_S_OK;
}

UINT32 CStubSlave::AddRef(void)
{
    m_cRef++;
    return m_cRef;
}

UINT32 CStubSlave::Release(void)
{
    m_cRef--;
    if (0 == m_cRef)
    {
        delete this;
        return 0;
    }
    return m_cRef;
}

MUX_RESULT CStubSlave::GetUnmarshalClass(MUX_IID riid, marshal_context ctx, MUX_CID *pcid)
{
    UNUSED_PARAMETER(ctx);

    if (NULL == pcid)
    {
        return MUX_E_INVALIDARG;
    }
    else if (  IID_ISlaveControl == riid
            && CrossProcess == ctx)
    {
        // We only support cross-process at the moment.
        //
        *pcid = CID_StubSlaveProxy;
        return MUX_S_OK;
    }
    return MUX_E_NOTIMPLEMENTED;
}

MUX_RESULT CStubSlave_Disconnect(CHANNEL_INFO *pci, QUEUE_INFO *pqi)
{
    UNUSED_PARAMETER(pqi);

    // Get our interface pointer from the channel.
    //
    mux_IUnknown *pIUnknown= static_cast<mux_IUnknown *>(pci->pInterface);
    pci->pInterface = NULL;

    // Tear down our side of the communication.  Our callback functions will
    // no longer be called.
    //
    Pipe_FreeChannel(pci);

    if (NULL != pIUnknown)
    {
        pIUnknown->Release();
        return MUX_S_OK;
    }
    else
    {
        return MUX_E_NOINTERFACE;
    }
}

MUX_RESULT CStubSlave_Call(CHANNEL_INFO *pci, QUEUE_INFO *pqi)
{
    mux_ISlaveControl *pISlaveControl = static_cast<mux_ISlaveControl *>(pci->pInterface);
    if (NULL == pISlaveControl)
    {
        return MUX_E_NOINTERFACE;
    }

    UINT32 iMethod;
    size_t nWanted = sizeof(iMethod);
    if (  !Pipe_GetBytes(pqi, &nWanted, &iMethod)
       || nWanted != sizeof(iMethod))
    {
        return MUX_E_INVALIDARG;
    }

    MUX_RESULT mr = MUX_S_OK;

    // The IUnknown methods (0, 1, and 2) do not make it across, so we don't
    // attempt to handle them here.  Instead, when the reference count on
    // CStubSlaveProxy goes to zero, it drops the connection and destroys itself.
    // We see that as a call to CStubSlave_Disconnect.
    //
    switch (iMethod)
    {
    case 3: // MUX_RESULT AddModule(const UTF8 aModuleName[], const UTF8/UTF16 aFileName[]);
        {
            struct FRAME
            {
                size_t nModuleName;
                size_t nFileName;
            } CallFrame;

            nWanted = sizeof(CallFrame);
            if (  !Pipe_GetBytes(pqi, &nWanted, &CallFrame)
               || nWanted != sizeof(CallFrame))
            {
                return MUX_E_INVALIDARG;
            }

            UTF8  *pModuleName = NULL;
#if defined(WINDOWS_FILES)
            UTF16 *pFileName = NULL;
#elif defined(UNIX_FILES)
            UTF8  *pFileName = NULL;
#endif // UNIX_FILES

            try
            {
                pModuleName = new UTF8[CallFrame.nModuleName];
#if defined(WINDOWS_FILES)
                pFileName   = new UTF16[CallFrame.nFileName];
#elif defined(UNIX_FILES)
                pFileName   = new UTF8[CallFrame.nFileName];
#endif // UNIX_FILES
            }
            catch (...)
            {
                ; // Nothing.
            }

            if (  NULL != pModuleName
               && NULL != pFileName)
            {
                nWanted = CallFrame.nModuleName;
                if (  Pipe_GetBytes(pqi, &nWanted, pModuleName)
                   && nWanted == CallFrame.nModuleName)
                {
                    nWanted = CallFrame.nFileName;
                    if (  Pipe_GetBytes(pqi, &nWanted, pFileName)
                       && nWanted == CallFrame.nFileName)
                    {
                        struct RETURN
                        {
                            MUX_RESULT mr;
                        } ReturnFrame;

                        ReturnFrame.mr = pISlaveControl->AddModule(pModuleName, pFileName);

                        Pipe_EmptyQueue(pqi);
                        Pipe_AppendBytes(pqi, sizeof(ReturnFrame), &ReturnFrame);
                        mr = MUX_S_OK;
                    }
                }
            }
            else
            {
                mr = MUX_E_OUTOFMEMORY;
            }

            if (NULL != pModuleName)
            {
                delete pModuleName;
                pModuleName = NULL;
            }
            if (NULL != pFileName)
            {
                delete pFileName;
                pFileName = NULL;
            }
        }
        break;

    case 4: // MUX_RESULT RemoveModule(const UTF8 aModuleName[]);
        {
            struct FRAME
            {
                size_t nModuleName;
            } CallFrame;

            nWanted = sizeof(CallFrame);
            if (  !Pipe_GetBytes(pqi, &nWanted, &CallFrame)
               || nWanted != sizeof(CallFrame))
            {
                return MUX_E_INVALIDARG;
            }

            UTF8  *pModuleName = NULL;

            try
            {
                pModuleName = new UTF8[CallFrame.nModuleName];
            }
            catch (...)
            {
                ; // Nothing.
            }

            if (NULL != pModuleName)
            {
                nWanted = CallFrame.nModuleName;
                if (  Pipe_GetBytes(pqi, &nWanted, pModuleName)
                   && nWanted == CallFrame.nModuleName)
                {
                    struct RETURN
                    {
                        MUX_RESULT mr;
                    } ReturnFrame;

                    ReturnFrame.mr = pISlaveControl->RemoveModule(pModuleName);

                    Pipe_EmptyQueue(pqi);
                    Pipe_AppendBytes(pqi, sizeof(ReturnFrame), &ReturnFrame);
                    mr = MUX_S_OK;
                }
            }
            else
            {
                mr = MUX_E_OUTOFMEMORY;
            }

            if (NULL != pModuleName)
            {
                delete pModuleName;
                pModuleName = NULL;
            }
        }
        break;

    case 5: // MUX_RESULT ModuleInfo(int iModule, MUX_MODULE_INFO *pModuleInfo);
        {
            struct FRAME
            {
                int iModule;
            } CallFrame;

            nWanted = sizeof(CallFrame);
            if (  !Pipe_GetBytes(pqi, &nWanted, &CallFrame)
               || nWanted != sizeof(CallFrame))
            {
                return MUX_E_INVALIDARG;
            }

            struct RETURN
            {
                size_t     nName;
                bool       bLoaded;
                MUX_RESULT mr;
            } ReturnFrame;

            MUX_MODULE_INFO ModuleInfo;
            ReturnFrame.mr = pISlaveControl->ModuleInfo(CallFrame.iModule, &ModuleInfo);

            Pipe_EmptyQueue(pqi);
            if (  MUX_SUCCEEDED(ReturnFrame.mr)
               && MUX_S_FALSE != ReturnFrame.mr)
            {
                ReturnFrame.bLoaded = ModuleInfo.bLoaded;
                ReturnFrame.nName   = strlen((const char *)ModuleInfo.pName)+1;
            }
            else
            {
                ReturnFrame.bLoaded = false;
                ReturnFrame.nName   = 0;
            }

            Pipe_AppendBytes(pqi, sizeof(ReturnFrame), &ReturnFrame);
            Pipe_AppendBytes(pqi, ReturnFrame.nName, ModuleInfo.pName);

            mr = MUX_S_OK;
        }
        break;

    case 6: // MUX_RESULT ModuleMaintenance(void);
        {
            struct RETURN
            {
                MUX_RESULT mr;
            } ReturnFrame;

            ReturnFrame.mr = pISlaveControl->ModuleMaintenance();

            Pipe_EmptyQueue(pqi);
            Pipe_AppendBytes(pqi, sizeof(ReturnFrame), &ReturnFrame);
            mr = MUX_S_OK;
        }
        break;

    case 7: // MUX_RESULT ShutdownSlave(void);
        {
            struct RETURN
            {
                MUX_RESULT mr;
            } ReturnFrame;

            ReturnFrame.mr = pISlaveControl->ShutdownSlave();

            Pipe_EmptyQueue(pqi);
            Pipe_AppendBytes(pqi, sizeof(ReturnFrame), &ReturnFrame);
            mr = MUX_S_OK;
        }
        break;
    }
    return mr;
}

MUX_RESULT CStubSlave::MarshalInterface(QUEUE_INFO *pqi, MUX_IID riid, marshal_context ctx)
{
    // Parameter validation and initialization.
    //
    MUX_RESULT mr = MUX_S_OK;
    if (NULL == pqi)
    {
        mr = MUX_E_INVALIDARG;
    }
    else if (IID_ISlaveControl != riid)
    {
        mr = MUX_E_FAIL;
    }
    else if (CrossProcess != ctx)
    {
        mr = MUX_E_NOTIMPLEMENTED;
    }
    else
    {
        mux_ISlaveControl *pISlaveControl = NULL;
        mr = QueryInterface(IID_ISlaveControl, (void **)&pISlaveControl);
        if (MUX_SUCCEEDED(mr))
        {
            // Construct a packet sufficient to allow the proxy to communicate with us.
            //
            CHANNEL_INFO *pChannel = Pipe_AllocateChannel(CStubSlave_Call, NULL, CStubSlave_Disconnect);
            if (NULL != pChannel)
            {
                pChannel->pInterface = pISlaveControl;
                Pipe_AppendBytes(pqi, sizeof(pChannel->nChannel), (UTF8*)(&pChannel->nChannel));
                mr =  MUX_S_OK;
            }
            else
            {
                pISlaveControl->Release();
                pISlaveControl = NULL;
                mr = MUX_E_OUTOFMEMORY;
            }
        }
    }
    return mr;
}

MUX_RESULT CStubSlave::UnmarshalInterface(QUEUE_INFO *pqi, MUX_IID riid, void **ppv)
{
    return MUX_E_NOTIMPLEMENTED;
}

MUX_RESULT CStubSlave::ReleaseMarshalData(QUEUE_INFO *pqi)
{
    // Since the Marshal Data is like an extra reference on an object, if the
    // Marshaled Data is never unmarshalled, libmux should use this function
    // to release the reference to the component.  This is only implemented on
    // the server side -- not the proxy.
    //
    UINT32 nChannel;
    size_t nWanted = sizeof(nChannel);
    if (  Pipe_GetBytes(pqi, &nWanted, &nChannel)
       && sizeof(nChannel) == nWanted)
    {
        CHANNEL_INFO *pChannel = Pipe_FindChannel(nChannel);
        if (NULL != pChannel)
        {
            CStubSlave_Disconnect(pChannel, pqi);
        }
    }
    return MUX_S_OK;
}

MUX_RESULT CStubSlave::DisconnectObject(void)
{
    // This is called when the hosting process is about to go down to give the
    // component a chance to notify its proxy that it is about to shut down.
    // This is only implemented on the server side -- not the proxy.
    //
    // TODO: There isn't a mechanism for sending such a notification, yet.
    //
    return MUX_S_OK;
}


#if defined(WINDOWS_FILES)
MUX_RESULT CStubSlave::AddModule(const UTF8 aModuleName[], const UTF16 aFileName[])
#elif defined(UNIX_FILES)
MUX_RESULT CStubSlave::AddModule(const UTF8 aModuleName[], const UTF8 aFileName[])
#endif // UNIX_FILES
{
    return mux_AddModule(aModuleName, aFileName);
}

MUX_RESULT CStubSlave::RemoveModule(const UTF8 aModuleName[])
{
    return mux_RemoveModule(aModuleName);
}

MUX_RESULT CStubSlave::ModuleInfo(int iModule, MUX_MODULE_INFO *pModuleInfo)
{
    return mux_ModuleInfo(iModule, pModuleInfo);
}

MUX_RESULT CStubSlave::ModuleMaintenance(void)
{
    return mux_ModuleMaintenance();
}

MUX_RESULT CStubSlave::ShutdownSlave(void)
{
    bStubSlaveShutdown = true;
    return MUX_S_OK;
}

// Factory for CStubSlave component which is not directly accessible.
//
CStubSlaveFactory::CStubSlaveFactory(void) : m_cRef(1)
{
}

CStubSlaveFactory::~CStubSlaveFactory()
{
}

MUX_RESULT CStubSlaveFactory::QueryInterface(MUX_IID iid, void **ppv)
{
    if (mux_IID_IUnknown == iid)
    {
        *ppv = static_cast<mux_IClassFactory *>(this);
    }
    else if (mux_IID_IClassFactory == iid)
    {
        *ppv = static_cast<mux_IClassFactory *>(this);
    }
    else
    {
        *ppv = NULL;
        return MUX_E_NOINTERFACE;
    }
    reinterpret_cast<mux_IUnknown *>(*ppv)->AddRef();
    return MUX_S_OK;
}

UINT32 CStubSlaveFactory::AddRef(void)
{
    m_cRef++;
    return m_cRef;
}

UINT32 CStubSlaveFactory::Release(void)
{
    m_cRef--;
    if (0 == m_cRef)
    {
        delete this;
        return 0;
    }
    return m_cRef;
}

MUX_RESULT CStubSlaveFactory::CreateInstance(mux_IUnknown *pUnknownOuter, MUX_IID iid, void **ppv)
{
    // Disallow attempts to aggregate this component.
    //
    if (NULL != pUnknownOuter)
    {
        return MUX_E_NOAGGREGATION;
    }

    CStubSlave *pStubSlave = NULL;
    try
    {
        pStubSlave = new CStubSlave;
    }
    catch (...)
    {
        ; // Nothing.
    }

    if (NULL == pStubSlave)
    {
        return MUX_E_OUTOFMEMORY;
    }

    MUX_RESULT mr = pStubSlave->QueryInterface(iid, ppv);
    pStubSlave->Release();
    return mr;
}

MUX_RESULT CStubSlaveFactory::LockServer(bool bLock)
{
    UNUSED_PARAMETER(bLock);
    return MUX_S_OK;
}

#endif // TINYMUX_MODULES