summaryrefslogtreecommitdiff
path: root/cpp/src/windows/QpiddBroker.cpp
blob: 89a8945d00cfd999c2df79287c4261d01d8f69fa (plain)
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
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

#include "config.h"
#include "qpidd.h"
#include "SCM.h"
#include "qpid/Exception.h"
#include "qpid/Options.h"
#include "qpid/Plugin.h"
#include "qpid/sys/IntegerTypes.h"
#include "qpid/sys/windows/check.h"
#include "qpid/broker/Broker.h"

#include <iostream>
#include <windows.h>

namespace qpid {
namespace broker {

BootstrapOptions::BootstrapOptions(const char* argv0)
  : qpid::Options("Options"),
    common("", QPIDD_CONF_FILE, QPIDC_CONF_FILE),
    module(QPIDD_MODULE_DIR),
    log(argv0)
{
    add(common);
    add(module);
    add(log);
}

// Local functions to set and get the pid via a LockFile.
namespace {

const std::string TCP = "tcp";

// ShutdownEvent maintains an event that can be used to ask the broker
// to stop. Analogous to sending SIGTERM/SIGINT to the posix broker.
// The signal() method signals the event.
class ShutdownEvent {
  public:
    ShutdownEvent(int port);
    ~ShutdownEvent();

    void create();
    void open();
    void signal();

  private:
    std::string eventName;

  protected:
    HANDLE event;
};

class ShutdownHandler : public ShutdownEvent, public qpid::sys::Runnable {
  public:
    ShutdownHandler(int port, const boost::intrusive_ptr<Broker>& b)
      : ShutdownEvent(port)  { broker = b; }

  private:
    virtual void run();     // Inherited from Runnable
    boost::intrusive_ptr<Broker> broker;
};

ShutdownEvent::ShutdownEvent(int port) : event(NULL) {
    std::ostringstream name;
    name << "qpidd_" << port << std::ends;
    eventName = name.str();
}

void ShutdownEvent::create() {
    // Auto-reset event in case multiple processes try to signal a
    // broker that doesn't respond for some reason. Initially not signaled.
    event = ::CreateEvent(NULL, false, false, eventName.c_str());
    QPID_WINDOWS_CHECK_NULL(event);
}

void ShutdownEvent::open() {
    // TODO: Might need to search Global\\ name if unadorned name fails
    event = ::OpenEvent(EVENT_MODIFY_STATE, false, eventName.c_str());
    QPID_WINDOWS_CHECK_NULL(event);
}

ShutdownEvent::~ShutdownEvent() {
    ::CloseHandle(event);
    event = NULL;
}

void ShutdownEvent::signal() {
    QPID_WINDOWS_CHECK_NOT(::SetEvent(event), 0);
}


void ShutdownHandler::run() {
    if (event == NULL)
        return;
    ::WaitForSingleObject(event, INFINITE);
    if (broker.get()) {
        broker->shutdown();
        broker = 0;             // Release the broker reference
    }
}

// Console control handler to properly handle ctl-c.
int ourPort;
BOOL CtrlHandler(DWORD ctl)
{
    ShutdownEvent shutter(ourPort);     // We have to have set up the port before interrupting
    shutter.open();
    shutter.signal();
    return ((ctl == CTRL_C_EVENT || ctl == CTRL_CLOSE_EVENT) ? TRUE : FALSE);
}

template <typename T>
class NamedSharedMemory {
    std::string name;
    HANDLE memory;
    T* data;

public:
    NamedSharedMemory(const std::string&);
    ~NamedSharedMemory();

    T& create();
    T& get();
};

template <typename T>
NamedSharedMemory<T>::NamedSharedMemory(const std::string& n) :
    name(n),
    memory(NULL),
    data(0)
{}

template <typename T>
NamedSharedMemory<T>::~NamedSharedMemory() {
    if (data)
        ::UnmapViewOfFile(data);
    if (memory != NULL)
        ::CloseHandle(memory);
}

template <typename T>
T& NamedSharedMemory<T>::create() {
    assert(memory == NULL);

    // Create named shared memory file
    memory = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(T), name.c_str());
    QPID_WINDOWS_CHECK_NULL(memory);

    // Map file into memory
    data = static_cast<T*>(::MapViewOfFile(memory, FILE_MAP_WRITE, 0, 0, 0));
    QPID_WINDOWS_CHECK_NULL(data);

