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/sys | |
| 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/sys')
| -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 |
4 files changed, 98 insertions, 60 deletions
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 */ |
