diff options
| author | Andrew Stitcher <astitcher@apache.org> | 2009-12-15 18:24:02 +0000 |
|---|---|---|
| committer | Andrew Stitcher <astitcher@apache.org> | 2009-12-15 18:24:02 +0000 |
| commit | faf8a3a3a9f2355ec7044144d63ef869788eebb3 (patch) | |
| tree | f8f1d22ceb1a04d44f2353fcc352fdeafeafbbad /cpp/src/qpid | |
| parent | a66973a94f41bc034d98d39028e13cbbff805b93 (diff) | |
| download | qpid-python-faf8a3a3a9f2355ec7044144d63ef869788eebb3.tar.gz | |
QPID-1951: Removed need for Windows versions of ssize_t and pid_t
- Trivially removed Windows uses of ssize_t
- Rearchitected how the Windows port finds an existing qpidd to stop it
- Split Posix Lockfile functionality using pids into a new PidFile class
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@890929 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src/qpid')
| -rw-r--r-- | cpp/src/qpid/DataDir.cpp | 1 | ||||
| -rw-r--r-- | cpp/src/qpid/DataDir.h | 5 | ||||
| -rw-r--r-- | cpp/src/qpid/broker/Daemon.cpp | 14 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/LockFile.h | 29 | ||||
| -rwxr-xr-x | cpp/src/qpid/sys/posix/LockFile.cpp | 42 | ||||
| -rw-r--r-- | cpp/src/qpid/sys/posix/PidFile.h | 62 | ||||
| -rwxr-xr-x | cpp/src/qpid/sys/windows/LockFile.cpp | 25 |
7 files changed, 113 insertions, 65 deletions
diff --git a/cpp/src/qpid/DataDir.cpp b/cpp/src/qpid/DataDir.cpp index 0ee30709af..ad732052ab 100644 --- a/cpp/src/qpid/DataDir.cpp +++ b/cpp/src/qpid/DataDir.cpp @@ -22,6 +22,7 @@ #include "qpid/DataDir.h" #include "qpid/log/Statement.h" #include "qpid/sys/FileSysDir.h" +#include "qpid/sys/LockFile.h" namespace qpid { diff --git a/cpp/src/qpid/DataDir.h b/cpp/src/qpid/DataDir.h index dfdd498cbc..828299f3ba 100644 --- a/cpp/src/qpid/DataDir.h +++ b/cpp/src/qpid/DataDir.h @@ -23,11 +23,14 @@ #include <string> #include <memory> -#include "qpid/sys/LockFile.h" #include "qpid/CommonImportExport.h" namespace qpid { + namespace sys { + class LockFile; + } + /** * DataDir class. */ diff --git a/cpp/src/qpid/broker/Daemon.cpp b/cpp/src/qpid/broker/Daemon.cpp index e1d400e01b..b30e5f18cb 100644 --- a/cpp/src/qpid/broker/Daemon.cpp +++ b/cpp/src/qpid/broker/Daemon.cpp @@ -15,10 +15,16 @@ * limitations under the License. * */ + +/* + * TODO: Note this is really a Posix specific implementation and so should be + * refactored together with windows/QpiddBroker into a more coherent daemon driver/ + * platform specific split + */ #include "qpid/broker/Daemon.h" #include "qpid/log/Statement.h" #include "qpid/Exception.h" -#include "qpid/sys/LockFile.h" +#include "qpid/sys/posix/PidFile.h" #include <errno.h> #include <fcntl.h> @@ -31,7 +37,7 @@ namespace qpid { namespace broker { using namespace std; -using qpid::sys::LockFile; +using qpid::sys::PidFile; Daemon::Daemon(std::string _pidDir) : pidDir(_pidDir) { struct stat s; @@ -176,7 +182,7 @@ uint16_t Daemon::wait(int timeout) { // parent waits for child. */ void Daemon::ready(uint16_t port) { // child lockFile = pidFile(pidDir, port); - LockFile lf(lockFile, true); + PidFile lf(lockFile, true); /* * Write the PID to the lockfile. @@ -200,7 +206,7 @@ void Daemon::ready(uint16_t port) { // child */ pid_t Daemon::getPid(string _pidDir, uint16_t port) { string name = pidFile(_pidDir, port); - LockFile lf(name, false); + PidFile lf(name, false); pid_t pid = lf.readPid(); if (kill(pid, 0) < 0 && errno != EPERM) { unlink(name.c_str()); diff --git a/cpp/src/qpid/sys/LockFile.h b/cpp/src/qpid/sys/LockFile.h index 1f0a9e13b3..14a76cbf3e 100644 --- a/cpp/src/qpid/sys/LockFile.h +++ b/cpp/src/qpid/sys/LockFile.h @@ -29,7 +29,7 @@ namespace qpid { namespace sys { -class LockFilePrivate; +class LockFilePrivate; /** * @class LockFile @@ -43,34 +43,17 @@ class LockFilePrivate; */ class LockFile : private boost::noncopyable { - boost::shared_ptr<LockFilePrivate> impl; - std::string path; bool created; + boost::shared_ptr<LockFilePrivate> impl; + +protected: + int read(void*, size_t) const; + int write(void*, size_t) const; public: QPID_COMMON_EXTERN LockFile(const std::string& path_, bool create); QPID_COMMON_EXTERN ~LockFile(); - - /** - * Read the process ID from the lock file. This method assumes that - * if there is a process ID in the file, it was written there by - * writePid(); thus, it's at the start of the file. - * - * Throws an exception if there is an error reading the file. - * - * @returns The stored process ID. No validity check is done on it. - */ - QPID_COMMON_EXTERN pid_t readPid(void) const; - - /** - * Write the current process's ID to the lock file. It's written at - * the start of the file and will overwrite any other content that - * may be in the file. - * - * Throws an exception if the write fails. - */ - QPID_COMMON_EXTERN void writePid(void); }; }} /* namespace qpid::sys */ diff --git a/cpp/src/qpid/sys/posix/LockFile.cpp b/cpp/src/qpid/sys/posix/LockFile.cpp index 4900252984..1862ff6ac9 100755 --- a/cpp/src/qpid/sys/posix/LockFile.cpp +++ b/cpp/src/qpid/sys/posix/LockFile.cpp @@ -17,6 +17,7 @@ */ #include "qpid/sys/LockFile.h" +#include "qpid/sys/posix/PidFile.h" #include <string> #include <unistd.h> @@ -31,6 +32,7 @@ namespace sys { class LockFilePrivate { friend class LockFile; + friend class PidFile; int fd; @@ -64,27 +66,43 @@ LockFile::~LockFile() { } } -pid_t LockFile::readPid(void) const { +int LockFile::read(void* bytes, size_t len) const { if (!impl) - throw Exception("Lock file not open"); + throw Exception("Lock file not open: " + path); - pid_t pid; - int desired_read = sizeof(pid_t); - if (desired_read > ::read(impl->fd, &pid, desired_read) ) { - throw Exception("Cannot read lock file " + path); + ssize_t rc = ::read(impl->fd, bytes, len); + if ((ssize_t)len > rc) { + throw Exception("Cannot read lock file: " + path); } - return pid; + return rc; } -void LockFile::writePid(void) { +int LockFile::write(void* bytes, size_t len) const { if (!impl) - throw Exception("Lock file not open"); + throw Exception("Lock file not open: " + path); + + ssize_t rc = ::write(impl->fd, bytes, len); + if ((ssize_t)len > rc) { + throw Exception("Cannot write lock file: " + path); + } + return rc; +} + +PidFile::PidFile(const std::string& path_, bool create): + LockFile(path_, create) +{} +pid_t PidFile::readPid(void) const { + pid_t pid; + int desired_read = sizeof(pid_t); + read(&pid, desired_read); + return pid; +} + +void PidFile::writePid(void) { pid_t pid = getpid(); int desired_write = sizeof(pid_t); - if (desired_write > ::write(impl->fd, &pid, desired_write)) { - throw Exception("Cannot write lock file " + path); - } + write(&pid, desired_write); } }} /* namespace qpid::sys */ diff --git a/cpp/src/qpid/sys/posix/PidFile.h b/cpp/src/qpid/sys/posix/PidFile.h new file mode 100644 index 0000000000..fb19d407f4 --- /dev/null +++ b/cpp/src/qpid/sys/posix/PidFile.h @@ -0,0 +1,62 @@ +#ifndef _sys_PidFile_h +#define _sys_PidFile_h + +/* + * + * Copyright (c) 2008 The Apache Software Foundation + * + * Licensed 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 "qpid/sys/LockFile.h" + +#include "qpid/CommonImportExport.h" +#include "qpid/sys/IntegerTypes.h" + +#include <boost/noncopyable.hpp> +#include <boost/shared_ptr.hpp> +#include <string> + +namespace qpid { +namespace sys { + +class PidFile : public LockFile +{ +public: + QPID_COMMON_EXTERN PidFile(const std::string& path_, bool create); + + /** + * Read the process ID from the lock file. This method assumes that + * if there is a process ID in the file, it was written there by + * writePid(); thus, it's at the start of the file. + * + * Throws an exception if there is an error reading the file. + * + * @returns The stored process ID. No validity check is done on it. + */ + QPID_COMMON_EXTERN pid_t readPid(void) const; + + /** + * Write the current process's ID to the lock file. It's written at + * the start of the file and will overwrite any other content that + * may be in the file. + * + * Throws an exception if the write fails. + */ + QPID_COMMON_EXTERN void writePid(void); +}; + +}} /* namespace qpid::sys */ + +#endif /*!_sys_PidFile_h*/ diff --git a/cpp/src/qpid/sys/windows/LockFile.cpp b/cpp/src/qpid/sys/windows/LockFile.cpp index e9079b6094..e9fe01ca72 100755 --- a/cpp/src/qpid/sys/windows/LockFile.cpp +++ b/cpp/src/qpid/sys/windows/LockFile.cpp @@ -56,29 +56,4 @@ LockFile::~LockFile() { } } -pid_t LockFile::readPid(void) const { - if (!impl) - throw Exception("Lock file not open"); - - pid_t pid; - DWORD desired_read = sizeof(pid_t); - DWORD actual_read = 0; - if (!ReadFile(impl->fd, &pid, desired_read, &actual_read, 0)) { - throw Exception("Cannot read lock file " + path); - } - return pid; -} - -void LockFile::writePid(void) { - if (!impl) - throw Exception("Lock file not open"); - - pid_t pid = GetCurrentProcessId(); - DWORD desired_write = sizeof(pid_t); - DWORD written = 0; - if (!WriteFile(impl->fd, &pid, desired_write, &written, 0)) { - throw Exception("Cannot write lock file " + path); - } -} - }} /* namespace qpid::sys */ |