    return *data;
}

template <typename T>
T& NamedSharedMemory<T>::get() {
    if (memory == NULL) {
        // TODO: Might need to search Global\\ name if unadorned name fails
        memory = ::OpenFileMapping(FILE_MAP_WRITE, FALSE, name.c_str());
        QPID_WINDOWS_CHECK_NULL(memory);

        data = static_cast<T*>(::MapViewOfFile(memory, FILE_MAP_WRITE, 0, 0, 0));
        QPID_WINDOWS_CHECK_NULL(data);
    }

    return *data;
}

std::string brokerInfoName(uint16_t port)
{
    std::ostringstream path;
    path << "qpidd_info_" << port;
    return path.str();
}

struct BrokerInfo {
    DWORD pid;
};

// Service-related items. Only involved when running the broker as a Windows
// service.

const std::string svcName = "qpidd";
SERVICE_STATUS svcStatus;
SERVICE_STATUS_HANDLE svcStatusHandle = 0;

// This function is only called when the broker is run as a Windows
// service. It receives control requests from Windows.
VOID WINAPI SvcCtrlHandler(DWORD control)
{
    switch(control) {
    case SERVICE_CONTROL_STOP:
        svcStatus.dwCurrentState = SERVICE_STOP_PENDING;
        svcStatus.dwControlsAccepted = 0;
        svcStatus.dwCheckPoint = 1;
        svcStatus.dwWaitHint = 5000;  // 5 secs.
        ::SetServiceStatus(svcStatusHandle, &svcStatus);
        CtrlHandler(CTRL_C_EVENT);
        break;
 
    case SERVICE_CONTROL_INTERROGATE:
        break;
 
    default:
        break;
    }
}

VOID WINAPI ServiceMain(DWORD argc, LPTSTR *argv)
{
    ::memset(&svcStatus, 0, sizeof(svcStatus));
    svcStatusHandle = ::RegisterServiceCtrlHandler(svcName.c_str(),
                                                   SvcCtrlHandler);
    svcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    svcStatus.dwCheckPoint = 1;
    svcStatus.dwWaitHint = 10000;  // 10 secs.
    svcStatus.dwCurrentState = SERVICE_START_PENDING;
    ::SetServiceStatus(svcStatusHandle, &svcStatus);
    // QpiddBroker class resets state to running.
    svcStatus.dwWin32ExitCode = run_broker(argc, argv, true);
    svcStatus.dwCurrentState = SERVICE_STOPPED;
    svcStatus.dwCheckPoint = 0;
    svcStatus.dwWaitHint = 0;
    ::SetServiceStatus(svcStatusHandle, &svcStatus);
}

}  // namespace


struct ProcessControlOptions : public qpid::Options {
    bool quit;
    bool check;
    std::string transport;

    ProcessControlOptions()
        : qpid::Options("Process control options"),
          quit(false),
          check(false),
          transport(TCP)
    {
        addOptions()
            ("check,c", qpid::optValue(check), "Prints the broker's process ID to stdout and returns 0 if the broker is running, otherwise returns 1")
            ("transport", qpid::optValue(transport, "TRANSPORT"), "The transport for which to return the port")
            ("quit,q", qpid::optValue(quit), "Tells the broker to shut down");
    }
};

struct ServiceOptions : public qpid::Options {
    bool install;
    bool start;
    bool stop;
    bool uninstall;
    bool daemon;
    std::string startType;
    std::string startArgs;
    std::string account;
    std::string password;
    std::string depends;

