summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/windows/QpiddBroker.cpp
blob: 5bf9477e6a41716a2b5619b2f2896599183d54ca (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
/*
 *
 * 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.
 *
 */

#ifdef HAVE_CONFIG_H
#  include "config.h"
#else
// These need to be made something sensible, like reading a value from
// the registry. But for now, get things going with a local definition.
namespace {
const char *QPIDD_CONF_FILE = "qpid_broker.conf";
const char *QPIDD_MODULE_DIR = ".";
}
#endif
#include "qpidd.h"
#include "qpid/Exception.h"
#include "qpid/Options.h"
#include "qpid/Plugin.h"
#include "qpid/sys/IntegerTypes.h"
#include "qpid/sys/LockFile.h"
#include "qpid/sys/windows/check.h"
#include "qpid/broker/Broker.h"

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

using namespace qpid::broker;

BootstrapOptions::BootstrapOptions(const char* argv0)
  : qpid::Options("Options"),
    common("", QPIDD_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";

std::string brokerPidFile(std::string piddir, uint16_t port)
{
    std::ostringstream path;
    path << piddir << "\\broker_" << port << ".pid";
    return path.str();
}

// 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(pid_t other = 0);
    ~ShutdownEvent();

    void signal();

  protected:
    std::string eventName(pid_t pid);
    HANDLE event;
};

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

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

ShutdownEvent::ShutdownEvent(pid_t other) : event(NULL) {
    // If given a pid, open an event assumedly created by that pid. If there's
    // no pid, create a new event using the current process id.
    if (other == 0) {
         std::string name = eventName(GetCurrentProcessId());
        // 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, name.c_str());
    }
    else {
        std::string name = eventName(other);
        event = OpenEvent(EVENT_MODIFY_STATE, false, name.c_str());
    }
    QPID_WINDOWS_CHECK_NULL(event);
}

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

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

std::string ShutdownEvent::eventName(pid_t pid) {
    std::ostringstream name;
    name << "qpidd_" << pid << std::ends;
    return name.str();
}


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.
BOOL CtrlHandler(DWORD ctl)
{
    ShutdownEvent shutter;     // no pid specified == shut me down
    shutter.signal();
    return ((ctl == CTRL_C_EVENT || ctl == CTRL_CLOSE_EVENT) ? TRUE : FALSE);
}

}

struct ProcessControlOptions : public qpid::Options {
    bool quit;
    bool check;
    std::string piddir;
    //std::string transport;   No transport options yet - TCP is it.

    ProcessControlOptions()
        : qpid::Options("Process control options"),
          quit(false),
          check(false) //, transport(TCP)
    {
        const DWORD pathLen = MAX_PATH + 1;
        char tempDir[pathLen];
        if (GetTempPath(pathLen, tempDir) == 0)
            piddir = "C:\\WINDOWS\\TEMP\\";
        else
            piddir = tempDir;
        piddir += "qpidd";

        // Only have TCP for now, so don't need this...
        //            ("transport", optValue(transport, "TRANSPORT"), "The transport for which to return the port")
        addOptions()
            ("pid-dir", qpid::optValue(piddir, "DIR"), "Directory where port-specific PID file is stored")
            ("check,c", qpid::optValue(check), "Prints the broker's process ID to stdout and returns 0 if the broker is running, otherwise returns 1")
            ("quit,q", qpid::optValue(quit), "Tells the broker to shut down");
    }
};

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

QpiddOptions::QpiddOptions(const char* argv0)
  : qpid::Options("Options"),
    common("", QPIDD_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) {
    // 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->control.check || myOptions->control.quit) {
        // Relies on port number being set via --port or QPID_PORT env variable.
        qpid::sys::LockFile getPid (brokerPidFile(myOptions->control.piddir,
                                                  options->broker.port),
                                    false);
        pid_t pid = getPid.readPid();
        if (pid < 0) 
            return 1;
        if (myOptions->control.check)
            std::cout << pid << std::endl;
        if (myOptions->control.quit) {
            ShutdownEvent shutter(pid);
            HANDLE brokerHandle = OpenProcess(SYNCHRONIZE, false, pid);
            QPID_WINDOWS_CHECK_NULL(brokerHandle);
            shutter.signal();
            WaitForSingleObject(brokerHandle, INFINITE);
            CloseHandle(brokerHandle);
        }
        return 0;
    }

    boost::intrusive_ptr<Broker> brokerPtr(new Broker(options->broker));
    if (options->broker.port == 0)
        options->broker.port = brokerPtr->getPort("");
    std::cout << options->broker.port << std::endl;

    // Make sure the pid directory exists, creating if needed. LockFile
    // will throw an exception that makes little sense if it can't create
    // the file.
    if (!CreateDirectory(myOptions->control.piddir.c_str(), 0)) {
        DWORD err = GetLastError();
        if (err != ERROR_ALREADY_EXISTS)
            throw qpid::Exception(QPID_MSG("Can't create pid-dir " +
                                           myOptions->control.piddir +
                                           ": " +
                                           qpid::sys::strError(err)));
    }
    qpid::sys::LockFile myPid(brokerPidFile(myOptions->control.piddir,
                                            options->broker.port),
                              true);
    myPid.writePid();

    // 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.

    ShutdownHandler waitShut(brokerPtr);
    qpid::sys::Thread waitThr(waitShut);   // Wait for shutdown event
    SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
    brokerPtr->run();
    waitShut.signal();   // In case we shut down some other way
    waitThr.join();
    return 0;
}