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
|
/*
* Phusion Passenger - http://www.modrails.com/
* Copyright (c) 2008, 2009 Phusion
*
* "Phusion Passenger" is a trademark of Hongli Lai & Ninh Bui.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef _PASSENGER_APPLICATION_POOL_STATUS_REPORTER_H_
#define _PASSENGER_APPLICATION_POOL_STATUS_REPORTER_H_
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <oxt/thread.hpp>
#include <oxt/backtrace.hpp>
#include <oxt/system_calls.hpp>
#include <string>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/stat.h>
#include <cstdio>
#include <unistd.h>
#include <errno.h>
#include "StandardApplicationPool.h"
#include "MessageChannel.h"
#include "Exceptions.h"
#include "Logging.h"
#include "Utils.h"
namespace Passenger {
using namespace boost;
using namespace oxt;
using namespace std;
/**
* An ApplicationPoolStatusReporter allows commandline admin tools to inspect
* the status of a StandardApplicationPool. It does so by creating a Unix socket
* in the Passenger temp folder, which tools can connect to to query for
* information.
*
* An ApplicationPoolStatusReporter creates a background thread for handling
* connections on the socket. This thread will be automatically cleaned up upon
* destroying the ApplicationPoolStatusReporter object.
*/
class ApplicationPoolStatusReporter {
private:
/**
* Wrapper class around a file descriptor integer, for RAII behavior.
*
* A FileDescriptor object behaves just like an int, so that you can pass it to
* system calls such as read(). It performs reference counting. When the last
* copy of a FileDescriptor has been destroyed, the underlying file descriptor
* will be automatically closed.
*/
class FileDescriptor {
private:
struct SharedData {
int fd;
/**
* Constructor to assign this file descriptor's handle.
*/
SharedData(int fd) {
this->fd = fd;
}
/**
* Attempts to close this file descriptor. When created on the stack,
* this destructor will automatically be invoked as a result of C++
* semantics when exiting the scope this object was created in. This
* ensures that stack created objects with destructors like these will
* de-allocate their resources upon leaving their corresponding scope.
* This pattern is also known Resource Acquisition Is Initialization (RAII).
*
* @throws SystemException File descriptor could not be closed.
*/
~SharedData() {
this_thread::disable_syscall_interruption dsi;
if (syscalls::close(fd) == -1) {
throw SystemException("Cannot close file descriptor", errno);
}
}
};
/* Shared pointer for reference counting on this file descriptor */
shared_ptr<SharedData> data;
public:
FileDescriptor() {
// Do nothing.
}
/**
* Creates a new FileDescriptor instance with the given fd as a handle.
*/
FileDescriptor(int fd) {
data = ptr(new SharedData(fd));
}
/**
* Overloads the integer cast operator so that it will return the file
* descriptor handle as an integer.
*
* @return This file descriptor's handle as an integer.
*/
operator int () const {
return data->fd;
}
};
/** The application pool to monitor. */
StandardApplicationPoolPtr pool;
/** The socket's filename. */
char filename[PATH_MAX];
/** The socket's file descriptor. */
int serverFd;
/** The main thread. */
oxt::thread *mainThread;
/** The mutex which protects the 'threads' member. */
boost::mutex threadsLock;
/** A map which maps a client file descriptor to its handling thread. */
map< int, shared_ptr<oxt::thread> > threads;
void writeScalarAndIgnoreErrors(MessageChannel &channel, const string &data) {
try {
channel.writeScalar(data);
} catch (const SystemException &e) {
// Don't care about write errors.
}
}
void mainThreadFunction() {
TRACE_POINT();
try {
while (!this_thread::interruption_requested()) {
UPDATE_TRACE_POINT();
sockaddr_un addr;
socklen_t addr_len = sizeof(addr);
FileDescriptor fd(syscalls::accept(serverFd, (struct sockaddr *) &addr, &addr_len));
if (fd == -1) {
int e = errno;
P_ERROR("Cannot accept new client on status reporter socket: " <<
strerror(e) << " (" << e << ")");
break;
}
boost::lock_guard<boost::mutex> l(threadsLock);
this_thread::disable_syscall_interruption dsi;
this_thread::disable_interruption di;
shared_ptr<oxt::thread> thread(new oxt::thread(
bind(&ApplicationPoolStatusReporter::clientThreadFunction, this, fd),
"Status reporter client thread " + toString(fd),
1024 * 128
));
threads[fd] = thread;
}
} catch (const boost::thread_interrupted &) {
P_TRACE(2, "Status reporter main thread interrupted.");
} catch (const exception &e) {
P_ERROR("Error in status reporter main thread: " << e.what());
}
}
void clientThreadFunction(FileDescriptor fd) {
TRACE_POINT();
MessageChannel channel(fd);
try {
while (!this_thread::interruption_requested()) {
vector<string> args;
UPDATE_TRACE_POINT();
if (!channel.read(args) || args.size() < 1) {
break;
}
if (args[0] == "backtraces") {
UPDATE_TRACE_POINT();
writeScalarAndIgnoreErrors(channel, oxt::thread::all_backtraces());
} else if (args[0] == "status") {
UPDATE_TRACE_POINT();
writeScalarAndIgnoreErrors(channel, pool->toString());
} else if (args[0] == "status_xml") {
UPDATE_TRACE_POINT();
writeScalarAndIgnoreErrors(channel, pool->toXml());
} else {
P_ERROR("Error in status reporter client thread: unknown query '" <<
args[0] << "'.");
}
}
} catch (const boost::thread_interrupted &) {
P_TRACE(2, "Status reporter client thread " << fd << " interrupted.");
} catch (const exception &e) {
P_ERROR("Error in status reporter client thread: " << e.what());
}
boost::lock_guard<boost::mutex> l(threadsLock);
this_thread::disable_syscall_interruption dsi;
this_thread::disable_interruption di;
threads.erase(fd);
}
public:
/**
* Creates a new ApplicationPoolStatusReporter.
*
* @param pool The application pool to monitor.
* @param userSwitching Whether user switching is enabled. This is used
* for determining the optimal permissions for the
* FIFO file and the temp directory that might get
* created.
* @param permissions The permissions with which the FIFO should
* be created.
* @param uid The UID of the user who should own the FIFO file, or
* -1 if the current user should be set as owner.
* @param gid The GID of the user who should own the FIFO file, or
* -1 if the current group should be set as group.
* @throws RuntimeException An error occurred.
* @throws SystemException An error occurred while creating the server socket.
* @throws boost::thread_resource_error Something went wrong during
* creation of the thread.
* @throws boost::thread_interrupted A system call has been interrupted.
*/
ApplicationPoolStatusReporter(StandardApplicationPoolPtr &pool,
bool userSwitching,
mode_t permissions = S_IRUSR | S_IWUSR,
uid_t uid = -1, gid_t gid = -1) {
int ret;
this->pool = pool;
createPassengerTempDir(getSystemTempDir(), userSwitching,
"nobody", geteuid(), getegid());
snprintf(filename, sizeof(filename) - 1, "%s/info/status.socket",
getPassengerTempDir().c_str());
filename[PATH_MAX - 1] = '\0';
serverFd = createUnixServer(filename, 10);
/* Set the socket file's permissions... */
do {
ret = chmod(filename, permissions);
} while (ret == -1 && errno == EINTR);
/* ...and ownership. */
if (uid != (uid_t) -1 && gid != (gid_t) -1) {
do {
ret = chown(filename, uid, gid);
} while (ret == -1 && errno == EINTR);
if (errno == -1) {
int e = errno;
char message[1024];
snprintf(message, sizeof(message) - 1,
"Cannot set the owner for socket file '%s' to %lld and its group to %lld",
filename, (long long) uid, (long long) gid);
message[sizeof(message) - 1] = '\0';
do {
ret = close(serverFd);
} while (ret == -1 && errno == EINTR);
throw SystemException(message, e);
}
}
try {
mainThread = new oxt::thread(
bind(&ApplicationPoolStatusReporter::mainThreadFunction, this),
"Status reporter main thread",
1024 * 128
);
} catch (...) {
do {
ret = close(serverFd);
} while (ret == -1 && errno == EINTR);
throw;
}
}
~ApplicationPoolStatusReporter() {
this_thread::disable_syscall_interruption dsi;
this_thread::disable_interruption di;
int ret;
do {
ret = unlink(filename);
} while (ret == -1 && errno == EINTR);
mainThread->interrupt_and_join();
delete mainThread;
do {
ret = close(serverFd);
} while (ret == -1 && errno == EINTR);
/* We make a copy of the data structure here to avoid deadlocks. */
map< int, shared_ptr<oxt::thread> > threadsCopy;
{
boost::lock_guard<boost::mutex> l(threadsLock);
threadsCopy = threads;
}
map< int, shared_ptr<oxt::thread> >::iterator it;
for (it = threadsCopy.begin(); it != threadsCopy.end(); it++) {
it->second->interrupt_and_join();
}
}
};
} // namespace Passenger
#endif /* _PASSENGER_APPLICATION_POOL_STATUS_REPORTER_H_ */
|