    ServiceOptions() 
        : qpid::Options("Service options"),
          install(false),
          start(false),
          stop(false),
          uninstall(false),
          daemon(false),
          startType("demand"),
          startArgs(""),
          account("NT AUTHORITY\\LocalService"),
          password(""),
          depends("")
    {
        addOptions()
            ("install", qpid::optValue(install), "Install as service")
            ("start-type", qpid::optValue(startType, "auto|demand|disabled"), "Service start type\nApplied at install time only.")
            ("arguments", qpid::optValue(startArgs, "COMMAND LINE ARGS"), "Arguments to pass when service auto-starts")
            ("account", qpid::optValue(account, "(LocalService)"), "Account to run as, default is LocalService\nApplied at install time only.")
            ("password", qpid::optValue(password, "PASSWORD"), "Account password, if needed\nApplied at install time only.")
            ("depends", qpid::optValue(depends, "(comma delimited list)"), "Names of services that must start before this service\nApplied at install time only.")
            ("start", qpid::optValue(start), "Start the service.")
            ("stop", qpid::optValue(stop), "Stop the service.")
            ("uninstall", qpid::optValue(uninstall), "Uninstall the service.");
    }
};

struct QpiddWindowsOptions : public QpiddOptionsPrivate {
    ProcessControlOptions control;
    ServiceOptions service;
    QpiddWindowsOptions(QpiddOptions *parent) : QpiddOptionsPrivate(parent) {
        parent->add(service);
        parent->add(control);
    }
};

QpiddOptions::QpiddOptions(const char* argv0)
  : qpid::Options("Options"),
    common("", QPIDD_CONF_FILE, QPIDC_CONF_FILE),
    module(QPIDD_MODULE_DIR),
    log(argv0)
{
    add(common);
    add(module);
    add(broker);
    add(log);

    platform.reset(new QpiddWindowsOptions(this));
    qpid::Plugin::addOptions(*this);
}

void QpiddOptions::usage() const {
    std::cout << "Usage: qpidd [OPTIONS]" << std::endl << std::endl
              << *this << std::endl;
}

int QpiddBroker::execute (QpiddOptions *options) {

    // If running as a service, bump the status checkpoint to let SCM know
    // we're still making progress.
    if (svcStatusHandle != 0) {
        svcStatus.dwCheckPoint++;
        ::SetServiceStatus(svcStatusHandle, &svcStatus);
    }

    // Options that affect a running daemon.
    QpiddWindowsOptions *myOptions =
        reinterpret_cast<QpiddWindowsOptions *>(options->platform.get());
    if (myOptions == 0)
        throw qpid::Exception("Internal error obtaining platform options");

    if (myOptions->service.install) {
        // Handle start type
        DWORD startType;
        if (myOptions->service.startType.compare("demand") == 0)
            startType = SERVICE_DEMAND_START;
        else if (myOptions->service.startType.compare("auto") == 0)
            startType = SERVICE_AUTO_START;
        else if (myOptions->service.startType.compare("disabled") == 0)
            startType = SERVICE_DISABLED;
        else if (!myOptions->service.startType.empty())
            throw qpid::Exception("Invalid service start type: " +
                                  myOptions->service.startType);

        // Install service and exit
        qpid::windows::SCM manager;
        manager.install(svcName,
                        "Apache Qpid Message Broker",
                        myOptions->service.startArgs,
                        startType,
                        myOptions->service.account,
                        myOptions->service.password,
                        myOptions->service.depends);
        return 0;
    }

    if (myOptions->service.start) {
        qpid::windows::SCM manager;
        manager.start(svcName);
        return 0;
    }

    if (myOptions->service.stop) {
        qpid::windows::SCM manager;
        manager.stop(svcName);
        return 0;
    }

    if (myOptions->service.uninstall) {
        qpid::windows::SCM manager;
        manager.uninstall(svcName);
        return 0;
    }

    if (myOptions->control.check || myOptions->control.quit) {
        // Relies on port number being set via --port or QPID_PORT env variable.
        NamedSharedMemory<BrokerInfo> info(brokerInfoName(options->broker.port));
        int pid = info.get().pid;
        if (pid < 0) 
            return 1;
        if (myOptions->control.check)
            std::cout << pid << std::endl;
        if (myOptions->control.quit) {
            ShutdownEvent shutter(options->broker.port);
            shutter.open();
            shutter.signal();
            HANDLE brokerHandle = ::OpenProcess(SYNCHRONIZE, false, pid);
            QPID_WINDOWS_CHECK_NULL(brokerHandle);
            ::WaitForSingleObject(brokerHandle, INFINITE);
            ::CloseHandle(brokerHandle);
        }
        return 0;
    }

    boost::intrusive_ptr<Broker> brokerPtr(new Broker(options->broker));

    // Need the correct port number to use in the pid file name.
    if (options->broker.port == 0)
        options->broker.port = brokerPtr->getPort(myOptions->control.transport);

    BrokerInfo info;
    info.pid = ::GetCurrentProcessId();

    NamedSharedMemory<BrokerInfo> sharedInfo(brokerInfoName(options->broker.port));
    sharedInfo.create() = info;

    // Allow the broker to receive a shutdown request via a qpidd --quit
    // command. Note that when the broker is run as a service this operation
    // should not be allowed.
    ourPort = options->broker.port;
    ShutdownHandler waitShut(ourPort, brokerPtr);
    waitShut.create();
    qpid::sys::Thread waitThr(waitShut);   // Wait for shutdown event
    ::SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
    brokerPtr->accept();
    std::cout << options->broker.port << std::endl;

    // If running as a service, tell SCM we're up. There's still a chance
    // that store recovery will drag out the time before the broker actually
    // responds to requests, but integrating that mechanism with the SCM
    // updating is probably more work than it's worth.
    if (svcStatusHandle != 0) {
        svcStatus.dwCheckPoint = 0;
        svcStatus.dwWaitHint = 0;
        svcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
        svcStatus.dwCurrentState = SERVICE_RUNNING;
        ::SetServiceStatus(svcStatusHandle, &svcStatus);
    }

    brokerPtr->run();
    waitShut.signal();   // In case we shut down some other way
    waitThr.join();
    return 0;
}

}} // namespace qpid::broker

int main(int argc, char* argv[])
{
    // If started as a service, notify the SCM we're up. Else just run.
    // If as a service, StartServiceControlDispatcher doesn't return until
    // the service is stopped.
    SERVICE_TABLE_ENTRY dispatchTable[] =
    {
        { "", (LPSERVICE_MAIN_FUNCTION)qpid::broker::ServiceMain },
        { NULL, NULL }
    };
    if (!StartServiceCtrlDispatcher(dispatchTable)) {
        DWORD err = ::GetLastError();
        if (err == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) // Run as console
            return qpid::broker::run_broker(argc, argv);
        throw QPID_WINDOWS_ERROR(err);
    }
    return 0;
}