diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-18 00:21:05 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-11-18 00:21:05 +0000 |
commit | 15a7b37e96bd00e1e6fd8b031f8bcafd4d769735 (patch) | |
tree | fb7b9a4706df75d770c018a8016d85fe9367def0 /base | |
parent | 6dce8adece39efbb9ae73aa12ca82d88d98da577 (diff) | |
download | chromium_src-15a7b37e96bd00e1e6fd8b031f8bcafd4d769735.zip chromium_src-15a7b37e96bd00e1e6fd8b031f8bcafd4d769735.tar.gz chromium_src-15a7b37e96bd00e1e6fd8b031f8bcafd4d769735.tar.bz2 |
Check ftruncate result
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@5586 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/shared_memory_posix.cc | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/base/shared_memory_posix.cc b/base/shared_memory_posix.cc index 5f0c05c..579d916 100644 --- a/base/shared_memory_posix.cc +++ b/base/shared_memory_posix.cc @@ -60,36 +60,40 @@ bool SharedMemory::Create(const std::wstring &name, bool read_only, posix_flags |= read_only ? O_RDONLY : O_RDWR; if (!open_existing || mapped_file_ <= 0) posix_flags |= O_CREAT; - + if (CreateOrOpen(name, posix_flags)) { - ftruncate(mapped_file_, size); - max_size_ = size; - return true; + if (0 == ftruncate(mapped_file_, size)) { + max_size_ = size; + return true; + } else { + Close(); + return false; + } } - + return false; } bool SharedMemory::Open(const std::wstring &name, bool read_only) { read_only_ = read_only; - + int posix_flags = 0; posix_flags |= read_only ? O_RDONLY : O_RDWR; - + return CreateOrOpen(name, posix_flags); } bool SharedMemory::CreateOrOpen(const std::wstring &name, int posix_flags) { DCHECK(mapped_file_ == -1); - + name_ = L"/" + name; - + mode_t posix_mode = S_IRUSR | S_IWUSR; // owner read/write std::string posix_name(WideToUTF8(name_)); mapped_file_ = shm_open(posix_name.c_str(), posix_flags, posix_mode); if (mapped_file_ < 0) return false; - + posix_name += kSemaphoreSuffix; lock_ = sem_open(posix_name.c_str(), O_CREAT, posix_mode, 1); if (lock_ == SEM_FAILED) { @@ -99,14 +103,14 @@ bool SharedMemory::CreateOrOpen(const std::wstring &name, int posix_flags) { lock_ = NULL; return false; } - + return true; } bool SharedMemory::Map(size_t bytes) { if (mapped_file_ == -1) return false; - + memory_ = mmap(NULL, bytes, PROT_READ | (read_only_ ? 0 : PROT_WRITE), MAP_SHARED, mapped_file_, 0); @@ -150,7 +154,7 @@ void SharedMemory::Close() { shm_unlink(posix_name.c_str()); mapped_file_ = -1; } - + if (lock_) { posix_name += kSemaphoreSuffix; sem_unlink(posix_name.c_str()); |