diff options
author | Alan Conway <aconway@apache.org> | 2015-05-20 12:59:46 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2015-05-20 12:59:46 +0000 |
commit | 525878991d3b85858b37edbf9916eaed5d31835e (patch) | |
tree | 3f84f94613b5f7466edfda9bc8e6aa07525e977b | |
parent | f71f9dfdca4372648b896c0b9b51a78f4ce61b5d (diff) | |
download | qpid-python-525878991d3b85858b37edbf9916eaed5d31835e.tar.gz |
QPID-6548: SYSV init scripts do not work properly wiht SSL-only broker.
Previously the broker was writing a PID file with the port number as a suffix.
This was confusing the tools when using SSL and no explicit port, as the actual
listening port is 5671 but qpidd -c was looking for 5672.
This commit introduces a simple --pidfile option which writes the pid exactly
where you tell it with no frills. The original port-pidfile setup is over
complex and not really necessary, it can be deprecated at some future time.
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1680550 13f79535-47bb-0310-9956-ffa450edef68
-rwxr-xr-x | qpid/cpp/etc/qpidd.in | 11 | ||||
-rw-r--r-- | qpid/cpp/src/posix/QpiddBroker.cpp | 60 |
2 files changed, 47 insertions, 24 deletions
diff --git a/qpid/cpp/etc/qpidd.in b/qpid/cpp/etc/qpidd.in index 3d5f5424bd..411f212119 100755 --- a/qpid/cpp/etc/qpidd.in +++ b/qpid/cpp/etc/qpidd.in @@ -100,16 +100,13 @@ lock() { start() { lock echo -n $"Starting Qpid AMQP daemon: " - daemon --pidfile $pidfile --check $prog --user qpidd $QPID_BIN --config $QPID_CONFIG --daemon $QPIDD_OPTIONS --close-fd $FLOCK_FD + touch $pidfile + chown qpidd.qpidd $pidfile + [ -x /sbin/restorecon ] && /sbin/restorecon $pidfile + daemon --pidfile $pidfile --check $prog --user qpidd $QPID_BIN --config $QPID_CONFIG --daemon $QPIDD_OPTIONS --close-fd $FLOCK_FD --pidfile $pidfile RETVAL=$? echo [ $RETVAL = 0 ] && touch $lockfile - if [ $RETVAL = 0 ]; then - touch $pidfile - chown qpidd.qpidd $pidfile - [ -x /sbin/restorecon ] && /sbin/restorecon $pidfile - runuser - -s /bin/sh qpidd -c "$QPID_BIN --config $QPID_CONFIG --check > $pidfile" - fi return $RETVAL } diff --git a/qpid/cpp/src/posix/QpiddBroker.cpp b/qpid/cpp/src/posix/QpiddBroker.cpp index 9228c2d18d..7caffd570e 100644 --- a/qpid/cpp/src/posix/QpiddBroker.cpp +++ b/qpid/cpp/src/posix/QpiddBroker.cpp @@ -27,21 +27,24 @@ #include "qpid/log/Logger.h" #include <iostream> +#include <fstream> #include <signal.h> #include <unistd.h> #include <sys/utsname.h> using std::cout; using std::endl; +using std::ifstream; +using std::ofstream; 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) + : qpid::Options("Options"), + common("", QPIDD_CONF_FILE, QPIDC_CONF_FILE), + module(QPIDD_MODULE_DIR), + log(argv0) { add(common); add(module); @@ -64,6 +67,7 @@ struct DaemonOptions : public qpid::Options { std::vector<int> closeFd; int wait; std::string piddir; + std::string pidfile; std::string transport; DaemonOptions() : qpid::Options("Daemon options"), daemon(false), quit(false), kill(false), check(false), wait(600), transport(TCP) @@ -80,6 +84,7 @@ struct DaemonOptions : public qpid::Options { ("daemon,d", pure_switch(daemon), "Run as a daemon. Logs to syslog by default in this mode.") ("transport", optValue(transport, "TRANSPORT"), "The transport for which to return the port") ("pid-dir", optValue(piddir, "DIR"), "Directory where port-specific PID file is stored") + ("pidfile", optValue(pidfile, "FILE"), "File name to store the PID in daemon mode. Used as-is, no directory or suffixes added.") ("close-fd", optValue(closeFd, "FD"), "File descriptors that the daemon should close") ("wait,w", optValue(wait, "SECONDS"), "Sets the maximum wait time to initialize or shutdown the daemon. If the daemon fails to initialize/shutdown, prints an error and returns 1") ("check,c", pure_switch(check), "Prints the daemon's process ID to stdout and returns 0 if the daemon is running, otherwise returns 1") @@ -98,10 +103,10 @@ struct QpiddPosixOptions : public QpiddOptionsPrivate { }; QpiddOptions::QpiddOptions(const char* argv0) - : qpid::Options("Options"), - common("", QPIDD_CONF_FILE, QPIDC_CONF_FILE), - module(QPIDD_MODULE_DIR), - log(argv0) + : qpid::Options("Options"), + common("", QPIDD_CONF_FILE, QPIDC_CONF_FILE), + module(QPIDD_MODULE_DIR), + log(argv0) { add(common); add(module); @@ -127,11 +132,23 @@ struct ScopedSetBroker { ~ScopedSetBroker() { qpid::broker::SignalHandler::setBroker(0); } }; +namespace { + +/// Write a pid file if requested +void writePid(const std::string& filename) { + if (!filename.empty()) { + ofstream pidfile(filename.c_str()); + pidfile << ::getpid() << endl; + pidfile.close(); + } +} +} + struct QpiddDaemon : public Daemon { QpiddPosixOptions *options; QpiddDaemon(std::string pidDir, QpiddPosixOptions *opts) - : Daemon(pidDir), options(opts) {} + : Daemon(pidDir), options(opts) {} /** Code for parent process */ void parent() { @@ -153,6 +170,7 @@ struct QpiddDaemon : public Daemon { if (options->parent->broker.enableMgmt && (options->parent->broker.port == 0 || options->daemon.transport != TCP)) { boost::dynamic_pointer_cast<qmf::org::apache::qpid::broker::Broker>(brokerPtr->GetManagementObject())->set_port(port); } + writePid(options->daemon.pidfile); brokerPtr->run(); } }; @@ -160,18 +178,25 @@ struct QpiddDaemon : public Daemon { int QpiddBroker::execute (QpiddOptions *options) { // Options that affect a running daemon. QpiddPosixOptions *myOptions = - static_cast<QpiddPosixOptions *>(options->platform.get()); + static_cast<QpiddPosixOptions *>(options->platform.get()); if (myOptions == 0) throw Exception("Internal error obtaining platform options"); if (myOptions->daemon.check || myOptions->daemon.quit || myOptions->daemon.kill) { - pid_t pid; - try { - pid = Daemon::getPid(myOptions->daemon.piddir, options->broker.port); - } catch (const Exception& e) { - // This is not a critical error, usually means broker is not running - QPID_LOG(notice, "Broker is not running: " << e.what()); - return 1; + pid_t pid = 0; + if (!myOptions->daemon.pidfile.empty()) { + ifstream pidfile(myOptions->daemon.pidfile.c_str()); + pidfile >> pid; + pidfile.close(); + } + if (pid == 0) { + try { + pid = Daemon::getPid(myOptions->daemon.piddir, options->broker.port); + } catch (const Exception& e) { + // This is not a critical error, usually means broker is not running + QPID_LOG(notice, "Broker is not running: " << e.what()); + return 1; + } } if (pid < 0) return 1; @@ -211,6 +236,7 @@ int QpiddBroker::execute (QpiddOptions *options) { boost::dynamic_pointer_cast<qmf::org::apache::qpid::broker::Broker>(brokerPtr->GetManagementObject())->set_port(port); } } + writePid(myOptions->daemon.pidfile); brokerPtr->run(); } return 0; |